fix(sys): safe cleanup previews, exact Flatpak/Snap update transactions, and correct reverse dependencies
This commit is contained in:
parent
d3f676af1f
commit
ec1f7c5b0b
5 changed files with 1421 additions and 290 deletions
|
|
@ -63,6 +63,55 @@ pub struct CleanupEffect {
|
|||
pub unavailable: bool,
|
||||
}
|
||||
|
||||
/// A non-Portage package source whose update is a single, reviewable
|
||||
/// transaction (Flatpak or Snap). Portage updates are represented as
|
||||
/// [`PackageEffect`]s from `emerge --pretend` instead.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum UpdateSource {
|
||||
Flatpak,
|
||||
Snap,
|
||||
}
|
||||
|
||||
impl UpdateSource {
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
UpdateSource::Flatpak => "flatpak",
|
||||
UpdateSource::Snap => "snap",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One exact, reviewable update transaction for a non-Portage lane.
|
||||
///
|
||||
/// Execution consumes these transactions rather than re-discovering updates,
|
||||
/// so a changed revision, a newly appearing update, or a different installation
|
||||
/// cannot be smuggled in after review. `exact` records whether the backend can
|
||||
/// actually guarantee the reviewed identity and immutable revision; when it is
|
||||
/// false the source must be refused rather than executed approximately.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct UpdateTransaction {
|
||||
pub source: UpdateSource,
|
||||
/// The immutable backend identity: a full Flatpak ref
|
||||
/// (`app/org.x/x86_64/stable`) or a snap name.
|
||||
pub identity: String,
|
||||
/// Human-readable version before the update, when the backend reports it.
|
||||
pub previous_version: Option<String>,
|
||||
/// Human-readable version after the update.
|
||||
pub next_version: String,
|
||||
/// Immutable revision before the update (Flatpak commit / snap revision).
|
||||
pub previous_revision: Option<String>,
|
||||
/// Immutable revision after the update.
|
||||
pub next_revision: Option<String>,
|
||||
/// Installation scope (Flatpak `system`/`user`; Snap tracking channel).
|
||||
pub scope: Option<String>,
|
||||
/// Material changes the update pulls in beyond the primary identity (for
|
||||
/// example an updated Flatpak runtime), or a confinement note.
|
||||
pub related: Vec<String>,
|
||||
/// The backend can guarantee this exact identity and immutable revision.
|
||||
pub exact: bool,
|
||||
}
|
||||
|
||||
/// A fully resolved, human-reviewable plan.
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(default)]
|
||||
|
|
@ -70,6 +119,10 @@ pub struct ResolvedPlan {
|
|||
pub effects: Vec<PackageEffect>,
|
||||
/// Destructive cache-cleanup intents, each bound to its reviewed dry run.
|
||||
pub cleanup: Vec<CleanupEffect>,
|
||||
/// Exact per-source update transactions (Flatpak/Snap), bound to approval
|
||||
/// and consumed by execution. Freshly discovered updates are never a
|
||||
/// substitute for these.
|
||||
pub updates: Vec<UpdateTransaction>,
|
||||
pub download_bytes: Option<u64>,
|
||||
/// A recovery point that will exist before the change (snapshot id).
|
||||
pub recovery_point: Option<String>,
|
||||
|
|
@ -158,6 +211,31 @@ impl ResolvedPlan {
|
|||
}
|
||||
hasher.update(b"\0");
|
||||
}
|
||||
// Exact update transactions (identity + immutable revision) are part of
|
||||
// the approved material change.
|
||||
for txn in &self.updates {
|
||||
hasher.update(b"update\0");
|
||||
hasher.update(txn.source.as_str().as_bytes());
|
||||
hasher.update(b"\0");
|
||||
hasher.update(txn.identity.as_bytes());
|
||||
hasher.update(b"\0");
|
||||
hasher.update(txn.previous_version.as_deref().unwrap_or("").as_bytes());
|
||||
hasher.update(b"\0");
|
||||
hasher.update(txn.next_version.as_bytes());
|
||||
hasher.update(b"\0");
|
||||
hasher.update(txn.previous_revision.as_deref().unwrap_or("").as_bytes());
|
||||
hasher.update(b"\0");
|
||||
hasher.update(txn.next_revision.as_deref().unwrap_or("").as_bytes());
|
||||
hasher.update(b"\0");
|
||||
hasher.update(txn.scope.as_deref().unwrap_or("").as_bytes());
|
||||
hasher.update(b"\0");
|
||||
hasher.update([txn.exact as u8]);
|
||||
for related in &txn.related {
|
||||
hasher.update(related.as_bytes());
|
||||
hasher.update(b"\n");
|
||||
}
|
||||
hasher.update(b"\0");
|
||||
}
|
||||
self.digest = format!("{:x}", hasher.finalize());
|
||||
}
|
||||
|
||||
|
|
@ -214,6 +292,33 @@ impl ResolvedPlan {
|
|||
}
|
||||
}
|
||||
}
|
||||
for txn in &self.updates {
|
||||
let revisions = match (&txn.previous_revision, &txn.next_revision) {
|
||||
(Some(prev), Some(next)) => format!(" [{prev} -> {next}]"),
|
||||
(None, Some(next)) => format!(" [-> {next}]"),
|
||||
_ => String::new(),
|
||||
};
|
||||
let scope = txn
|
||||
.scope
|
||||
.as_deref()
|
||||
.map(|s| format!(" ({s})"))
|
||||
.unwrap_or_default();
|
||||
let exact_note = if txn.exact {
|
||||
""
|
||||
} else {
|
||||
" — NOT EXACTLY BINDABLE"
|
||||
};
|
||||
out.push_str(&format!(
|
||||
" [{}] {}{scope} {}{revisions} -> {}{exact_note}\n",
|
||||
txn.source.as_str(),
|
||||
txn.identity,
|
||||
txn.previous_version.as_deref().unwrap_or("?"),
|
||||
txn.next_version,
|
||||
));
|
||||
for related in &txn.related {
|
||||
out.push_str(&format!(" ~ {related}\n"));
|
||||
}
|
||||
}
|
||||
if let Some(bytes) = self.download_bytes {
|
||||
out.push_str(&format!(" download: {} MiB\n", bytes / (1024 * 1024)));
|
||||
}
|
||||
|
|
@ -565,6 +670,39 @@ Total: 5 packages (2 upgrades, 2 new, 1 uninstall), Size of downloads: 10,240 Ki
|
|||
assert!(changed_materially(&a, &unavailable));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_transactions_are_part_of_the_digest_and_material_change() {
|
||||
let txn = UpdateTransaction {
|
||||
source: UpdateSource::Flatpak,
|
||||
identity: "app/org.mozilla.firefox/x86_64/stable".into(),
|
||||
previous_version: Some("128".into()),
|
||||
next_version: "129".into(),
|
||||
previous_revision: Some("OLD".into()),
|
||||
next_revision: Some("NEW".into()),
|
||||
scope: Some("system".into()),
|
||||
related: Vec::new(),
|
||||
exact: true,
|
||||
};
|
||||
let mut a = ResolvedPlan::default();
|
||||
a.updates.push(txn.clone());
|
||||
a.recompute_digest();
|
||||
|
||||
// A different immutable revision with the *same* human version must be
|
||||
// a material change.
|
||||
let mut b = a.clone();
|
||||
b.updates[0].next_revision = Some("OTHER".into());
|
||||
b.recompute_digest();
|
||||
assert_ne!(a.digest, b.digest);
|
||||
assert!(changed_materially(&a, &b));
|
||||
assert!(!approval_holds(&a, &b, &[]));
|
||||
|
||||
// A loss of exactness is also material.
|
||||
let mut c = a.clone();
|
||||
c.updates[0].exact = false;
|
||||
c.recompute_digest();
|
||||
assert!(changed_materially(&a, &c));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_states_source_binary_and_consequences() {
|
||||
let plan = parse_emerge_pretend(PRETEND);
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use std::path::Path;
|
|||
use phoebus_core::action::SystemAction;
|
||||
use phoebus_core::effects::{
|
||||
changed_materially, parse_emerge_pretend, CleanupEffect, EffectKind, EffectSource,
|
||||
PackageEffect, ResolvedPlan,
|
||||
PackageEffect, ResolvedPlan, UpdateSource, UpdateTransaction,
|
||||
};
|
||||
use phoebus_core::selection::{Backend, Need};
|
||||
use phoebus_core::transaction::TransactionOutcome;
|
||||
|
|
@ -301,10 +301,74 @@ fn flatpak_available(exec: &Exec, root: &Path) -> bool {
|
|||
crate::query::flatpak_available(exec, root)
|
||||
}
|
||||
|
||||
/// Exact per-application Flatpak update effects. Each affected application is
|
||||
/// an identity plus its planned new version, so approval cannot authorize an
|
||||
/// unrestricted update-all.
|
||||
fn flatpak_update_effects(exec: &Exec, root: &Path) -> Result<Vec<PackageEffect>> {
|
||||
/// Split a Flatpak `--columns=` row (tab-separated) or whitespace fallback.
|
||||
fn flatpak_cols(line: &str) -> Vec<String> {
|
||||
if line.contains('\t') {
|
||||
line.split('\t').map(|s| s.trim().to_string()).collect()
|
||||
} else {
|
||||
line.split_whitespace().map(|s| s.to_string()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse `flatpak remote-ls --updates` and `flatpak list` output into exact,
|
||||
/// immutable-revision update transactions.
|
||||
///
|
||||
/// `updates` columns: `ref,application,version,branch,commit,origin,installation,type,arch`.
|
||||
/// `installed` columns: `ref,application,version,branch,commit,installation,type,arch`.
|
||||
/// A transaction is `exact` only when both the ref and the target commit are
|
||||
/// present; otherwise the source must be refused rather than approximated.
|
||||
pub(crate) fn parse_flatpak_transactions(updates: &str, installed: &str) -> Vec<UpdateTransaction> {
|
||||
use std::collections::BTreeMap;
|
||||
// ref -> (version, commit)
|
||||
let mut current: BTreeMap<String, (Option<String>, Option<String>)> = BTreeMap::new();
|
||||
for line in installed.lines() {
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let cols = flatpak_cols(line);
|
||||
let (Some(r), version, commit) = (
|
||||
cols.first().filter(|s| !s.is_empty()).cloned(),
|
||||
cols.get(2).cloned(),
|
||||
cols.get(4).cloned(),
|
||||
) else {
|
||||
continue;
|
||||
};
|
||||
current.insert(r, (version, commit));
|
||||
}
|
||||
let mut txns = Vec::new();
|
||||
for line in updates.lines() {
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let cols = flatpak_cols(line);
|
||||
let Some(identity) = cols.first().filter(|s| !s.is_empty()).cloned() else {
|
||||
continue;
|
||||
};
|
||||
let next_version = cols.get(2).cloned().unwrap_or_default();
|
||||
let next_revision = cols.get(4).filter(|s| !s.is_empty()).cloned();
|
||||
let scope = cols.get(6).filter(|s| !s.is_empty()).cloned();
|
||||
let previous = current.get(&identity);
|
||||
txns.push(UpdateTransaction {
|
||||
source: UpdateSource::Flatpak,
|
||||
identity,
|
||||
previous_version: previous.and_then(|(v, _)| v.clone()),
|
||||
next_version,
|
||||
previous_revision: previous.and_then(|(_, c)| c.clone()),
|
||||
next_revision: next_revision.clone(),
|
||||
scope,
|
||||
related: Vec::new(),
|
||||
exact: next_revision.is_some(),
|
||||
});
|
||||
}
|
||||
txns.sort_by(|a, b| a.identity.cmp(&b.identity));
|
||||
txns
|
||||
}
|
||||
|
||||
/// Resolve the exact Flatpak update transactions for the target.
|
||||
pub(crate) fn flatpak_update_transactions(
|
||||
exec: &Exec,
|
||||
root: &Path,
|
||||
) -> Result<Vec<UpdateTransaction>> {
|
||||
if !flatpak_available(exec, root) {
|
||||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
|
|
@ -322,7 +386,8 @@ fn flatpak_update_effects(exec: &Exec, root: &Path) -> Result<Vec<PackageEffect>
|
|||
&[
|
||||
"remote-ls".into(),
|
||||
"--updates".into(),
|
||||
"--columns=application,version,branch".into(),
|
||||
"--columns=ref,application,version,branch,commit,origin,installation,type,arch"
|
||||
.into(),
|
||||
],
|
||||
))
|
||||
.map_err(|e| {
|
||||
|
|
@ -342,49 +407,94 @@ fn flatpak_update_effects(exec: &Exec, root: &Path) -> Result<Vec<PackageEffect>
|
|||
),
|
||||
));
|
||||
}
|
||||
let mut current = std::collections::BTreeMap::new();
|
||||
if let Ok(list) = exec.runner.execute(&target_spec(
|
||||
let installed = exec
|
||||
.runner
|
||||
.execute(&target_spec(
|
||||
root,
|
||||
"sys",
|
||||
"resolve flatpak updates",
|
||||
"flatpak",
|
||||
&[
|
||||
"list".into(),
|
||||
"--app".into(),
|
||||
"--columns=application,version".into(),
|
||||
"--columns=ref,application,version,branch,commit,installation,type,arch".into(),
|
||||
],
|
||||
)) {
|
||||
if list.success {
|
||||
for line in list.stdout_lossy().lines() {
|
||||
let mut parts = line.split_whitespace();
|
||||
if let (Some(id), Some(version)) = (parts.next(), parts.next()) {
|
||||
current.insert(id.to_string(), version.to_string());
|
||||
))
|
||||
.map_err(|e| {
|
||||
ExecError::operation(
|
||||
"sys",
|
||||
"resolve flatpak updates",
|
||||
format!("flatpak list could not run: {e}"),
|
||||
)
|
||||
})?;
|
||||
if !installed.success {
|
||||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
"resolve flatpak updates",
|
||||
format!("flatpak list failed: {}", installed.stderr_lossy().trim()),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut effects = Vec::new();
|
||||
for line in updates.stdout_lossy().lines() {
|
||||
let mut parts = line.split_whitespace();
|
||||
let (Some(id), Some(new_version)) = (parts.next(), parts.next()) else {
|
||||
continue;
|
||||
};
|
||||
if id.is_empty() || new_version.is_empty() {
|
||||
continue;
|
||||
}
|
||||
effects.push(PackageEffect {
|
||||
target: format!("{id}-{new_version}"),
|
||||
previous: current.get(id).cloned(),
|
||||
kind: EffectKind::Upgrade,
|
||||
source: EffectSource::Binary,
|
||||
});
|
||||
}
|
||||
Ok(effects)
|
||||
Ok(parse_flatpak_transactions(
|
||||
&updates.stdout_lossy(),
|
||||
&installed.stdout_lossy(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Exact per-snap update effects. Snaps act on the running daemon, so an
|
||||
/// alternate root is refused before any command is issued.
|
||||
fn snap_update_effects(exec: &Exec, root: &Path) -> Result<Vec<PackageEffect>> {
|
||||
/// Parse `snap refresh --list` and `snap list` output into exact update
|
||||
/// transactions. Columns: `Name Version Rev Tracking Publisher Notes`; the
|
||||
/// revision is the immutable identity and the tracking channel is the scope.
|
||||
pub(crate) fn parse_snap_transactions(refresh: &str, installed: &str) -> Vec<UpdateTransaction> {
|
||||
use std::collections::BTreeMap;
|
||||
// name -> (version, revision, tracking, notes)
|
||||
let mut current: BTreeMap<String, (Option<String>, Option<String>, Option<String>, String)> =
|
||||
BTreeMap::new();
|
||||
for line in installed.lines().skip(1) {
|
||||
let cols: Vec<&str> = line.split_whitespace().collect();
|
||||
if cols.len() < 4 || cols[0].is_empty() {
|
||||
continue;
|
||||
}
|
||||
current.insert(
|
||||
cols[0].to_string(),
|
||||
(
|
||||
Some(cols[1].to_string()),
|
||||
Some(cols[2].to_string()),
|
||||
Some(cols[3].to_string()),
|
||||
cols.get(5..).map(|n| n.join(" ")).unwrap_or_default(),
|
||||
),
|
||||
);
|
||||
}
|
||||
let mut txns = Vec::new();
|
||||
for line in refresh.lines().skip(1) {
|
||||
let cols: Vec<&str> = line.split_whitespace().collect();
|
||||
if cols.len() < 3 || cols[0].is_empty() {
|
||||
continue;
|
||||
}
|
||||
let name = cols[0].to_string();
|
||||
let previous = current.get(&name);
|
||||
let mut related = Vec::new();
|
||||
if let Some((_, _, _, notes)) = previous {
|
||||
if !notes.trim().is_empty() {
|
||||
related.push(format!("notes: {notes}"));
|
||||
}
|
||||
}
|
||||
txns.push(UpdateTransaction {
|
||||
source: UpdateSource::Snap,
|
||||
identity: name,
|
||||
previous_version: previous.and_then(|(v, _, _, _)| v.clone()),
|
||||
next_version: cols[1].to_string(),
|
||||
previous_revision: previous.and_then(|(_, r, _, _)| r.clone()),
|
||||
next_revision: Some(cols[2].to_string()),
|
||||
scope: Some(cols[3].to_string()),
|
||||
related,
|
||||
exact: !cols[2].is_empty(),
|
||||
});
|
||||
}
|
||||
txns.sort_by(|a, b| a.identity.cmp(&b.identity));
|
||||
txns
|
||||
}
|
||||
|
||||
/// Resolve the exact Snap update transactions. Snaps act on the running
|
||||
/// daemon, so an alternate root is refused before any command is issued.
|
||||
pub(crate) fn snap_update_transactions(exec: &Exec, root: &Path) -> Result<Vec<UpdateTransaction>> {
|
||||
if !is_host_root(root) {
|
||||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
|
|
@ -401,42 +511,45 @@ fn snap_update_effects(exec: &Exec, root: &Path) -> Result<Vec<PackageEffect>> {
|
|||
"snapd is not installed or active",
|
||||
));
|
||||
}
|
||||
let updates = exec.runner.execute(
|
||||
let refresh = exec.runner.execute(
|
||||
&CommandSpec::new("sys", "resolve snap updates", "snap").args(["refresh", "--list"]),
|
||||
)?;
|
||||
if !updates.success {
|
||||
if !refresh.success {
|
||||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
"resolve snap updates",
|
||||
format!(
|
||||
"snap refresh --list failed: {}",
|
||||
updates.stderr_lossy().trim()
|
||||
refresh.stderr_lossy().trim()
|
||||
),
|
||||
));
|
||||
}
|
||||
let mut current = std::collections::BTreeMap::new();
|
||||
if let Ok(entries) = snap::list(exec) {
|
||||
for entry in entries {
|
||||
current.insert(entry.name, entry.version);
|
||||
let installed = exec
|
||||
.runner
|
||||
.execute(&CommandSpec::new("sys", "resolve snap updates", "snap").args(["list"]))?;
|
||||
if !installed.success {
|
||||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
"resolve snap updates",
|
||||
format!("snap list failed: {}", installed.stderr_lossy().trim()),
|
||||
));
|
||||
}
|
||||
}
|
||||
let mut effects = Vec::new();
|
||||
for line in updates.stdout_lossy().lines().skip(1) {
|
||||
let mut parts = line.split_whitespace();
|
||||
let (Some(name), Some(new_version)) = (parts.next(), parts.next()) else {
|
||||
continue;
|
||||
};
|
||||
if name.is_empty() || new_version.is_empty() {
|
||||
continue;
|
||||
}
|
||||
effects.push(PackageEffect {
|
||||
target: format!("{name}-{new_version}"),
|
||||
previous: current.get(name).cloned(),
|
||||
Ok(parse_snap_transactions(
|
||||
&refresh.stdout_lossy(),
|
||||
&installed.stdout_lossy(),
|
||||
))
|
||||
}
|
||||
|
||||
/// The human-readable `PackageEffect` summary for a set of update transactions.
|
||||
fn update_effect_summary(txns: &[UpdateTransaction]) -> Vec<PackageEffect> {
|
||||
txns.iter()
|
||||
.map(|txn| PackageEffect {
|
||||
target: format!("{}-{}", txn.identity, txn.next_version),
|
||||
previous: txn.previous_version.clone(),
|
||||
kind: EffectKind::Upgrade,
|
||||
source: EffectSource::Binary,
|
||||
});
|
||||
}
|
||||
Ok(effects)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Resolve a reviewable, per-source update plan. Each lane contributes exact
|
||||
|
|
@ -489,16 +602,22 @@ fn resolve_update_effects(
|
|||
}
|
||||
plan.consequences.extend(parsed.consequences);
|
||||
} else if lane == Backend::Flatpak {
|
||||
match flatpak_update_effects(exec, root) {
|
||||
Ok(effects) => plan.effects.extend(effects),
|
||||
match flatpak_update_transactions(exec, root) {
|
||||
Ok(txns) => {
|
||||
plan.effects.extend(update_effect_summary(&txns));
|
||||
plan.updates.extend(txns);
|
||||
}
|
||||
Err(e) if backend.is_none() => plan
|
||||
.consequences
|
||||
.push(format!("[flatpak] updates could not be resolved: {e}")),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
} else if lane == Backend::Snap {
|
||||
match snap_update_effects(exec, root) {
|
||||
Ok(effects) => plan.effects.extend(effects),
|
||||
match snap_update_transactions(exec, root) {
|
||||
Ok(txns) => {
|
||||
plan.effects.extend(update_effect_summary(&txns));
|
||||
plan.updates.extend(txns);
|
||||
}
|
||||
Err(e) if backend.is_none() => plan
|
||||
.consequences
|
||||
.push(format!("[snap] updates could not be resolved: {e}")),
|
||||
|
|
@ -534,18 +653,47 @@ fn resolve_update_effects(
|
|||
Ok(plan)
|
||||
}
|
||||
|
||||
/// The cache cleaners for a lane, as `(cleaner, dry-run args, destructive args)`.
|
||||
/// The cache cleaners for a lane, as `(cleaner, preview args, destructive args)`.
|
||||
///
|
||||
/// `eclean-dist`/`eclean-pkg` **delete by default**; only `--pretend` previews
|
||||
/// (verified against the upstream `gentoolkit` CLI). The preview therefore
|
||||
/// passes `--pretend` explicitly, and both the preview and the destructive run
|
||||
/// use the *same* selection policy (`--deep`), so what is reviewed is exactly
|
||||
/// what is removed.
|
||||
pub(crate) fn portage_cleaners(root: &Path) -> Vec<(&'static str, Vec<String>, Vec<String>)> {
|
||||
let mut cleaners = Vec::new();
|
||||
if crate::exec::tool_in_target(root, "eclean-dist") {
|
||||
cleaners.push(("eclean-dist", vec![], vec!["-d".to_string()]));
|
||||
for cleaner in ["eclean-dist", "eclean-pkg"] {
|
||||
if crate::exec::tool_in_target(root, cleaner) {
|
||||
cleaners.push((
|
||||
cleaner,
|
||||
vec!["--pretend".to_string(), "--deep".to_string()],
|
||||
vec!["--deep".to_string()],
|
||||
));
|
||||
}
|
||||
if crate::exec::tool_in_target(root, "eclean-pkg") {
|
||||
cleaners.push(("eclean-pkg", vec![], vec!["-d".to_string()]));
|
||||
}
|
||||
cleaners
|
||||
}
|
||||
|
||||
/// Collect the items a cleaner reported, from *both* streams.
|
||||
///
|
||||
/// The upstream tools do not guarantee which stream the reviewable list lands
|
||||
/// on, so stdout and stderr are combined; only non-empty, trimmed lines become
|
||||
/// bound items.
|
||||
fn cleanup_items(output: &CommandOutput) -> Vec<String> {
|
||||
let mut text = output.stdout_lossy();
|
||||
if !output.stderr.is_empty() {
|
||||
if !text.is_empty() && !text.ends_with('\n') {
|
||||
text.push('\n');
|
||||
}
|
||||
text.push_str(&output.stderr_lossy());
|
||||
}
|
||||
text.lines()
|
||||
.map(str::trim)
|
||||
.filter(|l| !l.is_empty())
|
||||
.map(str::to_string)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Resolve a reviewable cache-cleanup plan from non-destructive dry runs. A
|
||||
/// cleaner that cannot run is recorded as unavailable, distinct from one that
|
||||
/// ran and found nothing.
|
||||
|
|
@ -564,26 +712,19 @@ fn resolve_cleanup_effects(
|
|||
};
|
||||
for lane in lanes {
|
||||
if lane.is_portage() {
|
||||
for (cleaner, dry_args, _) in portage_cleaners(root) {
|
||||
for (cleaner, preview_args, _) in portage_cleaners(root) {
|
||||
let out = exec.runner.execute(&target_spec(
|
||||
root,
|
||||
"sys",
|
||||
"resolve cache cleanup",
|
||||
cleaner,
|
||||
&dry_args,
|
||||
&preview_args,
|
||||
));
|
||||
match out {
|
||||
Ok(output) if output.success => {
|
||||
let items = output
|
||||
.stdout_lossy()
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.filter(|l| !l.is_empty())
|
||||
.map(str::to_string)
|
||||
.collect();
|
||||
plan.cleanup.push(CleanupEffect {
|
||||
cleaner: cleaner.to_string(),
|
||||
items,
|
||||
items: cleanup_items(&output),
|
||||
unavailable: false,
|
||||
});
|
||||
}
|
||||
|
|
@ -591,14 +732,14 @@ fn resolve_cleanup_effects(
|
|||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
"resolve cache cleanup",
|
||||
format!("{cleaner} dry run failed: {}", output.stderr_lossy().trim()),
|
||||
format!("{cleaner} preview failed: {}", output.stderr_lossy().trim()),
|
||||
));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
"resolve cache cleanup",
|
||||
format!("{cleaner} dry run could not run: {e}"),
|
||||
format!("{cleaner} preview could not run: {e}"),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
@ -611,25 +752,21 @@ fn resolve_cleanup_effects(
|
|||
unavailable: true,
|
||||
});
|
||||
} else {
|
||||
// `nix-store --gc` deletes by default; the documented,
|
||||
// non-destructive preview is `--print-dead` (the `--dry-run`
|
||||
// global is not documented for the `--gc` operation).
|
||||
let out = exec.runner.execute(&target_spec(
|
||||
root,
|
||||
"sys",
|
||||
"resolve cache cleanup",
|
||||
"/nix/var/nix/profiles/default/bin/nix-store",
|
||||
&["--gc".into(), "--dry-run".into()],
|
||||
&["--gc".into(), "--print-dead".into()],
|
||||
));
|
||||
match out {
|
||||
Ok(output) if output.success => {
|
||||
let items = output
|
||||
.stdout_lossy()
|
||||
.lines()
|
||||
.map(str::trim)
|
||||
.filter(|l| !l.is_empty())
|
||||
.map(str::to_string)
|
||||
.collect();
|
||||
plan.cleanup.push(CleanupEffect {
|
||||
cleaner: "nix-store --gc".into(),
|
||||
items,
|
||||
items: cleanup_items(&output),
|
||||
unavailable: false,
|
||||
});
|
||||
}
|
||||
|
|
@ -637,14 +774,17 @@ fn resolve_cleanup_effects(
|
|||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
"resolve cache cleanup",
|
||||
format!("nix-store --gc failed: {}", output.stderr_lossy().trim()),
|
||||
format!(
|
||||
"nix-store --gc preview failed: {}",
|
||||
output.stderr_lossy().trim()
|
||||
),
|
||||
));
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(ExecError::operation(
|
||||
"sys",
|
||||
"resolve cache cleanup",
|
||||
format!("nix-store --gc could not run: {e}"),
|
||||
format!("nix-store --gc preview could not run: {e}"),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
@ -677,7 +817,7 @@ pub fn execute_checked(
|
|||
root: &Path,
|
||||
approved: Option<&ResolvedPlan>,
|
||||
) -> Result<ActionOutcome> {
|
||||
if let Some(approved) = approved {
|
||||
let current = if let Some(approved) = approved {
|
||||
let current = resolve_effects(exec, action, root)?;
|
||||
if changed_materially(approved, ¤t) {
|
||||
return Err(ExecError::operation(
|
||||
|
|
@ -691,8 +831,11 @@ pub fn execute_checked(
|
|||
),
|
||||
));
|
||||
}
|
||||
}
|
||||
execute(exec, action, auth, root)
|
||||
Some(current)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
execute_inner(exec, action, auth, root, current.as_ref())
|
||||
}
|
||||
|
||||
/// The result of running a reviewed multi-action plan.
|
||||
|
|
@ -807,6 +950,21 @@ pub fn execute(
|
|||
action: &SystemAction,
|
||||
auth: Option<&DestructiveAuthorization>,
|
||||
root: &Path,
|
||||
) -> Result<ActionOutcome> {
|
||||
execute_inner(exec, action, auth, root, None)
|
||||
}
|
||||
|
||||
/// Execute an action, optionally carrying the reviewed plan through to
|
||||
/// execution. Actions whose mutation must equal the reviewed transaction
|
||||
/// (Flatpak/Snap updates, cache cleanup) consume `resolved` instead of
|
||||
/// re-discovering state; without it they refuse the mutation rather than
|
||||
/// presenting approximate approval as exact.
|
||||
pub fn execute_inner(
|
||||
exec: &Exec,
|
||||
action: &SystemAction,
|
||||
auth: Option<&DestructiveAuthorization>,
|
||||
root: &Path,
|
||||
resolved: Option<&ResolvedPlan>,
|
||||
) -> Result<ActionOutcome> {
|
||||
// The execution boundary validates every action regardless of its source
|
||||
// (manual phrase, JSON, or AI plan), so an invalid value can never reach a
|
||||
|
|
@ -1290,7 +1448,7 @@ pub fn execute(
|
|||
crate::query::check_updates(exec, root, *backend).render()
|
||||
}
|
||||
SystemAction::ApplyUpdates { backend } => {
|
||||
crate::query::apply_updates(exec, root, *backend)?
|
||||
crate::query::apply_updates(exec, root, *backend, resolved)?
|
||||
}
|
||||
SystemAction::ListPackages { backend, filter } => {
|
||||
crate::query::inventory(exec, root, *backend, filter.as_deref()).render()
|
||||
|
|
@ -1303,7 +1461,9 @@ pub fn execute(
|
|||
crate::query::package_dependencies(exec, root, package, *reverse)?
|
||||
}
|
||||
SystemAction::Depclean => crate::query::depclean(exec, root)?,
|
||||
SystemAction::CleanCache { backend } => crate::query::clean_cache(exec, root, *backend)?,
|
||||
SystemAction::CleanCache { backend } => {
|
||||
crate::query::clean_cache(exec, root, *backend, resolved)?
|
||||
}
|
||||
SystemAction::CreateSnapshot { operation } => {
|
||||
let auth = auth.ok_or_else(|| ExecError::NotAuthorized(action.describe()))?;
|
||||
let source = root.to_string_lossy().to_string();
|
||||
|
|
@ -1960,6 +2120,185 @@ Total: 1 package (1 new), Size of downloads: 512 KiB
|
|||
assert_eq!(runner.recorded().len(), 1, "{:?}", runner.recorded());
|
||||
}
|
||||
|
||||
fn install_fake_tool(root: &Path, name: &str) {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let bin = root.join("usr/bin");
|
||||
std::fs::create_dir_all(&bin).unwrap();
|
||||
let tool = bin.join(name);
|
||||
std::fs::write(&tool, "#!/bin/sh\n").unwrap();
|
||||
std::fs::set_permissions(&tool, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cleaner_preview_is_non_destructive_and_matches_execution_policy() {
|
||||
let root = tempfile::tempdir().unwrap();
|
||||
install_fake_tool(root.path(), "eclean-dist");
|
||||
let cleaners = portage_cleaners(root.path());
|
||||
assert_eq!(cleaners.len(), 1);
|
||||
let (_, preview, destructive) = &cleaners[0];
|
||||
// `eclean` deletes by default: the preview must be explicit.
|
||||
assert!(preview.iter().any(|a| a == "--pretend"), "{preview:?}");
|
||||
assert!(
|
||||
!destructive.iter().any(|a| a == "--pretend"),
|
||||
"the destructive run must not pretend: {destructive:?}"
|
||||
);
|
||||
// Same selection policy: the only difference is the preview flag.
|
||||
let mut selection = preview.clone();
|
||||
selection.retain(|a| a != "--pretend");
|
||||
assert_eq!(
|
||||
&selection, destructive,
|
||||
"preview policy must match execution"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_cleanup_effects_never_runs_a_destructive_cleaner() {
|
||||
let runner = RecordingRunner::new();
|
||||
runner.set_response(
|
||||
"chroot",
|
||||
CommandOutput {
|
||||
status: 0,
|
||||
success: true,
|
||||
stdout: b"/var/cache/distfiles/old.tar.gz\n".to_vec(),
|
||||
stderr: b"Here are the distfiles that would be deleted:\n".to_vec(),
|
||||
duration: std::time::Duration::from_millis(1),
|
||||
},
|
||||
);
|
||||
let exec = make_exec(runner.clone());
|
||||
let root = Path::new(&exec.root);
|
||||
install_fake_tool(root, "eclean-dist");
|
||||
let plan = resolve_effects(
|
||||
&exec,
|
||||
&SystemAction::CleanCache {
|
||||
backend: Some(Backend::PortageSource),
|
||||
},
|
||||
root,
|
||||
)
|
||||
.unwrap();
|
||||
// Items are taken from both streams and bound to the plan.
|
||||
let items = &plan.cleanup[0].items;
|
||||
assert!(items.iter().any(|i| i.contains("old.tar.gz")), "{items:?}");
|
||||
assert!(
|
||||
items.iter().any(|i| i.contains("would be deleted")),
|
||||
"{items:?}"
|
||||
);
|
||||
// Every invocation was a preview; no destructive `eclean` ran.
|
||||
for spec in runner.recorded() {
|
||||
assert!(
|
||||
spec.args.iter().any(|a| a == "--pretend"),
|
||||
"planning must only preview: {spec:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clean_cache_revalidation_only_previews_and_aborts_on_drift() {
|
||||
// Rank 1: reviewed preview (items X).
|
||||
let approved_runner = RecordingRunner::new();
|
||||
approved_runner.set_response(
|
||||
"chroot",
|
||||
response("Here are the distfiles that would be deleted:\n/var/cache/old-a.tar.gz\n"),
|
||||
);
|
||||
let approved_exec = make_exec(approved_runner);
|
||||
let approved_root = Path::new(&approved_exec.root);
|
||||
install_fake_tool(approved_root, "eclean-dist");
|
||||
let approved = resolve_effects(
|
||||
&approved_exec,
|
||||
&SystemAction::CleanCache {
|
||||
backend: Some(Backend::PortageSource),
|
||||
},
|
||||
approved_root,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Execution-time re-resolution previews a *different* item set.
|
||||
let runner = RecordingRunner::new();
|
||||
runner.set_response(
|
||||
"chroot",
|
||||
response("Here are the distfiles that would be deleted:\n/var/cache/old-b.tar.gz\n"),
|
||||
);
|
||||
let exec = make_exec(runner.clone());
|
||||
let root = Path::new(&exec.root);
|
||||
install_fake_tool(root, "eclean-dist");
|
||||
let err = execute_checked(
|
||||
&exec,
|
||||
&SystemAction::CleanCache {
|
||||
backend: Some(Backend::PortageSource),
|
||||
},
|
||||
Some(&auth()),
|
||||
root,
|
||||
Some(&approved),
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(err.to_string().contains("plan changed"), "{err}");
|
||||
// Only the non-destructive preview ran; the destructive `--deep`-only
|
||||
// invocation never did.
|
||||
for spec in runner.recorded() {
|
||||
assert!(
|
||||
spec.args.iter().any(|a| a == "--pretend"),
|
||||
"a destructive cleaner ran during revalidation: {spec:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clean_cache_without_authorization_runs_no_cleaner() {
|
||||
let runner = RecordingRunner::new();
|
||||
let exec = make_exec(runner.clone());
|
||||
let root = Path::new(&exec.root);
|
||||
install_fake_tool(root, "eclean-dist");
|
||||
let err = execute(
|
||||
&exec,
|
||||
&SystemAction::CleanCache {
|
||||
backend: Some(Backend::PortageSource),
|
||||
},
|
||||
None,
|
||||
root,
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, ExecError::NotAuthorized(_)));
|
||||
// A cancelled/unauthorized approval must not touch the cache.
|
||||
assert!(runner.recorded().is_empty(), "{:?}", runner.recorded());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nix_cleanup_preview_uses_the_documented_print_dead_and_not_gc() {
|
||||
let runner = RecordingRunner::new();
|
||||
runner.set_response(
|
||||
"chroot",
|
||||
CommandOutput {
|
||||
status: 0,
|
||||
success: true,
|
||||
stdout: b"/nix/store/dead-path-1\n".to_vec(),
|
||||
stderr: Vec::new(),
|
||||
duration: std::time::Duration::from_millis(1),
|
||||
},
|
||||
);
|
||||
let exec = make_exec(runner.clone());
|
||||
let root = Path::new(&exec.root);
|
||||
// Pretend Nix is installed by creating the pinned store path.
|
||||
std::fs::create_dir_all(root.join(format!("nix/store/{}", crate::nix::NIX_STORE_NAME)))
|
||||
.unwrap();
|
||||
let plan = resolve_effects(
|
||||
&exec,
|
||||
&SystemAction::CleanCache {
|
||||
backend: Some(Backend::Nix),
|
||||
},
|
||||
root,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(plan.cleanup[0].cleaner, "nix-store --gc");
|
||||
assert!(!runner.recorded().is_empty());
|
||||
for spec in runner.recorded() {
|
||||
let has_gc = spec.args.iter().any(|a| a == "--gc");
|
||||
let has_print_dead = spec.args.iter().any(|a| a == "--print-dead");
|
||||
assert!(
|
||||
!has_gc || has_print_dead,
|
||||
"a bare `--gc` deletes; preview must use --print-dead: {spec:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn portage_apply_updates_previews_the_world_plan() {
|
||||
let runner = RecordingRunner::new();
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ pub const NIX_URL: &str =
|
|||
"https://releases.nixos.org/nix/nix-2.24.9/nix-2.24.9-x86_64-linux.tar.xz";
|
||||
pub const NIX_SHA256: &str = "3c0779e4878d1289cf3fbb158ec5ea9bdf61dfb9b4efac6b3b0b6bec5ba4cf13";
|
||||
const NIX_TARBALL_DIR: &str = "nix-2.24.9-x86_64-linux";
|
||||
const NIX_STORE_NAME: &str = "2nhrwv91g6ycpyxvhmvc0xs8p92wp4bk-nix-2.24.9";
|
||||
pub(crate) const NIX_STORE_NAME: &str = "2nhrwv91g6ycpyxvhmvc0xs8p92wp4bk-nix-2.24.9";
|
||||
const NIX_BUILD_USERS: u32 = 8;
|
||||
|
||||
fn nix_store_bin() -> String {
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -443,6 +443,7 @@ fn validate_effect_bindings(plan: &PreparedPlan, steps: &[ResolvedPlan]) -> Resu
|
|||
for step in steps {
|
||||
combined.effects.extend(step.effects.clone());
|
||||
combined.cleanup.extend(step.cleanup.clone());
|
||||
combined.updates.extend(step.updates.clone());
|
||||
combined.consequences.extend(step.consequences.clone());
|
||||
}
|
||||
combined.recompute_digest();
|
||||
|
|
@ -1305,6 +1306,7 @@ pub fn plan_general(
|
|||
if eff.established {
|
||||
combined_effects.effects.extend(eff.effects.clone());
|
||||
combined_effects.cleanup.extend(eff.cleanup.clone());
|
||||
combined_effects.updates.extend(eff.updates.clone());
|
||||
combined_effects
|
||||
.consequences
|
||||
.extend(eff.consequences.clone());
|
||||
|
|
@ -1588,6 +1590,7 @@ fn revalidate_plan_state(exec: &Exec, root: &Path, plan: &PreparedPlan) -> Resul
|
|||
if eff.established {
|
||||
combined.effects.extend(eff.effects);
|
||||
combined.cleanup.extend(eff.cleanup);
|
||||
combined.updates.extend(eff.updates);
|
||||
combined.consequences.extend(eff.consequences);
|
||||
}
|
||||
}
|
||||
|
|
@ -1874,10 +1877,10 @@ fn execute_claimed(
|
|||
// `validate_prepared_plan` (install revalidation / installed-copy
|
||||
// checks); re-resolving them here would see an empty plan and falsely
|
||||
// report drift.
|
||||
let mut current = ResolvedPlan::default();
|
||||
let mut current: Option<ResolvedPlan> = None;
|
||||
if actions::resolves_effects(action) {
|
||||
let reviewed = &reviewed_steps[idx];
|
||||
current = match actions::resolve_effects(exec, action, root) {
|
||||
let resolved = match actions::resolve_effects(exec, action, root) {
|
||||
Ok(current) => current,
|
||||
Err(e) => {
|
||||
let detail = format!("effect resolution failed for `{described}`: {e}");
|
||||
|
|
@ -1889,7 +1892,7 @@ fn execute_claimed(
|
|||
continue;
|
||||
}
|
||||
};
|
||||
if !approval_holds(reviewed, ¤t, &introduced) {
|
||||
if !approval_holds(reviewed, &resolved, &introduced) {
|
||||
let detail = format!(
|
||||
"the reviewed effects for `{described}` changed materially before execution"
|
||||
);
|
||||
|
|
@ -1900,9 +1903,17 @@ fn execute_claimed(
|
|||
drift = Some(format!("stale plan: {detail}"));
|
||||
continue;
|
||||
}
|
||||
current = Some(resolved);
|
||||
}
|
||||
|
||||
match actions::execute(exec, action, auth, root) {
|
||||
// Execution carries the *reviewed* transaction for actions that must
|
||||
// not re-discover state (Flatpak/Snap updates, cache cleanup).
|
||||
let carried = if actions::resolves_effects(action) {
|
||||
Some(&reviewed_steps[idx])
|
||||
} else {
|
||||
None
|
||||
};
|
||||
match actions::execute_inner(exec, action, auth, root, carried) {
|
||||
Ok(result) => {
|
||||
if result.snapshot.is_some() {
|
||||
snapshot = result.snapshot;
|
||||
|
|
@ -1911,8 +1922,10 @@ fn execute_claimed(
|
|||
completed.push((described, result.output));
|
||||
// Packages this approved action installed may legitimately
|
||||
// appear in a later step's re-resolution.
|
||||
if let Some(current) = current {
|
||||
introduced.extend(current.effects);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
let detail = e.to_string();
|
||||
outcomes.push(TransactionOutcome::failed(
|
||||
|
|
@ -3735,6 +3748,7 @@ mod tests {
|
|||
for step in &steps {
|
||||
combined.effects.extend(step.effects.clone());
|
||||
combined.cleanup.extend(step.cleanup.clone());
|
||||
combined.updates.extend(step.updates.clone());
|
||||
combined.consequences.extend(step.consequences.clone());
|
||||
}
|
||||
combined.recompute_digest();
|
||||
|
|
@ -4085,6 +4099,38 @@ mod tests {
|
|||
.expect("cleanup intents must aggregate into the bound plan");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effect_binding_aggregates_update_transactions() {
|
||||
let root = tempdir().unwrap();
|
||||
let mut step = ResolvedPlan {
|
||||
established: true,
|
||||
..Default::default()
|
||||
};
|
||||
step.updates.push(phoebus_core::effects::UpdateTransaction {
|
||||
source: phoebus_core::effects::UpdateSource::Flatpak,
|
||||
identity: "app/org.mozilla.firefox/x86_64/stable".into(),
|
||||
previous_version: Some("128".into()),
|
||||
next_version: "129".into(),
|
||||
previous_revision: Some("OLD".into()),
|
||||
next_revision: Some("NEW".into()),
|
||||
scope: Some("system".into()),
|
||||
related: Vec::new(),
|
||||
exact: true,
|
||||
});
|
||||
let plan = general_portage_plan(
|
||||
"plan-updates",
|
||||
vec![SystemAction::ApplyUpdates {
|
||||
backend: Some(Backend::Flatpak),
|
||||
}],
|
||||
vec![step],
|
||||
root.path(),
|
||||
effective_uid(),
|
||||
);
|
||||
let steps = reviewed_steps(&plan).unwrap();
|
||||
validate_effect_bindings(&plan, &steps)
|
||||
.expect("update transactions must aggregate into the bound plan");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constrain_action_rewrites_optional_backend_discovery_before_querying() {
|
||||
let discover = constrain_action(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue