Commit | Line | Data |
---|---|---|
0fe5043d MH |
1 | #ifndef DIR_ITERATOR_H |
2 | #define DIR_ITERATOR_H | |
3 | ||
4 | /* | |
5 | * Iterate over a directory tree. | |
6 | * | |
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. | |
10 | * | |
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. | |
16 | * | |
17 | * A typical iteration looks like this: | |
18 | * | |
19 | * int ok; | |
20 | * struct iterator *iter = dir_iterator_begin(path); | |
21 | * | |
22 | * while ((ok = dir_iterator_advance(iter)) == ITER_OK) { | |
23 | * if (want_to_stop_iteration()) { | |
24 | * ok = dir_iterator_abort(iter); | |
25 | * break; | |
26 | * } | |
27 | * | |
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 | * } | |
32 | * | |
33 | * if (ok != ITER_DONE) | |
34 | * handle_error(); | |
35 | * | |
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. | |
39 | */ | |
40 | ||
41 | struct dir_iterator { | |
42 | /* The current path: */ | |
43 | struct strbuf path; | |
44 | ||
45 | /* | |
46 | * The current path relative to the starting path. This part | |
47 | * of the path always uses "/" characters to separate path | |
48 | * components: | |
49 | */ | |
50 | const char *relative_path; | |
51 | ||
52 | /* The current basename: */ | |
53 | const char *basename; | |
54 | ||
55 | /* The result of calling lstat() on path: */ | |
56 | struct stat st; | |
57 | }; | |
58 | ||
59 | /* | |
60 | * Start a directory iteration over path. Return a dir_iterator that | |
61 | * holds the internal state of the iteration. | |
62 | * | |
63 | * The iteration includes all paths under path, not including path | |
64 | * itself and not including "." or ".." entries. | |
65 | * | |
66 | * path is the starting directory. An internal copy will be made. | |
67 | */ | |
68 | struct dir_iterator *dir_iterator_begin(const char *path); | |
69 | ||
70 | /* | |
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. | |
77 | */ | |
78 | int dir_iterator_advance(struct dir_iterator *iterator); | |
79 | ||
80 | /* | |
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. | |
84 | */ | |
85 | int dir_iterator_abort(struct dir_iterator *iterator); | |
86 | ||
87 | #endif |