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, int flags)
29 struct dirtree *dt = NULL;
31 int len = 0, linklen = 0;
34 // open code this because haven't got node to call dirtree_parentfd() on yet
35 int fd = parent ? parent->data : AT_FDCWD;
37 if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW)))
39 if (S_ISLNK(st.st_mode)) {
40 if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
45 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
48 memcpy(&(dt->st), &st, sizeof(struct stat));
49 strcpy(dt->name, name);
52 dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
60 if (!(flags&DIRTREE_SHUTUP) && notdotdot(name)) {
61 char *path = parent ? dirtree_path(parent, 0) : "";
63 perror_msg("%s%s%s", path, parent ? "/" : "", name);
64 if (parent) free(path);
66 if (parent) parent->symlink = (char *)1;
71 // Return path to this node, assembled recursively.
73 // Initial call can pass in NULL to plen, or point to an int initialized to 0
74 // to return the length of the path, or a value greater than 0 to allocate
75 // extra space if you want to append your own text to the string.
77 char *dirtree_path(struct dirtree *node, int *plen)
83 path = xmalloc(*plen);
88 len = (plen ? *plen : 0)+strlen(node->name)+1;
89 path = dirtree_path(node->parent, &len);
90 if (len && path[len-1] != '/') path[len++]='/';
91 len = (stpcpy(path+len, node->name) - path);
92 if (plen) *plen = len;
97 int dirtree_parentfd(struct dirtree *node)
99 return node->parent ? node->parent->data : AT_FDCWD;
102 // Handle callback for a node in the tree. Returns saved node(s) or NULL.
104 // By default, allocates a tree of struct dirtree, not following symlinks
105 // If callback==NULL, or callback always returns 0, allocate tree of struct
106 // dirtree and return root of tree. Otherwise call callback(node) on each
107 // hit, free structures after use, and return NULL.
110 struct dirtree *dirtree_handle_callback(struct dirtree *new,
111 int (*callback)(struct dirtree *node))
116 if (!callback) callback = dirtree_notdotdot;
117 flags = callback(new);
119 if (S_ISDIR(new->st.st_mode)) {
120 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
121 new->data = openat(dirtree_parentfd(new), new->name, O_CLOEXEC);
122 flags = dirtree_recurse(new, callback, flags);
126 // If this had children, it was callback's job to free them already.
127 if (!(flags & DIRTREE_SAVE)) {
132 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
135 // Recursively read/process children of directory node (with dirfd in data),
136 // filtering through callback().
138 int dirtree_recurse(struct dirtree *node,
139 int (*callback)(struct dirtree *node), int flags)
141 struct dirtree *new, **ddt = &(node->child);
142 struct dirent *entry;
145 if (node->data == -1 || !(dir = fdopendir(node->data))) {
146 if (!(flags & DIRTREE_SHUTUP)) {
147 char *path = dirtree_path(node, 0);
148 perror_msg("No %s", path);
156 // according to the fddir() man page, the filehandle in the DIR * can still
157 // be externally used by things that don't lseek() it.
159 // The extra parentheses are to shut the stupid compiler up.
160 while ((entry = readdir(dir))) {
161 if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
162 new = dirtree_handle_callback(new, callback);
163 if (new == DIRTREE_ABORTVAL) break;
166 ddt = &((*ddt)->next);
170 if (flags & DIRTREE_COMEAGAIN) {
172 flags = callback(node);
175 // This closes filehandle as well, so note it
182 // Create dirtree root
183 struct dirtree *dirtree_start(char *name, int symfollow)
185 return dirtree_add_node(0, name, DIRTREE_SYMFOLLOW*!!symfollow);
188 // Create dirtree from path, using callback to filter nodes.
189 // If callback == NULL allocate a tree of struct dirtree nodes and return
190 // pointer to root node.
192 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
194 struct dirtree *root = dirtree_start(path, 0);
196 return root ? dirtree_handle_callback(root, callback) : DIRTREE_ABORTVAL;