Skip to content

Commit fa1da7d

Browse files
matheustavaresgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3012397 commit fa1da7d

File tree

5 files changed

+191
-36
lines changed

5 files changed

+191
-36
lines changed

dir-iterator.c

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ struct dir_iterator_int {
3838
* that will be included in this iteration.
3939
*/
4040
struct dir_iterator_level *levels;
41+
42+
/* Combination of flags for this dir-iterator */
43+
unsigned int flags;
4144
};
4245

4346
/*
4447
* Push a level in the iter stack and initialize it with information from
4548
* the directory pointed by iter->base->path. It is assumed that this
4649
* 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.
4851
*/
4952
static int push_level(struct dir_iterator_int *iter)
5053
{
@@ -59,11 +62,13 @@ static int push_level(struct dir_iterator_int *iter)
5962

6063
level->dir = opendir(iter->base.path.buf);
6164
if (!level->dir) {
65+
int saved_errno = errno;
6266
if (errno != ENOENT) {
6367
warning_errno("error opening directory '%s'",
6468
iter->base.path.buf);
6569
}
6670
iter->levels_nr--;
71+
errno = saved_errno;
6772
return -1;
6873
}
6974

@@ -90,11 +95,13 @@ static int pop_level(struct dir_iterator_int *iter)
9095
/*
9196
* Populate iter->base with the necessary information on the next iteration
9297
* entry, represented by the given dirent de. Return 0 on success and -1
93-
* otherwise.
98+
* otherwise, setting errno accordingly.
9499
*/
95100
static int prepare_next_entry_data(struct dir_iterator_int *iter,
96101
struct dirent *de)
97102
{
103+
int err, saved_errno;
104+
98105
strbuf_addstr(&iter->base.path, de->d_name);
99106
/*
100107
* We have to reset these because the path strbuf might have
@@ -105,25 +112,29 @@ static int prepare_next_entry_data(struct dir_iterator_int *iter,
105112
iter->base.basename = iter->base.path.buf +
106113
iter->levels[iter->levels_nr - 1].prefix_len;
107114

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);
113119

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;
115126
}
116127

117128
int dir_iterator_advance(struct dir_iterator *dir_iterator)
118129
{
119130
struct dir_iterator_int *iter =
120131
(struct dir_iterator_int *)dir_iterator;
121132

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;
127138
}
128139

129140
/* 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)
137148
de = readdir(level->dir);
138149

139150
if (!de) {
140-
if (errno)
151+
if (errno) {
141152
warning_errno("error reading directory '%s'",
142153
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) {
144157
return dir_iterator_abort(dir_iterator);
158+
}
145159
continue;
146160
}
147161

148162
if (is_dot_or_dotdot(de->d_name))
149163
continue;
150164

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;
152168
continue;
169+
}
153170

154171
return ITER_OK;
155172
}
173+
174+
error_out:
175+
dir_iterator_abort(dir_iterator);
176+
return ITER_ERROR;
156177
}
157178

158179
int dir_iterator_abort(struct dir_iterator *dir_iterator)
@@ -178,7 +199,7 @@ int dir_iterator_abort(struct dir_iterator *dir_iterator)
178199
return ITER_DONE;
179200
}
180201

181-
struct dir_iterator *dir_iterator_begin(const char *path)
202+
struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags)
182203
{
183204
struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
184205
struct dir_iterator *dir_iterator = &iter->base;
@@ -189,6 +210,7 @@ struct dir_iterator *dir_iterator_begin(const char *path)
189210

190211
ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
191212
iter->levels_nr = 0;
213+
iter->flags = flags;
192214

193215
/*
194216
* Note: stat already checks for NULL or empty strings and

dir-iterator.h

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
* A typical iteration looks like this:
2121
*
2222
* 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);
2425
*
2526
* if (!iter)
2627
* goto error_handler;
@@ -44,6 +45,29 @@
4445
* dir_iterator_advance() again.
4546
*/
4647

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+
4771
struct dir_iterator {
4872
/* The current path: */
4973
struct strbuf path;
@@ -58,29 +82,38 @@ struct dir_iterator {
5882
/* The current basename: */
5983
const char *basename;
6084

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+
*/
6290
struct stat st;
6391
};
6492

6593
/*
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.
6998
*
7099
* The iteration includes all paths under path, not including path
71100
* itself and not including "." or ".." entries.
72101
*
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.
74106
*/
75-
struct dir_iterator *dir_iterator_begin(const char *path);
107+
struct dir_iterator *dir_iterator_begin(const char *path, unsigned int flags);
76108

77109
/*
78110
* Advance the iterator to the first or next item and return ITER_OK.
79111
* 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).
84117
*/
85118
int dir_iterator_advance(struct dir_iterator *iterator);
86119

refs/files-backend.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2150,7 +2150,7 @@ static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
21502150

21512151
strbuf_addf(&sb, "%s/logs", gitdir);
21522152

2153-
diter = dir_iterator_begin(sb.buf);
2153+
diter = dir_iterator_begin(sb.buf, 0);
21542154
if(!diter)
21552155
return empty_ref_iterator_begin();
21562156

t/helper/test-dir-iterator.c

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,55 @@
44
#include "iterator.h"
55
#include "dir-iterator.h"
66

7-
/* Argument is a directory path to iterate over */
7+
/*
8+
* usage:
9+
* tool-test dir-iterator [--follow-symlinks] [--pedantic] directory_path
10+
*/
811
int cmd__dir_iterator(int argc, const char **argv)
912
{
1013
struct strbuf path = STRBUF_INIT;
1114
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+
}
1226

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");
1729

18-
diter = dir_iterator_begin(path.buf);
30+
strbuf_add(&path, *argv, strlen(*argv));
31+
diter = dir_iterator_begin(path.buf, flags);
1932

2033
if (!diter) {
2134
printf("dir_iterator_begin failure: %d\n", errno);
2235
exit(EXIT_FAILURE);
2336
}
2437

25-
while (dir_iterator_advance(diter) == ITER_OK) {
38+
while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) {
2639
if (S_ISDIR(diter->st.st_mode))
2740
printf("[d] ");
2841
else if (S_ISREG(diter->st.st_mode))
2942
printf("[f] ");
43+
else if (S_ISLNK(diter->st.st_mode))
44+
printf("[s] ");
3045
else
3146
printf("[?] ");
3247

3348
printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
3449
diter->path.buf);
3550
}
3651

52+
if (iter_status != ITER_DONE) {
53+
printf("dir_iterator_advance failure\n");
54+
return 1;
55+
}
56+
3757
return 0;
3858
}

t/t0066-dir-iterator.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,84 @@ test_expect_success 'begin should fail upon non directory paths' '
6565
test_cmp expected-non-dir-output actual-non-dir-output
6666
'
6767

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+
68148
test_done

0 commit comments

Comments
 (0)