Merge branch 'pw/add-p-recount' into maint
[git] / dir-iterator.h
1 #ifndef DIR_ITERATOR_H
2 #define DIR_ITERATOR_H
3
4 #include "strbuf.h"
5
6 /*
7  * Iterate over a directory tree.
8  *
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.
12  *
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.
18  *
19  * A typical iteration looks like this:
20  *
21  *     int ok;
22  *     struct iterator *iter = dir_iterator_begin(path);
23  *
24  *     while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
25  *             if (want_to_stop_iteration()) {
26  *                     ok = dir_iterator_abort(iter);
27  *                     break;
28  *             }
29  *
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  *     }
34  *
35  *     if (ok != ITER_DONE)
36  *             handle_error();
37  *
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.
41  */
42
43 struct dir_iterator {
44         /* The current path: */
45         struct strbuf path;
46
47         /*
48          * The current path relative to the starting path. This part
49          * of the path always uses "/" characters to separate path
50          * components:
51          */
52         const char *relative_path;
53
54         /* The current basename: */
55         const char *basename;
56
57         /* The result of calling lstat() on path: */
58         struct stat st;
59 };
60
61 /*
62  * Start a directory iteration over path. Return a dir_iterator that
63  * holds the internal state of the iteration.
64  *
65  * The iteration includes all paths under path, not including path
66  * itself and not including "." or ".." entries.
67  *
68  * path is the starting directory. An internal copy will be made.
69  */
70 struct dir_iterator *dir_iterator_begin(const char *path);
71
72 /*
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.
79  */
80 int dir_iterator_advance(struct dir_iterator *iterator);
81
82 /*
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.
86  */
87 int dir_iterator_abort(struct dir_iterator *iterator);
88
89 #endif