rm.c revision 1.26.4.1 1 /* $NetBSD: rm.c,v 1.26.4.1 2002/03/06 22:28:00 he 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.26.4.1 2002/03/06 22:28:00 he 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 /* NOTREACHED */
146 }
147
148 void
149 rm_tree(argv)
150 char **argv;
151 {
152 FTS *fts;
153 FTSENT *p;
154 int needstat;
155 int flags;
156
157 /*
158 * Remove a file hierarchy. If forcing removal (-f), or interactive
159 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
160 */
161 needstat = !fflag && !iflag && stdin_ok;
162
163 /*
164 * If the -i option is specified, the user can skip on the pre-order
165 * visit. The fts_number field flags skipped directories.
166 */
167 #define SKIPPED 1
168
169 flags = FTS_PHYSICAL;
170 if (!needstat)
171 flags |= FTS_NOSTAT;
172 if (Wflag)
173 flags |= FTS_WHITEOUT;
174 if (!(fts = fts_open(argv, flags,
175 (int (*) __P((const FTSENT **, const FTSENT **)))NULL)))
176 err(1, NULL);
177 while ((p = fts_read(fts)) != NULL) {
178 switch (p->fts_info) {
179 case FTS_DNR:
180 if (!fflag || p->fts_errno != ENOENT) {
181 warnx("%s: %s",
182 p->fts_path, strerror(p->fts_errno));
183 eval = 1;
184 }
185 continue;
186 case FTS_ERR:
187 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
188 /* NOTREACHED */
189 case FTS_NS:
190 /*
191 * FTS_NS: assume that if can't stat the file, it
192 * can't be unlinked.
193 */
194 if (fflag && NONEXISTENT(p->fts_errno))
195 continue;
196 if (needstat) {
197 warnx("%s: %s",
198 p->fts_path, strerror(p->fts_errno));
199 eval = 1;
200 continue;
201 }
202 break;
203 case FTS_D:
204 /* Pre-order: give user chance to skip. */
205 if (!fflag && !check(p->fts_path, p->fts_accpath,
206 p->fts_statp)) {
207 (void)fts_set(fts, p, FTS_SKIP);
208 p->fts_number = SKIPPED;
209 }
210 continue;
211 case FTS_DP:
212 /* Post-order: see if user skipped. */
213 if (p->fts_number == SKIPPED)
214 continue;
215 break;
216 default:
217 if (!fflag &&
218 !check(p->fts_path, p->fts_accpath, p->fts_statp))
219 continue;
220 }
221
222 /*
223 * If we can't read or search the directory, may still be
224 * able to remove it. Don't print out the un{read,search}able
225 * message unless the remove fails.
226 */
227 switch (p->fts_info) {
228 case FTS_DP:
229 case FTS_DNR:
230 if (!rmdir(p->fts_accpath) ||
231 (fflag && errno == ENOENT))
232 continue;
233 break;
234
235 case FTS_W:
236 if (!undelete(p->fts_accpath) ||
237 (fflag && errno == ENOENT))
238 continue;
239 break;
240
241 default:
242 if (Pflag)
243 rm_overwrite(p->fts_accpath, NULL);
244 if (!unlink(p->fts_accpath) ||
245 (fflag && NONEXISTENT(errno)))
246 continue;
247 }
248 warn("%s", p->fts_path);
249 eval = 1;
250 }
251 if (errno)
252 err(1, "fts_read");
253 }
254
255 void
256 rm_file(argv)
257 char **argv;
258 {
259 struct stat sb;
260 int rval;
261 char *f;
262
263 /*
264 * Remove a file. POSIX 1003.2 states that, by default, attempting
265 * to remove a directory is an error, so must always stat the file.
266 */
267 while ((f = *argv++) != NULL) {
268 /* Assume if can't stat the file, can't unlink it. */
269 if (lstat(f, &sb)) {
270 if (Wflag) {
271 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
272 } else {
273 if (!fflag || !NONEXISTENT(errno)) {
274 warn("%s", f);
275 eval = 1;
276 }
277 continue;
278 }
279 } else if (Wflag) {
280 warnx("%s: %s", f, strerror(EEXIST));
281 eval = 1;
282 continue;
283 }
284
285 if (S_ISDIR(sb.st_mode) && !dflag) {
286 warnx("%s: is a directory", f);
287 eval = 1;
288 continue;
289 }
290 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
291 continue;
292 if (S_ISWHT(sb.st_mode))
293 rval = undelete(f);
294 else if (S_ISDIR(sb.st_mode))
295 rval = rmdir(f);
296 else {
297 if (Pflag)
298 rm_overwrite(f, &sb);
299 rval = unlink(f);
300 }
301 if (rval && (!fflag || !NONEXISTENT(errno))) {
302 warn("%s", f);
303 eval = 1;
304 }
305 }
306 }
307
308 /*
309 * rm_overwrite --
310 * Overwrite the file 3 times with varying bit patterns.
311 *
312 * XXX
313 * This is a cheap way to *really* delete files. Note that only regular
314 * files are deleted, directories (and therefore names) will remain.
315 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
316 * System V file system). In a logging file system, you'll have to have
317 * kernel support.
318 */
319 void
320 rm_overwrite(file, sbp)
321 char *file;
322 struct stat *sbp;
323 {
324 struct stat sb;
325 off_t len;
326 int fd, wlen;
327 char buf[8 * 1024];
328
329 fd = -1;
330 if (sbp == NULL) {
331 if (lstat(file, &sb))
332 goto err;
333 sbp = &sb;
334 }
335 if (!S_ISREG(sbp->st_mode))
336 return;
337 if ((fd = open(file, O_WRONLY, 0)) == -1)
338 goto err;
339
340 #define PASS(byte) { \
341 memset(buf, byte, sizeof(buf)); \
342 for (len = sbp->st_size; len > 0; len -= wlen) { \
343 wlen = len < sizeof(buf) ? len : sizeof(buf); \
344 if (write(fd, buf, wlen) != wlen) \
345 goto err; \
346 } \
347 }
348 PASS(0xff);
349 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
350 goto err;
351 PASS(0x00);
352 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
353 goto err;
354 PASS(0xff);
355 if (!fsync(fd) && !close(fd))
356 return;
357
358 err: eval = 1;
359 warn("%s", file);
360 }
361
362
363 int
364 check(path, name, sp)
365 char *path, *name;
366 struct stat *sp;
367 {
368 int ch, first;
369 char modep[15];
370
371 /* Check -i first. */
372 if (iflag)
373 (void)fprintf(stderr, "remove %s? ", path);
374 else {
375 /*
376 * If it's not a symbolic link and it's unwritable and we're
377 * talking to a terminal, ask. Symbolic links are excluded
378 * because their permissions are meaningless. Check stdin_ok
379 * first because we may not have stat'ed the file.
380 */
381 if (!stdin_ok || S_ISLNK(sp->st_mode) ||
382 !(access(name, W_OK) && (errno != ETXTBSY)))
383 return (1);
384 strmode(sp->st_mode, modep);
385 (void)fprintf(stderr, "override %s%s%s/%s for %s? ",
386 modep + 1, modep[9] == ' ' ? "" : " ",
387 user_from_uid(sp->st_uid, 0),
388 group_from_gid(sp->st_gid, 0), path);
389 }
390 (void)fflush(stderr);
391
392 first = ch = getchar();
393 while (ch != '\n' && ch != EOF)
394 ch = getchar();
395 return (first == 'y' || first == 'Y');
396 }
397
398 /*
399 * POSIX.2 requires that if "." or ".." are specified as the basename
400 * portion of an operand, a diagnostic message be written to standard
401 * error and nothing more be done with such operands.
402 *
403 * Since POSIX.2 defines basename as the final portion of a path after
404 * trailing slashes have been removed, we'll remove them here.
405 */
406 #define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
407 void
408 checkdot(argv)
409 char **argv;
410 {
411 char *p, **save, **t;
412 int complained;
413
414 complained = 0;
415 for (t = argv; *t;) {
416 /* strip trailing slashes */
417 p = strrchr (*t, '\0');
418 while (--p > *t && *p == '/')
419 *p = '\0';
420
421 /* extract basename */
422 if ((p = strrchr(*t, '/')) != NULL)
423 ++p;
424 else
425 p = *t;
426
427 if (ISDOT(p)) {
428 if (!complained++)
429 warnx("\".\" and \"..\" may not be removed");
430 eval = 1;
431 for (save = t; (t[0] = t[1]) != NULL; ++t)
432 continue;
433 t = save;
434 } else
435 ++t;
436 }
437 }
438
439 void
440 usage()
441 {
442
443 (void)fprintf(stderr, "usage: rm [-dfiPRrW] file ...\n");
444 exit(1);
445 /* NOTREACHED */
446 }
447