1 /* dirtree.c - Functions for dealing with directory trees.
3 * Copyright 2007 Rob Landley <rob@landley.net>
8 static int notdotdot(char *name)
10 if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 0;
15 // Default callback, filters out "." and "..".
17 int dirtree_notdotdot(struct dirtree *catch)
19 // Should we skip "." and ".."?
20 return notdotdot(catch->name) ? DIRTREE_SAVE|DIRTREE_RECURSE : 0;
23 // Create a dirtree node from a path, with stat and symlink info.
24 // (This doesn't open directory filehandles yet so as not to exhaust the
25 // filehandle space on large trees, dirtree_handle_callback() does that.)
27 struct dirtree *dirtree_add_node(struct dirtree *parent, char *name,
30 struct dirtree *dt = NULL;
33 int len = 0, linklen = 0;
36 // open code this because haven't got node to call dirtree_parentfd() on yet
37 int fd = parent ? parent->data : AT_FDCWD;
39 if (fstatat(fd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW)) goto error;
40 if (S_ISLNK(st.st_mode)) {
41 if (0>(linklen = readlinkat(fd, name, buf, 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);
53 dt->symlink = memcpy(len+(char *)dt, buf, linklen);
61 if (notdotdot(name)) {
62 char *path = parent ? dirtree_path(parent, 0) : "";
64 perror_msg("%s%s%s", path, parent ? "/" : "", name);
65 if (parent) free(path);
67 if (parent) parent->symlink = (char *)1;
72 // Return path to this node, assembled recursively.
74 char *dirtree_path(struct dirtree *node, int *plen)
79 if (!node || !node->name) {
80 path = xmalloc(*plen);
85 len = (plen ? *plen : 0)+strlen(node->name)+1;
86 path = dirtree_path(node->parent, &len);
87 if (len && path[len-1] != '/') path[len++]='/';
88 len = (stpcpy(path+len, node->name) - path);
89 if (plen) *plen = len;
94 int dirtree_parentfd(struct dirtree *node)
96 return node->parent ? node->parent->data : AT_FDCWD;
99 // Handle callback for a node in the tree. Returns saved node(s) or NULL.
101 // By default, allocates a tree of struct dirtree, not following symlinks
102 // If callback==NULL, or callback always returns 0, allocate tree of struct
103 // dirtree and return root of tree. Otherwise call callback(node) on each
104 // hit, free structures after use, and return NULL.
107 struct dirtree *dirtree_handle_callback(struct dirtree *new,
108 int (*callback)(struct dirtree *node))
110 int flags, dir = S_ISDIR(new->st.st_mode);
112 if (!callback) callback = dirtree_notdotdot;
114 flags = callback(new);
117 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
118 new->data = openat(dirtree_parentfd(new), new->name, O_CLOEXEC);
119 flags = dirtree_recurse(new, callback, flags);
123 // If this had children, it was callback's job to free them already.
124 if (!(flags & DIRTREE_SAVE)) {
129 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
132 // Recursively read/process children of directory node (with dirfd in data),
133 // filtering through callback().
135 int dirtree_recurse(struct dirtree *node,
136 int (*callback)(struct dirtree *node), int flags)
138 struct dirtree *new, **ddt = &(node->child);
139 struct dirent *entry;
142 if (node->data == -1 || !(dir = fdopendir(node->data))) {
143 char *path = dirtree_path(node, 0);
144 perror_msg("No %s", path);
151 // according to the fddir() man page, the filehandle in the DIR * can still
152 // be externally used by things that don't lseek() it.
154 // The extra parentheses are to shut the stupid compiler up.
155 while ((entry = readdir(dir))) {
156 if (!(new = dirtree_add_node(node, entry->d_name, flags&DIRTREE_SYMFOLLOW)))
158 new = dirtree_handle_callback(new, callback);
159 if (new == DIRTREE_ABORTVAL) break;
162 ddt = &((*ddt)->next);
166 if (flags & DIRTREE_COMEAGAIN) {
168 flags = callback(node);
171 // This closes filehandle as well, so note it
178 // Create dirtree from path, using callback to filter nodes.
179 // If callback == NULL allocate a tree of struct dirtree nodes and return
180 // pointer to root node.
181 // symfollow is just for the top of tree, callback return code controls children
183 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
185 struct dirtree *root = dirtree_add_node(0, path, 0);
187 return root ? dirtree_handle_callback(root, callback) : DIRTREE_ABORTVAL;