Skip to content

Commit 114be93

Browse files
committed
Use common mode parsing in mkdirm, mkfifo, mknod
1 parent 22344ce commit 114be93

File tree

4 files changed

+19
-60
lines changed

4 files changed

+19
-60
lines changed

src/uu/mkdir/src/mkdir.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,11 @@ fn get_mode(_matches: &ArgMatches) -> Result<u32, String> {
5757
#[cfg(not(windows))]
5858
fn get_mode(matches: &ArgMatches) -> Result<u32, String> {
5959
// Not tested on Windows
60-
let mut new_mode = DEFAULT_PERM;
61-
6260
if let Some(m) = matches.get_one::<String>(options::MODE) {
63-
for mode in m.split(',') {
64-
if mode.chars().any(|c| c.is_ascii_digit()) {
65-
new_mode = mode::parse_numeric(new_mode, m, true)?;
66-
} else {
67-
new_mode = mode::parse_symbolic(new_mode, mode, mode::get_umask(), true)?;
68-
}
69-
}
70-
Ok(new_mode)
61+
mode::parse_chmod(DEFAULT_PERM, m, true, mode::get_umask())
7162
} else {
7263
// If no mode argument is specified return the mode derived from umask
73-
Ok(!mode::get_umask() & 0o0777)
64+
Ok(!mode::get_umask() & DEFAULT_PERM)
7465
}
7566
}
7667

src/uu/mkfifo/src/mkfifo.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,11 @@ pub fn uu_app() -> Command {
119119

120120
fn calculate_mode(mode_option: Option<&String>) -> Result<u32, String> {
121121
let umask = uucore::mode::get_umask();
122-
let mut mode = 0o666; // Default mode for FIFOs
122+
let mode = 0o666; // Default mode for FIFOs
123123

124124
if let Some(m) = mode_option {
125-
if m.chars().any(|c| c.is_ascii_digit()) {
126-
mode = uucore::mode::parse_numeric(mode, m, false)?;
127-
} else {
128-
for item in m.split(',') {
129-
mode = uucore::mode::parse_symbolic(mode, item, umask, false)?;
130-
}
131-
}
125+
uucore::mode::parse_chmod(mode, m, false, umask)
132126
} else {
133-
mode &= !umask; // Apply umask if no mode is specified
127+
Ok(mode & !umask) // Apply umask if no mode is specified
134128
}
135-
136-
Ok(mode)
137129
}

src/uu/mknod/src/mknod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,10 @@ pub fn uu_app() -> Command {
225225
)
226226
}
227227

228+
#[allow(clippy::unnecessary_cast)]
228229
fn parse_mode(str_mode: &str) -> Result<mode_t, String> {
229-
uucore::mode::parse_mode(str_mode)
230+
let default_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32;
231+
uucore::mode::parse_chmod(default_mode, str_mode, true, uucore::mode::get_umask())
230232
.map_err(|e| {
231233
translate!(
232234
"mknod-error-invalid-mode",
@@ -237,7 +239,7 @@ fn parse_mode(str_mode: &str) -> Result<mode_t, String> {
237239
if mode > 0o777 {
238240
Err(translate!("mknod-error-mode-permission-bits-only"))
239241
} else {
240-
Ok(mode)
242+
Ok(mode as mode_t)
241243
}
242244
})
243245
}

src/uucore/src/lib/features/mode.rs

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
// spell-checker:ignore (vars) fperm srwx
99

10-
use libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, mode_t, umask};
10+
use libc::umask;
1111

1212
pub fn parse_numeric(fperm: u32, mut mode: &str, considering_dir: bool) -> Result<u32, String> {
1313
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
169169
parse_chmod(0, mode_string, considering_dir, umask)
170170
}
171171

172-
#[allow(clippy::unnecessary_cast)]
173-
pub fn parse_mode(mode: &str) -> Result<mode_t, String> {
174-
let mut new_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32;
175-
new_mode = parse_chmod(new_mode, mode, true, get_umask())?;
176-
Ok(new_mode as mode_t)
177-
}
178-
179172
pub fn get_umask() -> u32 {
180173
// There's no portable way to read the umask without changing it.
181174
// We have to replace it and then quickly set it back, hopefully before
@@ -207,24 +200,20 @@ mod tests {
207200

208201
use super::parse;
209202
use super::parse_chmod;
210-
use super::parse_mode;
211203

212204
#[test]
213-
fn test_symbolic_modes() {
214-
assert_eq!(parse_mode("u+x").unwrap(), 0o766);
215-
assert_eq!(
216-
parse_mode("+x").unwrap(),
217-
if crate::os::is_wsl_1() { 0o776 } else { 0o777 }
218-
);
219-
assert_eq!(parse_mode("a-w").unwrap(), 0o444);
220-
assert_eq!(parse_mode("g-r").unwrap(), 0o626);
205+
fn test_chmod_symbolic_modes() {
206+
assert_eq!(parse_chmod(0o666, "u+x", false, 0).unwrap(), 0o766);
207+
assert_eq!(parse_chmod(0o666, "+x", false, 0).unwrap(), 0o777);
208+
assert_eq!(parse_chmod(0o666, "a-w", false, 0).unwrap(), 0o444);
209+
assert_eq!(parse_chmod(0o666, "g-r", false, 0).unwrap(), 0o626);
221210
}
222211

223212
#[test]
224-
fn test_numeric_modes() {
225-
assert_eq!(parse_mode("644").unwrap(), 0o644);
226-
assert_eq!(parse_mode("+100").unwrap(), 0o766);
227-
assert_eq!(parse_mode("-4").unwrap(), 0o662);
213+
fn test_chmod_numeric_modes() {
214+
assert_eq!(parse_chmod(0o666, "644", false, 0).unwrap(), 0o644);
215+
assert_eq!(parse_chmod(0o666, "+100", false, 0).unwrap(), 0o766);
216+
assert_eq!(parse_chmod(0o666, "-4", false, 0).unwrap(), 0o662);
228217
}
229218

230219
#[test]
@@ -345,19 +334,4 @@ mod tests {
345334
// First add user write, then set to 755 (should override)
346335
assert_eq!(parse("u+w,755", false, 0).unwrap(), 0o755);
347336
}
348-
349-
#[test]
350-
fn test_chmod_symbolic_modes() {
351-
assert_eq!(parse_chmod(0o666, "u+x", false, 0).unwrap(), 0o766);
352-
assert_eq!(parse_chmod(0o666, "+x", false, 0).unwrap(), 0o777);
353-
assert_eq!(parse_chmod(0o666, "a-w", false, 0).unwrap(), 0o444);
354-
assert_eq!(parse_chmod(0o666, "g-r", false, 0).unwrap(), 0o626);
355-
}
356-
357-
#[test]
358-
fn test_chmod_numeric_modes() {
359-
assert_eq!(parse_chmod(0o666, "644", false, 0).unwrap(), 0o644);
360-
assert_eq!(parse_chmod(0o666, "+100", false, 0).unwrap(), 0o766);
361-
assert_eq!(parse_chmod(0o666, "-4", false, 0).unwrap(), 0o662);
362-
}
363337
}

0 commit comments

Comments
 (0)