• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* rm.c - remove files
2  *
3  * Copyright 2012 Rob Landley <rob@landley.net>
4  *
5  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/rm.html
6 
7 USE_RM(NEWTOY(rm, "fiRrv[-fi]", TOYFLAG_BIN))
8 
9 config RM
10   bool "rm"
11   default y
12   help
13     usage: rm [-fiRrv] FILE...
14 
15     Remove each argument from the filesystem.
16 
17     -f	Force: remove without confirmation, no error if it doesn't exist
18     -i	Interactive: prompt for confirmation
19     -rR	Recursive: remove directory contents
20     -v	Verbose
21 */
22 
23 #define FOR_rm
24 #include "toys.h"
25 
do_rm(struct dirtree * try)26 static int do_rm(struct dirtree *try)
27 {
28   int fd = dirtree_parentfd(try), flags = toys.optflags;
29   int dir = S_ISDIR(try->st.st_mode), or = 0, using = 0;
30 
31   // Skip . and .. (yes, even explicitly on the command line: posix says to)
32   if (isdotdot(try->name)) return 0;
33 
34   // Intentionally fail non-recursive attempts to remove even an empty dir
35   // (via wrong flags to unlinkat) because POSIX says to.
36   if (dir && !(flags & (FLAG_r|FLAG_R))) goto skip;
37 
38   // This is either the posix section 2(b) prompt or the section 3 prompt.
39   if (!(flags & FLAG_f)
40     && (!S_ISLNK(try->st.st_mode) && faccessat(fd, try->name, W_OK, 0))) or++;
41   if (!(dir && try->again) && ((or && isatty(0)) || (flags & FLAG_i))) {
42     char *s = dirtree_path(try, 0);
43 
44     fprintf(stderr, "rm %s%s%s", or ? "ro " : "", dir ? "dir " : "", s);
45     free(s);
46     or = yesno(0);
47     if (!or) goto nodelete;
48   }
49 
50   // handle directory recursion
51   if (dir) {
52     using = AT_REMOVEDIR;
53     // Handle chmod 000 directories when -f
54     if (faccessat(fd, try->name, R_OK, 0)) {
55       if (toys.optflags & FLAG_f) wfchmodat(fd, try->name, 0700);
56       else goto skip;
57     }
58     if (!try->again) return DIRTREE_COMEAGAIN;
59     if (try->symlink) goto skip;
60     if (flags & FLAG_i) {
61       char *s = dirtree_path(try, 0);
62 
63       // This is the section 2(d) prompt. (Yes, posix says to prompt twice.)
64       fprintf(stderr, "rmdir %s", s);
65       free(s);
66       or = yesno(0);
67       if (!or) goto nodelete;
68     }
69   }
70 
71 skip:
72   if (!unlinkat(fd, try->name, using)) {
73     if (flags & FLAG_v) {
74       char *s = dirtree_path(try, 0);
75       printf("%s%s '%s'\n", toys.which->name, dir ? "dir" : "", s);
76       free(s);
77     }
78   } else {
79     if (!dir || try->symlink != (char *)2) perror_msg_raw(try->name);
80 nodelete:
81     if (try->parent) try->parent->symlink = (char *)2;
82   }
83 
84   return 0;
85 }
86 
rm_main(void)87 void rm_main(void)
88 {
89   char **s;
90 
91   // Can't use <1 in optstring because zero arguments with -f isn't an error
92   if (!toys.optc && !(toys.optflags & FLAG_f)) error_exit("Needs 1 argument");
93 
94   for (s = toys.optargs; *s; s++) {
95     if (!strcmp(*s, "/")) {
96       error_msg("rm /. if you mean it");
97       continue;
98     }
99 
100     // Files that already don't exist aren't errors for -f, so try a quick
101     // unlink now to see if it succeeds or reports that it didn't exist.
102     if ((toys.optflags & FLAG_f) && (!unlink(*s) || errno == ENOENT))
103       continue;
104 
105     // There's a race here where a file removed between the above check and
106     // dirtree's stat would report the nonexistence as an error, but that's
107     // not a normal "it didn't exist" so I'm ok with it.
108 
109     dirtree_read(*s, do_rm);
110   }
111 }
112