3
0

recursive_action.c 3.5 KB

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