recursive_action.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 source tree.
  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 FAST_FUNC true_action(const char *fileName UNUSED_PARAM,
  22. struct stat *statbuf UNUSED_PARAM,
  23. void* userData UNUSED_PARAM,
  24. int depth UNUSED_PARAM)
  25. {
  26. return TRUE;
  27. }
  28. /* fileName is (l)stat'ed (depending on ACTION_FOLLOWLINKS[_L0]).
  29. *
  30. * If it is a file: fileAction in run on it, its return value is returned.
  31. *
  32. * In case we are in a recursive invocation (see below):
  33. * normally, fileAction should return 1 (TRUE) to indicate that
  34. * everything is okay and processing should continue.
  35. * fileAction return value of 0 (FALSE) on any file in directory will make
  36. * recursive_action() also return 0, but it doesn't stop directory traversal
  37. * (fileAction/dirAction will be called on each file).
  38. *
  39. * [TODO: maybe introduce -1 to mean "stop traversal NOW and return"]
  40. *
  41. * If it is a directory:
  42. *
  43. * If !ACTION_RECURSE, dirAction is called and its
  44. * return value is returned from recursive_action(). No recursion.
  45. *
  46. * If ACTION_RECURSE, directory is opened, and recursive_action() is called
  47. * on each file/subdirectory.
  48. * If any one of these calls returns 0, current recursive_action() returns 0.
  49. *
  50. * If !ACTION_DEPTHFIRST, dirAction is called before recurse.
  51. * Return value of 0 (FALSE) is an error: prevents recursion,
  52. * the warning is printed (unless ACTION_QUIET) and recursive_action() returns 0.
  53. * Return value of 2 (SKIP) prevents recursion, instead recursive_action()
  54. * returns 1 (TRUE, no error).
  55. *
  56. * If ACTION_DEPTHFIRST, dirAction is called after recurse.
  57. * If it returns 0, the warning is printed and recursive_action() returns 0.
  58. *
  59. * ACTION_FOLLOWLINKS mainly controls handling of links to dirs.
  60. * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
  61. * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
  62. */
  63. int FAST_FUNC recursive_action(const char *fileName,
  64. unsigned flags,
  65. int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
  66. int FAST_FUNC (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
  67. void* userData,
  68. unsigned depth)
  69. {
  70. struct stat statbuf;
  71. unsigned follow;
  72. int status;
  73. DIR *dir;
  74. struct dirent *next;
  75. if (!fileAction) fileAction = true_action;
  76. if (!dirAction) dirAction = true_action;
  77. follow = ACTION_FOLLOWLINKS;
  78. if (depth == 0)
  79. follow = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
  80. follow &= flags;
  81. status = (follow ? stat : lstat)(fileName, &statbuf);
  82. if (status < 0) {
  83. #ifdef DEBUG_RECURS_ACTION
  84. bb_error_msg("status=%d flags=%x", status, flags);
  85. #endif
  86. if ((flags & ACTION_DANGLING_OK)
  87. && errno == ENOENT
  88. && lstat(fileName, &statbuf) == 0
  89. ) {
  90. /* Dangling link */
  91. return fileAction(fileName, &statbuf, userData, depth);
  92. }
  93. goto done_nak_warn;
  94. }
  95. /* If S_ISLNK(m), then we know that !S_ISDIR(m).
  96. * Then we can skip checking first part: if it is true, then
  97. * (!dir) is also true! */
  98. if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
  99. !S_ISDIR(statbuf.st_mode)
  100. ) {
  101. return fileAction(fileName, &statbuf, userData, depth);
  102. }
  103. /* It's a directory (or a link to one, and followLinks is set) */
  104. if (!(flags & ACTION_RECURSE)) {
  105. return dirAction(fileName, &statbuf, userData, depth);
  106. }
  107. if (!(flags & ACTION_DEPTHFIRST)) {
  108. status = dirAction(fileName, &statbuf, userData, depth);
  109. if (status == FALSE)
  110. goto done_nak_warn;
  111. if (status == SKIP)
  112. return TRUE;
  113. }
  114. dir = opendir(fileName);
  115. if (!dir) {
  116. /* findutils-4.1.20 reports this */
  117. /* (i.e. it doesn't silently return with exit code 1) */
  118. /* To trigger: "find -exec rm -rf {} \;" */
  119. goto done_nak_warn;
  120. }
  121. status = TRUE;
  122. while ((next = readdir(dir)) != NULL) {
  123. char *nextFile;
  124. int s;
  125. nextFile = concat_subpath_file(fileName, next->d_name);
  126. if (nextFile == NULL)
  127. continue;
  128. /* process every file (NB: ACTION_RECURSE is set in flags) */
  129. s = recursive_action(nextFile, flags, fileAction, dirAction,
  130. userData, depth + 1);
  131. if (s == FALSE)
  132. status = FALSE;
  133. free(nextFile);
  134. //#define RECURSE_RESULT_ABORT -1
  135. // if (s == RECURSE_RESULT_ABORT) {
  136. // closedir(dir);
  137. // return s;
  138. // }
  139. }
  140. closedir(dir);
  141. if (flags & ACTION_DEPTHFIRST) {
  142. if (!dirAction(fileName, &statbuf, userData, depth))
  143. goto done_nak_warn;
  144. }
  145. return status;
  146. done_nak_warn:
  147. if (!(flags & ACTION_QUIET))
  148. bb_simple_perror_msg(fileName);
  149. return FALSE;
  150. }