Skip to content

Commit

Permalink
Replace list of digit by is_ascii_digit
Browse files Browse the repository at this point in the history
and some ride along simplification
  • Loading branch information
sylvestre committed Sep 28, 2023
1 parent f0f64bd commit 55fd1f3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 deletions.
14 changes: 7 additions & 7 deletions src/uu/chmod/src/chmod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,7 @@ impl Chmoder {
let mut new_mode = fperm;
let mut naively_expected_new_mode = new_mode;
for mode in cmode_unwrapped.split(',') {
// cmode is guaranteed to be Some in this case
let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
let result = if mode.contains(arr) {
let result = if mode.chars().any(|c| c.is_ascii_digit()) {
mode::parse_numeric(new_mode, mode, file.is_dir()).map(|v| (v, v))
} else {
mode::parse_symbolic(new_mode, mode, get_umask(), file.is_dir()).map(|m| {
Expand All @@ -352,20 +350,22 @@ impl Chmoder {
(m, naive_mode)
})
};

match result {
Ok((mode, naive_mode)) => {
new_mode = mode;
naively_expected_new_mode = naive_mode;
}
Err(f) => {
if self.quiet {
return Err(ExitCode::new(1));
return if self.quiet {
Err(ExitCode::new(1))

Check warning on line 361 in src/uu/chmod/src/chmod.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/chmod/src/chmod.rs#L361

Added line #L361 was not covered by tests
} else {
return Err(USimpleError::new(1, f));
}
Err(USimpleError::new(1, f))
};
}
}
}

self.change_file(fperm, new_mode, file)?;
// if a permission would have been removed if umask was 0, but it wasn't because umask was not 0, print an error and fail
if (new_mode & !naively_expected_new_mode) != 0 {
Expand Down
5 changes: 1 addition & 4 deletions src/uu/install/src/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use uucore::mode;

/// Takes a user-supplied string and tries to parse to u16 mode bitmask.
pub fn parse(mode_string: &str, considering_dir: bool, umask: u32) -> Result<u32, String> {
let numbers: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

// Passing 000 as the existing permissions seems to mirror GNU behavior.
if mode_string.contains(numbers) {
if mode_string.chars().any(|c| c.is_ascii_digit()) {
mode::parse_numeric(0, mode_string, considering_dir)
} else {
mode::parse_symbolic(0, mode_string, umask, considering_dir)
Expand Down
36 changes: 16 additions & 20 deletions src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,27 @@ fn get_mode(_matches: &ArgMatches, _mode_had_minus_prefix: bool) -> Result<u32,

#[cfg(not(windows))]
fn get_mode(matches: &ArgMatches, mode_had_minus_prefix: bool) -> Result<u32, String> {
let digits: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
// Translate a ~str in octal form to u16, default to 777
// Not tested on Windows
let mut new_mode = DEFAULT_PERM;
match matches.get_one::<String>(options::MODE) {
Some(m) => {
for mode in m.split(',') {
if mode.contains(digits) {
new_mode = mode::parse_numeric(new_mode, m, true)?;

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 {
let cmode = if mode_had_minus_prefix {
// clap parsing is finished, now put prefix back
format!("-{mode}")
} else {
let cmode = if mode_had_minus_prefix {
// clap parsing is finished, now put prefix back
format!("-{mode}")
} else {
mode.to_string()
};
new_mode = mode::parse_symbolic(new_mode, &cmode, mode::get_umask(), true)?;
}
mode.to_string()
};
new_mode = mode::parse_symbolic(new_mode, &cmode, mode::get_umask(), true)?;
}
Ok(new_mode)
}
None => {
// If no mode argument is specified return the mode derived from umask
Ok(!mode::get_umask() & 0o0777)
}
Ok(new_mode)
} else {
// If no mode argument is specified return the mode derived from umask
Ok(!mode::get_umask() & 0o0777)
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/uu/mknod/src/parsemode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ use uucore::mode;
pub const MODE_RW_UGO: mode_t = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

pub fn parse_mode(mode: &str) -> Result<mode_t, String> {
let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
let result = if mode.contains(arr) {
let result = if mode.chars().any(|c| c.is_ascii_digit()) {
mode::parse_numeric(MODE_RW_UGO as u32, mode)
} else {
mode::parse_symbolic(MODE_RW_UGO as u32, mode, true)
Expand Down
3 changes: 1 addition & 2 deletions src/uucore/src/lib/features/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,7 @@ pub fn parse_mode(mode: &str) -> Result<mode_t, String> {
#[cfg(any(target_os = "freebsd", target_vendor = "apple", target_os = "android"))]
let fperm = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32;

let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
let result = if mode.contains(arr) {
let result = if mode.chars().any(|c| c.is_ascii_digit()) {
parse_numeric(fperm, mode, true)
} else {
parse_symbolic(fperm, mode, get_umask(), true)
Expand Down

0 comments on commit 55fd1f3

Please sign in to comment.