]> git.ipfire.org Git - thirdparty/git.git/blob - dir-iterator.c
Merge branch 'ps/run-auto-maintenance-in-receive-pack'
[thirdparty/git.git] / dir-iterator.c
1 #include "git-compat-util.h"
2 #include "dir.h"
3 #include "iterator.h"
4 #include "dir-iterator.h"
5 #include "string-list.h"
6
7 struct dir_iterator_level {
8 DIR *dir;
9
10 /*
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`.
14 */
15 struct string_list entries;
16 size_t entries_idx;
17
18 /*
19 * The length of the directory part of path at this level
20 * (including a trailing '/'):
21 */
22 size_t prefix_len;
23 };
24
25 /*
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.
29 */
30 struct dir_iterator_int {
31 struct dir_iterator base;
32
33 /*
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.
39 */
40 size_t levels_nr;
41
42 /* The number of levels that have been allocated on the stack */
43 size_t levels_alloc;
44
45 /*
46 * A stack of levels. levels[0] is the uppermost directory
47 * that will be included in this iteration.
48 */
49 struct dir_iterator_level *levels;
50
51 /* Combination of flags for this dir-iterator */
52 unsigned int flags;
53 };
54
55 static int next_directory_entry(DIR *dir, const char *path,
56 struct dirent **out)
57 {
58 struct dirent *de;
59
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;
68 }
69
70 return 1;
71 }
72
73 if (is_dot_or_dotdot(de->d_name))
74 goto repeat;
75
76 *out = de;
77 return 0;
78 }
79
80 /*
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.
85 */
86 static int push_level(struct dir_iterator_int *iter)
87 {
88 struct dir_iterator_level *level;
89
90 ALLOC_GROW(iter->levels, iter->levels_nr + 1, iter->levels_alloc);
91 level = &iter->levels[iter->levels_nr++];
92
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;
96
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);
103 }
104 iter->levels_nr--;
105 errno = saved_errno;
106 return -1;
107 }
108
109 string_list_init_dup(&level->entries);
110 level->entries_idx = 0;
111
112 /*
113 * When the iterator is sorted we read and sort all directory entries
114 * directly.
115 */
116 if (iter->flags & DIR_ITERATOR_SORTED) {
117 struct dirent *de;
118
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;
128 }
129
130 string_list_append(&level->entries, de->d_name);
131 }
132 string_list_sort(&level->entries);
133
134 closedir(level->dir);
135 level->dir = NULL;
136 }
137
138 return 0;
139 }
140
141 /*
142 * Pop the top level on the iter stack, releasing any resources associated
143 * with it. Return the new value of iter->levels_nr.
144 */
145 static int pop_level(struct dir_iterator_int *iter)
146 {
147 struct dir_iterator_level *level =
148 &iter->levels[iter->levels_nr - 1];
149
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);
155
156 return --iter->levels_nr;
157 }
158
159 /*
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.
163 */
164 static int prepare_next_entry_data(struct dir_iterator_int *iter,
165 const char *name)
166 {
167 int err, saved_errno;
168
169 strbuf_addstr(&iter->base.path, name);
170 /*
171 * We have to reset these because the path strbuf might have
172 * been realloc()ed at the previous strbuf_addstr().
173 */
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;
178
179 err = lstat(iter->base.path.buf, &iter->base.st);
180
181 saved_errno = errno;
182 if (err && errno != ENOENT)
183 warning_errno("failed to stat '%s'", iter->base.path.buf);
184
185 errno = saved_errno;
186 return err;
187 }
188
189 int dir_iterator_advance(struct dir_iterator *dir_iterator)
190 {
191 struct dir_iterator_int *iter =
192 (struct dir_iterator_int *)dir_iterator;
193
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;
199 }
200
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;
207
208 strbuf_setlen(&iter->base.path, level->prefix_len);
209
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;
220 }
221
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;
228 }
229
230 name = level->entries.items[level->entries_idx++].string;
231 }
232
233 if (prepare_next_entry_data(iter, name)) {
234 if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC)
235 goto error_out;
236 continue;
237 }
238
239 return ITER_OK;
240 }
241
242 error_out:
243 dir_iterator_abort(dir_iterator);
244 return ITER_ERROR;
245 }
246
247 int dir_iterator_abort(struct dir_iterator *dir_iterator)
248 {
249 struct dir_iterator_int *iter = (struct dir_iterator_int *)dir_iterator;
250
251 for (; iter->levels_nr; iter->levels_nr--) {
252 struct dir_iterator_level *level =
253 &iter->levels[iter->levels_nr - 1];
254
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);
261 }
262
263 string_list_clear(&level->entries, 0);
264 }
265
266 free(iter->levels);
267 strbuf_release(&iter->base.path);
268 free(iter);
269 return ITER_DONE;
270 }
271
272 struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
273 {
274 struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
275 struct dir_iterator *dir_iterator = &iter->base;
276 int saved_errno, err;
277
278 strbuf_init(&iter->base.path, PATH_MAX);
279 strbuf_addstr(&iter->base.path, path);
280
281 ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
282 iter->levels_nr = 0;
283 iter->flags = flags;
284
285 /*
286 * Note: lstat already checks for NULL or empty strings and
287 * nonexistent paths.
288 */
289 err = lstat(iter->base.path.buf, &iter->base.st);
290
291 if (err < 0) {
292 saved_errno = errno;
293 goto error_out;
294 }
295
296 if (!S_ISDIR(iter->base.st.st_mode)) {
297 saved_errno = ENOTDIR;
298 goto error_out;
299 }
300
301 return dir_iterator;
302
303 error_out:
304 dir_iterator_abort(dir_iterator);
305 errno = saved_errno;
306 return NULL;
307 }