Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 1 | /* 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 Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 8 | static 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 | |
| 17 | int dirtree_notdotdot(struct dirtree *catch) |
| 18 | { |
| 19 | // Should we skip "." and ".."? |
| 20 | return notdotdot(catch->name) ? DIRTREE_SAVE|DIRTREE_RECURSE : 0; |
| 21 | } |
| 22 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 23 | // Create a dirtree node from a path, with stat and symlink info. |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 24 | // (This doesn't open directory filehandles yet so as not to exhaust the |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 25 | // filehandle space on large trees, dirtree_handle_callback() does that.) |
Rob Landley | 7bc7971 | 2008-02-16 19:41:20 -0600 | [diff] [blame] | 26 | |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 27 | struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags) |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 28 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 29 | struct dirtree *dt = NULL; |
| 30 | struct stat st; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 31 | int len = 0, linklen = 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 32 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 33 | if (name) { |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 34 | // open code this because haven't got node to call dirtree_parentfd() on yet |
| 35 | int fd = parent ? parent->data : AT_FDCWD; |
| 36 | |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 37 | if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW))) |
| 38 | goto error; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 39 | if (S_ISLNK(st.st_mode)) { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 40 | if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error; |
| 41 | libbuf[linklen++]=0; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 42 | } |
| 43 | len = strlen(name); |
| 44 | } |
| 45 | dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen); |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 46 | dt->parent = parent; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 47 | if (name) { |
| 48 | memcpy(&(dt->st), &st, sizeof(struct stat)); |
| 49 | strcpy(dt->name, name); |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 50 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 51 | if (linklen) { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 52 | dt->symlink = memcpy(len+(char *)dt, libbuf, linklen); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 53 | dt->data = --linklen; |
| 54 | } |
| 55 | } |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 56 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 57 | return dt; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 58 | |
| 59 | error: |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 60 | if (!(flags&DIRTREE_SHUTUP) && notdotdot(name)) { |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 61 | char *path = parent ? dirtree_path(parent, 0) : ""; |
Rob Landley | 280bb59 | 2014-07-21 19:55:06 -0500 | [diff] [blame] | 62 | |
| 63 | perror_msg("%s%s%s", path, parent ? "/" : "", name); |
| 64 | if (parent) free(path); |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 65 | } |
Rob Landley | 37de8ed | 2012-12-08 02:26:56 -0600 | [diff] [blame] | 66 | if (parent) parent->symlink = (char *)1; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 67 | free(dt); |
| 68 | return 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 71 | // Return path to this node, assembled recursively. |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 72 | |
Rob Landley | 29d30be | 2014-09-22 07:52:15 -0500 | [diff] [blame] | 73 | // 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 77 | char *dirtree_path(struct dirtree *node, int *plen) |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 78 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 79 | char *path; |
| 80 | int len; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 81 | |
Rob Landley | 29d30be | 2014-09-22 07:52:15 -0500 | [diff] [blame] | 82 | if (!node) { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 83 | path = xmalloc(*plen); |
| 84 | *plen = 0; |
| 85 | return path; |
| 86 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 87 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 88 | 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 93 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 94 | return path; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 97 | int dirtree_parentfd(struct dirtree *node) |
| 98 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 99 | return node->parent ? node->parent->data : AT_FDCWD; |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 102 | // 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 Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 106 | // dirtree and return root of tree. Otherwise call callback(node) on each |
| 107 | // hit, free structures after use, and return NULL. |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 108 | // |
| 109 | |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 110 | struct dirtree *dirtree_handle_callback(struct dirtree *new, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 111 | int (*callback)(struct dirtree *node)) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 112 | { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 113 | int flags; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 114 | |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 115 | if (!new) return 0; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 116 | if (!callback) callback = dirtree_notdotdot; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 117 | flags = callback(new); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 118 | |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 119 | if (S_ISDIR(new->st.st_mode)) { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 120 | if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) { |
Rob Landley | ccb73f8 | 2014-07-26 13:27:07 -0500 | [diff] [blame] | 121 | new->data = openat(dirtree_parentfd(new), new->name, O_CLOEXEC); |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 122 | flags = dirtree_recurse(new, callback, flags); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 123 | } |
| 124 | } |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 125 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 126 | // 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 Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 131 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 132 | return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 135 | // Recursively read/process children of directory node (with dirfd in data), |
| 136 | // filtering through callback(). |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 137 | |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 138 | int dirtree_recurse(struct dirtree *node, |
| 139 | int (*callback)(struct dirtree *node), int flags) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 140 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 141 | struct dirtree *new, **ddt = &(node->child); |
| 142 | struct dirent *entry; |
| 143 | DIR *dir; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 144 | |
Isaac Dunham | c810f9f | 2013-07-06 11:26:15 -0500 | [diff] [blame] | 145 | if (node->data == -1 || !(dir = fdopendir(node->data))) { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 146 | if (!(flags & DIRTREE_SHUTUP)) { |
| 147 | char *path = dirtree_path(node, 0); |
| 148 | perror_msg("No %s", path); |
| 149 | free(path); |
| 150 | } |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 151 | close(node->data); |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 152 | |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 153 | return flags; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 154 | } |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 155 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 156 | // 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 158 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 159 | // The extra parentheses are to shut the stupid compiler up. |
| 160 | while ((entry = readdir(dir))) { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 161 | if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue; |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 162 | new = dirtree_handle_callback(new, callback); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 163 | if (new == DIRTREE_ABORTVAL) break; |
| 164 | if (new) { |
| 165 | *ddt = new; |
| 166 | ddt = &((*ddt)->next); |
| 167 | } |
| 168 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 169 | |
Rob Landley | 749c523 | 2014-07-29 20:02:31 -0500 | [diff] [blame] | 170 | if (flags & DIRTREE_COMEAGAIN) { |
| 171 | node->again++; |
| 172 | flags = callback(node); |
| 173 | } |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 174 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 175 | // This closes filehandle as well, so note it |
| 176 | closedir(dir); |
| 177 | node->data = -1; |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 178 | |
| 179 | return flags; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 180 | } |
| 181 | |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 182 | // Create dirtree root |
| 183 | struct dirtree *dirtree_start(char *name, int symfollow) |
| 184 | { |
| 185 | return dirtree_add_node(0, name, DIRTREE_SYMFOLLOW*!!symfollow); |
| 186 | } |
| 187 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 188 | // 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 | |
| 192 | struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)) |
| 193 | { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame^] | 194 | struct dirtree *root = dirtree_start(path, 0); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 195 | |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 196 | return root ? dirtree_handle_callback(root, callback) : DIRTREE_ABORTVAL; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 197 | } |