fix(setup): have a repair of a full install medium that prepares no boot bundle download only the pieces the USB lacks, not the image's boot files

This commit is contained in:
Tyler Smith committed 2026-11-12 04:01:28 -05:00
1 parent ba88694541
commit 6130462b54
9 files changed
+78 -27

No files matched your search

+7 -1
View File
@@ -731,7 +731,13 @@ pub fn remote_inventory<R: Read + Seek>(
/// 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<R: Read + Seek>(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))
+4 -2
View File
@@ -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),
+1 -1
View File
@@ -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 {
+1 -1
View File
@@ -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)
+1 -1
View File
@@ -261,7 +261,7 @@ pub(super) fn add(environment: &Environment<'_>, command: &Request) -> Result<St
record.build
)
})?;
if need == sources::Need::WholeImage {
if need.whole() {
sources::check_room(environment, &next, &next.build, &source)?;
}
let restore: Vec<(bundle::Kind, Option<Ecid>)> =
+12 -3
View File
@@ -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<Self> {
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)
+35 -10
View File
@@ -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<Need> {
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))
}
+16 -7
View File
@@ -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<sources::Fetched> {
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}");
}
+1 -1
View File
@@ -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,