find.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini find implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Reworked by David Douthitt <n9ubh@callsign.net> and
  8. * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
  9. *
  10. * Licensed under the GPL version 2, see the file LICENSE in this tarball.
  11. */
  12. /* findutils-4.1.20:
  13. *
  14. * # find file.txt -exec 'echo {}' '{} {}' ';'
  15. * find: echo file.txt: No such file or directory
  16. * # find file.txt -exec 'echo' '{} {}' '; '
  17. * find: missing argument to `-exec'
  18. * # find file.txt -exec 'echo {}' '{} {}' ';' junk
  19. * find: paths must precede expression
  20. * # find file.txt -exec 'echo {}' '{} {}' ';' junk ';'
  21. * find: paths must precede expression
  22. * # find file.txt -exec 'echo' '{} {}' ';'
  23. * file.txt file.txt
  24. * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ]))
  25. * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';'
  26. * file.txt file.txt
  27. * file.txt
  28. * /tmp
  29. * # find -name '*.c' -o -name '*.h'
  30. * [shows files, *.c and *.h intermixed]
  31. * # find file.txt -name '*f*' -o -name '*t*'
  32. * file.txt
  33. * # find file.txt -name '*z*' -o -name '*t*'
  34. * file.txt
  35. * # find file.txt -name '*f*' -o -name '*z*'
  36. * file.txt
  37. *
  38. * # find t z -name '*t*' -print -o -name '*z*'
  39. * t
  40. * # find t z t z -name '*t*' -o -name '*z*' -print
  41. * z
  42. * z
  43. * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print
  44. * (no output)
  45. */
  46. #include <fnmatch.h>
  47. #include "busybox.h"
  48. USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
  49. USE_FEATURE_FIND_XDEV(static int xdev_count;)
  50. typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
  51. typedef struct {
  52. action_fp f;
  53. } action;
  54. #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
  55. #define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap)
  56. ACTS(print)
  57. ACTS(name, const char *pattern;)
  58. USE_FEATURE_FIND_PRINT0(ACTS(print0))
  59. USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
  60. USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
  61. USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
  62. USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
  63. USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
  64. USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
  65. USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; int *subst_count; int exec_argc;))
  66. USE_DESKTOP( ACTS(paren, action ***subexpr;))
  67. USE_DESKTOP( ACTS(size, off_t size;))
  68. USE_DESKTOP( ACTS(prune))
  69. static action ***actions;
  70. static int need_print = 1;
  71. #if ENABLE_FEATURE_FIND_EXEC
  72. static int count_subst(const char *str)
  73. {
  74. int count = 0;
  75. while ((str = strstr(str, "{}"))) {
  76. count++;
  77. str++;
  78. }
  79. return count;
  80. }
  81. static char* subst(const char *src, int count, const char* filename)
  82. {
  83. char *buf, *dst, *end;
  84. int flen = strlen(filename);
  85. /* we replace each '{}' with filename: growth by strlen-2 */
  86. buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
  87. while ((end = strstr(src, "{}"))) {
  88. memcpy(dst, src, end - src);
  89. dst += end - src;
  90. src = end + 2;
  91. memcpy(dst, filename, flen);
  92. dst += flen;
  93. }
  94. strcpy(dst, src);
  95. return buf;
  96. }
  97. #endif
  98. static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
  99. {
  100. int cur_group;
  101. int cur_action;
  102. int rc = TRUE;
  103. action **app, *ap;
  104. cur_group = -1;
  105. while ((app = appp[++cur_group])) {
  106. cur_action = -1;
  107. do {
  108. ap = app[++cur_action];
  109. } while (ap && (rc = ap->f(fileName, statbuf, ap)));
  110. if (!ap) {
  111. /* all actions in group were successful */
  112. break;
  113. }
  114. }
  115. return rc;
  116. }
  117. ACTF(name)
  118. {
  119. const char *tmp = strrchr(fileName, '/');
  120. if (tmp == NULL)
  121. tmp = fileName;
  122. else
  123. tmp++;
  124. return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
  125. }
  126. #if ENABLE_FEATURE_FIND_TYPE
  127. ACTF(type)
  128. {
  129. return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
  130. }
  131. #endif
  132. #if ENABLE_FEATURE_FIND_PERM
  133. ACTF(perm)
  134. {
  135. /* -perm +mode: at least one of perm_mask bits are set */
  136. if (ap->perm_char == '+')
  137. return (statbuf->st_mode & ap->perm_mask) != 0;
  138. /* -perm -mode: all of perm_mask are set */
  139. if (ap->perm_char == '-')
  140. return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
  141. /* -perm mode: file mode must match perm_mask */
  142. return (statbuf->st_mode & 07777) == ap->perm_mask;
  143. }
  144. #endif
  145. #if ENABLE_FEATURE_FIND_MTIME
  146. ACTF(mtime)
  147. {
  148. time_t file_age = time(NULL) - statbuf->st_mtime;
  149. time_t mtime_secs = ap->mtime_days * 24*60*60;
  150. if (ap->mtime_char == '+')
  151. return file_age >= mtime_secs + 24*60*60;
  152. if (ap->mtime_char == '-')
  153. return file_age < mtime_secs;
  154. /* just numeric mtime */
  155. return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
  156. }
  157. #endif
  158. #if ENABLE_FEATURE_FIND_MMIN
  159. ACTF(mmin)
  160. {
  161. time_t file_age = time(NULL) - statbuf->st_mtime;
  162. time_t mmin_secs = ap->mmin_mins * 60;
  163. if (ap->mmin_char == '+')
  164. return file_age >= mmin_secs + 60;
  165. if (ap->mmin_char == '-')
  166. return file_age < mmin_secs;
  167. /* just numeric mmin */
  168. return file_age >= mmin_secs && file_age < (mmin_secs + 60);
  169. }
  170. #endif
  171. #if ENABLE_FEATURE_FIND_NEWER
  172. ACTF(newer)
  173. {
  174. return (ap->newer_mtime < statbuf->st_mtime);
  175. }
  176. #endif
  177. #if ENABLE_FEATURE_FIND_INUM
  178. ACTF(inum)
  179. {
  180. return (statbuf->st_ino == ap->inode_num);
  181. }
  182. #endif
  183. #if ENABLE_FEATURE_FIND_EXEC
  184. ACTF(exec)
  185. {
  186. int i, rc;
  187. char *argv[ap->exec_argc+1];
  188. for (i = 0; i < ap->exec_argc; i++)
  189. argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
  190. argv[i] = NULL; /* terminate the list */
  191. rc = wait4pid(spawn(argv));
  192. if (rc)
  193. bb_perror_msg("%s", argv[0]);
  194. for (i = 0; i < ap->exec_argc; i++)
  195. free(argv[i]);
  196. return rc == 0; /* return 1 if success */
  197. }
  198. #endif
  199. #if ENABLE_FEATURE_FIND_PRINT0
  200. ACTF(print0)
  201. {
  202. printf("%s%c", fileName, '\0');
  203. return TRUE;
  204. }
  205. #endif
  206. ACTF(print)
  207. {
  208. puts(fileName);
  209. return TRUE;
  210. }
  211. #if ENABLE_DESKTOP
  212. ACTF(paren)
  213. {
  214. return exec_actions(ap->subexpr, fileName, statbuf);
  215. }
  216. /*
  217. * -prune: if -depth is not given, return true and do not descend
  218. * current dir; if -depth is given, return false with no effect.
  219. * Example:
  220. * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
  221. */
  222. ACTF(prune)
  223. {
  224. return SKIP;
  225. }
  226. ACTF(size)
  227. {
  228. return statbuf->st_size == ap->size;
  229. }
  230. #endif
  231. static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
  232. {
  233. int rc;
  234. #ifdef CONFIG_FEATURE_FIND_XDEV
  235. if (S_ISDIR(statbuf->st_mode) && xdev_count) {
  236. int i;
  237. for (i = 0; i < xdev_count; i++) {
  238. if (xdev_dev[i] != statbuf->st_dev)
  239. return SKIP;
  240. }
  241. }
  242. #endif
  243. rc = exec_actions(actions, fileName, statbuf);
  244. /* Had no explicit -print[0] or -exec? then print */
  245. if (rc && need_print)
  246. puts(fileName);
  247. /* Cannot return 0: our caller, recursive_action(),
  248. * will perror() and skip dirs (if called on dir) */
  249. return rc == 0 ? TRUE : rc;
  250. }
  251. #if ENABLE_FEATURE_FIND_TYPE
  252. static int find_type(const char *type)
  253. {
  254. int mask = 0;
  255. switch (type[0]) {
  256. case 'b':
  257. mask = S_IFBLK;
  258. break;
  259. case 'c':
  260. mask = S_IFCHR;
  261. break;
  262. case 'd':
  263. mask = S_IFDIR;
  264. break;
  265. case 'p':
  266. mask = S_IFIFO;
  267. break;
  268. case 'f':
  269. mask = S_IFREG;
  270. break;
  271. case 'l':
  272. mask = S_IFLNK;
  273. break;
  274. case 's':
  275. mask = S_IFSOCK;
  276. break;
  277. }
  278. if (mask == 0 || type[1] != '\0')
  279. bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
  280. return mask;
  281. }
  282. #endif
  283. static const char* plus_minus_num(const char* str)
  284. {
  285. if (*str == '-' || *str == '+')
  286. str++;
  287. return str;
  288. }
  289. static action*** parse_params(char **argv)
  290. {
  291. action*** appp;
  292. int cur_group = 0;
  293. int cur_action = 0;
  294. action* alloc_action(int sizeof_struct, action_fp f)
  295. {
  296. action *ap;
  297. appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
  298. appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
  299. appp[cur_group][cur_action] = NULL;
  300. ap->f = f;
  301. return ap;
  302. }
  303. #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
  304. appp = xzalloc(2 * sizeof(*appp)); /* appp[0],[1] == NULL */
  305. // Actions have side effects and return a true or false value
  306. // We implement: -print, -print0, -exec
  307. // The rest are tests.
  308. // Tests and actions are grouped by operators
  309. // ( expr ) Force precedence
  310. // ! expr True if expr is false
  311. // -not expr Same as ! expr
  312. // expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
  313. // expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
  314. // expr1 , expr2 List; both expr1 and expr2 are always evaluated
  315. // We implement: (), -a, -o
  316. while (*argv) {
  317. const char *arg = argv[0];
  318. const char *arg1 = argv[1];
  319. /* --- Operators --- */
  320. if (strcmp(arg, "-a") == 0
  321. USE_DESKTOP(|| strcmp(arg, "-and") == 0)
  322. ) {
  323. /* no special handling required */
  324. }
  325. else if (strcmp(arg, "-o") == 0
  326. USE_DESKTOP(|| strcmp(arg, "-or") == 0)
  327. ) {
  328. /* start new OR group */
  329. cur_group++;
  330. appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
  331. appp[cur_group] = NULL;
  332. appp[cur_group+1] = NULL;
  333. cur_action = 0;
  334. }
  335. /* --- Tests and actions --- */
  336. else if (strcmp(arg, "-print") == 0) {
  337. need_print = 0;
  338. (void) ALLOC_ACTION(print);
  339. }
  340. #if ENABLE_FEATURE_FIND_PRINT0
  341. else if (strcmp(arg, "-print0") == 0) {
  342. need_print = 0;
  343. (void) ALLOC_ACTION(print0);
  344. }
  345. #endif
  346. else if (strcmp(arg, "-name") == 0) {
  347. action_name *ap;
  348. if (!*++argv)
  349. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  350. ap = ALLOC_ACTION(name);
  351. ap->pattern = arg1;
  352. }
  353. #if ENABLE_FEATURE_FIND_TYPE
  354. else if (strcmp(arg, "-type") == 0) {
  355. action_type *ap;
  356. if (!*++argv)
  357. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  358. ap = ALLOC_ACTION(type);
  359. ap->type_mask = find_type(arg1);
  360. }
  361. #endif
  362. #if ENABLE_FEATURE_FIND_PERM
  363. /* TODO:
  364. * -perm mode File's permission bits are exactly mode (octal or symbolic).
  365. * Symbolic modes use mode 0 as a point of departure.
  366. * -perm -mode All of the permission bits mode are set for the file.
  367. * -perm +mode Any of the permission bits mode are set for the file.
  368. */
  369. else if (strcmp(arg, "-perm") == 0) {
  370. action_perm *ap;
  371. if (!*++argv)
  372. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  373. ap = ALLOC_ACTION(perm);
  374. ap->perm_char = arg1[0];
  375. arg1 = plus_minus_num(arg1);
  376. ap->perm_mask = 0;
  377. if (!bb_parse_mode(arg1, &ap->perm_mask))
  378. bb_error_msg_and_die("invalid mode: %s", arg1);
  379. }
  380. #endif
  381. #if ENABLE_FEATURE_FIND_MTIME
  382. else if (strcmp(arg, "-mtime") == 0) {
  383. action_mtime *ap;
  384. if (!*++argv)
  385. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  386. ap = ALLOC_ACTION(mtime);
  387. ap->mtime_char = arg1[0];
  388. ap->mtime_days = xatoul(plus_minus_num(arg1));
  389. }
  390. #endif
  391. #if ENABLE_FEATURE_FIND_MMIN
  392. else if (strcmp(arg, "-mmin") == 0) {
  393. action_mmin *ap;
  394. if (!*++argv)
  395. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  396. ap = ALLOC_ACTION(mmin);
  397. ap->mmin_char = arg1[0];
  398. ap->mmin_mins = xatoul(plus_minus_num(arg1));
  399. }
  400. #endif
  401. #if ENABLE_FEATURE_FIND_NEWER
  402. else if (strcmp(arg, "-newer") == 0) {
  403. action_newer *ap;
  404. struct stat stat_newer;
  405. if (!*++argv)
  406. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  407. xstat(arg1, &stat_newer);
  408. ap = ALLOC_ACTION(newer);
  409. ap->newer_mtime = stat_newer.st_mtime;
  410. }
  411. #endif
  412. #if ENABLE_FEATURE_FIND_INUM
  413. else if (strcmp(arg, "-inum") == 0) {
  414. action_inum *ap;
  415. if (!*++argv)
  416. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  417. ap = ALLOC_ACTION(inum);
  418. ap->inode_num = xatoul(arg1);
  419. }
  420. #endif
  421. #if ENABLE_FEATURE_FIND_EXEC
  422. else if (strcmp(arg, "-exec") == 0) {
  423. int i;
  424. action_exec *ap;
  425. need_print = 0;
  426. ap = ALLOC_ACTION(exec);
  427. ap->exec_argv = ++argv; /* first arg after -exec */
  428. ap->exec_argc = 0;
  429. while (1) {
  430. if (!*argv) /* did not see ';' till end */
  431. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  432. if (LONE_CHAR(argv[0], ';'))
  433. break;
  434. argv++;
  435. ap->exec_argc++;
  436. }
  437. if (ap->exec_argc == 0)
  438. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  439. ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
  440. i = ap->exec_argc;
  441. while (i--)
  442. ap->subst_count[i] = count_subst(ap->exec_argv[i]);
  443. }
  444. #endif
  445. #if ENABLE_DESKTOP
  446. else if (LONE_CHAR(arg, '(')) {
  447. action_paren *ap;
  448. char **endarg;
  449. int nested = 1;
  450. endarg = argv;
  451. while (1) {
  452. if (!*++endarg)
  453. bb_error_msg_and_die("unpaired '('");
  454. if (LONE_CHAR(*endarg, '('))
  455. nested++;
  456. else if (LONE_CHAR(*endarg, ')') && !--nested) {
  457. *endarg = NULL;
  458. break;
  459. }
  460. }
  461. ap = ALLOC_ACTION(paren);
  462. ap->subexpr = parse_params(argv + 1);
  463. *endarg = (char*) ")"; /* restore NULLed parameter */
  464. argv = endarg;
  465. }
  466. else if (strcmp(arg, "-prune") == 0) {
  467. (void) ALLOC_ACTION(prune);
  468. }
  469. else if (strcmp(arg, "-size") == 0) {
  470. action_size *ap;
  471. if (!*++argv)
  472. bb_error_msg_and_die(bb_msg_requires_arg, arg);
  473. ap = ALLOC_ACTION(size);
  474. ap->size = XATOOFF(arg1);
  475. }
  476. #endif
  477. else
  478. bb_show_usage();
  479. argv++;
  480. }
  481. return appp;
  482. #undef ALLOC_ACTION
  483. }
  484. int find_main(int argc, char **argv)
  485. {
  486. int dereference = FALSE;
  487. char *arg;
  488. char **argp;
  489. int i, firstopt, status = EXIT_SUCCESS;
  490. for (firstopt = 1; firstopt < argc; firstopt++) {
  491. if (argv[firstopt][0] == '-')
  492. break;
  493. #if ENABLE_DESKTOP
  494. if (LONE_CHAR(argv[firstopt], '('))
  495. break;
  496. #endif
  497. }
  498. if (firstopt == 1) {
  499. argv[0] = (char*)".";
  500. argv--;
  501. firstopt++;
  502. }
  503. // All options always return true. They always take effect,
  504. // rather than being processed only when their place in the
  505. // expression is reached
  506. // We implement: -follow, -xdev
  507. /* Process options, and replace then with -a */
  508. /* (-a will be ignored by recursive parser later) */
  509. argp = &argv[firstopt];
  510. while ((arg = argp[0])) {
  511. if (strcmp(arg, "-follow") == 0) {
  512. dereference = TRUE;
  513. argp[0] = (char*)"-a";
  514. }
  515. #if ENABLE_FEATURE_FIND_XDEV
  516. else if (strcmp(arg, "-xdev") == 0) {
  517. struct stat stbuf;
  518. if (!xdev_count) {
  519. xdev_count = firstopt - 1;
  520. xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
  521. for (i = 1; i < firstopt; i++) {
  522. /* not xstat(): shouldn't bomb out on
  523. * "find not_exist exist -xdev" */
  524. if (stat(argv[i], &stbuf))
  525. stbuf.st_dev = -1L;
  526. xdev_dev[i-1] = stbuf.st_dev;
  527. }
  528. }
  529. argp[0] = (char*)"-a";
  530. }
  531. #endif
  532. argp++;
  533. }
  534. actions = parse_params(&argv[firstopt]);
  535. for (i = 1; i < firstopt; i++) {
  536. if (!recursive_action(argv[i],
  537. TRUE, // recurse
  538. dereference, // follow links
  539. FALSE, // depth first
  540. fileAction, // file action
  541. fileAction, // dir action
  542. NULL, // user data
  543. 0)) // depth
  544. status = EXIT_FAILURE;
  545. }
  546. return status;
  547. }