VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/rm.c@ 269

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

whiteout.

  • 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
69extern void strmode(mode_t mode, char *p);
70
71static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
72static uid_t uid;
73
74static char *argv0;
75
76static int check(char *, char *, struct stat *);
77static void checkdot(char **);
78static void rm_file(char **);
79static int rm_overwrite(char *, struct stat *);
80static void rm_tree(char **);
81static int usage(void);
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 */
90int
91kmk_builtin_rm(int argc, char *argv[])
92{
93 int ch, rflag;
94 char *p;
95
96 argv0 = argv[0];
97 dflag = eval = fflag = iflag = Pflag = vflag = Wflag = stdin_ok = 0;
98 uid = 0;
99 opterr = 1;
100 optarg = NULL;
101 optopt = 0;
102 optind = 0; /* init */
103
104 /*
105 * Test for the special case where the utility is called as
106 * "unlink", for which the functionality provided is greatly
107 * simplified.
108 */
109 if ((p = rindex(argv[0], '/')) == NULL)
110 p = argv[0];
111 else
112 ++p;
113 if (strcmp(p, "unlink") == 0) {
114 while (getopt(argc, argv, "") != -1)
115 return usage();
116 argc -= optind;
117 argv += optind;
118 if (argc != 1)
119 return usage();
120 rm_file(&argv[0]);
121 return eval;
122 }
123
124 Pflag = rflag = 0;
125 while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1)
126 switch(ch) {
127 case 'd':
128 dflag = 1;
129 break;
130 case 'f':
131 fflag = 1;
132 iflag = 0;
133 break;
134 case 'i':
135 fflag = 0;
136 iflag = 1;
137 break;
138 case 'P':
139 Pflag = 1;
140 break;
141 case 'R':
142 case 'r': /* Compatibility. */
143 rflag = 1;
144 break;
145 case 'v':
146 vflag = 1;
147 break;
148#ifdef FTS_WHITEOUT
149 case 'W':
150 Wflag = 1;
151 break;
152#endif
153 default:
154 return usage();
155 }
156 argc -= optind;
157 argv += optind;
158
159 if (argc < 1) {
160 if (fflag)
161 return (0);
162 return usage();
163 }
164
165 checkdot(argv);
166 uid = geteuid();
167
168 if (*argv) {
169 stdin_ok = isatty(STDIN_FILENO);
170
171 if (rflag)
172 rm_tree(argv);
173 else
174 rm_file(argv);
175 }
176
177 return eval;
178}
179
180static void
181rm_tree(char **argv)
182{
183 FTS *fts;
184 FTSENT *p;
185 int needstat;
186 int flags;
187 int rval;
188
189 /*
190 * Remove a file hierarchy. If forcing removal (-f), or interactive
191 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
192 */
193 needstat = !uid || (!fflag && !iflag && stdin_ok);
194
195 /*
196 * If the -i option is specified, the user can skip on the pre-order
197 * visit. The fts_number field flags skipped directories.
198 */
199#define SKIPPED 1
200
201 flags = FTS_PHYSICAL;
202 if (!needstat)
203 flags |= FTS_NOSTAT;
204#ifdef FTS_WHITEOUT
205 if (Wflag)
206 flags |= FTS_WHITEOUT;
207#endif
208 if (!(fts = fts_open(argv, flags, NULL)))
209 err(1, "fts_open");
210 while ((p = fts_read(fts)) != NULL) {
211 switch (p->fts_info) {
212 case FTS_DNR:
213 if (!fflag || p->fts_errno != ENOENT) {
214 fprintf(stderr, "%s: %s: %s\n",
215 argv0, p->fts_path, strerror(p->fts_errno));
216 eval = 1;
217 }
218 continue;
219 case FTS_ERR:
220 errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
221 case FTS_NS:
222 /*
223 * Assume that since fts_read() couldn't stat the
224 * file, it can't be unlinked.
225 */
226 if (!needstat)
227 break;
228 if (!fflag || p->fts_errno != ENOENT) {
229 fprintf(stderr, "%s: %s: %s\n",
230 argv0, p->fts_path, strerror(p->fts_errno));
231 eval = 1;
232 }
233 continue;
234 case FTS_D:
235 /* Pre-order: give user chance to skip. */
236 if (!fflag && !check(p->fts_path, p->fts_accpath,
237 p->fts_statp)) {
238 (void)fts_set(fts, p, FTS_SKIP);
239 p->fts_number = SKIPPED;
240 }
241#ifdef UF_APPEND
242 else if (!uid &&
243 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
244 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
245 chflags(p->fts_accpath,
246 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0)
247 goto err;
248#endif
249 continue;
250 case FTS_DP:
251 /* Post-order: see if user skipped. */
252 if (p->fts_number == SKIPPED)
253 continue;
254 break;
255 default:
256 if (!fflag &&
257 !check(p->fts_path, p->fts_accpath, p->fts_statp))
258 continue;
259 }
260
261 rval = 0;
262#ifdef UF_APPEND
263 if (!uid &&
264 (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
265 !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)))
266 rval = chflags(p->fts_accpath,
267 p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE));
268#endif
269 if (rval == 0) {
270 /*
271 * If we can't read or search the directory, may still be
272 * able to remove it. Don't print out the un{read,search}able
273 * message unless the remove fails.
274 */
275 switch (p->fts_info) {
276 case FTS_DP:
277 case FTS_DNR:
278 rval = rmdir(p->fts_accpath);
279 if (rval == 0 || (fflag && errno == ENOENT)) {
280 if (rval == 0 && vflag)
281 (void)printf("%s\n",
282 p->fts_path);
283 continue;
284 }
285 break;
286
287#ifdef FTS_W
288 case FTS_W:
289 rval = undelete(p->fts_accpath);
290 if (rval == 0 && (fflag && errno == ENOENT)) {
291 if (vflag)
292 (void)printf("%s\n",
293 p->fts_path);
294 continue;
295 }
296 break;
297#endif
298
299 case FTS_NS:
300 /*
301 * Assume that since fts_read() couldn't stat
302 * the file, it can't be unlinked.
303 */
304 if (fflag)
305 continue;
306 /* FALLTHROUGH */
307 default:
308 if (Pflag)
309 if (!rm_overwrite(p->fts_accpath, NULL))
310 continue;
311 rval = unlink(p->fts_accpath);
312 if (rval == 0 || (fflag && errno == ENOENT)) {
313 if (rval == 0 && vflag)
314 (void)printf("%s\n",
315 p->fts_path);
316 continue;
317 }
318 }
319 }
320err:
321 fprintf(stderr, "%s: %s: %s\n", argv0, p->fts_path, strerror(errno));
322 eval = 1;
323 }
324 if (errno) {
325 fprintf(stderr, "%s: fts_read: %s\n", argv0, strerror(errno));
326 eval = 1;
327 }
328}
329
330static void
331rm_file(char **argv)
332{
333 struct stat sb;
334 int rval;
335 char *f;
336
337 /*
338 * Remove a file. POSIX 1003.2 states that, by default, attempting
339 * to remove a directory is an error, so must always stat the file.
340 */
341 while ((f = *argv++) != NULL) {
342 /* Assume if can't stat the file, can't unlink it. */
343 if (lstat(f, &sb)) {
344#ifdef FTS_WHITEOUT
345 if (Wflag) {
346 sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
347 } else {
348#else
349 {
350#endif
351 if (!fflag || errno != ENOENT) {
352 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(errno));
353 eval = 1;
354 }
355 continue;
356 }
357#ifdef FTS_WHITEOUT
358 } else if (Wflag) {
359 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(EEXIST));
360 eval = 1;
361 continue;
362#endif
363 }
364
365 if (S_ISDIR(sb.st_mode) && !dflag) {
366 fprintf(stderr, "%s: %s: is a directory\n", argv0, f);
367 eval = 1;
368 continue;
369 }
370 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
371 continue;
372 rval = 0;
373#ifdef UF_APPEND
374 if (!uid &&
375 (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) &&
376 !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE)))
377 rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE));
378#endif
379 if (rval == 0) {
380 if (S_ISWHT(sb.st_mode))
381 rval = undelete(f);
382 else if (S_ISDIR(sb.st_mode))
383 rval = rmdir(f);
384 else {
385 if (Pflag)
386 if (!rm_overwrite(f, &sb))
387 continue;
388 rval = unlink(f);
389 }
390 }
391 if (rval && (!fflag || errno != ENOENT)) {
392 fprintf(stderr, "%s: %s: %s\n", argv0, f, strerror(errno));
393 eval = 1;
394 }
395 if (vflag && rval == 0)
396 (void)printf("%s\n", f);
397 }
398}
399
400/*
401 * rm_overwrite --
402 * Overwrite the file 3 times with varying bit patterns.
403 *
404 * XXX
405 * This is a cheap way to *really* delete files. Note that only regular
406 * files are deleted, directories (and therefore names) will remain.
407 * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
408 * System V file system). In a logging file system, you'll have to have
409 * kernel support.
410 */
411static int
412rm_overwrite(char *file, struct stat *sbp)
413{
414 struct stat sb;
415#ifdef HAVE_FSTATFS
416 struct statfs fsb;
417#endif
418 off_t len;
419 int bsize, fd, wlen;
420 char *buf = NULL;
421
422 fd = -1;
423 if (sbp == NULL) {
424 if (lstat(file, &sb))
425 goto err;
426 sbp = &sb;
427 }
428 if (!S_ISREG(sbp->st_mode))
429 return (1);
430 if ((fd = open(file, O_WRONLY, 0)) == -1)
431 goto err;
432#ifdef HAVE_FSTATFS
433 if (fstatfs(fd, &fsb) == -1)
434 goto err;
435 bsize = MAX(fsb.f_iosize, 1024);
436#elif defined(HAVE_ST_BLKSIZE)
437 bsize = MAX(sb.st_blksize, 1024);
438#else
439 bsize = 1024;
440#endif
441 if ((buf = malloc(bsize)) == NULL)
442 err(1, "%s: malloc", file);
443
444#define PASS(byte) { \
445 memset(buf, byte, bsize); \
446 for (len = sbp->st_size; len > 0; len -= wlen) { \
447 wlen = len < bsize ? len : bsize; \
448 if (write(fd, buf, wlen) != wlen) \
449 goto err; \
450 } \
451}
452 PASS(0xff);
453 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
454 goto err;
455 PASS(0x00);
456 if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
457 goto err;
458 PASS(0xff);
459 if (!fsync(fd) && !close(fd)) {
460 free(buf);
461 return (1);
462 }
463
464err: eval = 1;
465 if (buf)
466 free(buf);
467 if (fd != -1)
468 close(fd);
469 fprintf(stderr, "%s: %s: %s\n", argv0, file, strerror(errno));
470 return (0);
471}
472
473
474static int
475check(char *path, char *name, struct stat *sp)
476{
477 int ch, first;
478 char modep[15], *flagsp;
479
480 /* Check -i first. */
481 if (iflag)
482 (void)fprintf(stderr, "remove %s? ", path);
483 else {
484 /*
485 * If it's not a symbolic link and it's unwritable and we're
486 * talking to a terminal, ask. Symbolic links are excluded
487 * because their permissions are meaningless. Check stdin_ok
488 * first because we may not have stat'ed the file.
489 * Also skip this check if the -P option was specified because
490 * we will not be able to overwrite file contents and will
491 * barf later.
492 */
493 if (!stdin_ok || S_ISLNK(sp->st_mode) || Pflag ||
494 (!access(name, W_OK) &&
495#ifdef SF_APPEND
496 !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) &&
497 (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))
498#else
499 1)
500#endif
501 )
502 return (1);
503 strmode(sp->st_mode, modep);
504#ifdef SF_APPEND
505 if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
506 err(1, "fflagstostr");
507 (void)fprintf(stderr, "override %s%s%s/%s %s%sfor %s? ",
508 modep + 1, modep[9] == ' ' ? "" : " ",
509 user_from_uid(sp->st_uid, 0),
510 group_from_gid(sp->st_gid, 0),
511 *flagsp ? flagsp : "", *flagsp ? " " : "",
512 path);
513 free(flagsp);
514#else
515 (void)fprintf(stderr, "override %s%s %d/%d for %s? ",
516 modep + 1, modep[9] == ' ' ? "" : " ",
517 sp->st_uid, sp->st_gid, path);
518#endif
519 }
520 (void)fflush(stderr);
521
522 first = ch = getchar();
523 while (ch != '\n' && ch != EOF)
524 ch = getchar();
525 return (first == 'y' || first == 'Y');
526}
527
528#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2])))
529static void
530checkdot(char **argv)
531{
532 char *p, **save, **t;
533 int complained;
534
535 complained = 0;
536 for (t = argv; *t;) {
537 if ((p = strrchr(*t, '/')) != NULL)
538 ++p;
539 else
540 p = *t;
541 if (ISDOT(p)) {
542 if (!complained++)
543 fprintf(stderr, "%s: \".\" and \"..\" may not be removed\n", argv0);
544 eval = 1;
545 for (save = t; (t[0] = t[1]) != NULL; ++t)
546 continue;
547 t = save;
548 } else
549 ++t;
550 }
551}
552
553static int
554usage(void)
555{
556
557 (void)fprintf(stderr, "%s\n%s\n",
558 "usage: rm [-f | -i] [-dPRrvW] file ...\n",
559 " unlink file");
560 return EX_USAGE;
561}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette