Merge branch 'ps/t7800-variable-interpolation-fix'
[alt-git.git] / dir-iterator.c
blobde619846f29ad9c51d42b538d12c1b62e3958b5d
1 #include "git-compat-util.h"
2 #include "dir.h"
3 #include "iterator.h"
4 #include "dir-iterator.h"
5 #include "string-list.h"
7 struct dir_iterator_level {
8 DIR *dir;
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
13 * set to `NULL`.
15 struct string_list entries;
16 size_t entries_idx;
19 * The length of the directory part of path at this level
20 * (including a trailing '/'):
22 size_t prefix_len;
26 * The full data structure used to manage the internal directory
27 * iteration state. It includes members that are not part of the
28 * public interface.
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
38 * struct is freed.
40 size_t levels_nr;
42 /* The number of levels that have been allocated on the stack */
43 size_t levels_alloc;
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 */
52 unsigned int flags;
55 static int next_directory_entry(DIR *dir, const char *path,
56 struct dirent **out)
58 struct dirent *de;
60 repeat:
61 errno = 0;
62 de = readdir(dir);
63 if (!de) {
64 if (errno) {
65 warning_errno("error reading directory '%s'",
66 path);
67 return -1;
70 return 1;
73 if (is_dot_or_dotdot(de->d_name))
74 goto repeat;
76 *out = de;
77 return 0;
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);
98 if (!level->dir) {
99 int saved_errno = errno;
100 if (errno != ENOENT) {
101 warning_errno("error opening directory '%s'",
102 iter->base.path.buf);
104 iter->levels_nr--;
105 errno = saved_errno;
106 return -1;
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
114 * directly.
116 if (iter->flags & DIR_ITERATOR_SORTED) {
117 struct dirent *de;
119 while (1) {
120 int ret = next_directory_entry(level->dir, iter->base.path.buf, &de);
121 if (ret < 0) {
122 if (errno != ENOENT &&
123 iter->flags & DIR_ITERATOR_PEDANTIC)
124 return -1;
125 continue;
126 } else if (ret > 0) {
127 break;
130 string_list_append(&level->entries, de->d_name);
132 string_list_sort(&level->entries);
134 closedir(level->dir);
135 level->dir = NULL;
138 return 0;
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);
153 level->dir = NULL;
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,
165 const char *name)
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);
181 saved_errno = errno;
182 if (err && errno != ENOENT)
183 warning_errno("failed to stat '%s'", iter->base.path.buf);
185 errno = saved_errno;
186 return err;
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)
196 goto error_out;
197 if (iter->levels_nr == 0)
198 goto error_out;
201 /* Loop until we find an entry that we can give back to the caller. */
202 while (1) {
203 struct dirent *de;
204 struct dir_iterator_level *level =
205 &iter->levels[iter->levels_nr - 1];
206 const char *name;
208 strbuf_setlen(&iter->base.path, level->prefix_len);
210 if (level->dir) {
211 int ret = next_directory_entry(level->dir, iter->base.path.buf, &de);
212 if (ret < 0) {
213 if (iter->flags & DIR_ITERATOR_PEDANTIC)
214 goto error_out;
215 continue;
216 } else if (ret > 0) {
217 if (pop_level(iter) == 0)
218 return dir_iterator_abort(dir_iterator);
219 continue;
222 name = de->d_name;
223 } else {
224 if (level->entries_idx >= level->entries.nr) {
225 if (pop_level(iter) == 0)
226 return dir_iterator_abort(dir_iterator);
227 continue;
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)
235 goto error_out;
236 continue;
239 return ITER_OK;
242 error_out:
243 dir_iterator_abort(dir_iterator);
244 return ITER_ERROR;
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);
258 errno = saved_errno;
259 warning_errno("error closing directory '%s'",
260 iter->base.path.buf);
263 string_list_clear(&level->entries, 0);
266 free(iter->levels);
267 strbuf_release(&iter->base.path);
268 free(iter);
269 return ITER_DONE;
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);
282 iter->levels_nr = 0;
283 iter->flags = flags;
286 * Note: lstat already checks for NULL or empty strings and
287 * nonexistent paths.
289 err = lstat(iter->base.path.buf, &iter->base.st);
291 if (err < 0) {
292 saved_errno = errno;
293 goto error_out;
296 if (!S_ISDIR(iter->base.st.st_mode)) {
297 saved_errno = ENOTDIR;
298 goto error_out;
301 return dir_iterator;
303 error_out:
304 dir_iterator_abort(dir_iterator);
305 errno = saved_errno;
306 return NULL;