rm.c revision 20421
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1990, 1993, 1994 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 3. All advertising materials mentioning features or use of this software 141556Srgrimes * must display the following acknowledgement: 151556Srgrimes * This product includes software developed by the University of 161556Srgrimes * California, Berkeley and its contributors. 171556Srgrimes * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 323044Sdg * 3320421Ssteve * $Id: rm.c,v 1.11 1996/03/07 23:26:59 wosch Exp $ 341556Srgrimes */ 351556Srgrimes 361556Srgrimes#ifndef lint 3720421Sstevestatic char const copyright[] = 381556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\ 391556Srgrimes The Regents of the University of California. All rights reserved.\n"; 401556Srgrimes#endif /* not lint */ 411556Srgrimes 421556Srgrimes#ifndef lint 4320421Sstevestatic char const sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94"; 441556Srgrimes#endif /* not lint */ 451556Srgrimes 461556Srgrimes#include <sys/types.h> 471556Srgrimes#include <sys/stat.h> 481556Srgrimes 491556Srgrimes#include <err.h> 501556Srgrimes#include <errno.h> 511556Srgrimes#include <fcntl.h> 521556Srgrimes#include <fts.h> 531556Srgrimes#include <stdio.h> 541556Srgrimes#include <stdlib.h> 551556Srgrimes#include <string.h> 561556Srgrimes#include <unistd.h> 571556Srgrimes 5820421Ssteve/* 5920421Ssteve * XXX Until we get kernel support for the undelete(2) system call, 6020421Ssteve * this define *must* remain in place. 6120421Ssteve */ 6220421Ssteve#define BSD4_4_LITE 6320421Ssteve 647798Sacheextern char *flags_to_string __P((u_long, char *)); 657798Sache 6620421Ssteve#ifdef BSD4_4_LITE 671556Srgrimesint dflag, eval, fflag, iflag, Pflag, stdin_ok; 6820421Ssteve#else 6920421Ssteveint dflag, eval, fflag, iflag, Pflag, Wflag, stdin_ok; 7020421Ssteve#endif 717798Sacheuid_t uid; 721556Srgrimes 731556Srgrimesint check __P((char *, char *, struct stat *)); 741556Srgrimesvoid checkdot __P((char **)); 751556Srgrimesvoid rm_file __P((char **)); 761556Srgrimesvoid rm_overwrite __P((char *, struct stat *)); 771556Srgrimesvoid rm_tree __P((char **)); 781556Srgrimesvoid usage __P((void)); 791556Srgrimes 801556Srgrimes/* 811556Srgrimes * rm -- 821556Srgrimes * This rm is different from historic rm's, but is expected to match 831556Srgrimes * POSIX 1003.2 behavior. The most visible difference is that -f 841556Srgrimes * has two specific effects now, ignore non-existent files and force 851556Srgrimes * file removal. 861556Srgrimes */ 871556Srgrimesint 881556Srgrimesmain(argc, argv) 891556Srgrimes int argc; 901556Srgrimes char *argv[]; 911556Srgrimes{ 921556Srgrimes int ch, rflag; 931556Srgrimes 9420421Ssteve Pflag = rflag = 0; 9520421Ssteve#ifdef BSD4_4_LITE 9614148Spst while ((ch = getopt(argc, argv, "dfiPRr")) != EOF) 9720421Ssteve#else 9820421Ssteve while ((ch = getopt(argc, argv, "dfiPRrW")) != EOF) 9920421Ssteve#endif 1001556Srgrimes switch(ch) { 1011556Srgrimes case 'd': 1021556Srgrimes dflag = 1; 1031556Srgrimes break; 1041556Srgrimes case 'f': 1051556Srgrimes fflag = 1; 1061556Srgrimes iflag = 0; 1071556Srgrimes break; 1081556Srgrimes case 'i': 1091556Srgrimes fflag = 0; 1101556Srgrimes iflag = 1; 1111556Srgrimes break; 1121556Srgrimes case 'P': 1131556Srgrimes Pflag = 1; 1141556Srgrimes break; 1151556Srgrimes case 'R': 1161556Srgrimes case 'r': /* Compatibility. */ 1171556Srgrimes rflag = 1; 1181556Srgrimes break; 11920421Ssteve#ifndef BSD4_4_LITE 12020421Ssteve case 'W': 12120421Ssteve Wflag = 1; 12220421Ssteve break; 12320421Ssteve#endif 1241556Srgrimes default: 1251556Srgrimes usage(); 1261556Srgrimes } 1271556Srgrimes argc -= optind; 1281556Srgrimes argv += optind; 1291556Srgrimes 1301556Srgrimes if (argc < 1) 1311556Srgrimes usage(); 1321556Srgrimes 1331556Srgrimes checkdot(argv); 1347798Sache uid = geteuid(); 1351556Srgrimes 13620421Ssteve if (*argv) { 13720421Ssteve stdin_ok = isatty(STDIN_FILENO); 13820421Ssteve 13920421Ssteve if (rflag) 14020421Ssteve rm_tree(argv); 14120421Ssteve else 14220421Ssteve rm_file(argv); 14320421Ssteve } 14420421Ssteve 1451556Srgrimes exit (eval); 1461556Srgrimes} 1471556Srgrimes 1481556Srgrimesvoid 1491556Srgrimesrm_tree(argv) 1501556Srgrimes char **argv; 1511556Srgrimes{ 1521556Srgrimes FTS *fts; 1531556Srgrimes FTSENT *p; 1541556Srgrimes int needstat; 15520421Ssteve int flags; 1567798Sache int rval; 1571556Srgrimes 1581556Srgrimes /* 1591556Srgrimes * Remove a file hierarchy. If forcing removal (-f), or interactive 1601556Srgrimes * (-i) or can't ask anyway (stdin_ok), don't stat the file. 1611556Srgrimes */ 16220421Ssteve needstat = !uid || (!fflag && !iflag && stdin_ok); 1631556Srgrimes 1641556Srgrimes /* 1651556Srgrimes * If the -i option is specified, the user can skip on the pre-order 1661556Srgrimes * visit. The fts_number field flags skipped directories. 1671556Srgrimes */ 1681556Srgrimes#define SKIPPED 1 1691556Srgrimes 17020421Ssteve flags = FTS_PHYSICAL | FTS_NOCHDIR; 17120421Ssteve if (!needstat) 17220421Ssteve flags |= FTS_NOSTAT; 17320421Ssteve#ifndef BSD4_4_LITE 17420421Ssteve if (Wflag) 17520421Ssteve flags |= FTS_WHITEOUT; 17620421Ssteve#endif 17720421Ssteve if (!(fts = fts_open(argv, flags, (int (*)())NULL))) 1781556Srgrimes err(1, NULL); 1791556Srgrimes while ((p = fts_read(fts)) != NULL) { 1801556Srgrimes switch (p->fts_info) { 1811556Srgrimes case FTS_DNR: 1821556Srgrimes if (!fflag || p->fts_errno != ENOENT) { 1831556Srgrimes warnx("%s: %s", 1841556Srgrimes p->fts_path, strerror(p->fts_errno)); 1851556Srgrimes eval = 1; 1861556Srgrimes } 1871556Srgrimes continue; 1881556Srgrimes case FTS_ERR: 1891556Srgrimes errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno)); 1901556Srgrimes case FTS_NS: 1911556Srgrimes /* 1921556Srgrimes * FTS_NS: assume that if can't stat the file, it 1931556Srgrimes * can't be unlinked. 1941556Srgrimes */ 1951556Srgrimes if (!needstat) 1961556Srgrimes break; 1971556Srgrimes if (!fflag || p->fts_errno != ENOENT) { 1981556Srgrimes warnx("%s: %s", 1991556Srgrimes p->fts_path, strerror(p->fts_errno)); 2001556Srgrimes eval = 1; 2011556Srgrimes } 2021556Srgrimes continue; 2031556Srgrimes case FTS_D: 2041556Srgrimes /* Pre-order: give user chance to skip. */ 20520421Ssteve if (!fflag && !check(p->fts_path, p->fts_accpath, 2061556Srgrimes p->fts_statp)) { 2071556Srgrimes (void)fts_set(fts, p, FTS_SKIP); 2081556Srgrimes p->fts_number = SKIPPED; 2091556Srgrimes } 2107798Sache else if (!uid && 2117798Sache (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2127798Sache !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 2137798Sache chflags(p->fts_accpath, 2147798Sache p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0) 2157798Sache goto err; 2161556Srgrimes continue; 2171556Srgrimes case FTS_DP: 2181556Srgrimes /* Post-order: see if user skipped. */ 2191556Srgrimes if (p->fts_number == SKIPPED) 2201556Srgrimes continue; 2211556Srgrimes break; 22220421Ssteve default: 22320421Ssteve if (!fflag && 22420421Ssteve !check(p->fts_path, p->fts_accpath, p->fts_statp)) 22520421Ssteve continue; 2261556Srgrimes } 2271556Srgrimes 2287798Sache rval = 0; 2297798Sache if (!uid && 2307798Sache (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2317798Sache !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE))) 2327798Sache rval = chflags(p->fts_accpath, 2337798Sache p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 2347798Sache if (!rval) { 2357798Sache /* 2367798Sache * If we can't read or search the directory, may still be 2377798Sache * able to remove it. Don't print out the un{read,search}able 2387798Sache * message unless the remove fails. 2397798Sache */ 24020421Ssteve switch (p->fts_info) { 24120421Ssteve case FTS_DP: 24220421Ssteve case FTS_DNR: 24320421Ssteve if (!rmdir(p->fts_accpath) || (fflag && errno == ENOENT)) 2441556Srgrimes continue; 24520421Ssteve break; 24620421Ssteve 24720421Ssteve#ifndef BSD4_4_LITE 24820421Ssteve case FTS_W: 24920421Ssteve if (!undelete(p->fts_accpath) || 25020421Ssteve fflag && errno == ENOENT) 25120421Ssteve continue; 25220421Ssteve break; 25320421Ssteve#endif 25420421Ssteve 25520421Ssteve default: 2567798Sache if (Pflag) 2577798Sache rm_overwrite(p->fts_accpath, NULL); 2587798Sache if (!unlink(p->fts_accpath) || (fflag && errno == ENOENT)) 2597798Sache continue; 2607798Sache } 2611556Srgrimes } 2627798Sacheerr: 2631556Srgrimes warn("%s", p->fts_path); 2641556Srgrimes eval = 1; 2651556Srgrimes } 2661556Srgrimes if (errno) 2671556Srgrimes err(1, "fts_read"); 2681556Srgrimes} 2691556Srgrimes 2701556Srgrimesvoid 2711556Srgrimesrm_file(argv) 2721556Srgrimes char **argv; 2731556Srgrimes{ 2741556Srgrimes struct stat sb; 27520421Ssteve int rval; 2761556Srgrimes char *f; 2771556Srgrimes 2781556Srgrimes /* 2791556Srgrimes * Remove a file. POSIX 1003.2 states that, by default, attempting 2801556Srgrimes * to remove a directory is an error, so must always stat the file. 2811556Srgrimes */ 2821556Srgrimes while ((f = *argv++) != NULL) { 2831556Srgrimes /* Assume if can't stat the file, can't unlink it. */ 2841556Srgrimes if (lstat(f, &sb)) { 28520421Ssteve#ifdef BSD4_4_LITE 2861556Srgrimes if (!fflag || errno != ENOENT) { 2871556Srgrimes warn("%s", f); 2881556Srgrimes eval = 1; 28920421Ssteve } 29020421Ssteve#else 29120421Ssteve if (Wflag) { 29220421Ssteve sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR; 29320421Ssteve } else { 29420421Ssteve if (!fflag || errno != ENOENT) { 29520421Ssteve warn("%s", f); 29620421Ssteve eval = 1; 29720421Ssteve } 29820421Ssteve continue; 2991556Srgrimes } 30020421Ssteve } else if (Wflag) { 30120421Ssteve warnx("%s: %s", f, strerror(EEXIST)); 30220421Ssteve eval = 1; 30320421Ssteve#endif 3041556Srgrimes continue; 3051556Srgrimes } 30620421Ssteve 30720421Ssteve if (S_ISDIR(sb.st_mode) && !dflag) { 3081556Srgrimes warnx("%s: is a directory", f); 3091556Srgrimes eval = 1; 3101556Srgrimes continue; 3111556Srgrimes } 31220421Ssteve#ifdef BSD4_4_LITE 3131556Srgrimes if (!fflag && !check(f, f, &sb)) 31420421Ssteve#else 31520421Ssteve if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb)) 31620421Ssteve#endif 3171556Srgrimes continue; 3187798Sache rval = 0; 3197798Sache if (!uid && 3207798Sache (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) && 3217798Sache !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE))) 3227798Sache rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE)); 3237798Sache if (!rval) { 32420421Ssteve#ifdef BSD4_4_LITE 3257798Sache if (S_ISDIR(sb.st_mode)) 32620421Ssteve#else 32720421Ssteve if (S_ISWHT(sb.st_mode)) 32820421Ssteve rval = undelete(f); 32920421Ssteve else if (S_ISDIR(sb.st_mode)) 33020421Ssteve#endif 3317798Sache rval = rmdir(f); 3327798Sache else { 3337798Sache if (Pflag) 3347798Sache rm_overwrite(f, &sb); 3357798Sache rval = unlink(f); 3367798Sache } 3371556Srgrimes } 3381556Srgrimes if (rval && (!fflag || errno != ENOENT)) { 3391556Srgrimes warn("%s", f); 3401556Srgrimes eval = 1; 3411556Srgrimes } 3421556Srgrimes } 3431556Srgrimes} 3441556Srgrimes 3451556Srgrimes/* 3461556Srgrimes * rm_overwrite -- 3471556Srgrimes * Overwrite the file 3 times with varying bit patterns. 3481556Srgrimes * 3491556Srgrimes * XXX 3501556Srgrimes * This is a cheap way to *really* delete files. Note that only regular 3511556Srgrimes * files are deleted, directories (and therefore names) will remain. 3521556Srgrimes * Also, this assumes a fixed-block file system (like FFS, or a V7 or a 3531556Srgrimes * System V file system). In a logging file system, you'll have to have 3541556Srgrimes * kernel support. 3551556Srgrimes */ 3561556Srgrimesvoid 3571556Srgrimesrm_overwrite(file, sbp) 3581556Srgrimes char *file; 3591556Srgrimes struct stat *sbp; 3601556Srgrimes{ 3611556Srgrimes struct stat sb; 3621556Srgrimes off_t len; 3631556Srgrimes int fd, wlen; 3641556Srgrimes char buf[8 * 1024]; 3651556Srgrimes 3661556Srgrimes fd = -1; 3671556Srgrimes if (sbp == NULL) { 3681556Srgrimes if (lstat(file, &sb)) 3691556Srgrimes goto err; 3701556Srgrimes sbp = &sb; 3711556Srgrimes } 3721556Srgrimes if (!S_ISREG(sbp->st_mode)) 3731556Srgrimes return; 3741556Srgrimes if ((fd = open(file, O_WRONLY, 0)) == -1) 3751556Srgrimes goto err; 3761556Srgrimes 3771556Srgrimes#define PASS(byte) { \ 3781556Srgrimes memset(buf, byte, sizeof(buf)); \ 3791556Srgrimes for (len = sbp->st_size; len > 0; len -= wlen) { \ 3801556Srgrimes wlen = len < sizeof(buf) ? len : sizeof(buf); \ 3811556Srgrimes if (write(fd, buf, wlen) != wlen) \ 3821556Srgrimes goto err; \ 3831556Srgrimes } \ 3841556Srgrimes} 3851556Srgrimes PASS(0xff); 3861556Srgrimes if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 3871556Srgrimes goto err; 3881556Srgrimes PASS(0x00); 3891556Srgrimes if (fsync(fd) || lseek(fd, (off_t)0, SEEK_SET)) 3901556Srgrimes goto err; 3911556Srgrimes PASS(0xff); 3921556Srgrimes if (!fsync(fd) && !close(fd)) 3931556Srgrimes return; 3941556Srgrimes 3951556Srgrimeserr: eval = 1; 3961556Srgrimes warn("%s", file); 3971556Srgrimes} 3981556Srgrimes 3991556Srgrimes 4001556Srgrimesint 4011556Srgrimescheck(path, name, sp) 4021556Srgrimes char *path, *name; 4031556Srgrimes struct stat *sp; 4041556Srgrimes{ 4051556Srgrimes int ch, first; 4067798Sache char modep[15], flagsp[128]; 4071556Srgrimes 4081556Srgrimes /* Check -i first. */ 4091556Srgrimes if (iflag) 4101556Srgrimes (void)fprintf(stderr, "remove %s? ", path); 4111556Srgrimes else { 4121556Srgrimes /* 4131556Srgrimes * If it's not a symbolic link and it's unwritable and we're 4141556Srgrimes * talking to a terminal, ask. Symbolic links are excluded 4151556Srgrimes * because their permissions are meaningless. Check stdin_ok 4161556Srgrimes * first because we may not have stat'ed the file. 4171556Srgrimes */ 4187798Sache if (!stdin_ok || S_ISLNK(sp->st_mode) || 41920421Ssteve (!access(name, W_OK) && 4207798Sache !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 42120421Ssteve (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))) 4221556Srgrimes return (1); 4231556Srgrimes strmode(sp->st_mode, modep); 4247798Sache strcpy(flagsp, flags_to_string(sp->st_flags, NULL)); 4257798Sache if (*flagsp) 4267798Sache strcat(flagsp, " "); 4277798Sache (void)fprintf(stderr, "override %s%s%s/%s %sfor %s? ", 4281556Srgrimes modep + 1, modep[9] == ' ' ? "" : " ", 4291556Srgrimes user_from_uid(sp->st_uid, 0), 4307798Sache group_from_gid(sp->st_gid, 0), 4317798Sache *flagsp ? flagsp : "", 4327798Sache path); 4331556Srgrimes } 4341556Srgrimes (void)fflush(stderr); 4351556Srgrimes 4361556Srgrimes first = ch = getchar(); 4371556Srgrimes while (ch != '\n' && ch != EOF) 4381556Srgrimes ch = getchar(); 43914409Swosch return (first == 'y' || first == 'Y'); 4401556Srgrimes} 4411556Srgrimes 4422927Sphk#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 4431556Srgrimesvoid 4441556Srgrimescheckdot(argv) 4451556Srgrimes char **argv; 4461556Srgrimes{ 4471556Srgrimes char *p, **save, **t; 4481556Srgrimes int complained; 4491556Srgrimes 4501556Srgrimes complained = 0; 4511556Srgrimes for (t = argv; *t;) { 4521556Srgrimes if ((p = strrchr(*t, '/')) != NULL) 4531556Srgrimes ++p; 4541556Srgrimes else 4551556Srgrimes p = *t; 4561556Srgrimes if (ISDOT(p)) { 4571556Srgrimes if (!complained++) 4581556Srgrimes warnx("\".\" and \"..\" may not be removed"); 4591556Srgrimes eval = 1; 46020421Ssteve for (save = t; (t[0] = t[1]) != NULL; ++t) 46120421Ssteve continue; 4621556Srgrimes t = save; 4631556Srgrimes } else 4641556Srgrimes ++t; 4651556Srgrimes } 4661556Srgrimes} 4671556Srgrimes 4681556Srgrimesvoid 4691556Srgrimesusage() 4701556Srgrimes{ 4711556Srgrimes 47220421Ssteve#ifdef BSD4_4_LITE 47314409Swosch (void)fprintf(stderr, "usage: rm [-f | -i] [-dPRr] file ...\n"); 47420421Ssteve#else 47520421Ssteve (void)fprintf(stderr, "usage: rm [-f | -i] [-dPRrW] file ...\n"); 47620421Ssteve#endif 4771556Srgrimes exit(1); 4781556Srgrimes} 479