• 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, statless ? offsetof(struct dirtree, again)
57     : offsetof(struct dirtree, st));
58   dt->parent = parent;
59   dt->again = statless ? 2 : 0;
60   if (!statless) memcpy(&dt->st, &st, sizeof(struct stat));
61   strcpy(dt->name, name ? name : "");
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   return 0;
76 }
77 
78 // Return path to this node, assembled recursively.
79 
80 // Initial call can pass in NULL to plen, or point to an int initialized to 0
81 // to return the length of the path, or a value greater than 0 to allocate
82 // extra space if you want to append your own text to the string.
83 
dirtree_path(struct dirtree * node,int * plen)84 char *dirtree_path(struct dirtree *node, int *plen)
85 {
86   char *path;
87   int len;
88 
89   if (!node) {
90     path = xmalloc(*plen);
91     *plen = 0;
92     return path;
93   }
94 
95   len = (plen ? *plen : 0)+strlen(node->name)+1;
96   path = dirtree_path(node->parent, &len);
97   if (len && path[len-1] != '/') path[len++]='/';
98   len = stpcpy(path+len, node->name) - path;
99   if (plen) *plen = len;
100 
101   return path;
102 }
103 
dirtree_parentfd(struct dirtree * node)104 int dirtree_parentfd(struct dirtree *node)
105 {
106   return node->parent ? node->parent->dirfd : AT_FDCWD;
107 }
108 
109 // Handle callback for a node in the tree. Returns saved node(s) if
110 // callback returns DIRTREE_SAVE, otherwise frees consumed nodes and
111 // returns NULL. If !callback return top node unchanged.
112 // If !new return DIRTREE_ABORTVAL
113 
dirtree_handle_callback(struct dirtree * new,int (* callback)(struct dirtree * node))114 struct dirtree *dirtree_handle_callback(struct dirtree *new,
115           int (*callback)(struct dirtree *node))
116 {
117   int flags;
118 
119   if (!new) return DIRTREE_ABORTVAL;
120   if (!callback) return new;
121   flags = callback(new);
122 
123   if (S_ISDIR(new->st.st_mode) && (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)))
124     flags = dirtree_recurse(new, callback,
125       openat(dirtree_parentfd(new), new->name, O_CLOEXEC), flags);
126 
127   // If this had children, it was callback's job to free them already.
128   if (!(flags & DIRTREE_SAVE)) {
129     free(new);
130     new = 0;
131   }
132 
133   return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
134 }
135 
136 // Recursively read/process children of directory node, filtering through
137 // callback(). Uses and closes supplied ->dirfd.
138 
dirtree_recurse(struct dirtree * node,int (* callback)(struct dirtree * node),int dirfd,int flags)139 int dirtree_recurse(struct dirtree *node,
140           int (*callback)(struct dirtree *node), int dirfd, int flags)
141 {
142   struct dirtree *new, **ddt = &(node->child);
143   struct dirent *entry;
144   DIR *dir;
145 
146   node->dirfd = dirfd;
147   if (node->dirfd == -1 || !(dir = fdopendir(node->dirfd))) {
148     if (!(flags & DIRTREE_SHUTUP)) {
149       char *path = dirtree_path(node, 0);
150       perror_msg_raw(path);
151       free(path);
152     }
153     close(node->dirfd);
154 
155     return flags;
156   }
157 
158   // according to the fddir() man page, the filehandle in the DIR * can still
159   // be externally used by things that don't lseek() it.
160 
161   // The extra parentheses are to shut the stupid compiler up.
162   while ((entry = readdir(dir))) {
163     if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
164     if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
165     if (!new->st.st_blksize && !new->st.st_mode)
166       new->st.st_mode = entry->d_type<<12;
167     new = dirtree_handle_callback(new, callback);
168     if (new == DIRTREE_ABORTVAL) break;
169     if (new) {
170       *ddt = new;
171       ddt = &((*ddt)->next);
172     }
173   }
174 
175   if (flags & DIRTREE_COMEAGAIN) {
176     node->again |= 1;
177     flags = callback(node);
178   }
179 
180   // This closes filehandle as well, so note it
181   closedir(dir);
182   node->dirfd = -1;
183 
184   return flags;
185 }
186 
187 // Create dirtree from path, using callback to filter nodes. If !callback
188 // return just the top node. Use dirtree_notdotdot callback to allocate a
189 // tree of struct dirtree nodes and return pointer to root node for later
190 // processing.
191 // Returns DIRTREE_ABORTVAL if path didn't exist (use DIRTREE_SHUTUP to handle
192 // error message yourself).
193 
dirtree_flagread(char * path,int flags,int (* callback)(struct dirtree * node))194 struct dirtree *dirtree_flagread(char *path, int flags,
195   int (*callback)(struct dirtree *node))
196 {
197   return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback);
198 }
199 
200 // Common case
dirtree_read(char * path,int (* callback)(struct dirtree * node))201 struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
202 {
203   return dirtree_flagread(path, 0, callback);
204 }
205