1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #5336 from sylvestre/isdigit

Replace list of digit by is_digit
This commit is contained in:
Daniel Hofstetter 2023-09-29 10:32:07 +02:00 committed by GitHub
commit 1a87c31b1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 35 deletions

View file

@ -335,9 +335,7 @@ impl Chmoder {
let mut new_mode = fperm; let mut new_mode = fperm;
let mut naively_expected_new_mode = new_mode; let mut naively_expected_new_mode = new_mode;
for mode in cmode_unwrapped.split(',') { for mode in cmode_unwrapped.split(',') {
// cmode is guaranteed to be Some in this case let result = if mode.chars().any(|c| c.is_ascii_digit()) {
let arr: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
let result = if mode.contains(arr) {
mode::parse_numeric(new_mode, mode, file.is_dir()).map(|v| (v, v)) mode::parse_numeric(new_mode, mode, file.is_dir()).map(|v| (v, v))
} else { } else {
mode::parse_symbolic(new_mode, mode, get_umask(), file.is_dir()).map(|m| { mode::parse_symbolic(new_mode, mode, get_umask(), file.is_dir()).map(|m| {
@ -352,20 +350,22 @@ impl Chmoder {
(m, naive_mode) (m, naive_mode)
}) })
}; };
match result { match result {
Ok((mode, naive_mode)) => { Ok((mode, naive_mode)) => {
new_mode = mode; new_mode = mode;
naively_expected_new_mode = naive_mode; naively_expected_new_mode = naive_mode;
} }
Err(f) => { Err(f) => {
if self.quiet { return if self.quiet {
return Err(ExitCode::new(1)); Err(ExitCode::new(1))
} else { } else {
return Err(USimpleError::new(1, f)); Err(USimpleError::new(1, f))
} };
} }
} }
} }
self.change_file(fperm, new_mode, file)?; 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 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 { if (new_mode & !naively_expected_new_mode) != 0 {

View file

@ -9,10 +9,7 @@ use uucore::mode;
/// Takes a user-supplied string and tries to parse to u16 mode bitmask. /// 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> { 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']; if mode_string.chars().any(|c| c.is_ascii_digit()) {
// Passing 000 as the existing permissions seems to mirror GNU behavior.
if mode_string.contains(numbers) {
mode::parse_numeric(0, mode_string, considering_dir) mode::parse_numeric(0, mode_string, considering_dir)
} else { } else {
mode::parse_symbolic(0, mode_string, umask, considering_dir) mode::parse_symbolic(0, mode_string, umask, considering_dir)

View file

@ -38,31 +38,27 @@ fn get_mode(_matches: &ArgMatches, _mode_had_minus_prefix: bool) -> Result<u32,
#[cfg(not(windows))] #[cfg(not(windows))]
fn get_mode(matches: &ArgMatches, mode_had_minus_prefix: bool) -> Result<u32, String> { 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 // Not tested on Windows
let mut new_mode = DEFAULT_PERM; let mut new_mode = DEFAULT_PERM;
match matches.get_one::<String>(options::MODE) {
Some(m) => { if let Some(m) = matches.get_one::<String>(options::MODE) {
for mode in m.split(',') { for mode in m.split(',') {
if mode.contains(digits) { if mode.chars().any(|c| c.is_ascii_digit()) {
new_mode = mode::parse_numeric(new_mode, m, true)?; 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 { } else {
let cmode = if mode_had_minus_prefix { mode.to_string()
// clap parsing is finished, now put prefix back };
format!("-{mode}") new_mode = mode::parse_symbolic(new_mode, &cmode, mode::get_umask(), true)?;
} else {
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)
} }
} }

View file

@ -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 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> { 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.chars().any(|c| c.is_ascii_digit()) {
let result = if mode.contains(arr) {
mode::parse_numeric(MODE_RW_UGO as u32, mode) mode::parse_numeric(MODE_RW_UGO as u32, mode)
} else { } else {
mode::parse_symbolic(MODE_RW_UGO as u32, mode, true) mode::parse_symbolic(MODE_RW_UGO as u32, mode, true)

View file

@ -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"))] #[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 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.chars().any(|c| c.is_ascii_digit()) {
let result = if mode.contains(arr) {
parse_numeric(fperm, mode, true) parse_numeric(fperm, mode, true)
} else { } else {
parse_symbolic(fperm, mode, get_umask(), true) parse_symbolic(fperm, mode, get_umask(), true)