aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/find.c
blob: 5159172a4538cf775767b0266b062b17d89a4726 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* find.c - Search directories for matching files.
 *
 * Copyright 2014 Rob Landley <rob@landley.net>
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.c
 *
 * Our "unspecified" behavior for no paths is to use "."
 * Parentheses can only stack 4096 deep
 * Not treating two {} as an error, but only using last

USE_FIND(NEWTOY(find, "?^HL", TOYFLAG_USR|TOYFLAG_BIN))

config FIND
  bool "find"
  default n
  help
    usage: find [-HL] [DIR...] [<options>]

    Search directories for matching files.
    Default: search "." match all -print all matches.

    -H  Follow command line symlinks         -L  Follow all symlinks

    Match filters:
    -name  PATTERN filename with wildcards   -iname   case insensitive -name
    -path  PATTERN path name with wildcards  -ipath   case insensitive -path
    -user  UNAME   belongs to user           -nouser  belongs to unknown user
    -group GROUP   belongs to group          -nogroup belongs to unknown group
    -perm  [-]MODE permissons (-=at least)   -prune   ignore contents of dir
    -size  N[c]    512 byte blocks (c=bytes) -xdev    stay in this filesystem
    -links N       hardlink count            -atime N accessed N days ago
    -ctime N       created N days ago        -mtime N modified N days ago
    -type [bcdflps] (block, char, dir, file, symlink, pipe, socket)

    Numbers N may be prefixed by a - (less than) or + (greater than):

    Combine matches with:
    !, -a, -o, ( )    not, and, or, group expressions

    Actions:
    -print   Print match with newline  -print0    Print match with null
    -exec    Run command with path     -execdir   Run command in file's dir
    -ok      Ask before exec           -okdir     Ask before execdir

    Commands substitute "{}" with matched file. End with ";" to run each file,
    or "+" (next argument after "{}") to collect and run with multiple files.
*/

// find . ! \( -name blah -print \)
// find . -o
// find -type f

// pending issues:
// old false -a ! new false does not yield true.
// 
// -user -group -newer evaluate once and save result (where?)
// add -print if no action (-exec, -ok, -print)
// find . -print -xdev (should xdev before print)
// -exec {} + accepts any + after {}, not just immediately after. ";" optional

#define FOR_find
#include "toys.h"

GLOBALS(
  char **filter;
  struct double_list *argdata;
  int topdir, xdev, depth, envsize;
  time_t now;
)

// None of this can go in TT because you can have more than one -exec
struct exec_range {
  char *next, *prev;

  int dir, plus, arglen, argsize, curly, namecount, namesize;
  char **argstart;
  struct double_list *names;
};

// Perform pending -exec (if any)
static int flush_exec(struct dirtree *new, struct exec_range *aa)
{
  struct double_list **dl;
  char **newargs;
  int rc;

  if (!aa->namecount) return 0;

  if (aa->dir && new->parent) dl = (void *)&new->parent->extra;
  else dl = &aa->names;
  dlist_terminate(dl);

  // switch to directory for -execdir, or back to top if we have an -execdir
  // _and_ a normal -exec, or are at top of tree in -execdir
  if (aa->dir && new->parent) fchdir(new->parent->data);
  else if (TT.topdir != -1) fchdir(TT.topdir);

  // execdir: accumulated execs in this directory's children.
  newargs = xmalloc(sizeof(char *)*(aa->arglen+aa->namecount+1));
  if (!aa->curly) {
    memcpy(newargs, aa->argstart+1, sizeof(char *)*aa->arglen);
    newargs[aa->arglen] = 0;
  } else {
    struct double_list *dl2 = *dl;
    int pos = aa->curly, rest = aa->arglen - aa->curly;

    // Collate argument list
    memcpy(newargs, aa->argstart+1, sizeof(char *)*pos);
    for (dl2 = *dl; dl2; dl2 = dl2->next) newargs[pos++] = dl2->data;
    rest = aa->arglen - aa->curly;
    memcpy(newargs+pos, aa->argstart+aa->curly+1,
      sizeof(char *)*(rest-1));
    newargs[pos+rest] = 0;
  }

  rc = xpclose(xpopen(newargs, 0), 0);

  llist_traverse(dl, llist_free_double);

  return rc;
}

