2378 lines
88 KiB
Rust
2378 lines
88 KiB
Rust
//! Unified package catalogue: search, inventory, information and maintenance.
|
|
//!
|
|
//! Every result is derived from real backend metadata through the same
|
|
//! argument-vector command layer used elsewhere. A lane reports one of three
|
|
//! honest states: **unavailable** (tooling absent), **failed** (the tool ran
|
|
//! and did not answer), or **empty** (a successful query with no results).
|
|
//! Package facts are never invented, and no query mutates the system.
|
|
|
|
use std::path::Path;
|
|
|
|
use phoebus_core::catalog::{
|
|
CatalogEntry, CatalogReport, PackageInfoMatch, PackageInfoReport, SourceReport,
|
|
};
|
|
use phoebus_core::effects::{ResolvedPlan, UpdateSource, UpdateTransaction};
|
|
use phoebus_core::selection::Backend;
|
|
use phoebus_core::update::UnifiedUpdateOverview;
|
|
|
|
use crate::command::{CommandOutput, CommandSpec};
|
|
use crate::error::{ExecError, Result};
|
|
use crate::exec::Exec;
|
|
use crate::{appimage, chooser, ebuild, nix, portage, snap};
|
|
|
|
/// The lanes an unfiltered request covers, in display order.
|
|
fn all_lanes() -> Vec<Backend> {
|
|
vec![
|
|
Backend::PortageSource,
|
|
Backend::Flatpak,
|
|
Backend::Snap,
|
|
Backend::AppImage,
|
|
Backend::Nix,
|
|
Backend::CustomEbuild,
|
|
]
|
|
}
|
|
|
|
fn selected_lanes(filter: Option<Backend>) -> Vec<Backend> {
|
|
match filter {
|
|
Some(backend) => vec![backend],
|
|
None => all_lanes(),
|
|
}
|
|
}
|
|
|
|
fn is_portage(backend: Backend) -> bool {
|
|
backend.is_portage() || backend == Backend::PortageSource
|
|
}
|
|
|
|
fn lane_matches(candidate: Backend, filter: Backend) -> bool {
|
|
candidate == filter || (is_portage(candidate) && is_portage(filter))
|
|
}
|
|
|
|
fn spec(root: &Path, program: &str, args: &[&str]) -> CommandSpec {
|
|
let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
|
|
if crate::actions::is_host_root(root) {
|
|
CommandSpec::new("query", "package catalogue", program).args(args)
|
|
} else {
|
|
portage::chroot_spec("query", "package catalogue", root, program, &args)
|
|
}
|
|
}
|
|
|
|
/// Capture a read-only command against the intended system.
|
|
fn capture(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
program: &str,
|
|
args: &[&str],
|
|
) -> std::result::Result<CommandOutput, ExecError> {
|
|
exec.runner.execute(&spec(root, program, args))
|
|
}
|
|
|
|
/// Run a mutating command against the intended system.
|
|
fn run_mut(exec: &Exec, root: &Path, program: &str, args: &[&str]) -> Result<()> {
|
|
exec.run(spec(root, program, args)).map(|_| ())
|
|
}
|
|
|
|
/// A command that could not even be started means the lane is unavailable, not
|
|
/// that the query failed.
|
|
fn is_unavailable(error: &ExecError) -> bool {
|
|
matches!(error, ExecError::Spawn { .. } | ExecError::MissingTool(_))
|
|
}
|
|
|
|
fn stderr_tail(text: &str) -> String {
|
|
let lines: Vec<&str> = text.lines().filter(|l| !l.trim().is_empty()).collect();
|
|
let tail = lines
|
|
.get(lines.len().saturating_sub(2)..)
|
|
.unwrap_or(&[])
|
|
.join("; ");
|
|
if tail.is_empty() {
|
|
"no diagnostic output".to_string()
|
|
} else {
|
|
tail
|
|
}
|
|
}
|
|
|
|
/// Whether the Flatpak client answers in the intended target. For an alternate
|
|
/// root the check runs inside the target chroot, never against the host.
|
|
pub(crate) fn flatpak_available(exec: &Exec, root: &Path) -> bool {
|
|
matches!(capture(exec, root, "flatpak", &["--version"]), Ok(o) if o.success)
|
|
}
|
|
|
|
/// Snap operations act on the running daemon and cannot be redirected into an
|
|
/// alternate root. Availability must therefore refer to the running system;
|
|
/// pretending a target lane is usable would mutate the host.
|
|
fn snap_target_usable(exec: &Exec, root: &Path) -> bool {
|
|
crate::actions::is_host_root(root) && matches!(snap::status(exec), Ok(s) if s.usable())
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Search
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Cross-source package search. `filter` constrains the search to one lane.
|
|
pub fn search(exec: &Exec, root: &Path, query: &str, filter: Option<Backend>) -> CatalogReport {
|
|
let mut report = CatalogReport::new(query);
|
|
for lane in selected_lanes(filter) {
|
|
let source = match lane {
|
|
Backend::PortageSource | Backend::PortageBinary | Backend::PortageVendor => {
|
|
search_portage(exec, root, query, lane)
|
|
}
|
|
Backend::Flatpak => search_flatpak(exec, root, query, lane),
|
|
Backend::Snap => search_snap(exec, root, query, lane),
|
|
Backend::AppImage => search_appimage(root, query, lane),
|
|
Backend::Nix => search_nix(exec, root, query, lane),
|
|
Backend::CustomEbuild => search_ebuild(root, query, lane),
|
|
};
|
|
report.push(source);
|
|
}
|
|
report
|
|
}
|
|
|
|
fn search_portage(exec: &Exec, root: &Path, query: &str, backend: Backend) -> SourceReport {
|
|
match capture(
|
|
exec,
|
|
root,
|
|
"emerge",
|
|
&["--search", "--color=n", "--", query],
|
|
) {
|
|
Ok(out) if out.success => {
|
|
let entries = portage::parse_search(&out.stdout_lossy())
|
|
.into_iter()
|
|
.map(|hit| {
|
|
let mut entry = CatalogEntry::new(backend, hit.atom)
|
|
.installed(hit.version_installed.is_some());
|
|
if let Some(version) = hit.version_available {
|
|
entry = entry.version(version);
|
|
}
|
|
if let Some(description) = hit.description {
|
|
entry = entry.description(description);
|
|
}
|
|
entry
|
|
})
|
|
.collect();
|
|
SourceReport::ok(backend, "Gentoo tree (emerge --search)", entries)
|
|
}
|
|
Ok(out) => SourceReport::failed(
|
|
backend,
|
|
format!(
|
|
"emerge --search failed: {}",
|
|
stderr_tail(&out.stderr_lossy())
|
|
),
|
|
),
|
|
Err(e) if is_unavailable(&e) => {
|
|
SourceReport::unsupported(backend, "Portage (emerge) is not present in the target")
|
|
}
|
|
Err(e) => SourceReport::failed(backend, format!("emerge --search could not run: {e}")),
|
|
}
|
|
}
|
|
|
|
fn parse_flatpak_lines(text: &str) -> Vec<Vec<String>> {
|
|
text.lines()
|
|
.filter(|l| !l.trim().is_empty())
|
|
.map(|line| {
|
|
if line.contains('\t') {
|
|
line.split('\t').map(|s| s.trim().to_string()).collect()
|
|
} else {
|
|
line.split_whitespace()
|
|
.map(|s| s.trim().to_string())
|
|
.collect()
|
|
}
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn installed_flatpak_ids(exec: &Exec, root: &Path) -> Option<Vec<String>> {
|
|
let out = capture(
|
|
exec,
|
|
root,
|
|
"flatpak",
|
|
&["list", "--app", "--columns=application"],
|
|
)
|
|
.ok()?;
|
|
if !out.success {
|
|
return None;
|
|
}
|
|
Some(
|
|
out.stdout_lossy()
|
|
.lines()
|
|
.map(|l| l.trim().to_string())
|
|
.filter(|l| !l.is_empty())
|
|
.collect(),
|
|
)
|
|
}
|
|
|
|
fn search_flatpak(exec: &Exec, root: &Path, query: &str, backend: Backend) -> SourceReport {
|
|
if !flatpak_available(exec, root) {
|
|
return SourceReport::unsupported(
|
|
backend,
|
|
"the Flatpak client is not available in the target",
|
|
);
|
|
}
|
|
let out = match capture(
|
|
exec,
|
|
root,
|
|
"flatpak",
|
|
&[
|
|
"search",
|
|
"--columns=application,version,description",
|
|
"--",
|
|
query,
|
|
],
|
|
) {
|
|
Ok(out) => out,
|
|
Err(e) => {
|
|
return SourceReport::failed(backend, format!("flatpak search could not run: {e}"))
|
|
}
|
|
};
|
|
if !out.success {
|
|
return SourceReport::failed(
|
|
backend,
|
|
format!(
|
|
"flatpak search failed: {}",
|
|
stderr_tail(&out.stderr_lossy())
|
|
),
|
|
);
|
|
}
|
|
let installed = installed_flatpak_ids(exec, root).unwrap_or_default();
|
|
let entries = parse_flatpak_lines(&out.stdout_lossy())
|
|
.into_iter()
|
|
.filter_map(|cols| {
|
|
let id = cols.first()?.clone();
|
|
if id.is_empty() {
|
|
return None;
|
|
}
|
|
let mut entry = CatalogEntry::new(backend, &id).installed(installed.contains(&id));
|
|
if let Some(version) = cols.get(1).filter(|v| !v.is_empty()) {
|
|
entry = entry.version(version.clone());
|
|
}
|
|
if let Some(description) = cols.get(2).filter(|v| !v.is_empty()) {
|
|
entry = entry.description(description.clone());
|
|
}
|
|
Some(entry)
|
|
})
|
|
.collect();
|
|
SourceReport::ok(backend, "flathub remote search", entries)
|
|
}
|
|
|
|
fn search_snap(exec: &Exec, root: &Path, query: &str, backend: Backend) -> SourceReport {
|
|
if !crate::actions::is_host_root(root) {
|
|
return SourceReport::unsupported(
|
|
backend,
|
|
"Snap cannot be queried for an alternate root; it acts on the running daemon",
|
|
);
|
|
}
|
|
if !snap_target_usable(exec, root) {
|
|
return SourceReport::unsupported(backend, "snapd is not installed or active");
|
|
}
|
|
let out = match capture(exec, root, "snap", &["find", query, "--color=never"]) {
|
|
Ok(out) => out,
|
|
Err(e) => return SourceReport::failed(backend, format!("snap find could not run: {e}")),
|
|
};
|
|
if !out.success {
|
|
return SourceReport::failed(
|
|
backend,
|
|
format!("snap find failed: {}", stderr_tail(&out.stderr_lossy())),
|
|
);
|
|
}
|
|
let installed: Vec<String> = snap::list(exec)
|
|
.map(|list| list.into_iter().map(|e| e.name).collect())
|
|
.unwrap_or_default();
|
|
let entries = snap::parse_find(&out.stdout_lossy())
|
|
.into_iter()
|
|
.map(|candidate| {
|
|
let mut entry = CatalogEntry::new(backend, &candidate.name)
|
|
.installed(installed.contains(&candidate.name));
|
|
if !candidate.version.is_empty() {
|
|
entry = entry.version(candidate.version);
|
|
}
|
|
if !candidate.summary.is_empty() {
|
|
entry = entry.description(candidate.summary);
|
|
}
|
|
entry
|
|
})
|
|
.collect();
|
|
SourceReport::ok(backend, "Snap Store search", entries)
|
|
}
|
|
|
|
fn search_appimage(root: &Path, query: &str, backend: Backend) -> SourceReport {
|
|
let lower = query.to_ascii_lowercase();
|
|
let entries = appimage::list(root)
|
|
.unwrap_or_default()
|
|
.into_iter()
|
|
.filter(|r| {
|
|
r.name.to_ascii_lowercase().contains(&lower)
|
|
|| r.provenance.to_ascii_lowercase().contains(&lower)
|
|
})
|
|
.map(|r| {
|
|
CatalogEntry::new(backend, r.name)
|
|
.version(r.version)
|
|
.description("managed AppImage (no sandbox)")
|
|
.installed(true)
|
|
})
|
|
.collect();
|
|
SourceReport::ok(backend, "Phoebus-managed AppImages", entries)
|
|
}
|
|
|
|
fn search_nix(exec: &Exec, root: &Path, query: &str, backend: Backend) -> SourceReport {
|
|
if !nix::installed(root) {
|
|
return SourceReport::unsupported(backend, "Nix is not installed");
|
|
}
|
|
let binary = "/nix/var/nix/profiles/default/bin/nix";
|
|
let out = match capture(exec, root, binary, &["search", "nixpkgs", query, "--json"]) {
|
|
Ok(out) => out,
|
|
Err(e) => return SourceReport::failed(backend, format!("nix search could not run: {e}")),
|
|
};
|
|
if !out.success {
|
|
return SourceReport::failed(
|
|
backend,
|
|
format!(
|
|
"nix search failed (offline or flake metadata unavailable): {}",
|
|
stderr_tail(&out.stderr_lossy())
|
|
),
|
|
);
|
|
}
|
|
let value: serde_json::Value = match serde_json::from_str(&out.stdout_lossy()) {
|
|
Ok(value) => value,
|
|
Err(e) => {
|
|
return SourceReport::failed(backend, format!("nix search returned no JSON: {e}"))
|
|
}
|
|
};
|
|
let mut entries = Vec::new();
|
|
if let Some(map) = value.as_object() {
|
|
for (attr, meta) in map {
|
|
let id = attr.rsplit('.').next().unwrap_or(attr).to_string();
|
|
let mut entry = CatalogEntry::new(backend, id);
|
|
if let Some(version) = meta.get("version").and_then(|v| v.as_str()) {
|
|
if !version.is_empty() {
|
|
entry = entry.version(version);
|
|
}
|
|
}
|
|
if let Some(description) = meta.get("description").and_then(|v| v.as_str()) {
|
|
if !description.is_empty() {
|
|
entry = entry.description(description);
|
|
}
|
|
}
|
|
entries.push(entry);
|
|
}
|
|
}
|
|
SourceReport::ok(backend, "nixpkgs search (realised metadata)", entries)
|
|
}
|
|
|
|
fn search_ebuild(root: &Path, query: &str, backend: Backend) -> SourceReport {
|
|
let lower = query.to_ascii_lowercase();
|
|
let entries = ebuild::list(root)
|
|
.into_iter()
|
|
.filter(|e| {
|
|
e.atom.to_ascii_lowercase().contains(&lower)
|
|
|| e.package_name().to_ascii_lowercase().contains(&lower)
|
|
})
|
|
.map(|e| CatalogEntry::new(backend, e.atom).version(e.version))
|
|
.collect();
|
|
SourceReport::ok(backend, "staged Phoebus overlay ebuilds", entries)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Inventory
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Installed-package inventory across the available lanes, optionally filtered
|
|
/// to one lane and by a free-text substring.
|
|
pub fn inventory(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
filter: Option<Backend>,
|
|
text: Option<&str>,
|
|
) -> CatalogReport {
|
|
let mut report = CatalogReport::new(text.unwrap_or("(all)"));
|
|
for lane in selected_lanes(filter) {
|
|
let source = match lane {
|
|
Backend::PortageSource | Backend::PortageBinary | Backend::PortageVendor => {
|
|
inventory_portage(root, lane)
|
|
}
|
|
Backend::Flatpak => inventory_flatpak(exec, root, lane),
|
|
Backend::Snap => inventory_snap(exec, root, lane),
|
|
Backend::AppImage => inventory_appimage(root, lane),
|
|
Backend::Nix => inventory_nix(root, lane),
|
|
Backend::CustomEbuild => SourceReport::unsupported(
|
|
lane,
|
|
"custom ebuilds are installed through Portage and are listed there, never twice",
|
|
),
|
|
};
|
|
report.push(source);
|
|
}
|
|
if let Some(text) = text {
|
|
let lower = text.to_ascii_lowercase();
|
|
for source in &mut report.sources {
|
|
source.entries.retain(|e| {
|
|
e.id.to_ascii_lowercase().contains(&lower)
|
|
|| e.description
|
|
.as_deref()
|
|
.map(|d| d.to_ascii_lowercase().contains(&lower))
|
|
.unwrap_or(false)
|
|
});
|
|
}
|
|
}
|
|
report
|
|
}
|
|
|
|
fn inventory_portage(root: &Path, backend: Backend) -> SourceReport {
|
|
let installed = portage::installed_packages(root);
|
|
let entries = installed
|
|
.into_iter()
|
|
.map(|p| {
|
|
CatalogEntry::new(backend, p.atom)
|
|
.version(p.version)
|
|
.installed(true)
|
|
})
|
|
.collect();
|
|
SourceReport::ok(backend, "Portage VDB (var/db/pkg)", entries)
|
|
}
|
|
|
|
fn inventory_flatpak(exec: &Exec, root: &Path, backend: Backend) -> SourceReport {
|
|
if !flatpak_available(exec, root) {
|
|
return SourceReport::unsupported(
|
|
backend,
|
|
"the Flatpak client is not available in the target",
|
|
);
|
|
}
|
|
let out = match capture(
|
|
exec,
|
|
root,
|
|
"flatpak",
|
|
&["list", "--app", "--columns=application,version,origin"],
|
|
) {
|
|
Ok(out) => out,
|
|
Err(e) => return SourceReport::failed(backend, format!("flatpak list could not run: {e}")),
|
|
};
|
|
if !out.success {
|
|
return SourceReport::failed(
|
|
backend,
|
|
format!("flatpak list failed: {}", stderr_tail(&out.stderr_lossy())),
|
|
);
|
|
}
|
|
let entries = parse_flatpak_lines(&out.stdout_lossy())
|
|
.into_iter()
|
|
.filter_map(|cols| {
|
|
let id = cols.first()?.clone();
|
|
if id.is_empty() {
|
|
return None;
|
|
}
|
|
let mut entry = CatalogEntry::new(backend, &id).installed(true);
|
|
if let Some(version) = cols.get(1).filter(|v| !v.is_empty()) {
|
|
entry = entry.version(version.clone());
|
|
}
|
|
if let Some(origin) = cols.get(2).filter(|v| !v.is_empty()) {
|
|
entry = entry.scope(origin.clone());
|
|
}
|
|
Some(entry)
|
|
})
|
|
.collect();
|
|
SourceReport::ok(backend, "installed Flatpak applications", entries)
|
|
}
|
|
|
|
fn inventory_snap(exec: &Exec, root: &Path, backend: Backend) -> SourceReport {
|
|
if !crate::actions::is_host_root(root) {
|
|
return SourceReport::unsupported(
|
|
backend,
|
|
"Snap cannot be inspected for an alternate root; it acts on the running daemon",
|
|
);
|
|
}
|
|
if !snap_target_usable(exec, root) {
|
|
return SourceReport::unsupported(backend, "snapd is not installed or active");
|
|
}
|
|
match snap::list(exec) {
|
|
Ok(list) => {
|
|
let entries = list
|
|
.into_iter()
|
|
.map(|e| {
|
|
let mut entry = CatalogEntry::new(backend, e.name)
|
|
.version(e.version)
|
|
.scope(e.channel)
|
|
.installed(true);
|
|
if !e.publisher.is_empty() {
|
|
entry = entry.description(format!("publisher: {}", e.publisher));
|
|
}
|
|
entry
|
|
})
|
|
.collect();
|
|
SourceReport::ok(backend, "installed snaps", entries)
|
|
}
|
|
Err(e) => SourceReport::failed(backend, format!("snap list failed: {e}")),
|
|
}
|
|
}
|
|
|
|
fn inventory_appimage(root: &Path, backend: Backend) -> SourceReport {
|
|
let entries = appimage::list(root)
|
|
.unwrap_or_default()
|
|
.into_iter()
|
|
.map(|r| {
|
|
CatalogEntry::new(backend, r.name)
|
|
.version(r.version)
|
|
.description(format!("{} ({})", r.provenance, r.arch))
|
|
.installed(true)
|
|
})
|
|
.collect();
|
|
SourceReport::ok(backend, "Phoebus-managed AppImages", entries)
|
|
}
|
|
|
|
fn inventory_nix(root: &Path, backend: Backend) -> SourceReport {
|
|
let Some(store_paths) = nix::store_path_count(root) else {
|
|
return SourceReport::unsupported(backend, "Nix is not installed");
|
|
};
|
|
let inventory = nix::installed_packages(root);
|
|
let entries = inventory
|
|
.packages
|
|
.into_iter()
|
|
.map(|p| {
|
|
let mut entry = CatalogEntry::new(backend, p.path)
|
|
.scope(p.profile)
|
|
.installed(true);
|
|
if let Some(version) = p.version {
|
|
entry = entry.version(version);
|
|
}
|
|
entry = entry.description(format!("Nix package `{}` ({})", p.name, p.store_path));
|
|
entry
|
|
})
|
|
.collect();
|
|
let mut note = format!(
|
|
"active Nix profiles; {store_paths} additional store path(s) are realised \
|
|
dependencies, not installed packages"
|
|
);
|
|
for additional in inventory.notes {
|
|
note.push_str("; ");
|
|
note.push_str(&additional);
|
|
}
|
|
SourceReport::ok(backend, note, entries)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Package information
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Detailed information about a package, across lanes unless filtered. An
|
|
/// ambiguous query returns every real match rather than guessing.
|
|
pub fn package_info(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
package: &str,
|
|
filter: Option<Backend>,
|
|
) -> PackageInfoReport {
|
|
let lanes = selected_lanes(filter);
|
|
// The filter is applied before any backend is queried, so a
|
|
// source-constrained request never probes (or reports) another lane.
|
|
let installed = chooser::installed_copies_filtered(exec, root, package, filter);
|
|
let installed_portage = if filter.map(is_portage).unwrap_or(true) {
|
|
portage::installed_packages(root)
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
let mut matches: Vec<PackageInfoMatch> = Vec::new();
|
|
let mut notes: Vec<String> = Vec::new();
|
|
|
|
let push_unique = |matches: &mut Vec<PackageInfoMatch>, candidate: PackageInfoMatch| {
|
|
let already = matches
|
|
.iter()
|
|
.any(|m| m.source == candidate.source && m.id == candidate.id);
|
|
if !already {
|
|
matches.push(candidate);
|
|
}
|
|
};
|
|
|
|
for lane in lanes {
|
|
let resolution = chooser::resolve_for_backend(exec, root, lane, package);
|
|
for candidate in resolution.candidates.iter().filter(|c| c.available) {
|
|
let installed_copy = installed.copies.iter().find(|copy| {
|
|
copy.id == candidate.id || (is_portage(lane) && copy.backend == "portage-source")
|
|
});
|
|
// A bare-name Portage candidate (`firefox`) is the same installed
|
|
// package as its VDB atom (`www-client/firefox`); use the atom so
|
|
// the report never splits one package into two rows.
|
|
let record = if is_portage(lane) {
|
|
installed_portage.iter().find(|p| {
|
|
p.atom == candidate.id
|
|
|| installed_copy.map(|c| c.id == p.atom).unwrap_or(false)
|
|
})
|
|
} else {
|
|
None
|
|
};
|
|
let effective_id = record
|
|
.map(|r| r.atom.clone())
|
|
.unwrap_or_else(|| candidate.id.clone());
|
|
let mut info = PackageInfoMatch {
|
|
source: lane,
|
|
id: effective_id,
|
|
version: candidate
|
|
.version
|
|
.clone()
|
|
.or_else(|| record.map(|r| r.version.clone()))
|
|
.or_else(|| installed_copy.map(|c| c.version.clone())),
|
|
description: candidate.notes.first().cloned(),
|
|
installed: installed_copy.is_some() || record.is_some(),
|
|
dependencies: Vec::new(),
|
|
reverse_dependencies: Vec::new(),
|
|
size: size_label(candidate),
|
|
provenance: candidate.provenance.clone(),
|
|
permissions: candidate.permissions.clone(),
|
|
notes: candidate.notes.clone(),
|
|
};
|
|
if let Some(record) = record {
|
|
if let Some(repo) = &record.repository {
|
|
info.provenance = repo.clone();
|
|
info.notes.push(format!("repository: {repo}"));
|
|
}
|
|
if let Some(size) = record.size_bytes {
|
|
info.size = Some(format!("{size} bytes installed"));
|
|
}
|
|
if let Some(rdepend) = &record.rdepend {
|
|
info.dependencies = split_depend(rdepend);
|
|
}
|
|
}
|
|
if is_portage(lane) && info.dependencies.is_empty() {
|
|
let detail = chooser::candidate_detail(exec, root, lane, &candidate.id);
|
|
info.dependencies = detail.dependencies;
|
|
}
|
|
push_unique(&mut matches, info);
|
|
}
|
|
if resolution.is_ambiguous() {
|
|
notes.push(format!(
|
|
"{}: multiple candidates matched ({})",
|
|
lane.as_str(),
|
|
resolution.ambiguous.join(", ")
|
|
));
|
|
}
|
|
}
|
|
|
|
// Installed copies with no corresponding available candidate (for example
|
|
// an installed Flatpak whose remote search is unavailable) are still real.
|
|
for copy in &installed.copies {
|
|
let source = parse_installed_backend(©.backend);
|
|
let Some(source) = source else {
|
|
continue;
|
|
};
|
|
if filter.map(|f| !lane_matches(source, f)).unwrap_or(false) {
|
|
continue;
|
|
}
|
|
let record = if is_portage(source) {
|
|
installed_portage.iter().find(|p| p.atom == copy.id)
|
|
} else {
|
|
None
|
|
};
|
|
let mut info = PackageInfoMatch {
|
|
source,
|
|
id: copy.id.clone(),
|
|
version: Some(copy.version.clone()),
|
|
description: None,
|
|
installed: true,
|
|
dependencies: record
|
|
.and_then(|r| r.rdepend.as_ref())
|
|
.map(|d| split_depend(d))
|
|
.unwrap_or_default(),
|
|
reverse_dependencies: Vec::new(),
|
|
size: record
|
|
.and_then(|r| r.size_bytes)
|
|
.map(|s| format!("{s} bytes installed")),
|
|
provenance: record
|
|
.and_then(|r| r.repository.clone())
|
|
.unwrap_or_else(|| "installed".into()),
|
|
permissions: Vec::new(),
|
|
notes: Vec::new(),
|
|
};
|
|
if let Some(repo) = record.and_then(|r| r.repository.clone()) {
|
|
info.notes.push(format!("repository: {repo}"));
|
|
}
|
|
push_unique(&mut matches, info);
|
|
}
|
|
|
|
if matches.len() > 1 {
|
|
notes.push("ambiguous: choose a source or use a full backend id".into());
|
|
}
|
|
|
|
PackageInfoReport {
|
|
query: package.to_string(),
|
|
ambiguous: matches.len() > 1,
|
|
matches,
|
|
notes,
|
|
}
|
|
}
|
|
|
|
fn parse_installed_backend(name: &str) -> Option<Backend> {
|
|
match name {
|
|
"portage-source" | "portage" => Some(Backend::PortageSource),
|
|
"flatpak" => Some(Backend::Flatpak),
|
|
"snap" => Some(Backend::Snap),
|
|
"appimage" => Some(Backend::AppImage),
|
|
"nix" => Some(Backend::Nix),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
fn size_label(candidate: &phoebus_core::selection::Candidate) -> Option<String> {
|
|
let mut parts = Vec::new();
|
|
if let Some(download) = candidate.download_mib.filter(|v| *v > 0) {
|
|
parts.push(format!("{download} MiB download"));
|
|
}
|
|
if let Some(install) = candidate.install_mib.filter(|v| *v > 0) {
|
|
parts.push(format!("{install} MiB installed"));
|
|
}
|
|
if parts.is_empty() {
|
|
None
|
|
} else {
|
|
Some(parts.join(", "))
|
|
}
|
|
}
|
|
|
|
/// Split a Portage dependency string into bounded, non-empty atoms.
|
|
fn split_depend(value: &str) -> Vec<String> {
|
|
value
|
|
.split_whitespace()
|
|
.map(|s| s.trim_start_matches(['!', '?']).trim())
|
|
.filter(|s| s.contains('/') && !s.starts_with('|'))
|
|
.map(|s| s.to_string())
|
|
.take(64)
|
|
.collect()
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Maintenance
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Which installed package owns a file path (Portage).
|
|
pub fn package_owners(exec: &Exec, root: &Path, path: &str) -> Result<String> {
|
|
// Prefer `qfile` (portage-utils), then `equery belongs` (gentoolkit). The
|
|
// tool must be present in the intended target, not merely on the host.
|
|
if crate::exec::tool_in_target(root, "qfile") {
|
|
let out = capture(exec, root, "qfile", &[path])?;
|
|
if !out.success {
|
|
return Ok(format!("no installed package owns {path}"));
|
|
}
|
|
return Ok(format!(
|
|
"packages owning {path}:\n{}",
|
|
out.stdout_lossy().trim()
|
|
));
|
|
}
|
|
if crate::exec::tool_in_target(root, "equery") {
|
|
let out = capture(exec, root, "equery", &["belongs", path])?;
|
|
if !out.success {
|
|
return Ok(format!("no installed package owns {path}"));
|
|
}
|
|
return Ok(format!(
|
|
"packages owning {path}:\n{}",
|
|
out.stdout_lossy().trim()
|
|
));
|
|
}
|
|
Err(ExecError::operation(
|
|
"sys",
|
|
"package owners",
|
|
"no file-ownership tool is available; install app-portage/portage-utils (qfile) or \
|
|
app-portage/gentoolkit (equery)",
|
|
))
|
|
}
|
|
|
|
/// The declared dependency fields of an installed VDB record.
|
|
fn declared_dependencies(record: &portage::InstalledPortage) -> Vec<portage::DeclaredDependency> {
|
|
let mut deps = Vec::new();
|
|
for text in [&record.depend, &record.rdepend, &record.bdepend]
|
|
.into_iter()
|
|
.flatten()
|
|
{
|
|
deps.extend(portage::parse_dependencies(text));
|
|
}
|
|
deps
|
|
}
|
|
|
|
/// Whether `atom` (a `category/name`) matches a requested package (bare name,
|
|
/// full atom, or atom-with-version).
|
|
fn atom_matches(atom: &str, package: &str) -> bool {
|
|
let wanted = portage::normalize_dep_atom(package);
|
|
atom == wanted
|
|
|| wanted.ends_with(&format!("/{package}"))
|
|
|| atom.ends_with(&format!("/{package}"))
|
|
}
|
|
|
|
/// Forward or reverse dependencies of an installed Portage package.
|
|
///
|
|
/// Dependencies come from the authoritative installed metadata (the VDB
|
|
/// `DEPEND`/`RDEPEND`/`BDEPEND` fields), so already-satisfied dependencies are
|
|
/// included and conditional/versioned dependencies keep their context. An
|
|
/// install plan from `emerge --pretend` is deliberately not used: it describes
|
|
/// pending merges, not the full dependency graph. For a package that is not
|
|
/// installed, `qdepends` (portage-utils) is used when present, else the query is
|
|
/// refused with a clear limitation rather than guessing.
|
|
pub fn package_dependencies(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
package: &str,
|
|
reverse: bool,
|
|
) -> Result<String> {
|
|
let installed = portage::installed_packages(root);
|
|
let installed_atoms: std::collections::BTreeSet<String> =
|
|
installed.iter().map(|p| p.atom.clone()).collect();
|
|
let self_atom = installed
|
|
.iter()
|
|
.find(|p| atom_matches(&p.atom, package))
|
|
.map(|p| p.atom.clone());
|
|
|
|
let mut lines: Vec<String> = Vec::new();
|
|
if reverse {
|
|
for record in &installed {
|
|
// A genuine self-reference is a package declaring a dependency on
|
|
// *itself*; it is not a reverse dependency of any other package.
|
|
// The dependent is `record`, so compare `record.atom` with the
|
|
// queried package — not the dependency target, which is exactly
|
|
// what must match.
|
|
if self_atom.as_deref() == Some(record.atom.as_str()) {
|
|
continue;
|
|
}
|
|
for dep in declared_dependencies(record) {
|
|
if atom_matches(&dep.atom, package) {
|
|
let conditional = dep
|
|
.condition
|
|
.as_deref()
|
|
.map(|c| format!("[{c}] "))
|
|
.unwrap_or_default();
|
|
lines.push(format!("{conditional}{} (from {})", dep.raw, record.atom));
|
|
}
|
|
}
|
|
}
|
|
lines.sort();
|
|
lines.dedup();
|
|
if lines.is_empty() {
|
|
return Ok(format!(
|
|
"no installed package depends on {package}\n (installed metadata is authoritative \
|
|
for reverse dependencies; nothing references it)"
|
|
));
|
|
}
|
|
return Ok(format!(
|
|
"installed packages depending on {package}:\n {}",
|
|
lines.join("\n ")
|
|
));
|
|
}
|
|
|
|
// Forward: prefer the installed metadata; fall back to the tree via
|
|
// qdepends only when the package is not installed.
|
|
let deps: Vec<(String, Option<String>, bool)> =
|
|
if let Some(record) = installed.iter().find(|p| atom_matches(&p.atom, package)) {
|
|
declared_dependencies(record)
|
|
.into_iter()
|
|
.filter(|d| Some(&d.atom) != self_atom.as_ref())
|
|
.map(|d| (d.raw, d.condition, d.negative))
|
|
.collect()
|
|
} else if crate::exec::tool_in_target(root, "qdepends") {
|
|
let out = capture(exec, root, "qdepends", &["-Q", "--", package])?;
|
|
if !out.success {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"dependencies",
|
|
format!(
|
|
"qdepends could not resolve dependencies for {package}: {}",
|
|
stderr_tail(&out.stderr_lossy())
|
|
),
|
|
));
|
|
}
|
|
portage::parse_dependencies(&out.stdout_lossy())
|
|
.into_iter()
|
|
.map(|d| (d.raw, d.condition, d.negative))
|
|
.collect()
|
|
} else {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"dependencies",
|
|
format!(
|
|
"{package} is not installed, and no dependency inspection tool is available; \
|
|
install app-portage/portage-utils (qdepends) or inspect it through Portage"
|
|
),
|
|
));
|
|
};
|
|
|
|
if deps.is_empty() {
|
|
return Ok(format!(
|
|
"{package} has no declared dependencies in the installed metadata"
|
|
));
|
|
}
|
|
let mut rendered = Vec::new();
|
|
for (raw, condition, negative) in deps {
|
|
let atom = portage::normalize_dep_atom(&raw);
|
|
let satisfied = if negative {
|
|
"blocker"
|
|
} else if installed_atoms.contains(&atom) {
|
|
"installed"
|
|
} else {
|
|
"not installed"
|
|
};
|
|
let conditional = condition
|
|
.as_deref()
|
|
.map(|c| format!("[{c}] "))
|
|
.unwrap_or_default();
|
|
rendered.push(format!("{conditional}{raw} ({satisfied})"));
|
|
}
|
|
rendered.sort();
|
|
rendered.dedup();
|
|
Ok(format!(
|
|
"{package} declares {} dependency token(s):\n {}",
|
|
rendered.len(),
|
|
rendered.join("\n ")
|
|
))
|
|
}
|
|
|
|
/// Remove packages no longer required by any installed package (Portage).
|
|
///
|
|
/// The selection is previewed with `emerge --pretend --depclean` first; if
|
|
/// nothing is selected nothing is removed. User data is not touched.
|
|
pub fn depclean(exec: &Exec, root: &Path) -> Result<String> {
|
|
let pretend = capture(
|
|
exec,
|
|
root,
|
|
"emerge",
|
|
&["--pretend", "--depclean", "--color=n"],
|
|
)?;
|
|
if !pretend.success {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"depclean",
|
|
format!(
|
|
"emerge --pretend --depclean failed: {}",
|
|
stderr_tail(&pretend.stderr_lossy())
|
|
),
|
|
));
|
|
}
|
|
let preview = pretend.stdout_lossy();
|
|
let selected_nothing = preview.lines().any(|line| {
|
|
let text = line.trim().trim_start_matches(['>', ' ']).trim();
|
|
text.starts_with("0 packages selected")
|
|
});
|
|
if preview.contains("No packages selected") || selected_nothing {
|
|
return Ok(format!(
|
|
"nothing to remove; no unused packages were selected\npreview:\n{}",
|
|
preview.trim()
|
|
));
|
|
}
|
|
// `--ask=n` keeps the destructive run non-interactive: the human
|
|
// confirmation happened in the approval layer, and Portage must not try to
|
|
// read a second answer from the (already consumed) stdin.
|
|
run_mut(
|
|
exec,
|
|
root,
|
|
"emerge",
|
|
&["--depclean", "--ask=n", "--color=n"],
|
|
)?;
|
|
Ok(format!(
|
|
"removed unused packages; configuration files and user data were preserved\n\
|
|
reviewed selection (emerge --pretend --depclean):\n{}",
|
|
preview.trim()
|
|
))
|
|
}
|
|
|
|
/// The destructive arguments for a named Portage cleaner, if it is present.
|
|
fn destructive_args_for(root: &Path, cleaner: &str) -> Option<Vec<String>> {
|
|
crate::actions::portage_cleaners(root)
|
|
.into_iter()
|
|
.find(|(name, _, _)| *name == cleaner)
|
|
.map(|(_, _, destructive)| destructive)
|
|
}
|
|
|
|
/// Remove unreferenced package caches / store paths. Never removes an
|
|
/// installed package.
|
|
///
|
|
/// When a reviewed plan is supplied, **only the cleaners and item sets that
|
|
/// were reviewed are executed**; the destructive invocation uses the same
|
|
/// selection policy as the preview. Every cleaner's outcome is preserved: a
|
|
/// cleaner that ran and failed makes the whole action fail (with partial
|
|
/// successes still reported), and a missing cleaner is distinguished from one
|
|
/// that ran and found nothing. A cleanup that could not run at all is a
|
|
/// failure, not a silent success.
|
|
pub fn clean_cache(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
filter: Option<Backend>,
|
|
approved: Option<&phoebus_core::effects::ResolvedPlan>,
|
|
) -> Result<String> {
|
|
let lanes: Vec<Backend> = match filter {
|
|
Some(backend) => vec![backend],
|
|
None => vec![Backend::PortageSource, Backend::Nix],
|
|
};
|
|
let mut report = String::new();
|
|
report.push_str("cache cleanup (installed packages are never removed)\n");
|
|
let mut completed = 0usize;
|
|
let mut failed = false;
|
|
|
|
for lane in lanes {
|
|
if lane.is_portage() {
|
|
// The reviewed cleaner set, when present, is the only thing that may
|
|
// run. Otherwise fall back to every cleaner present in the target.
|
|
let reviewed: Vec<String> = match approved {
|
|
Some(plan) => plan
|
|
.cleanup
|
|
.iter()
|
|
.filter(|c| !c.unavailable)
|
|
.map(|c| c.cleaner.clone())
|
|
.collect(),
|
|
None => crate::actions::portage_cleaners(root)
|
|
.into_iter()
|
|
.map(|(name, _, _)| name.to_string())
|
|
.collect(),
|
|
};
|
|
let mut attempted = false;
|
|
for cleaner in reviewed {
|
|
let Some(args) = destructive_args_for(root, &cleaner) else {
|
|
failed = true;
|
|
report.push_str(&format!(
|
|
" [portage] {cleaner} failed: the reviewed cleaner is no longer \
|
|
available in the target\n"
|
|
));
|
|
continue;
|
|
};
|
|
attempted = true;
|
|
let refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
|
|
match run_mut(exec, root, &cleaner, &refs) {
|
|
Ok(()) => {
|
|
completed += 1;
|
|
report.push_str(&format!(" [portage] {cleaner} completed\n"));
|
|
}
|
|
Err(e) => {
|
|
failed = true;
|
|
report.push_str(&format!(" [portage] {cleaner} failed: {e}\n"));
|
|
}
|
|
}
|
|
}
|
|
if !attempted {
|
|
report.push_str(
|
|
" [portage] unavailable: no cleaner in the target (install \
|
|
app-portage/gentoolkit for eclean-dist / eclean-pkg)\n",
|
|
);
|
|
}
|
|
} else if lane == Backend::Nix {
|
|
let reviewed = match approved {
|
|
Some(plan) => plan
|
|
.cleanup
|
|
.iter()
|
|
.find(|c| c.cleaner == "nix-store --gc")
|
|
.map(|c| !c.unavailable),
|
|
None => Some(nix::installed(root)),
|
|
};
|
|
match reviewed {
|
|
None => report.push_str(" [nix] not executed: not part of the reviewed plan\n"),
|
|
Some(false) => {
|
|
report.push_str(" [nix] unavailable: Nix is not installed\n");
|
|
}
|
|
Some(true) => match nix::gc(exec, root) {
|
|
Ok(()) => {
|
|
completed += 1;
|
|
report.push_str(
|
|
" [nix] unreferenced store paths collected (nix-store --gc)\n",
|
|
);
|
|
}
|
|
Err(e) => {
|
|
failed = true;
|
|
report.push_str(&format!(" [nix] cleanup failed: {e}\n"));
|
|
}
|
|
},
|
|
}
|
|
} else {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"clean cache",
|
|
format!(
|
|
"the {} lane has no package cache to clean; refusing",
|
|
lane.as_str()
|
|
),
|
|
));
|
|
}
|
|
}
|
|
|
|
if failed {
|
|
return Err(ExecError::operation("sys", "clean cache", report));
|
|
}
|
|
if completed == 0 {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"clean cache",
|
|
format!("{report}no cleaner ran"),
|
|
));
|
|
}
|
|
Ok(report)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Updates
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// Check for updates across the supported lanes, optionally restricted to one.
|
|
pub fn check_updates(exec: &Exec, root: &Path, filter: Option<Backend>) -> UnifiedUpdateOverview {
|
|
// The filter is applied before any backend is queried, not merely to the
|
|
// displayed results.
|
|
crate::update::check_unified_updates_filtered(exec, root, filter)
|
|
}
|
|
|
|
/// Whether the reviewed plan recorded that a source could not be resolved.
|
|
fn source_resolution_failed(approved: &ResolvedPlan, tag: &str) -> bool {
|
|
approved
|
|
.consequences
|
|
.iter()
|
|
.any(|c| c.starts_with(&format!("[{tag}] updates could not be resolved")))
|
|
}
|
|
|
|
/// Revalidate an approved Flatpak transaction immediately before mutation.
|
|
/// The freshly resolved set must equal the approved set exactly (identity,
|
|
/// immutable commit, scope): a changed commit with an unchanged human version,
|
|
/// a newly appeared update, or a vanished one all fail.
|
|
fn revalidate_flatpak(exec: &Exec, root: &Path, approved: &[UpdateTransaction]) -> Result<()> {
|
|
let current = crate::actions::flatpak_update_transactions(exec, root)?;
|
|
if current != approved {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"apply flatpak updates",
|
|
format!(
|
|
"the reviewed Flatpak transaction changed before execution ({} reviewed, {} now); \
|
|
not applying it",
|
|
approved.len(),
|
|
current.len()
|
|
),
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Revalidate an approved Snap transaction immediately before mutation.
|
|
fn revalidate_snap(exec: &Exec, root: &Path, approved: &[UpdateTransaction]) -> Result<()> {
|
|
let current = crate::actions::snap_update_transactions(exec, root)?;
|
|
if current != approved {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"apply snap updates",
|
|
format!(
|
|
"the reviewed Snap transaction changed before execution ({} reviewed, {} now); \
|
|
not applying it",
|
|
approved.len(),
|
|
current.len()
|
|
),
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Installed Flatpak commits keyed by ref.
|
|
/// The installed active commit for every ref, from
|
|
/// `flatpak list --system --columns=ref,active`. `list` reports the bare ref
|
|
/// (without the `app/`/`runtime/` type prefix) and an abbreviated commit.
|
|
fn installed_flatpak_active(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
) -> Result<std::collections::BTreeMap<String, String>> {
|
|
let out = capture(
|
|
exec,
|
|
root,
|
|
"flatpak",
|
|
&["list", "--system", "--columns=ref,active"],
|
|
)?;
|
|
if !out.success {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"verify flatpak updates",
|
|
format!("flatpak list failed: {}", stderr_tail(&out.stderr_lossy())),
|
|
));
|
|
}
|
|
let mut map = std::collections::BTreeMap::new();
|
|
for line in out.stdout_lossy().lines() {
|
|
let mut cols = line.splitn(2, '\t');
|
|
let (Some(reference), Some(active)) = (cols.next(), cols.next()) else {
|
|
continue;
|
|
};
|
|
if !reference.trim().is_empty() && !active.trim().is_empty() {
|
|
map.insert(reference.trim().to_string(), active.trim().to_string());
|
|
}
|
|
}
|
|
Ok(map)
|
|
}
|
|
|
|
/// Strip the `app/`/`runtime/` type prefix so a remote ref can be matched
|
|
/// against a `flatpak list` bare ref.
|
|
fn strip_flatpak_type(reference: &str) -> &str {
|
|
reference
|
|
.strip_prefix("app/")
|
|
.or_else(|| reference.strip_prefix("runtime/"))
|
|
.unwrap_or(reference)
|
|
}
|
|
|
|
fn execute_flatpak(exec: &Exec, root: &Path, txns: &[UpdateTransaction]) -> Result<String> {
|
|
for txn in txns {
|
|
// Scope to the system installation and pin the exact reviewed commit so
|
|
// a remote that republishes in the window cannot deploy unreviewed.
|
|
let mut args = vec![
|
|
"update".to_string(),
|
|
"--system".to_string(),
|
|
"--noninteractive".to_string(),
|
|
"-y".to_string(),
|
|
];
|
|
if let Some(commit) = &txn.next_revision {
|
|
args.push(format!("--commit={commit}"));
|
|
}
|
|
args.push(txn.identity.clone());
|
|
let refs: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
|
|
run_mut(exec, root, "flatpak", &refs)?;
|
|
}
|
|
// Post-execution verification: every reviewed ref must now be at the
|
|
// reviewed commit (compared by prefix, since `flatpak list` abbreviates).
|
|
let active = installed_flatpak_active(exec, root)?;
|
|
for txn in txns {
|
|
let key = strip_flatpak_type(&txn.identity);
|
|
let actual = active.get(key);
|
|
let reached = match (txn.next_revision.as_deref(), actual) {
|
|
(Some(next), Some(a)) => next.starts_with(a.as_str()),
|
|
_ => false,
|
|
};
|
|
if !reached {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"verify flatpak updates",
|
|
format!(
|
|
"{} did not reach the reviewed commit {} (now {:?})",
|
|
txn.identity,
|
|
txn.next_revision.as_deref().unwrap_or("?"),
|
|
actual
|
|
),
|
|
));
|
|
}
|
|
}
|
|
Ok(format!(
|
|
" [flatpak] updated {} reviewed ref(s) to their reviewed commits\n",
|
|
txns.len()
|
|
))
|
|
}
|
|
|
|
/// Installed Snap revisions keyed by name.
|
|
fn installed_snap_revisions(exec: &Exec) -> Result<std::collections::BTreeMap<String, String>> {
|
|
let entries = snap::list(exec)?;
|
|
Ok(entries.into_iter().map(|e| (e.name, e.revision)).collect())
|
|
}
|
|
|
|
fn execute_snap(exec: &Exec, txns: &[UpdateTransaction]) -> Result<String> {
|
|
for txn in txns {
|
|
snap::refresh(exec, Some(txn.identity.as_str()))?;
|
|
}
|
|
let revisions = installed_snap_revisions(exec)?;
|
|
for txn in txns {
|
|
let actual = revisions.get(&txn.identity);
|
|
if txn.next_revision.is_some() && actual != txn.next_revision.as_ref() {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"verify snap updates",
|
|
format!(
|
|
"{} did not reach the reviewed revision {} (now {:?})",
|
|
txn.identity,
|
|
txn.next_revision.as_deref().unwrap_or("?"),
|
|
actual
|
|
),
|
|
));
|
|
}
|
|
}
|
|
Ok(format!(
|
|
" [snap] refreshed {} reviewed snap(s) to their reviewed revisions\n",
|
|
txns.len()
|
|
))
|
|
}
|
|
|
|
/// Apply updates across the supported lanes and report a truthful per-source
|
|
/// outcome. Flatpak and Snap execute **exactly the reviewed transactions**
|
|
/// carried in `approved` (after revalidating each source immediately before its
|
|
/// mutation); freshly discovered updates are never used as replacement
|
|
/// approval. A source that ran and failed is reported as failed with partial
|
|
/// successes preserved, and the whole action then fails.
|
|
pub fn apply_updates(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
filter: Option<Backend>,
|
|
approved: Option<&ResolvedPlan>,
|
|
) -> Result<String> {
|
|
let lanes: Vec<Backend> = match filter {
|
|
Some(backend) => vec![backend],
|
|
None => vec![Backend::PortageSource, Backend::Flatpak, Backend::Snap],
|
|
};
|
|
let mut report = String::new();
|
|
report.push_str("update application (per-source outcome)\n");
|
|
let mut failed = false;
|
|
let mut completed = 0usize;
|
|
|
|
for lane in lanes {
|
|
if lane.is_portage() {
|
|
match crate::actions::portage_world_update(exec, root) {
|
|
Ok(()) => {
|
|
completed += 1;
|
|
report.push_str(" [portage] updated (emerge -uDN @world)\n");
|
|
}
|
|
Err(e) => {
|
|
failed = true;
|
|
report.push_str(&format!(" [portage] failed: {e}\n"));
|
|
}
|
|
}
|
|
} else if lane == Backend::Flatpak {
|
|
let Some(plan) = approved else {
|
|
failed = true;
|
|
report.push_str(
|
|
" [flatpak] refused: no reviewed transaction; refusing to discover unreviewed\n",
|
|
);
|
|
continue;
|
|
};
|
|
let txns: Vec<UpdateTransaction> = plan
|
|
.updates
|
|
.iter()
|
|
.filter(|t| t.source == UpdateSource::Flatpak)
|
|
.cloned()
|
|
.collect();
|
|
if txns.is_empty() {
|
|
if source_resolution_failed(plan, "flatpak") {
|
|
report.push_str(
|
|
" [flatpak] not executed: resolution failed at review; not re-discovered\n",
|
|
);
|
|
} else {
|
|
completed += 1;
|
|
report.push_str(" [flatpak] no updates were reviewed\n");
|
|
}
|
|
continue;
|
|
}
|
|
if !flatpak_available(exec, root) {
|
|
failed = true;
|
|
report.push_str(
|
|
" [flatpak] failed: the reviewed transaction cannot run; the client is not \
|
|
available in the target\n",
|
|
);
|
|
continue;
|
|
}
|
|
if !txns.iter().all(|t| t.exact) {
|
|
failed = true;
|
|
report.push_str(
|
|
" [flatpak] refused: the reviewed transaction is not exactly bindable \
|
|
(a ref or commit is missing)\n",
|
|
);
|
|
continue;
|
|
}
|
|
if let Err(e) = revalidate_flatpak(exec, root, &txns) {
|
|
failed = true;
|
|
report.push_str(&format!(" [flatpak] failed: {e}\n"));
|
|
continue;
|
|
}
|
|
match execute_flatpak(exec, root, &txns) {
|
|
Ok(note) => {
|
|
completed += 1;
|
|
report.push_str(¬e);
|
|
}
|
|
Err(e) => {
|
|
failed = true;
|
|
report.push_str(&format!(" [flatpak] failed: {e}\n"));
|
|
}
|
|
}
|
|
} else if lane == Backend::Snap {
|
|
if !crate::actions::is_host_root(root) {
|
|
failed = true;
|
|
report
|
|
.push_str(" [snap] refused: Snap cannot be redirected to an alternate root\n");
|
|
continue;
|
|
}
|
|
let Some(plan) = approved else {
|
|
failed = true;
|
|
report.push_str(
|
|
" [snap] refused: no reviewed transaction; refusing to discover unreviewed\n",
|
|
);
|
|
continue;
|
|
};
|
|
let txns: Vec<UpdateTransaction> = plan
|
|
.updates
|
|
.iter()
|
|
.filter(|t| t.source == UpdateSource::Snap)
|
|
.cloned()
|
|
.collect();
|
|
if txns.is_empty() {
|
|
if source_resolution_failed(plan, "snap") {
|
|
report.push_str(
|
|
" [snap] not executed: resolution failed at review; not re-discovered\n",
|
|
);
|
|
} else {
|
|
completed += 1;
|
|
report.push_str(" [snap] no updates were reviewed\n");
|
|
}
|
|
continue;
|
|
}
|
|
let usable = matches!(snap::status(exec), Ok(s) if s.usable());
|
|
if !usable {
|
|
failed = true;
|
|
report.push_str(
|
|
" [snap] failed: the reviewed transaction cannot run; snapd is not installed \
|
|
or active\n",
|
|
);
|
|
continue;
|
|
}
|
|
if !txns.iter().all(|t| t.exact) {
|
|
failed = true;
|
|
report.push_str(
|
|
" [snap] refused: the reviewed transaction is not exactly bindable \
|
|
(a revision is missing)\n",
|
|
);
|
|
continue;
|
|
}
|
|
if let Err(e) = revalidate_snap(exec, root, &txns) {
|
|
failed = true;
|
|
report.push_str(&format!(" [snap] failed: {e}\n"));
|
|
continue;
|
|
}
|
|
match execute_snap(exec, &txns) {
|
|
Ok(note) => {
|
|
completed += 1;
|
|
report.push_str(¬e);
|
|
}
|
|
Err(e) => {
|
|
failed = true;
|
|
report.push_str(&format!(" [snap] failed: {e}\n"));
|
|
}
|
|
}
|
|
} else if filter.is_some() {
|
|
// An explicit request for a lane with no safe system-level update
|
|
// transaction is refused, not silently reported as "updated".
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"apply updates",
|
|
format!(
|
|
"{} updates cannot be applied safely by `sys` from the system; {}",
|
|
lane.as_str(),
|
|
crate::actions::update_limitation(lane)
|
|
),
|
|
));
|
|
} else {
|
|
report.push_str(&format!(
|
|
" [{}] not applied: {}\n",
|
|
lane.as_str(),
|
|
crate::actions::update_limitation(lane)
|
|
));
|
|
}
|
|
}
|
|
|
|
if failed {
|
|
return Err(ExecError::operation("sys", "apply updates", report));
|
|
}
|
|
if completed == 0 {
|
|
return Err(ExecError::operation(
|
|
"sys",
|
|
"apply updates",
|
|
format!("{report}no source was updated"),
|
|
));
|
|
}
|
|
Ok(report)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::command::{CommandOutput, RecordingRunner};
|
|
use crate::log::Logger;
|
|
use phoebus_core::Redactor;
|
|
use std::time::Duration;
|
|
|
|
fn make_exec(runner: RecordingRunner) -> Exec {
|
|
let path = tempfile::tempdir().unwrap().keep();
|
|
let logger = Logger::open(path.join("l.log"), Redactor::new(), false);
|
|
Exec::new(Box::new(runner), logger, false, path.join("root"), path)
|
|
}
|
|
|
|
fn ok(stdout: &str) -> CommandOutput {
|
|
CommandOutput {
|
|
status: 0,
|
|
success: true,
|
|
stdout: stdout.as_bytes().to_vec(),
|
|
stderr: Vec::new(),
|
|
duration: Duration::from_millis(1),
|
|
}
|
|
}
|
|
|
|
fn fail(stderr: &str) -> CommandOutput {
|
|
CommandOutput {
|
|
status: 1,
|
|
success: false,
|
|
stdout: Vec::new(),
|
|
stderr: stderr.as_bytes().to_vec(),
|
|
duration: Duration::from_millis(1),
|
|
}
|
|
}
|
|
|
|
const EMERGE_SEARCH: &str = "\
|
|
* www-client/firefox
|
|
Latest version available: 128.0
|
|
Latest version installed: 127.0
|
|
Description: Firefox Web Browser
|
|
";
|
|
|
|
#[test]
|
|
fn search_reports_portage_results_and_distinguishes_empty() {
|
|
let runner = RecordingRunner::new();
|
|
runner.set_response("emerge", ok(EMERGE_SEARCH));
|
|
let exec = make_exec(runner);
|
|
// Host root so the recording runner sees `emerge` directly, exactly as
|
|
// it does for a normal `sys` invocation.
|
|
let report = search(
|
|
&exec,
|
|
Path::new("/"),
|
|
"firefox",
|
|
Some(Backend::PortageSource),
|
|
);
|
|
assert_eq!(report.sources.len(), 1);
|
|
let source = &report.sources[0];
|
|
assert!(source.supported && source.ok);
|
|
assert_eq!(source.entries.len(), 1);
|
|
assert_eq!(source.entries[0].id, "www-client/firefox");
|
|
assert!(source.entries[0].installed);
|
|
assert_eq!(source.entries[0].version.as_deref(), Some("128.0"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_failed_search_is_not_reported_as_empty() {
|
|
let runner = RecordingRunner::new();
|
|
runner.set_response("emerge", fail("!!! offline"));
|
|
let exec = make_exec(runner);
|
|
let report = search(
|
|
&exec,
|
|
Path::new("/"),
|
|
"firefox",
|
|
Some(Backend::PortageSource),
|
|
);
|
|
let source = &report.sources[0];
|
|
assert!(source.supported);
|
|
assert!(!source.ok, "a failed search must not look ok");
|
|
assert!(source.note.contains("failed"));
|
|
}
|
|
|
|
#[test]
|
|
fn unavailable_nix_is_explicit_in_search_and_inventory() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let report = search(&exec, Path::new(&exec.root), "nodejs", Some(Backend::Nix));
|
|
assert!(!report.sources[0].supported);
|
|
assert!(report.sources[0].note.contains("not installed"));
|
|
|
|
let inv = inventory(&exec, Path::new(&exec.root), Some(Backend::Nix), None);
|
|
assert!(!inv.sources[0].supported);
|
|
}
|
|
|
|
#[test]
|
|
fn inventory_reads_the_portage_vdb_and_never_counts_custom_ebuilds() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
let dir = root.join("var/db/pkg/app-editors/vim-9.1.0");
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
std::fs::write(dir.join("SIZE"), "4096").unwrap();
|
|
|
|
let report = inventory(&exec, root, None, None);
|
|
let portage = report
|
|
.sources
|
|
.iter()
|
|
.find(|s| s.backend == Backend::PortageSource)
|
|
.unwrap();
|
|
assert_eq!(portage.entries.len(), 1);
|
|
assert_eq!(portage.entries[0].id, "app-editors/vim");
|
|
let custom = report
|
|
.sources
|
|
.iter()
|
|
.find(|s| s.backend == Backend::CustomEbuild)
|
|
.unwrap();
|
|
assert!(
|
|
!custom.supported,
|
|
"custom ebuilds are not counted separately"
|
|
);
|
|
assert_eq!(report.total(), 1, "the installed package is counted once");
|
|
}
|
|
|
|
#[test]
|
|
fn inventory_text_filter_matches_id_or_description() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
let dir = root.join("var/db/pkg/app-editors/vim-9.1.0");
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
std::fs::create_dir_all(root.join("var/db/pkg/net-misc/curl-8.0")).unwrap();
|
|
|
|
let report = inventory(&exec, root, None, Some("vim"));
|
|
let portage = report
|
|
.sources
|
|
.iter()
|
|
.find(|s| s.backend == Backend::PortageSource)
|
|
.unwrap();
|
|
assert_eq!(portage.entries.len(), 1);
|
|
assert_eq!(portage.entries[0].id, "app-editors/vim");
|
|
}
|
|
|
|
#[test]
|
|
fn package_info_reports_installed_portage_metadata() {
|
|
let runner = RecordingRunner::new();
|
|
runner.set_response("emerge", ok(""));
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
let dir = root.join("var/db/pkg/www-client/firefox-128.0");
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
std::fs::write(dir.join("SIZE"), "1048576").unwrap();
|
|
std::fs::write(dir.join("repository"), "gentoo").unwrap();
|
|
std::fs::write(dir.join("RDEPEND"), ">=dev-libs/glib-2.0").unwrap();
|
|
|
|
let report = package_info(&exec, root, "firefox", Some(Backend::PortageSource));
|
|
let m = report
|
|
.matches
|
|
.iter()
|
|
.find(|m| m.id == "www-client/firefox")
|
|
.expect("the installed VDB atom is reported");
|
|
assert!(m.installed);
|
|
assert_eq!(m.version.as_deref(), Some("128.0"));
|
|
assert_eq!(m.provenance, "gentoo");
|
|
assert_eq!(m.size.as_deref(), Some("1048576 bytes installed"));
|
|
assert!(m.dependencies.iter().any(|d| d.contains("glib")));
|
|
assert!(!report.ambiguous(), "the bare name and atom must be merged");
|
|
}
|
|
|
|
#[test]
|
|
fn package_info_disambiguates_multiple_real_matches() {
|
|
let runner = RecordingRunner::new();
|
|
// Portage resolves one candidate; a managed AppImage supplies another.
|
|
// Both are real, so the report is ambiguous rather than guessing.
|
|
runner.set_response(
|
|
"emerge",
|
|
ok("These are the packages that would be merged:\n[ebuild N ] www-client/firefox-128.0\n"),
|
|
);
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
let dir = root.join("var/lib/phoebus/appimages");
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
std::fs::write(
|
|
dir.join("firefox.toml"),
|
|
"name = \"firefox\"\nversion = \"128.0\"\nsha256 = \"\"\nprovenance = \"local:/tmp/firefox.AppImage\"\narch = \"x86_64\"\nsize_bytes = 1\ninstalled_unix = 1\n",
|
|
)
|
|
.unwrap();
|
|
let report = package_info(&exec, root, "firefox", None);
|
|
assert!(
|
|
report.matches.len() >= 2,
|
|
"expected real matches, got {:?}",
|
|
report.matches
|
|
);
|
|
assert!(report.ambiguous());
|
|
assert!(report.render().contains("ambiguous"));
|
|
}
|
|
|
|
#[test]
|
|
fn depclean_previews_then_removes_and_skips_when_empty() {
|
|
let runner = RecordingRunner::new();
|
|
runner.set_response(
|
|
"emerge",
|
|
ok(">>> These are the packages that would be unmerged:\n\n\
|
|
>>> No packages selected\n\n0 packages selected\n"),
|
|
);
|
|
let exec = make_exec(runner.clone());
|
|
let out = depclean(&exec, Path::new("/")).unwrap();
|
|
assert!(out.contains("nothing to remove"));
|
|
// Only the preview ran.
|
|
assert_eq!(runner.recorded().len(), 1);
|
|
|
|
let runner2 = RecordingRunner::new();
|
|
runner2.set_response("emerge", ok("...\n>>> 3 packages selected\n"));
|
|
let exec2 = make_exec(runner2.clone());
|
|
let out2 = depclean(&exec2, Path::new("/")).unwrap();
|
|
assert!(out2.contains("removed unused packages"));
|
|
assert_eq!(runner2.recorded().len(), 2);
|
|
}
|
|
|
|
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 clean_cache_without_a_cleaner_is_a_failure_not_a_silent_success() {
|
|
// A temp dir with neither eclean-dist nor eclean-pkg in the target.
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let err = clean_cache(
|
|
&exec,
|
|
Path::new(&exec.root),
|
|
Some(Backend::PortageSource),
|
|
None,
|
|
)
|
|
.unwrap_err();
|
|
let text = err.to_string();
|
|
assert!(text.contains("no cleaner ran"), "{text}");
|
|
// The unavailable tool is named distinctly from a failed tool.
|
|
assert!(text.contains("unavailable"), "{text}");
|
|
}
|
|
|
|
#[test]
|
|
fn clean_cache_propagates_a_failed_cleaner_and_preserves_partial_success() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner.clone());
|
|
let root = Path::new(&exec.root);
|
|
install_fake_tool(root, "eclean-dist");
|
|
install_fake_tool(root, "eclean-pkg");
|
|
// The first cleaner runs; the second fails. Execution is rooted in the
|
|
// target (chroot), so both are the `chroot` wrapper; fail the second.
|
|
let chroot_calls = std::sync::Arc::new(std::sync::atomic::AtomicUsize::new(0));
|
|
struct FailSecond(std::sync::Arc<std::sync::atomic::AtomicUsize>);
|
|
impl crate::command::ProcessRunner for FailSecond {
|
|
fn execute(&self, _spec: &crate::command::CommandSpec) -> Result<CommandOutput> {
|
|
let n = self.0.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
|
if n == 0 {
|
|
Ok(CommandOutput {
|
|
status: 0,
|
|
success: true,
|
|
stdout: Vec::new(),
|
|
stderr: Vec::new(),
|
|
duration: std::time::Duration::from_millis(1),
|
|
})
|
|
} else {
|
|
Ok(CommandOutput {
|
|
status: 1,
|
|
success: false,
|
|
stdout: Vec::new(),
|
|
stderr: b"eclean boom".to_vec(),
|
|
duration: std::time::Duration::from_millis(1),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
let logger_path = tempfile::tempdir().unwrap().keep();
|
|
let logger = crate::log::Logger::open(
|
|
logger_path.join("l.log"),
|
|
phoebus_core::Redactor::new(),
|
|
false,
|
|
);
|
|
let exec2 = Exec::new(
|
|
Box::new(FailSecond(chroot_calls)),
|
|
logger,
|
|
false,
|
|
root,
|
|
logger_path,
|
|
);
|
|
let err = clean_cache(&exec2, root, Some(Backend::PortageSource), None).unwrap_err();
|
|
let text = err.to_string();
|
|
assert!(text.contains("eclean-dist completed"), "{text}");
|
|
assert!(text.contains("eclean-pkg failed"), "{text}");
|
|
// The runner was consulted, and no host command was issued.
|
|
assert!(runner.recorded().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn apply_updates_refuses_a_lane_without_a_safe_transaction() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let err =
|
|
apply_updates(&exec, Path::new(&exec.root), Some(Backend::AppImage), None).unwrap_err();
|
|
let text = err.to_string();
|
|
assert!(text.contains("cannot be applied safely"), "{text}");
|
|
assert!(text.contains("pinned URL"), "{text}");
|
|
}
|
|
|
|
#[test]
|
|
fn apply_updates_fails_the_action_when_an_attempted_source_fails() {
|
|
let runner = RecordingRunner::new();
|
|
runner.set_response("chroot", fail("!!! emerge failed"));
|
|
let exec = make_exec(runner);
|
|
let err = apply_updates(
|
|
&exec,
|
|
Path::new(&exec.root),
|
|
Some(Backend::PortageSource),
|
|
None,
|
|
)
|
|
.unwrap_err();
|
|
assert!(err.to_string().contains("[portage] failed"));
|
|
}
|
|
|
|
#[test]
|
|
fn flatpak_update_plan_is_refused_when_the_target_has_no_client() {
|
|
let runner = RecordingRunner::new();
|
|
runner.fail("chroot", "flatpak: not found");
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
let plan = crate::actions::resolve_effects(
|
|
&exec,
|
|
&phoebus_core::action::SystemAction::ApplyUpdates {
|
|
backend: Some(Backend::Flatpak),
|
|
},
|
|
root,
|
|
);
|
|
// No flatpak in the target, so the plan is refused rather than silently
|
|
// reporting success.
|
|
assert!(plan.is_err());
|
|
}
|
|
|
|
// --- Exact Flatpak/Snap update transactions ---------------------------
|
|
|
|
fn ok_out(s: &str) -> crate::command::CommandOutput {
|
|
crate::command::CommandOutput {
|
|
status: 0,
|
|
success: true,
|
|
stdout: s.as_bytes().to_vec(),
|
|
stderr: Vec::new(),
|
|
duration: std::time::Duration::from_millis(1),
|
|
}
|
|
}
|
|
|
|
/// One fake Flatpak ref update (normalized ref, latest version, target commit).
|
|
#[derive(Clone)]
|
|
struct FpUpdate {
|
|
reference: String,
|
|
version: String,
|
|
commit: String,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct FpState {
|
|
/// ref -> (version, commit)
|
|
installed: std::collections::BTreeMap<String, (String, String)>,
|
|
updates: Vec<FpUpdate>,
|
|
/// Apply the update to `installed` on `flatpak update`.
|
|
apply: bool,
|
|
/// A ref whose update command fails.
|
|
fail_ref: Option<String>,
|
|
update_calls: Vec<Vec<String>>,
|
|
}
|
|
|
|
struct FpRunner {
|
|
state: std::sync::Mutex<FpState>,
|
|
}
|
|
|
|
impl FpRunner {
|
|
fn new(state: FpState) -> Self {
|
|
Self {
|
|
state: std::sync::Mutex::new(state),
|
|
}
|
|
}
|
|
fn set_updates(&self, updates: Vec<FpUpdate>) {
|
|
self.state.lock().unwrap().updates = updates;
|
|
}
|
|
fn update_calls(&self) -> Vec<Vec<String>> {
|
|
self.state.lock().unwrap().update_calls.clone()
|
|
}
|
|
}
|
|
|
|
impl crate::command::ProcessRunner for FpRunner {
|
|
fn execute(&self, spec: &crate::command::CommandSpec) -> Result<CommandOutput> {
|
|
let is_flatpak = spec.args.iter().any(|a| a == "flatpak");
|
|
if !is_flatpak {
|
|
return Ok(ok_out(""));
|
|
}
|
|
if spec.args.iter().any(|a| a == "--version") {
|
|
return Ok(ok_out("Flatpak 1.18\n"));
|
|
}
|
|
if spec.args.iter().any(|a| a == "remote-ls") {
|
|
let state = self.state.lock().unwrap();
|
|
let mut text = String::new();
|
|
// remote-ls columns: ref, version (the remote ref form is
|
|
// normalized by the caller, so use the local form here).
|
|
for u in &state.updates {
|
|
text.push_str(&format!("{}\t{}\n", u.reference, u.version));
|
|
}
|
|
return Ok(ok_out(&text));
|
|
}
|
|
// The user installation is empty in these fixtures.
|
|
if spec.args.iter().any(|a| a == "--user") {
|
|
return Ok(ok_out(""));
|
|
}
|
|
// Verification: bare ref -> active commit.
|
|
if spec.args.iter().any(|a| a == "list") {
|
|
let state = self.state.lock().unwrap();
|
|
let mut text = String::new();
|
|
for (r, (_, commit)) in &state.installed {
|
|
let bare = r
|
|
.strip_prefix("app/")
|
|
.or_else(|| r.strip_prefix("runtime/"))
|
|
.unwrap_or(r);
|
|
text.push_str(&format!("{bare}\t{commit}\n"));
|
|
}
|
|
return Ok(ok_out(&text));
|
|
}
|
|
if spec.args.iter().any(|a| a == "info") {
|
|
let state = self.state.lock().unwrap();
|
|
let identity = spec.args.last().cloned().unwrap_or_default();
|
|
let (version, active) = state
|
|
.installed
|
|
.get(&identity)
|
|
.cloned()
|
|
.unwrap_or(("?".into(), "?".into()));
|
|
let latest = state
|
|
.updates
|
|
.iter()
|
|
.find(|u| u.reference == identity)
|
|
.map(|u| u.commit.clone())
|
|
.unwrap_or_else(|| active.clone());
|
|
return Ok(ok_out(&format!(
|
|
"Version: {version}\nActive commit: {active}\nLatest commit: {latest}\n"
|
|
)));
|
|
}
|
|
if spec.args.iter().any(|a| a == "update") {
|
|
let mut state = self.state.lock().unwrap();
|
|
let target = spec.args.last().cloned().unwrap_or_default();
|
|
state.update_calls.push(spec.args.clone());
|
|
if state.fail_ref.as_deref() == Some(target.as_str()) {
|
|
return Ok(crate::command::CommandOutput {
|
|
status: 1,
|
|
success: false,
|
|
stdout: Vec::new(),
|
|
stderr: b"update failed".to_vec(),
|
|
duration: std::time::Duration::from_millis(1),
|
|
});
|
|
}
|
|
if state.apply {
|
|
let applied = state
|
|
.updates
|
|
.iter()
|
|
.find(|u| u.reference == target)
|
|
.map(|u| (u.version.clone(), u.commit.clone()));
|
|
if let Some((version, commit)) = applied {
|
|
state.installed.insert(target.clone(), (version, commit));
|
|
}
|
|
}
|
|
return Ok(ok_out("updated\n"));
|
|
}
|
|
Ok(ok_out(""))
|
|
}
|
|
}
|
|
|
|
fn fp_exec(state: FpState) -> (Exec, std::sync::Arc<FpRunner>) {
|
|
let path = tempfile::tempdir().unwrap().keep();
|
|
let logger =
|
|
crate::log::Logger::open(path.join("l.log"), phoebus_core::Redactor::new(), false);
|
|
let runner = std::sync::Arc::new(FpRunner::new(state));
|
|
// The runner must be shared; Exec owns a Box, so wrap an Arc-backed clone.
|
|
struct Shared(std::sync::Arc<FpRunner>);
|
|
impl crate::command::ProcessRunner for Shared {
|
|
fn execute(&self, spec: &crate::command::CommandSpec) -> Result<CommandOutput> {
|
|
self.0.execute(spec)
|
|
}
|
|
}
|
|
let exec = Exec::new(
|
|
Box::new(Shared(runner.clone())),
|
|
logger,
|
|
false,
|
|
path.join("root"),
|
|
path,
|
|
);
|
|
// `tool_in_target` needs a real flatpak binary in the target.
|
|
install_fake_tool(&exec.root, "flatpak");
|
|
(exec, runner)
|
|
}
|
|
|
|
fn fp_update(reference: &str, version: &str, commit: &str) -> FpUpdate {
|
|
FpUpdate {
|
|
reference: reference.into(),
|
|
version: version.into(),
|
|
commit: commit.into(),
|
|
}
|
|
}
|
|
|
|
fn resolve_flatpak_plan(exec: &Exec) -> ResolvedPlan {
|
|
crate::actions::resolve_effects(
|
|
exec,
|
|
&phoebus_core::action::SystemAction::ApplyUpdates {
|
|
backend: Some(Backend::Flatpak),
|
|
},
|
|
&exec.root,
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn flatpak_update_transactions_bind_full_refs_and_immutable_commits() {
|
|
let ref_firefox = "app/org.mozilla.firefox/x86_64/stable";
|
|
// updates: ref, application, version, branch, commit, origin, arch
|
|
// `flatpak info` is the full-commit source; the ref keeps its
|
|
// `app/`/`runtime/` type prefix, which `info` and `update` require.
|
|
let info = "Version: 51.0\nActive commit: OLDCOMMIT\nLatest commit: NEWCOMMIT\n";
|
|
let (version, active, latest) = crate::actions::parse_flatpak_info(info);
|
|
assert_eq!(version.as_deref(), Some("51.0"));
|
|
assert_eq!(active.as_deref(), Some("OLDCOMMIT"));
|
|
assert_eq!(latest.as_deref(), Some("NEWCOMMIT"));
|
|
assert!(ref_firefox.starts_with("app/"));
|
|
}
|
|
|
|
#[test]
|
|
fn snap_update_transactions_bind_revisions_and_scope() {
|
|
let refresh = "Name Version Rev Tracking Publisher Notes\n\
|
|
hello 2.11 32 latest/stable canonical✓ -\n";
|
|
let installed = "Name Version Rev Tracking Publisher Notes\n\
|
|
hello 2.10 29 latest/stable canonical✓ -\n";
|
|
let txns = crate::actions::parse_snap_transactions(refresh, installed);
|
|
assert_eq!(txns.len(), 1);
|
|
let t = &txns[0];
|
|
assert_eq!(t.identity, "hello");
|
|
assert_eq!(t.previous_version.as_deref(), Some("2.10"));
|
|
assert_eq!(t.next_version, "2.11");
|
|
assert_eq!(t.previous_revision.as_deref(), Some("29"));
|
|
assert_eq!(t.next_revision.as_deref(), Some("32"));
|
|
assert_eq!(t.scope.as_deref(), Some("latest/stable"));
|
|
assert!(t.exact);
|
|
}
|
|
|
|
#[test]
|
|
fn flatpak_update_execution_carries_the_reviewed_transaction() {
|
|
let ref_a = "app/org.mozilla.firefox/x86_64/stable";
|
|
let ref_b = "runtime/org.gnome.Platform/x86_64/45";
|
|
let mut state = FpState {
|
|
apply: true,
|
|
..Default::default()
|
|
};
|
|
state
|
|
.installed
|
|
.insert(ref_a.into(), ("128".into(), "OLD_A".into()));
|
|
state
|
|
.installed
|
|
.insert(ref_b.into(), ("45.1".into(), "OLD_B".into()));
|
|
let (exec, runner) = fp_exec(state);
|
|
runner.set_updates(vec![
|
|
fp_update(ref_a, "129", "NEW_A"),
|
|
fp_update(ref_b, "45.2", "NEW_B"),
|
|
]);
|
|
let plan = resolve_flatpak_plan(&exec);
|
|
// The reviewed plan binds full refs, commits and the runtime.
|
|
assert_eq!(plan.updates.len(), 2);
|
|
assert!(plan.updates.iter().all(|t| t.exact));
|
|
assert!(plan.updates.iter().any(|t| t.identity == ref_b));
|
|
|
|
let out = apply_updates(
|
|
&exec,
|
|
Path::new(&exec.root),
|
|
Some(Backend::Flatpak),
|
|
Some(&plan),
|
|
)
|
|
.unwrap();
|
|
assert!(out.contains("updated 2 reviewed ref"), "{out}");
|
|
let calls = runner.update_calls();
|
|
assert_eq!(calls.len(), 2);
|
|
for call in &calls {
|
|
assert!(call.iter().any(|a| a == "-y"));
|
|
assert!(call.iter().any(|a| a == "--system"));
|
|
assert!(call.iter().any(|a| a == "--noninteractive"));
|
|
assert!(call.iter().any(|a| a.starts_with("--commit=")));
|
|
}
|
|
assert!(calls.iter().any(|c| c.last().unwrap() == ref_a));
|
|
assert!(calls.iter().any(|c| c.last().unwrap() == ref_b));
|
|
}
|
|
|
|
#[test]
|
|
fn flatpak_update_refuses_a_changed_commit_with_unchanged_version() {
|
|
let reference = "app/org.mozilla.firefox/x86_64/stable";
|
|
let mut state = FpState {
|
|
apply: true,
|
|
..Default::default()
|
|
};
|
|
state
|
|
.installed
|
|
.insert(reference.into(), ("128".into(), "OLD".into()));
|
|
let (exec, runner) = fp_exec(state);
|
|
runner.set_updates(vec![fp_update(reference, "129", "REVIEWED")]);
|
|
let plan = resolve_flatpak_plan(&exec);
|
|
// The remote silently republishes a different commit with the same
|
|
// human-readable version.
|
|
runner.set_updates(vec![fp_update(reference, "129", "DIFFERENT")]);
|
|
let err = apply_updates(
|
|
&exec,
|
|
Path::new(&exec.root),
|
|
Some(Backend::Flatpak),
|
|
Some(&plan),
|
|
)
|
|
.unwrap_err();
|
|
assert!(
|
|
err.to_string().contains("changed before execution"),
|
|
"{err}"
|
|
);
|
|
// No update command may have run for an unreviewed commit.
|
|
assert!(runner.update_calls().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn flatpak_update_refuses_a_newly_appeared_update() {
|
|
let reference = "app/org.mozilla.firefox/x86_64/stable";
|
|
let mut state = FpState {
|
|
apply: true,
|
|
..Default::default()
|
|
};
|
|
state
|
|
.installed
|
|
.insert(reference.into(), ("128".into(), "OLD".into()));
|
|
let (exec, runner) = fp_exec(state);
|
|
runner.set_updates(vec![fp_update(reference, "129", "NEW")]);
|
|
let plan = resolve_flatpak_plan(&exec);
|
|
// A second update appears after review.
|
|
runner.set_updates(vec![
|
|
fp_update(reference, "129", "NEW"),
|
|
fp_update("app/org.gnome.Calc/x86_64/stable", "46", "CALC"),
|
|
]);
|
|
let err = apply_updates(
|
|
&exec,
|
|
Path::new(&exec.root),
|
|
Some(Backend::Flatpak),
|
|
Some(&plan),
|
|
)
|
|
.unwrap_err();
|
|
assert!(
|
|
err.to_string().contains("changed before execution"),
|
|
"{err}"
|
|
);
|
|
assert!(runner.update_calls().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn flatpak_update_verifies_the_resulting_state() {
|
|
let reference = "app/org.mozilla.firefox/x86_64/stable";
|
|
let mut state = FpState {
|
|
// Do NOT apply: the installed commit never reaches the reviewed one.
|
|
apply: false,
|
|
..Default::default()
|
|
};
|
|
state
|
|
.installed
|
|
.insert(reference.into(), ("128".into(), "OLD".into()));
|
|
let (exec, runner) = fp_exec(state);
|
|
runner.set_updates(vec![fp_update(reference, "129", "NEW")]);
|
|
let plan = resolve_flatpak_plan(&exec);
|
|
let err = apply_updates(
|
|
&exec,
|
|
Path::new(&exec.root),
|
|
Some(Backend::Flatpak),
|
|
Some(&plan),
|
|
)
|
|
.unwrap_err();
|
|
let text = err.to_string();
|
|
assert!(text.contains("did not reach the reviewed commit"), "{text}");
|
|
}
|
|
|
|
#[test]
|
|
fn flatpak_execution_requires_a_reviewed_transaction() {
|
|
let (exec, runner) = fp_exec(FpState {
|
|
apply: true,
|
|
..Default::default()
|
|
});
|
|
// No approved plan is supplied: the source must refuse rather than
|
|
// discover and execute.
|
|
let err =
|
|
apply_updates(&exec, Path::new(&exec.root), Some(Backend::Flatpak), None).unwrap_err();
|
|
assert!(err.to_string().contains("no reviewed transaction"), "{err}");
|
|
assert!(runner.update_calls().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_source_that_failed_resolution_is_not_re_discovered_at_execution() {
|
|
let (exec, runner) = fp_exec(FpState {
|
|
apply: true,
|
|
..Default::default()
|
|
});
|
|
runner.set_updates(vec![fp_update(
|
|
"org.mozilla.firefox/x86_64/stable",
|
|
"129",
|
|
"NEW",
|
|
)]);
|
|
// The reviewed plan recorded that Flatpak could not be resolved and so
|
|
// carries no transaction. Even though updates are now available, they
|
|
// must not be executed.
|
|
let mut approved = ResolvedPlan {
|
|
established: true,
|
|
..Default::default()
|
|
};
|
|
approved
|
|
.consequences
|
|
.push("[flatpak] updates could not be resolved: offline".into());
|
|
approved.recompute_digest();
|
|
let err = apply_updates(
|
|
&exec,
|
|
Path::new(&exec.root),
|
|
Some(Backend::Flatpak),
|
|
Some(&approved),
|
|
)
|
|
.unwrap_err();
|
|
let text = err.to_string();
|
|
assert!(text.contains("not re-discovered"), "{text}");
|
|
// Most importantly, no update and no re-discovery happened: the runner
|
|
// recorded no update call.
|
|
assert!(runner.update_calls().is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn alternate_root_flatpak_search_is_chrooted_and_never_host() {
|
|
let runner = RecordingRunner::new();
|
|
runner.set_response("chroot", ok("org.mozilla.firefox\t128\tFirefox\n"));
|
|
let exec = make_exec(runner.clone());
|
|
let root = Path::new(&exec.root);
|
|
install_fake_tool(root, "flatpak");
|
|
let report = search(&exec, root, "firefox", Some(Backend::Flatpak));
|
|
assert!(report.sources[0].supported, "{:?}", report.sources[0]);
|
|
// No host flatpak/snap process may be invoked for an alternate root.
|
|
assert!(
|
|
!runner
|
|
.recorded()
|
|
.iter()
|
|
.any(|s| s.program == "flatpak" || s.program == "snap"),
|
|
"{:?}",
|
|
runner.programs()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn apply_updates_never_runs_host_snap_for_an_alternate_root() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner.clone());
|
|
let err =
|
|
apply_updates(&exec, Path::new(&exec.root), Some(Backend::Snap), None).unwrap_err();
|
|
assert!(err.to_string().contains("alternate root"), "{err}");
|
|
// Crucially, no host `snap` command was attempted.
|
|
assert!(
|
|
!runner.recorded().iter().any(|s| s.program == "snap"),
|
|
"alternate-root snap must not touch the host: {:?}",
|
|
runner.programs()
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn parse_flatpak_lines_handles_tabs_and_spaces() {
|
|
let rows =
|
|
parse_flatpak_lines("org.mozilla.firefox\t128.0\tFirefox\norg.gnome.Calc 45 Calc\n");
|
|
assert_eq!(rows.len(), 2);
|
|
assert_eq!(rows[0], vec!["org.mozilla.firefox", "128.0", "Firefox"]);
|
|
assert_eq!(rows[1], vec!["org.gnome.Calc", "45", "Calc"]);
|
|
}
|
|
|
|
#[test]
|
|
fn split_depend_keeps_real_atoms() {
|
|
let deps = split_depend(">=dev-libs/glib-2.0 !dev-libs/foo app-misc/hello");
|
|
assert_eq!(
|
|
deps,
|
|
vec![">=dev-libs/glib-2.0", "dev-libs/foo", "app-misc/hello"]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn package_dependencies_uses_installed_metadata_not_an_install_plan() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
// `vim` depends on an already-installed runtime library, a conditional
|
|
// dependency and a blocker; `vim-core` is already installed.
|
|
for (atom, version) in [
|
|
("app-editors/vim", "9.1.0"),
|
|
("app-editors/vim-core", "9.1.0"),
|
|
] {
|
|
let dir = root.join(format!("var/db/pkg/{atom}-{version}"));
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
}
|
|
std::fs::write(
|
|
root.join("var/db/pkg/app-editors/vim-9.1.0/RDEPEND"),
|
|
"app-editors/vim-core python? ( dev-lang/python ) !app-editors/vim-minimal",
|
|
)
|
|
.unwrap();
|
|
|
|
let out = package_dependencies(&exec, root, "app-editors/vim", false).unwrap();
|
|
assert!(out.contains("app-editors/vim-core"), "{out}");
|
|
assert!(
|
|
out.contains("(installed)"),
|
|
"satisfied deps included: {out}"
|
|
);
|
|
assert!(out.contains("dev-lang/python"), "{out}");
|
|
assert!(
|
|
out.contains("python?"),
|
|
"conditional context preserved: {out}"
|
|
);
|
|
assert!(out.contains("blocker"), "{out}");
|
|
// The requested package is never reported as its own dependency.
|
|
assert!(
|
|
!out.contains("app-editors/vim (") && !out.contains("app-editors/vim-9.1.0"),
|
|
"{out}"
|
|
);
|
|
}
|
|
|
|
fn install_vdb(root: &Path, atom: &str, version: &str, fields: &[(&str, &str)]) {
|
|
let dir = root.join(format!("var/db/pkg/{atom}-{version}"));
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
for (name, value) in fields {
|
|
std::fs::write(dir.join(name), value).unwrap();
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn package_dependencies_reverse_finds_installed_dependants() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
let dir = root.join("var/db/pkg/app-misc/tool-1.0");
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
std::fs::write(dir.join("RDEPEND"), ">=dev-libs/glib-2.0").unwrap();
|
|
|
|
let out = package_dependencies(&exec, root, "dev-libs/glib", true).unwrap();
|
|
assert!(out.contains("app-misc/tool"), "{out}");
|
|
|
|
// A genuinely empty reverse set is distinct from a failed query.
|
|
let empty = package_dependencies(&exec, root, "dev-libs/nothing", true).unwrap();
|
|
assert!(empty.contains("no installed package depends on"), "{empty}");
|
|
}
|
|
|
|
#[test]
|
|
fn reverse_dependencies_include_an_installed_target() {
|
|
// Regression: the queried package is installed, and a *different*
|
|
// installed package depends on it. The old self-reference check compared
|
|
// the dependency target with the queried package and wrongly dropped
|
|
// exactly this valid match.
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
install_vdb(root, "dev-libs/libfoo", "1.2.3", &[]);
|
|
install_vdb(
|
|
root,
|
|
"app-misc/consumer",
|
|
"2.0",
|
|
&[("RDEPEND", ">=dev-libs/libfoo-1.2")],
|
|
);
|
|
|
|
// Full and bare names both find the real dependent.
|
|
for query in ["dev-libs/libfoo", "libfoo"] {
|
|
let out = package_dependencies(&exec, root, query, true).unwrap();
|
|
assert!(out.contains("app-misc/consumer"), "{query} -> {out}");
|
|
assert!(out.contains("dev-libs/libfoo"), "{query} -> {out}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn reverse_dependencies_handle_conditionals_and_exclude_self_references() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let root = Path::new(&exec.root);
|
|
install_vdb(root, "dev-libs/libfoo", "1.2.3", &[]);
|
|
install_vdb(
|
|
root,
|
|
"app-misc/consumer",
|
|
"2.0",
|
|
&[(
|
|
"RDEPEND",
|
|
"ssl? ( >=dev-libs/libfoo-1.2 ) !dev-libs/libfoo[old]",
|
|
)],
|
|
);
|
|
// A genuine self-reference: libfoo declares a dependency on itself.
|
|
install_vdb(
|
|
root,
|
|
"dev-libs/libfoo",
|
|
"1.2.3",
|
|
&[("RDEPEND", ">=dev-libs/libfoo-1.2")],
|
|
);
|
|
|
|
let out = package_dependencies(&exec, root, "dev-libs/libfoo", true).unwrap();
|
|
assert!(out.contains("from app-misc/consumer"), "{out}");
|
|
assert!(out.contains("ssl?"), "condition preserved: {out}");
|
|
assert!(
|
|
!out.contains("from dev-libs/libfoo"),
|
|
"a package declaring a dependency on itself is not a dependent: {out}"
|
|
);
|
|
|
|
// A negative: nothing depends on an unrelated installed package.
|
|
let none = package_dependencies(&exec, root, "app-misc/lonely", true).unwrap();
|
|
assert!(none.contains("no installed package depends on"), "{none}");
|
|
}
|
|
|
|
#[test]
|
|
fn check_updates_can_be_restricted_to_one_source() {
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner);
|
|
let overview = check_updates(&exec, Path::new("/"), Some(Backend::AppImage));
|
|
assert_eq!(overview.sources.len(), 1);
|
|
assert_eq!(overview.sources[0].backend, Backend::AppImage);
|
|
assert!(!overview.sources[0].supported);
|
|
assert!(overview.sources[0].note.contains("AppImage"));
|
|
}
|
|
}
|