recursive_action.c 5.4 KB

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