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 | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 27 | struct dirtree *dirtree_add_node(struct dirtree *parent, char *name, |
| 28 | int symfollow) |
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; |
| 32 | char buf[4096]; |
| 33 | int len = 0, linklen = 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 34 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 35 | if (name) { |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 36 | // open code this because haven't got node to call dirtree_parentfd() on yet |
| 37 | int fd = parent ? parent->data : AT_FDCWD; |
| 38 | |
| 39 | if (fstatat(fd, name, &st, symfollow ? 0 : AT_SYMLINK_NOFOLLOW)) goto error; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 40 | if (S_ISLNK(st.st_mode)) { |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 41 | if (0>(linklen = readlinkat(fd, name, buf, 4095))) goto error; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 42 | buf[linklen++]=0; |
| 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 | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 52 | if (linklen) { |
| 53 | dt->symlink = memcpy(len+(char *)dt, buf, linklen); |
| 54 | dt->data = --linklen; |
| 55 | } |
| 56 | } |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 57 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 58 | return dt; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 59 | |
| 60 | error: |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 61 | if (notdotdot(name)) { |
| 62 | char *path = parent ? dirtree_path(parent, 0) : ""; |
| 63 | perror_msg("%s%s%s",path, parent ? "/" : "", name); |
| 64 | } |
Rob Landley | 37de8ed | 2012-12-08 02:26:56 -0600 | [diff] [blame] | 65 | if (parent) parent->symlink = (char *)1; |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 66 | free(dt); |
| 67 | return 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 70 | // Return path to this node, assembled recursively. |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 71 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 72 | char *dirtree_path(struct dirtree *node, int *plen) |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 73 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 74 | char *path; |
| 75 | int len; |
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 | if (!node || !node->name) { |
| 78 | path = xmalloc(*plen); |
| 79 | *plen = 0; |
| 80 | return path; |
| 81 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 82 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 83 | len = (plen ? *plen : 0)+strlen(node->name)+1; |
| 84 | path = dirtree_path(node->parent, &len); |
| 85 | if (len && path[len-1] != '/') path[len++]='/'; |
| 86 | len = (stpcpy(path+len, node->name) - path); |
| 87 | if (plen) *plen = len; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 88 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 89 | return path; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 92 | int dirtree_parentfd(struct dirtree *node) |
| 93 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 94 | return node->parent ? node->parent->data : AT_FDCWD; |
Rob Landley | 6ec2178 | 2012-06-16 15:16:08 -0500 | [diff] [blame] | 95 | } |
| 96 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 97 | // Handle callback for a node in the tree. Returns saved node(s) or NULL. |
| 98 | // |
| 99 | // By default, allocates a tree of struct dirtree, not following symlinks |
| 100 | // If callback==NULL, or callback always returns 0, allocate tree of struct |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 101 | // dirtree and return root of tree. Otherwise call callback(node) on each |
| 102 | // hit, free structures after use, and return NULL. |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 103 | // |
| 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 | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 108 | int flags, dir = S_ISDIR(new->st.st_mode); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 109 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 110 | if (!callback) callback = dirtree_notdotdot; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 111 | |
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 | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 114 | if (dir) { |
| 115 | if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) { |
| 116 | new->data = openat(dirtree_parentfd(new), new->name, 0); |
| 117 | dirtree_recurse(new, callback, flags & DIRTREE_SYMFOLLOW); |
| 118 | if (flags & DIRTREE_COMEAGAIN) flags = callback(new); |
| 119 | } |
| 120 | } |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame] | 121 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 122 | // If this had children, it was callback's job to free them already. |
| 123 | if (!(flags & DIRTREE_SAVE)) { |
| 124 | free(new); |
| 125 | new = NULL; |
| 126 | } |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 127 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 128 | return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 131 | // Recursively read/process children of directory node (with dirfd in data), |
| 132 | // filtering through callback(). |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 133 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 134 | void dirtree_recurse(struct dirtree *node, |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 135 | int (*callback)(struct dirtree *node), int symfollow) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 136 | { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 137 | struct dirtree *new, **ddt = &(node->child); |
| 138 | struct dirent *entry; |
| 139 | DIR *dir; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 140 | |
Isaac Dunham | c810f9f | 2013-07-06 11:26:15 -0500 | [diff] [blame^] | 141 | if (node->data == -1 || !(dir = fdopendir(node->data))) { |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 142 | char *path = dirtree_path(node, 0); |
| 143 | perror_msg("No %s", path); |
| 144 | free(path); |
| 145 | close(node->data); |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 146 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 147 | return; |
| 148 | } |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 149 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 150 | // according to the fddir() man page, the filehandle in the DIR * can still |
| 151 | // be externally used by things that don't lseek() it. |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 152 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 153 | // The extra parentheses are to shut the stupid compiler up. |
| 154 | while ((entry = readdir(dir))) { |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 155 | if (!(new = dirtree_add_node(node, entry->d_name, symfollow))) |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 156 | continue; |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 157 | new = dirtree_handle_callback(new, callback); |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 158 | if (new == DIRTREE_ABORTVAL) break; |
| 159 | if (new) { |
| 160 | *ddt = new; |
| 161 | ddt = &((*ddt)->next); |
| 162 | } |
| 163 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 164 | |
Rob Landley | 7aa651a | 2012-11-13 17:14:08 -0600 | [diff] [blame] | 165 | // This closes filehandle as well, so note it |
| 166 | closedir(dir); |
| 167 | node->data = -1; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Create dirtree from path, using callback to filter nodes. |
| 171 | // If callback == NULL allocate a tree of struct dirtree nodes and return |
| 172 | // pointer to root node. |
Rob Landley | 78aaef2 | 2012-06-24 18:35:49 -0500 | [diff] [blame] | 173 | // symfollow is just for the top of tree, callback return code controls children |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 174 | |
| 175 | struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)) |
| 176 | { |
Rob Landley | 3162c27 | 2012-12-06 15:13:30 -0600 | [diff] [blame] | 177 | struct dirtree *root = dirtree_add_node(0, path, 0); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 178 | |
Rob Landley | 090c5c6 | 2012-12-31 14:38:13 -0600 | [diff] [blame] | 179 | return root ? dirtree_handle_callback(root, callback) : DIRTREE_ABORTVAL; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 180 | } |