// Return numeric value with explicit sign
static int compare_numsign(long val, long units, char *str)
{
  char sign = 0;
  long myval;

  if (*str == '+' || *str == '-') sign = *(str++);
  else if (!isdigit(*str)) error_exit("%s not [+-]N", str);
  myval = atolx(str);
  if (units && isdigit(str[strlen(str)-1])) myval *= units;

  if (sign == '+') return val > myval;
  if (sign == '-') return val < myval;
  return val == myval;
}

static void do_print(struct dirtree *new, char c)
{
  char *s=dirtree_path(new, 0);

  xprintf("%s%c", s, c);
  free(s);
}

void todo_store_argument(void)
{
  error_exit("NOP");
}

char *strlower(char *s)
{
  char *new;

  if (!CFG_TOYBOX_I18N) {
    new = xstrdup(s);
    for (; *s; s++) *(new++) = tolower(*s);
  } else {
    // I can't guarantee the string _won't_ expand during reencoding, so...?
    new = xmalloc(strlen(s)*2+1);

    while (*s) {
      wchar_t c;
      int len = mbrtowc(&c, s, MB_CUR_MAX, 0);

      if (len < 1) *(new++) = *(s++);
      else {
        // squash title case too
        c = towlower(c);

        // if we had a valid utf8 sequence, convert it to lower case, and can't
        // encode back to utf8, something is wrong with your libc. But just
        // in case somebody finds an exploit...
        len = wcrtomb(s, c, 0);
        if (len < 1) error_exit("bad utf8 %x", c);
        s += len;
      }
    }
  }

  return new;
}

