From b0b88d26c9df6d336167f87ce926bba9b56f6af0 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 25 Mar 2021 14:23:58 +0100 Subject: libfstree: Allow / as argument for "glob" and "dir" commands This allows putting globbed files & directories into the filesystem root, as well as explicitly setting attributes of the root directory from the file lisiting. Signed-off-by: David Oberhollenzer --- lib/fstree/add_by_path.c | 7 ++++++- lib/fstree/fstree.c | 1 + lib/fstree/fstree_from_file.c | 26 +++++++++++++++++--------- 3 files changed, 24 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/fstree/add_by_path.c b/lib/fstree/add_by_path.c index 7c9063e..8918efc 100644 --- a/lib/fstree/add_by_path.c +++ b/lib/fstree/add_by_path.c @@ -17,6 +17,11 @@ tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, tree_node_t *child, *parent; const char *name; + if (*path == '\0') { + child = fs->root; + goto out; + } + parent = fstree_get_node_by_path(fs, fs->root, path, true, true); if (parent == NULL) return NULL; @@ -27,7 +32,7 @@ tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, child = parent->data.dir.children; while (child != NULL && strcmp(child->name, name) != 0) child = child->next; - +out: if (child != NULL) { if (!S_ISDIR(child->mode) || !S_ISDIR(sb->st_mode) || !child->data.dir.created_implicitly) { diff --git a/lib/fstree/fstree.c b/lib/fstree/fstree.c index b2e199d..7c00755 100644 --- a/lib/fstree/fstree.c +++ b/lib/fstree/fstree.c @@ -120,6 +120,7 @@ int fstree_init(fstree_t *fs, char *defaults) return -1; } + fs->root->data.dir.created_implicitly = true; return 0; } diff --git a/lib/fstree/fstree_from_file.c b/lib/fstree/fstree_from_file.c index dd7ca22..3bf8cd1 100644 --- a/lib/fstree/fstree_from_file.c +++ b/lib/fstree/fstree_from_file.c @@ -344,19 +344,20 @@ static const struct callback_t { unsigned int mode; bool need_extra; bool is_glob; + bool allow_root; int (*callback)(fstree_t *fs, const char *filename, size_t line_num, const char *path, struct stat *sb, const char *basepath, unsigned int glob_flags, const char *extra); } file_list_hooks[] = { - { "dir", S_IFDIR, false, false, add_generic }, - { "slink", S_IFLNK, true, false, add_generic }, - { "link", 0, true, false, add_hard_link }, - { "nod", 0, true, false, add_device }, - { "pipe", S_IFIFO, false, false, add_generic }, - { "sock", S_IFSOCK, false, false, add_generic }, - { "file", S_IFREG, false, false, add_file }, - { "glob", 0, true, true, glob_files }, + { "dir", S_IFDIR, false, false, true, add_generic }, + { "slink", S_IFLNK, true, false, false, add_generic }, + { "link", 0, true, false, false, add_hard_link }, + { "nod", 0, true, false, false, add_device }, + { "pipe", S_IFIFO, false, false, false, add_generic }, + { "sock", S_IFSOCK, false, false, false, add_generic }, + { "file", S_IFREG, false, false, false, add_file }, + { "glob", 0, true, true, true, glob_files }, }; #define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0])) @@ -456,9 +457,12 @@ static int handle_line(fstree_t *fs, const char *filename, if ((line = read_str(line, &path)) == NULL) goto fail_ent; - if (canonicalize_name(path) || *path == '\0') + if (canonicalize_name(path)) goto fail_ent; + if (*path == '\0' && !cb->allow_root) + goto fail_root; + if (cb->is_glob && *line == '*') { ++line; mode = 0; @@ -507,6 +511,10 @@ static int handle_line(fstree_t *fs, const char *filename, return cb->callback(fs, filename, line_num, path, &sb, basepath, glob_flags, extra); +fail_root: + fprintf(stderr, "%s: " PRI_SZ ": cannot use / as argument for %s.\n", + filename, line_num, cb->keyword); + return -1; fail_no_extra: fprintf(stderr, "%s: " PRI_SZ ": missing argument for %s.\n", filename, line_num, cb->keyword); -- cgit v1.2.3