Skip to content

Commit 11e77c7

Browse files
committed
uucore: mode parsing: support comma-separated mode
Parsing in uucore::mode did not support multiple mode chunks separated by commas, e.g. "ug+rw,o+r"
1 parent 5b261bc commit 11e77c7

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,16 @@ fn parse_change(mode: &str, fperm: u32, considering_dir: bool) -> (u32, usize) {
139139

140140
#[allow(clippy::unnecessary_cast)]
141141
pub fn parse_mode(mode: &str) -> Result<mode_t, String> {
142-
#[cfg(all(
143-
not(target_os = "freebsd"),
144-
not(target_vendor = "apple"),
145-
not(target_os = "android")
146-
))]
147-
let fperm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
148-
#[cfg(any(target_os = "freebsd", target_vendor = "apple", target_os = "android"))]
149-
let fperm = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32;
142+
let mut new_mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32;
150143

151-
let result = if mode.chars().any(|c| c.is_ascii_digit()) {
152-
parse_numeric(fperm as u32, mode, true)
153-
} else {
154-
parse_symbolic(fperm as u32, mode, get_umask(), true)
155-
};
156-
result.map(|mode| mode as mode_t)
144+
for mode_chunk in mode.split(',') {
145+
new_mode = if mode_chunk.chars().any(|c| c.is_ascii_digit()) {
146+
parse_numeric(new_mode, mode_chunk, true)?
147+
} else {
148+
parse_symbolic(new_mode, mode_chunk, get_umask(), true)?
149+
};
150+
}
151+
Ok(new_mode as mode_t)
157152
}
158153

159154
pub fn get_umask() -> u32 {
@@ -202,4 +197,9 @@ mod test {
202197
assert_eq!(super::parse_mode("+100").unwrap(), 0o766);
203198
assert_eq!(super::parse_mode("-4").unwrap(), 0o662);
204199
}
200+
201+
#[test]
202+
fn multiple_modes() {
203+
assert_eq!(super::parse_mode("+100,+010").unwrap(), 0o776);
204+
}
205205
}

0 commit comments

Comments
 (0)