diff --git a/crates/stela-cli/src/setup/create.rs b/crates/stela-cli/src/setup/create.rs index bb6b16c24d..dde9736fbb 100644 --- a/crates/stela-cli/src/setup/create.rs +++ b/crates/stela-cli/src/setup/create.rs @@ -1043,6 +1043,24 @@ pub(super) fn setup(environment: &Environment<'_>, mut command: Request) -> Resu } // Every new or resumed setup knows its installer from here on. let install_image = command.install_image.unwrap_or(InstallImage::Network); + // An image of the build this computer holds already, downloaded by an + // earlier command or by another user under sudo, serves as --ipsw. + if command.ipsw.is_none() + && command.fetch + && let Some(path) = super::sources::cached_image( + environment, + match install_image { + InstallImage::Full => super::sources::Need::WholeImage, + InstallImage::Network => super::sources::Need::BootFiles, + }, + &command.build, + command.model(), + command.download_dir.as_deref(), + ) + { + command.ipsw = Some(path); + command.fetch = false; + } // Downloaded boot archives are kept in one place until setup finishes, // so a setup run again after a stop reuses them, and removed once it // has. A full image goes directly to the USB after preparation and diff --git a/crates/stela-cli/src/setup/sources.rs b/crates/stela-cli/src/setup/sources.rs index 2c887c0f1f..f443bf5278 100644 --- a/crates/stela-cli/src/setup/sources.rs +++ b/crates/stela-cli/src/setup/sources.rs @@ -371,6 +371,30 @@ pub(super) fn obtain( } } +/// An image this computer holds that serves `need` for `build` and `model` +/// (`--download-dir` first, then every cache [`image_caches`] names): a new +/// setup takes it as `--ipsw` instead of downloading. +pub(super) fn cached_image( + environment: &Environment<'_>, + need: Need, + build: &str, + model: &str, + download_dir: Option<&Path>, +) -> Option { + let lookup = Lookup { + need, + build, + model, + ipsw: None, + download_dir, + medium: None, + }; + lookup + .directories(environment) + .iter() + .find_map(|directory| cached(directory, &lookup)) +} + /// A complete image of `lookup`'s build and model in `directory`, or for /// boot files also a boot archive: the one a command downloaded, or the /// one a first setup left. diff --git a/crates/stela-cli/src/setup/tests.rs b/crates/stela-cli/src/setup/tests.rs index 650f5b61b6..0389c42c68 100644 --- a/crates/stela-cli/src/setup/tests.rs +++ b/crates/stela-cli/src/setup/tests.rs @@ -2633,6 +2633,32 @@ fn os_update_checks_everything_before_any_download() { assert_eq!((snapshot(&f.esp), snapshot(&stela_volume(&f))), before); } +#[test] +fn a_new_setup_takes_an_image_this_computer_holds_instead_of_downloading() { + for installer in [InstallImage::Full, InstallImage::Network] { + let mut f = fixture(64 * GIB, false); + add_stela(&mut f); + let cache = f._dir.path().join("cache"); + fs::create_dir_all(&cache).unwrap(); + let cached = cache.join(format!( + "UniversalMac_0_{}_Restore.ipsw", + restore::payload::SYNTHETIC_BUILD + )); + fs::copy(&f.ipsw, &cached).unwrap(); + let mut request = command(&f, &f.esp); + request.ipsw = None; + request.fetch = true; + request.download_dir = Some(cache.clone()); + request.install_image = Some(installer); + let text = setup(&environment(&f.host), request).unwrap(); + assert!( + text.contains(&format!("restore image {}", cached.display())), + "{text}" + ); + assert_eq!(record(&f).install_kind(), installer); + } +} + #[test] fn repair_of_a_short_piece_downloads_the_boot_files_and_that_piece_saying_so() { let mut f = fixture(64 * GIB, false);