1 /* dirtree.c - Functions for dealing with directory trees.
3 * Copyright 2007 Rob Landley <rob@landley.net>
8 int isdotdot(char *name)
10 if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 1;
15 // Default callback, filters out "." and ".." except at top level.
17 int dirtree_notdotdot(struct dirtree *catch)
19 // Should we skip "." and ".."?
20 return (!catch->parent||!isdotdot(catch->name))
21 *(DIRTREE_SAVE|DIRTREE_RECURSE);
24 // Create a dirtree node from a path, with stat and symlink info.
25 // (This doesn't open directory filehandles yet so as not to exhaust the
26 // filehandle space on large trees, dirtree_handle_callback() does that.)
28 struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
30 struct dirtree *dt = NULL;
32 int len = 0, linklen = 0;
35 // open code this because haven't got node to call dirtree_parentfd() on yet
36 int fd = parent ? parent->dirfd : AT_FDCWD;
38 if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW)))
40 if (S_ISLNK(st.st_mode)) {
41 if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
46 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
49 memcpy(&(dt->st), &st, sizeof(struct stat));
50 strcpy(dt->name, name);
52 if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
58 if (!(flags&DIRTREE_SHUTUP) && !isdotdot(name)) {
59 char *path = parent ? dirtree_path(parent, 0) : "";
61 perror_msg("%s%s%s", path, parent ? "/" : "", name);
62 if (parent) free(path);
64 if (parent) parent->symlink = (char *)1;
69 // Return path to this node, assembled recursively.
71 // Initial call can pass in NULL to plen, or point to an int initialized to 0
72 // to return the length of the path, or a value greater than 0 to allocate
73 // extra space if you want to append your own text to the string.
75 char *dirtree_path(struct dirtree *node, int *plen)
81 path = xmalloc(*plen);
86 len = (plen ? *plen : 0)+strlen(node->name)+1;
87 path = dirtree_path(node->parent, &len);
88 if (len && path[len-1] != '/') path[len++]='/';
89 len = (stpcpy(path+len, node->name) - path);
90 if (plen) *plen = len;
95 int dirtree_parentfd(struct dirtree *node)
97 return node->parent ? node->parent->dirfd : AT_FDCWD;
100 // Handle callback for a node in the tree. Returns saved node(s) if
101 // callback returns DIRTREE_SAVE, otherwise frees consumed nodes and
102 // returns NULL. If !callback return top node unchanged.
103 // If !new return DIRTREE_ABORTVAL
105 struct dirtree *dirtree_handle_callback(struct dirtree *new,
106 int (*callback)(struct dirtree *node))
110 if (!new) return DIRTREE_ABORTVAL;
111 if (!callback) return new;
112 flags = callback(new);
114 if (S_ISDIR(new->st.st_mode) && (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)))
115 flags = dirtree_recurse(new, callback,
116 openat(dirtree_parentfd(new), new->name, O_CLOEXEC), flags);
118 // If this had children, it was callback's job to free them already.
119 if (!(flags & DIRTREE_SAVE)) {
124 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
127 // Recursively read/process children of directory node, filtering through
128 // callback(). Uses and closes supplied ->dirfd.
130 int dirtree_recurse(struct dirtree *node,
131 int (*callback)(struct dirtree *node), int dirfd, int flags)
133 struct dirtree *new, **ddt = &(node->child);
134 struct dirent *entry;
138 if (node->dirfd == -1 || !(dir = fdopendir(node->dirfd))) {
139 if (!(flags & DIRTREE_SHUTUP)) {
140 char *path = dirtree_path(node, 0);
141 perror_msg("No %s", path);
149 // according to the fddir() man page, the filehandle in the DIR * can still
150 // be externally used by things that don't lseek() it.
152 // The extra parentheses are to shut the stupid compiler up.
153 while ((entry = readdir(dir))) {
154 if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
155 if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
156 new = dirtree_handle_callback(new, callback);
157 if (new == DIRTREE_ABORTVAL) break;
160 ddt = &((*ddt)->next);
164 if (flags & DIRTREE_COMEAGAIN) {
166 flags = callback(node);
169 // This closes filehandle as well, so note it
176 // Create dirtree from path, using callback to filter nodes. If !callback
177 // return just the top node. Use dirtree_notdotdot callback to allocate a
178 // tree of struct dirtree nodes and return pointer to root node for later
180 // Returns DIRTREE_ABORTVAL if path didn't exist (use DIRTREE_SHUTUP to handle
181 // error message yourself).
183 struct dirtree *dirtree_flagread(char *path, int flags,
184 int (*callback)(struct dirtree *node))
186 return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback);
190 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
192 return dirtree_flagread(path, 0, callback);