rm.c revision 50872
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. 321556Srgrimes */ 331556Srgrimes 341556Srgrimes#ifndef lint 3527959Sstevestatic const char copyright[] = 361556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\ 371556Srgrimes The Regents of the University of California. All rights reserved.\n"; 3827964Ssteve#endif /* not lint */ 3927964Ssteve 4027964Ssteve#ifndef lint 4127964Ssteve#if 0 4227964Sstevestatic char sccsid[] = "@(#)rm.c 8.5 (Berkeley) 4/18/94"; 4327964Ssteve#else 4427959Sstevestatic const char rcsid[] = 4550471Speter "$FreeBSD: head/bin/rm/rm.c 50872 1999-09-04 03:40:10Z mharo $"; 4627964Ssteve#endif 471556Srgrimes#endif /* not lint */ 481556Srgrimes 491556Srgrimes#include <sys/types.h> 501556Srgrimes#include <sys/stat.h> 5147584Skris#include <sys/param.h> 5247584Skris#include <sys/mount.h> 531556Srgrimes 541556Srgrimes#include <err.h> 551556Srgrimes#include <errno.h> 561556Srgrimes#include <fcntl.h> 571556Srgrimes#include <fts.h> 581556Srgrimes#include <stdio.h> 591556Srgrimes#include <stdlib.h> 601556Srgrimes#include <string.h> 6150539Smharo#include <sysexits.h> 621556Srgrimes#include <unistd.h> 631556Srgrimes 647798Sacheextern char *flags_to_string __P((u_long, char *)); 657798Sache 6650872Smharoint dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok; 677798Sacheuid_t uid; 681556Srgrimes 691556Srgrimesint check __P((char *, char *, struct stat *)); 701556Srgrimesvoid checkdot __P((char **)); 711556Srgrimesvoid rm_file __P((char **)); 721556Srgrimesvoid rm_overwrite __P((char *, struct stat *)); 731556Srgrimesvoid rm_tree __P((char **)); 741556Srgrimesvoid usage __P((void)); 751556Srgrimes 761556Srgrimes/* 771556Srgrimes * rm -- 781556Srgrimes * This rm is different from historic rm's, but is expected to match 791556Srgrimes * POSIX 1003.2 behavior. The most visible difference is that -f 801556Srgrimes * has two specific effects now, ignore non-existent files and force 811556Srgrimes * file removal. 821556Srgrimes */ 831556Srgrimesint 841556Srgrimesmain(argc, argv) 851556Srgrimes int argc; 861556Srgrimes char *argv[]; 871556Srgrimes{ 881556Srgrimes int ch, rflag; 891556Srgrimes 9020421Ssteve Pflag = rflag = 0; 9150872Smharo while ((ch = getopt(argc, argv, "dfiPRrvW")) != -1) 921556Srgrimes switch(ch) { 931556Srgrimes case 'd': 941556Srgrimes dflag = 1; 951556Srgrimes break; 961556Srgrimes case 'f': 971556Srgrimes fflag = 1; 981556Srgrimes iflag = 0; 991556Srgrimes break; 1001556Srgrimes case 'i': 1011556Srgrimes fflag = 0; 1021556Srgrimes iflag = 1; 1031556Srgrimes break; 1041556Srgrimes case 'P': 1051556Srgrimes Pflag = 1; 1061556Srgrimes break; 1071556Srgrimes case 'R': 1081556Srgrimes case 'r': /* Compatibility. */ 1091556Srgrimes rflag = 1; 1101556Srgrimes break; 11150872Smharo case 'v': 11250872Smharo vflag = 1; 11350872Smharo break; 11420421Ssteve case 'W': 11520421Ssteve Wflag = 1; 11620421Ssteve break; 1171556Srgrimes default: 1181556Srgrimes usage(); 1191556Srgrimes } 1201556Srgrimes argc -= optind; 1211556Srgrimes argv += optind; 1221556Srgrimes 12344282Sjkh if (argc < 1) { 12444282Sjkh if (fflag) 12544282Sjkh return 0; 1261556Srgrimes usage(); 12744282Sjkh } 1281556Srgrimes 1291556Srgrimes checkdot(argv); 1307798Sache uid = geteuid(); 1311556Srgrimes 13220421Ssteve if (*argv) { 13320421Ssteve stdin_ok = isatty(STDIN_FILENO); 13420421Ssteve 13520421Ssteve if (rflag) 13620421Ssteve rm_tree(argv); 13720421Ssteve else 13820421Ssteve rm_file(argv); 13920421Ssteve } 14020421Ssteve 1411556Srgrimes exit (eval); 1421556Srgrimes} 1431556Srgrimes 1441556Srgrimesvoid 1451556Srgrimesrm_tree(argv) 1461556Srgrimes char **argv; 1471556Srgrimes{ 1481556Srgrimes FTS *fts; 1491556Srgrimes FTSENT *p; 1501556Srgrimes int needstat; 15120421Ssteve int flags; 1527798Sache int rval; 15350872Smharo int e; 1541556Srgrimes 1551556Srgrimes /* 1561556Srgrimes * Remove a file hierarchy. If forcing removal (-f), or interactive 1571556Srgrimes * (-i) or can't ask anyway (stdin_ok), don't stat the file. 1581556Srgrimes */ 15920421Ssteve needstat = !uid || (!fflag && !iflag && stdin_ok); 1601556Srgrimes 1611556Srgrimes /* 1621556Srgrimes * If the -i option is specified, the user can skip on the pre-order 1631556Srgrimes * visit. The fts_number field flags skipped directories. 1641556Srgrimes */ 1651556Srgrimes#define SKIPPED 1 1661556Srgrimes 16720421Ssteve flags = FTS_PHYSICAL | FTS_NOCHDIR; 16820421Ssteve if (!needstat) 16920421Ssteve flags |= FTS_NOSTAT; 17020421Ssteve if (Wflag) 17120421Ssteve flags |= FTS_WHITEOUT; 17220421Ssteve if (!(fts = fts_open(argv, flags, (int (*)())NULL))) 1731556Srgrimes err(1, NULL); 1741556Srgrimes while ((p = fts_read(fts)) != NULL) { 1751556Srgrimes switch (p->fts_info) { 1761556Srgrimes case FTS_DNR: 1771556Srgrimes if (!fflag || p->fts_errno != ENOENT) { 1781556Srgrimes warnx("%s: %s", 1791556Srgrimes p->fts_path, strerror(p->fts_errno)); 1801556Srgrimes eval = 1; 1811556Srgrimes } 1821556Srgrimes continue; 1831556Srgrimes case FTS_ERR: 1841556Srgrimes errx(1, "%s: %s", p->fts_path, strerror(p->fts_errno)); 1851556Srgrimes case FTS_NS: 1861556Srgrimes /* 1871556Srgrimes * FTS_NS: assume that if can't stat the file, it 1881556Srgrimes * can't be unlinked. 1891556Srgrimes */ 1901556Srgrimes if (!needstat) 1911556Srgrimes break; 1921556Srgrimes if (!fflag || p->fts_errno != ENOENT) { 1931556Srgrimes warnx("%s: %s", 1941556Srgrimes p->fts_path, strerror(p->fts_errno)); 1951556Srgrimes eval = 1; 1961556Srgrimes } 1971556Srgrimes continue; 1981556Srgrimes case FTS_D: 1991556Srgrimes /* Pre-order: give user chance to skip. */ 20020421Ssteve if (!fflag && !check(p->fts_path, p->fts_accpath, 2011556Srgrimes p->fts_statp)) { 2021556Srgrimes (void)fts_set(fts, p, FTS_SKIP); 2031556Srgrimes p->fts_number = SKIPPED; 2041556Srgrimes } 2057798Sache else if (!uid && 2067798Sache (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2077798Sache !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 2087798Sache chflags(p->fts_accpath, 2097798Sache p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)) < 0) 2107798Sache goto err; 2111556Srgrimes continue; 2121556Srgrimes case FTS_DP: 2131556Srgrimes /* Post-order: see if user skipped. */ 2141556Srgrimes if (p->fts_number == SKIPPED) 2151556Srgrimes continue; 2161556Srgrimes break; 21720421Ssteve default: 21820421Ssteve if (!fflag && 21920421Ssteve !check(p->fts_path, p->fts_accpath, p->fts_statp)) 22020421Ssteve continue; 2211556Srgrimes } 2221556Srgrimes 2237798Sache rval = 0; 2247798Sache if (!uid && 2257798Sache (p->fts_statp->st_flags & (UF_APPEND|UF_IMMUTABLE)) && 2267798Sache !(p->fts_statp->st_flags & (SF_APPEND|SF_IMMUTABLE))) 2277798Sache rval = chflags(p->fts_accpath, 2287798Sache p->fts_statp->st_flags &= ~(UF_APPEND|UF_IMMUTABLE)); 2297798Sache if (!rval) { 2307798Sache /* 2317798Sache * If we can't read or search the directory, may still be 2327798Sache * able to remove it. Don't print out the un{read,search}able 2337798Sache * message unless the remove fails. 2347798Sache */ 23520421Ssteve switch (p->fts_info) { 23620421Ssteve case FTS_DP: 23720421Ssteve case FTS_DNR: 23850872Smharo e = rmdir(p->fts_accpath); 23950872Smharo if (e == 0 || (fflag && errno == ENOENT)) { 24050539Smharo if (e == 0 && vflag) 24150872Smharo (void)printf("%s\n", 24250872Smharo p->fts_accpath); 2431556Srgrimes continue; 24450539Smharo } 24520421Ssteve break; 24620421Ssteve 24720421Ssteve case FTS_W: 24850872Smharo e = undelete(p->fts_accpath); 24950872Smharo if (e == 0 || (fflag && errno == ENOENT)) { 25050539Smharo if (e == 0 && vflag) 25150872Smharo (void)printf("%s\n", 25250872Smharo p->fts_accpath); 25320421Ssteve continue; 25450539Smharo } 25520421Ssteve break; 25620421Ssteve 25720421Ssteve default: 2587798Sache if (Pflag) 2597798Sache rm_overwrite(p->fts_accpath, NULL); 26050872Smharo e = unlink(p->fts_accpath); 26150872Smharo if (e == 0 || (fflag && errno == ENOENT)) { 26250539Smharo if (e == 0 && vflag) 26350872Smharo (void)printf("%s\n", 26450872Smharo p->fts_accpath); 2657798Sache continue; 26650539Smharo } 2677798Sache } 2681556Srgrimes } 2697798Sacheerr: 2701556Srgrimes warn("%s", p->fts_path); 2711556Srgrimes eval = 1; 2721556Srgrimes } 2731556Srgrimes if (errno) 2741556Srgrimes err(1, "fts_read"); 2751556Srgrimes} 2761556Srgrimes 2771556Srgrimesvoid 2781556Srgrimesrm_file(argv) 2791556Srgrimes char **argv; 2801556Srgrimes{ 2811556Srgrimes struct stat sb; 28220421Ssteve int rval; 2831556Srgrimes char *f; 2841556Srgrimes 2851556Srgrimes /* 2861556Srgrimes * Remove a file. POSIX 1003.2 states that, by default, attempting 2871556Srgrimes * to remove a directory is an error, so must always stat the file. 2881556Srgrimes */ 2891556Srgrimes while ((f = *argv++) != NULL) { 2901556Srgrimes /* Assume if can't stat the file, can't unlink it. */ 2911556Srgrimes if (lstat(f, &sb)) { 29220421Ssteve if (Wflag) { 29320421Ssteve sb.st_mode = S_IFWHT|S_IWUSR|S_IRUSR; 29420421Ssteve } else { 29520421Ssteve if (!fflag || errno != ENOENT) { 29620421Ssteve warn("%s", f); 29720421Ssteve eval = 1; 29820421Ssteve } 29920421Ssteve continue; 3001556Srgrimes } 30120421Ssteve } else if (Wflag) { 30220421Ssteve warnx("%s: %s", f, strerror(EEXIST)); 30320421Ssteve eval = 1; 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 if (!fflag && !S_ISWHT(sb.st_mode) && !check(f, f, &sb)) 3131556Srgrimes continue; 3147798Sache rval = 0; 3157798Sache if (!uid && 3167798Sache (sb.st_flags & (UF_APPEND|UF_IMMUTABLE)) && 3177798Sache !(sb.st_flags & (SF_APPEND|SF_IMMUTABLE))) 3187798Sache rval = chflags(f, sb.st_flags & ~(UF_APPEND|UF_IMMUTABLE)); 3197798Sache if (!rval) { 32020421Ssteve if (S_ISWHT(sb.st_mode)) 32120421Ssteve rval = undelete(f); 32220421Ssteve else if (S_ISDIR(sb.st_mode)) 3237798Sache rval = rmdir(f); 3247798Sache else { 3257798Sache if (Pflag) 3267798Sache rm_overwrite(f, &sb); 3277798Sache rval = unlink(f); 3287798Sache } 3291556Srgrimes } 3301556Srgrimes if (rval && (!fflag || errno != ENOENT)) { 3311556Srgrimes warn("%s", f); 3321556Srgrimes eval = 1; 3331556Srgrimes } 33450539Smharo if (vflag) 33550539Smharo (void)printf("%s\n", f); 3361556Srgrimes } 3371556Srgrimes} 3381556Srgrimes 3391556Srgrimes/* 3401556Srgrimes * rm_overwrite -- 3411556Srgrimes * Overwrite the file 3 times with varying bit patterns. 3421556Srgrimes * 3431556Srgrimes * XXX 3441556Srgrimes * This is a cheap way to *really* delete files. Note that only regular 3451556Srgrimes * files are deleted, directories (and therefore names) will remain. 3461556Srgrimes * Also, this assumes a fixed-block file system (like FFS, or a V7 or a 3471556Srgrimes * System V file system). In a logging file system, you'll have to have 3481556Srgrimes * kernel support. 3491556Srgrimes */ 3501556Srgrimesvoid 3511556Srgrimesrm_overwrite(file, sbp) 3521556Srgrimes char *file; 3531556Srgrimes struct stat *sbp; 3541556Srgrimes{ 3551556Srgrimes struct stat sb; 35647584Skris struct statfs fsb; 3571556Srgrimes off_t len; 35847584Skris int bsize, fd, wlen; 35947584Skris char *buf = NULL; 3601556Srgrimes 3611556Srgrimes fd = -1; 3621556Srgrimes if (sbp == NULL) { 3631556Srgrimes if (lstat(file, &sb)) 3641556Srgrimes goto err; 3651556Srgrimes sbp = &sb; 3661556Srgrimes } 3671556Srgrimes if (!S_ISREG(sbp->st_mode)) 3681556Srgrimes return; 3691556Srgrimes if ((fd = open(file, O_WRONLY, 0)) == -1) 3701556Srgrimes goto err; 37147584Skris if (fstatfs(fd, &fsb) == -1) 37247584Skris goto err; 37347584Skris bsize = MAX(fsb.f_iosize, 1024); 37447584Skris if ((buf = malloc(bsize)) == NULL) 37547584Skris err(1, "malloc"); 3761556Srgrimes 3771556Srgrimes#define PASS(byte) { \ 37847584Skris memset(buf, byte, bsize); \ 3791556Srgrimes for (len = sbp->st_size; len > 0; len -= wlen) { \ 38047584Skris wlen = len < bsize ? len : bsize; \ 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); 39247584Skris if (!fsync(fd) && !close(fd)) { 39347584Skris free(buf); 3941556Srgrimes return; 39547584Skris } 3961556Srgrimes 3971556Srgrimeserr: eval = 1; 39847584Skris if (buf) 39947584Skris free(buf); 4001556Srgrimes warn("%s", file); 4011556Srgrimes} 4021556Srgrimes 4031556Srgrimes 4041556Srgrimesint 4051556Srgrimescheck(path, name, sp) 4061556Srgrimes char *path, *name; 4071556Srgrimes struct stat *sp; 4081556Srgrimes{ 4091556Srgrimes int ch, first; 4107798Sache char modep[15], flagsp[128]; 4111556Srgrimes 4121556Srgrimes /* Check -i first. */ 4131556Srgrimes if (iflag) 4141556Srgrimes (void)fprintf(stderr, "remove %s? ", path); 4151556Srgrimes else { 4161556Srgrimes /* 4171556Srgrimes * If it's not a symbolic link and it's unwritable and we're 4181556Srgrimes * talking to a terminal, ask. Symbolic links are excluded 4191556Srgrimes * because their permissions are meaningless. Check stdin_ok 4201556Srgrimes * first because we may not have stat'ed the file. 4211556Srgrimes */ 4227798Sache if (!stdin_ok || S_ISLNK(sp->st_mode) || 42320421Ssteve (!access(name, W_OK) && 4247798Sache !(sp->st_flags & (SF_APPEND|SF_IMMUTABLE)) && 42520421Ssteve (!(sp->st_flags & (UF_APPEND|UF_IMMUTABLE)) || !uid))) 4261556Srgrimes return (1); 4271556Srgrimes strmode(sp->st_mode, modep); 4287798Sache strcpy(flagsp, flags_to_string(sp->st_flags, NULL)); 4297798Sache if (*flagsp) 4307798Sache strcat(flagsp, " "); 4317798Sache (void)fprintf(stderr, "override %s%s%s/%s %sfor %s? ", 4321556Srgrimes modep + 1, modep[9] == ' ' ? "" : " ", 4331556Srgrimes user_from_uid(sp->st_uid, 0), 4347798Sache group_from_gid(sp->st_gid, 0), 4357798Sache *flagsp ? flagsp : "", 4367798Sache path); 4371556Srgrimes } 4381556Srgrimes (void)fflush(stderr); 4391556Srgrimes 4401556Srgrimes first = ch = getchar(); 4411556Srgrimes while (ch != '\n' && ch != EOF) 4421556Srgrimes ch = getchar(); 44314409Swosch return (first == 'y' || first == 'Y'); 4441556Srgrimes} 4451556Srgrimes 4462927Sphk#define ISDOT(a) ((a)[0] == '.' && (!(a)[1] || ((a)[1] == '.' && !(a)[2]))) 4471556Srgrimesvoid 4481556Srgrimescheckdot(argv) 4491556Srgrimes char **argv; 4501556Srgrimes{ 4511556Srgrimes char *p, **save, **t; 4521556Srgrimes int complained; 4531556Srgrimes 4541556Srgrimes complained = 0; 4551556Srgrimes for (t = argv; *t;) { 4561556Srgrimes if ((p = strrchr(*t, '/')) != NULL) 4571556Srgrimes ++p; 4581556Srgrimes else 4591556Srgrimes p = *t; 4601556Srgrimes if (ISDOT(p)) { 4611556Srgrimes if (!complained++) 4621556Srgrimes warnx("\".\" and \"..\" may not be removed"); 4631556Srgrimes eval = 1; 46420421Ssteve for (save = t; (t[0] = t[1]) != NULL; ++t) 46520421Ssteve continue; 4661556Srgrimes t = save; 4671556Srgrimes } else 4681556Srgrimes ++t; 4691556Srgrimes } 4701556Srgrimes} 4711556Srgrimes 4721556Srgrimesvoid 4731556Srgrimesusage() 4741556Srgrimes{ 47550872Smharo (void)fprintf(stderr, "usage: rm [-f | -i] [-dPRrvW] file ...\n"); 47650539Smharo exit(EX_USAGE); 4771556Srgrimes} 478