source: trunk/src/gmake/kmkbuiltin/rm.c@ 297

Last change on this file since 297 was 297, checked in by bird, 20 years ago

FreeBSD config

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1/*-
2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static const char copyright[] =
33"@(#) Copyright (c) 1990, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94";
39#endif /* not lint */
40#endif
41#include <sys/cdefs.h>
42//__FBSDID("$FreeBSD: src/bin/rm/rm.c,v 1.47 2004/04/06 20:06:50 markm Exp $");
43
44#include <sys/stat.h>
45#include <sys/param.h>
46#include <sys/mount.h>
47
48#include <err.h>
49#include <errno.h>
50#include <fcntl.h>
51#include <fts.h>
52#include <grp.h>
53#include <pwd.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <sysexits.h>
58#include <unistd.h>
59
60#ifdef __EMX__
61#undef S_IFWHT
62#endif
63#ifndef S_IFWHT
64#define S_IFWHT 0
65#define S_ISWHT(s) 0
66#define undelete(s) (-1)
67#endif
68
69#ifndef __FreeBSD__
70extern void strmode(mode_t mode, char *p);
71#endif
72
73static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
74static uid_t uid;
75
76static char *argv0;
77
78static int check(char *, char *, struct stat *);
79static void checkdot(char **);
80static void rm_file(char **);
81static int rm_overwrite(char *, struct stat *);
82static void rm_tree(char **);
83static int usage(void);
84
85/*
86 * rm --
87 * This rm is different from historic rm's, but is expected to match
88 * POSIX 1003.2 behavior. The most visible difference is that -f
89 * has two specific effects now, ignore non-existent files and force
90 * file removal.
91 */
92int
93kmk_builtin_rm(int argc, char *argv[])
94{
95 int ch, rflag;
96 char *p;
97
98 argv0 = argv[0];
99 dflag = eval = fflag = iflag = Pflag = vflag = Wflag = stdin_ok = 0;
100 uid = 0;
101 opterr = 1;
102 optarg = NULL;
103 optopt = 0;
104 optind = 0; /* init */
105
106 /*
107 * Test for the special case where the utility is called as
108 * "unlink", for which the functionality provided is greatly
109 * simplified.
110 */
111 if ((p = rindex(argv[0], '/')) == NULL)
112 p = argv[0];
113 else
114 ++p;
115 if (strcmp(p, "unlink") == 0) {
116 while (getopt(argc, argv, "") != -1)
117 return usage();
118 argc -= optind;
119 argv += optind;
120 if (argc != 1)
121 return usage();
122 rm_file(&argv[0]);
123 return eval;
124 }
125
126 Pflag = rflag = 0;
127 while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
128 switch(ch) {
129 case 'd':
130 dflag = 1;
131 break;
132 case 'f':
133 fflag = 1;
134 iflag = 0;
135 break;
136 case 'i':
137 fflag = 0;
138 iflag = 1;
139 break;
140 case 'P':
141 Pflag = 1;
142 break;
143 case 'R':
144 case 'r': /* Compatibility. */
145 rflag = 1;
146 break;
147 case 'v':
148 vflag = 1;
149 break;
150#ifdef FTS_WHITEOUT
151 case 'W':
152 Wflag = 1;
153 break;
154#endif
155 default:
156 return usage();
157 }
158 argc -= optind;
159 argv += optind;
160
161 if (argc < 1) {
162 if (fflag)
163 return (0);
164 return usage();
165 }
166
167 checkdot(argv);
168 uid = geteuid();
169
170 if (*argv) {
171 stdin_ok = isatty(STDIN_FILENO);
172
173 if (rflag)
174 rm_tree(argv);
175 else
176 rm_file(argv);
177 }
178
179 return eval;
180}
181
182static void
183rm_tree(char **argv)
184{
185 FTS *fts;
186 FTSENT *p;
187 int needstat;
188 int flags;
189 int rval;
190
191 /*
192 * Remove a file hierarchy. If forcing removal (-f), or interactive
193 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
194 */
195 needstat = !uid || (!fflag && !iflag && stdin_ok);
196
197 /*
198 * If the -i option is specified, the user can skip on the pre-order
199 * visit. The fts_number field flags skipped directories.
200 */
201#define SKIPPED 1
202
203 flags = FTS_PHYSICAL;
204 if (!needstat)
205 flags |= FTS_NOSTAT;
206#ifdef FTS_WHITEOUT
207 if (Wflag)
208 flags |= FTS_WHITEOUT;
209#endif
210 if (!(fts = fts_open(argv, flags, NULL)))
211 err(1, "fts_open");
212 while ((p = fts_read(fts)) != NULL) {
213 switch (p->fts_info) {
214 case FTS_DNR:
215 if (!fflag || p->fts_errno != ENOENT) {
216 fprintf(stderr, "%s: %s: %s\n",
217 argv0, p->fts_path, strerror(p->fts_errno));
218 eval = 1;
219 }
220 continue;
221 case FTS_ERR:
222 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
223 case FTS_NS:
224 /*
225 * Assume that since fts_read() couldn't stat the
226 * file, it can't be unlinked.
227 */
228 if (!needstat)
229 break;
230 if (!fflag || p->fts_errno != ENOENT) {
231 fprintf(stderr, "%s: %s: %s\n",
232 argv0, p->fts_path, strerror(p->fts_errno));
233 eval = 1;
234 }
235 continue;
236 case FTS_D:
237 /* Pre-order: give user chance to skip. */
238 if (!fflag && !check(p->fts_path, p->fts_accpath,
239 p->fts_statp)) {
240 (void)fts_set(fts, p, FTS_SKIP);
241 p->fts_number = SKIPPED;
242 }
243#ifdef UF_APPEND
244 else if (!uid &&
245 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
246 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
247 chflags(p->fts_accpath,
248 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
249 goto err;
250#endif
251 continue;
252 case FTS_DP:
253 /* Post-order: see if user skipped. */
254 if (p->fts_number == SKIPPED)
255 continue;
256 break;
257 default:
258 if (!fflag &&
259 !check(p->fts_path, p->fts_accpath, p->fts_statp))
260 continue;
261 }
262
263 rval = 0;
264#ifdef UF_APPEND
265 if (!uid &&
266 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
267 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
268 rval = chflags(p->fts_accpath,
269 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
270#endif
271 if (rval == 0) {
272 /*
273 * If we can't read or search the directory, may still be
274 * able to remove it. Don't print out the un{read,search}able
275 * message unless the remove fails.
276 */
277 switch (p->fts_info) {
278 case FTS_DP:
279 case FTS_DNR:
280 rval = rmdir(p->fts_accpath);
281 if (rval == 0 || (fflag && errno == ENOENT)) {
282 if (rval == 0 && vflag)
283 (void)printf("%s\n",
284 p->fts_path);
285 continue;
286 }
287 break;
288
289#ifdef FTS_W
290 case FTS_W:
291 rval = undelete(p->fts_accpath);
292 if (rval == 0 && (fflag && errno == ENOENT)) {
293 if (vflag)
294 (void)printf("%s\n",
295 p->fts_path);
296 continue;
297 }
298 break;
299#endif
300
301 case FTS_NS:
302 /*
303 * Assume that since fts_read() couldn't stat
304 * the file, it can't be unlinked.
305 */
306 if (fflag)
307 continue;
308 /* FALLTHROUGH */
309 default:
310 if (Pflag)
311 if (!rm_overwrite(p->fts_accpath, NULL))
312 continue;
313 rval = unlink(p->fts_accpath);
314 if (rval == 0 || (fflag && errno == ENOENT)) {
315 if (rval == 0 && vflag)
316 (void)printf("%s\n",
317 p->fts_path);
318 continue;
319 }
320 }
321 }
322err:
323 fprintf(stderr, "%s: %s: %s\n", argv0, p->fts_path, strerror(errno));
324 eval = 1;
325 }
326 if (errno) {
327 fprintf(stderr, "%s: fts_read: %s\n", argv0, strerror(errno));
328 eval = 1;
329 }
330}
331
332static void
333rm_file(char **argv)
334{
335 struct stat sb;
336 int rval;
337 char *f;
338
339 /*
340 * Remove a file. POSIX 1003.2 states that, by default, attempting
341 * to remove a directory is an error, so must always stat the file.
342 */
343 while ((f = *argv++) != NULL) {
344 /* Assume if can't stat the file, can't unlink it. */
345 if (lstat(f, &sb)) {
346#ifdef FTS_WHITEOUT
347 if (Wflag) {
348 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
349 } else {
350#else
351 {
352#endif
353 if (!fflag || errno != ENOENT) {
354 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(errno));
355 eval = 1;
356 }
357 continue;
358 }
359#ifdef FTS_WHITEOUT
360 } else if (Wflag) {
361 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(EEXIST));
362 eval = 1;
363 continue;
364#endif
365 }
366
367 if (S_ISDIR(sb.st_mode) && !dflag) {
368 fprintf(stderr, "%s: %s: is a directory\n", argv0, f);
369 eval = 1;
370 continue;
371 }
372 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
373 continue;
374 rval = 0;
375#ifdef UF_APPEND
376 if (!uid &&
377 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
378 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
379 rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
380#endif
381 if (rval == 0) {
382 if (S_ISWHT(sb.st_mode))
383 rval = undelete(f);
384 else if (S_ISDIR(sb.st_mode))
385 rval = rmdir(f);
386 else {
387 if (Pflag)
388 if (!rm_overwrite(f, &sb))
389 continue;
390 rval = unlink(f);
391 }
392 }
393 if (rval && (!fflag || errno != ENOENT)) {
394 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(errno));
395 eval = 1;
396 }
397 if (vflag && rval == 0)
398 (void)printf("%s\n", f);
399 }
400}
401
402/*
403 * rm_overwrite --
404 * Overwrite the file 3 times with varying bit patterns.
405 *
406 * XXX
407 * This is a cheap way to *really* delete files. Note that only regular
408 * files are deleted, directories (and therefore names) will remain.
409 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
410 * System V file system). In a logging file system, you'll have to have
411 * kernel support.
412 */
413static int
414rm_overwrite(char *file, struct stat *sbp)
415{
416 struct stat sb;
417#ifdef HAVE_FSTATFS
418 struct statfs fsb;
419#endif
420 off_t len;
421 int bsize, fd, wlen;
422 char *buf = NULL;
423
424 fd = -1;
425 if (sbp == NULL) {
426 if (lstat(file, &sb))
427 goto err;
428 sbp = &sb;
429 }
430 if (!S_ISREG(sbp->st_mode))
431 return (1);
432 if ((fd = open(file, O_WRONLY, 0)) == -1)
433 goto err;
434#ifdef HAVE_FSTATFS
435 if (fstatfs(fd, &fsb) == -1)
436 goto err;
437 bsize = MAX(fsb.f_iosize, 1024);
438#elif defined(HAVE_ST_BLKSIZE)
439 bsize = MAX(sb.st_blksize, 1024);
440#else
441 bsize = 1024;
442#endif
443 if ((buf = malloc(bsize)) == NULL)
444 err(1, "%s: malloc", file);
445
446#define PASS(byte) { \
447 memset(buf, byte, bsize); \
448 for (len = sbp->st_size; len > 0; len -= wlen) { \
449 wlen = len < bsize ? len : bsize; \
450 if (write(fd, buf, wlen) != wlen) \
451 goto err; \
452 } \
453}
454 PASS(0xff);
455 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
456 goto err;
457 PASS(0x00);
458 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
459 goto err;
460 PASS(0xff);
461 if (!fsync(fd) && !close(fd)) {
462 free(buf);
463 return (1);
464 }
465
466err: eval = 1;
467 if (buf)
468 free(buf);
469 if (fd != -1)
470 close(fd);
471 fprintf(stderr, "%s: %s: %s\n", argv0, file, strerror(errno));
472 return (0);
473}
474
475
476static int
477check(char *path, char *name, struct stat *sp)
478{
479 int ch, first;
480 char modep[15], *flagsp;
481
482 /* Check -i first. */
483 if (iflag)
484 (void)fprintf(stderr, "remove %s? ", path);
485 else {
486 /*
487 * If it's not a symbolic link and it's unwritable and we're
488 * talking to a terminal, ask. Symbolic links are excluded
489 * because their permissions are meaningless. Check stdin_ok
490 * first because we may not have stat'ed the file.
491 * Also skip this check if the -P option was specified because
492 * we will not be able to overwrite file contents and will
493 * barf later.
494 */
495 if (!stdin_ok || S_ISLNK(sp->st_mode) || Pflag ||
496 (!access(name, W_OK) &&
497#ifdef SF_APPEND
498 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
499 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))
500#else
501 1)
502#endif
503 )
504 return (1);
505 strmode(sp->st_mode, modep);
506#ifdef SF_APPEND
507 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
508 err(1, "fflagstostr");
509 (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
510 modep + 1, modep[9] == ' ' ? "" : " ",
511 user_from_uid(sp->st_uid, 0),
512 group_from_gid(sp->st_gid, 0),
513 *flagsp ? flagsp : "", *flagsp ? " " : "",
514 path);
515 free(flagsp);
516#else
517 (void)fprintf(stderr, "override %s%s %d/%d for %s? ",
518 modep + 1, modep[9] == ' ' ? "" : " ",
519 sp->st_uid, sp->st_gid, path);
520#endif
521 }
522 (void)fflush(stderr);
523
524 first = ch = getchar();
525 while (ch != '\n' && ch != EOF)
526 ch = getchar();
527 return (first == 'y' || first == 'Y');
528}
529
530#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
531static void
532checkdot(char **argv)
533{
534 char *p, **save, **t;
535 int complained;
536
537 complained = 0;
538 for (t = argv; *t;) {
539 if ((p = strrchr(*t, '/')) != NULL)
540 ++p;
541 else
542 p = *t;
543 if (ISDOT(p)) {
544 if (!complained++)
545 fprintf(stderr, "%s: \".\" and \"..\" may not be removed\n", argv0);
546 eval = 1;
547 for (save = t; (t[0] = t[1]) != NULL; ++t)
548 continue;
549 t = save;
550 } else
551 ++t;
552 }
553}
554
555static int
556usage(void)
557{
558
559 (void)fprintf(stderr, "%s\n%s\n",
560 "usage: rm [-f | -i] [-dPRrvW] file ...\n",
561 " unlink file");
562 return EX_USAGE;
563}
Note: See TracBrowser for help on using the repository browser.