aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2021-02-18 21:50:36 +0100
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2021-02-18 23:50:46 +0100
commit9e510d4ae6dbad555a3416fbe041f4aa8189cbde (patch)
treec0076c297f7039697c79174879e6d08703ae3bdd /lib/fstree
parentb3051223fcafc954767058811d279f5b6941a9ba (diff)
libfstree: add a subdirectory scanning function
So we can scan a sub-directory within a the base directory without having to do string operations first. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/fstree')
-rw-r--r--lib/fstree/fstree_from_dir.c48
1 files changed, 39 insertions, 9 deletions
diff --git a/lib/fstree/fstree_from_dir.c b/lib/fstree/fstree_from_dir.c
index fe12b24..d3e188d 100644
--- a/lib/fstree/fstree_from_dir.c
+++ b/lib/fstree/fstree_from_dir.c
@@ -13,13 +13,20 @@
#include <errno.h>
#ifdef _WIN32
-int fstree_from_dir(fstree_t *fs, tree_node_t *root,
- const char *path, unsigned int flags)
+int fstree_from_subdir(fstree_t *fs, tree_node_t *root,
+ const char *path, const char *subdir,
+ unsigned int flags)
{
- (void)fs; (void)root; (void)path; (void)flags;
+ (void)fs; (void)root; (void)path; (void)subdir; (void)flags;
fputs("Packing a directory is not supported on Windows.\n", stderr);
return -1;
}
+
+int fstree_from_dir(fstree_t *fs, tree_node_t *root,
+ const char *path, unsigned int flags)
+{
+ return fstree_from_subdir(fs, root, path, NULL, flags);
+}
#else
static int populate_dir(int dir_fd, fstree_t *fs, tree_node_t *root,
dev_t devstart, unsigned int flags)
@@ -150,16 +157,17 @@ fail:
return -1;
}
-int fstree_from_dir(fstree_t *fs, tree_node_t *root,
- const char *path, unsigned int flags)
+int fstree_from_subdir(fstree_t *fs, tree_node_t *root,
+ const char *path, const char *subdir,
+ unsigned int flags)
{
struct stat sb;
- int fd;
+ int fd, subfd;
if (!S_ISDIR(root->mode)) {
fprintf(stderr,
- "scanning %s into %s: target is not a directory\n",
- path, root->name);
+ "scanning %s/%s into %s: target is not a directory\n",
+ path, subdir == NULL ? "" : subdir, root->name);
return -1;
}
@@ -169,12 +177,34 @@ int fstree_from_dir(fstree_t *fs, tree_node_t *root,
return -1;
}
+ if (subdir != NULL) {
+ subfd = openat(fd, subdir, O_DIRECTORY | O_RDONLY | O_CLOEXEC);
+
+ if (subfd < 0) {
+ fprintf(stderr, "%s/%s: %s\n", path, subdir,
+ strerror(errno));
+ close(fd);
+ return -1;
+ }
+
+ close(fd);
+ fd = subfd;
+ }
+
if (fstat(fd, &sb)) {
- perror(path);
+ fprintf(stderr, "%s/%s: %s\n", path,
+ subdir == NULL ? "" : subdir,
+ strerror(errno));
close(fd);
return -1;
}
return populate_dir(fd, fs, root, sb.st_dev, flags);
}
+
+int fstree_from_dir(fstree_t *fs, tree_node_t *root,
+ const char *path, unsigned int flags)
+{
+ return fstree_from_subdir(fs, root, path, NULL, flags);
+}
#endif