2 * Copyright (c) 2003-2004 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * There is a single list of "tree_entry" items that represent
29 * filesystem objects that require further attention. Non-directories
30 * are not kept in memory: they are pulled from readdir(), returned to
31 * the client, then freed as soon as possible. Any directory entry to
32 * be traversed gets pushed onto the stack.
34 * There is surprisingly little information that needs to be kept for
35 * each item on the stack. Just the name, depth (represented here as the
36 * string length of the parent directory's pathname), and some markers
37 * indicating how to get back to the parent (via chdir("..") for a
38 * regular dir or via fchdir(2) for a symlink).
53 * 3) Arbitrary logical traversals by closing/reopening intermediate fds.
57 struct tree_entry *next;
59 size_t dirname_length;
64 /* Definitions for tree_entry.flags bitmap. */
65 #define isDir 1 /* This entry is a regular directory. */
66 #define isDirLink 2 /* This entry is a symbolic link to a directory. */
67 #define needsTraversal 4 /* This entry hasn't yet been traversed. */
70 * Local data for this package.
73 struct tree_entry *stack;
82 size_t dirname_length;
92 /* Definitions for tree.flags bitmap. */
93 #define needsReturn 8 /* Marks first entry as not having been returned yet. */
94 #define hasStat 16 /* The st entry is set. */
95 #define hasLstat 32 /* The lst entry is set. */
98 #define HAVE_DIRENT_D_NAMLEN 1
99 #ifdef HAVE_DIRENT_D_NAMLEN
100 /* BSD extension; avoids need for a strlen() call. */
101 #define D_NAMELEN(dp) (dp)->d_namlen
103 #define D_NAMELEN(dp) (strlen((dp)->d_name))
108 dumpStack(struct tree *t)
110 struct tree_entry *te;
112 printf("\tbuff: %s\n", t->buff);
113 printf("\tpwd: "); fflush(stdout); system("pwd");
114 printf("\tstack:\n");
115 for (te = t->stack; te != NULL; te = te->next) {
116 printf("\t\tte->name: %s %s\n", te->name, te->flags & needsTraversal ? "" : "*");
122 * Add a directory path to the current stack.
125 tree_add(struct tree *t, const char *path)
127 struct tree_entry *te;
129 te = malloc(sizeof(*te));
130 memset(te, 0, sizeof(*te));
134 te->name = strdup(path);
135 te->flags = needsTraversal;
136 te->dirname_length = t->dirname_length;
140 * Append a name to the current path.
143 tree_append(struct tree *t, const char *name, size_t name_length)
146 t->buff[t->dirname_length] = '\0';
148 /* Resize pathname buffer as needed. */
149 while (name_length + 1 + t->dirname_length >= t->buff_length) {
151 if (t->buff_length < 1024)
152 t->buff_length = 1024;
153 t->buff = realloc(t->buff, t->buff_length);
155 t->basename = t->buff + t->dirname_length;
156 t->path_length = t->dirname_length + name_length;
157 if (t->dirname_length > 0) {
158 *t->basename++ = '/';
161 strcpy(t->basename, name);
165 * Open a directory tree for traversal.
168 tree_open(const char *path)
172 t = malloc(sizeof(*t));
173 memset(t, 0, sizeof(*t));
174 tree_append(t, path, strlen(path));
175 t->initialDirFd = open(".", O_RDONLY);
177 * During most of the traversal, items are set up and then
178 * returned immediately from tree_next(). That doesn't work
179 * for the very first entry, so we set a flag for this special
182 t->flags = needsReturn;
187 * We've finished a directory; ascend back to the parent.
190 tree_ascend(struct tree *t)
192 struct tree_entry *te;
196 if (te->flags & isDirLink) {
206 * Pop the working stack.
209 tree_pop(struct tree *t)
211 struct tree_entry *te;
215 t->dirname_length = te->dirname_length;
221 * Get the next item in the tree traversal.
224 tree_next(struct tree *t)
226 struct dirent *de = NULL;
228 /* Handle the startup case by returning the initial entry. */
229 if (t->flags & needsReturn) {
230 t->flags &= ~needsReturn;
234 while (t->stack != NULL) {
235 /* If there's an open dir, get the next entry from there. */
236 while (t->d != NULL) {
241 } else if (de->d_name[0] == '.'
242 && de->d_name[1] == '\0') {
244 } else if (de->d_name[0] == '.'
245 && de->d_name[1] == '.'
246 && de->d_name[2] == '\0') {
250 * Append the path to the current path
253 tree_append(t, de->d_name, D_NAMELEN(de));
254 t->flags &= ~hasLstat;
255 t->flags &= ~hasStat;
260 /* If the current dir needs to be traversed, set it up. */
261 if (t->stack->flags & needsTraversal) {
262 tree_append(t, t->stack->name, strlen(t->stack->name));
263 t->stack->flags &= ~needsTraversal;
264 /* If it is a link, set up fd for the ascent. */
265 if (t->stack->flags & isDirLink) {
266 t->stack->fd = open(".", O_RDONLY);
268 if (t->openCount > t->maxOpenCount)
269 t->maxOpenCount = t->openCount;
271 if (chdir(t->stack->name) == 0) {
273 t->dirname_length = t->path_length;
280 /* We've done everything necessary for the top stack entry. */
288 * Called by the client to mark the directory just returned from
289 * tree_next() as needing to be visited.
292 tree_descend(struct tree *t)
294 const struct stat *s = tree_current_lstat(t);
296 if (S_ISDIR(s->st_mode)) {
297 tree_add(t, t->basename);
298 t->stack->flags |= isDir;
301 if (S_ISLNK(s->st_mode) && S_ISDIR(tree_current_stat(t)->st_mode)) {
302 tree_add(t, t->basename);
303 t->stack->flags |= isDirLink;
308 * Get the stat() data for the entry just returned from tree_next().
311 tree_current_stat(struct tree *t)
313 if (!(t->flags & hasStat)) {
314 stat(t->basename, &t->st);
321 * Get the lstat() data for the entry just returned from tree_next().
324 tree_current_lstat(struct tree *t)
326 if (!(t->flags & hasLstat)) {
327 lstat(t->basename, &t->lst);
328 t->flags |= hasLstat;
334 * Return the access path for the entry just returned from tree_next().
337 tree_current_access_path(struct tree *t)
339 return (t->basename);
343 * Return the full path for the entry just returned from tree_next().
346 tree_current_path(struct tree *t)
352 * Return the length of the path for the entry just returned from tree_next().
355 tree_current_pathlen(struct tree *t)
357 return (t->path_length);
361 * Return the nesting depth of the entry just returned from tree_next().
364 tree_current_depth(struct tree *t)
370 * Terminate the traversal and release any resources.
373 tree_close(struct tree *t)
375 /* Release anything remaining in the stack. */
376 while (t->stack != NULL)
380 /* chdir() back to where we started. */
381 if (t->initialDirFd >= 0) {
382 fchdir(t->initialDirFd);
383 close(t->initialDirFd);
384 t->initialDirFd = -1;
391 /* Main function for testing. */
394 int main(int argc, char **argv)
396 size_t max_path_len = 0;
401 struct tree *t = tree_open(*argv);
402 while (tree_next(t)) {
403 size_t path_len = tree_current_pathlen(t);
404 int depth = tree_current_depth(t);
405 if (path_len > max_path_len)
406 max_path_len = path_len;
407 if (depth > max_depth)
409 printf("%s\n", tree_current_path(t));
410 if (S_ISDIR(tree_current_lstat(t)->st_mode))
411 tree_descend(t); /* Descend into every dir. */
414 printf("Max path length: %d\n", max_path_len);
415 printf("Max depth: %d\n", max_depth);
416 printf("Final open count: %d\n", t->openCount);
417 printf("Max open count: %d\n", t->maxOpenCount);