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