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)
15 struct dirtree *dt = NULL;
18 int len = 0, linklen = 0;
21 if (fstatat(dirfd, name, &st, AT_SYMLINK_NOFOLLOW)) goto error;
22 if (S_ISLNK(st.st_mode)) {
23 if (0>(linklen = readlinkat(dirfd, name, buf, 4095))) goto error;
28 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
30 memcpy(&(dt->st), &st, sizeof(struct stat));
31 strcpy(dt->name, name);
34 dt->symlink = memcpy(len+(char *)dt, buf, linklen);
42 perror_msg("%s",name);
47 // Return path to this node, assembled recursively.
49 char *dirtree_path(struct dirtree *node, int *plen)
54 if (!node || !node->name) {
55 path = xmalloc(*plen);
60 len = (plen ? *plen : 0)+strlen(node->name)+1;
61 path = dirtree_path(node->parent, &len);
62 if (len) path[len++]='/';
63 len = (stpcpy(path+len, node->name) - path);
64 if (plen) *plen = len;
69 // Default callback, filters out "." and "..".
71 int dirtree_notdotdot(struct dirtree *catch)
73 // Should we skip "." and ".."?
74 if (catch->name[0]=='.' && (!catch->name[1] ||
75 (catch->name[1]=='.' && !catch->name[2])))
78 return DIRTREE_SAVE|DIRTREE_RECURSE;
81 // Handle callback for a node in the tree. Returns saved node(s) or NULL.
83 // By default, allocates a tree of struct dirtree, not following symlinks
84 // If callback==NULL, or callback always returns 0, allocate tree of struct
85 // dirtree and return root of tree. Otherwise call callback(node) on each hit, free
86 // structures after use, and return NULL.
89 struct dirtree *handle_callback(struct dirtree *new,
90 int (*callback)(struct dirtree *node))
94 if (!callback) callback = dirtree_notdotdot;
96 flags = callback(new);
97 if (S_ISDIR(new->st.st_mode)) {
98 if (flags & DIRTREE_RECURSE) {
99 new->data = openat (new->parent ? new->parent->data : AT_FDCWD,
101 dirtree_recurse(new, callback);
104 if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
106 // If this had children, it was callback's job to free them already.
107 if (!(flags & DIRTREE_SAVE)) {
112 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
115 // Recursively read/process children of directory node (with dirfd in data),
116 // filtering through callback().
118 void dirtree_recurse(struct dirtree *node,
119 int (*callback)(struct dirtree *node))
121 struct dirtree *new, **ddt = &(node->child);
122 struct dirent *entry;
125 if (!(dir = fdopendir(node->data))) {
126 char *path = dirtree_path(node, 0);
127 perror_msg("No %s", path);
134 // according to the fddir() man page, the filehandle in the DIR * can still
135 // be externally used by things that don't lseek() it.
137 // The extra parentheses are to shut the stupid compiler up.
138 while ((entry = readdir(dir))) {
139 if (!(new = dirtree_add_node(node->data, entry->d_name))) continue;
141 new = handle_callback(new, callback);
142 if (new == DIRTREE_ABORTVAL) break;
145 ddt = &((*ddt)->next);
152 // Create dirtree from path, using callback to filter nodes.
153 // If callback == NULL allocate a tree of struct dirtree nodes and return
154 // pointer to root node.
156 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
158 struct dirtree *root = dirtree_add_node(AT_FDCWD, path);
160 return handle_callback(root, callback);