resolve issues

This commit is contained in:
Samuel Talbot
2024-04-05 08:43:57 +13:00
parent fd1ba3ff2a
commit dbb14eb5b5
5 changed files with 30 additions and 30 deletions
+2 -3
View File
@@ -17,6 +17,7 @@ path-absolutize = "3.1"
regex = { version = "1.10", default-features = false, features = ["std"] } regex = { version = "1.10", default-features = false, features = ["std"] }
termios = "0.3" termios = "0.3"
walkdir = "2.5" walkdir = "2.5"
libc = "0.2"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
@@ -29,12 +30,10 @@ pacdef_macros = { path = "../pacdef_macros", version = "1.0" }
alpm = { version = "3.0", optional = true } alpm = { version = "3.0", optional = true }
rust-apt = { version = "0.7", optional = true } rust-apt = { version = "0.7", optional = true }
libc = { version = "0.2", optional = true } # for debian
[dev-dependencies] [dev-dependencies]
rstest = "0.18" rstest = "0.18"
[features] [features]
default = [] default = []
arch = ["dep:alpm"] arch = ["dep:alpm"]
debian = ["dep:rust-apt", "dep:libc"] debian = ["dep:rust-apt"]
@@ -8,6 +8,7 @@ use rust_apt::new_cache;
use crate::backend::backend_trait::{Backend, Switches, Text}; use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants; use crate::backend::macros::impl_backend_constants;
use crate::backend::root::build_base_command_with_privileges;
use crate::{Group, Package}; use crate::{Group, Package};
#[derive(Debug, Clone)] #[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
}
+9 -11
View File
@@ -6,6 +6,7 @@ use regex::Regex;
use crate::backend::backend_trait::{Backend, Switches, Text}; use crate::backend::backend_trait::{Backend, Switches, Text};
use crate::backend::macros::impl_backend_constants; use crate::backend::macros::impl_backend_constants;
use crate::backend::root::build_base_command_with_privileges;
use crate::{Group, Package}; use crate::{Group, Package};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -42,12 +43,12 @@ impl Backend for Void {
cmd.args(["-l"]); cmd.args(["-l"]);
let output = String::from_utf8(cmd.output()?.stdout)?; let output = String::from_utf8(cmd.output()?.stdout)?;
let packages: HashSet<Package> = output let packages = output
.lines() .lines()
.map(|line| { .map(|line| {
let result = re1.replace_all(line, "").to_string(); let result = re1.replace_all(line, "");
let result = re2.replace_all(&result, "").to_string(); let result = re2.replace_all(&result, "");
result.into() result.to_string().into()
}) })
.collect(); .collect();
Ok(packages) Ok(packages)
@@ -61,7 +62,7 @@ impl Backend for Void {
cmd.args(["-m"]); cmd.args(["-m"]);
let output = String::from_utf8(cmd.output()?.stdout)?; let output = String::from_utf8(cmd.output()?.stdout)?;
let packages: HashSet<Package> = output let packages = output
.lines() .lines()
.map(|line| { .map(|line| {
let result = re.replace_all(line, "").to_string(); let result = re.replace_all(line, "").to_string();
@@ -73,8 +74,7 @@ impl Backend for Void {
/// Install the specified packages. /// Install the specified packages.
fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> { fn install_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
let mut cmd = Command::new("sudo"); let mut cmd = build_base_command_with_privileges(INSTALL_BINARY);
cmd.arg(INSTALL_BINARY);
cmd.args(self.get_switches_install()); cmd.args(self.get_switches_install());
if noconfirm { if noconfirm {
@@ -90,8 +90,7 @@ impl Backend for Void {
} }
fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> { fn remove_packages(&self, packages: &[Package], noconfirm: bool) -> Result<ExitStatus> {
let mut cmd = Command::new("sudo"); let mut cmd = build_base_command_with_privileges(REMOVE_BINARY);
cmd.arg(REMOVE_BINARY);
cmd.args(self.get_switches_remove()); cmd.args(self.get_switches_remove());
if noconfirm { if noconfirm {
@@ -107,8 +106,7 @@ impl Backend for Void {
} }
fn make_dependency(&self, packages: &[Package]) -> Result<ExitStatus> { fn make_dependency(&self, packages: &[Package]) -> Result<ExitStatus> {
let mut cmd = Command::new("sudo"); let mut cmd = build_base_command_with_privileges(PKGDB_BINARY);
cmd.arg(PKGDB_BINARY);
cmd.args(self.get_switches_make_dependency()); cmd.args(self.get_switches_make_dependency());
for p in packages { for p in packages {
+1
View File
@@ -2,6 +2,7 @@ mod actual;
mod backend_trait; mod backend_trait;
mod iter; mod iter;
mod macros; mod macros;
mod root;
mod todo_per_backend; mod todo_per_backend;
pub use backend_trait::Backend; pub use backend_trait::Backend;
+17
View File
@@ -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
}