blob: 33188e1f62be85452b0b37f5d03b6dfcce556030 [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
25// filehandle space on large trees. handle_callback() does that instead.)
Rob Landley7bc79712008-02-16 19:41:20 -060026
Rob Landley3162c272012-12-06 15:13:30 -060027struct dirtree *dirtree_add_node(struct dirtree *parent, char *name,
28 int symfollow)
Rob Landley103b7e02007-10-04 02:04:10 -050029{
Rob Landley7aa651a2012-11-13 17:14:08 -060030 struct dirtree *dt = NULL;
31 struct stat st;
32 char buf[4096];
33 int len = 0, linklen = 0;
Rob Landley103b7e02007-10-04 02:04:10 -050034
Rob Landley7aa651a2012-11-13 17:14:08 -060035 if (name) {
Rob Landley3162c272012-12-06 15:13:30 -060036 // open code this because haven't got node to call dirtree_parentfd() on yet
37 int fd = parent ? parent->data : AT_FDCWD;
38
39 if (fstatat(fd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW)) goto error;
Rob Landley7aa651a2012-11-13 17:14:08 -060040 if (S_ISLNK(st.st_mode)) {
Rob Landley3162c272012-12-06 15:13:30 -060041 if (0>(linklen = readlinkat(fd, name, buf, 4095))) goto error;
Rob Landley7aa651a2012-11-13 17:14:08 -060042 buf[linklen++]=0;
43 }
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 Landley7aa651a2012-11-13 17:14:08 -060052 if (linklen) {
53 dt->symlink = memcpy(len+(char *)dt, buf, linklen);
54 dt->data = --linklen;
55 }
56 }
Rob Landley103b7e02007-10-04 02:04:10 -050057
Rob Landley7aa651a2012-11-13 17:14:08 -060058 return dt;
Rob Landleyeb7ea222012-04-14 22:30:41 -050059
60error:
Rob Landley3162c272012-12-06 15:13:30 -060061 if (notdotdot(name)) {
62 char *path = parent ? dirtree_path(parent, 0) : "";
63 perror_msg("%s%s%s",path, parent ? "/" : "", name);
64 }
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 Landleyeb7ea222012-04-14 22:30:41 -050071char *dirtree_path(struct dirtree *node, int *plen)
Rob Landley103b7e02007-10-04 02:04:10 -050072{
Rob Landley7aa651a2012-11-13 17:14:08 -060073 char *path;
74 int len;
Rob Landley103b7e02007-10-04 02:04:10 -050075
Rob Landley7aa651a2012-11-13 17:14:08 -060076 if (!node || !node->name) {
77 path = xmalloc(*plen);
78 *plen = 0;
79 return path;
80 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050081
Rob Landley7aa651a2012-11-13 17:14:08 -060082 len = (plen ? *plen : 0)+strlen(node->name)+1;
83 path = dirtree_path(node->parent, &len);
84 if (len && path[len-1] != '/') path[len++]='/';
85 len = (stpcpy(path+len, node->name) - path);
86 if (plen) *plen = len;
Rob Landleyeb7ea222012-04-14 22:30:41 -050087
Rob Landley7aa651a2012-11-13 17:14:08 -060088 return path;
Rob Landleyeb7ea222012-04-14 22:30:41 -050089}
90
Rob Landley6ec21782012-06-16 15:16:08 -050091int dirtree_parentfd(struct dirtree *node)
92{
Rob Landley7aa651a2012-11-13 17:14:08 -060093 return node->parent ? node->parent->data : AT_FDCWD;
Rob Landley6ec21782012-06-16 15:16:08 -050094}
95
Rob Landleyeb7ea222012-04-14 22:30:41 -050096// Handle callback for a node in the tree. Returns saved node(s) or NULL.
97//
98// By default, allocates a tree of struct dirtree, not following symlinks
99// If callback==NULL, or callback always returns 0, allocate tree of struct
Rob Landley6cd8ae62012-05-27 00:56:17 -0500100// dirtree and return root of tree. Otherwise call callback(node) on each
101// hit, free structures after use, and return NULL.
Rob Landleyeb7ea222012-04-14 22:30:41 -0500102//
103
104struct dirtree *handle_callback(struct dirtree *new,
Rob Landley7aa651a2012-11-13 17:14:08 -0600105 int (*callback)(struct dirtree *node))
Rob Landleyeb7ea222012-04-14 22:30:41 -0500106{
Rob Landley7aa651a2012-11-13 17:14:08 -0600107 int flags, dir = S_ISDIR(new->st.st_mode);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500108
Rob Landley7aa651a2012-11-13 17:14:08 -0600109 if (!callback) callback = dirtree_notdotdot;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500110
Rob Landley7aa651a2012-11-13 17:14:08 -0600111 flags = callback(new);
Rob Landley6cd8ae62012-05-27 00:56:17 -0500112
Rob Landley7aa651a2012-11-13 17:14:08 -0600113 if (dir) {
114 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
115 new->data = openat(dirtree_parentfd(new), new->name, 0);
116 dirtree_recurse(new, callback, flags & DIRTREE_SYMFOLLOW);
117 if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
118 }
119 }
Rob Landley6cd8ae62012-05-27 00:56:17 -0500120
Rob Landley7aa651a2012-11-13 17:14:08 -0600121 // If this had children, it was callback's job to free them already.
122 if (!(flags & DIRTREE_SAVE)) {
123 free(new);
124 new = NULL;
125 }
Rob Landley103b7e02007-10-04 02:04:10 -0500126
Rob Landley7aa651a2012-11-13 17:14:08 -0600127 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
Rob Landley103b7e02007-10-04 02:04:10 -0500128}
129
Rob Landleyeb7ea222012-04-14 22:30:41 -0500130// Recursively read/process children of directory node (with dirfd in data),
131// filtering through callback().
Rob Landley103b7e02007-10-04 02:04:10 -0500132
Rob Landleyeb7ea222012-04-14 22:30:41 -0500133void dirtree_recurse(struct dirtree *node,
Rob Landley7aa651a2012-11-13 17:14:08 -0600134 int (*callback)(struct dirtree *node), int symfollow)
Rob Landleyeb7ea222012-04-14 22:30:41 -0500135{
Rob Landley7aa651a2012-11-13 17:14:08 -0600136 struct dirtree *new, **ddt = &(node->child);
137 struct dirent *entry;
138 DIR *dir;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500139
Rob Landley7aa651a2012-11-13 17:14:08 -0600140 if (!(dir = fdopendir(node->data))) {
141 char *path = dirtree_path(node, 0);
142 perror_msg("No %s", path);
143 free(path);
144 close(node->data);
Rob Landley9b3af462012-04-22 23:01:23 -0500145
Rob Landley7aa651a2012-11-13 17:14:08 -0600146 return;
147 }
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 Landley3162c272012-12-06 15:13:30 -0600154 if (!(new = dirtree_add_node(node, entry->d_name, symfollow)))
Rob Landley7aa651a2012-11-13 17:14:08 -0600155 continue;
Rob Landley7aa651a2012-11-13 17:14:08 -0600156 new = handle_callback(new, callback);
157 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 Landley7aa651a2012-11-13 17:14:08 -0600164 // This closes filehandle as well, so note it
165 closedir(dir);
166 node->data = -1;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500167}
168
169// Create dirtree from path, using callback to filter nodes.
170// If callback == NULL allocate a tree of struct dirtree nodes and return
171// pointer to root node.
Rob Landley78aaef22012-06-24 18:35:49 -0500172// symfollow is just for the top of tree, callback return code controls children
Rob Landleyeb7ea222012-04-14 22:30:41 -0500173
174struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
175{
Rob Landley3162c272012-12-06 15:13:30 -0600176 struct dirtree *root = dirtree_add_node(0, path, 0);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500177
Rob Landley7aa651a2012-11-13 17:14:08 -0600178 return root ? handle_callback(root, callback) : DIRTREE_ABORTVAL;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500179}