VirtualBox

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

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

FreeBSD 5.x on amd64

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

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