blob: 1e8981619857be9db13e340af95edcf39dd1aa3e [file] [log] [blame]
Rob Landley103b7e02007-10-04 02:04:10 -05001/* dirtree.c - Functions for dealing with directory trees.
2 *
3 * Copyright 2007 Rob Landley <rob@landley.net>
4 */
5
6#include "toys.h"
7
Rob Landley3162c272012-12-06 15:13:30 -06008static int notdotdot(char *name)
9{
10 if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 0;
11
12 return 1;
13}
14
15// Default callback, filters out "." and "..".
16
17int dirtree_notdotdot(struct dirtree *catch)
18{
19 // Should we skip "." and ".."?
20 return notdotdot(catch->name) ? DIRTREE_SAVE|DIRTREE_RECURSE : 0;
21}
22
Rob Landleyeb7ea222012-04-14 22:30:41 -050023// Create a dirtree node from a path, with stat and symlink info.
Rob Landley9b3af462012-04-22 23:01:23 -050024// (This doesn't open directory filehandles yet so as not to exhaust the
Rob Landley090c5c62012-12-31 14:38:13 -060025// filehandle space on large trees, dirtree_handle_callback() does that.)
Rob Landley7bc79712008-02-16 19:41:20 -060026
Rob Landleyaab91642015-05-09 18:11:22 -050027struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
Rob Landley103b7e02007-10-04 02:04:10 -050028{
Rob Landley7aa651a2012-11-13 17:14:08 -060029 struct dirtree *dt = NULL;
30 struct stat st;
Rob Landley7aa651a2012-11-13 17:14:08 -060031 int len = 0, linklen = 0;
Rob Landley103b7e02007-10-04 02:04:10 -050032
Rob Landley7aa651a2012-11-13 17:14:08 -060033 if (name) {
Rob Landley3162c272012-12-06 15:13:30 -060034 // open code this because haven't got node to call dirtree_parentfd() on yet
35 int fd = parent ? parent->data : AT_FDCWD;
36
Rob Landleyaab91642015-05-09 18:11:22 -050037 if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW)))
38 goto error;
Rob Landley7aa651a2012-11-13 17:14:08 -060039 if (S_ISLNK(st.st_mode)) {
Rob Landleyaab91642015-05-09 18:11:22 -050040 if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
41 libbuf[linklen++]=0;
Rob Landley7aa651a2012-11-13 17:14:08 -060042 }
43 len = strlen(name);
44 }
45 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
Rob Landley3162c272012-12-06 15:13:30 -060046 dt->parent = parent;
Rob Landley7aa651a2012-11-13 17:14:08 -060047 if (name) {
48 memcpy(&(dt->st), &st, sizeof(struct stat));
49 strcpy(dt->name, name);
Rob Landley103b7e02007-10-04 02:04:10 -050050
Rob Landley7aa651a2012-11-13 17:14:08 -060051 if (linklen) {
Rob Landleyaab91642015-05-09 18:11:22 -050052 dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
Rob Landley7aa651a2012-11-13 17:14:08 -060053 dt->data = --linklen;
54 }
55 }
Rob Landley103b7e02007-10-04 02:04:10 -050056
Rob Landley7aa651a2012-11-13 17:14:08 -060057 return dt;
Rob Landleyeb7ea222012-04-14 22:30:41 -050058
59error:
Rob Landleyaab91642015-05-09 18:11:22 -050060 if (!(flags&DIRTREE_SHUTUP) && notdotdot(name)) {
Rob Landley3162c272012-12-06 15:13:30 -060061 char *path = parent ? dirtree_path(parent, 0) : "";
Rob Landley280bb592014-07-21 19:55:06 -050062
63 perror_msg("%s%s%s", path, parent ? "/" : "", name);
64 if (parent) free(path);
Rob Landley3162c272012-12-06 15:13:30 -060065 }
Rob Landley37de8ed2012-12-08 02:26:56 -060066 if (parent) parent->symlink = (char *)1;
Rob Landley7aa651a2012-11-13 17:14:08 -060067 free(dt);
68 return 0;
Rob Landley103b7e02007-10-04 02:04:10 -050069}
70
Rob Landley9b3af462012-04-22 23:01:23 -050071// Return path to this node, assembled recursively.
Rob Landley103b7e02007-10-04 02:04:10 -050072
Rob Landley29d30be2014-09-22 07:52:15 -050073// Initial call can pass in NULL to plen, or point to an int initialized to 0
74// to return the length of the path, or a value greater than 0 to allocate
75// extra space if you want to append your own text to the string.
76
Rob Landleyeb7ea222012-04-14 22:30:41 -050077char *dirtree_path(struct dirtree *node, int *plen)
Rob Landley103b7e02007-10-04 02:04:10 -050078{
Rob Landley7aa651a2012-11-13 17:14:08 -060079 char *path;
80 int len;
Rob Landley103b7e02007-10-04 02:04:10 -050081
Rob Landley29d30be2014-09-22 07:52:15 -050082 if (!node) {
Rob Landley7aa651a2012-11-13 17:14:08 -060083 path = xmalloc(*plen);
84 *plen = 0;
85 return path;
86 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050087
Rob Landley7aa651a2012-11-13 17:14:08 -060088 len = (plen ? *plen : 0)+strlen(node->name)+1;
89 path = dirtree_path(node->parent, &len);
90 if (len && path[len-1] != '/') path[len++]='/';
91 len = (stpcpy(path+len, node->name) - path);
92 if (plen) *plen = len;
Rob Landleyeb7ea222012-04-14 22:30:41 -050093
Rob Landley7aa651a2012-11-13 17:14:08 -060094 return path;
Rob Landleyeb7ea222012-04-14 22:30:41 -050095}
96
Rob Landley6ec21782012-06-16 15:16:08 -050097int dirtree_parentfd(struct dirtree *node)
98{
Rob Landley7aa651a2012-11-13 17:14:08 -060099 return node->parent ? node->parent->data : AT_FDCWD;
Rob Landley6ec21782012-06-16 15:16:08 -0500100}
101
Rob Landleyeb7ea222012-04-14 22:30:41 -0500102// Handle callback for a node in the tree. Returns saved node(s) or NULL.
103//
104// By default, allocates a tree of struct dirtree, not following symlinks
105// If callback==NULL, or callback always returns 0, allocate tree of struct
Rob Landley6cd8ae62012-05-27 00:56:17 -0500106// dirtree and return root of tree. Otherwise call callback(node) on each
107// hit, free structures after use, and return NULL.
Rob Landleyeb7ea222012-04-14 22:30:41 -0500108//
109
Rob Landley090c5c62012-12-31 14:38:13 -0600110struct dirtree *dirtree_handle_callback(struct dirtree *new,
Rob Landley7aa651a2012-11-13 17:14:08 -0600111 int (*callback)(struct dirtree *node))
Rob Landleyeb7ea222012-04-14 22:30:41 -0500112{
Rob Landleyaab91642015-05-09 18:11:22 -0500113 int flags;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500114
Rob Landleyaab91642015-05-09 18:11:22 -0500115 if (!new) return 0;
Rob Landley7aa651a2012-11-13 17:14:08 -0600116 if (!callback) callback = dirtree_notdotdot;
Rob Landley7aa651a2012-11-13 17:14:08 -0600117 flags = callback(new);
Rob Landley6cd8ae62012-05-27 00:56:17 -0500118
Rob Landleyaab91642015-05-09 18:11:22 -0500119 if (S_ISDIR(new->st.st_mode)) {
Rob Landley7aa651a2012-11-13 17:14:08 -0600120 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
Rob Landleyccb73f82014-07-26 13:27:07 -0500121 new->data = openat(dirtree_parentfd(new), new->name, O_CLOEXEC);
Rob Landleyfec3fd12014-07-26 13:30:40 -0500122 flags = dirtree_recurse(new, callback, flags);
Rob Landley7aa651a2012-11-13 17:14:08 -0600123 }
124 }
Rob Landley6cd8ae62012-05-27 00:56:17 -0500125
Rob Landley7aa651a2012-11-13 17:14:08 -0600126 // If this had children, it was callback's job to free them already.
127 if (!(flags & DIRTREE_SAVE)) {
128 free(new);
129 new = NULL;
130 }
Rob Landley103b7e02007-10-04 02:04:10 -0500131
Rob Landley7aa651a2012-11-13 17:14:08 -0600132 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
Rob Landley103b7e02007-10-04 02:04:10 -0500133}
134
Rob Landleyeb7ea222012-04-14 22:30:41 -0500135// Recursively read/process children of directory node (with dirfd in data),
136// filtering through callback().
Rob Landley103b7e02007-10-04 02:04:10 -0500137
Rob Landleyfec3fd12014-07-26 13:30:40 -0500138int dirtree_recurse(struct dirtree *node,
139 int (*callback)(struct dirtree *node), int flags)
Rob Landleyeb7ea222012-04-14 22:30:41 -0500140{
Rob Landley7aa651a2012-11-13 17:14:08 -0600141 struct dirtree *new, **ddt = &(node->child);
142 struct dirent *entry;
143 DIR *dir;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500144
Isaac Dunhamc810f9f2013-07-06 11:26:15 -0500145 if (node->data == -1 || !(dir = fdopendir(node->data))) {
Rob Landleyaab91642015-05-09 18:11:22 -0500146 if (!(flags & DIRTREE_SHUTUP)) {
147 char *path = dirtree_path(node, 0);
148 perror_msg("No %s", path);
149 free(path);
150 }
Rob Landley7aa651a2012-11-13 17:14:08 -0600151 close(node->data);
Rob Landley9b3af462012-04-22 23:01:23 -0500152
Rob Landleyfec3fd12014-07-26 13:30:40 -0500153 return flags;
Rob Landley7aa651a2012-11-13 17:14:08 -0600154 }
Rob Landley9b3af462012-04-22 23:01:23 -0500155
Rob Landley7aa651a2012-11-13 17:14:08 -0600156 // according to the fddir() man page, the filehandle in the DIR * can still
157 // be externally used by things that don't lseek() it.
Rob Landleyeb7ea222012-04-14 22:30:41 -0500158
Rob Landley7aa651a2012-11-13 17:14:08 -0600159 // The extra parentheses are to shut the stupid compiler up.
160 while ((entry = readdir(dir))) {
Rob Landleyaab91642015-05-09 18:11:22 -0500161 if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
Rob Landley090c5c62012-12-31 14:38:13 -0600162 new = dirtree_handle_callback(new, callback);
Rob Landley7aa651a2012-11-13 17:14:08 -0600163 if (new == DIRTREE_ABORTVAL) break;
164 if (new) {
165 *ddt = new;
166 ddt = &((*ddt)->next);
167 }
168 }
Rob Landleyeb7ea222012-04-14 22:30:41 -0500169
Rob Landley749c5232014-07-29 20:02:31 -0500170 if (flags & DIRTREE_COMEAGAIN) {
171 node->again++;
172 flags = callback(node);
173 }
Rob Landleyfec3fd12014-07-26 13:30:40 -0500174
Rob Landley7aa651a2012-11-13 17:14:08 -0600175 // This closes filehandle as well, so note it
176 closedir(dir);
177 node->data = -1;
Rob Landleyfec3fd12014-07-26 13:30:40 -0500178
179 return flags;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500180}
181
Rob Landleyaab91642015-05-09 18:11:22 -0500182// Create dirtree root
183struct dirtree *dirtree_start(char *name, int symfollow)
184{
185 return dirtree_add_node(0, name, DIRTREE_SYMFOLLOW*!!symfollow);
186}
187
Rob Landleyeb7ea222012-04-14 22:30:41 -0500188// Create dirtree from path, using callback to filter nodes.
189// If callback == NULL allocate a tree of struct dirtree nodes and return
190// pointer to root node.
191
192struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
193{
Rob Landleyaab91642015-05-09 18:11:22 -0500194 struct dirtree *root = dirtree_start(path, 0);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500195
Rob Landley090c5c62012-12-31 14:38:13 -0600196 return root ? dirtree_handle_callback(root, callback) : DIRTREE_ABORTVAL;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500197}