summaryrefslogtreecommitdiff
path: root/lib/fstree/fstree_from_dir.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-12-15 11:07:12 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-12-15 11:07:12 +0100
commitefa204aedf3578d193b2832b42cf365c7aee48f9 (patch)
tree310f5e01aa656aeaf791808e14033eb3d40b5ea0 /lib/fstree/fstree_from_dir.c
parentf138e4a24919682cf477cf93ae47b9a89bb5a3f0 (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/fstree/fstree_from_dir.c')
-rw-r--r--lib/fstree/fstree_from_dir.c19
1 files changed, 14 insertions, 5 deletions
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 <errno.h>
#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