Print this page
3965 find does not support -delete option

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/find/find.c
          +++ new/usr/src/cmd/find/find.c
↓ open down ↓ 79 lines elided ↑ open up ↑
  80   80   * in sys/acl.h
  81   81   */
  82   82  
  83   83  enum Command
  84   84  {
  85   85          PRINT,
  86   86          ACL, AMIN, AND, ATIME, CMIN, CPIO, CSIZE, CTIME, DEPTH, EXEC, F_GROUP,
  87   87          F_GROUPACL, F_USER, F_USERACL, FOLLOW, FSTYPE, INAME, INUM, IPATH,
  88   88          IREGEX, LINKS, LOCAL, LPAREN, LS, MAXDEPTH, MINDEPTH, MMIN, MOUNT,
  89   89          MTIME, NAME, NCPIO, NEWER, NOGRP, NOT, NOUSER, OK, OR, PATH, PERM,
  90      -        PRINT0, PRUNE, REGEX, RPAREN, SIZE, TYPE, VARARGS, XATTR
       90 +        PRINT0, PRUNE, REGEX, RPAREN, SIZE, TYPE, VARARGS, XATTR, DELETE
  91   91  };
  92   92  
  93   93  enum Type
  94   94  {
  95   95          Unary, Id, Num, Str, Exec, Cpio, Op
  96   96  };
  97   97  
  98   98  struct Args
  99   99  {
 100  100          char            name[10];
↓ open down ↓ 11 lines elided ↑ open up ↑
 112  112          ")",            RPAREN,         Unary,
 113  113          "-a",           AND,            Op,
 114  114          "-acl",         ACL,            Unary,
 115  115          "-amin",        AMIN,           Num,
 116  116          "-and",         AND,            Op,
 117  117          "-atime",       ATIME,          Num,
 118  118          "-cmin",        CMIN,           Num,
 119  119          "-cpio",        CPIO,           Cpio,
 120  120          "-ctime",       CTIME,          Num,
 121  121          "-depth",       DEPTH,          Unary,
      122 +        "-delete",      DELETE,         Unary,
 122  123          "-exec",        EXEC,           Exec,
 123  124          "-follow",      FOLLOW,         Unary,
 124  125          "-fstype",      FSTYPE,         Str,
 125  126          "-group",       F_GROUP,        Num,
 126  127          "-groupacl",    F_GROUPACL,     Num,
 127  128          "-iname",       INAME,          Str,
 128  129          "-inum",        INUM,           Num,
 129  130          "-ipath",       IPATH,          Str,
 130  131          "-iregex",      IREGEX,         Str,
 131  132          "-links",       LINKS,          Num,
↓ open down ↓ 65 lines elided ↑ open up ↑
 197  198          char            *nextstr;
 198  199          char            **firstvar;
 199  200          char            **nextvar;
 200  201          char            *arglist[1];
 201  202  };
 202  203  
 203  204  
 204  205  static int              compile();
 205  206  static int              execute();
 206  207  static int              doexec(char *, char **, int *);
      208 +static int              dodelete(char *, struct stat *, struct FTW *);
 207  209  static struct Args      *lookup();
 208  210  static int              ok();
 209  211  static void             usage(void)     __NORETURN;
 210  212  static struct Arglist   *varargs();
 211  213  static int              list();
 212  214  static char             *getgroup();
 213  215  static FILE             *cmdopen();
 214  216  static int              cmdclose();
 215  217  static char             *getshell();
 216  218  static void             init_remote_fs();
