rm.c revision 1.22 1 /* $NetBSD: rm.c,v 1.22 1998/07/28 04:01:03 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)rm.c 8.8 (Berkeley) 4/27/95";
45 #else
46 __RCSID("$NetBSD: rm.c,v 1.22 1998/07/28 04:01:03 mycroft Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/types.h>
51 #include <sys/stat.h>
52
53 #include <locale.h>
54 #include <err.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <fts.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <pwd.h>
63 #include <grp.h>
64
65 int dflag, eval, fflag, iflag, Pflag, Wflag, stdin_ok;
66
67 int check __P((char *, char *, struct stat *));
68 void checkdot __P((char **));
69 void rm_file __P((char **));
70 void rm_overwrite __P((char *, struct stat *));
71 void rm_tree __P((char **));
72 void usage __P((void));
73 int main __P((int, char *[]));
74
75 /*
76 * For the sake of the `-f' flag, check whether an error number indicates the
77 * failure of an operation due to an non-existent file, either per se (ENOENT)
78 * or because its filename argument was illegal (ENAMETOOLONG, ENOTDIR).
79 */
80 #define NONEXISTENT(x) \
81 ((x) == ENOENT || (x) == ENAMETOOLONG || (x) == ENOTDIR)
82
83 /*
84 * rm --
85 * This rm is different from historic rm's, but is expected to match
86 * POSIX 1003.2 behavior. The most visible difference is that -f
87 * has two specific effects now, ignore non-existent files and force
88 * file removal.
89 */
90 int
91 main(argc, argv)
92 int argc;
93 char *argv[];
94 {
95 int ch, rflag;
96
97 (void)setlocale(LC_ALL, "");
98
99 Pflag = rflag = 0;
100 while ((ch = getopt(argc, argv, "dfiPRrW")) != -1)
101 switch(ch) {
102 case 'd':
103 dflag = 1;
104 break;
105 case 'f':
106 fflag = 1;
107 iflag = 0;
108 break;
109 case 'i':
110 fflag = 0;
111 iflag = 1;
112 break;
113 case 'P':
114 Pflag = 1;
115 break;
116 case 'R':
117 case 'r': /* Compatibility. */
118 rflag = 1;
119 break;
120 case 'W':
121 Wflag = 1;
122 break;
123 case '?':
124 default:
125 usage();
126 }
127 argc -= optind;
128 argv += optind;
129
130 if (argc < 1)
131 usage();
132
133 checkdot(argv);
134
135 if (*argv) {
136 stdin_ok = isatty(STDIN_FILENO);
137
138 if (rflag)
139 rm_tree(argv);
140 else
141 rm_file(argv);
142 }
143
144 exit (eval);
145 }
146
147 void
148 rm_tree(argv)
149 char **argv;
150 {
151 FTS *fts;
152 FTSENT *p;
153 int needstat;
154 int flags;
155
156 /*
157 * Remove a file hierarchy. If forcing removal (-f), or interactive
158 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
159 */
160 needstat = !fflag && !iflag && stdin_ok;
161
162 /*
163 * If the -i option is specified, the user can skip on the pre-order
164 * visit. The fts_number field flags skipped directories.
165 */
166 #define SKIPPED 1
167
168 flags = FTS_PHYSICAL;
169 if (!needstat)
170 flags |= FTS_NOSTAT;
171 if (Wflag)
172 flags |= FTS_WHITEOUT;
173 if (!(fts = fts_open(argv, flags,
174 (int (*) __P((const FTSENT **, const FTSENT **)))NULL)))
175 err(1, "%s", "");
176 while ((p = fts_read(fts)) != NULL) {
177 switch (p->fts_info) {
178 case FTS_DNR:
179 if (!fflag || p->fts_errno != ENOENT) {
180 warnx("%s: %s",
181 p->fts_path, strerror(p->fts_errno));
182 eval = 1;
183 }
184 continue;
185 case FTS_ERR:
186 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
187 case FTS_NS:
188 /*
189 * FTS_NS: assume that if can't stat the file, it
190 * can't be unlinked.
191 */
192 if (!needstat)
193 break;
194 if (!fflag || !NONEXISTENT(p->fts_errno)) {
195 warnx("%s: %s",
196 p->fts_path, strerror(p->fts_errno));
197 eval = 1;
198 }
199 continue;
200 case FTS_D:
201 /* Pre-order: give user chance to skip. */
202 if (!fflag && !check(p->fts_path, p->fts_accpath,
203 p->fts_statp)) {
204 (void)fts_set(fts, p, FTS_SKIP);
205 p->fts_number = SKIPPED;
206 }
207 continue;
208 case FTS_DP:
209 /* Post-order: see if user skipped. */
210 if (p->fts_number == SKIPPED)
211 continue;
212 break;
213 default:
214 if (!fflag &&
215 !check(p->fts_path, p->fts_accpath, p->fts_statp))
216 continue;
217 }
218
219 /*
220 * If we can't read or search the directory, may still be
221 * able to remove it. Don't print out the un{read,search}able
222 * message unless the remove fails.
223 */
224 switch (p->fts_info) {
225 case FTS_DP:
226 case FTS_DNR:
227 if (!rmdir(p->fts_accpath) ||
228 (fflag && errno == ENOENT))
229 continue;
230 break;
231
232 case FTS_W:
233 if (!undelete(p->fts_accpath) ||
234 (fflag && errno == ENOENT))
235 continue;
236 break;
237
238 default:
239 if (Pflag)
240 rm_overwrite(p->fts_accpath, NULL);
241 if (!unlink(p->fts_accpath) ||
242 (fflag && NONEXISTENT(errno)))
243 continue;
244 }
245 warn("%s", p->fts_path);
246 eval = 1;
247 }
248 if (errno)
249 err(1, "fts_read");
250 }
251
252 void
253 rm_file(argv)
254 char **argv;
255 {
256 struct stat sb;
257 int rval;
258 char *f;
259
260 /*
261 * Remove a file. POSIX 1003.2 states that, by default, attempting
262 * to remove a directory is an error, so must always stat the file.
263 */
264 while ((f = *argv++) != NULL) {
265 /* Assume if can't stat the file, can't unlink it. */
266 if (lstat(f, &sb)) {
267 if (Wflag) {
268 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
269 } else {
270 if (!fflag || !NONEXISTENT(errno)) {
271 warn("%s", f);
272 eval = 1;
273 }
274 continue;
275 }
276 } else if (Wflag) {
277 warnx("%s: %s", f, strerror(EEXIST));
278 eval = 1;
279 continue;
280 }
281
282 if (S_ISDIR(sb.st_mode) && !dflag) {
283 warnx("%s: is a directory", f);
284 eval = 1;
285 continue;
286 }
287 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
288 continue;
289 if (S_ISWHT(sb.st_mode))
290 rval = undelete(f);
291 else if (S_ISDIR(sb.st_mode))
292 rval = rmdir(f);
293 else {
294 if (Pflag)
295 rm_overwrite(f, &sb);
296 rval = unlink(f);
297 }
298 if (rval && (!fflag || !NONEXISTENT(errno))) {
299 warn("%s", f);
300 eval = 1;
301 }
302 }
303 }
304
305 /*
306 * rm_overwrite --
307 * Overwrite the file 3 times with varying bit patterns.
308 *
309 * XXX
310 * This is a cheap way to *really* delete files. Note that only regular
311 * files are deleted, directories (and therefore names) will remain.
312 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
313 * System V file system). In a logging file system, you'll have to have
314 * kernel support.
315 */
316 void
317 rm_overwrite(file, sbp)
318 char *file;
319 struct stat *sbp;
320 {
321 struct stat sb;
322 off_t len;
323 int fd, wlen;
324 char buf[8 * 1024];
325
326 fd = -1;
327 if (sbp == NULL) {
328 if (lstat(file, &sb))
329 goto err;
330 sbp = &sb;
331 }
332 if (!S_ISREG(sbp->st_mode))
333 return;
334 if ((fd = open(file, O_WRONLY, 0)) == -1)
335 goto err;
336
337 #define PASS(byte) { \
338 memset(buf, byte, sizeof(buf)); \
339 for (len = sbp->st_size; len > 0; len -= wlen) { \
340 wlen = len < sizeof(buf) ? len : sizeof(buf); \
341 if (write(fd, buf, wlen) != wlen) \
342 goto err; \
343 } \
344 }
345 PASS(0xff);
346 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
347 goto err;
348 PASS(0x00);
349 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
350 goto err;
351 PASS(0xff);
352 if (!fsync(fd) && !close(fd))
353 return;
354
355 err: eval = 1;
356 warn("%s", file);
357 }
358
359
360 int
361 check(path, name, sp)
362 char *path, *name;
363 struct stat *sp;
364 {
365 int ch, first;
366 char modep[15];
367
368 /* Check -i first. */
369 if (iflag)
370 (void)fprintf(stderr, "remove %s? ", path);
371 else {
372 /*
373 * If it's not a symbolic link and it's unwritable and we're
374 * talking to a terminal, ask. Symbolic links are excluded
375 * because their permissions are meaningless. Check stdin_ok
376 * first because we may not have stat'ed the file.
377 */
378 if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK))
379 return (1);
380 strmode(sp->st_mode, modep);
381 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
382 modep + 1, modep[9] == ' ' ? "" : " ",
383 user_from_uid(sp->st_uid, 0),
384 group_from_gid(sp->st_gid, 0), path);
385 }
386 (void)fflush(stderr);
387
388 first = ch = getchar();
389 while (ch != '\n' && ch != EOF)
390 ch = getchar();
391 return (first == 'y' || first == 'Y');
392 }
393
394 /*
395 * POSIX.2 requires that if "." or ".." are specified as the basename
396 * portion of an operand, a diagnostic message be written to standard
397 * error and nothing more be done with such operands.
398 *
399 * Since POSIX.2 defines basename as the final portion of a path after
400 * trailing slashes have been removed, we'll remove them here.
401 */
402 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
403 void
404 checkdot(argv)
405 char **argv;
406 {
407 char *p, **save, **t;
408 int complained;
409
410 complained = 0;
411 for (t = argv; *t;) {
412 /* strip trailing slashes */
413 p = strrchr (*t, '\0');
414 while (--p > *t && *p == '/')
415 *p = '\0';
416
417 /* extract basename */
418 if ((p = strrchr(*t, '/')) != NULL)
419 ++p;
420 else
421 p = *t;
422
423 if (ISDOT(p)) {
424 if (!complained++)
425 warnx("\".\" and \"..\" may not be removed");
426 eval = 1;
427 for (save = t; (t[0] = t[1]) != NULL; ++t)
428 continue;
429 t = save;
430 } else
431 ++t;
432 }
433 }
434
435 void
436 usage()
437 {
438
439 (void)fprintf(stderr, "usage: rm [-dfiPRrW] file ...\n");
440 exit(1);
441 }
442