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 | 382057f | 2016-11-21 16:47:23 -0600 | [diff] [blame] | 8 | int isdotdot(char *name) |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 9 | { |
Rob Landley | 382057f | 2016-11-21 16:47:23 -0600 | [diff] [blame] | 10 | if (name[0]=='.' && (!name[1] || (name[1]=='.' && !name[2]))) return 1; |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 11 | |
Rob Landley | 382057f | 2016-11-21 16:47:23 -0600 | [diff] [blame] | 12 | return 0; |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 13 | } |
| 14 | |
Rob Landley | 382057f | 2016-11-21 16:47:23 -0600 | [diff] [blame] | 15 | // Default callback, filters out "." and ".." except at top level. |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 16 | |
| 17 | int dirtree_notdotdot(struct dirtree *catch) |
| 18 | { |
| 19 | // Should we skip "." and ".."? |
Rob Landley | 382057f | 2016-11-21 16:47:23 -0600 | [diff] [blame] | 20 | return (!catch->parent||!isdotdot(catch->name)) |
| 21 | *(DIRTREE_SAVE|DIRTREE_RECURSE); |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 22 | } |
| 23 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 24 | // Create a dirtree node from a path, with stat and symlink info. |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 25 | // (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] | 26 | // filehandle space on large trees, dirtree_handle_callback() does that.) |
Rob Landley | 7bc7971 | 2008-02-16 19:41:20 -0600 | [diff] [blame] | 27 | |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame] | 28 | struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, int flags) |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 29 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 30 | struct dirtree *dt = NULL; |
| 31 | struct stat st; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 32 | int len = 0, linklen = 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 33 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 34 | if (name) { |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 35 | // open code this because haven't got node to call dirtree_parentfd() on yet |
Rob Landley | d336af4 | 2015-12-06 20:36:59 -0600 | [diff] [blame] | 36 | int fd = parent ? parent->dirfd : AT_FDCWD; |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 37 | |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame] | 38 | if (fstatat(fd, name, &st, AT_SYMLINK_NOFOLLOW*!(flags&DIRTREE_SYMFOLLOW))) |
| 39 | goto error; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 40 | if (S_ISLNK(st.st_mode)) { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame] | 41 | if (0>(linklen = readlinkat(fd, name, libbuf, 4095))) goto error; |
| 42 | libbuf[linklen++]=0; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 43 | } |
| 44 | len = strlen(name); |
| 45 | } |
| 46 | dt = xzalloc((len = sizeof(struct dirtree)+len+1)+linklen); |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 47 | dt->parent = parent; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 48 | if (name) { |
| 49 | memcpy(&(dt->st), &st, sizeof(struct stat)); |
| 50 | strcpy(dt->name, name); |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 51 | |
Rob Landley | d336af4 | 2015-12-06 20:36:59 -0600 | [diff] [blame] | 52 | if (linklen) dt->symlink = memcpy(len+(char *)dt, libbuf, linklen); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 53 | } |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 54 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 55 | return dt; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 56 | |
| 57 | error: |
Rob Landley | 382057f | 2016-11-21 16:47:23 -0600 | [diff] [blame] | 58 | if (!(flags&DIRTREE_SHUTUP) && !isdotdot(name)) { |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 59 | char *path = parent ? dirtree_path(parent, 0) : ""; |
Rob Landley | 280bb59 | 2014-07-21 19:55:06 -0500 | [diff] [blame] | 60 | |
| 61 | perror_msg("%s%s%s", path, parent ? "/" : "", name); |
| 62 | if (parent) free(path); |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 63 | } |
Rob Landley | 37de8ed | 2012-12-08 02:26:56 -0600 | [diff] [blame] | 64 | if (parent) parent->symlink = (char *)1; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 65 | free(dt); |
| 66 | return 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 69 | // Return path to this node, assembled recursively. |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 70 | |
Rob Landley | 29d30be | 2014-09-22 07:52:15 -0500 | [diff] [blame] | 71 | // 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 75 | char *dirtree_path(struct dirtree *node, int *plen) |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 76 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 77 | char *path; |
| 78 | int len; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 79 | |
Rob Landley | 29d30be | 2014-09-22 07:52:15 -0500 | [diff] [blame] | 80 | if (!node) { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 81 | path = xmalloc(*plen); |
| 82 | *plen = 0; |
| 83 | return path; |
| 84 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 85 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 86 | 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 91 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 92 | return path; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 95 | int dirtree_parentfd(struct dirtree *node) |
| 96 | { |
Rob Landley | d336af4 | 2015-12-06 20:36:59 -0600 | [diff] [blame] | 97 | return node->parent ? node->parent->dirfd : AT_FDCWD; |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Rob Landley | 8d95074 | 2016-03-07 16:02:47 -0600 | [diff] [blame] | 100 | // 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 104 | |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 105 | struct dirtree *dirtree_handle_callback(struct dirtree *new, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 106 | int (*callback)(struct dirtree *node)) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 107 | { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame] | 108 | int flags; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 109 | |
Rob Landley | 8d95074 | 2016-03-07 16:02:47 -0600 | [diff] [blame] | 110 | if (!new) return DIRTREE_ABORTVAL; |
| 111 | if (!callback) return new; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 112 | flags = callback(new); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 113 | |
Rob Landley | bb77dde | 2016-04-21 17:46:25 -0500 | [diff] [blame] | 114 | 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 Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 117 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 118 | // 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 Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 123 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 124 | return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Rob Landley | d336af4 | 2015-12-06 20:36:59 -0600 | [diff] [blame] | 127 | // Recursively read/process children of directory node, filtering through |
| 128 | // callback(). Uses and closes supplied ->dirfd. |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 129 | |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 130 | int dirtree_recurse(struct dirtree *node, |
Rob Landley | bb77dde | 2016-04-21 17:46:25 -0500 | [diff] [blame] | 131 | int (*callback)(struct dirtree *node), int dirfd, int flags) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 132 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 133 | struct dirtree *new, **ddt = &(node->child); |
| 134 | struct dirent *entry; |
| 135 | DIR *dir; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 136 | |
Rob Landley | bb77dde | 2016-04-21 17:46:25 -0500 | [diff] [blame] | 137 | node->dirfd = dirfd; |
Rob Landley | d336af4 | 2015-12-06 20:36:59 -0600 | [diff] [blame] | 138 | if (node->dirfd == -1 || !(dir = fdopendir(node->dirfd))) { |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame] | 139 | if (!(flags & DIRTREE_SHUTUP)) { |
| 140 | char *path = dirtree_path(node, 0); |
| 141 | perror_msg("No %s", path); |
| 142 | free(path); |
| 143 | } |
Rob Landley | d336af4 | 2015-12-06 20:36:59 -0600 | [diff] [blame] | 144 | close(node->dirfd); |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 145 | |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 146 | return flags; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 147 | } |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 148 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 149 | // 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 151 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 152 | // The extra parentheses are to shut the stupid compiler up. |
| 153 | while ((entry = readdir(dir))) { |
Rob Landley | a975952 | 2016-12-08 21:29:00 -0600 | [diff] [blame] | 154 | if ((flags&DIRTREE_PROC) && !isdigit(*entry->d_name)) continue; |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame] | 155 | if (!(new = dirtree_add_node(node, entry->d_name, flags))) continue; |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 156 | new = dirtree_handle_callback(new, callback); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 157 | if (new == DIRTREE_ABORTVAL) break; |
| 158 | if (new) { |
| 159 | *ddt = new; |
| 160 | ddt = &((*ddt)->next); |
| 161 | } |
| 162 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 163 | |
Rob Landley | 749c523 | 2014-07-29 20:02:31 -0500 | [diff] [blame] | 164 | if (flags & DIRTREE_COMEAGAIN) { |
| 165 | node->again++; |
| 166 | flags = callback(node); |
| 167 | } |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 168 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 169 | // This closes filehandle as well, so note it |
| 170 | closedir(dir); |
Rob Landley | d336af4 | 2015-12-06 20:36:59 -0600 | [diff] [blame] | 171 | node->dirfd = -1; |
Rob Landley | fec3fd1 | 2014-07-26 13:30:40 -0500 | [diff] [blame] | 172 | |
| 173 | return flags; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 174 | } |
| 175 | |
Rob Landley | 8d95074 | 2016-03-07 16:02:47 -0600 | [diff] [blame] | 176 | // 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 | |
| 183 | struct dirtree *dirtree_flagread(char *path, int flags, |
| 184 | int (*callback)(struct dirtree *node)) |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame] | 185 | { |
Rob Landley | 8d95074 | 2016-03-07 16:02:47 -0600 | [diff] [blame] | 186 | return dirtree_handle_callback(dirtree_add_node(0, path, flags), callback); |
Rob Landley | aab9164 | 2015-05-09 18:11:22 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Rob Landley | 8d95074 | 2016-03-07 16:02:47 -0600 | [diff] [blame] | 189 | // Common case |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 190 | struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)) |
| 191 | { |
Rob Landley | 8d95074 | 2016-03-07 16:02:47 -0600 | [diff] [blame] | 192 | return dirtree_flagread(path, 0, callback); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 193 | } |