diff --git a/installer-rs/crates/phoebus-core/src/action.rs b/installer-rs/crates/phoebus-core/src/action.rs index ed7ca82..21506b0 100644 --- a/installer-rs/crates/phoebus-core/src/action.rs +++ b/installer-rs/crates/phoebus-core/src/action.rs @@ -1210,7 +1210,21 @@ fn parse_natural_raw(text: &str) -> Option { } pub fn parse_action(json: &str) -> Result { - let action: SystemAction = serde_json::from_str(json)?; + let value: serde_json::Value = serde_json::from_str(json)?; + let action: SystemAction = serde_json::from_value(value.clone())?; + // `deny_unknown_fields` does not reject extra keys on internally tagged + // *unit* variants (e.g. `{"action":"history","x":1}`), so compare the + // input keys against the canonical serialisation and fail closed. + if let (Some(input), Some(canonical)) = ( + value.as_object(), + serde_json::to_value(&action)?.as_object(), + ) { + for key in input.keys() { + if !canonical.contains_key(key) { + return Err(ActionError::Invalid(format!("unknown field `{key}`"))); + } + } + } action.validate().map_err(ActionError::Invalid)?; Ok(action) } @@ -1245,6 +1259,11 @@ mod tests { assert!(parse_action(r#"{"action":"install_package","atom":"a/b/c"}"#).is_err()); assert!(parse_action(r#"{"action":"enable_service","name":"bad name"}"#).is_err()); assert!(parse_action(r#"{"action":"search_packages","query":""}"#).is_err()); + // Unit variants must reject unknown keys too (serde's + // deny_unknown_fields does not cover internally tagged unit variants). + assert!(parse_action(r#"{"action":"history","x":1}"#).is_err()); + assert!(parse_action(r#"{"action":"update_system","shell":"sh"}"#).is_err()); + assert!(parse_action(r#"{"action":"snap_list","extra":true}"#).is_err()); } #[test] diff --git a/installer-rs/crates/phoebus-core/src/effects.rs b/installer-rs/crates/phoebus-core/src/effects.rs index b37bf48..2684c21 100644 --- a/installer-rs/crates/phoebus-core/src/effects.rs +++ b/installer-rs/crates/phoebus-core/src/effects.rs @@ -282,11 +282,28 @@ pub fn parse_emerge_pretend(text: &str) -> ResolvedPlan { source, }); } + // `emerge --unmerge --pretend` does not use the `[ebuild ...]` form; it + // lists the packages slated for removal. Parse them so a removal has a + // concrete, reviewable plan instead of being refused as unestablished. + for line in text.lines() { + if let Some(rest) = line.trim().strip_prefix("All selected packages:") { + for atom in rest.split_whitespace() { + plan.effects.push(PackageEffect { + target: atom.to_string(), + previous: None, + kind: EffectKind::Remove, + source: EffectSource::None, + }); + } + } + } // Distinguish a real "0 packages" plan from output we could not parse. A // Portage pretend run always prints a recognisable header or totals line. plan.established = !plan.effects.is_empty() || text.contains("These are the packages that would be merged") || text.contains("Calculating dependencies") + || text.contains("would be unmerged") + || text.contains("All selected packages:") || text.lines().any(|l| l.trim_start().starts_with("Total:")); plan.download_bytes = parse_download_bytes(text); if plan.removals() > 0 { @@ -387,6 +404,19 @@ Total: 5 packages (2 upgrades, 2 new, 1 uninstall), Size of downloads: 10,240 Ki assert_eq!(plan.digest.len(), 64); } + #[test] + fn parses_unmerge_pretend_output() { + let text = "\n>>> These are the packages that would be unmerged:\n\n \ + app-misc/hello\n selected: 2.12.3\n\n\ + All selected packages: app-misc/hello-2.12.3\n\n\ + >>> 'Selected' packages are slated for removal.\n"; + let plan = parse_emerge_pretend(text); + assert!(plan.established, "unmerge output is an established plan"); + assert_eq!(plan.removals(), 1); + assert_eq!(plan.effects[0].target, "app-misc/hello-2.12.3"); + assert_eq!(plan.effects[0].kind, EffectKind::Remove); + } + #[test] fn unparseable_output_is_not_an_empty_plan() { // A genuinely empty plan and output we cannot parse must be diff --git a/installer-rs/crates/phoebus-core/src/transaction.rs b/installer-rs/crates/phoebus-core/src/transaction.rs index 2790aa9..7ce505d 100644 --- a/installer-rs/crates/phoebus-core/src/transaction.rs +++ b/installer-rs/crates/phoebus-core/src/transaction.rs @@ -133,10 +133,23 @@ impl TransactionLog { } pub fn save(&self, path: &Path) -> Result<()> { + use std::io::Write; if let Some(parent) = path.parent() { std::fs::create_dir_all(parent).map_err(|e| CoreError::io(parent, e))?; } - std::fs::write(path, toml::to_string_pretty(self)?).map_err(|e| CoreError::io(path, e))?; + let text = toml::to_string_pretty(self)?; + let mut file = std::fs::File::create(path).map_err(|e| CoreError::io(path, e))?; + file.write_all(text.as_bytes()) + .map_err(|e| CoreError::io(path, e))?; + // Make the record durable: an operation's outcome (including a + // "finished" update to an "in progress" record) must survive a power + // loss, or the history would show a completed action as interrupted. + file.sync_all().map_err(|e| CoreError::io(path, e))?; + if let Some(parent) = path.parent() { + if let Ok(dir) = std::fs::File::open(parent) { + let _ = dir.sync_all(); + } + } Ok(()) } diff --git a/installer-rs/crates/phoebus-executor/src/chooser.rs b/installer-rs/crates/phoebus-executor/src/chooser.rs index 0007299..8e5c507 100644 --- a/installer-rs/crates/phoebus-executor/src/chooser.rs +++ b/installer-rs/crates/phoebus-executor/src/chooser.rs @@ -145,8 +145,47 @@ pub fn parse_pretend_version(text: &str) -> Option { None } +/// Split an `emerge`-reported `category/package-version` token into the plain +/// atom and the version. `emerge` rejects a bare versioned atom +/// (`app-misc/hello-2.12.3` is not a valid atom), so the plain atom is the +/// install identifier and the version is recorded separately. Gentoo versions +/// always start with a digit, so the version begins at the last `-` followed +/// by a digit. +pub fn split_atom_version(versioned: &str) -> (String, Option) { + // Drop a `:slot`/`::repo` suffix if present. + let token = versioned.split(':').next().unwrap_or(versioned); + let (category, rest) = match token.split_once('/') { + Some((c, r)) => (Some(c), r), + None => (None, token), + }; + let bytes = rest.as_bytes(); + let mut split_at: Option = None; + for i in 1..bytes.len() { + if bytes[i - 1] == b'-' && bytes[i].is_ascii_digit() { + split_at = Some(i - 1); + } + } + match split_at { + Some(i) => { + let package = &rest[..i]; + let version = rest[i + 1..].to_string(); + let atom = match category { + Some(c) => format!("{c}/{package}"), + None => package.to_string(), + }; + (atom, Some(version)) + } + None => (token.to_string(), None), + } +} + /// A rough capability profile for a plain Portage source/binary candidate. -fn portage_candidate(id: &str, binary: bool, facts: &HardwareFacts) -> Candidate { +fn portage_candidate( + id: &str, + binary: bool, + version: Option<&str>, + facts: &HardwareFacts, +) -> Candidate { let mut c = Candidate::new( if binary { Backend::PortageBinary @@ -162,6 +201,7 @@ fn portage_candidate(id: &str, binary: bool, facts: &HardwareFacts) -> Candidate ); c.verified = true; c.arch = facts.arch.clone(); + c.version = version.map(str::to_string); c.update_reliability = UpdateReliability::Automatic; if !binary { c.build_seconds = facts.estimated_build_seconds(id); @@ -188,14 +228,16 @@ pub fn resolve_candidates(exec: &Exec, root: &Path, need: &Need) -> Vec { let text = resolved.stdout_lossy(); - let id = parse_pretend_version(&text).unwrap_or_else(|| name.clone()); - out.push(portage_candidate(&id, false, &facts)); + let (atom, version) = parse_pretend_version(&text) + .map(|v| split_atom_version(&v)) + .unwrap_or_else(|| (name.clone(), None)); + out.push(portage_candidate(&atom, false, version.as_deref(), &facts)); if facts.binhost_available { - out.push(portage_candidate(&id, true, &facts)); + out.push(portage_candidate(&atom, true, version.as_deref(), &facts)); } } _ => { - let mut c = portage_candidate(name, false, &facts); + let mut c = portage_candidate(name, false, None, &facts); c.available = false; c.notes .push("emerge could not resolve this name from the Gentoo tree".into()); @@ -644,11 +686,13 @@ pub fn resolve_for_backend( .arg(application), ) { if out.success { - let id = parse_pretend_version(&out.stdout_lossy()) - .unwrap_or_else(|| application.to_string()); + let (atom, version) = parse_pretend_version(&out.stdout_lossy()) + .map(|v| split_atom_version(&v)) + .unwrap_or_else(|| (application.to_string(), None)); candidates.push(portage_candidate( - &id, + &atom, backend == Backend::PortageBinary, + version.as_deref(), &hardware_facts(exec, root), )); } @@ -931,6 +975,49 @@ mod tests { assert_eq!(parse_pretend_version("nothing here"), None); } + #[test] + fn split_atom_version_recovers_the_plain_install_atom() { + // emerge rejects `cat/pkg-version`, so the install id must stay plain. + assert_eq!( + split_atom_version("app-misc/hello-2.12.3"), + ("app-misc/hello".to_string(), Some("2.12.3".to_string())) + ); + assert_eq!( + split_atom_version("dev-lang/python-3.12.6:3.12"), + ("dev-lang/python".to_string(), Some("3.12.6".to_string())) + ); + assert_eq!( + split_atom_version("www-client/firefox-128.0::gentoo"), + ("www-client/firefox".to_string(), Some("128.0".to_string())) + ); + assert_eq!( + split_atom_version("sys-apps/foo-bar-1.0-r1"), + ("sys-apps/foo-bar".to_string(), Some("1.0-r1".to_string())) + ); + assert_eq!(split_atom_version("vim"), ("vim".to_string(), None)); + } + + #[test] + fn portage_candidate_keeps_a_valid_install_atom() { + let runner = RecordingRunner::new(); + runner.set_response( + "emerge", + ok("Calculating dependencies... done!\n[ebuild N ] app-misc/hello-2.12.3 USE=\"nls\"\n"), + ); + let exec = make_exec(runner); + let root = tempfile::tempdir().unwrap(); + let need = Need::new("app-misc/hello"); + let candidates = resolve_candidates(&exec, root.path(), &need); + let portage = candidates + .iter() + .find(|c| c.backend == Backend::PortageSource && c.available) + .expect("portage candidate"); + assert_eq!(portage.id, "app-misc/hello"); + assert_eq!(portage.version.as_deref(), Some("2.12.3")); + // And the id is a valid package spec that emerge accepts. + assert!(phoebus_core::action::valid_package_spec(&portage.id)); + } + #[test] fn preferences_roundtrip_through_disk() { let root = tempfile::tempdir().unwrap(); diff --git a/installer/tests/vm/serial-driver.py b/installer/tests/vm/serial-driver.py index 60a2e02..a65cb47 100644 --- a/installer/tests/vm/serial-driver.py +++ b/installer/tests/vm/serial-driver.py @@ -22,6 +22,7 @@ by `PHOEBUS_VM_DISKS` (default `$PHOEBUS_VM_STATE/disks`); block devices, Results are structured and the exit status is nonzero on failure. """ import os +import re import sys import pexpect