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);
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->dirfd : 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);
51 if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
57 if (!(flags&DIRTREE_SHUTUP) && notdotdot(name)) {
58 char *path = parent ? dirtree_path(parent, 0) : "";
60 perror_msg("%s%s%s", path, parent ? "/" : "", name);
61 if (parent) free(path);
63 if (parent) parent->symlink = (char *)1;
68 // Return path to this node, assembled recursively.
70 // Initial call can pass in NULL to plen, or point to an int initialized to 0
71 // to return the length of the path, or a value greater than 0 to allocate
72 // extra space if you want to append your own text to the string.
74 char *dirtree_path(struct dirtree *node, int *plen)
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->dirfd : AT_FDCWD;
99 // Handle callback for a node in the tree. Returns saved node(s) if
100 // callback returns DIRTREE_SAVE, otherwise frees consumed nodes and
101 // returns NULL. If !callback return top node unchanged.
102 // If !new return DIRTREE_ABORTVAL
104 struct dirtree *dirtree_handle_callback(struct dirtree *new,
105 int (*callback)(struct dirtree *node))
109 if (!new) return DIRTREE_ABORTVAL;
110 if (!callback) return new;
111 flags = callback(new);
113 if (S_ISDIR(new->st.st_mode) && (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)))
114 flags = dirtree_recurse(new, callback,
115 openat(dirtree_parentfd(new), new->name, O_CLOEXEC), flags);
117 // If this had children, it was callback's job to free them already.
118 if (!(flags & DIRTREE_SAVE)) {
123 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
126 // Recursively read/process children of directory node, filtering through
127 // callback(). Uses and closes supplied ->dirfd.
129 int dirtree_recurse(struct dirtree *node,
130 int (*callback)(struct dirtree *node), int dirfd, int flags)
132 struct dirtree *new, **ddt = &(node->child);
133 struct dirent *entry;
137 if (node->dirfd == -1 || !(dir = fdopendir(node->dirfd))) {
138 if (!(flags & DIRTREE_SHUTUP)) {
139 char *path = dirtree_path(node, 0);
140 perror_msg("No %s", path);
148 // according to the fddir() man page, the filehandle in the DIR * can still
149 // be externally used by things that don't lseek() it.
151 // The extra parentheses are to shut the stupid compiler up.
152 while ((entry = readdir(dir))) {
153 if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
154 new = dirtree_handle_callback(new, callback);
155 if (new == DIRTREE_ABORTVAL) break;
158 ddt = &((*ddt)->next);
162 if (flags & DIRTREE_COMEAGAIN) {
164 flags = callback(node);
167 // This closes filehandle as well, so note it
174 // Create dirtree from path, using callback to filter nodes. If !callback
175 // return just the top node. Use dirtree_notdotdot callback to allocate a
176 // tree of struct dirtree nodes and return pointer to root node for later
178 // Returns DIRTREE_ABORTVAL if path didn't exist (use DIRTREE_SHUTUP to handle
179 // error message yourself).
181 struct dirtree *dirtree_flagread(char *path, int flags,
182 int (*callback)(struct dirtree *node))
184 return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback);
188 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
190 return dirtree_flagread(path, 0, callback);