1
2
3
4
5
6#include "toys.h"
7
8int isdotdot(char *name)
9{
10 if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 1;
11
12 return 0;
13}
14
15
16
17int dirtree_notdotdot(struct dirtree *catch)
18{
19
20 return (!catch->parent||!isdotdot(catch->name))
21 *(DIRTREE_SAVE|DIRTREE_RECURSE);
22}
23
24
25
26
27
28struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
29{
30 struct dirtree *dt = NULL;
31 struct stat st;
32 int len = 0, linklen = 0;
33
34 if (name) {
35
36 int fd = parent ? parent->dirfd : AT_FDCWD;
37
38 if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW)))
39 goto error;
40 if (S_ISLNK(st.st_mode)) {
41 if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
42 libbuf[linklen++]=0;
43 }
44 len = strlen(name);
45 }
46 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
47 dt->parent = parent;
48 if (name) {
49 memcpy(&(dt->st), &st, sizeof(struct stat));
50 strcpy(dt->name, name);
51
52 if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
53 }
54
55 return dt;
56
57error:
58 if (!(flags&DIRTREE_SHUTUP) && !isdotdot(name)) {
59 char *path = parent ? dirtree_path(parent, 0) : "";
60
61 perror_msg("%s%s%s", path, parent ? "/" : "", name);
62 if (parent) free(path);
63 }
64 if (parent) parent->symlink = (char *)1;
65 free(dt);
66 return 0;
67}
68
69
70
71
72
73
74
75char *dirtree_path(struct dirtree *node, int *plen)
76{
77 char *path;
78 int len;
79
80 if (!node) {
81 path = xmalloc(*plen);
82 *plen = 0;
83 return path;
84 }
85
86 len = (plen ? *plen : 0)+strlen(node->name)+1;
87 path = dirtree_path(node->parent, &len);
88 if (len && path[len-1] != '/') path[len++]='/';
89 len = (stpcpy(path+len, node->name) - path);
90 if (plen) *plen = len;
91
92 return path;
93}
94
95int dirtree_parentfd(struct dirtree *node)
96{
97 return node->parent ? node->parent->dirfd : AT_FDCWD;
98}
99
100
101
102
103
104
105struct dirtree *dirtree_handle_callback(struct dirtree *new,
106 int (*callback)(struct dirtree *node))
107{
108 int flags;
109
110 if (!new) return DIRTREE_ABORTVAL;
111 if (!callback) return new;
112 flags = callback(new);
113
114 if (S_ISDIR(new->st.st_mode) && (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)))
115 flags = dirtree_recurse(new, callback,
116 openat(dirtree_parentfd(new), new->name, O_CLOEXEC), flags);
117
118
119 if (!(flags & DIRTREE_SAVE)) {
120 free(new);
121 new = NULL;
122 }
123
124 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
125}
126
127
128
129
130int dirtree_recurse(struct dirtree *node,
131 int (*callback)(struct dirtree *node), int dirfd, int flags)
132{
133 struct dirtree *new, **ddt = &(node->child);
134 struct dirent *entry;
135 DIR *dir;
136
137 node->dirfd = dirfd;
138 if (node->dirfd == -1 || !(dir = fdopendir(node->dirfd))) {
139 if (!(flags & DIRTREE_SHUTUP)) {
140 char *path = dirtree_path(node, 0);
141 perror_msg_raw(path);
142 free(path);
143 }
144 close(node->dirfd);
145
146 return flags;
147 }
148
149
150
151
152
153 while ((entry = readdir(dir))) {
154 if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
155 if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
156 new = dirtree_handle_callback(new, callback);
157 if (new == DIRTREE_ABORTVAL) break;
158 if (new) {
159 *ddt = new;
160 ddt = &((*ddt)->next);
161 }
162 }
163
164 if (flags & DIRTREE_COMEAGAIN) {
165 node->again++;
166 flags = callback(node);
167 }
168
169
170 closedir(dir);
171 node->dirfd = -1;
172
173 return flags;
174}
175
176
177
178
179
180
181
182
183struct dirtree *dirtree_flagread(char *path, int flags,
184 int (*callback)(struct dirtree *node))
185{
186 return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback);
187}
188
189
190struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
191{
192 return dirtree_flagread(path, 0, callback);
193}
194