mirror of
https://github.com/uutils/coreutils.git
synced 2025-12-23 08:47:37 +00:00
Use common mode parsing in mkdirm, mkfifo, mknod
This commit is contained in:
parent
22344cea99
commit
114be93e01
4 changed files with 19 additions and 60 deletions
|
|
@ -57,20 +57,11 @@ fn get_mode(_matches: &ArgMatches) -> Result<u32, String> {
|
|||
#[cfg(not(windows))]
|
||||
fn get_mode(matches: &ArgMatches) -> Result<u32, String> {
|
||||
// Not tested on Windows
|
||||
let mut new_mode = DEFAULT_PERM;
|
||||
|
||||
if let Some(m) = matches.get_one::<String>(options::MODE) {
|
||||
for mode in m.split(',') {
|
||||
if mode.chars().any(|c| c.is_ascii_digit()) {
|
||||
new_mode = mode::parse_numeric(new_mode, m, true)?;
|
||||
} else {
|
||||
new_mode = mode::parse_symbolic(new_mode, mode, mode::get_umask(), true)?;
|
||||
}
|
||||
}
|
||||
Ok(new_mode)
|
||||
mode::parse_chmod(DEFAULT_PERM, m, true, mode::get_umask())
|
||||
} else {
|
||||
// If no mode argument is specified return the mode derived from umask
|
||||
Ok(!mode::get_umask() & 0o0777)
|
||||
Ok(!mode::get_umask() & DEFAULT_PERM)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -119,19 +119,11 @@ pub fn uu_app() -> Command {
|
|||
|
||||
fn calculate_mode(mode_option: Option<&String>) -> Result<u32, String> {
|
||||
let umask = uucore::mode::get_umask();
|
||||
let mut mode = 0o666; // Default mode for FIFOs
|
||||
let mode = 0o666; // Default mode for FIFOs
|
||||
|
||||
if let Some(m) = mode_option {
|
||||
if m.chars().any(|c| c.is_ascii_digit()) {
|
||||
mode = uucore::mode::parse_numeric(mode, m, false)?;
|
||||
} else {
|
||||
for item in m.split(',') {
|
||||
mode = uucore::mode::parse_symbolic(mode, item, umask, false)?;
|
||||
}
|
||||
}
|
||||
uucore::mode::parse_chmod(mode, m, false, umask)
|
||||
} else {
|
||||
mode &= !umask; // Apply umask if no mode is specified
|
||||
Ok(mode & !umask) // Apply umask if no mode is specified
|
||||
}
|
||||
|
||||
Ok(mode)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -225,8 +225,10 @@ pub fn uu_app() -> Command {
|
|||
)
|
||||
}
|
||||
|
||||
#[allow(clippy::unnecessary_cast)]
|
||||
fn parse_mode(str_mode: &str) -> Result<mode_t, String> {
|
||||
uucore::mode::parse_mode(str_mode)
|
||||
let default_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32;
|
||||
uucore::mode::parse_chmod(default_mode, str_mode, true, uucore::mode::get_umask())
|
||||
.map_err(|e| {
|
||||
translate!(
|
||||
"mknod-error-invalid-mode",
|
||||
|
|
@ -237,7 +239,7 @@ fn parse_mode(str_mode: &str) -> Result<mode_t, String> {
|
|||
if mode > 0o777 {
|
||||
Err(translate!("mknod-error-mode-permission-bits-only"))
|
||||
} else {
|
||||
Ok(mode)
|
||||
Ok(mode as mode_t)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
// spell-checker:ignore (vars) fperm srwx
|
||||
|
||||
use libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, mode_t, umask};
|
||||
use libc::umask;
|
||||
|
||||
pub fn parse_numeric(fperm: u32, mut mode: &str, considering_dir: bool) -> Result<u32, String> {
|
||||
let (op, pos) = parse_op(mode).map_or_else(|_| (None, 0), |(op, pos)| (Some(op), pos));
|
||||
|
|
@ -169,13 +169,6 @@ pub fn parse(mode_string: &str, considering_dir: bool, umask: u32) -> Result<u32
|
|||
parse_chmod(0, mode_string, considering_dir, umask)
|
||||
}
|
||||
|
||||
#[allow(clippy::unnecessary_cast)]
|
||||
pub fn parse_mode(mode: &str) -> Result<mode_t, String> {
|
||||
let mut new_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32;
|
||||
new_mode = parse_chmod(new_mode, mode, true, get_umask())?;
|
||||
Ok(new_mode as mode_t)
|
||||
}
|
||||
|
||||
pub fn get_umask() -> u32 {
|
||||
// There's no portable way to read the umask without changing it.
|
||||
// We have to replace it and then quickly set it back, hopefully before
|
||||
|
|
@ -207,24 +200,20 @@ mod tests {
|
|||
|
||||
use super::parse;
|
||||
use super::parse_chmod;
|
||||
use super::parse_mode;
|
||||
|
||||
#[test]
|
||||
fn test_symbolic_modes() {
|
||||
assert_eq!(parse_mode("u+x").unwrap(), 0o766);
|
||||
assert_eq!(
|
||||
parse_mode("+x").unwrap(),
|
||||
if crate::os::is_wsl_1() { 0o776 } else { 0o777 }
|
||||
);
|
||||
assert_eq!(parse_mode("a-w").unwrap(), 0o444);
|
||||
assert_eq!(parse_mode("g-r").unwrap(), 0o626);
|
||||
fn test_chmod_symbolic_modes() {
|
||||
assert_eq!(parse_chmod(0o666, "u+x", false, 0).unwrap(), 0o766);
|
||||
assert_eq!(parse_chmod(0o666, "+x", false, 0).unwrap(), 0o777);
|
||||
assert_eq!(parse_chmod(0o666, "a-w", false, 0).unwrap(), 0o444);
|
||||
assert_eq!(parse_chmod(0o666, "g-r", false, 0).unwrap(), 0o626);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_numeric_modes() {
|
||||
assert_eq!(parse_mode("644").unwrap(), 0o644);
|
||||
assert_eq!(parse_mode("+100").unwrap(), 0o766);
|
||||
assert_eq!(parse_mode("-4").unwrap(), 0o662);
|
||||
fn test_chmod_numeric_modes() {
|
||||
assert_eq!(parse_chmod(0o666, "644", false, 0).unwrap(), 0o644);
|
||||
assert_eq!(parse_chmod(0o666, "+100", false, 0).unwrap(), 0o766);
|
||||
assert_eq!(parse_chmod(0o666, "-4", false, 0).unwrap(), 0o662);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -345,19 +334,4 @@ mod tests {
|
|||
// First add user write, then set to 755 (should override)
|
||||
assert_eq!(parse("u+w,755", false, 0).unwrap(), 0o755);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chmod_symbolic_modes() {
|
||||
assert_eq!(parse_chmod(0o666, "u+x", false, 0).unwrap(), 0o766);
|
||||
assert_eq!(parse_chmod(0o666, "+x", false, 0).unwrap(), 0o777);
|
||||
assert_eq!(parse_chmod(0o666, "a-w", false, 0).unwrap(), 0o444);
|
||||
assert_eq!(parse_chmod(0o666, "g-r", false, 0).unwrap(), 0o626);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_chmod_numeric_modes() {
|
||||
assert_eq!(parse_chmod(0o666, "644", false, 0).unwrap(), 0o644);
|
||||
assert_eq!(parse_chmod(0o666, "+100", false, 0).unwrap(), 0o766);
|
||||
assert_eq!(parse_chmod(0o666, "-4", false, 0).unwrap(), 0o662);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue