blob: 0e2a385c233a6dfd7411b9e078e386802591b734 [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 Landleyeb7ea222012-04-14 22:30:41 -050013struct dirtree *dirtree_add_node(int dirfd, char *name)
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) {
21 if (fstatat(dirfd, name, &st, AT_SYMLINK_NOFOLLOW)) goto error;
22 if (S_ISLNK(st.st_mode)) {
23 if (0>(linklen = readlinkat(dirfd, name, buf, 4095))) goto error;
24 buf[linklen++]=0;
Rob Landley103b7e02007-10-04 02:04:10 -050025 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050026 len = strlen(name);
Rob Landley103b7e02007-10-04 02:04:10 -050027 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050028 dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen);
29 if (name) {
30 memcpy(&(dt->st), &st, sizeof(struct stat));
31 strcpy(dt->name, name);
Rob Landley103b7e02007-10-04 02:04:10 -050032
Rob Landleyeb7ea222012-04-14 22:30:41 -050033 if (linklen) {
34 dt->symlink = memcpy(len+(char *)dt, buf, linklen);
35 dt->data = --linklen;
36 }
Rob Landley7bc79712008-02-16 19:41:20 -060037 }
Rob Landley103b7e02007-10-04 02:04:10 -050038
39 return dt;
Rob Landleyeb7ea222012-04-14 22:30:41 -050040
41error:
42 perror_msg("%s",name);
43 free(dt);
44 return 0;
Rob Landley103b7e02007-10-04 02:04:10 -050045}
46
Rob Landley9b3af462012-04-22 23:01:23 -050047// Return path to this node, assembled recursively.
Rob Landley103b7e02007-10-04 02:04:10 -050048
Rob Landleyeb7ea222012-04-14 22:30:41 -050049char *dirtree_path(struct dirtree *node, int *plen)
Rob Landley103b7e02007-10-04 02:04:10 -050050{
Rob Landleyeb7ea222012-04-14 22:30:41 -050051 char *path;
52 int len;
Rob Landley103b7e02007-10-04 02:04:10 -050053
Rob Landley9b3af462012-04-22 23:01:23 -050054 if (!node || !node->name) {
55 path = xmalloc(*plen);
56 *plen = 0;
57 return path;
58 }
Rob Landleyeb7ea222012-04-14 22:30:41 -050059
Rob Landley9b3af462012-04-22 23:01:23 -050060 len = (plen ? *plen : 0)+strlen(node->name)+1;
Rob Landleyeb7ea222012-04-14 22:30:41 -050061 path = dirtree_path(node->parent, &len);
Rob Landley9b3af462012-04-22 23:01:23 -050062 if (len) path[len++]='/';
63 len = (stpcpy(path+len, node->name) - path);
64 if (plen) *plen = len;
Rob Landleyeb7ea222012-04-14 22:30:41 -050065
66 return path;
67}
68
69// Default callback, filters out "." and "..".
70
71int dirtree_isdotdot(struct dirtree *catch)
72{
73 // Should we skip "." and ".."?
74 if (catch->name[0]=='.' && (!catch->name[1] ||
75 (catch->name[1]=='.' && !catch->name[2])))
76 return DIRTREE_NOSAVE|DIRTREE_NORECURSE;
77
78 return 0;
79}
80
81// Handle callback for a node in the tree. Returns saved node(s) or NULL.
82//
83// By default, allocates a tree of struct dirtree, not following symlinks
84// If callback==NULL, or callback always returns 0, allocate tree of struct
85// dirtree and return root of tree. Otherwise call callback(node) on each hit, free
86// structures after use, and return NULL.
87//
88
89struct dirtree *handle_callback(struct dirtree *new,
90 int (*callback)(struct dirtree *node))
91{
92 int flags;
93
94 if (!callback) callback = dirtree_isdotdot;
95
96 flags = callback(new);
97 if (S_ISDIR(new->st.st_mode)) {
98 if (!(flags & DIRTREE_NORECURSE)) {
Rob Landley9b3af462012-04-22 23:01:23 -050099 new->data = openat (new->parent ? new->parent->data : AT_FDCWD,
100 new->name, 0);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500101 dirtree_recurse(new, callback);
Rob Landley603a93d2008-03-24 05:34:58 -0500102 }
Rob Landleyeb7ea222012-04-14 22:30:41 -0500103 new->data = -1;
104 if (flags & DIRTREE_COMEAGAIN) flags = callback(new);
105 }
106 // If this had children, it was callback's job to free them already.
107 if (flags & DIRTREE_NOSAVE) {
108 free(new);
109 new = NULL;
Rob Landley103b7e02007-10-04 02:04:10 -0500110 }
111
Rob Landleyeb7ea222012-04-14 22:30:41 -0500112 return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new;
Rob Landley103b7e02007-10-04 02:04:10 -0500113}
114
Rob Landleyeb7ea222012-04-14 22:30:41 -0500115// Recursively read/process children of directory node (with dirfd in data),
116// filtering through callback().
Rob Landley103b7e02007-10-04 02:04:10 -0500117
Rob Landleyeb7ea222012-04-14 22:30:41 -0500118void dirtree_recurse(struct dirtree *node,
119 int (*callback)(struct dirtree *node))
120{
121 struct dirtree *new, **ddt = &(node->child);
122 struct dirent *entry;
123 DIR *dir;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500124
125 if (!(dir = fdopendir(node->data))) {
126 char *path = dirtree_path(node, 0);
127 perror_msg("No %s", path);
128 free(path);
129 close(node->data);
Rob Landley9b3af462012-04-22 23:01:23 -0500130
131 return;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500132 }
Rob Landley9b3af462012-04-22 23:01:23 -0500133
134 // according to the fddir() man page, the filehandle in the DIR * can still
135 // be externally used by things that don't lseek() it.
Rob Landleyeb7ea222012-04-14 22:30:41 -0500136
137 // The extra parentheses are to shut the stupid compiler up.
138 while ((entry = readdir(dir))) {
Rob Landley9b3af462012-04-22 23:01:23 -0500139 if (!(new = dirtree_add_node(node->data, entry->d_name))) continue;
Rob Landleyeb7ea222012-04-14 22:30:41 -0500140 new->parent = node;
141 new = handle_callback(new, callback);
142 if (new == DIRTREE_ABORTVAL) break;
143 if (new) {
144 *ddt = new;
145 ddt = &((*ddt)->next);
146 }
147 }
148
149 closedir(dir);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500150}
151
152// Create dirtree from path, using callback to filter nodes.
153// If callback == NULL allocate a tree of struct dirtree nodes and return
154// pointer to root node.
155
156struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node))
157{
Rob Landley9b3af462012-04-22 23:01:23 -0500158 struct dirtree *root = dirtree_add_node(AT_FDCWD, path);
Rob Landleyeb7ea222012-04-14 22:30:41 -0500159
160 return handle_callback(root, callback);
161}