From eafbf424b2c3fdb167619cde86776867c534e51d Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Thu, 12 Nov 2026 03:59:30 -0500 Subject: [PATCH] fix(setup): have repair remove damaged install pieces only after everything that can refuse it, with the dry run naming their download --- crates/stela-cli/src/setup/create.rs | 7 +++++- crates/stela-cli/src/setup/doctor.rs | 19 ++++++++++----- crates/stela-cli/src/setup/medium.rs | 35 ++++++++++++++++++++++----- crates/stela-cli/src/setup/sources.rs | 1 + crates/stela-cli/src/setup/tests.rs | 34 ++++++++++++++++++++++++++ 5 files changed, 83 insertions(+), 13 deletions(-) diff --git a/crates/stela-cli/src/setup/create.rs b/crates/stela-cli/src/setup/create.rs index 5dedf64a1d..688657560b 100644 --- a/crates/stela-cli/src/setup/create.rs +++ b/crates/stela-cli/src/setup/create.rs @@ -158,7 +158,11 @@ 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 damaged = record + .medium_of(build) + .map_or(&[][..], |m| m.damaged.as_slice()); + let write = + medium::missing_bytes(&volume, build, ipsw_bytes, environment.piece_bytes, damaged); let created = missing_directories(&volume, &medium::directory(&volume, build)); Some(MediumPlan { volume, @@ -296,6 +300,7 @@ pub(super) fn apply_install( partition_guid: plan.guid.clone(), manifest_sha256: String::new(), remote: plan.remote, + damaged: Vec::new(), }); let mut created = record.created_directories.clone(); for directory in plan.created { diff --git a/crates/stela-cli/src/setup/doctor.rs b/crates/stela-cli/src/setup/doctor.rs index 307bbd23ee..fe7c42d210 100644 --- a/crates/stela-cli/src/setup/doctor.rs +++ b/crates/stela-cli/src/setup/doctor.rs @@ -771,7 +771,8 @@ pub(super) fn repair(environment: &Environment<'_>, command: &Request) -> Result let has_media = record.installs_media(); // The full medium's bytes, read whole when asked or when an install // found a piece damaged since it was last checked: a damaged piece is - // removed (in a dry run, named) and written again like a missing one. + // written again like a missing one, and removed only after everything + // that can refuse the repair. let mut checked = None; if let Some(medium) = record .active_medium() @@ -800,14 +801,12 @@ pub(super) fn repair(environment: &Environment<'_>, command: &Request) -> Result damaged.join(", "), match command.dry_run { true => "would be removed and written again", - false => "removed, to be written again", + false => "removed and written again", } )?, } - if !command.dry_run { - for piece in &damaged { - fs::remove_file(medium.directory().join(piece))?; - } + if let Some(active) = record.media.iter_mut().find(|m| m.build == medium.build) { + active.damaged = damaged; } checked = Some(super::medium::log_head(&record.esp)); } @@ -1116,6 +1115,14 @@ pub(super) fn repair(environment: &Environment<'_>, command: &Request) -> Result text.push_str("Dry run: nothing was changed.\n"); return Ok(text); } + // Nothing refuses the repair from here: the damaged pieces go, and the + // install below writes them again as missing ones. + if let Some(medium) = record.media.iter_mut().find(|m| !m.damaged.is_empty()) { + for piece in std::mem::take(&mut medium.damaged) { + let path = medium.directory().join(&piece); + fs::remove_file(&path).with_context(|| format!("cannot remove {}", path.display()))?; + } + } if let Some(store) = &record.translations && !store_repairs.is_empty() { diff --git a/crates/stela-cli/src/setup/medium.rs b/crates/stela-cli/src/setup/medium.rs index c2857b3d64..87e5431198 100644 --- a/crates/stela-cli/src/setup/medium.rs +++ b/crates/stela-cli/src/setup/medium.rs @@ -60,6 +60,11 @@ pub(super) struct Medium { /// It names the IPSW on Apple's servers instead of holding it. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub(super) remote: bool, + /// Pieces a repair read and found damaged ([`Medium::damaged_pieces`]): + /// written again like missing ones, and removed only once nothing can + /// refuse the repair. Never recorded. + #[serde(skip)] + pub(super) damaged: Vec, } impl Medium { @@ -161,6 +166,12 @@ impl Medium { Err(_) => problems.push(format!("{} is missing", path.display())), } } + for piece in &self.damaged { + problems.push(format!( + "{} differs from its recorded SHA-256", + directory.join(piece).display() + )); + } for file in [PIECE_HASHES_FILE, BLOCK_HASHES_FILE] { if !directory.join(file).is_file() { problems.push(format!("{} is missing", directory.join(file).display())); @@ -332,17 +343,25 @@ pub(super) fn needed_bytes(volume: &Path, build: &str, ipsw_bytes: u64, piece_by } /// 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 { +/// download onto it fetches, the intact pieces being kept. The `damaged` +/// pieces ([`Medium::damaged`]) count as missing. +pub(super) fn missing_bytes( + volume: &Path, + build: &str, + ipsw_bytes: u64, + piece_bytes: u64, + damaged: &[String], +) -> 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) + damaged.contains(&piece_name(*index)) + || fs::metadata(directory.join(piece_name(*index))) + .map(|m| m.len()) + .ok() + != Some(*size) }) .map(|(_, size)| size) .sum() @@ -815,6 +834,7 @@ pub(super) mod tests { partition_guid: "G".into(), manifest_sha256: first.manifest_sha256.clone(), remote: false, + damaged: Vec::new(), } .problems() .is_empty() @@ -921,6 +941,7 @@ pub(super) mod tests { partition_guid: "G".into(), manifest_sha256: String::new(), remote: false, + damaged: Vec::new(), }; assert_eq!(medium.problems().len(), 1); } @@ -953,6 +974,7 @@ pub(super) mod tests { partition_guid: String::new(), manifest_sha256: write_remote(&volume, remote).unwrap(), remote: true, + damaged: Vec::new(), }; assert!(medium.problems().is_empty(), "{:?}", medium.problems()); let mut names: Vec<_> = fs::read_dir(medium.directory()) @@ -977,6 +999,7 @@ pub(super) mod tests { partition_guid: String::new(), manifest_sha256: String::new(), remote: false, + damaged: Vec::new(), }; assert_eq!( medium("26A434").removable_directory().unwrap(), diff --git a/crates/stela-cli/src/setup/sources.rs b/crates/stela-cli/src/setup/sources.rs index 77b1f54e80..325d2e306d 100644 --- a/crates/stela-cli/src/setup/sources.rs +++ b/crates/stela-cli/src/setup/sources.rs @@ -227,6 +227,7 @@ pub(super) fn locate(environment: &Environment<'_>, lookup: &Lookup<'_>) -> Resu lookup.build, whole, environment.piece_bytes, + &m.damaged, ), _ => whole, }, diff --git a/crates/stela-cli/src/setup/tests.rs b/crates/stela-cli/src/setup/tests.rs index 418c424e58..3702a5c9b6 100644 --- a/crates/stela-cli/src/setup/tests.rs +++ b/crates/stela-cli/src/setup/tests.rs @@ -2701,6 +2701,39 @@ fn a_piece_an_install_found_damaged_is_named_by_status_and_put_right_by_repair() clean.contains("every block is checked as the install reads it"), "{clean}" ); + // Without the image or the network, repair refuses before it changes + // anything: the damaged piece stays rather than going missing. + let before = (snapshot(&f.esp), snapshot(&stela_volume(&f))); + let offline = || { + let mut request = update_request(&f, &f._dir.path().join("empty")); + request.action = Action::Repair; + request.verify = true; + request + }; + let error = format!( + "{:#}", + offline().dispatch(&environment(&f.host)).unwrap_err() + ); + assert!(error.contains("needs the macOS"), "{error}"); + assert_eq!((snapshot(&f.esp), snapshot(&stela_volume(&f))), before); + // With Apple's servers reachable, the dry run names the download of + // that piece alone. + let whole = fs::metadata(&f.ipsw).unwrap().len(); + let size = |_: &str, _: &str| -> Result<(u64, u64)> { Ok((2_200_000_000, whole)) }; + let mut request = offline(); + request.dry_run = true; + let plan = request + .dispatch(&Environment { + image_size: &size, + ..environment(&f.host) + }) + .unwrap(); + let piece = host::bytes_text(good.len() as u64); + assert!( + plan.contains(&format!("download {piece} from Apple")), + "{plan}" + ); + assert!(plan.contains("this USB's copy lacks"), "{plan}"); // The image, downloaded earlier. let cache = f._dir.path().join("cache"); fs::create_dir_all(&cache).unwrap(); @@ -2725,6 +2758,7 @@ fn a_piece_an_install_found_damaged_is_named_by_status_and_put_right_by_repair() plan.contains("ipsw.000 damaged: would be removed"), "{plan}" ); + assert!(plan.contains(&format!("({piece} to write)")), "{plan}"); assert_eq!(fs::read(&first).unwrap(), bad); // An install that found the block: status names the piece and repair. let line = format!("[ 12.000] {DAMAGED_LOG_LINE}ipsw.000 block 0\n");