git-p4: auto-size the block
[git.git] / dir-iterator.h
blob27739e6c29560492254c7fb7e22170cd1ff327ba
1 #ifndef DIR_ITERATOR_H
2 #define DIR_ITERATOR_H
4 /*
5 * Iterate over a directory tree.
7 * Iterate over a directory tree, recursively, including paths of all
8 * types and hidden paths. Skip "." and ".." entries and don't follow
9 * symlinks except for the original path.
11 * Every time dir_iterator_advance() is called, update the members of
12 * the dir_iterator structure to reflect the next path in the
13 * iteration. The order that paths are iterated over within a
14 * directory is undefined, but directory paths are always iterated
15 * over before the subdirectory contents.
17 * A typical iteration looks like this:
19 * int ok;
20 * struct iterator *iter = dir_iterator_begin(path);
22 * while ((ok = dir_iterator_advance(iter)) == ITER_OK) {
23 * if (want_to_stop_iteration()) {
24 * ok = dir_iterator_abort(iter);
25 * break;
26 * }
28 * // Access information about the current path:
29 * if (S_ISDIR(iter->st.st_mode))
30 * printf("%s is a directory\n", iter->relative_path);
31 * }
33 * if (ok != ITER_DONE)
34 * handle_error();
36 * Callers are allowed to modify iter->path while they are working,
37 * but they must restore it to its original contents before calling
38 * dir_iterator_advance() again.
41 struct dir_iterator {
42 /* The current path: */
43 struct strbuf path;
46 * The current path relative to the starting path. This part
47 * of the path always uses "/" characters to separate path
48 * components:
50 const char *relative_path;
52 /* The current basename: */
53 const char *basename;
55 /* The result of calling lstat() on path: */
56 struct stat st;
60 * Start a directory iteration over path. Return a dir_iterator that
61 * holds the internal state of the iteration.
63 * The iteration includes all paths under path, not including path
64 * itself and not including "." or ".." entries.
66 * path is the starting directory. An internal copy will be made.
68 struct dir_iterator *dir_iterator_begin(const char *path);
71 * Advance the iterator to the first or next item and return ITER_OK.
72 * If the iteration is exhausted, free the dir_iterator and any
73 * resources associated with it and return ITER_DONE. On error, free
74 * dir_iterator and associated resources and return ITER_ERROR. It is
75 * a bug to use iterator or call this function again after it has
76 * returned ITER_DONE or ITER_ERROR.
78 int dir_iterator_advance(struct dir_iterator *iterator);
81 * End the iteration before it has been exhausted. Free the
82 * dir_iterator and any associated resources and return ITER_DONE. On
83 * error, free the dir_iterator and return ITER_ERROR.
85 int dir_iterator_abort(struct dir_iterator *iterator);
87 #endif