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