recursive_action.c 4.5 KB

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