Sixth batch for 2.20
[git.git] / dir-iterator.h
blob970793d07a1d72761159adca51d16859c7b72a29
1 #ifndef DIR_ITERATOR_H
2 #define DIR_ITERATOR_H
4 #include "strbuf.h"
6 /*
7 * Iterate over a directory tree.
9 * Iterate over a directory tree, recursively, including paths of all
10 * types and hidden paths. Skip "." and ".." entries and don't follow
11 * symlinks except for the original path.
13 * Every time dir_iterator_advance() is called, update the members of
14 * the dir_iterator structure to reflect the next path in the
15 * 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.
19 * A typical iteration looks like this:
21 * int ok;
22 * struct iterator *iter = dir_iterator_begin(path);
24 * while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
25 * if (want_to_stop_iteration()) {
26 * ok = dir_iterator_abort(iter);
27 * break;
28 * }
30 * // Access information about the current path:
31 * if (S_ISDIR(iter->st.st_mode))
32 * printf("%s is a directory\n", iter->relative_path);
33 * }
35 * if (ok != ITER_DONE)
36 * handle_error();
38 * Callers are allowed to modify iter->path while they are working,
39 * but they must restore it to its original contents before calling
40 * dir_iterator_advance() again.
43 struct dir_iterator {
44 /* The current path: */
45 struct strbuf path;
48 * The current path relative to the starting path. This part
49 * of the path always uses "/" characters to separate path
50 * components:
52 const char *relative_path;
54 /* The current basename: */
55 const char *basename;
57 /* The result of calling lstat() on path: */
58 struct stat st;
62 * Start a directory iteration over path. Return a dir_iterator that
63 * holds the internal state of the iteration.
65 * The iteration includes all paths under path, not including path
66 * itself and not including "." or ".." entries.
68 * path is the starting directory. An internal copy will be made.
70 struct dir_iterator *dir_iterator_begin(const char *path);
73 * Advance the iterator to the first or next item and return ITER_OK.
74 * If the iteration is exhausted, free the dir_iterator and any
75 * resources associated with it and return ITER_DONE. On error, free
76 * dir_iterator and associated resources and return ITER_ERROR. It is
77 * a bug to use iterator or call this function again after it has
78 * returned ITER_DONE or ITER_ERROR.
80 int dir_iterator_advance(struct dir_iterator *iterator);
83 * End the iteration before it has been exhausted. Free the
84 * dir_iterator and any associated resources and return ITER_DONE. On
85 * error, free the dir_iterator and return ITER_ERROR.
87 int dir_iterator_abort(struct dir_iterator *iterator);
89 #endif