From eebea73a935cd98d7592c8d5d9151cb75c558dbd Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 11 Nov 2026 22:19:43 -0500 Subject: [PATCH] fix(setup): state what a download or a repair of the install medium really fetches and writes: the boot files and only the pieces the USB lacks --- crates/stela-cli/src/setup/create.rs | 10 ++- crates/stela-cli/src/setup/medium.rs | 17 +++++ crates/stela-cli/src/setup/sources.rs | 98 +++++++++++++++++---------- crates/stela-cli/src/setup/tests.rs | 39 +++++++++++ 4 files changed, 126 insertions(+), 38 deletions(-) diff --git a/crates/stela-cli/src/setup/create.rs b/crates/stela-cli/src/setup/create.rs index ae93e868b1..bb6b16c24d 100644 --- a/crates/stela-cli/src/setup/create.rs +++ b/crates/stela-cli/src/setup/create.rs @@ -61,8 +61,11 @@ pub(super) struct MediumPlan { /// It names the IPSW on Apple's servers (`medium::write_remote`). pub(super) remote: bool, pub(super) ipsw_bytes: u64, - /// Bytes still to write, with the hash files and a margin. + /// Room it needs: the bytes still to write, less a damaged piece's + /// that goes first, with the hash files and a margin. pub(super) need: u64, + /// The bytes of the pieces still to write. + pub(super) write: u64, pub(super) expected: medium::Expected, /// Directories it creates, deepest first. pub(super) created: Vec, @@ -143,6 +146,7 @@ pub(super) fn plan_install( remote, ipsw_bytes: 0, need: MARGIN, + write: 0, expected: medium::Expected::default(), created: missing_directories(&record.volume, &medium::directory(&record.volume, build)), }), @@ -154,6 +158,7 @@ pub(super) fn plan_install( None => fs::metadata(ipsw)?.len(), }; let need = medium::needed_bytes(&volume, build, ipsw_bytes, environment.piece_bytes); + let write = medium::missing_bytes(&volume, build, ipsw_bytes, environment.piece_bytes); let created = missing_directories(&volume, &medium::directory(&volume, build)); Some(MediumPlan { volume, @@ -161,6 +166,7 @@ pub(super) fn plan_install( remote, ipsw_bytes, need, + write, expected: match environment.download { Some(download) => medium::Expected { sha256: None, @@ -250,7 +256,7 @@ pub(super) fn describe_install( medium::directory(&medium.volume, &plan.build).display(), plan.build, medium.guid, - host::bytes_text(medium.need) + host::bytes_text(medium.write) )?; if medium.expected.sha1.is_none() { writeln!( diff --git a/crates/stela-cli/src/setup/medium.rs b/crates/stela-cli/src/setup/medium.rs index d3435956be..64040c608a 100644 --- a/crates/stela-cli/src/setup/medium.rs +++ b/crates/stela-cli/src/setup/medium.rs @@ -255,6 +255,23 @@ pub(super) fn needed_bytes(volume: &Path, build: &str, ipsw_bytes: u64, piece_by need } +/// The bytes of the IPSW's pieces not on `volume` at their size: what a +/// download onto it fetches, the intact pieces being kept. +pub(super) fn missing_bytes(volume: &Path, build: &str, ipsw_bytes: u64, piece_bytes: u64) -> u64 { + let directory = directory(volume, build); + piece_sizes(ipsw_bytes, piece_bytes) + .into_iter() + .enumerate() + .filter(|(index, size)| { + fs::metadata(directory.join(piece_name(*index))) + .map(|m| m.len()) + .ok() + != Some(*size) + }) + .map(|(_, size)| size) + .sum() +} + /// Volume and piece layout whose required capacity is checked. pub(super) struct SpaceOptions<'a> { pub(super) volume: &'a Path, diff --git a/crates/stela-cli/src/setup/sources.rs b/crates/stela-cli/src/setup/sources.rs index 0aa2437dc2..2c887c0f1f 100644 --- a/crates/stela-cli/src/setup/sources.rs +++ b/crates/stela-cli/src/setup/sources.rs @@ -72,8 +72,10 @@ pub(super) enum Source { Kept(PathBuf), /// The USB's full install medium of the build. Medium(medium::Medium), - /// Apple's servers: `bytes` to download, of the image's `whole`. - Remote { bytes: u64, whole: u64 }, + /// Apple's servers: the image's boot files, `boot` bytes read by range, + /// and for a whole image the `pieces` bytes of it the USB lacks, of the + /// image's `whole`. + Remote { boot: u64, pieces: u64, whole: u64 }, } impl Source { @@ -198,9 +200,19 @@ pub(super) fn locate(environment: &Environment<'_>, lookup: &Lookup<'_>) -> Resu }; match (environment.image_size)(lookup.build, lookup.model) { Ok((boot, whole)) => Ok(Source::Remote { - bytes: match lookup.need { - Need::BootFiles => boot, - Need::WholeImage => whole, + boot, + // A damaged copy of the build keeps its intact pieces. + pieces: match lookup.need { + Need::BootFiles => 0, + Need::WholeImage => match lookup.medium { + Some(m) if m.build == lookup.build => medium::missing_bytes( + &m.volume, + lookup.build, + whole, + environment.piece_bytes, + ), + _ => whole, + }, }, whole, }), @@ -232,20 +244,45 @@ pub(super) fn describe(source: &Source, lookup: &Lookup<'_>) -> String { "from this USB's copy of macOS {build} in {}; nothing is downloaded", medium.directory().display() ), - Source::Remote { bytes, whole } => match lookup.need { - Need::BootFiles => format!( - "download {} of the macOS {build} image's {} from Apple, its boot files: no \ - copy of it is on this USB or this computer (pass --ipsw FILE to use one you \ - have)", - bytes_text(*bytes), - bytes_text(*whole) - ), - Need::WholeImage => format!( - "download the whole macOS {build} image, {}, from Apple straight onto the USB: \ - no copy of it is on this computer (pass --ipsw FILE to use one you have)", - bytes_text(*whole) - ), - }, + Source::Remote { .. } => format!( + "download {}: no copy of it is on this {}computer (pass --ipsw FILE to use one you \ + have)", + remote_text(source, build), + match lookup.need { + Need::BootFiles => "USB or this ", + Need::WholeImage => "", + } + ), + } +} + +/// What a [`Source::Remote`] downloads of `build`'s image, with the sizes. +fn remote_text(source: &Source, build: &str) -> String { + let Source::Remote { + boot, + pieces, + whole, + } = *source + else { + return String::new(); + }; + let total = bytes_text(boot + pieces); + match pieces { + 0 => format!( + "{total} of the macOS {build} image's {} from Apple: its boot files, read by range", + bytes_text(whole) + ), + _ if pieces == whole => format!( + "{total} from Apple: the macOS {build} image's boot files, read by range, then the \ + whole image, {}, straight onto the USB", + bytes_text(whole) + ), + _ => format!( + "{total} from Apple: the macOS {build} image's boot files, read by range, then the \ + {} of it this USB's copy lacks, straight onto the USB; the pieces it holds are \ + checked and kept", + bytes_text(pieces) + ), } } @@ -307,23 +344,12 @@ pub(super) fn obtain( _temporary: Some(temporary), }) } - Source::Remote { bytes, whole } => { - match lookup.need { - Need::BootFiles => eprintln!( - "Downloading {} of the macOS {} image's {}: its boot files, read by range. \ - No copy of the image is on this USB or this computer; pass --ipsw FILE to \ - use one you have", - bytes_text(*bytes), - lookup.build, - bytes_text(*whole) - ), - Need::WholeImage => eprintln!( - "Downloading the macOS {} image, {}, straight onto the USB. No copy of it is \ - on this computer; pass --ipsw FILE to use one you have", - lookup.build, - bytes_text(*whole) - ), - } + Source::Remote { .. } => { + eprintln!( + "Downloading {}. No copy of it is on this computer; pass --ipsw FILE to use one \ + you have", + remote_text(source, lookup.build) + ); let directory = lookup.download_dir(environment); let (directory, temporary) = match lookup.need { // Kept, so a later command finds it. diff --git a/crates/stela-cli/src/setup/tests.rs b/crates/stela-cli/src/setup/tests.rs index 66aa9244cb..650f5b61b6 100644 --- a/crates/stela-cli/src/setup/tests.rs +++ b/crates/stela-cli/src/setup/tests.rs @@ -2633,6 +2633,45 @@ fn os_update_checks_everything_before_any_download() { assert_eq!((snapshot(&f.esp), snapshot(&stela_volume(&f))), before); } +#[test] +fn repair_of_a_short_piece_downloads_the_boot_files_and_that_piece_saying_so() { + let mut f = fixture(64 * GIB, false); + add_stela(&mut f); + setup(&environment(&f.host), command(&f, &f.esp)).unwrap(); + let medium = record(&f).active_medium().cloned().unwrap(); + let first = medium + .directory() + .join(stela_formats::payload::piece_name(0)); + let piece = fs::read(&first).unwrap(); + fs::write(&first, &piece[1..]).unwrap(); + let whole = fs::metadata(&f.ipsw).unwrap().len(); + let boot = 2_200_000_000; + let size = |_: &str, _: &str| -> Result<(u64, u64)> { Ok((boot, whole)) }; + let environment = Environment { + image_size: &size, + ..environment(&f.host) + }; + let repair = |ipsw: Option<&Path>| { + let mut request = update_request(&f, &f._dir.path().join("cache")); + request.action = Action::Repair; + request.dry_run = true; + request.ipsw = ipsw.map(Path::to_path_buf); + request.dispatch(&environment).unwrap() + }; + let plan = repair(None); + let total = host::bytes_text(boot + piece.len() as u64); + assert!( + plan.contains(&format!("download {total} from Apple")), + "{plan}" + ); + assert!(plan.contains("this USB's copy lacks"), "{plan}"); + assert!(plan.contains("checked and kept"), "{plan}"); + // From an image on this computer it writes that piece alone. + let plan = repair(Some(&f.ipsw)); + let piece_text = host::bytes_text(piece.len() as u64); + assert!(plan.contains(&format!("({piece_text} to write)")), "{plan}"); +} + /// A full-installer setup whose USB has room for one copy of macOS only, /// and the newer build's image. fn one_copy_setup() -> (Fixture, PathBuf, u64) {