↓ open down ↓ 305 lines elided ↑ open up ↑
 522  524  
 523  525                  case FOLLOW:
 524  526                          walkflags &= ~FTW_PHYS;
 525  527                          break;
 526  528                  case MOUNT:
 527  529                          walkflags |= FTW_MOUNT;
 528  530                          break;
 529  531                  case DEPTH:
 530  532                          walkflags |= FTW_DEPTH;
 531  533                          break;
      534 +                case DELETE:
      535 +                        walkflags |= (FTW_DEPTH | FTW_PHYS);
      536 +                        walkflags &= ~FTW_CHDIR;
      537 +                        (*actionp)++;
      538 +                        break;
 532  539  
 533  540                  case LOCAL:
 534  541                          np->first.l = 0L;
 535  542                          np->first.ll = 0LL;
 536  543                          np->second.i = '+';
 537  544                          /*
 538  545                           * Make it compatible to df -l for
 539  546                           * future enhancement. So, anything
 540  547                           * that is not remote, then it is
 541  548                           * local.
↓ open down ↓ 418 lines elided ↑ open up ↑
 960  967                                  val = (l < np->first.l);
 961  968                          else
 962  969                                  val = (l == np->first.l);
 963  970                          break;
 964  971                  case OK:
 965  972                          val = ok(name, np->first.ap);
 966  973                          break;
 967  974                  case EXEC:
 968  975                          val = doexec(name, np->first.ap, NULL);
 969  976                          break;
      977 +                case DELETE:
      978 +                        val = dodelete(name, statb, state);
      979 +                        break;
 970  980  
 971  981                  case VARARGS: {
 972  982                          struct Arglist *ap = np->first.vp;
 973  983                          char *cp;
 974  984                          cp = ap->nextstr - (strlen(name)+1);
 975  985                          if (cp >= (char *)(ap->nextvar+3)) {
 976  986                                  /* there is room just copy the name */
 977  987                                  val = 1;
 978  988                                  (void) strcpy(cp, name);
 979  989                                  *ap->nextvar++ = cp;
↓ open down ↓ 331 lines elided ↑ open up ↑
1311 1321                                  av[-1] = dummyarg;
1312 1322                  }
1313 1323          }
1314 1324  
1315 1325          if (r && exitcode != NULL)
1316 1326                  *exitcode = 3; /* use to indicate error in cmd invocation */
1317 1327  
1318 1328          return (!r);
1319 1329  }
1320 1330  
     1331 +static int
     1332 +dodelete(char *name, struct stat *statb, struct FTW *state)
     1333 +{
     1334 +        char *fn;
     1335 +        int rc = 0;
1321 1336  
     1337 +        /* restrict symlinks */
     1338 +        if ((walkflags & FTW_PHYS) == 0) {
     1339 +                (void) fprintf(stderr,
     1340 +                    gettext("-delete is not allowed when symlinks are "
     1341 +                    "followed.\n"));
     1342 +                return (1);
     1343 +        }
     1344 +
     1345 +        fn = name + state->base;
     1346 +        if (strcmp(fn, ".") == 0) {
     1347 +                /* nothing to do */
     1348 +                return (1);
     1349 +        }
     1350 +
     1351 +        if (strchr(fn, '/') != NULL) {
     1352 +                (void) fprintf(stderr,
     1353 +                    gettext("-delete with relative path is unsafe."));
     1354 +                return (1);
     1355 +        }
     1356 +
     1357 +        if (S_ISDIR(statb->st_mode)) {
     1358 +                /* delete directory */
     1359 +                rc = rmdir(name);
     1360 +        } else {
     1361 +                /* delete file */
     1362 +                rc = unlink(name);
     1363 +        }
     1364 +
     1365 +        if (rc < 0) {
     1366 +                /* operation failed */
     1367 +                (void) fprintf(stderr, gettext("delete failed %s: %s\n"),
     1368 +                    name, strerror(errno));
     1369 +                return (1);
     1370 +        }
     1371 +
     1372 +        return (1);
     1373 +}
     1374 +
1322 1375  /*
1323 1376   *  Table lookup routine
1324 1377   */
1325 1378  static struct Args *
1326 1379  lookup(word)
1327 1380  char *word;
1328 1381  {
1329 1382          struct Args *argp = commands;
1330 1383          int second;
1331 1384          if (word == 0 || *word == 0)
↓ open down ↓ 897 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX