linux: only run pacman when the pacman database exists, so an unrelated pacman on PATH is not launched (#158) (#180)

This commit is contained in:
MarekSpodBiedry 2026-10-12 07:53:25 +02:00 committed by GitHub
parent 165f5358e0
commit 31a5e426e7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 21 additions and 1 deletions

View File

@ -232,7 +232,11 @@ pub mod linux {
if let Some(list) = capture("rpm", &["-qa", "--qf", "%{NAME}|%{VERSION}\n"], t) {
add(InstallSource::Rpm, packages::parse_rpm(&list));
}
if let Some(list) = capture("pacman", &["-Q"], t) {
// Only run `pacman` where its database exists: elsewhere `pacman` on PATH may be an unrelated program
// (the Pac-Man game in /usr/games on Debian-family systems).
if pacman_db_present(Path::new(PACMAN_DB))
&& let Some(list) = capture("pacman", &["-Q"], t)
{
add(InstallSource::Pacman, packages::parse_pacman(&list));
}
let arch = format!("--arch={}", std::env::consts::ARCH);
@ -249,6 +253,14 @@ pub mod linux {
out
}
/// The local package database of Arch's pacman.
const PACMAN_DB: &str = "/var/lib/pacman/local";
/// True when `db` is a directory, i.e. this system really uses pacman.
pub(crate) fn pacman_db_present(db: &Path) -> bool {
db.is_dir()
}
/// Where a package put the app's binary: on `PATH`, else `/usr/bin/<id>`.
fn binary_on_path(id: &str) -> String {
std::env::var_os("PATH")
@ -384,6 +396,14 @@ mod linux_tests {
assert!(!ours("unrelated-package"));
}
#[test]
fn pacman_is_only_queried_where_its_database_exists() {
let root = tempfile::tempdir().unwrap();
assert!(!linux::pacman_db_present(&root.path().join("local")));
std::fs::create_dir(root.path().join("local")).unwrap();
assert!(linux::pacman_db_present(&root.path().join("local")));
}
#[test]
fn reads_binary_version_from_cli() {
let photo = CraftApp::by_id("photocraft").unwrap();