Skip to content
Open
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
18 changes: 14 additions & 4 deletions src/uu/rm/src/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,11 +515,21 @@ pub fn remove(files: &[&OsStr], options: &Options) -> bool {
}
}

Err(e) if e.kind() == io::ErrorKind::NotFound => {
if options.force {
false
} else {
show_error!(
"{}",
RmError::CannotRemoveNoSuchFile(filename.to_os_string())
);
true
}
}

Err(_e) => {
// TODO: actually print out the specific error
// TODO: When the error is not about missing files
// (e.g., permission), even rm -f should fail with
// outputting the error, but there's no easy way.
// TODO: report the specific error (e.g. "Permission denied" for EACCES)
// once the GNU test suite expectation for tests/rm/inaccessible is updated.
if options.force {
false
} else {
Expand Down
27 changes: 26 additions & 1 deletion tests/by-util/test_rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
// spell-checker:ignore rootlink
// spell-checker:ignore rootlink ENOTDIR
#![allow(clippy::stable_sort_primitive)]

use std::process::Stdio;
Expand Down Expand Up @@ -1377,3 +1377,28 @@ fn test_preserve_root_literal_root() {
.stderr_contains("it is dangerous to operate recursively on '/'")
.stderr_contains("use --no-preserve-root to override this failsafe");
}

/// Test that `rm -f` silently ignores paths that cannot be stat'd because a
/// component of the path is not a directory (ENOTDIR), matching GNU behavior.
#[cfg(unix)]
#[test]
fn test_rm_force_ignores_symlink_metadata_error() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("existing_file");
// "existing_file/subpath" triggers ENOTDIR; -f must suppress it silently.
ucmd.args(&["-f", "existing_file/subpath"])
.succeeds()
.no_stderr();
}

/// Test that without `-f`, a path that cannot be stat'd due to ENOTDIR still
/// causes a non-zero exit and reports an error.
#[cfg(unix)]
#[test]
fn test_rm_reports_error_for_symlink_metadata_failure() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("existing_file");
ucmd.args(&["existing_file/subpath"])
.fails()
.stderr_contains("existing_file/subpath");
}
Loading