From fddc8c9d9d6896ceb91932cdb273013aa07b05d8 Mon Sep 17 00:00:00 2001 From: Pat Laster Date: Thu, 13 Oct 2022 17:22:59 -0500 Subject: [PATCH] More readable unix write permissions for directory using libc --- src/uu/rm/Cargo.toml | 3 +++ src/uu/rm/src/rm.rs | 8 +++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/uu/rm/Cargo.toml b/src/uu/rm/Cargo.toml index e992d86d1..3196c71ce 100644 --- a/src/uu/rm/Cargo.toml +++ b/src/uu/rm/Cargo.toml @@ -20,6 +20,9 @@ walkdir = "2.2" remove_dir_all = "0.7.0" uucore = { version=">=0.0.16", package="uucore", path="../../uucore", features=["fs"] } +[target.'cfg(unix)'.dependencies] +libc = "0.2.135" + [target.'cfg(windows)'.dependencies] winapi = { version="0.3", features=[] } diff --git a/src/uu/rm/src/rm.rs b/src/uu/rm/src/rm.rs index ead7a63b6..fa4d8cc35 100644 --- a/src/uu/rm/src/rm.rs +++ b/src/uu/rm/src/rm.rs @@ -557,8 +557,8 @@ fn prompt_file(path: &Path, options: &Options, is_dir: bool) -> bool { fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata) -> bool { use std::os::unix::fs::PermissionsExt; let mode = metadata.permissions().mode(); - let user_write_permission = (mode & 0b1_1100_0000) >> 6; - let user_writable = !matches!(user_write_permission, 0o0 | 0o1 | 0o4 | 0o5); + // Check if directory has user write permissions + let user_writable = (mode & libc::S_IWUSR) != 0; if !user_writable { prompt(&(format!("remove write-protected directory {}? ", path.quote()))) } else if options.interactive == InteractiveMode::Always { @@ -587,9 +587,7 @@ fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata #[cfg(not(windows))] #[cfg(not(unix))] fn handle_writable_directory(path: &Path, options: &Options, metadata: &Metadata) -> bool { - if metadata.permissions().readonly() { - prompt(&(format!("remove write-protected directory {}? ", path.quote()))) - } else if options.interactive == InteractiveMode::Always { + if options.interactive == InteractiveMode::Always { prompt(&(format!("remove directory {}? ", path.quote()))) } else { true