dir-iterator: refactor state machine model
[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;
41};
42
3012397e
MT
43/*
44 * Push a level in the iter stack and initialize it with information from
45 * the directory pointed by iter->base->path. It is assumed that this
46 * strbuf points to a valid directory path. Return 0 on success and -1
47 * otherwise, leaving the stack unchanged.
48 */
49static int push_level(struct dir_iterator_int *iter)
50{
51 struct dir_iterator_level *level;
52
53 ALLOC_GROW(iter->levels, iter->levels_nr + 1, iter->levels_alloc);
54 level = &iter->levels[iter->levels_nr++];
55
56 if (!is_dir_sep(iter->base.path.buf[iter->base.path.len - 1]))
57 strbuf_addch(&iter->base.path, '/');
58 level->prefix_len = iter->base.path.len;
59
60 level->dir = opendir(iter->base.path.buf);
61 if (!level->dir) {
62 if (errno != ENOENT) {
63 warning_errno("error opening directory '%s'",
64 iter->base.path.buf);
65 }
66 iter->levels_nr--;
67 return -1;
68 }
69
70 return 0;
71}
72
73/*
74 * Pop the top level on the iter stack, releasing any resources associated
75 * with it. Return the new value of iter->levels_nr.
76 */
77static int pop_level(struct dir_iterator_int *iter)
78{
79 struct dir_iterator_level *level =
80 &iter->levels[iter->levels_nr - 1];
81
82 if (level->dir && closedir(level->dir))
83 warning_errno("error closing directory '%s'",
84 iter->base.path.buf);
85 level->dir = NULL;
86
87 return --iter->levels_nr;
88}
89
90/*
91 * Populate iter->base with the necessary information on the next iteration
92 * entry, represented by the given dirent de. Return 0 on success and -1
93 * otherwise.
94 */
95static int prepare_next_entry_data(struct dir_iterator_int *iter,
96 struct dirent *de)
97{
98 strbuf_addstr(&iter->base.path, de->d_name);
99 /*
100 * We have to reset these because the path strbuf might have
101 * been realloc()ed at the previous strbuf_addstr().
102 */
103 iter->base.relative_path = iter->base.path.buf +
104 iter->levels[0].prefix_len;
105 iter->base.basename = iter->base.path.buf +
106 iter->levels[iter->levels_nr - 1].prefix_len;
107
108 if (lstat(iter->base.path.buf, &iter->base.st)) {
109 if (errno != ENOENT)
110 warning_errno("failed to stat '%s'", iter->base.path.buf);
111 return -1;
112 }
113
114 return 0;
115}
116
0fe5043d
MH
117int dir_iterator_advance(struct dir_iterator *dir_iterator)
118{
119 struct dir_iterator_int *iter =
120 (struct dir_iterator_int *)dir_iterator;
121
3012397e
MT
122 if (S_ISDIR(iter->base.st.st_mode)) {
123 if (push_level(iter) && iter->levels_nr == 0) {
124 /* Pushing the first level failed */
125 return dir_iterator_abort(dir_iterator);
126 }
127 }
128
129 /* Loop until we find an entry that we can give back to the caller. */
0fe5043d 130 while (1) {
3012397e 131 struct dirent *de;
0fe5043d
MH
132 struct dir_iterator_level *level =
133 &iter->levels[iter->levels_nr - 1];
0fe5043d 134
3012397e
MT
135 strbuf_setlen(&iter->base.path, level->prefix_len);
136 errno = 0;
137 de = readdir(level->dir);
138
139 if (!de) {
140 if (errno)
141 warning_errno("error reading directory '%s'",
c9bba372 142 iter->base.path.buf);
3012397e
MT
143 else if (pop_level(iter) == 0)
144 return dir_iterator_abort(dir_iterator);
145 continue;
0fe5043d
MH
146 }
147
3012397e
MT
148 if (is_dot_or_dotdot(de->d_name))
149 continue;
0fe5043d 150
3012397e 151 if (prepare_next_entry_data(iter, de))
0fe5043d 152 continue;
0fe5043d 153
3012397e 154 return ITER_OK;
0fe5043d
MH
155 }
156}
157
158int dir_iterator_abort(struct dir_iterator *dir_iterator)
159{
160 struct dir_iterator_int *iter = (struct dir_iterator_int *)dir_iterator;
161
162 for (; iter->levels_nr; iter->levels_nr--) {
163 struct dir_iterator_level *level =
164 &iter->levels[iter->levels_nr - 1];
165
166 if (level->dir && closedir(level->dir)) {
c9bba372 167 int saved_errno = errno;
0fe5043d 168 strbuf_setlen(&iter->base.path, level->prefix_len);
c9bba372
MT
169 errno = saved_errno;
170 warning_errno("error closing directory '%s'",
171 iter->base.path.buf);
0fe5043d
MH
172 }
173 }
174
175 free(iter->levels);
176 strbuf_release(&iter->base.path);
177 free(iter);
178 return ITER_DONE;
179}
180
181struct dir_iterator *dir_iterator_begin(const char *path)
182{
183 struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
184 struct dir_iterator *dir_iterator = &iter->base;
3012397e 185 int saved_errno;
0fe5043d
MH
186
187 strbuf_init(&iter->base.path, PATH_MAX);
188 strbuf_addstr(&iter->base.path, path);
189
190 ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
3012397e 191 iter->levels_nr = 0;
0fe5043d 192
3012397e
MT
193 /*
194 * Note: stat already checks for NULL or empty strings and
195 * inexistent paths.
196 */
197 if (stat(iter->base.path.buf, &iter->base.st) < 0) {
198 saved_errno = errno;
199 goto error_out;
200 }
201
202 if (!S_ISDIR(iter->base.st.st_mode)) {
203 saved_errno = ENOTDIR;
204 goto error_out;
205 }
0fe5043d
MH
206
207 return dir_iterator;
3012397e
MT
208
209error_out:
210 dir_iterator_abort(dir_iterator);
211 errno = saved_errno;
212 return NULL;
0fe5043d 213}