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 | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 13 | struct dirtree *dirtree_add_node(int dirfd, char *name) |
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) { |
| 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 Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 25 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 26 | len = strlen(name); |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 27 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 28 | 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 Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 32 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 33 | if (linklen) { |
| 34 | dt->symlink = memcpy(len+(char *)dt, buf, linklen); |
| 35 | dt->data = --linklen; |
| 36 | } |
Rob Landley | 7bc7971 | 2008-02-16 19:41:20 -0600 | [diff] [blame] | 37 | } |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 38 | |
| 39 | return dt; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 40 | |
| 41 | error: |
| 42 | perror_msg("%s",name); |
| 43 | free(dt); |
| 44 | return 0; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 47 | // Return path to this node, assembled recursively. |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 48 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 49 | char *dirtree_path(struct dirtree *node, int *plen) |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 50 | { |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 51 | char *path; |
| 52 | int len; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 53 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 54 | if (!node || !node->name) { |
| 55 | path = xmalloc(*plen); |
| 56 | *plen = 0; |
| 57 | return path; |
| 58 | } |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 59 | |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 60 | len = (plen ? *plen : 0)+strlen(node->name)+1; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 61 | path = dirtree_path(node->parent, &len); |
Rob Landley | 4d0f02f | 2012-05-09 06:11:23 -0500 | [diff] [blame] | 62 | if (len) path[len++]='/'; |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 63 | len = (stpcpy(path+len, node->name) - path); |
Rob Landley | 4d0f02f | 2012-05-09 06:11:23 -0500 | [diff] [blame] | 64 | if (plen) *plen = len; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 65 | |
| 66 | return path; |
| 67 | } |
| 68 | |
| 69 | // Default callback, filters out "." and "..". |
| 70 | |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 71 | int dirtree_notdotdot(struct dirtree *catch) |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 72 | { |
| 73 | // Should we skip "." and ".."? |
| 74 | if (catch->name[0]=='.' && (!catch->name[1] || |
| 75 | (catch->name[1]=='.' && !catch->name[2]))) |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 76 | return 0; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 77 | |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 78 | return DIRTREE_SAVE|DIRTREE_RECURSE; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 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 |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 85 | // dirtree and return root of tree. Otherwise call callback(node) on each |
| 86 | // hit, free structures after use, and return NULL. |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 87 | // |
| 88 | |
| 89 | struct dirtree *handle_callback(struct dirtree *new, |
| 90 | int (*callback)(struct dirtree *node)) |
| 91 | { |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 92 | int flags, dir = S_ISDIR(new->st.st_mode); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 93 | |
Rob Landley | 8c4ae8a | 2012-05-20 15:00:19 -0500 | [diff] [blame] | 94 | if (!callback) callback = dirtree_notdotdot; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 95 | |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 96 | // Directory always has filehandle for examining contents. Whether or |
| 97 | // not we'll recurse into it gets decided later. |
| 98 | |
| 99 | if (dir) |
| 100 | new->data = openat(new->parent ? new->parent->data : AT_FDCWD, |
| 101 | new->name, 0); |
| 102 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 103 | flags = callback(new); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 104 | |
| 105 | if (dir) { |
| 106 | if (flags & (DIRTREE_RECURSE|DIRTREE_COMEAGAIN)) { |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 107 | dirtree_recurse(new, callback); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 108 | if (flags & DIRTREE_COMEAGAIN) flags = callback(new); |
| 109 | } else close(new->data); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 110 | } |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 111 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 112 | // 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] | 113 | if (!(flags & DIRTREE_SAVE)) { |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 114 | free(new); |
| 115 | new = NULL; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 118 | return (flags & DIRTREE_ABORT)==DIRTREE_ABORT ? DIRTREE_ABORTVAL : new; |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 119 | } |
| 120 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 121 | // Recursively read/process children of directory node (with dirfd in data), |
| 122 | // filtering through callback(). |
Rob Landley | 103b7e0 | 2007-10-04 02:04:10 -0500 | [diff] [blame] | 123 | |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 124 | void dirtree_recurse(struct dirtree *node, |
| 125 | int (*callback)(struct dirtree *node)) |
| 126 | { |
| 127 | struct dirtree *new, **ddt = &(node->child); |
| 128 | struct dirent *entry; |
| 129 | DIR *dir; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 130 | |
| 131 | if (!(dir = fdopendir(node->data))) { |
| 132 | char *path = dirtree_path(node, 0); |
| 133 | perror_msg("No %s", path); |
| 134 | free(path); |
| 135 | close(node->data); |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 136 | |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 137 | return; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 138 | } |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 139 | |
| 140 | // according to the fddir() man page, the filehandle in the DIR * can still |
| 141 | // be externally used by things that don't lseek() it. |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 142 | |
| 143 | // The extra parentheses are to shut the stupid compiler up. |
| 144 | while ((entry = readdir(dir))) { |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 145 | if (!(new = dirtree_add_node(node->data, entry->d_name))) continue; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 146 | new->parent = node; |
| 147 | new = handle_callback(new, callback); |
| 148 | if (new == DIRTREE_ABORTVAL) break; |
| 149 | if (new) { |
| 150 | *ddt = new; |
| 151 | ddt = &((*ddt)->next); |
| 152 | } |
| 153 | } |
| 154 | |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 155 | // This closes filehandle as well, so note it |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 156 | closedir(dir); |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 157 | node->data = -1; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // Create dirtree from path, using callback to filter nodes. |
| 161 | // If callback == NULL allocate a tree of struct dirtree nodes and return |
| 162 | // pointer to root node. |
| 163 | |
| 164 | struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)) |
| 165 | { |
Rob Landley | 9b3af46 | 2012-04-22 23:01:23 -0500 | [diff] [blame] | 166 | struct dirtree *root = dirtree_add_node(AT_FDCWD, path); |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 167 | |
Rob Landley | 6cd8ae6 | 2012-05-27 00:56:17 -0500 | [diff] [blame^] | 168 | return root ? handle_callback(root, callback) : DIRTREE_ABORTVAL; |
Rob Landley | eb7ea22 | 2012-04-14 22:30:41 -0500 | [diff] [blame] | 169 | } |