blob: f9524a5d34463013efbe434e70b3db0273ad01a7 [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 Landley382057f2016-11-21 16:47:23 -06008int isdotdot(char *name)
Rob Landley3162c272012-12-06 15:13:30 -06009{
Rob Landley382057f2016-11-21 16:47:23 -060010 if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 1;
Rob Landley3162c272012-12-06 15:13:30 -060011
Rob Landley382057f2016-11-21 16:47:23 -060012 return 0;
Rob Landley3162c272012-12-06 15:13:30 -060013}
14
Rob Landley382057f2016-11-21 16:47:23 -060015// Default callback, filters out "." and ".." except at top level.
Rob Landley3162c272012-12-06 15:13:30 -060016
17int dirtree_notdotdot(struct dirtree *catch)
18{
19 // Should we skip "." and ".."?
Rob Landley382057f2016-11-21 16:47:23 -060020 return (!catch->parent||!isdotdot(catch->name))
21 *(DIRTREE_SAVE|DIRTREE_RECURSE);
Rob Landley3162c272012-12-06 15:13:30 -060022}
23
Rob Landleyeb7ea222012-04-14 22:30:41 -050024// Create a dirtree node from a path, with stat and symlink info.
Rob Landley9b3af462012-04-22 23:01:23 -050025// (This doesn't open directory filehandles yet so as not to exhaust the
Rob Landley090c5c62012-12-31 14:38:13 -060026// filehandle space on large trees, dirtree_handle_callback() does that.)
Rob Landley7bc79712008-02-16 19:41:20 -060027
Rob Landleyaab91642015-05-09 18:11:22 -050028struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags)
Rob Landley103b7e02007-10-04 02:04:10 -050029{
Rob Landley7aa651a2012-11-13 17:14:08 -060030 struct dirtree *dt = NULL;
31 struct stat st;
Rob Landley7aa651a2012-11-13 17:14:08 -060032 int len = 0, linklen = 0;
Rob Landley103b7e02007-10-04 02:04:10 -050033
Rob Landley7aa651a2012-11-13 17:14:08 -060034 if (name) {
Rob Landley3162c272012-12-06 15:13:30 -060035 // open code this because haven't got node to call dirtree_parentfd() on yet
Rob Landleyd336af42015-12-06 20:36:59 -060036 int fd = parent ? parent->dirfd : AT_FDCWD;
Rob Landley3162c272012-12-06 15:13:30 -060037
Rob Landleyaab91642015-05-09 18:11:22 -050038 if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW)))
39 goto error;
Rob Landley7aa651a2012-11-13 17:14:08 -060040 if (S_ISLNK(st.st_mode)) {
Rob Landleyaab91642015-05-09 18:11:22 -050041 if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error;
42 libbuf[linklen++]=0;
Rob Landley7aa651a2012-11-13 17:14:08 -060043 }
44 len = strlen(name);
45 }
46 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
Rob Landley3162c272012-12-06 15:13:30 -060047 dt->parent = parent;
Rob Landley7aa651a2012-11-13 17:14:08 -060048 if (name) {
49 memcpy(&(dt->st), &st, sizeof(struct stat));
50 strcpy(dt->name, name);
Rob Landley103b7e02007-10-04 02:04:10 -050051
Rob Landleyd336af42015-12-06 20:36:59 -060052 if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen);
Rob Landley7aa651a2012-11-13 17:14:08 -060053 }
Rob Landley103b7e02007-10-04 02:04:10 -050054
Rob Landley7aa651a2012-11-13 17:14:08 -060055 return dt;
Rob Landleyeb7ea222012-04-14 22:30:41 -050056
57error:
Rob Landley382057f2016-11-21 16:47:23 -060058 if (!(flags&DIRTREE_SHUTUP) && !isdotdot(name)) {
Rob Landley3162c272012-12-06 15:13:30 -060059 char *path = parent ? dirtree_path(parent, 0) : "";
Rob Landley280bb592014-07-21 19:55:06 -050060
61 perror_msg("%s%s%s", path, parent ? "/" : "", name);
62 if (parent) free(path);
Rob Landley3162c272012-12-06 15:13:30 -060063 }
Rob Landley37de8ed2012-12-08 02:26:56 -060064 if (parent) parent->symlink = (char *)1;
Rob Landley7aa651a2012-11-13 17:14:08 -060065 free(dt);
66 return 0;
Rob Landley103b7e02007-10-04 02:04:10 -050067}
68
Rob Landley9b3af462012-04-22 23:01:23 -050069// Return path to this node, assembled recursively.
Rob Landley103b7e02007-10-04 02:04:10 -050070
Rob Landley29d30be2014-09-22 07:52:15 -050071// Initial call can pass in NULL to plen, or point to an int initialized to 0
72// to return the length of the path, or a value greater than 0 to allocate
73// extra space if you want to append your own text to the string.
74
Rob Landleyeb7ea222012-04-14 22:30:41 -050075char *dirtree_path(struct dirtree *node, int *plen)
Rob Landley103b7e02007-10-04 02:04:10 -050076{
Rob Landley7aa651a2012-11-13 17:14:08 -060077 char *path;
78 int len;
Rob Landley103b7e02007-10-04 02:04:10 -050079
Rob Landley29d30be2014-09-22 07:52:15 -050080 if (!node) {
Rob Landley7aa651a2012-11-13 17:14:08 -060081 path = xmalloc(*plen);
82 *plen = 0;
83 return path;
84 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050085
Rob Landley7aa651a2012-11-13 17:14:08 -060086 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;
Rob Landleyeb7ea222012-04-14 22:30:41 -050091
Rob Landley7aa651a2012-11-13 17:14:08 -060092 return path;
Rob Landleyeb7ea222012-04-14 22:30:41 -050093}
94
Rob Landley6ec21782012-06-16 15:16:08 -050095int dirtree_parentfd(struct dirtree *node)
96{
Rob Landleyd336af42015-12-06 20:36:59 -060097 return node->parent ? node->parent->dirfd : AT_FDCWD;
Rob Landley6ec21782012-06-16 15:16:08 -050098}
99
Rob Landley8d950742016-03-07 16:02:47 -0600100// Handle callback for a node in the tree. Returns saved node(s) if
101// callback returns DIRTREE_SAVE, otherwise frees consumed nodes and
102// returns NULL. If !callback return top node unchanged.
103// If !new return DIRTREE_ABORTVAL
Rob Landleyeb7ea222012-04-14 22:30:41 -0500104
Rob Landley090c5c62012-12-31 14:38:13 -0600105struct dirtree *dirtree_handle_callback(struct dirtree *new,
Rob Landley7aa651a2012-11-13 17:14:08 -0600106 int (*callback)(struct dirtree *node))
Rob Landleyeb7ea222012-04-14 22:30:41 -0500107{
Rob Landleyaab91642015-05-09 18:11:22 -0500108 int flags;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500109
Rob Landley8d950742016-03-07 16:02:47 -0600110 if (!new) return DIRTREE_ABORTVAL;
111 if (!callback) return new;
Rob Landley7aa651a2012-11-13 17:14:08 -0600112 flags = callback(new);
Rob Landley6cd8ae62012-05-27 00:56:17 -0500113
Rob Landleybb77dde2016-04-21 17:46:25 -0500114 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);
Rob Landley6cd8ae62012-05-27 00:56:17 -0500117
Rob Landley7aa651a2012-11-13 17:14:08 -0600118 // If this had children, it was callback's job to free them already.
119 if (!(flags & DIRTREE_SAVE)) {
120 free(new);
121 new = NULL;
122 }
Rob Landley103b7e02007-10-04 02:04:10 -0500123
Rob Landley7aa651a2012-11-13 17:14:08 -0600124 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
Rob Landley103b7e02007-10-04 02:04:10 -0500125}
126
Rob Landleyd336af42015-12-06 20:36:59 -0600127// Recursively read/process children of directory node, filtering through
128// callback(). Uses and closes supplied ->dirfd.
Rob Landley103b7e02007-10-04 02:04:10 -0500129
Rob Landleyfec3fd12014-07-26 13:30:40 -0500130int dirtree_recurse(struct dirtree *node,
Rob Landleybb77dde2016-04-21 17:46:25 -0500131 int (*callback)(struct dirtree *node), int dirfd, int flags)
Rob Landleyeb7ea222012-04-14 22:30:41 -0500132{
Rob Landley7aa651a2012-11-13 17:14:08 -0600133 struct dirtree *new, **ddt = &(node->child);
134 struct dirent *entry;
135 DIR *dir;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500136
Rob Landleybb77dde2016-04-21 17:46:25 -0500137 node->dirfd = dirfd;
Rob Landleyd336af42015-12-06 20:36:59 -0600138 if (node->dirfd == -1 || !(dir = fdopendir(node->dirfd))) {
Rob Landleyaab91642015-05-09 18:11:22 -0500139 if (!(flags & DIRTREE_SHUTUP)) {
140 char *path = dirtree_path(node, 0);
141 perror_msg("No %s", path);
142 free(path);
143 }
Rob Landleyd336af42015-12-06 20:36:59 -0600144 close(node->dirfd);
Rob Landley9b3af462012-04-22 23:01:23 -0500145
Rob Landleyfec3fd12014-07-26 13:30:40 -0500146 return flags;
Rob Landley7aa651a2012-11-13 17:14:08 -0600147 }
Rob Landley9b3af462012-04-22 23:01:23 -0500148
Rob Landley7aa651a2012-11-13 17:14:08 -0600149 // according to the fddir() man page, the filehandle in the DIR * can still
150 // be externally used by things that don't lseek() it.
Rob Landleyeb7ea222012-04-14 22:30:41 -0500151
Rob Landley7aa651a2012-11-13 17:14:08 -0600152 // The extra parentheses are to shut the stupid compiler up.
153 while ((entry = readdir(dir))) {
Rob Landleya9759522016-12-08 21:29:00 -0600154 if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue;
Rob Landleyaab91642015-05-09 18:11:22 -0500155 if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue;
Rob Landley090c5c62012-12-31 14:38:13 -0600156 new = dirtree_handle_callback(new, callback);
Rob Landley7aa651a2012-11-13 17:14:08 -0600157 if (new == DIRTREE_ABORTVAL) break;
158 if (new) {
159 *ddt = new;
160 ddt = &((*ddt)->next);
161 }
162 }
Rob Landleyeb7ea222012-04-14 22:30:41 -0500163
Rob Landley749c5232014-07-29 20:02:31 -0500164 if (flags & DIRTREE_COMEAGAIN) {
165 node->again++;
166 flags = callback(node);
167 }
Rob Landleyfec3fd12014-07-26 13:30:40 -0500168
Rob Landley7aa651a2012-11-13 17:14:08 -0600169 // This closes filehandle as well, so note it
170 closedir(dir);
Rob Landleyd336af42015-12-06 20:36:59 -0600171 node->dirfd = -1;
Rob Landleyfec3fd12014-07-26 13:30:40 -0500172
173 return flags;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500174}
175
Rob Landley8d950742016-03-07 16:02:47 -0600176// Create dirtree from path, using callback to filter nodes. If !callback
177// return just the top node. Use dirtree_notdotdot callback to allocate a
178// tree of struct dirtree nodes and return pointer to root node for later
179// processing.
180// Returns DIRTREE_ABORTVAL if path didn't exist (use DIRTREE_SHUTUP to handle
181// error message yourself).
182
183struct dirtree *dirtree_flagread(char *path, int flags,
184 int (*callback)(struct dirtree *node))
Rob Landleyaab91642015-05-09 18:11:22 -0500185{
Rob Landley8d950742016-03-07 16:02:47 -0600186 return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback);
Rob Landleyaab91642015-05-09 18:11:22 -0500187}
188
Rob Landley8d950742016-03-07 16:02:47 -0600189// Common case
Rob Landleyeb7ea222012-04-14 22:30:41 -0500190struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
191{
Rob Landley8d950742016-03-07 16:02:47 -0600192 return dirtree_flagread(path, 0, callback);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500193}