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 = 0;
31 struct stat st;
32 int len = 0, linklen = 0, statless = 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 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
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
61error:
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
74
75
76
77
78
79char *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
99int dirtree_parentfd(struct dirtree *node)
100{
101 return node->parent ? node->parent->dirfd : AT_FDCWD;
102}
103
104
105
106
107
108
109struct 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
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
132
133
134int 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
154
155
156
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
176 closedir(dir);
177 node->dirfd = -1;
178
179 return flags;
180}
181
182
183
184
185
186
187
188
189struct 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
196struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
197{
198 return dirtree_flagread(path, 0, callback);
199}
200