// Call this with 0 for first pass argument parsing and syntax checking (which
// populates argdata). Later commands traverse argdata (in order) when they
// need "do once" results.
static int do_find(struct dirtree *new)
{
  int pcount = 0, print = 0, not = 0, active = !!new, test = active, recurse;
  struct double_list *argdata = TT.argdata;
  char *s, **ss;

  recurse = DIRTREE_COMEAGAIN|((toys.optflags&FLAG_L) ? DIRTREE_SYMFOLLOW : 0);

  // skip . and .. below topdir, handle -xdev and -depth
  if (new) {
    if (new->parent) {
      if (!dirtree_notdotdot(new)) return 0;
      if (TT.xdev && new->st.st_dev != new->parent->st.st_dev) return 0;
    }
    if (S_ISDIR(new->st.st_mode)) {
      if (!new->again) {
        if (TT.depth) return recurse;
      } else {
        struct double_list *dl;

        if (TT.topdir != -1)
          for (dl = TT.argdata; dl; dl = dl->next)
            if (dl->prev == (void *)1 || !new->parent)
              toys.exitval |= flush_exec(new, (void *)dl);

        return 0;
      }
    }
  }

  // pcount: parentheses stack depth (using toybuf bytes, 4096 max depth)
  // test: result of most recent test
  // active: if 0 don't perform tests
  // not: a pending ! applies to this test (only set if performing tests)
  // print: saw one of print/ok/exec, no need for default -print

  if (TT.filter) for (ss = TT.filter; *ss; ss++) {
    int check = active && test;

    s = *ss;

    // handle ! ( ) using toybuf as a stack
    if (*s != '-') {
      if (s[1]) goto error;

      if (*s == '!') {
        // Don't invert if we're not making a decision
        if (check) not = !not;

      // Save old "not" and "active" on toybuf stack.
      // Deactivate this parenthetical if !test
      // Note: test value should never change while !active
      } else if (*s == '(') {
        if (pcount == sizeof(toybuf)) goto error;
        toybuf[pcount++] = not+(active<<1);
        if (!check) active = 0;
        not = 0;

      // Pop status, apply deferred not to test
      } else if (*s == ')') {
        if (--pcount < 0) goto error;
        // Pop active state, apply deferred not (which was only set if checking)
        active = (toybuf[pcount]>>1)&1;
        if (active && (toybuf[pcount]&1)) test = !test;
        not = 0;
      } else goto error;

      continue;
    } else s++;

    if (!strcmp(s, "xdev")) TT.xdev = 1;
    else if (!strcmp(s, "depth")) TT.depth = 1;
    else if (!strcmp(s, "o") || !strcmp(s, "or")) {
      if (not) goto error;
      if (active) {
        if (!test) test = 1;
        else active = 0;     // decision has been made until next ")"
      }

    // Mostly ignore NOP argument
    } else if (!strcmp(s, "a") || !strcmp(s, "and")) {
      if (not) goto error;

    } else if (!strcmp(s, "print") || !strcmp("print0", s)) {
      print++;
      if (check) do_print(new, s[5] ? 0 : '\n');

    } else if (!strcmp(s, "nouser")) {
      if (check) if (getpwuid(new->st.st_uid)) test = 0;
    } else if (!strcmp(s, "nogroup")) {
      if (check) if (getgrgid(new->st.st_gid)) test = 0;
    } else if (!strcmp(s, "prune")) {
      if (check && S_ISDIR(new->st.st_dev) && !TT.depth) recurse = 0;

    // Remaining filters take an argument
    } else {
      if (!strcmp(s, "name") || !strcmp(s, "iname")
        || !strcmp(s, "path") || !strcmp(s, "ipath"))
      {
        int i = (*s == 'i');
        char *arg = ss[1], *path = 0, *name = new->name;

        // Handle path expansion and case flattening
        if (new && s[i] == 'p') name = path = dirtree_path(new, 0);
        if (i) {
          if (check || !new) {
            name = strlower(new ? name : arg);
            if (!new) {
              dlist_add(&TT.argdata, name);
              free(path);
            } else arg = ((struct double_list *)llist_pop(&argdata))->data;
          }
        }

        if (check) {
          test = !fnmatch(arg, name, FNM_PATHNAME*(s[i] == 'p'));
          free(path);
          if (i) free(name);
        }
      } else if (!strcmp(s, "perm")) {
        if (check) {
          char *m = ss[1];
          mode_t m1 = string_to_mode(m+(*m == '-'), 0),
                 m2 = new->st.st_dev & 07777;

          if (*m != '-') m2 &= m1;
          test = m1 == m2;
        }
      } else if (!strcmp(s, "type")) {
        if (check) {
          char c = stridx("bcdlpfs", *ss[1]);
          int types[] = {S_IFBLK, S_IFCHR, S_IFDIR, S_IFLNK, S_IFIFO,
                         S_IFREG, S_IFSOCK};

          if ((new->st.st_mode & S_IFMT) != types[c]) test = 0;
        }

      } else if (!strcmp(s, "atime")) {
        if (check)
          test = compare_numsign(TT.now - new->st.st_atime, 86400, ss[1]);
      } else if (!strcmp(s, "ctime")) {
        if (check)
          test = compare_numsign(TT.now - new->st.st_ctime, 86400, ss[1]);
      } else if (!strcmp(s, "mtime")) {
        if (check)
          test = compare_numsign(TT.now - new->st.st_mtime, 86400, ss[1]);
      } else if (!strcmp(s, "size")) {
        if (check)
          test = compare_numsign(new->st.st_size, 512, ss[1]);
      } else if (!strcmp(s, "links")) {
        if (check) test = compare_numsign(new->st.st_nlink, 0, ss[1]);
      } else if (!strcmp(s, "user")) {
        todo_store_argument();
      } else if (!strcmp(s, "group")) {
        todo_store_argument();
      } else if (!strcmp(s, "newer")) {
        todo_store_argument();
      } else if (!strcmp(s, "exec") || !strcmp("ok", s)
              || !strcmp(s, "execdir") || !strcmp(s, "okdir"))
      {
        struct exec_range *aa;

        print++;

        // Initial argument parsing pass
        if (!new) {
          // special case "-exec \;" to fall through to "needs 1 arg" error.
          if (!strcmp(ss[1], ";")) {
            int len;

            dlist_add_nomalloc(&TT.argdata,(void *)(aa = xzalloc(sizeof(*aa))));
            aa->argstart = ++ss;

            // Record command line arguments to -exec
            for (len = 0; ss[len]; len++) {
              if (!strcmp(ss[len], ";")) break;
              else if (!strcmp(ss[len], "{}")) {
                aa->curly = len;
                if (!strcmp(ss[len+1], "+")) {

                  // Measure environment space
                  if (!TT.envsize) {
                    char **env;

                    for (env = environ; *env; env++)
                      TT.envsize += sizeof(char *) + strlen(*env) + 1;
                    TT.envsize += sizeof(char *);
                  }
                  aa->plus++;
                  len++;
                  break;
                }
              } else aa->argsize += sizeof(char *) + strlen(ss[len]) + 1;
            }
            if (!ss[len]) error_exit("-exec without \\;");
            ss += len-1;
            aa->arglen = len;
            aa->dir = !!strchr(s, 'd');
            if (aa->dir && TT.topdir == -1) TT.topdir = xopen(".", 0);
          }

        // collect names and execute commands
        } else {
          if (check) {
            char *name;
            struct double_list **dl;

            // Grab command line exec argument list
            aa = (void *)llist_pop(&argdata);

            // name is always a new malloc, so we can always free it.
            name = aa->dir ? xstrdup(new->name) : dirtree_path(new, 0);

            // Mark entry so COMEAGAIN can call flush_exec() in parent.
            // This is never a valid pointer valud for prev to have otherwise
            if (aa->dir) aa->prev = (void *)1;

            if (*s == 'o') {
              char *prompt = xmprintf("[%s] %s", ss[1], name);
              if(!(test = yesno(prompt, 0))) goto cont;
            }

            // Add next name to list (global list without -dir, local with)
            if (aa->dir && new->parent)
              dl = (struct double_list **)&new->parent->extra;
            else dl = &aa->names;

            // Is this + mode?
            if (aa->plus) {
              int size = sizeof(char *)+strlen(name)+1;

              // Linux caps environment space (env vars + args) at 32 4k pages.
              // todo: is there a way to probe this instead of constant here?

              if (TT.envsize+aa->argsize+aa->namesize+size >= 131072)
                toys.exitval |= flush_exec(new, aa);
              aa->namesize += size;
            }
            dlist_add(dl, name);
            if (!aa->plus) test = flush_exec(new, aa);
          }
        }
      } else goto error;

      // This test can go at the end because we do a syntax checking
      // pass first. Putting it here gets the error message (-unknown
      // vs -known noarg) right.
      if (!*++ss) error_exit("'%s' needs 1 arg", --s);
    }
cont:
    // Apply pending "!" to result
    if (active && not) test = !test;
    not = 0;
  }

  if (new) {
    // If there was no action, print
    if (!print && test) do_print(new, '\n');
  } else dlist_terminate(TT.argdata);

  return recurse;

error:
  error_exit("bad arg '%s'", *ss);
}

void find_main(void)
{
  int i, len;
  char **ss = toys.optargs;

  TT.topdir = -1;

  // Distinguish paths from filters
  for (len = 0; toys.optargs[len]; len++)
    if (strchr("-!(", *toys.optargs[len])) break;
  TT.filter = toys.optargs+len;

  // use "." if no paths
  if (!*ss || **ss == '-') {
    ss = (char *[]){"."};
    len = 1;
  }

  // first pass argument parsing, verify args match up, handle "evaluate once"
  TT.now = time(0);
  do_find(0);

  // Loop through paths
  for (i = 0; i < len; i++) {
    struct dirtree *new;

    new = dirtree_add_node(0, ss[i], toys.optflags&(FLAG_H|FLAG_L));
    if (new) dirtree_handle_callback(new, do_find);
  }

  if (CFG_TOYBOX_FREE) {
    close(TT.topdir);
    llist_traverse(TT.argdata, free);
  }
}