fix: defects found by the VM install journey
- chooser: keep the plain Portage install atom and record the version separately. emerge rejects a bare versioned atom (app-misc/hello-2.12.3), so resolve/split now produce app-misc/hello + version 2.12.3. - effects: parse emerge --unmerge --pretend output (it does not use the [ebuild] form), so sys remove has a concrete plan instead of being refused as unestablished. - action: reject unknown fields on internally tagged unit variants (deny_unknown_fields does not cover them), closing a fail-closed gap. - transaction: fsync the log so a completed outcome survives power loss and is not later read as an interrupted operation. - serial-driver: import re at module level (do_boot used it unqualified).
This commit is contained in:
parent
b4729e443d
commit
dedaa4bc48
5 changed files with 160 additions and 10 deletions
|
|
@ -1210,7 +1210,21 @@ fn parse_natural_raw(text: &str) -> Option<SystemAction> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_action(json: &str) -> Result<SystemAction, ActionError> {
|
pub fn parse_action(json: &str) -> Result<SystemAction, ActionError> {
|
||||||
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)?;
|
action.validate().map_err(ActionError::Invalid)?;
|
||||||
Ok(action)
|
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":"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":"enable_service","name":"bad name"}"#).is_err());
|
||||||
assert!(parse_action(r#"{"action":"search_packages","query":""}"#).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]
|
#[test]
|
||||||
|
|
|
||||||
|
|
@ -282,11 +282,28 @@ pub fn parse_emerge_pretend(text: &str) -> ResolvedPlan {
|
||||||
source,
|
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
|
// Distinguish a real "0 packages" plan from output we could not parse. A
|
||||||
// Portage pretend run always prints a recognisable header or totals line.
|
// Portage pretend run always prints a recognisable header or totals line.
|
||||||
plan.established = !plan.effects.is_empty()
|
plan.established = !plan.effects.is_empty()
|
||||||
|| text.contains("These are the packages that would be merged")
|
|| text.contains("These are the packages that would be merged")
|
||||||
|| text.contains("Calculating dependencies")
|
|| text.contains("Calculating dependencies")
|
||||||
|
|| text.contains("would be unmerged")
|
||||||
|
|| text.contains("All selected packages:")
|
||||||
|| text.lines().any(|l| l.trim_start().starts_with("Total:"));
|
|| text.lines().any(|l| l.trim_start().starts_with("Total:"));
|
||||||
plan.download_bytes = parse_download_bytes(text);
|
plan.download_bytes = parse_download_bytes(text);
|
||||||
if plan.removals() > 0 {
|
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);
|
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]
|
#[test]
|
||||||
fn unparseable_output_is_not_an_empty_plan() {
|
fn unparseable_output_is_not_an_empty_plan() {
|
||||||
// A genuinely empty plan and output we cannot parse must be
|
// A genuinely empty plan and output we cannot parse must be
|
||||||
|
|
|
||||||
|
|
@ -133,10 +133,23 @@ impl TransactionLog {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn save(&self, path: &Path) -> Result<()> {
|
pub fn save(&self, path: &Path) -> Result<()> {
|
||||||
|
use std::io::Write;
|
||||||
if let Some(parent) = path.parent() {
|
if let Some(parent) = path.parent() {
|
||||||
std::fs::create_dir_all(parent).map_err(|e| CoreError::io(parent, e))?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -145,8 +145,47 @@ pub fn parse_pretend_version(text: &str) -> Option<String> {
|
||||||
None
|
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<String>) {
|
||||||
|
// 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<usize> = 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.
|
/// 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(
|
let mut c = Candidate::new(
|
||||||
if binary {
|
if binary {
|
||||||
Backend::PortageBinary
|
Backend::PortageBinary
|
||||||
|
|
@ -162,6 +201,7 @@ fn portage_candidate(id: &str, binary: bool, facts: &HardwareFacts) -> Candidate
|
||||||
);
|
);
|
||||||
c.verified = true;
|
c.verified = true;
|
||||||
c.arch = facts.arch.clone();
|
c.arch = facts.arch.clone();
|
||||||
|
c.version = version.map(str::to_string);
|
||||||
c.update_reliability = UpdateReliability::Automatic;
|
c.update_reliability = UpdateReliability::Automatic;
|
||||||
if !binary {
|
if !binary {
|
||||||
c.build_seconds = facts.estimated_build_seconds(id);
|
c.build_seconds = facts.estimated_build_seconds(id);
|
||||||
|
|
@ -188,14 +228,16 @@ pub fn resolve_candidates(exec: &Exec, root: &Path, need: &Need) -> Vec<Candidat
|
||||||
match pretend {
|
match pretend {
|
||||||
Ok(resolved) if resolved.success => {
|
Ok(resolved) if resolved.success => {
|
||||||
let text = resolved.stdout_lossy();
|
let text = resolved.stdout_lossy();
|
||||||
let id = parse_pretend_version(&text).unwrap_or_else(|| name.clone());
|
let (atom, version) = parse_pretend_version(&text)
|
||||||
out.push(portage_candidate(&id, false, &facts));
|
.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 {
|
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.available = false;
|
||||||
c.notes
|
c.notes
|
||||||
.push("emerge could not resolve this name from the Gentoo tree".into());
|
.push("emerge could not resolve this name from the Gentoo tree".into());
|
||||||
|
|
@ -644,11 +686,13 @@ pub fn resolve_for_backend(
|
||||||
.arg(application),
|
.arg(application),
|
||||||
) {
|
) {
|
||||||
if out.success {
|
if out.success {
|
||||||
let id = parse_pretend_version(&out.stdout_lossy())
|
let (atom, version) = parse_pretend_version(&out.stdout_lossy())
|
||||||
.unwrap_or_else(|| application.to_string());
|
.map(|v| split_atom_version(&v))
|
||||||
|
.unwrap_or_else(|| (application.to_string(), None));
|
||||||
candidates.push(portage_candidate(
|
candidates.push(portage_candidate(
|
||||||
&id,
|
&atom,
|
||||||
backend == Backend::PortageBinary,
|
backend == Backend::PortageBinary,
|
||||||
|
version.as_deref(),
|
||||||
&hardware_facts(exec, root),
|
&hardware_facts(exec, root),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
@ -931,6 +975,49 @@ mod tests {
|
||||||
assert_eq!(parse_pretend_version("nothing here"), None);
|
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]
|
#[test]
|
||||||
fn preferences_roundtrip_through_disk() {
|
fn preferences_roundtrip_through_disk() {
|
||||||
let root = tempfile::tempdir().unwrap();
|
let root = tempfile::tempdir().unwrap();
|
||||||
|
|
|
||||||
|
|
@ -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.
|
Results are structured and the exit status is nonzero on failure.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import pexpect
|
import pexpect
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue