recursive_action.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #undef DEBUG_RECURS_ACTION
  11. /*
  12. * Walk down all the directories under the specified
  13. * location, and do something (something specified
  14. * by the fileAction and dirAction function pointers).
  15. *
  16. * Unfortunately, while nftw(3) could replace this and reduce
  17. * code size a bit, nftw() wasn't supported before GNU libc 2.1,
  18. * and so isn't sufficiently portable to take over since glibc2.1
  19. * is so stinking huge.
  20. */
  21. static int true_action(const char *fileName ATTRIBUTE_UNUSED,
  22. struct stat *statbuf ATTRIBUTE_UNUSED,
  23. void* userData ATTRIBUTE_UNUSED,
  24. int depth ATTRIBUTE_UNUSED)
  25. {
  26. return TRUE;
  27. }
  28. /* fileAction return value of 0 on any file in directory will make
  29. * recursive_action() return 0, but it doesn't stop directory traversal
  30. * (fileAction/dirAction will be called on each file).
  31. *
  32. * if !depthFirst, dirAction return value of 0 (FALSE) or 2 (SKIP)
  33. * prevents recursion into that directory, instead
  34. * recursive_action() returns 0 (if FALSE) or 1 (if SKIP).
  35. *
  36. * followLinks=0/1 differs mainly in handling of links to dirs.
  37. * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
  38. * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
  39. */
  40. int recursive_action(const char *fileName,
  41. unsigned flags,
  42. int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
  43. int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
  44. void* userData,
  45. unsigned depth)
  46. {
  47. struct stat statbuf;
  48. int status;
  49. DIR *dir;
  50. struct dirent *next;
  51. if (!fileAction) fileAction = true_action;
  52. if (!dirAction) dirAction = true_action;
  53. status = ACTION_FOLLOWLINKS; /* hijack a variable for bitmask... */
  54. if (!depth) status = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
  55. status = ((flags & status) ? stat : lstat)(fileName, &statbuf);
  56. if (status < 0) {
  57. #ifdef DEBUG_RECURS_ACTION
  58. bb_error_msg("status=%d flags=%x", status, flags);
  59. #endif
  60. goto done_nak_warn;
  61. }
  62. /* If S_ISLNK(m), then we know that !S_ISDIR(m).
  63. * Then we can skip checking first part: if it is true, then
  64. * (!dir) is also true! */
  65. if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
  66. !S_ISDIR(statbuf.st_mode)
  67. ) {
  68. return fileAction(fileName, &statbuf, userData, depth);
  69. }
  70. /* It's a directory (or a link to one, and followLinks is set) */
  71. if (!(flags & ACTION_RECURSE)) {
  72. return dirAction(fileName, &statbuf, userData, depth);
  73. }
  74. if (!(flags & ACTION_DEPTHFIRST)) {
  75. status = dirAction(fileName, &statbuf, userData, depth);
  76. if (!status)
  77. goto done_nak_warn;
  78. if (status == SKIP)
  79. return TRUE;
  80. }
  81. dir = opendir(fileName);
  82. if (!dir) {
  83. /* findutils-4.1.20 reports this */
  84. /* (i.e. it doesn't silently return with exit code 1) */
  85. /* To trigger: "find -exec rm -rf {} \;" */
  86. goto done_nak_warn;
  87. }
  88. status = TRUE;
  89. while ((next = readdir(dir)) != NULL) {
  90. char *nextFile;
  91. nextFile = concat_subpath_file(fileName, next->d_name);
  92. if (nextFile == NULL)
  93. continue;
  94. /* now descend into it (NB: ACTION_RECURSE is set in flags) */
  95. if (!recursive_action(nextFile, flags, fileAction, dirAction, userData, depth+1))
  96. status = FALSE;
  97. free(nextFile);
  98. }
  99. closedir(dir);
  100. if (flags & ACTION_DEPTHFIRST) {
  101. if (!dirAction(fileName, &statbuf, userData, depth))
  102. goto done_nak_warn;
  103. }
  104. if (!status)
  105. return FALSE;
  106. return TRUE;
  107. done_nak_warn:
  108. bb_simple_perror_msg(fileName);
  109. return FALSE;
  110. }