recursive_action.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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, struct stat *statbuf, void* userData, int depth)
  22. {
  23. return TRUE;
  24. }
  25. /* fileAction return value of 0 on any file in directory will make
  26. * recursive_action() return 0, but it doesn't stop directory traversal
  27. * (fileAction/dirAction will be called on each file).
  28. *
  29. * if !depthFirst, dirAction return value of 0 (FALSE) or 2 (SKIP)
  30. * prevents recursion into that directory, instead
  31. * recursive_action() returns 0 (if FALSE) or 1 (if SKIP).
  32. *
  33. * followLinks=0/1 differs mainly in handling of links to dirs.
  34. * 0: lstat(statbuf). Calls fileAction on link name even if points to dir.
  35. * 1: stat(statbuf). Calls dirAction and optionally recurse on link to dir.
  36. */
  37. int recursive_action(const char *fileName,
  38. int recurse, int followLinks, int depthFirst,
  39. int (*fileAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
  40. int (*dirAction)(const char *fileName, struct stat *statbuf, void* userData, int depth),
  41. void* userData,
  42. int depth)
  43. {
  44. struct stat statbuf;
  45. int status;
  46. DIR *dir;
  47. struct dirent *next;
  48. if (!fileAction) fileAction = true_action;
  49. if (!dirAction) dirAction = true_action;
  50. status = (followLinks ? stat : lstat)(fileName, &statbuf);
  51. if (status < 0) {
  52. #ifdef DEBUG_RECURS_ACTION
  53. bb_error_msg("status=%d followLinks=%d TRUE=%d",
  54. status, followLinks, TRUE);
  55. #endif
  56. bb_perror_msg("%s", fileName);
  57. return FALSE;
  58. }
  59. /* If S_ISLNK(m), then we know that !S_ISDIR(m).
  60. * Then we can skip checking first part: if it is true, then
  61. * (!dir) is also true! */
  62. if ( /* (!followLinks && S_ISLNK(statbuf.st_mode)) || */
  63. !S_ISDIR(statbuf.st_mode)
  64. ) {
  65. return fileAction(fileName, &statbuf, userData, depth);
  66. }
  67. /* It's a directory (or a link to one, and followLinks is set) */
  68. if (!recurse) {
  69. return dirAction(fileName, &statbuf, userData, depth);
  70. }
  71. if (!depthFirst) {
  72. status = dirAction(fileName, &statbuf, userData, depth);
  73. if (!status) {
  74. bb_perror_msg("%s", fileName);
  75. return FALSE;
  76. }
  77. if (status == SKIP)
  78. return TRUE;
  79. }
  80. dir = opendir(fileName);
  81. if (!dir) {
  82. /* findutils-4.1.20 reports this */
  83. /* (i.e. it doesn't silently return with exit code 1) */
  84. /* To trigger: "find -exec rm -rf {} \;" */
  85. bb_perror_msg("%s", fileName);
  86. return FALSE;
  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. if (!recursive_action(nextFile, TRUE, followLinks, depthFirst,
  95. fileAction, dirAction, userData, depth+1)) {
  96. status = FALSE;
  97. }
  98. free(nextFile);
  99. }
  100. closedir(dir);
  101. if (depthFirst) {
  102. if (!dirAction(fileName, &statbuf, userData, depth)) {
  103. bb_perror_msg("%s", fileName);
  104. return FALSE;
  105. }
  106. }
  107. if (!status)
  108. return FALSE;
  109. return TRUE;
  110. }