diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 40 |
1 files changed, 36 insertions, 4 deletions
@@ -242,14 +242,46 @@ static struct tree_node* make_dir_tree(const struct options *opts, struct string return result; } -static struct tree_node* make_dir_tree_root(const struct options *opts, const char *rootpath) { - int fd = open(rootpath, O_DIRECTORY); +static struct tree_node* make_dir_tree_root(const struct options *opts, const char *rootpath, const char *treeroot) { + // If the first character is a /, keep it + size_t rootpath_len = strlen(rootpath); + while (rootpath_len > 1 && rootpath[rootpath_len - 1] == '/') rootpath_len--; + + // But not in the tree root + size_t treeroot_len = treeroot != NULL ? strlen(treeroot) : 0; + while (treeroot_len > 0 && treeroot[treeroot_len - 1] == '/') treeroot_len--; + + if (treeroot_len > 0 && treeroot[0] == '/') { + fprintf(stderr, "Tree root is not a relative path\n"); + exit(1); + } + + char *pathbuf = malloc(rootpath_len + 1 + treeroot_len + 1); + size_t pathbuf_len = rootpath_len; + strcpy(pathbuf, rootpath); + if (treeroot) { + pathbuf[pathbuf_len++] = '/'; + strcpy(pathbuf + pathbuf_len, treeroot); + pathbuf_len += treeroot_len; + } + + if (opts->debug) { + fprintf(stderr, "Expanding from path <%s>\n", pathbuf); + } + + int fd = open(pathbuf, O_DIRECTORY); if (fd < 0) { perror("open"); exit(1); } - struct string s = string_make(""); + struct string s = string_make(treeroot_len > 0 ? pathbuf + rootpath_len+1 : ""); + + if (opts->debug) { + fprintf(stderr, "Virtual start path <%s>\n", string_read(s)); + } + + free(pathbuf); bool was_cache_dir = false; struct tree_node *tree = make_dir_tree(opts, &s, fd, &was_cache_dir); @@ -300,7 +332,7 @@ int main(int argc, char **argv) { } } - struct tree_node *root = make_dir_tree_root(&opts, opts.rootpath); + struct tree_node *root = make_dir_tree_root(&opts, opts.rootpath, opts.treeroot); printf("files=%zu dirs=%zu size=%zu\n", root->total_files, root->total_dirs, root->total_size); |