Skip to content

Commit 3012397

Browse files
matheustavaresgitster
authored andcommitted
dir-iterator: refactor state machine model
dir_iterator_advance() is a large function with two nested loops. Let's improve its readability factoring out three functions and simplifying its mechanics. The refactored model will no longer depend on level.initialized and level.dir_state to keep track of the iteration state and will perform on a single loop. Also, dir_iterator_begin() currently does not check if the given string represents a valid directory path. Since the refactored model will have to stat() the given path at initialization, let's also check for this kind of error and make dir_iterator_begin() return NULL, on failures, with errno appropriately set. And add tests for this new behavior. Improve documentation at dir-iteration.h and code comments at dir-iterator.c to reflect the changes and eliminate possible ambiguities. Finally, adjust refs/files-backend.c to check for now possible dir_iterator_begin() failures. Original-patch-by: Daniel Ferreira <[email protected]> Signed-off-by: Matheus Tavares <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c9bba37 commit 3012397

File tree

5 files changed

+164
-122
lines changed

5 files changed

+164
-122
lines changed

dir-iterator.c

Lines changed: 122 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,13 @@
44
#include "dir-iterator.h"
55

66
struct dir_iterator_level {
7-
int initialized;
8-
97
DIR *dir;
108

119
/*
1210
* The length of the directory part of path at this level
1311
* (including a trailing '/'):
1412
*/
1513
size_t prefix_len;
16-
17-
/*
18-
* The last action that has been taken with the current entry
19-
* (needed for directories, which have to be included in the
20-
* iteration and also iterated into):
21-
*/
22-
enum {
23-
DIR_STATE_ITER,
24-
DIR_STATE_RECURSE
25-
} dir_state;
2614
};
2715

2816
/*
@@ -34,9 +22,11 @@ struct dir_iterator_int {
3422
struct dir_iterator base;
3523

3624
/*
37-
* The number of levels currently on the stack. This is always
38-
* at least 1, because when it becomes zero the iteration is
39-
* ended and this struct is freed.
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.
4030
*/
4131
size_t levels_nr;
4232

@@ -50,113 +40,118 @@ struct dir_iterator_int {
5040
struct dir_iterator_level *levels;
5141
};
5242

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+
*/
49+
static 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+
*/
77+
static 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+
*/
95+
static 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+
53117
int dir_iterator_advance(struct dir_iterator *dir_iterator)
54118
{
55119
struct dir_iterator_int *iter =
56120
(struct dir_iterator_int *)dir_iterator;
57121

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. */
58130
while (1) {
131+
struct dirent *de;
59132
struct dir_iterator_level *level =
60133
&iter->levels[iter->levels_nr - 1];
61-
struct dirent *de;
62134

63-
if (!level->initialized) {
64-
/*
65-
* Note: dir_iterator_begin() ensures that
66-
* path is not the empty string.
67-
*/
68-
if (!is_dir_sep(iter->base.path.buf[iter->base.path.len - 1]))
69-
strbuf_addch(&iter->base.path, '/');
70-
level->prefix_len = iter->base.path.len;
71-
72-
level->dir = opendir(iter->base.path.buf);
73-
if (!level->dir && errno != ENOENT) {
74-
warning_errno("error opening directory '%s'",
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'",
75142
iter->base.path.buf);
76-
/* Popping the level is handled below */
77-
}
78-
79-
level->initialized = 1;
80-
} else if (S_ISDIR(iter->base.st.st_mode)) {
81-
if (level->dir_state == DIR_STATE_ITER) {
82-
/*
83-
* The directory was just iterated
84-
* over; now prepare to iterate into
85-
* it.
86-
*/
87-
level->dir_state = DIR_STATE_RECURSE;
88-
ALLOC_GROW(iter->levels, iter->levels_nr + 1,
89-
iter->levels_alloc);
90-
level = &iter->levels[iter->levels_nr++];
91-
level->initialized = 0;
92-
continue;
93-
} else {
94-
/*
95-
* The directory has already been
96-
* iterated over and iterated into;
97-
* we're done with it.
98-
*/
99-
}
143+
else if (pop_level(iter) == 0)
144+
return dir_iterator_abort(dir_iterator);
145+
continue;
100146
}
101147

102-
if (!level->dir) {
103-
/*
104-
* This level is exhausted (or wasn't opened
105-
* successfully); pop up a level.
106-
*/
107-
if (--iter->levels_nr == 0)
108-
return dir_iterator_abort(dir_iterator);
148+
if (is_dot_or_dotdot(de->d_name))
149+
continue;
109150

151+
if (prepare_next_entry_data(iter, de))
110152
continue;
111-
}
112153

113-
/*
114-
* Loop until we find an entry that we can give back
115-
* to the caller:
116-
*/
117-
while (1) {
118-
strbuf_setlen(&iter->base.path, level->prefix_len);
119-
errno = 0;
120-
de = readdir(level->dir);
121-
122-
if (!de) {
123-
/* This level is exhausted; pop up a level. */
124-
if (errno) {
125-
warning_errno("error reading directory '%s'",
126-
iter->base.path.buf);
127-
} else if (closedir(level->dir))
128-
warning_errno("error closing directory '%s'",
129-
iter->base.path.buf);
130-
131-
level->dir = NULL;
132-
if (--iter->levels_nr == 0)
133-
return dir_iterator_abort(dir_iterator);
134-
break;
135-
}
136-
137-
if (is_dot_or_dotdot(de->d_name))
138-
continue;
139-
140-
strbuf_addstr(&iter->base.path, de->d_name);
141-
if (lstat(iter->base.path.buf, &iter->base.st) < 0) {
142-
if (errno != ENOENT)
143-
warning_errno("failed to stat '%s'",
144-
iter->base.path.buf);
145-
continue;
146-
}
147-
148-
/*
149-
* We have to set these each time because
150-
* the path strbuf might have been realloc()ed.
151-
*/
152-
iter->base.relative_path =
153-
iter->base.path.buf + iter->levels[0].prefix_len;
154-
iter->base.basename =
155-
iter->base.path.buf + level->prefix_len;
156-
level->dir_state = DIR_STATE_ITER;
157-
158-
return ITER_OK;
159-
}
154+
return ITER_OK;
160155
}
161156
}
162157

