1 /* dirtree.c - Functions for dealing with directory trees.
2 *
3 * Copyright 2007 Rob Landley <rob@landley.net>
4 */
5
6 #include "toys.h"
7
isdotdot(char * name)8 int isdotdot(char *name)
9 {
10 if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 1;
11
12 return 0;
13 }
14
15 // Default callback, filters out "." and ".." except at top level.
16
dirtree_notdotdot(struct dirtree * catch)17 int dirtree_notdotdot(struct dirtree *catch)
18 {
19 // Should we skip "." and ".."?
20 return (!catch->parent||!isdotdot(catch->name))
21 *(DIRTREE_SAVE|DIRTREE_RECURSE);
22 }
23
24 // Create a dirtree node from a path, with stat and symlink info.
25 // (This doesn't open directory filehandles yet so as not to exhaust the
26 // filehandle space on large trees, dirtree_handle_callback() does that.)
27
dirtree_add_node(struct dirtree * parent,char * name,int flags)28 struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
29 {
30 struct dirtree *dt = 0;
31 struct stat st;
32 int len = 0, linklen = 0, statless = 0;
33
34 if (name) {
35 // open code this because haven't got node to call dirtree_parentfd() on yet
36 int fd = parent ? parent->dirfd : AT_FDCWD;
37
38 if (fstatat(fd, name, &st,AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW))) {
39 if (flags&DIRTREE_STATLESS) statless++;
40 else goto error;
41 }
42 if (S_ISLNK(st.st_mode)) {
43 if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
44 libbuf[linklen++]=0;
45 }
46 len = strlen(name);
47 }
48
49 // Allocate/populate return structure
50 dt = xmalloc((len = sizeof(struct dirtree)+len+1)+linklen);
51 memset(dt, 0, statless ? offsetof(struct dirtree, again)
52 : offsetof(struct dirtree, st));
53 dt->parent = parent;
54 dt->again = statless ? 2 : 0;
55 if (!statless) memcpy(&dt->st, &st, sizeof(struct stat));
56 strcpy(dt->name, name ? name : "");
57 if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
58
59 return dt;
60
61 error:
62 if (!(flags&DIRTREE_SHUTUP) && !isdotdot(name)) {
63 char *path = parent ? dirtree_path(parent, 0) : "";
64
65 perror_msg("%s%s%s", path, parent ? "/" : "", name);
66 if (parent) free(path);
67 }
68 if (parent) parent->symlink = (char *)1;
69 free(dt);
70 return 0;
71 }
72
73 // Return path to this node, assembled recursively.
74
75 // Initial call can pass in NULL to plen, or point to an int initialized to 0
76 // to return the length of the path, or a value greater than 0 to allocate
77 // extra space if you want to append your own text to the string.
78
dirtree_path(struct dirtree * node,int * plen)79 char *dirtree_path(struct dirtree *node, int *plen)
80 {
81 char *path;
82 int len;
83
84 if (!node) {
85 path = xmalloc(*plen);
86 *plen = 0;
87 return path;
88 }
89
90 len = (plen ? *plen : 0)+strlen(node->name)+1;
91 path = dirtree_path(node->parent, &len);
92 if (len && path[len-1] != '/') path[len++]='/';
93 len = stpcpy(path+len, node->name) - path;
94 if (plen) *plen = len;
95
96 return path;
97 }
98
dirtree_parentfd(struct dirtree * node)99 int dirtree_parentfd(struct dirtree *node)
100 {
101 return node->parent ? node->parent->dirfd : AT_FDCWD;
102 }
103
104 // Handle callback for a node in the tree. Returns saved node(s) if
105 // callback returns DIRTREE_SAVE, otherwise frees consumed nodes and
106 // returns NULL. If !callback return top node unchanged.
107 // If !new return DIRTREE_ABORTVAL
108
dirtree_handle_callback(struct dirtree * new,int (* callback)(struct dirtree * node))109 struct dirtree *dirtree_handle_callback(struct dirtree *new,
110 int (*callback)(struct dirtree *node))
111 {
112 int flags;
113
114 if (!new) return DIRTREE_ABORTVAL;
115 if (!callback) return new;
116 flags = callback(new);
117
118 if (S_ISDIR(new->st.st_mode) && (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)))
119 flags = dirtree_recurse(new, callback,
120 openat(dirtree_parentfd(new), new->name, O_CLOEXEC), flags);
121
122 // If this had children, it was callback's job to free them already.
123 if (!(flags & DIRTREE_SAVE)) {
124 free(new);
125 new = 0;
126 }
127
128 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
129 }
130
131 // Recursively read/process children of directory node, filtering through
132 // callback(). Uses and closes supplied ->dirfd.
133
dirtree_recurse(struct dirtree * node,int (* callback)(struct dirtree * node),int dirfd,int flags)134 int dirtree_recurse(struct dirtree *node,
135 int (*callback)(struct dirtree *node), int dirfd, int flags)
136 {
137 struct dirtree *new, **ddt = &(node->child);
138 struct dirent *entry;
139 DIR *dir;
140
141 node->dirfd = dirfd;
142 if (node->dirfd == -1 || !(dir = fdopendir(node->dirfd))) {
143 if (!(flags & DIRTREE_SHUTUP)) {
144 char *path = dirtree_path(node, 0);
145 perror_msg_raw(path);
146 free(path);
147 }
148 close(node->dirfd);
149
150 return flags;
151 }
152
153 // according to the fddir() man page, the filehandle in the DIR * can still
154 // be externally used by things that don't lseek() it.
155
156 // The extra parentheses are to shut the stupid compiler up.
157 while ((entry = readdir(dir))) {
158 if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
159 if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
160 if (!new->st.st_blksize && !new->st.st_mode)
161 new->st.st_mode = entry->d_type<<12;
162 new = dirtree_handle_callback(new, callback);
163 if (new == DIRTREE_ABORTVAL) break;
164 if (new) {
165 *ddt = new;
166 ddt = &((*ddt)->next);
167 }
168 }
169
170 if (flags & DIRTREE_COMEAGAIN) {
171 node->again |= 1;
172 flags = callback(node);
173 }
174
175 // This closes filehandle as well, so note it
176 closedir(dir);
177 node->dirfd = -1;
178
179 return flags;
180 }
181
182 // Create dirtree from path, using callback to filter nodes. If !callback
183 // return just the top node. Use dirtree_notdotdot callback to allocate a
184 // tree of struct dirtree nodes and return pointer to root node for later
185 // processing.
186 // Returns DIRTREE_ABORTVAL if path didn't exist (use DIRTREE_SHUTUP to handle
187 // error message yourself).
188
dirtree_flagread(char * path,int flags,int (* callback)(struct dirtree * node))189 struct dirtree *dirtree_flagread(char *path, int flags,
190 int (*callback)(struct dirtree *node))
191 {
192 return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback);
193 }
194
195 // Common case
dirtree_read(char * path,int (* callback)(struct dirtree * node))196 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
197 {
198 return dirtree_flagread(path, 0, callback);
199 }
200