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