THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
5.8KB

  1. #include "cache.h"
  2. #include "dir.h"
  3. #include "iterator.h"
  4. #include "dir-iterator.h"
  5. struct dir_iterator_level {
  6. DIR *dir;
  7. /*
  8. * The length of the directory part of path at this level
  9. * (including a trailing '/'):
  10. */
  11. size_t prefix_len;
  12. };
  13. /*
  14. * The full data structure used to manage the internal directory
  15. * iteration state. It includes members that are not part of the
  16. * public interface.
  17. */
  18. struct dir_iterator_int {
  19. struct dir_iterator base;
  20. /*
  21. * The number of levels currently on the stack. After the first
  22. * call to dir_iterator_begin(), if it succeeds to open the
  23. * first level's dir, this will always be at least 1. Then,
  24. * when it comes to zero the iteration is ended and this
  25. * struct is freed.
  26. */
  27. size_t levels_nr;
  28. /* The number of levels that have been allocated on the stack */
  29. size_t levels_alloc;
  30. /*
  31. * A stack of levels. levels[0] is the uppermost directory
  32. * that will be included in this iteration.
  33. */
  34. struct dir_iterator_level *levels;
  35. /* Combination of flags for this dir-iterator */
  36. unsigned int flags;
  37. };
  38. /*
  39. * Push a level in the iter stack and initialize it with information from
  40. * the directory pointed by iter->base->path. It is assumed that this
  41. * strbuf points to a valid directory path. Return 0 on success and -1
  42. * otherwise, setting errno accordingly and leaving the stack unchanged.
  43. */
  44. static int push_level(struct dir_iterator_int *iter)
  45. {
  46. struct dir_iterator_level *level;
  47. ALLOC_GROW(iter->levels, iter->levels_nr + 1, iter->levels_alloc);
  48. level = &iter->levels[iter->levels_nr++];
  49. if (!is_dir_sep(iter->base.path.buf[iter->base.path.len - 1]))
  50. strbuf_addch(&iter->base.path, '/');
  51. level->prefix_len = iter->base.path.len;
  52. level->dir = opendir(iter->base.path.buf);
  53. if (!level->dir) {
  54. int saved_errno = errno;
  55. if (errno != ENOENT) {
  56. warning_errno("error opening directory '%s'",
  57. iter->base.path.buf);
  58. }
  59. iter->levels_nr--;
  60. errno = saved_errno;
  61. return -1;
  62. }
  63. return 0;
  64. }
  65. /*
  66. * Pop the top level on the iter stack, releasing any resources associated
  67. * with it. Return the new value of iter->levels_nr.
  68. */
  69. static int pop_level(struct dir_iterator_int *iter)
  70. {
  71. struct dir_iterator_level *level =
  72. &iter->levels[iter->levels_nr - 1];
  73. if (level->dir && closedir(level->dir))
  74. warning_errno("error closing directory '%s'",
  75. iter->base.path.buf);
  76. level->dir = NULL;
  77. return --iter->levels_nr;
  78. }
  79. /*
  80. * Populate iter->base with the necessary information on the next iteration
  81. * entry, represented by the given dirent de. Return 0 on success and -1
  82. * otherwise, setting errno accordingly.
  83. */
  84. static int prepare_next_entry_data(struct dir_iterator_int *iter,
  85. struct dirent *de)
  86. {
  87. int err, saved_errno;
  88. strbuf_addstr(&iter->base.path, de->d_name);
  89. /*
  90. * We have to reset these because the path strbuf might have
  91. * been realloc()ed at the previous strbuf_addstr().
  92. */
  93. iter->base.relative_path = iter->base.path.buf +
  94. iter->levels[0].prefix_len;
  95. iter->base.basename = iter->base.path.buf +
  96. iter->levels[iter->levels_nr - 1].prefix_len;
  97. if (iter->flags & DIR_ITERATOR_FOLLOW_SYMLINKS)
  98. err = stat(iter->base.path.buf, &iter->base.st);
  99. else
  100. err = lstat(iter->base.path.buf, &iter->base.st);
  101. saved_errno = errno;
  102. if (err && errno != ENOENT)
  103. warning_errno("failed to stat '%s'", iter->base.path.buf);
  104. errno = saved_errno;
  105. return err;
  106. }
  107. int dir_iterator_advance(struct dir_iterator *dir_iterator)
  108. {
  109. struct dir_iterator_int *iter =
  110. (struct dir_iterator_int *)dir_iterator;
  111. if (S_ISDIR(iter->base.st.st_mode) && push_level(iter)) {
  112. if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC)
  113. goto error_out;
  114. if (iter->levels_nr == 0)
  115. goto error_out;
  116. }
  117. /* Loop until we find an entry that we can give back to the caller. */
  118. while (1) {
  119. struct dirent *de;
  120. struct dir_iterator_level *level =
  121. &iter->levels[iter->levels_nr - 1];
  122. strbuf_setlen(&iter->base.path, level->prefix_len);
  123. errno = 0;
  124. de = readdir(level->dir);
  125. if (!de) {
  126. if (errno) {
  127. warning_errno("error reading directory '%s'",
  128. iter->base.path.buf);
  129. if (iter->flags & DIR_ITERATOR_PEDANTIC)
  130. goto error_out;
  131. } else if (pop_level(iter) == 0) {
  132. return dir_iterator_abort(dir_iterator);
  133. }
  134. continue;
  135. }
  136. if (is_dot_or_dotdot(de->d_name))
  137. continue;
  138. if (prepare_next_entry_data(iter, de)) {
  139. if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC)
  140. goto error_out;
  141. continue;
  142. }
  143. return ITER_OK;
  144. }
  145. error_out:
  146. dir_iterator_abort(dir_iterator);
  147. return ITER_ERROR;
  148. }
  149. int dir_iterator_abort(struct dir_iterator *dir_iterator)
  150. {
  151. struct dir_iterator_int *iter = (struct dir_iterator_int *)dir_iterator;
  152. for (; iter->levels_nr; iter->levels_nr--) {
  153. struct dir_iterator_level *level =
  154. &iter->levels[iter->levels_nr - 1];
  155. if (level->dir && closedir(level->dir)) {
  156. int saved_errno = errno;
  157. strbuf_setlen(&iter->base.path, level->prefix_len);
  158. errno = saved_errno;
  159. warning_errno("error closing directory '%s'",
  160. iter->base.path.buf);
  161. }
  162. }
  163. free(iter->levels);
  164. strbuf_release(&iter->base.path);
  165. free(iter);
  166. return ITER_DONE;
  167. }
  168. struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
  169. {
  170. struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
  171. struct dir_iterator *dir_iterator = &iter->base;
  172. int saved_errno;
  173. strbuf_init(&iter->base.path, PATH_MAX);
  174. strbuf_addstr(&iter->base.path, path);
  175. ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
  176. iter->levels_nr = 0;
  177. iter->flags = flags;
  178. /*
  179. * Note: stat already checks for NULL or empty strings and
  180. * inexistent paths.
  181. */
  182. if (stat(iter->base.path.buf, &iter->base.st) < 0) {
  183. saved_errno = errno;
  184. goto error_out;
  185. }
  186. if (!S_ISDIR(iter->base.st.st_mode)) {
  187. saved_errno = ENOTDIR;
  188. goto error_out;
  189. }
  190. return dir_iterator;
  191. error_out:
  192. dir_iterator_abort(dir_iterator);
  193. errno = saved_errno;
  194. return NULL;
  195. }