blob: 53d519027c7b8c3132cb39fcae6319e0818baa3f [file] [log] [blame]
Rob Landley103b7e02007-10-04 02:04:10 -05001/* vi: set sw=4 ts=4 :*/
2/* dirtree.c - Functions for dealing with directory trees.
3 *
4 * Copyright 2007 Rob Landley <rob@landley.net>
5 */
6
7#include "toys.h"
8
Rob Landleyeb7ea222012-04-14 22:30:41 -05009// Create a dirtree node from a path, with stat and symlink info.
Rob Landley9b3af462012-04-22 23:01:23 -050010// (This doesn't open directory filehandles yet so as not to exhaust the
11// filehandle space on large trees. handle_callback() does that instead.)
Rob Landley7bc79712008-02-16 19:41:20 -060012
Rob Landley4af1e1d2012-06-09 22:25:49 -050013struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow)
Rob Landley103b7e02007-10-04 02:04:10 -050014{
Rob Landleyeb7ea222012-04-14 22:30:41 -050015 struct dirtree *dt = NULL;
16 struct stat st;
17 char buf[4096];
18 int len = 0, linklen = 0;
Rob Landley103b7e02007-10-04 02:04:10 -050019
Rob Landleyeb7ea222012-04-14 22:30:41 -050020 if (name) {
Rob Landley4af1e1d2012-06-09 22:25:49 -050021 if (fstatat(dirfd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW))
22 goto error;
Rob Landleyeb7ea222012-04-14 22:30:41 -050023 if (S_ISLNK(st.st_mode)) {
24 if (0>(linklen = readlinkat(dirfd, name, buf, 4095))) goto error;
25 buf[linklen++]=0;
Rob Landley103b7e02007-10-04 02:04:10 -050026 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050027 len = strlen(name);
Rob Landley103b7e02007-10-04 02:04:10 -050028 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050029 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
30 if (name) {
31 memcpy(&(dt->st), &st, sizeof(struct stat));
32 strcpy(dt->name, name);
Rob Landley103b7e02007-10-04 02:04:10 -050033
Rob Landleyeb7ea222012-04-14 22:30:41 -050034 if (linklen) {
35 dt->symlink = memcpy(len+(char *)dt, buf, linklen);
36 dt->data = --linklen;
37 }
Rob Landley7bc79712008-02-16 19:41:20 -060038 }
Rob Landley103b7e02007-10-04 02:04:10 -050039
40 return dt;
Rob Landleyeb7ea222012-04-14 22:30:41 -050041
42error:
43 perror_msg("%s",name);
44 free(dt);
45 return 0;
Rob Landley103b7e02007-10-04 02:04:10 -050046}
47
Rob Landley9b3af462012-04-22 23:01:23 -050048// Return path to this node, assembled recursively.
Rob Landley103b7e02007-10-04 02:04:10 -050049
Rob Landleyeb7ea222012-04-14 22:30:41 -050050char *dirtree_path(struct dirtree *node, int *plen)
Rob Landley103b7e02007-10-04 02:04:10 -050051{
Rob Landleyeb7ea222012-04-14 22:30:41 -050052 char *path;
53 int len;
Rob Landley103b7e02007-10-04 02:04:10 -050054
Rob Landley9b3af462012-04-22 23:01:23 -050055 if (!node || !node->name) {
56 path = xmalloc(*plen);
57 *plen = 0;
58 return path;
59 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050060
Rob Landley9b3af462012-04-22 23:01:23 -050061 len = (plen ? *plen : 0)+strlen(node->name)+1;
Rob Landleyeb7ea222012-04-14 22:30:41 -050062 path = dirtree_path(node->parent, &len);
Rob Landley4d0f02f2012-05-09 06:11:23 -050063 if (len) path[len++]='/';
Rob Landley9b3af462012-04-22 23:01:23 -050064 len = (stpcpy(path+len, node->name) - path);
Rob Landley4d0f02f2012-05-09 06:11:23 -050065 if (plen) *plen = len;
Rob Landleyeb7ea222012-04-14 22:30:41 -050066
67 return path;
68}
69
70// Default callback, filters out "." and "..".
71
Rob Landley8c4ae8a2012-05-20 15:00:19 -050072int dirtree_notdotdot(struct dirtree *catch)
Rob Landleyeb7ea222012-04-14 22:30:41 -050073{
74 // Should we skip "." and ".."?
75 if (catch->name[0]=='.' && (!catch->name[1] ||
76 (catch->name[1]=='.' && !catch->name[2])))
Rob Landley8c4ae8a2012-05-20 15:00:19 -050077 return 0;
Rob Landleyeb7ea222012-04-14 22:30:41 -050078
Rob Landley8c4ae8a2012-05-20 15:00:19 -050079 return DIRTREE_SAVE|DIRTREE_RECURSE;
Rob Landleyeb7ea222012-04-14 22:30:41 -050080}
81
Rob Landley6ec21782012-06-16 15:16:08 -050082int dirtree_parentfd(struct dirtree *node)
83{
84 return node->parent ? node->parent->data : AT_FDCWD;
85}
86
Rob Landley38d3cfb2012-06-01 20:27:51 -050087// get open filehandle for node in extra, giving caller the option of
88// using DIRTREE_COMEAGAIN or not.
89int dirtree_opennode(struct dirtree *try)
Rob Landley4fa1b322012-06-01 20:04:39 -050090{
Rob Landley38d3cfb2012-06-01 20:27:51 -050091 if (!dirtree_notdotdot(try)) return 0;
92 if (S_ISDIR(try->st.st_mode)) {
93 if (!try->extra) {
94 try->extra = xdup(try->data);
95 return DIRTREE_COMEAGAIN;
96 }
Rob Landley6ec21782012-06-16 15:16:08 -050097 } else try->extra = openat(dirtree_parentfd(try), try->name, 0);
Rob Landley4fa1b322012-06-01 20:04:39 -050098
Rob Landley38d3cfb2012-06-01 20:27:51 -050099 return DIRTREE_SAVE|DIRTREE_RECURSE;
Rob Landley4fa1b322012-06-01 20:04:39 -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
110struct dirtree *handle_callback(struct dirtree *new,
111 int (*callback)(struct dirtree *node))
112{
Rob Landley6cd8ae62012-05-27 00:56:17 -0500113 int flags, dir = S_ISDIR(new->st.st_mode);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500114
Rob Landley8c4ae8a2012-05-20 15:00:19 -0500115 if (!callback) callback = dirtree_notdotdot;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500116
Rob Landley6cd8ae62012-05-27 00:56:17 -0500117 // Directory always has filehandle for examining contents. Whether or
118 // not we'll recurse into it gets decided later.
119
Rob Landley6ec21782012-06-16 15:16:08 -0500120 if (dir) new->data = openat(dirtree_parentfd(new), new->name, 0);
Rob Landley6cd8ae62012-05-27 00:56:17 -0500121
Rob Landleyeb7ea222012-04-14 22:30:41 -0500122 flags = callback(new);
Rob Landley6cd8ae62012-05-27 00:56:17 -0500123
124 if (dir) {
125 if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) {
Rob Landley4af1e1d2012-06-09 22:25:49 -0500126 dirtree_recurse(new, callback, flags & DIRTREE_SYMFOLLOW);
Rob Landley6cd8ae62012-05-27 00:56:17 -0500127 if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
128 } else close(new->data);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500129 }
Rob Landley6cd8ae62012-05-27 00:56:17 -0500130
Rob Landleyeb7ea222012-04-14 22:30:41 -0500131 // If this had children, it was callback's job to free them already.
Rob Landley8c4ae8a2012-05-20 15:00:19 -0500132 if (!(flags & DIRTREE_SAVE)) {
Rob Landleyeb7ea222012-04-14 22:30:41 -0500133 free(new);
134 new = NULL;
Rob Landley103b7e02007-10-04 02:04:10 -0500135 }
136
Rob Landleyeb7ea222012-04-14 22:30:41 -0500137 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
Rob Landley103b7e02007-10-04 02:04:10 -0500138}
139
Rob Landleyeb7ea222012-04-14 22:30:41 -0500140// Recursively read/process children of directory node (with dirfd in data),
141// filtering through callback().
Rob Landley103b7e02007-10-04 02:04:10 -0500142
Rob Landleyeb7ea222012-04-14 22:30:41 -0500143void dirtree_recurse(struct dirtree *node,
Rob Landley4af1e1d2012-06-09 22:25:49 -0500144 int (*callback)(struct dirtree *node), int symfollow)
Rob Landleyeb7ea222012-04-14 22:30:41 -0500145{
146 struct dirtree *new, **ddt = &(node->child);
147 struct dirent *entry;
148 DIR *dir;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500149
150 if (!(dir = fdopendir(node->data))) {
151 char *path = dirtree_path(node, 0);
152 perror_msg("No %s", path);
153 free(path);
154 close(node->data);
Rob Landley9b3af462012-04-22 23:01:23 -0500155
Rob Landley6cd8ae62012-05-27 00:56:17 -0500156 return;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500157 }
Rob Landley9b3af462012-04-22 23:01:23 -0500158
159 // according to the fddir() man page, the filehandle in the DIR * can still
160 // be externally used by things that don't lseek() it.
Rob Landleyeb7ea222012-04-14 22:30:41 -0500161
162 // The extra parentheses are to shut the stupid compiler up.
163 while ((entry = readdir(dir))) {
Rob Landley4af1e1d2012-06-09 22:25:49 -0500164 if (!(new = dirtree_add_node(node->data, entry->d_name, symfollow)))
165 continue;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500166 new->parent = node;
167 new = handle_callback(new, callback);
168 if (new == DIRTREE_ABORTVAL) break;
169 if (new) {
170 *ddt = new;
171 ddt = &((*ddt)->next);
172 }
173 }
174
Rob Landley6cd8ae62012-05-27 00:56:17 -0500175 // This closes filehandle as well, so note it
Rob Landleyeb7ea222012-04-14 22:30:41 -0500176 closedir(dir);
Rob Landley6cd8ae62012-05-27 00:56:17 -0500177 node->data = -1;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500178}
179
180// Create dirtree from path, using callback to filter nodes.
181// If callback == NULL allocate a tree of struct dirtree nodes and return
182// pointer to root node.
183
184struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
185{
Rob Landley4af1e1d2012-06-09 22:25:49 -0500186 struct dirtree *root = dirtree_add_node(AT_FDCWD, path, 0);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500187
Rob Landley6cd8ae62012-05-27 00:56:17 -0500188 return root ? handle_callback(root, callback) : DIRTREE_ABORTVAL;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500189}