1 /* vi: set sw=4 ts=4 :*/
2 /* dirtree.c - Functions for dealing with directory trees.
4 * Copyright 2007 Rob Landley <rob@landley.net>
9 // Create a dirtree node from a path, with stat and symlink info.
10 // (This doesn't open directory filehandles yet so as not to exhaust the
11 // filehandle space on large trees. handle_callback() does that instead.)
13 struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow)
15 struct dirtree *dt = NULL;
18 int len = 0, linklen = 0;
21 if (fstatat(dirfd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW))
23 if (S_ISLNK(st.st_mode)) {
24 if (0>(linklen = readlinkat(dirfd, name, buf, 4095))) goto error;
29 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
31 memcpy(&(dt->st), &st, sizeof(struct stat));
32 strcpy(dt->name, name);
35 dt->symlink = memcpy(len+(char *)dt, buf, linklen);
43 perror_msg("%s",name);
48 // Return path to this node, assembled recursively.
50 char *dirtree_path(struct dirtree *node, int *plen)
55 if (!node || !node->name) {
56 path = xmalloc(*plen);
61 len = (plen ? *plen : 0)+strlen(node->name)+1;
62 path = dirtree_path(node->parent, &len);
63 if (len) path[len++]='/';
64 len = (stpcpy(path+len, node->name) - path);
65 if (plen) *plen = len;
70 // Default callback, filters out "." and "..".
72 int dirtree_notdotdot(struct dirtree *catch)
74 // Should we skip "." and ".."?
75 if (catch->name[0]=='.' && (!catch->name[1] ||
76 (catch->name[1]=='.' && !catch->name[2])))
79 return DIRTREE_SAVE|DIRTREE_RECURSE;
82 // get open filehandle for node in extra, giving caller the option of
83 // using DIRTREE_COMEAGAIN or not.
84 int dirtree_opennode(struct dirtree *try)
86 if (!dirtree_notdotdot(try)) return 0;
87 if (S_ISDIR(try->st.st_mode)) {
89 try->extra = xdup(try->data);
90 return DIRTREE_COMEAGAIN;
92 } else try->extra = openat(try->parent ? try->parent->data : AT_FDCWD,
95 return DIRTREE_SAVE|DIRTREE_RECURSE;
98 // Handle callback for a node in the tree. Returns saved node(s) or NULL.
100 // By default, allocates a tree of struct dirtree, not following symlinks
101 // If callback==NULL, or callback always returns 0, allocate tree of struct
102 // dirtree and return root of tree. Otherwise call callback(node) on each
103 // hit, free structures after use, and return NULL.
106 struct dirtree *handle_callback(struct dirtree *new,
107 int (*callback)(struct dirtree *node))
109 int flags, dir = S_ISDIR(new->st.st_mode);
111 if (!callback) callback = dirtree_notdotdot;
113 // Directory always has filehandle for examining contents. Whether or
114 // not we'll recurse into it gets decided later.
117 new->data = openat(new->parent ? new->parent->data : AT_FDCWD,
120 flags = callback(new);
123 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
124 dirtree_recurse(new, callback, flags & DIRTREE_SYMFOLLOW);
125 if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
126 } else close(new->data);
129 // If this had children, it was callback's job to free them already.
130 if (!(flags & DIRTREE_SAVE)) {
135 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
138 // Recursively read/process children of directory node (with dirfd in data),
139 // filtering through callback().
141 void dirtree_recurse(struct dirtree *node,
142 int (*callback)(struct dirtree *node), int symfollow)
144 struct dirtree *new, **ddt = &(node->child);
145 struct dirent *entry;
148 if (!(dir = fdopendir(node->data))) {
149 char *path = dirtree_path(node, 0);
150 perror_msg("No %s", path);
157 // according to the fddir() man page, the filehandle in the DIR * can still
158 // be externally used by things that don't lseek() it.
160 // The extra parentheses are to shut the stupid compiler up.
161 while ((entry = readdir(dir))) {
162 if (!(new = dirtree_add_node(node->data, entry->d_name, symfollow)))
165 new = handle_callback(new, callback);
166 if (new == DIRTREE_ABORTVAL) break;
169 ddt = &((*ddt)->next);
173 // 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.
182 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
184 struct dirtree *root = dirtree_add_node(AT_FDCWD, path, 0);
186 return root ? handle_callback(root, callback) : DIRTREE_ABORTVAL;