Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
58 | 0 | struct dirent *de; |
59 | |
|
60 | 0 | repeat: |
61 | 0 | errno = 0; |
62 | 0 | de = readdir(dir); |
63 | 0 | if (!de) { |
64 | 0 | if (errno) { |
65 | 0 | warning_errno("error reading directory '%s'", |
66 | 0 | path); |
67 | 0 | return -1; |
68 | 0 | } |
69 | | |
70 | 0 | return 1; |
71 | 0 | } |
72 | | |
73 | 0 | if (is_dot_or_dotdot(de->d_name)) |
74 | 0 | goto repeat; |
75 | | |
76 | 0 | *out = de; |
77 | 0 | return 0; |
78 | 0 | } |
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 | 0 | { |
88 | 0 | struct dir_iterator_level *level; |
89 | |
|
90 | 0 | ALLOC_GROW(iter->levels, iter->levels_nr + 1, iter->levels_alloc); |
91 | 0 | level = &iter->levels[iter->levels_nr++]; |
92 | |
|
93 | 0 | if (!is_dir_sep(iter->base.path.buf[iter->base.path.len - 1])) |
94 | 0 | strbuf_addch(&iter->base.path, '/'); |
95 | 0 | level->prefix_len = iter->base.path.len; |
96 | |
|
97 | 0 | level->dir = opendir(iter->base.path.buf); |
98 | 0 | if (!level->dir) { |
99 | 0 | int saved_errno = errno; |
100 | 0 | if (errno != ENOENT) { |
101 | 0 | warning_errno("error opening directory '%s'", |
102 | 0 | iter->base.path.buf); |
103 | 0 | } |
104 | 0 | iter->levels_nr--; |
105 | 0 | errno = saved_errno; |
106 | 0 | return -1; |
107 | 0 | } |
108 | | |
109 | 0 | string_list_init_dup(&level->entries); |
110 | 0 | level->entries_idx = 0; |
111 | | |
112 | | /* |
113 | | * When the iterator is sorted we read and sort all directory entries |
114 | | * directly. |
115 | | */ |
116 | 0 | if (iter->flags & DIR_ITERATOR_SORTED) { |
117 | 0 | struct dirent *de; |
118 | |
|
119 | 0 | while (1) { |
120 | 0 | int ret = next_directory_entry(level->dir, iter->base.path.buf, &de); |
121 | 0 | if (ret < 0) { |
122 | 0 | if (errno != ENOENT && |
123 | 0 | iter->flags & DIR_ITERATOR_PEDANTIC) |
124 | 0 | return -1; |
125 | 0 | continue; |
126 | 0 | } else if (ret > 0) { |
127 | 0 | break; |
128 | 0 | } |
129 | | |
130 | 0 | string_list_append(&level->entries, de->d_name); |
131 | 0 | } |
132 | 0 | string_list_sort(&level->entries); |
133 | |
|
134 | 0 | closedir(level->dir); |
135 | 0 | level->dir = NULL; |
136 | 0 | } |
137 | | |
138 | 0 | return 0; |
139 | 0 | } |
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 | 0 | { |
147 | 0 | struct dir_iterator_level *level = |
148 | 0 | &iter->levels[iter->levels_nr - 1]; |
149 | |
|
150 | 0 | if (level->dir && closedir(level->dir)) |
151 | 0 | warning_errno("error closing directory '%s'", |
152 | 0 | iter->base.path.buf); |
153 | 0 | level->dir = NULL; |
154 | 0 | string_list_clear(&level->entries, 0); |
155 | |
|
156 | 0 | return --iter->levels_nr; |
157 | 0 | } |
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 | 0 | { |
167 | 0 | int err, saved_errno; |
168 | |
|
169 | 0 | 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 | 0 | iter->base.relative_path = iter->base.path.buf + |
175 | 0 | iter->levels[0].prefix_len; |
176 | 0 | iter->base.basename = iter->base.path.buf + |
177 | 0 | iter->levels[iter->levels_nr - 1].prefix_len; |
178 | |
|
179 | 0 | err = lstat(iter->base.path.buf, &iter->base.st); |
180 | |
|
181 | 0 | saved_errno = errno; |
182 | 0 | if (err && errno != ENOENT) |
183 | 0 | warning_errno("failed to stat '%s'", iter->base.path.buf); |
184 | |
|
185 | 0 | errno = saved_errno; |
186 | 0 | return err; |
187 | 0 | } |
188 | | |
189 | | int dir_iterator_advance(struct dir_iterator *dir_iterator) |
190 | 0 | { |
191 | 0 | struct dir_iterator_int *iter = |
192 | 0 | (struct dir_iterator_int *)dir_iterator; |
193 | |
|
194 | 0 | if (S_ISDIR(iter->base.st.st_mode) && push_level(iter)) { |
195 | 0 | if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC) |
196 | 0 | goto error_out; |
197 | 0 | if (iter->levels_nr == 0) |
198 | 0 | goto error_out; |
199 | 0 | } |
200 | | |
201 | | /* Loop until we find an entry that we can give back to the caller. */ |
202 | 0 | while (1) { |
203 | 0 | struct dirent *de; |
204 | 0 | struct dir_iterator_level *level = |
205 | 0 | &iter->levels[iter->levels_nr - 1]; |
206 | 0 | const char *name; |
207 | |
|
208 | 0 | strbuf_setlen(&iter->base.path, level->prefix_len); |
209 | |
|
210 | 0 | if (level->dir) { |
211 | 0 | int ret = next_directory_entry(level->dir, iter->base.path.buf, &de); |
212 | 0 | if (ret < 0) { |
213 | 0 | if (iter->flags & DIR_ITERATOR_PEDANTIC) |
214 | 0 | goto error_out; |
215 | 0 | continue; |
216 | 0 | } else if (ret > 0) { |
217 | 0 | if (pop_level(iter) == 0) |
218 | 0 | return dir_iterator_abort(dir_iterator); |
219 | 0 | continue; |
220 | 0 | } |
221 | | |
222 | 0 | name = de->d_name; |
223 | 0 | } else { |
224 | 0 | if (level->entries_idx >= level->entries.nr) { |
225 | 0 | if (pop_level(iter) == 0) |
226 | 0 | return dir_iterator_abort(dir_iterator); |
227 | 0 | continue; |
228 | 0 | } |
229 | | |
230 | 0 | name = level->entries.items[level->entries_idx++].string; |
231 | 0 | } |
232 | | |
233 | 0 | if (prepare_next_entry_data(iter, name)) { |
234 | 0 | if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC) |
235 | 0 | goto error_out; |
236 | 0 | continue; |
237 | 0 | } |
238 | | |
239 | 0 | return ITER_OK; |
240 | 0 | } |
241 | | |
242 | 0 | error_out: |
243 | 0 | dir_iterator_abort(dir_iterator); |
244 | 0 | return ITER_ERROR; |
245 | 0 | } |
246 | | |
247 | | int dir_iterator_abort(struct dir_iterator *dir_iterator) |
248 | 0 | { |
249 | 0 | struct dir_iterator_int *iter = (struct dir_iterator_int *)dir_iterator; |
250 | |
|
251 | 0 | for (; iter->levels_nr; iter->levels_nr--) { |
252 | 0 | struct dir_iterator_level *level = |
253 | 0 | &iter->levels[iter->levels_nr - 1]; |
254 | |
|
255 | 0 | if (level->dir && closedir(level->dir)) { |
256 | 0 | int saved_errno = errno; |
257 | 0 | strbuf_setlen(&iter->base.path, level->prefix_len); |
258 | 0 | errno = saved_errno; |
259 | 0 | warning_errno("error closing directory '%s'", |
260 | 0 | iter->base.path.buf); |
261 | 0 | } |
262 | |
|
263 | 0 | string_list_clear(&level->entries, 0); |
264 | 0 | } |
265 | |
|
266 | 0 | free(iter->levels); |
267 | 0 | strbuf_release(&iter->base.path); |
268 | 0 | free(iter); |
269 | 0 | return ITER_DONE; |
270 | 0 | } |
271 | | |
272 | | struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags) |
273 | 0 | { |
274 | 0 | struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter)); |
275 | 0 | struct dir_iterator *dir_iterator = &iter->base; |
276 | 0 | int saved_errno, err; |
277 | |
|
278 | 0 | strbuf_init(&iter->base.path, PATH_MAX); |
279 | 0 | strbuf_addstr(&iter->base.path, path); |
280 | |
|
281 | 0 | ALLOC_GROW(iter->levels, 10, iter->levels_alloc); |
282 | 0 | iter->levels_nr = 0; |
283 | 0 | iter->flags = flags; |
284 | | |
285 | | /* |
286 | | * Note: lstat already checks for NULL or empty strings and |
287 | | * nonexistent paths. |
288 | | */ |
289 | 0 | err = lstat(iter->base.path.buf, &iter->base.st); |
290 | |
|
291 | 0 | if (err < 0) { |
292 | 0 | saved_errno = errno; |
293 | 0 | goto error_out; |
294 | 0 | } |
295 | | |
296 | 0 | if (!S_ISDIR(iter->base.st.st_mode)) { |
297 | 0 | saved_errno = ENOTDIR; |
298 | 0 | goto error_out; |
299 | 0 | } |
300 | | |
301 | 0 | return dir_iterator; |
302 | | |
303 | 0 | error_out: |
304 | 0 | dir_iterator_abort(dir_iterator); |
305 | 0 | errno = saved_errno; |
306 | 0 | return NULL; |
307 | 0 | } |