diff --git a/crates/stela-cli/src/restore/payload.rs b/crates/stela-cli/src/restore/payload.rs index 153ff0cf6b..fc02007e81 100644 --- a/crates/stela-cli/src/restore/payload.rs +++ b/crates/stela-cli/src/restore/payload.rs @@ -731,7 +731,13 @@ pub fn remote_inventory( /// The product version and build of an IPSW's restore identity for /// `model`, from its ZIP directory and `Restore.plist` alone. pub fn identity(image: &Path, model: &str) -> Result<(String, String)> { - let mut archive = ZipArchive::new(File::open(image)?)?; + identity_of(File::open(image)?, model) +} + +/// [`identity`] of an IPSW read from `source`, a local file or one read by +/// ranges, whose directory and BuildManifest alone are read. +pub fn identity_of(source: R, model: &str) -> Result<(String, String)> { + let mut archive = ZipArchive::new(source)?; let recovery = partial(&archive); let (_, restore) = profile_manifest(&mut archive, model, recovery)?; Ok((restore.product_version, restore.product_build)) diff --git a/crates/stela-cli/src/setup.rs b/crates/stela-cli/src/setup.rs index fdc6230862..960b254949 100644 --- a/crates/stela-cli/src/setup.rs +++ b/crates/stela-cli/src/setup.rs @@ -788,10 +788,12 @@ impl Request { path: prepare::fetch_recovery(directory, build, model)?, download: None, }), - sources::Need::WholeImage => { + sources::Need::WholeImage | sources::Need::Pieces => { let image = restore::select(&restore::catalog()?, Some(build), Some(model))?; let path = directory.join("boot.ipsw"); - let download = medium::download::Download::prepare(image, model, &path)?; + // Without a boot bundle to prepare, no boot file is read. + let boot = (need == sources::Need::WholeImage).then_some(path.as_path()); + let download = medium::download::Download::prepare(image, model, boot)?; Ok(sources::Fetched { path, download: Some(download), diff --git a/crates/stela-cli/src/setup/create.rs b/crates/stela-cli/src/setup/create.rs index dde9736fbb..dd597c8be7 100644 --- a/crates/stela-cli/src/setup/create.rs +++ b/crates/stela-cli/src/setup/create.rs @@ -1083,7 +1083,7 @@ pub(super) fn setup(environment: &Environment<'_>, mut command: Request) -> Resu download = Some(medium::download::Download::prepare( image, command.model(), - &boot, + Some(&boot), )?); boot } else { diff --git a/crates/stela-cli/src/setup/doctor.rs b/crates/stela-cli/src/setup/doctor.rs index 28fd5ab4d3..b85d45308e 100644 --- a/crates/stela-cli/src/setup/doctor.rs +++ b/crates/stela-cli/src/setup/doctor.rs @@ -784,7 +784,7 @@ pub(super) fn repair(environment: &Environment<'_>, command: &Request) -> Result let source = sources::locate(environment, &lookup).map_err(|error| { anyhow::anyhow!("repair needs the macOS {build} restore image; but {error:#}") })?; - if need == sources::Need::WholeImage { + if need.whole() { sources::check_room(environment, &record, &build, &source)?; } Some(source) diff --git a/crates/stela-cli/src/setup/machines.rs b/crates/stela-cli/src/setup/machines.rs index 098d2c9392..e9cef8390a 100644 --- a/crates/stela-cli/src/setup/machines.rs +++ b/crates/stela-cli/src/setup/machines.rs @@ -261,7 +261,7 @@ pub(super) fn add(environment: &Environment<'_>, command: &Request) -> Result)> = diff --git a/crates/stela-cli/src/setup/medium/download.rs b/crates/stela-cli/src/setup/medium/download.rs index 14cbadfe20..8975be5522 100644 --- a/crates/stela-cli/src/setup/medium/download.rs +++ b/crates/stela-cli/src/setup/medium/download.rs @@ -24,10 +24,13 @@ pub(in super::super) struct Download { } impl Download { + /// Open the catalog's image on Apple's servers, pinning its size and + /// entity tag, and copy its boot files to `boot` when bundles are + /// prepared; the catalog's build is checked either way. pub(in super::super) fn prepare( image: restore::Image, model: &str, - boot: &Path, + boot: Option<&Path>, ) -> Result { ensure!( image.sha1.is_some(), @@ -39,9 +42,15 @@ impl Download { etag: source.etag().into(), image, }; - restore::payload::trim(source, model, boot, "Downloading boot files")?; + let build = match boot { + Some(boot) => { + restore::payload::trim(source, model, boot, "Downloading boot files")?; + restore::payload::identity(boot, model)?.1 + } + None => restore::payload::identity_of(source, model)?.1, + }; ensure!( - restore::payload::identity(boot, model)?.1 == download.image.build, + build == download.image.build, "the catalog download contains a different macOS build" ); Ok(download) diff --git a/crates/stela-cli/src/setup/sources.rs b/crates/stela-cli/src/setup/sources.rs index f443bf5278..0ca779258b 100644 --- a/crates/stela-cli/src/setup/sources.rs +++ b/crates/stela-cli/src/setup/sources.rs @@ -33,8 +33,19 @@ pub(super) enum Need { /// The boot files: enough to prepare boot bundles and name the image /// on Apple's servers. BootFiles, - /// The whole image, which a full install medium holds. + /// The whole image, which a full install medium holds, and its boot + /// files, to prepare boot bundles. WholeImage, + /// The whole image for a full install medium, and no boot bundle: its + /// boot files are not read. + Pieces, +} + +impl Need { + /// Whether it writes the whole image as a full install medium. + pub(super) fn whole(self) -> bool { + matches!(self, Self::WholeImage | Self::Pieces) + } } /// Whether `record` installs from a medium that streams the image from @@ -55,7 +66,10 @@ pub(super) fn medium_ready(record: &Record, build: &str) -> bool { /// install medium of `build` right needs from the image. pub(super) fn need(record: &Record, build: &str, bundles: bool) -> Option { match medium_ready(record, build) { - false if !wants_remote(record) => Some(Need::WholeImage), + false if !wants_remote(record) => Some(match bundles { + true => Need::WholeImage, + false => Need::Pieces, + }), false => Some(Need::BootFiles), true => bundles.then_some(Need::BootFiles), } @@ -155,7 +169,7 @@ impl Lookup<'_> { fn what(&self) -> &'static str { match self.need { Need::BootFiles => "its boot files", - Need::WholeImage => "the whole image", + Need::WholeImage | Need::Pieces => "the whole image", } } } @@ -200,11 +214,14 @@ pub(super) fn locate(environment: &Environment<'_>, lookup: &Lookup<'_>) -> Resu }; match (environment.image_size)(lookup.build, lookup.model) { Ok((boot, whole)) => Ok(Source::Remote { - boot, + boot: match lookup.need { + Need::Pieces => 0, + Need::BootFiles | Need::WholeImage => boot, + }, // A damaged copy of the build keeps its intact pieces. pieces: match lookup.need { Need::BootFiles => 0, - Need::WholeImage => match lookup.medium { + Need::WholeImage | Need::Pieces => match lookup.medium { Some(m) if m.build == lookup.build => medium::missing_bytes( &m.volume, lookup.build, @@ -224,7 +241,7 @@ pub(super) fn locate(environment: &Environment<'_>, lookup: &Lookup<'_>) -> Resu what = lookup.what(), only = match lookup.need { Need::BootFiles => ", which downloads only its boot files, not the whole image", - Need::WholeImage => "", + Need::WholeImage | Need::Pieces => "", }, ), } @@ -250,7 +267,7 @@ pub(super) fn describe(source: &Source, lookup: &Lookup<'_>) -> String { remote_text(source, build), match lookup.need { Need::BootFiles => "USB or this ", - Need::WholeImage => "", + Need::WholeImage | Need::Pieces => "", } ), } @@ -267,8 +284,16 @@ fn remote_text(source: &Source, build: &str) -> String { return String::new(); }; let total = bytes_text(boot + pieces); - match pieces { - 0 => format!( + match (boot, pieces) { + // No boot bundle is prepared: the image's pieces alone. + (0, _) if pieces == whole => { + format!("{total} from Apple: the whole macOS {build} image straight onto the USB") + } + (0, _) => format!( + "{total} from Apple: the pieces of the macOS {build} image this USB's copy lacks, \ + straight onto the USB; the pieces it holds are checked and kept" + ), + (_, 0) => format!( "{total} of the macOS {build} image's {} from Apple: its boot files, read by range", bytes_text(whole) ), @@ -355,7 +380,7 @@ pub(super) fn obtain( // Kept, so a later command finds it. Need::BootFiles => (directory, None), // Only its boot files land here, until the USB holds them. - Need::WholeImage => { + Need::WholeImage | Need::Pieces => { let temporary = temporary_in(&directory)?; (temporary.path().to_path_buf(), Some(temporary)) } diff --git a/crates/stela-cli/src/setup/tests.rs b/crates/stela-cli/src/setup/tests.rs index 9ad94e0b0b..345907ffc8 100644 --- a/crates/stela-cli/src/setup/tests.rs +++ b/crates/stela-cli/src/setup/tests.rs @@ -2669,7 +2669,7 @@ fn a_new_setup_takes_an_image_this_computer_holds_instead_of_downloading() { } #[test] -fn repair_of_a_short_piece_downloads_the_boot_files_and_that_piece_saying_so() { +fn repair_of_a_short_piece_downloads_that_piece_alone_saying_so() { let mut f = fixture(64 * GIB, false); add_stela(&mut f); setup(&environment(&f.host), command(&f, &f.esp)).unwrap(); @@ -2682,27 +2682,36 @@ fn repair_of_a_short_piece_downloads_the_boot_files_and_that_piece_saying_so() { let whole = fs::metadata(&f.ipsw).unwrap().len(); let boot = 2_200_000_000; let size = |_: &str, _: &str| -> Result<(u64, u64)> { Ok((boot, whole)) }; + // No bundle needs preparing: the download is asked for the pieces + // alone, never the boot files. + let fetch = |_: &Path, _: &str, _: &str, need: sources::Need| -> Result { + anyhow::bail!("asked for {need:?}") + }; let environment = Environment { image_size: &size, + fetch_image: &fetch, ..environment(&f.host) }; - let repair = |ipsw: Option<&Path>| { + let repair = |ipsw: Option<&Path>, dry_run: bool| { let mut request = update_request(&f, &f._dir.path().join("cache")); request.action = Action::Repair; - request.dry_run = true; + request.dry_run = dry_run; request.ipsw = ipsw.map(Path::to_path_buf); - request.dispatch(&environment).unwrap() + request.dispatch(&environment) }; - let plan = repair(None); - let total = host::bytes_text(boot + piece.len() as u64); + let plan = repair(None, true).unwrap(); + let total = host::bytes_text(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}"); + assert!(!plan.contains("boot files"), "{plan}"); + let error = repair(None, false).unwrap_err().to_string(); + assert!(error.contains("asked for Pieces"), "{error}"); // From an image on this computer it writes that piece alone. - let plan = repair(Some(&f.ipsw)); + let plan = repair(Some(&f.ipsw), true).unwrap(); let piece_text = host::bytes_text(piece.len() as u64); assert!(plan.contains(&format!("({piece_text} to write)")), "{plan}"); } diff --git a/crates/stela-cli/src/setup/update.rs b/crates/stela-cli/src/setup/update.rs index ec53d3a5cc..89c3155153 100644 --- a/crates/stela-cli/src/setup/update.rs +++ b/crates/stela-cli/src/setup/update.rs @@ -567,7 +567,7 @@ pub(super) fn update_os( // the user agrees: it cannot install macOS until the new copy is // complete. let replace = match need { - super::sources::Need::WholeImage => { + super::sources::Need::WholeImage | super::sources::Need::Pieces => { room_for_new_copy(environment, &record, &build, &source, dry_run, &mut text)? } super::sources::Need::BootFiles => None,