@@ -187,17 +182,32 @@ struct dir_iterator *dir_iterator_begin(const char *path)
187182
{
188183
struct dir_iterator_int *iter = xcalloc(1, sizeof(*iter));
189184
struct dir_iterator *dir_iterator = &iter->base;
190-
191-
if (!path || !*path)
192-
BUG("empty path passed to dir_iterator_begin()");
185+
int saved_errno;
193186

194187
strbuf_init(&iter->base.path, PATH_MAX);
195188
strbuf_addstr(&iter->base.path, path);
196189

197190
ALLOC_GROW(iter->levels, 10, iter->levels_alloc);
191+
iter->levels_nr = 0;
198192

199-
iter->levels_nr = 1;
200-
iter->levels[0].initialized = 0;
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+
}
201206

202207
return dir_iterator;
208+
209+
error_out:
210+
dir_iterator_abort(dir_iterator);
211+
errno = saved_errno;
212+
return NULL;
203213
}

dir-iterator.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88
*
99
* Iterate over a directory tree, recursively, including paths of all
1010
* types and hidden paths. Skip "." and ".." entries and don't follow
11-
* symlinks except for the original path.
11+
* symlinks except for the original path. Note that the original path
12+
* is not included in the iteration.
1213
*
1314
* Every time dir_iterator_advance() is called, update the members of
1415
* the dir_iterator structure to reflect the next path in the
1516
* iteration. The order that paths are iterated over within a
16-
* directory is undefined, but directory paths are always iterated
17-
* over before the subdirectory contents.
17+
* directory is undefined, directory paths are always given before
18+
* their contents.
1819
*
1920
* A typical iteration looks like this:
2021
*
2122
* int ok;
22-
* struct iterator *iter = dir_iterator_begin(path);
23+
* struct dir_iterator *iter = dir_iterator_begin(path);
24+
*
25+
* if (!iter)
26+
* goto error_handler;
2327
*
2428
* while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
2529
* if (want_to_stop_iteration()) {
@@ -59,8 +63,9 @@ struct dir_iterator {
5963
};
6064

6165
/*
62-
* Start a directory iteration over path. Return a dir_iterator that
63-
* holds the internal state of the iteration.
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.
6469
*
6570
* The iteration includes all paths under path, not including path
6671
* itself and not including "." or ".." entries.

refs/files-backend.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,13 +2143,22 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = {
21432143
static struct ref_iterator *reflog_iterator_begin(struct ref_store *ref_store,
21442144
const char *gitdir)
21452145
{
2146-
struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
2147-
struct ref_iterator *ref_iterator = &iter->base;
2146+
struct dir_iterator *diter;
2147+
struct files_reflog_iterator *iter;
2148+
struct ref_iterator *ref_iterator;
21482149
struct strbuf sb = STRBUF_INIT;
21492150

2150-
base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);
21512151
strbuf_addf(&sb, "%s/logs", gitdir);
2152-
iter->dir_iterator = dir_iterator_begin(sb.buf);
2152+
2153+
diter = dir_iterator_begin(sb.buf);
2154+
if(!diter)
2155+
return empty_ref_iterator_begin();
2156+
2157+
iter = xcalloc(1, sizeof(*iter));
2158+
ref_iterator = &iter->base;
2159+
2160+
base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable, 0);
2161+
iter->dir_iterator = diter;
21532162
iter->ref_store = ref_store;
21542163
strbuf_release(&sb);
21552164

t/helper/test-dir-iterator.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ int cmd__dir_iterator(int argc, const char **argv)
1717

1818
diter = dir_iterator_begin(path.buf);
1919

20+
if (!diter) {
21+
printf("dir_iterator_begin failure: %d\n", errno);
22+
exit(EXIT_FAILURE);
23+
}
24+
2025
while (dir_iterator_advance(diter) == ITER_OK) {
2126
if (S_ISDIR(diter->st.st_mode))
2227
printf("[d] ");

t/t0066-dir-iterator.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,17 @@ test_expect_success 'dir-iterator should list files in the correct order' '
5252
test_cmp expected-pre-order-output actual-pre-order-output
5353
'
5454

55+
test_expect_success 'begin should fail upon inexistent paths' '
56+
test_must_fail test-tool dir-iterator ./inexistent-path \
57+
>actual-inexistent-path-output &&
58+
echo "dir_iterator_begin failure: 2" >expected-inexistent-path-output &&
59+
test_cmp expected-inexistent-path-output actual-inexistent-path-output
60+
'
61+
62+
test_expect_success 'begin should fail upon non directory paths' '
63+
test_must_fail test-tool dir-iterator ./dir/b >actual-non-dir-output &&
64+
echo "dir_iterator_begin failure: 20" >expected-non-dir-output &&
65+
test_cmp expected-non-dir-output actual-non-dir-output
66+
'
67+
5568
test_done

0 commit comments

Comments
 (0)