1 #include "git-compat-util.h"
4 #include "dir-iterator.h"
5 #include "string-list.h"
7 struct dir_iterator_level
{
11 * The directory entries of the current level. This list will only be
12 * populated when the iterator is ordered. In that case, `dir` will be
15 struct string_list entries
;
19 * The length of the directory part of path at this level
20 * (including a trailing '/'):
26 * The full data structure used to manage the internal directory
27 * iteration state. It includes members that are not part of the
30 struct dir_iterator_int
{
31 struct dir_iterator base
;
34 * The number of levels currently on the stack. After the first
35 * call to dir_iterator_begin(), if it succeeds to open the
36 * first level's dir, this will always be at least 1. Then,
37 * when it comes to zero the iteration is ended and this
42 /* The number of levels that have been allocated on the stack */
46 * A stack of levels. levels[0] is the uppermost directory
47 * that will be included in this iteration.
49 struct dir_iterator_level
*levels
;
51 /* Combination of flags for this dir-iterator */
55 static int next_directory_entry(DIR *dir
, const char *path
,
65 warning_errno("error reading directory '%s'",
73 if (is_dot_or_dotdot(de
->d_name
))
81 * Push a level in the iter stack and initialize it with information from
82 * the directory pointed by iter->base->path. It is assumed that this
83 * strbuf points to a valid directory path. Return 0 on success and -1
84 * otherwise, setting errno accordingly and leaving the stack unchanged.
86 static int push_level(struct dir_iterator_int
*iter
)
88 struct dir_iterator_level
*level
;
90 ALLOC_GROW(iter
->levels
, iter
->levels_nr
+ 1, iter
->levels_alloc
);
91 level
= &iter
->levels
[iter
->levels_nr
++];
93 if (!is_dir_sep(iter
->base
.path
.buf
[iter
->base
.path
.len
- 1]))
94 strbuf_addch(&iter
->base
.path
, '/');
95 level
->prefix_len
= iter
->base
.path
.len
;
97 level
->dir
= opendir(iter
->base
.path
.buf
);
99 int saved_errno
= errno
;
100 if (errno
!= ENOENT
) {
101 warning_errno("error opening directory '%s'",
102 iter
->base
.path
.buf
);
109 string_list_init_dup(&level
->entries
);
110 level
->entries_idx
= 0;
113 * When the iterator is sorted we read and sort all directory entries
116 if (iter
->flags
& DIR_ITERATOR_SORTED
) {
120 int ret
= next_directory_entry(level
->dir
, iter
->base
.path
.buf
, &de
);
122 if (errno
!= ENOENT
&&
123 iter
->flags
& DIR_ITERATOR_PEDANTIC
)
126 } else if (ret
> 0) {
130 string_list_append(&level
->entries
, de
->d_name
);
132 string_list_sort(&level
->entries
);
134 closedir(level
->dir
);
142 * Pop the top level on the iter stack, releasing any resources associated
143 * with it. Return the new value of iter->levels_nr.
145 static int pop_level(struct dir_iterator_int
*iter
)
147 struct dir_iterator_level
*level
=
148 &iter
->levels
[iter
->levels_nr
- 1];
150 if (level
->dir
&& closedir(level
->dir
))
151 warning_errno("error closing directory '%s'",
152 iter
->base
.path
.buf
);
154 string_list_clear(&level
->entries
, 0);
156 return --iter
->levels_nr
;
160 * Populate iter->base with the necessary information on the next iteration
161 * entry, represented by the given name. Return 0 on success and -1
162 * otherwise, setting errno accordingly.
164 static int prepare_next_entry_data(struct dir_iterator_int
*iter
,
167 int err
, saved_errno
;
169 strbuf_addstr(&iter
->base
.path
, name
);
171 * We have to reset these because the path strbuf might have
172 * been realloc()ed at the previous strbuf_addstr().
174 iter
->base
.relative_path
= iter
->base
.path
.buf
+
175 iter
->levels
[0].prefix_len
;
176 iter
->base
.basename
= iter
->base
.path
.buf
+
177 iter
->levels
[iter
->levels_nr
- 1].prefix_len
;
179 err
= lstat(iter
->base
.path
.buf
, &iter
->base
.st
);
182 if (err
&& errno
!= ENOENT
)
183 warning_errno("failed to stat '%s'", iter
->base
.path
.buf
);
189 int dir_iterator_advance(struct dir_iterator
*dir_iterator
)
191 struct dir_iterator_int
*iter
=
192 (struct dir_iterator_int
*)dir_iterator
;
194 if (S_ISDIR(iter
->base
.st
.st_mode
) && push_level(iter
)) {
195 if (errno
!= ENOENT
&& iter
->flags
& DIR_ITERATOR_PEDANTIC
)
197 if (iter
->levels_nr
== 0)
201 /* Loop until we find an entry that we can give back to the caller. */
204 struct dir_iterator_level
*level
=
205 &iter
->levels
[iter
->levels_nr
- 1];
208 strbuf_setlen(&iter
->base
.path
, level
->prefix_len
);
211 int ret
= next_directory_entry(level
->dir
, iter
->base
.path
.buf
, &de
);
213 if (iter
->flags
& DIR_ITERATOR_PEDANTIC
)
216 } else if (ret
> 0) {
217 if (pop_level(iter
) == 0)
218 return dir_iterator_abort(dir_iterator
);
224 if (level
->entries_idx
>= level
->entries
.nr
) {
225 if (pop_level(iter
) == 0)
226 return dir_iterator_abort(dir_iterator
);
230 name
= level
->entries
.items
[level
->entries_idx
++].string
;
233 if (prepare_next_entry_data(iter
, name
)) {
234 if (errno
!= ENOENT
&& iter
->flags
& DIR_ITERATOR_PEDANTIC
)
243 dir_iterator_abort(dir_iterator
);
247 int dir_iterator_abort(struct dir_iterator
*dir_iterator
)
249 struct dir_iterator_int
*iter
= (struct dir_iterator_int
*)dir_iterator
;
251 for (; iter
->levels_nr
; iter
->levels_nr
--) {
252 struct dir_iterator_level
*level
=
253 &iter
->levels
[iter
->levels_nr
- 1];
255 if (level
->dir
&& closedir(level
->dir
)) {
256 int saved_errno
= errno
;
257 strbuf_setlen(&iter
->base
.path
, level
->prefix_len
);
259 warning_errno("error closing directory '%s'",
260 iter
->base
.path
.buf
);
263 string_list_clear(&level
->entries
, 0);
267 strbuf_release(&iter
->base
.path
);
272 struct dir_iterator
*dir_iterator_begin(const char *path
, unsigned int flags
)
274 struct dir_iterator_int
*iter
= xcalloc(1, sizeof(*iter
));
275 struct dir_iterator
*dir_iterator
= &iter
->base
;
276 int saved_errno
, err
;
278 strbuf_init(&iter
->base
.path
, PATH_MAX
);
279 strbuf_addstr(&iter
->base
.path
, path
);
281 ALLOC_GROW(iter
->levels
, 10, iter
->levels_alloc
);
286 * Note: lstat already checks for NULL or empty strings and
289 err
= lstat(iter
->base
.path
.buf
, &iter
->base
.st
);
296 if (!S_ISDIR(iter
->base
.st
.st_mode
)) {
297 saved_errno
= ENOTDIR
;
304 dir_iterator_abort(dir_iterator
);