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

Merge pull request #5733 from sylvestre/getegid_geteuid

use our getegid & geteuid wrappers function instead of libc calls
This commit is contained in:
Daniel Hofstetter 2023-12-28 08:49:34 +01:00 committed by GitHub
commit 179f0a7c3c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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::perms::{wrap_chown, Verbosity, VerbosityLevel};
use uucore::{format_usage, help_about, help_usage, show, show_error, show_if_err, uio_error}; 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::error::Error;
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
use std::fs; use std::fs;
@ -29,6 +28,8 @@ use std::os::unix::fs::MetadataExt;
use std::os::unix::prelude::OsStrExt; use std::os::unix::prelude::OsStrExt;
use std::path::{Path, PathBuf, MAIN_SEPARATOR}; use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use std::process; use std::process;
#[cfg(not(target_os = "windows"))]
use uucore::process::{getegid, geteuid};
const DEFAULT_MODE: u32 = 0o755; const DEFAULT_MODE: u32 = 0o755;
const DEFAULT_STRIP_PROGRAM: &str = "strip"; const DEFAULT_STRIP_PROGRAM: &str = "strip";
@ -959,10 +960,8 @@ fn need_copy(from: &Path, to: &Path, b: &Behavior) -> UResult<bool> {
} }
} else { } else {
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
unsafe { if to_meta.uid() != geteuid() || to_meta.gid() != getegid() {
if to_meta.uid() != geteuid() || to_meta.gid() != getegid() { return Ok(true);
return Ok(true);
}
} }
} }

View file

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

View file

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

View file

@ -7,10 +7,9 @@ use std::ffi::OsString;
use std::io; use std::io;
use uucore::entries::uid2usr; use uucore::entries::uid2usr;
use uucore::process::geteuid;
pub fn get_username() -> io::Result<OsString> { 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 should arguably return an OsString but currently doesn't
uid2usr(uid).map(Into::into) uid2usr(geteuid()).map(Into::into)
} }