1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

use our getegid & geteuid wrappers function instead of libc calls

This commit is contained in:
Sylvestre Ledru 2023-12-27 23:51:49 +01:00
parent 04a7f9c153
commit c38a43210c
4 changed files with 11 additions and 11 deletions

View file

@ -19,7 +19,6 @@ use uucore::mode::get_umask;
use uucore::perms::{wrap_chown, Verbosity, VerbosityLevel};
use uucore::{format_usage, help_about, help_usage, show, show_error, show_if_err, uio_error};
use libc::{getegid, geteuid};
use std::error::Error;
use std::fmt::{Debug, Display};
use std::fs;
@ -29,6 +28,8 @@ use std::os::unix::fs::MetadataExt;
use std::os::unix::prelude::OsStrExt;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use std::process;
#[cfg(not(target_os = "windows"))]
use uucore::process::{getegid, geteuid};
const DEFAULT_MODE: u32 = 0o755;
const DEFAULT_STRIP_PROGRAM: &str = "strip";
@ -959,10 +960,8 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> UResult<bool> {
}
} else {
#[cfg(not(target_os = "windows"))]
unsafe {
if to_meta.uid() != geteuid() || to_meta.gid() != getegid() {
return Ok(true);
}
if to_meta.uid() != geteuid() || to_meta.gid() != getegid() {
return Ok(true);
}
}

View file

@ -17,7 +17,7 @@ path = "src/test.rs"
[dependencies]
clap = { workspace = true }
libc = { workspace = true }
uucore = { workspace = true }
uucore = { workspace = true, features = ["process"] }
[target.'cfg(target_os = "redox")'.dependencies]
redox_syscall = { workspace = true }

View file

@ -17,6 +17,8 @@ use std::fs;
use std::os::unix::fs::MetadataExt;
use uucore::display::Quotable;
use uucore::error::{UResult, USimpleError};
#[cfg(not(windows))]
use uucore::process::{getegid, geteuid};
use uucore::{format_usage, help_about, help_section};
const ABOUT: &str = help_about!("test.md");
@ -276,7 +278,7 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool {
let geteuid = || {
#[cfg(not(target_os = "redox"))]
let euid = unsafe { libc::geteuid() };
let euid = geteuid();
#[cfg(target_os = "redox")]
let euid = syscall::geteuid().unwrap() as u32;
@ -285,7 +287,7 @@ fn path(path: &OsStr, condition: &PathCondition) -> bool {
let getegid = || {
#[cfg(not(target_os = "redox"))]
let egid = unsafe { libc::getegid() };
let egid = getegid();
#[cfg(target_os = "redox")]
let egid = syscall::getegid().unwrap() as u32;

View file

@ -7,10 +7,9 @@ use std::ffi::OsString;
use std::io;
use uucore::entries::uid2usr;
use uucore::process::geteuid;
pub fn get_username() -> io::Result<OsString> {
// SAFETY: getuid() does nothing with memory and is always successful.
let uid = unsafe { libc::geteuid() };
// uid2usr should arguably return an OsString but currently doesn't
uid2usr(uid).map(Into::into)
uid2usr(geteuid()).map(Into::into)
}