Hi,

I wanted to rm -rP some files on my disk and didn't notice that
they lacked write permission for the user who executed rm(1)
command.

$ echo foo > file-mode-444.txt
$ chmod 0444 file-mode-444.txt
$ ls -ln file-mode-444.txt
-r--r--r--  1 5001  5001  4 Sep  3 09:36 file-mode-444.txt

$ rm -vfP file-mode-444.txt
rm: file-mode-444.txt: Permission denied
file-mode-444.txt
$ echo $?
1

$ ls -l file-mode-444.txt
ls: file-mode-444.txt: No such file or directory

I was not expecting this behaviour. My expectation was the file would
NOT be removed. Hence the diff below:


Index: rm.c
===================================================================
RCS file: /cvs/src/bin/rm/rm.c,v
retrieving revision 1.44
diff -u -p -u -r1.44 rm.c
--- rm.c        16 Aug 2022 13:52:41 -0000      1.44
+++ rm.c        3 Sep 2022 09:37:44 -0000
@@ -215,8 +215,11 @@ rm_tree(char **argv)
                case FTS_F:
                case FTS_NSOK:
                        if (Pflag)
-                               rm_overwrite(p->fts_accpath, p->fts_info ==
-                                   FTS_NSOK ? NULL : p->fts_statp);
+                               if (!rm_overwrite(p->fts_accpath, p->fts_info ==
+                                   FTS_NSOK ? NULL : p->fts_statp)) {
+                                       eval = 1;
+                                       continue;
+                               }
                        /* FALLTHROUGH */
                default:
                        if (!unlink(p->fts_accpath)) {
@@ -267,7 +270,10 @@ rm_file(char **argv)
                        rval = rmdir(f);
                else {
                        if (Pflag)
-                               rm_overwrite(f, &sb);
+                               if (!rm_overwrite(f, &sb)) {
+                                       eval = 1;
+                                       continue;
+                               }
                        rval = unlink(f);
                }
                if (rval && (!fflag || errno != ENOENT)) {


What do you guys think?


$ ./obj/rm -vfP file-mode-444.txt
rm: file-mode-444.txt: Permission denied
$ echo $?
1

$ ls -ln file-mode-444.txt
-r--r--r--  1 5001  5001  4 Sep  3 09:36 file-mode-444.txt

I did use `rm -fP` in the invocation, and reading the rm(1) manual page:

   -f      Attempt to remove the files without prompting for confirmation,
           regardless of the file's permissions.  If the file does not
           exist, do not display a diagnostic message or modify the exit
           status to reflect an error.  The -f option overrides any previous
           -i options.

but not sure then what exactly should happen when -P and -f and no write
permission.


-- 
Regards,
 Mikolaj

Reply via email to