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