Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 1 | /* 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 9 | // Create a dirtree node from a path, with stat and symlink info. |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 10 | // (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 Landley | 7bc7971 | 2008-02-16 19:41:20 -0600 | [diff] [blame] | 12 | |
Rob Landley | 4af1e1d | 2012-06-09 22:25:49 -0500 | [diff] [blame] | 13 | struct dirtree *dirtree_add_node(int dirfd, char *name, int symfollow) |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 14 | { |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 15 | struct dirtree *dt = NULL; |
| 16 | struct stat st; |
| 17 | char buf[4096]; |
| 18 | int len = 0, linklen = 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 19 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 20 | if (name) { |
Rob Landley | 4af1e1d | 2012-06-09 22:25:49 -0500 | [diff] [blame] | 21 | if (fstatat(dirfd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW)) |
| 22 | goto error; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 23 | if (S_ISLNK(st.st_mode)) { |
| 24 | if (0>(linklen = readlinkat(dirfd, name, buf, 4095))) goto error; |
| 25 | buf[linklen++]=0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 26 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 27 | len = strlen(name); |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 28 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 29 | 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 Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 33 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 34 | if (linklen) { |
| 35 | dt->symlink = memcpy(len+(char *)dt, buf, linklen); |
| 36 | dt->data = --linklen; |
| 37 | } |
Rob Landley | 7bc7971 | 2008-02-16 19:41:20 -0600 | [diff] [blame] | 38 | } |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 39 | |
| 40 | return dt; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 41 | |
| 42 | error: |
| 43 | perror_msg("%s",name); |
| 44 | free(dt); |
| 45 | return 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 46 | } |
| 47 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 48 | // Return path to this node, assembled recursively. |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 49 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 50 | char *dirtree_path(struct dirtree *node, int *plen) |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 51 | { |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 52 | char *path; |
| 53 | int len; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 54 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 55 | if (!node || !node->name) { |
| 56 | path = xmalloc(*plen); |
| 57 | *plen = 0; |
| 58 | return path; |
| 59 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 60 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 61 | len = (plen ? *plen : 0)+strlen(node->name)+1; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 62 | path = dirtree_path(node->parent, &len); |
Rob Landley | 4d0f02f | 2012-05-09 06:11:23 -0500 | [diff] [blame] | 63 | if (len) path[len++]='/'; |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 64 | len = (stpcpy(path+len, node->name) - path); |
Rob Landley | 4d0f02f | 2012-05-09 06:11:23 -0500 | [diff] [blame] | 65 | if (plen) *plen = len; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 66 | |
| 67 | return path; |
| 68 | } |
| 69 | |
| 70 | // Default callback, filters out "." and "..". |
| 71 | |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 72 | int dirtree_notdotdot(struct dirtree *catch) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 73 | { |
| 74 | // Should we skip "." and ".."? |
| 75 | if (catch->name[0]=='.' && (!catch->name[1] || |
| 76 | (catch->name[1]=='.' && !catch->name[2]))) |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 77 | return 0; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 78 | |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 79 | return DIRTREE_SAVE|DIRTREE_RECURSE; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 80 | } |
| 81 | |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 82 | int dirtree_parentfd(struct dirtree *node) |
| 83 | { |
| 84 | return node->parent ? node->parent->data : AT_FDCWD; |
| 85 | } |
| 86 | |
Rob Landley | 38d3cfb | 2012-06-01 20:27:51 -0500 | [diff] [blame] | 87 | // get open filehandle for node in extra, giving caller the option of |
| 88 | // using DIRTREE_COMEAGAIN or not. |
| 89 | int dirtree_opennode(struct dirtree *try) |
Rob Landley | 4fa1b32 | 2012-06-01 20:04:39 -0500 | [diff] [blame] | 90 | { |
Rob Landley | 38d3cfb | 2012-06-01 20:27:51 -0500 | [diff] [blame] | 91 | 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 Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 97 | } else try->extra = openat(dirtree_parentfd(try), try->name, 0); |
Rob Landley | 4fa1b32 | 2012-06-01 20:04:39 -0500 | [diff] [blame] | 98 | |
Rob Landley | 38d3cfb | 2012-06-01 20:27:51 -0500 | [diff] [blame] | 99 | return DIRTREE_SAVE|DIRTREE_RECURSE; |
Rob Landley | 4fa1b32 | 2012-06-01 20:04:39 -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 | |
| 110 | struct dirtree *handle_callback(struct dirtree *new, |
| 111 | int (*callback)(struct dirtree *node)) |
| 112 | { |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 113 | int flags, dir = S_ISDIR(new->st.st_mode); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 114 | |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 115 | if (!callback) callback = dirtree_notdotdot; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 116 | |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 117 | // Directory always has filehandle for examining contents. Whether or |
| 118 | // not we'll recurse into it gets decided later. |
| 119 | |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 120 | if (dir) new->data = openat(dirtree_parentfd(new), new->name, 0); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 121 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 122 | flags = callback(new); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 123 | |
| 124 | if (dir) { |
| 125 | if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) { |
Rob Landley | 4af1e1d | 2012-06-09 22:25:49 -0500 | [diff] [blame] | 126 | dirtree_recurse(new, callback, flags & DIRTREE_SYMFOLLOW); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 127 | if (flags & DIRTREE_COMEAGAIN) flags = callback(new); |
| 128 | } else close(new->data); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 129 | } |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 130 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 131 | // If this had children, it was callback's job to free them already. |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 132 | if (!(flags & DIRTREE_SAVE)) { |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 133 | free(new); |
| 134 | new = NULL; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 135 | } |
| 136 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 137 | return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 140 | // Recursively read/process children of directory node (with dirfd in data), |
| 141 | // filtering through callback(). |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 142 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 143 | void dirtree_recurse(struct dirtree *node, |
Rob Landley | 4af1e1d | 2012-06-09 22:25:49 -0500 | [diff] [blame] | 144 | int (*callback)(struct dirtree *node), int symfollow) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 145 | { |
| 146 | struct dirtree *new, **ddt = &(node->child); |
| 147 | struct dirent *entry; |
| 148 | DIR *dir; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 149 | |
| 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 Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 155 | |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 156 | return; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 157 | } |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 158 | |
| 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 Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 161 | |
| 162 | // The extra parentheses are to shut the stupid compiler up. |
| 163 | while ((entry = readdir(dir))) { |
Rob Landley | 4af1e1d | 2012-06-09 22:25:49 -0500 | [diff] [blame] | 164 | if (!(new = dirtree_add_node(node->data, entry->d_name, symfollow))) |
| 165 | continue; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 166 | 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 Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 175 | // This closes filehandle as well, so note it |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 176 | closedir(dir); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 177 | node->data = -1; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 178 | } |
| 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 | |
| 184 | struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)) |
| 185 | { |
Rob Landley | 4af1e1d | 2012-06-09 22:25:49 -0500 | [diff] [blame] | 186 | struct dirtree *root = dirtree_add_node(AT_FDCWD, path, 0); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 187 | |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 188 | return root ? handle_callback(root, callback) : DIRTREE_ABORTVAL; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 189 | } |