Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rm: In some cases, remove_dir is doing a better job than remove_dir_all #5403

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (path) eacces
// spell-checker:ignore (path) eacces inacc

use clap::{builder::ValueParser, crate_version, parser::ValueSource, Arg, ArgAction, Command};
use std::collections::VecDeque;
Expand Down Expand Up @@ -330,14 +330,20 @@
if options.recursive && (!is_root || !options.preserve_root) {
if options.interactive != InteractiveMode::Always && !options.verbose {
if let Err(e) = fs::remove_dir_all(path) {
had_err = true;
if e.kind() == std::io::ErrorKind::PermissionDenied {
// GNU compatibility (rm/fail-eacces.sh)
// here, GNU doesn't use some kind of remove_dir_all
// It will show directory+file
show_error!("cannot remove {}: {}", path.quote(), "Permission denied");
} else {
show_error!("cannot remove {}: {}", path.quote(), e);
// GNU compatibility (rm/empty-inacc.sh)
// remove_dir_all failed. maybe it is because of the permissions
// but if the directory is empty, remove_dir might work.
// So, let's try that before failing for real
if let Err(_e) = fs::remove_dir(path) {
had_err = true;

Check warning on line 338 in src/uu/rm/src/rm.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/rm/src/rm.rs#L338

Added line #L338 was not covered by tests
if e.kind() == std::io::ErrorKind::PermissionDenied {
// GNU compatibility (rm/fail-eacces.sh)
// here, GNU doesn't use some kind of remove_dir_all
// It will show directory+file
show_error!("cannot remove {}: {}", path.quote(), "Permission denied");

Check warning on line 343 in src/uu/rm/src/rm.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/rm/src/rm.rs#L343

Added line #L343 was not covered by tests
} else {
show_error!("cannot remove {}: {}", path.quote(), e);

Check warning on line 345 in src/uu/rm/src/rm.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/rm/src/rm.rs#L345

Added line #L345 was not covered by tests
}
}
}
} else {
Expand Down
15 changes: 15 additions & 0 deletions tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,21 @@ fn test_prompt_write_protected_no() {
assert!(at.file_exists(file_2));
}

#[cfg(feature = "chmod")]
#[test]
fn test_remove_inaccessible_dir() {
let scene = TestScenario::new(util_name!());
let at = &scene.fixtures;
let dir_1 = "test_rm_protected";

at.mkdir(dir_1);

scene.ccmd("chmod").arg("0").arg(dir_1).succeeds();

scene.ucmd().arg("-rf").arg(dir_1).succeeds();
assert!(!at.dir_exists(dir_1));
}

#[test]
#[cfg(not(windows))]
fn test_fifo_removal() {
Expand Down
Loading