logo

utils-std

Collection of commonly available Unix tools
commit: 46f972a09717e8184b6796370237b5425063e5f4
parent f05ec647c2727dd38eaaa8a7ac039b5fc253b144
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue, 26 Mar 2024 07:51:26 +0100

cmd/rm: When -f is passed, stat errors do not change error status

Diffstat:

Mcmd/rm.12+-
Mcmd/rm.c2++
Mtest-cmd/rm.t9+++++++--
3 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/cmd/rm.1 b/cmd/rm.1 @@ -29,7 +29,7 @@ only fails when the containing directory isn't writable, rather than the file it .Sh OPTIONS .Bl -tag -width Ds .It Fl f -Force: Never prompt before recursing into directories and removing files. +Force: Never prompt before recursing into directories and removing files, non-existing files do not change exit status nor produce diagnostic messages. Overrides .Fl i . .It Fl i diff --git a/cmd/rm.c b/cmd/rm.c @@ -96,6 +96,8 @@ do_unlinkat(int fd, char *name, char *acc_path) if(fstatat(fd, name, &stats, AT_SYMLINK_NOFOLLOW) != 0) { + if(force && errno == ENOENT) return 0; + fprintf(stderr, "rm: Failed getting status for '%s': %s\n", acc_path, strerror(errno)); return 1; } diff --git a/test-cmd/rm.t b/test-cmd/rm.t @@ -25,8 +25,6 @@ Note: Still printing an error message, even if POSIX doesn't requires it $ test -f exists_f $ test ! -f enoent $ rm -f enoent exists_f - rm: Failed getting status for 'enoent': No such file or directory - [1] $ test ! -e exists_f POSIX rm(1p) step 2a: @@ -118,3 +116,10 @@ Verbose $ touch file_v $ rm -v file_v rm: Removed: file_v + + +POSIX, -f shouldn't return an error when non-existant files are passed + $ rm -f /var/empty/e/no/ent + $ rm /var/empty/e/no/ent + rm: Failed getting status for '/var/empty/e/no/ent': No such file or directory + [1]