dir-iterator: add flags parameter to dir_iterator_begin

Add the possibility of giving flags to dir_iterator_begin to initialize a dir-iterator with special options. Currently possible flags are: - DIR_ITERATOR_PEDANTIC, which makes dir_iterator_advance abort immediately in the case of an error, instead of keep looking for the next valid entry; - DIR_ITERATOR_FOLLOW_SYMLINKS, which makes the iterator follow symlinks and include linked directories' contents in the iteration. These new flags will be used in a subsequent patch. Also add tests for the flags' usage and adjust refs/files-backend.c to the new dir_iterator_begin signature. Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matheus Tavares committed Jul 10, 2019 at 20:59 UTC fa1da7d2eef0fedf0e5942028513912bf2bb64ed
5 files changed +191 -36
dir-iterator.c
+39 -17
@@ -38,13 +38,16 @@ struct dir_iterator_int {
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
47 - * otherwise, leaving the stack unchanged.
50 + * otherwise, setting errno accordingly and leaving the stack unchanged.
51 */
52 static int push_level(struct dir_iterator_int *iter)
53 {
@@ -59,11 +62,13 @@ static int push_level(struct dir_iterator_int *iter)
62
63 level->dir = opendir(iter->base.path.buf);
64 if (!level->dir) {
65 + int saved_errno = errno;
66 if (errno != ENOENT) {
67 warning_errno("error opening directory '%s'",
68 iter->base.path.buf);
69 }
70 iter->levels_nr--;
71 + errno = saved_errno;
72 return -1;
73 }
74
@@ -90,11 +95,13 @@ static int pop_level(struct dir_iterator_int *iter)
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
93 - * otherwise.
98 + * otherwise, setting errno accordingly.
99 */
100 static int prepare_next_entry_data(struct dir_iterator_int *iter,
101 struct dirent *de)
102 {
103 + int err, saved_errno;
104 +
105 strbuf_addstr(&iter->base.path, de->d_name);
106 /*
107 * We have to reset these because the path strbuf might have
@@ -105,13 +112,17 @@ static int prepare_next_entry_data(struct dir_iterator_int *iter,
112 iter->base.basename = iter->base.path.buf +
113 iter->levels[iter->levels_nr - 1].prefix_len;
114
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 - }
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);
119
114 - return 0;
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;
126 }
127
128 int dir_iterator_advance(struct dir_iterator *dir_iterator)
@@ -119,11 +130,11 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
130 struct dir_iterator_int *iter =
131 (struct dir_iterator_int *)dir_iterator;
132
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 - }
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;
138 }
139
140 /* Loop until we find an entry that we can give back to the caller. */
@@ -137,22 +148,32 @@ int dir_iterator_advance(struct dir_iterator *dir_iterator)
148 de = readdir(level->dir);
149
150 if (!de) {
140 - if (errno)
151 + if (errno) {
152 warning_errno("error reading directory '%s'",
153 iter->base.path.buf);
143 - else if (pop_level(iter) == 0)
154 + if (iter->flags & DIR_ITERATOR_PEDANTIC)
155 + goto error_out;
156 + } else if (pop_level(iter) == 0) {
157 return dir_iterator_abort(dir_iterator);
158 + }
159 continue;
160 }
161
162 if (is_dot_or_dotdot(de->d_name))
163 continue;
164
151 - if (prepare_next_entry_data(iter, de))
165 + if (prepare_next_entry_data(iter, de)) {
166 + if (errno != ENOENT && iter->flags & DIR_ITERATOR_PEDANTIC)
167 + goto error_out;
168 continue;
169 + }
170
171 return ITER_OK;
172 }
173 +
174 +error_out:
175 + dir_iterator_abort(dir_iterator);
176 + return ITER_ERROR;
177 }
178
179 int dir_iterator_abort(struct dir_iterator *dir_iterator)
@@ -178,7 +199,7 @@ int dir_iterator_abort(struct dir_iterator *dir_iterator)
199 return ITER_DONE;
200 }
201
181 -struct dir_iterator *dir_iterator_begin(const char *path)
202 +struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
203 {
204 struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
205 struct dir_iterator *dir_iterator = &iter->base;
@@ -189,6 +210,7 @@ struct dir_iterator *dir_iterator_begin(const char *path)
210
211 ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
212 iter->levels_nr = 0;
213 + iter->flags = flags;
214
215 /*
216 * Note: stat already checks for NULL or empty strings and
dir-iterator.h
+44 -11
@@ -20,7 +20,8 @@
20 * A typical iteration looks like this:
21 *
22 * int ok;
23 - * struct dir_iterator *iter = dir_iterator_begin(path);
23 + * unsigned int flags = DIR_ITERATOR_PEDANTIC;
24 + * struct dir_iterator *iter = dir_iterator_begin(path, flags);
25 *
26 * if (!iter)
27 * goto error_handler;
@@ -44,6 +45,29 @@
45 * dir_iterator_advance() again.
46 */
47
48 +/*
49 + * Flags for dir_iterator_begin:
50 + *
51 + * - DIR_ITERATOR_PEDANTIC: override dir-iterator's default behavior
52 + * in case of an error at dir_iterator_advance(), which is to keep
53 + * looking for a next valid entry. With this flag, resources are freed
54 + * and ITER_ERROR is returned immediately. In both cases, a meaningful
55 + * warning is emitted. Note: ENOENT errors are always ignored so that
56 + * the API users may remove files during iteration.
57 + *
58 + * - DIR_ITERATOR_FOLLOW_SYMLINKS: make dir-iterator follow symlinks.
59 + * i.e., linked directories' contents will be iterated over and
60 + * iter->base.st will contain information on the referred files,
61 + * not the symlinks themselves, which is the default behavior. Broken
62 + * symlinks are ignored.
63 + *
64 + * Warning: circular symlinks are also followed when
65 + * DIR_ITERATOR_FOLLOW_SYMLINKS is set. The iteration may end up with
66 + * an ELOOP if they happen and DIR_ITERATOR_PEDANTIC is set.
67 + */
68 +#define DIR_ITERATOR_PEDANTIC (1 << 0)
69 +#define DIR_ITERATOR_FOLLOW_SYMLINKS (1 << 1)
70 +
71 struct dir_iterator {
72 /* The current path: */
73 struct strbuf path;
@@ -58,29 +82,38 @@ struct dir_iterator {
82 /* The current basename: */
83 const char *basename;
84
61 - /* The result of calling lstat() on path: */
85 + /*
86 + * The result of calling lstat() on path; or stat(), if the
87 + * DIR_ITERATOR_FOLLOW_SYMLINKS flag was set at
88 + * dir_iterator's initialization.
89 + */
90 struct stat st;
91 };
92
93 /*
66 - * Start a directory iteration over path. On success, return a
67 - * dir_iterator that holds the internal state of the iteration.
68 - * In case of failure, return NULL and set errno accordingly.
94 + * Start a directory iteration over path with the combination of
95 + * options specified by flags. On success, return a dir_iterator
96 + * that holds the internal state of the iteration. In case of
97 + * failure, return NULL and set errno accordingly.
98 *
99 * The iteration includes all paths under path, not including path
100 * itself and not including "." or ".." entries.
101 *
73 - * path is the starting directory. An internal copy will be made.
102 + * Parameters are:
103 + * - path is the starting directory. An internal copy will be made.
104 + * - flags is a combination of the possible flags to initialize a
105 + * dir-iterator or 0 for default behavior.
106 */
75 -struct dir_iterator *dir_iterator_begin(const char *path);
107 +struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags);
108
109 /*
110 * Advance the iterator to the first or next item and return ITER_OK.
111 * If the iteration is exhausted, free the dir_iterator and any
80 - * resources associated with it and return ITER_DONE. On error, free
81 - * dir_iterator and associated resources and return ITER_ERROR. It is
82 - * a bug to use iterator or call this function again after it has
83 - * returned ITER_DONE or ITER_ERROR.
112 + * resources associated with it and return ITER_DONE.
113 + *
114 + * It is a bug to use iterator or call this function again after it
115 + * has returned ITER_DONE or ITER_ERROR (which may be returned iff
116 + * the DIR_ITERATOR_PEDANTIC flag was set).
117 */
118 int dir_iterator_advance(struct dir_iterator *iterator);
119
refs/files-backend.c
+1 -1
@@ -2150,7 +2150,7 @@ static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
2150
2151 strbuf_addf(&sb, "%s/logs", gitdir);
2152
2153 - diter = dir_iterator_begin(sb.buf);
2153 + diter = dir_iterator_begin(sb.buf, 0);
2154 if(!diter)
2155 return empty_ref_iterator_begin();
2156
t/helper/test-dir-iterator.c
+27 -7
@@ -4,29 +4,44 @@
4 #include "iterator.h"
5 #include "dir-iterator.h"
6
7 -/* Argument is a directory path to iterate over */
7 +/*
8 + * usage:
9 + * tool-test dir-iterator [--follow-symlinks] [--pedantic] directory_path
10 + */
11 int cmd__dir_iterator(int argc, const char **argv)
12 {
13 struct strbuf path = STRBUF_INIT;
14 struct dir_iterator *diter;
15 + unsigned int flags = 0;
16 + int iter_status;
17 +
18 + for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
19 + if (strcmp(*argv, "--follow-symlinks") == 0)
20 + flags |= DIR_ITERATOR_FOLLOW_SYMLINKS;
21 + else if (strcmp(*argv, "--pedantic") == 0)
22 + flags |= DIR_ITERATOR_PEDANTIC;
23 + else
24 + die("invalid option '%s'", *argv);
25 + }
26
13 - if (argc < 2)
14 - die("BUG: test-dir-iterator needs one argument");
15 -
16 - strbuf_add(&path, argv[1], strlen(argv[1]));
27 + if (!*argv || argc != 1)
28 + die("dir-iterator needs exactly one non-option argument");
29
18 - diter = dir_iterator_begin(path.buf);
30 + strbuf_add(&path, *argv, strlen(*argv));
31 + diter = dir_iterator_begin(path.buf, flags);
32
33 if (!diter) {
34 printf("dir_iterator_begin failure: %d\n", errno);
35 exit(EXIT_FAILURE);
36 }
37
25 - while (dir_iterator_advance(diter) == ITER_OK) {
38 + while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) {
39 if (S_ISDIR(diter->st.st_mode))
40 printf("[d] ");
41 else if (S_ISREG(diter->st.st_mode))
42 printf("[f] ");
43 + else if (S_ISLNK(diter->st.st_mode))
44 + printf("[s] ");
45 else
46 printf("[?] ");
47
@@ -34,5 +49,10 @@ int cmd__dir_iterator(int argc, const char **argv)
49 diter->path.buf);
50 }
51
52 + if (iter_status != ITER_DONE) {
53 + printf("dir_iterator_advance failure\n");
54 + return 1;
55 + }
56 +
57 return 0;
58 }
t/t0066-dir-iterator.sh
+80
@@ -65,4 +65,84 @@ test_expect_success 'begin should fail upon non directory paths' '
65 test_cmp expected-non-dir-output actual-non-dir-output
66 '
67
68 +test_expect_success POSIXPERM,SANITY 'advance should not fail on errors by default' '
69 + cat >expected-no-permissions-output <<-EOF &&
70 + [d] (a) [a] ./dir3/a
71 + EOF
72 +
73 + mkdir -p dir3/a &&
74 + >dir3/a/b &&
75 + chmod 0 dir3/a &&
76 +
77 + test-tool dir-iterator ./dir3 >actual-no-permissions-output &&
78 + test_cmp expected-no-permissions-output actual-no-permissions-output &&
79 + chmod 755 dir3/a &&
80 + rm -rf dir3
81 +'
82 +
83 +test_expect_success POSIXPERM,SANITY 'advance should fail on errors, w/ pedantic flag' '
84 + cat >expected-no-permissions-pedantic-output <<-EOF &&
85 + [d] (a) [a] ./dir3/a
86 + dir_iterator_advance failure
87 + EOF
88 +
89 + mkdir -p dir3/a &&
90 + >dir3/a/b &&
91 + chmod 0 dir3/a &&
92 +
93 + test_must_fail test-tool dir-iterator --pedantic ./dir3 \
94 + >actual-no-permissions-pedantic-output &&
95 + test_cmp expected-no-permissions-pedantic-output \
96 + actual-no-permissions-pedantic-output &&
97 + chmod 755 dir3/a &&
98 + rm -rf dir3
99 +'
100 +
101 +test_expect_success SYMLINKS 'setup dirs with symlinks' '
102 + mkdir -p dir4/a &&
103 + mkdir -p dir4/b/c &&
104 + >dir4/a/d &&
105 + ln -s d dir4/a/e &&
106 + ln -s ../b dir4/a/f &&
107 +
108 + mkdir -p dir5/a/b &&
109 + mkdir -p dir5/a/c &&
110 + ln -s ../c dir5/a/b/d &&
111 + ln -s ../ dir5/a/b/e &&
112 + ln -s ../../ dir5/a/b/f
113 +'
114 +
115 +test_expect_success SYMLINKS 'dir-iterator should not follow symlinks by default' '
116 + cat >expected-no-follow-sorted-output <<-EOF &&
117 + [d] (a) [a] ./dir4/a
118 + [d] (b) [b] ./dir4/b
119 + [d] (b/c) [c] ./dir4/b/c
120 + [f] (a/d) [d] ./dir4/a/d
121 + [s] (a/e) [e] ./dir4/a/e
122 + [s] (a/f) [f] ./dir4/a/f
123 + EOF
124 +
125 + test-tool dir-iterator ./dir4 >out &&
126 + sort out >actual-no-follow-sorted-output &&
127 +
128 + test_cmp expected-no-follow-sorted-output actual-no-follow-sorted-output
129 +'
130 +
131 +test_expect_success SYMLINKS 'dir-iterator should follow symlinks w/ follow flag' '
132 + cat >expected-follow-sorted-output <<-EOF &&
133 + [d] (a) [a] ./dir4/a
134 + [d] (a/f) [f] ./dir4/a/f
135 + [d] (a/f/c) [c] ./dir4/a/f/c
136 + [d] (b) [b] ./dir4/b
137 + [d] (b/c) [c] ./dir4/b/c
138 + [f] (a/d) [d] ./dir4/a/d
139 + [f] (a/e) [e] ./dir4/a/e
140 + EOF
141 +
142 + test-tool dir-iterator --follow-symlinks ./dir4 >out &&
143 + sort out >actual-follow-sorted-output &&
144 +
145 + test_cmp expected-follow-sorted-output actual-follow-sorted-output
146 +'
147 +
148 test_done