diff --git a/src/uu/mkdir/src/mkdir.rs b/src/uu/mkdir/src/mkdir.rs index a16be0c26..6ee610013 100644 --- a/src/uu/mkdir/src/mkdir.rs +++ b/src/uu/mkdir/src/mkdir.rs @@ -57,20 +57,11 @@ fn get_mode(_matches: &ArgMatches) -> Result { #[cfg(not(windows))] fn get_mode(matches: &ArgMatches) -> Result { // Not tested on Windows - let mut new_mode = DEFAULT_PERM; - if let Some(m) = matches.get_one::(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) } } diff --git a/src/uu/mkfifo/src/mkfifo.rs b/src/uu/mkfifo/src/mkfifo.rs index 572ea00b8..c55593dcb 100644 --- a/src/uu/mkfifo/src/mkfifo.rs +++ b/src/uu/mkfifo/src/mkfifo.rs @@ -119,19 +119,11 @@ pub fn uu_app() -> Command { fn calculate_mode(mode_option: Option<&String>) -> Result { 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) } diff --git a/src/uu/mknod/src/mknod.rs b/src/uu/mknod/src/mknod.rs index ca2640b68..cc22aee5f 100644 --- a/src/uu/mknod/src/mknod.rs +++ b/src/uu/mknod/src/mknod.rs @@ -225,8 +225,10 @@ pub fn uu_app() -> Command { ) } +#[allow(clippy::unnecessary_cast)] fn parse_mode(str_mode: &str) -> Result { - 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 { if mode > 0o777 { Err(translate!("mknod-error-mode-permission-bits-only")) } else { - Ok(mode) + Ok(mode as mode_t) } }) } diff --git a/src/uucore/src/lib/features/mode.rs b/src/uucore/src/lib/features/mode.rs index a9477fa5f..d562f1fe0 100644 --- a/src/uucore/src/lib/features/mode.rs +++ b/src/uucore/src/lib/features/mode.rs @@ -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 { 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 Result { - 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); - } }