From efa204aedf3578d193b2832b42cf365c7aee48f9 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 15 Dec 2020 11:07:12 +0100 Subject: libfstree: make the directory scanning code a little more generic - Instead of using the fstree root, let the caller specify it. - Add a flag to prevent recursion into sub directories. Signed-off-by: David Oberhollenzer --- lib/fstree/fstree_from_dir.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'lib/fstree') diff --git a/lib/fstree/fstree_from_dir.c b/lib/fstree/fstree_from_dir.c index e61b706..fca916d 100644 --- a/lib/fstree/fstree_from_dir.c +++ b/lib/fstree/fstree_from_dir.c @@ -13,9 +13,10 @@ #include #ifdef _WIN32 -int fstree_from_dir(fstree_t *fs, const char *path, unsigned int flags) +int fstree_from_dir(fstree_t *fs, tree_node_t *root, + const char *path, unsigned int flags) { - (void)fs; (void)path; (void)flags; + (void)fs; (void)root; (void)path; (void)flags; fputs("Packing a directory is not supported on Windows.\n", stderr); return -1; } @@ -90,7 +91,7 @@ static int populate_dir(int dir_fd, fstree_t *fs, tree_node_t *root, free(extra); extra = NULL; - if (S_ISDIR(n->mode)) { + if (S_ISDIR(n->mode) && !(flags & DIR_SCAN_NO_RECURSION)) { childfd = openat(dir_fd, n->name, O_DIRECTORY | O_RDONLY | O_CLOEXEC); if (childfd < 0) { @@ -113,11 +114,19 @@ fail: return -1; } -int fstree_from_dir(fstree_t *fs, const char *path, unsigned int flags) +int fstree_from_dir(fstree_t *fs, tree_node_t *root, + const char *path, unsigned int flags) { struct stat sb; int fd; + if (!S_ISDIR(root->mode)) { + fprintf(stderr, + "scanning %s into %s: target is not a directory\n", + path, root->name); + return -1; + } + fd = open(path, O_DIRECTORY | O_RDONLY | O_CLOEXEC); if (fd < 0) { perror(path); @@ -130,6 +139,6 @@ int fstree_from_dir(fstree_t *fs, const char *path, unsigned int flags) return -1; } - return populate_dir(fd, fs, fs->root, sb.st_dev, flags); + return populate_dir(fd, fs, root, sb.st_dev, flags); } #endif -- cgit v1.2.3