merge: use refresh_and_write_cache
[git/git.git] / dir-iterator.c
CommitLineData
0fe5043d
MH
1#include "cache.h"
2#include "dir.h"
3#include "iterator.h"
4#include "dir-iterator.h"
5
6struct dir_iterator_level {
0fe5043d
MH
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;
0fe5043d
MH
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 */
21struct dir_iterator_int {
22 struct dir_iterator base;
23
24 /*
3012397e
MT
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.
0fe5043d
MH
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;
fa1da7d2
MT
41
42 /* Combination of flags for this dir-iterator */
43 unsigned int flags;
0fe5043d
MH
44};
45
3012397e
MT
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
fa1da7d2 50 * otherwise, setting errno accordingly and leaving the stack unchanged.
3012397e
MT
51 */
52static int push_level(struct dir_iterator_int *iter)
53{
54 struct dir_iterator_level *level;
55
56 ALLOC_GROW(iter->levels, iter->levels_nr + 1, iter->levels_alloc);
57 level = &iter->levels[iter->levels_nr++];
58
59 if (!is_dir_sep(iter->base.path.buf[iter->base.path.len - 1]))
60 strbuf_addch(&iter->base.path, '/');
61 level->prefix_len = iter->base.path.len;
62
63 level->dir = opendir(iter->base.path.buf);
64 if (!level->dir) {
fa1da7d2 65 int saved_errno = errno;
3012397e
MT
66 if (errno != ENOENT) {
67 warning_errno("error opening directory '%s'",
68 iter->base.path.buf);
69 }
70 iter->levels_nr--;
fa1da7d2 71 errno = saved_errno;
3012397e
MT
72 return -1;
73 }
74
75 return 0;
76}
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 */
82static int pop_level(struct dir_iterator_int *iter)
83{
84 struct dir_iterator_level *level =
85 &iter->levels[iter->levels_nr - 1];
86
87 if (level->dir && closedir(level->dir))
88 warning_errno("error closing directory '%s'",
89 iter->base.path.buf);
90 level->dir = NULL;
91
92 return --iter->levels_nr;
93}
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
fa1da7d2 98 * otherwise, setting errno accordingly.
3012397e
MT
99 */
100static int prepare_next_entry_data(struct dir_iterator_int *iter,
101 struct dirent *de)
102{
fa1da7d2
MT
103 int err, saved_errno;
104
3012397e
MT
105 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 iter->base.relative_path = iter->base.path.buf +
111 iter->levels[0].prefix_len;
112 iter->base.basename = iter->base.path.buf +
113 iter->levels[iter->levels_nr - 1].prefix_len;
114
fa1da7d2
MT
115 if (iter->flags & DIR_ITERATOR_FOLLOW_SYMLINKS)
116 err = stat(iter->base.path.buf, &iter->base.st);
117 else
118 err = lstat(iter->base.path.buf, &iter->base.st);
3012397e 119
fa1da7d2
MT
120 saved_errno = errno;
121 if (err && errno != ENOENT)
122 warning_errno("failed to stat '%s'", iter->base.path.buf);
123
124 errno = saved_errno;
125 return err;
3012397e
MT
126}
127
0fe5043d
MH
128int dir_iterator_advance(struct dir_iterator *dir_iterator)
129{
130 struct dir_iterator_int *iter =
131 (struct dir_iterator_int *)dir_iterator;
132
fa1da7d2
MT
133 if (S_ISDIR(iter->base.st.st_mode) && push_level(iter)) {
134 if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC)
135 goto error_out;
136 if (iter->levels_nr == 0)
137 goto error_out;
3012397e
MT
138 }
139
140 /* Loop until we find an entry that we can give back to the caller. */
0fe5043d 141 while (1) {
3012397e 142 struct dirent *de;
0fe5043d
MH
143 struct dir_iterator_level *level =
144 &iter->levels[iter->levels_nr - 1];
0fe5043d 145
3012397e
MT
146 strbuf_setlen(&iter->base.path, level->prefix_len);
147 errno = 0;
148 de = readdir(level->dir);
149
150 if (!de) {
fa1da7d2 151 if (errno) {
3012397e 152 warning_errno("error reading directory '%s'",
c9bba372 153 iter->base.path.buf);
fa1da7d2
MT
154 if (iter->flags & DIR_ITERATOR_PEDANTIC)
155 goto error_out;
156 } else if (pop_level(iter) == 0) {
3012397e 157 return dir_iterator_abort(dir_iterator);
fa1da7d2 158 }
3012397e 159 continue;
0fe5043d
MH
160 }
161
3012397e
MT
162 if (is_dot_or_dotdot(de->d_name))
163 continue;
0fe5043d 164
fa1da7d2
MT
165 if (prepare_next_entry_data(iter, de)) {
166 if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC)
167 goto error_out;
0fe5043d 168 continue;
fa1da7d2 169 }
0fe5043d 170
3012397e 171 return ITER_OK;
0fe5043d 172 }
fa1da7d2
MT
173
174error_out:
175 dir_iterator_abort(dir_iterator);
176 return ITER_ERROR;
0fe5043d
MH
177}
178
179int dir_iterator_abort(struct dir_iterator *dir_iterator)
180{
181 struct dir_iterator_int *iter = (struct dir_iterator_int *)dir_iterator;
182
183 for (; iter->levels_nr; iter->levels_nr--) {
184 struct dir_iterator_level *level =
185 &iter->levels[iter->levels_nr - 1];
186
187 if (level->dir && closedir(level->dir)) {
c9bba372 188 int saved_errno = errno;
0fe5043d 189 strbuf_setlen(&iter->base.path, level->prefix_len);
c9bba372
MT
190 errno = saved_errno;
191 warning_errno("error closing directory '%s'",
192 iter->base.path.buf);
0fe5043d
MH
193 }
194 }
195
196 free(iter->levels);
197 strbuf_release(&iter->base.path);
198 free(iter);
199 return ITER_DONE;
200}
201
fa1da7d2 202struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
0fe5043d
MH
203{
204 struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
205 struct dir_iterator *dir_iterator = &iter->base;
3012397e 206 int saved_errno;
0fe5043d
MH
207
208 strbuf_init(&iter->base.path, PATH_MAX);
209 strbuf_addstr(&iter->base.path, path);
210
211 ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
3012397e 212 iter->levels_nr = 0;
fa1da7d2 213 iter->flags = flags;
0fe5043d 214
3012397e
MT
215 /*
216 * Note: stat already checks for NULL or empty strings and
217 * inexistent paths.
218 */
219 if (stat(iter->base.path.buf, &iter->base.st) < 0) {
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
231error_out:
232 dir_iterator_abort(dir_iterator);
233 errno = saved_errno;
234 return NULL;
0fe5043d 235}