Home | History | Annotate | Line # | Download | only in rm
rm.c revision 1.1.1.3
      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  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 static char copyright[] =
     36 "@(#) Copyright (c) 1990, 1993, 1994\n\
     37 	The Regents of the University of California.  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 static char sccsid[] = "@(#)rm.c	8.8 (Berkeley) 4/27/95";
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 #include <sys/stat.h>
     46 
     47 #include <err.h>
     48 #include <errno.h>
     49 #include <fcntl.h>
     50 #include <fts.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 
     56 int dflag, eval, fflag, iflag, Pflag, Wflag, stdin_ok;
     57 
     58 int	check __P((char *, char *, struct stat *));
     59 void	checkdot __P((char **));
     60 void	rm_file __P((char **));
     61 void	rm_overwrite __P((char *, struct stat *));
     62 void	rm_tree __P((char **));
     63 void	usage __P((void));
     64 
     65 /*
     66  * rm --
     67  *	This rm is different from historic rm's, but is expected to match
     68  *	POSIX 1003.2 behavior.  The most visible difference is that -f
     69  *	has two specific effects now, ignore non-existent files and force
     70  * 	file removal.
     71  */
     72 int
     73 main(argc, argv)
     74 	int argc;
     75 	char *argv[];
     76 {
     77 	int ch, rflag;
     78 
     79 	Pflag = rflag = 0;
     80 	while ((ch = getopt(argc, argv, "dfiPRrW")) != -1)
     81 		switch(ch) {
     82 		case 'd':
     83 			dflag = 1;
     84 			break;
     85 		case 'f':
     86 			fflag = 1;
     87 			iflag = 0;
     88 			break;
     89 		case 'i':
     90 			fflag = 0;
     91 			iflag = 1;
     92 			break;
     93 		case 'P':
     94 			Pflag = 1;
     95 			break;
     96 		case 'R':
     97 		case 'r':			/* Compatibility. */
     98 			rflag = 1;
     99 			break;
    100 		case 'W':
    101 			Wflag = 1;
    102 			break;
    103 		case '?':
    104 		default:
    105 			usage();
    106 		}
    107 	argc -= optind;
    108 	argv += optind;
    109 
    110 	if (argc < 1)
    111 		usage();
    112 
    113 	checkdot(argv);
    114 
    115 	if (*argv) {
    116 		stdin_ok = isatty(STDIN_FILENO);
    117 
    118 		if (rflag)
    119 			rm_tree(argv);
    120 		else
    121 			rm_file(argv);
    122 	}
    123 
    124 	exit (eval);
    125 }
    126 
    127 void
    128 rm_tree(argv)
    129 	char **argv;
    130 {
    131 	FTS *fts;
    132 	FTSENT *p;
    133 	int needstat;
    134 	int flags;
    135 
    136 	/*
    137 	 * Remove a file hierarchy.  If forcing removal (-f), or interactive
    138 	 * (-i) or can't ask anyway (stdin_ok), don't stat the file.
    139 	 */
    140 	needstat = !fflag && !iflag && stdin_ok;
    141 
    142 	/*
    143 	 * If the -i option is specified, the user can skip on the pre-order
    144 	 * visit.  The fts_number field flags skipped directories.
    145 	 */
    146 #define	SKIPPED	1
    147 
    148 	flags = FTS_PHYSICAL;
    149 	if (!needstat)
    150 		flags |= FTS_NOSTAT;
    151 	if (Wflag)
    152 		flags |= FTS_WHITEOUT;
    153 	if (!(fts = fts_open(argv, flags, (int (*)())NULL)))
    154 		err(1, NULL);
    155 	while ((p = fts_read(fts)) != NULL) {
    156 		switch (p->fts_info) {
    157 		case FTS_DNR:
    158 			if (!fflag || p->fts_errno != ENOENT) {
    159 				warnx("%s: %s",
    160 				    p->fts_path, strerror(p->fts_errno));
    161 				eval = 1;
    162 			}
    163 			continue;
    164 		case FTS_ERR:
    165 			errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno));
    166 		case FTS_NS:
    167 			/*
    168 			 * FTS_NS: assume that if can't stat the file, it
    169 			 * can't be unlinked.
    170 			 */
    171 			if (!needstat)
    172 				break;
    173 			if (!fflag || p->fts_errno != ENOENT) {
    174 				warnx("%s: %s",
    175 				    p->fts_path, strerror(p->fts_errno));
    176 				eval = 1;
    177 			}
    178 			continue;
    179 		case FTS_D:
    180 			/* Pre-order: give user chance to skip. */
    181 			if (!fflag && !check(p->fts_path, p->fts_accpath,
    182 			    p->fts_statp)) {
    183 				(void)fts_set(fts, p, FTS_SKIP);
    184 				p->fts_number = SKIPPED;
    185 			}
    186 			continue;
    187 		case FTS_DP:
    188 			/* Post-order: see if user skipped. */
    189 			if (p->fts_number == SKIPPED)
    190 				continue;
    191 			break;
    192 		default:
    193 			if (!fflag &&
    194 			    !check(p->fts_path, p->fts_accpath, p->fts_statp))
    195 				continue;
    196 		}
    197 
    198 		/*
    199 		 * If we can't read or search the directory, may still be
    200 		 * able to remove it.  Don't print out the un{read,search}able
    201 		 * message unless the remove fails.
    202 		 */
    203 		switch (p->fts_info) {
    204 		case FTS_DP:
    205 		case FTS_DNR:
    206 			if (!rmdir(p->fts_accpath) || fflag && errno == ENOENT)
    207 				continue;
    208 			break;
    209 
    210 		case FTS_W:
    211 			if (!undelete(p->fts_accpath) ||
    212 			    fflag && errno == ENOENT)
    213 				continue;
    214 			break;
    215 
    216 		default:
    217 			if (Pflag)
    218 				rm_overwrite(p->fts_accpath, NULL);
    219 			if (!unlink(p->fts_accpath) || fflag && errno == ENOENT)
    220 				continue;
    221 		}
    222 		warn("%s", p->fts_path);
    223 		eval = 1;
    224 	}
    225 	if (errno)
    226 		err(1, "fts_read");
    227 }
    228 
    229 void
    230 rm_file(argv)
    231 	char **argv;
    232 {
    233 	struct stat sb;
    234 	int rval;
    235 	char *f;
    236 
    237 	/*
    238 	 * Remove a file.  POSIX 1003.2 states that, by default, attempting
    239 	 * to remove a directory is an error, so must always stat the file.
    240 	 */
    241 	while ((f = *argv++) != NULL) {
    242 		/* Assume if can't stat the file, can't unlink it. */
    243 		if (lstat(f, &sb)) {
    244 			if (Wflag) {
    245 				sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR;
    246 			} else {
    247 				if (!fflag || errno != ENOENT) {
    248 					warn("%s", f);
    249 					eval = 1;
    250 				}
    251 				continue;
    252 			}
    253 		} else if (Wflag) {
    254 			warnx("%s: %s", f, strerror(EEXIST));
    255 			eval = 1;
    256 			continue;
    257 		}
    258 
    259 		if (S_ISDIR(sb.st_mode) && !dflag) {
    260 			warnx("%s: is a directory", f);
    261 			eval = 1;
    262 			continue;
    263 		}
    264 		if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb))
    265 			continue;
    266 		if (S_ISWHT(sb.st_mode))
    267 			rval = undelete(f);
    268 		else if (S_ISDIR(sb.st_mode))
    269 			rval = rmdir(f);
    270 		else {
    271 			if (Pflag)
    272 				rm_overwrite(f, &sb);
    273 			rval = unlink(f);
    274 		}
    275 		if (rval && (!fflag || errno != ENOENT)) {
    276 			warn("%s", f);
    277 			eval = 1;
    278 		}
    279 	}
    280 }
    281 
    282 /*
    283  * rm_overwrite --
    284  *	Overwrite the file 3 times with varying bit patterns.
    285  *
    286  * XXX
    287  * This is a cheap way to *really* delete files.  Note that only regular
    288  * files are deleted, directories (and therefore names) will remain.
    289  * Also, this assumes a fixed-block file system (like FFS, or a V7 or a
    290  * System V file system).  In a logging file system, you'll have to have
    291  * kernel support.
    292  */
    293 void
    294 rm_overwrite(file, sbp)
    295 	char *file;
    296 	struct stat *sbp;
    297 {
    298 	struct stat sb;
    299 	off_t len;
    300 	int fd, wlen;
    301 	char buf[8 * 1024];
    302 
    303 	fd = -1;
    304 	if (sbp == NULL) {
    305 		if (lstat(file, &sb))
    306 			goto err;
    307 		sbp = &sb;
    308 	}
    309 	if (!S_ISREG(sbp->st_mode))
    310 		return;
    311 	if ((fd = open(file, O_WRONLY, 0)) == -1)
    312 		goto err;
    313 
    314 #define	PASS(byte) {							\
    315 	memset(buf, byte, sizeof(buf));					\
    316 	for (len = sbp->st_size; len > 0; len -= wlen) {		\
    317 		wlen = len < sizeof(buf) ? len : sizeof(buf);		\
    318 		if (write(fd, buf, wlen) != wlen)			\
    319 			goto err;					\
    320 	}								\
    321 }
    322 	PASS(0xff);
    323 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
    324 		goto err;
    325 	PASS(0x00);
    326 	if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET))
    327 		goto err;
    328 	PASS(0xff);
    329 	if (!fsync(fd) && !close(fd))
    330 		return;
    331 
    332 err:	eval = 1;
    333 	warn("%s", file);
    334 }
    335 
    336 
    337 int
    338 check(path, name, sp)
    339 	char *path, *name;
    340 	struct stat *sp;
    341 {
    342 	int ch, first;
    343 	char modep[15];
    344 
    345 	/* Check -i first. */
    346 	if (iflag)
    347 		(void)fprintf(stderr, "remove %s? ", path);
    348 	else {
    349 		/*
    350 		 * If it's not a symbolic link and it's unwritable and we're
    351 		 * talking to a terminal, ask.  Symbolic links are excluded
    352 		 * because their permissions are meaningless.  Check stdin_ok
    353 		 * first because we may not have stat'ed the file.
    354 		 */
    355 		if (!stdin_ok || S_ISLNK(sp->st_mode) || !access(name, W_OK))
    356 			return (1);
    357 		strmode(sp->st_mode, modep);
    358 		(void)fprintf(stderr, "override %s%s%s/%s for %s? ",
    359 		    modep + 1, modep[9] == ' ' ? "" : " ",
    360 		    user_from_uid(sp->st_uid, 0),
    361 		    group_from_gid(sp->st_gid, 0), path);
    362 	}
    363 	(void)fflush(stderr);
    364 
    365 	first = ch = getchar();
    366 	while (ch != '\n' && ch != EOF)
    367 		ch = getchar();
    368 	return (first == 'y');
    369 }
    370 
    371 #define ISDOT(a)	((a)[0] == '.' && (!(a)[1] || (a)[1] == '.' && !(a)[2]))
    372 void
    373 checkdot(argv)
    374 	char **argv;
    375 {
    376 	char *p, **save, **t;
    377 	int complained;
    378 
    379 	complained = 0;
    380 	for (t = argv; *t;) {
    381 		if ((p = strrchr(*t, '/')) != NULL)
    382 			++p;
    383 		else
    384 			p = *t;
    385 		if (ISDOT(p)) {
    386 			if (!complained++)
    387 				warnx("\".\" and \"..\" may not be removed");
    388 			eval = 1;
    389 			for (save = t; (t[0] = t[1]) != NULL; ++t)
    390 				continue;
    391 			t = save;
    392 		} else
    393 			++t;
    394 	}
    395 }
    396 
    397 void
    398 usage()
    399 {
    400 
    401 	(void)fprintf(stderr, "usage: rm [-dfiPRrW] file ...\n");
    402 	exit(1);
    403 }
    404