ksud: minor tweaks

This commit is contained in:
weishu 2024-01-04 16:12:15 +08:00
parent 153ce9a39a
commit 01711b4114
2 changed files with 20 additions and 22 deletions

View File

@ -58,7 +58,7 @@ fn print_usage(program: &str, opts: Options) {
print!("{}", opts.usage(&brief));
}
fn set_identity(uid: u32, gid: u32, groups: &Vec<u32>) {
fn set_identity(uid: u32, gid: u32, groups: &[u32]) {
#[cfg(any(target_os = "linux", target_os = "android"))]
unsafe {
if !groups.is_empty() {
@ -77,6 +77,8 @@ pub fn root_shell() -> Result<()> {
#[cfg(unix)]
pub fn root_shell() -> Result<()> {
// we are root now, this was set in kernel!
use anyhow::anyhow;
let env_args: Vec<String> = std::env::args().collect();
let program = env_args[0].clone();
let args = env_args
@ -169,27 +171,19 @@ pub fn root_shell() -> Result<()> {
let mut is_login = matches.opt_present("l");
let preserve_env = matches.opt_present("p");
let mount_master = matches.opt_present("M");
let mut groups = Vec::<u32>::new();
for g in matches.opt_strs("G") {
if let core::result::Result::Ok(id) = g.parse::<u32>() {
groups.push(id);
} else {
println!("Invalid GID: {g}");
print_usage(&program, opts);
return Ok(());
}
}
let mut gid: Option<u32> = None;
let groups = matches
.opt_strs("G")
.into_iter()
.map(|g| g.parse::<u32>().map_err(|_| anyhow!("Invalid GID: {}", g)))
.collect::<Result<Vec<_>, _>>()?;
// if -g provided, use it.
if let Some(g) = matches.opt_str("g") {
if let core::result::Result::Ok(id) = g.parse::<u32>() {
gid = Some(id);
} else {
println!("Invalid GID: {g}");
print_usage(&program, opts);
return Ok(());
}
}
let mut gid = matches
.opt_str("g")
.map(|g| g.parse::<u32>().map_err(|_| anyhow!("Invalid GID: {}", g)))
.transpose()?;
// otherwise, use the first gid of groups.
if gid.is_none() && !groups.is_empty() {
gid = Some(groups[0]);

View File

@ -45,7 +45,11 @@ pub fn ensure_dir_exists<T: AsRef<Path>>(dir: T) -> Result<()> {
}
}
pub fn ensure_binary<T: AsRef<Path>>(path: T, contents: &[u8], ignore_if_exist: bool) -> Result<()> {
pub fn ensure_binary<T: AsRef<Path>>(
path: T,
contents: &[u8],
ignore_if_exist: bool,
) -> Result<()> {
if ignore_if_exist && path.as_ref().exists() {
return Ok(());
}