From dbb14eb5b5a10f3f471315ce2ce90ba8ef7fe3ec Mon Sep 17 00:00:00 2001 From: Samuel Talbot Date: Fri, 5 Apr 2024 08:43:57 +1300 Subject: [PATCH] resolve issues --- crates/pacdef_core/Cargo.toml | 5 ++--- .../pacdef_core/src/backend/actual/debian.rs | 17 +--------------- crates/pacdef_core/src/backend/actual/void.rs | 20 +++++++++---------- crates/pacdef_core/src/backend/mod.rs | 1 + crates/pacdef_core/src/backend/root.rs | 17 ++++++++++++++++ 5 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 crates/pacdef_core/src/backend/root.rs diff --git a/crates/pacdef_core/Cargo.toml b/crates/pacdef_core/Cargo.toml index 802d8fa..95a5240 100644 --- a/crates/pacdef_core/Cargo.toml +++ b/crates/pacdef_core/Cargo.toml @@ -17,6 +17,7 @@ path-absolutize = "3.1" regex = { version = "1.10", default-features = false, features = ["std"] } termios = "0.3" walkdir = "2.5" +libc = "0.2" serde = "1.0" serde_derive = "1.0" @@ -29,12 +30,10 @@ pacdef_macros = { path = "../pacdef_macros", version = "1.0" } alpm = { version = "3.0", optional = true } rust-apt = { version = "0.7", optional = true } -libc = { version = "0.2", optional = true } # for debian - [dev-dependencies] rstest = "0.18" [features] default = [] arch = ["dep:alpm"] -debian = ["dep:rust-apt", "dep:libc"] +debian = ["dep:rust-apt"] diff --git a/crates/pacdef_core/src/backend/actual/debian.rs b/crates/pacdef_core/src/backend/actual/debian.rs index 55e2088..054a07d 100644 --- a/crates/pacdef_core/src/backend/actual/debian.rs +++ b/crates/pacdef_core/src/backend/actual/debian.rs @@ -8,6 +8,7 @@ use rust_apt::new_cache; use crate::backend::backend_trait::{Backend, Switches, Text}; use crate::backend::macros::impl_backend_constants; +use crate::backend::root::build_base_command_with_privileges; use crate::{Group, Package}; #[derive(Debug, Clone)] @@ -104,19 +105,3 @@ impl Debian { } } } - -fn we_are_root() -> bool { - let uid = unsafe { libc::geteuid() }; - uid == 0 -} - -fn build_base_command_with_privileges(binary: &str) -> Command { - let cmd = if we_are_root() { - Command::new(binary) - } else { - let mut cmd = Command::new("sudo"); - cmd.arg(binary); - cmd - }; - cmd -} diff --git a/crates/pacdef_core/src/backend/actual/void.rs b/crates/pacdef_core/src/backend/actual/void.rs index 8917508..de476cd 100644 --- a/crates/pacdef_core/src/backend/actual/void.rs +++ b/crates/pacdef_core/src/backend/actual/void.rs @@ -6,6 +6,7 @@ use regex::Regex; use crate::backend::backend_trait::{Backend, Switches, Text}; use crate::backend::macros::impl_backend_constants; +use crate::backend::root::build_base_command_with_privileges; use crate::{Group, Package}; #[derive(Debug, Clone)] @@ -42,12 +43,12 @@ impl Backend for Void { cmd.args(["-l"]); let output = String::from_utf8(cmd.output()?.stdout)?; - let packages: HashSet = output + let packages = output .lines() .map(|line| { - let result = re1.replace_all(line, "").to_string(); - let result = re2.replace_all(&result, "").to_string(); - result.into() + let result = re1.replace_all(line, ""); + let result = re2.replace_all(&result, ""); + result.to_string().into() }) .collect(); Ok(packages) @@ -61,7 +62,7 @@ impl Backend for Void { cmd.args(["-m"]); let output = String::from_utf8(cmd.output()?.stdout)?; - let packages: HashSet = output + let packages = output .lines() .map(|line| { let result = re.replace_all(line, "").to_string(); @@ -73,8 +74,7 @@ impl Backend for Void { /// Install the specified packages. fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result { - let mut cmd = Command::new("sudo"); - cmd.arg(INSTALL_BINARY); + let mut cmd = build_base_command_with_privileges(INSTALL_BINARY); cmd.args(self.get_switches_install()); if noconfirm { @@ -90,8 +90,7 @@ impl Backend for Void { } fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result { - let mut cmd = Command::new("sudo"); - cmd.arg(REMOVE_BINARY); + let mut cmd = build_base_command_with_privileges(REMOVE_BINARY); cmd.args(self.get_switches_remove()); if noconfirm { @@ -107,8 +106,7 @@ impl Backend for Void { } fn make_dependency(&self, packages: &[Package]) -> Result { - let mut cmd = Command::new("sudo"); - cmd.arg(PKGDB_BINARY); + let mut cmd = build_base_command_with_privileges(PKGDB_BINARY); cmd.args(self.get_switches_make_dependency()); for p in packages { diff --git a/crates/pacdef_core/src/backend/mod.rs b/crates/pacdef_core/src/backend/mod.rs index 623475f..ab67304 100644 --- a/crates/pacdef_core/src/backend/mod.rs +++ b/crates/pacdef_core/src/backend/mod.rs @@ -2,6 +2,7 @@ mod actual; mod backend_trait; mod iter; mod macros; +mod root; mod todo_per_backend; pub use backend_trait::Backend; diff --git a/crates/pacdef_core/src/backend/root.rs b/crates/pacdef_core/src/backend/root.rs new file mode 100644 index 0000000..3f17861 --- /dev/null +++ b/crates/pacdef_core/src/backend/root.rs @@ -0,0 +1,17 @@ +use std::process::Command; + +pub fn we_are_root() -> bool { + let uid = unsafe { libc::geteuid() }; + uid == 0 +} + +pub fn build_base_command_with_privileges(binary: &str) -> Command { + let cmd = if we_are_root() { + Command::new(binary) + } else { + let mut cmd = Command::new("sudo"); + cmd.arg(binary); + cmd + }; + cmd +}