3
0

recursive_action.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. int status;
  59. DIR *dir;
  60. struct dirent *next;
  61. if (!fileAction) fileAction = true_action;
  62. if (!dirAction) dirAction = true_action;
  63. status = ACTION_FOLLOWLINKS; /* hijack a variable for bitmask... */
  64. if (!depth)
  65. status = ACTION_FOLLOWLINKS | ACTION_FOLLOWLINKS_L0;
  66. status = ((flags & status) ? stat : lstat)(fileName, &statbuf);
  67. if (status < 0) {
  68. #ifdef DEBUG_RECURS_ACTION
  69. bb_error_msg("status=%d flags=%x", status, flags);
  70. #endif
  71. goto done_nak_warn;
  72. }
  73. /* If S_ISLNK(m), then we know that !S_ISDIR(m).
  74. * Then we can skip checking first part: if it is true, then
  75. * (!dir) is also true! */
  76. if ( /* (!(flags & ACTION_FOLLOWLINKS) && S_ISLNK(statbuf.st_mode)) || */
  77. !S_ISDIR(statbuf.st_mode)
  78. ) {
  79. return fileAction(fileName, &statbuf, userData, depth);
  80. }
  81. /* It's a directory (or a link to one, and followLinks is set) */
  82. if (!(flags & ACTION_RECURSE)) {
  83. return dirAction(fileName, &statbuf, userData, depth);
  84. }
  85. if (!(flags & ACTION_DEPTHFIRST)) {
  86. status = dirAction(fileName, &statbuf, userData, depth);
  87. if (!status)
  88. goto done_nak_warn;
  89. if (status == SKIP)
  90. return TRUE;
  91. }
  92. dir = opendir(fileName);
  93. if (!dir) {
  94. /* findutils-4.1.20 reports this */
  95. /* (i.e. it doesn't silently return with exit code 1) */
  96. /* To trigger: "find -exec rm -rf {} \;" */
  97. goto done_nak_warn;
  98. }
  99. status = TRUE;
  100. while ((next = readdir(dir)) != NULL) {
  101. char *nextFile;
  102. nextFile = concat_subpath_file(fileName, next->d_name);
  103. if (nextFile == NULL)
  104. continue;
  105. /* process every file (NB: ACTION_RECURSE is set in flags) */
  106. if (!recursive_action(nextFile, flags, fileAction, dirAction,
  107. userData, depth + 1))
  108. status = FALSE;
  109. // s = recursive_action(nextFile, flags, fileAction, dirAction,
  110. // userData, depth + 1);
  111. free(nextFile);
  112. //#define RECURSE_RESULT_ABORT 3
  113. // if (s == RECURSE_RESULT_ABORT) {
  114. // closedir(dir);
  115. // return s;
  116. // }
  117. // if (s == FALSE)
  118. // status = FALSE;
  119. }
  120. closedir(dir);
  121. if (flags & ACTION_DEPTHFIRST) {
  122. if (!dirAction(fileName, &statbuf, userData, depth))
  123. goto done_nak_warn;
  124. }
  125. return status;
  126. done_nak_warn:
  127. if (!(flags & ACTION_QUIET))
  128. bb_simple_perror_msg(fileName);
  129. return FALSE;
  130. }