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

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

fixing and adding more commands.

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