• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 fd = because haven't got node to call dirtree_parentfd() on yet
36     int fd = parent ? parent->dirfd : AT_FDCWD,
37       sym = AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW);
38 
39     // stat dangling symlinks
40     if (fstatat(fd, name, &st, sym)) {
41       // If we got ENOENT without NOFOLLOW, try again with NOFOLLOW.
42       if (errno!=ENOENT || sym || fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW)) {
43         if (flags&DIRTREE_STATLESS) statless++;
44         else goto error;
45       }
46     }
47     if (!statless && S_ISLNK(st.st_mode)) {
48       if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
49       libbuf[linklen++]=0;
50     }
51     len = strlen(name);
52   }
53 
54   // Allocate/populate return structure
55   dt = xmalloc((len = sizeof(struct dirtree)+len+1)+linklen);
56   memset(dt, 0, sizeof(struct dirtree));
57   dt->parent = parent;
58   if (statless) dt->again = DIRTREE_STATLESS;
59   else memcpy(&dt->st, &st, sizeof(struct stat));
60   if (name) strcpy(dt->name, name);
61   else *dt->name = 0, dt->st.st_mode = S_IFDIR;
62   if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
63 
64   return dt;
65 
66 error:
67   if (!(flags&DIRTREE_SHUTUP) && !isdotdot(name)) {
68     char *path = parent ? dirtree_path(parent, 0) : "";
69 
70     perror_msg("%s%s%s", path, parent ? "/" : "", name);
71     if (parent) free(path);
72   }
73   if (parent) parent->symlink = (char *)1;
74   free(dt);
75 
76   return 0;
77 }
78 
79 // Return path to this node.
80 
81 // Initial call can pass in NULL to plen, or point to an int initialized to 0
82 // to return the length of the path, or a value greater than 0 to allocate
83 // extra space if you want to append your own text to the string.
84 
dirtree_path(struct dirtree * node,int * plen)85 char *dirtree_path(struct dirtree *node, int *plen)
86 {
87   struct dirtree *nn;
88   char *path;
89   int ii, ll, len;
90 
91   ll = len = plen ? *plen : 0;
92   if (!node->parent)
93     return strcpy(xmalloc(strlen(node->name)+ll+1), node->name);
94   for (nn = node; nn; nn = nn->parent)
95     if ((ii = strlen(nn->name))) len += ii+1-(nn->name[ii-1]=='/');
96   if (plen) *plen = len;
97   path = xmalloc(len)+len-ll;
98   for (nn = node; nn; nn = nn->parent) if ((len = strlen(nn->name))) {
99     *--path = '/'*(nn != node);
100     if (nn->name[len-1]=='/') len--;
101     memcpy(path -= len, nn->name, len);
102   }
103 
104   return path;
105 }
106 
dirtree_parentfd(struct dirtree * node)107 int dirtree_parentfd(struct dirtree *node)
108 {
109   return node->parent ? node->parent->dirfd : AT_FDCWD;
110 }
111 
112 // Handle callback for a node in the tree. Returns saved node(s) if
113 // callback returns DIRTREE_SAVE, otherwise frees consumed nodes and
114 // returns NULL. If !callback return top node unchanged.
115 // If !new return DIRTREE_ABORTVAL
116 
dirtree_handle_callback(struct dirtree * new,int (* callback)(struct dirtree * node))117 static struct dirtree *dirtree_handle_callback(struct dirtree *new,
118   int (*callback)(struct dirtree *node))
119 {
120   int flags, df = DIRTREE_RECURSE|DIRTREE_COMEAGAIN|DIRTREE_BREADTH,
121       fd = AT_FDCWD;
122 
123   if (!new) return DIRTREE_ABORTVAL;
124   if (!callback) return new;
125   flags = callback(new);
126 
127   if (S_ISDIR(new->st.st_mode) && (flags & df)) {
128     // TODO: check openat returned fd for errors... and do what about it?
129     if (*new->name) fd = openat(dirtree_parentfd(new), new->name, O_CLOEXEC);
130     if (flags&DIRTREE_BREADTH) {
131       new->again |= DIRTREE_BREADTH;
132       if ((DIRTREE_ABORT & dirtree_recurse(new, 0, fd, flags)) ||
133           (DIRTREE_ABORT & (flags = callback(new))))
134         return DIRTREE_ABORTVAL;
135     }
136     flags = dirtree_recurse(new, callback, fd, flags);
137     close(fd);
138   }
139 
140   // Free node that didn't request saving and has no saved children.
141   if (!new->child && !(flags & DIRTREE_SAVE)) {
142     free(new);
143     new = 0;
144   }
145 
146   return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
147 }
148 
149 // Recursively read/process children of directory node, filtering through
150 // callback().
151 
dirtree_recurse(struct dirtree * node,int (* callback)(struct dirtree * node),int dirfd,int flags)152 int dirtree_recurse(struct dirtree *node,
153           int (*callback)(struct dirtree *node), int dirfd, int flags)
154 {
155   struct dirtree *new = 0, *next, **ddt = &(node->child);
156   struct dirent *entry;
157   DIR *dir = 0;
158 
159   // fdopendir() doesn't support AT_FDCWD, closedir() closes fd from opendir()
160   if (AT_FDCWD == (node->dirfd = dirfd)) dir = opendir(".");
161   else if (node->dirfd != -1) dir = fdopendir(xdup(node->dirfd));
162 
163   if (!dir) {
164     if (!(flags & DIRTREE_SHUTUP)) {
165       char *path = dirtree_path(node, 0);
166       perror_msg_raw(path);
167       free(path);
168     }
169     goto done;
170   }
171 
172   // Iterate through stored entries, if any
173   if (callback && *ddt) while (*ddt) {
174     next = (*ddt)->next;
175     if (!(new = dirtree_handle_callback(*ddt, callback))) *ddt = next;
176     else if (new == DIRTREE_ABORTVAL) goto done;
177     else ddt = &new->next;
178 
179   // according to the fddir() man page, the filehandle in the DIR * can still
180   // be externally used by things that don't lseek() it.
181   } else while ((entry = readdir(dir))) {
182     if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
183     if ((flags&DIRTREE_BREADTH) && isdotdot(entry->d_name)) continue;
184     if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
185     if (!new->st.st_blksize && !new->st.st_mode)
186       new->st.st_mode = entry->d_type<<12;
187     new = dirtree_handle_callback(new, callback);
188     if (new == DIRTREE_ABORTVAL) goto done;
189     if (new) {
190       *ddt = new;
191       ddt = &((*ddt)->next);
192       if (flags&DIRTREE_BREADTH) node->extra++;
193     }
194   }
195 
196   if (callback && (flags & DIRTREE_COMEAGAIN)) {
197     node->again |= DIRTREE_COMEAGAIN;
198     flags = callback(node);
199   }
200 
201 done:
202   closedir(dir);
203   node->dirfd = -1;
204 
205   return (new == DIRTREE_ABORTVAL) ? DIRTREE_ABORT : flags;
206 }
207 
208 // Create dirtree from path, using callback to filter nodes. If !callback
209 // return just the top node. Use dirtree_notdotdot callback to allocate a
210 // tree of struct dirtree nodes and return pointer to root node for later
211 // processing.
212 // Returns DIRTREE_ABORTVAL if path didn't exist (use DIRTREE_SHUTUP to handle
213 // error message yourself).
214 
dirtree_flagread(char * path,int flags,int (* callback)(struct dirtree * node))215 struct dirtree *dirtree_flagread(char *path, int flags,
216   int (*callback)(struct dirtree *node))
217 {
218   return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback);
219 }
220 
221 // Common case
dirtree_read(char * path,int (* callback)(struct dirtree * node))222 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
223 {
224   return dirtree_flagread(path, 0, callback);
225 }
226