aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-02 17:22:24 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-02 17:22:24 +0200
commit9940efe053263478c5f29367b11d6f7ed1276aa4 (patch)
tree99d1c9e9712c73ba4e6e8e145f007cb74294f12a /lib/fstree
parent32eb57dd9a19254565a0792ab9b627a3dac319f9 (diff)
Move fstree CLI code to libcommon
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/fstree')
-rw-r--r--lib/fstree/Makemodule.am14
-rw-r--r--lib/fstree/src/fstree.c96
-rw-r--r--lib/fstree/src/get_by_path.c10
-rw-r--r--lib/fstree/test/add_by_path.c5
-rw-r--r--lib/fstree/test/fstree_init.c50
-rw-r--r--lib/fstree/test/fstree_sort.c7
-rw-r--r--lib/fstree/test/gen_inode_numbers.c7
-rw-r--r--lib/fstree/test/get_path.c5
8 files changed, 42 insertions, 152 deletions
diff --git a/lib/fstree/Makemodule.am b/lib/fstree/Makemodule.am
index e2d2ae7..4d553f4 100644
--- a/lib/fstree/Makemodule.am
+++ b/lib/fstree/Makemodule.am
@@ -18,25 +18,21 @@ test_mknode_dir_SOURCES = lib/fstree/test/mknode_dir.c
test_mknode_dir_LDADD = libfstree.a libcompat.a
test_gen_inode_numbers_SOURCES = lib/fstree/test/gen_inode_numbers.c
-test_gen_inode_numbers_LDADD = libfstree.a libutil.a libcompat.a
+test_gen_inode_numbers_LDADD = libcommon.a libfstree.a libutil.a libcompat.a
test_add_by_path_SOURCES = lib/fstree/test/add_by_path.c
-test_add_by_path_LDADD = libfstree.a libutil.a libcompat.a
+test_add_by_path_LDADD = libcommon.a libfstree.a libutil.a libcompat.a
test_get_path_SOURCES = lib/fstree/test/get_path.c
-test_get_path_LDADD = libfstree.a libutil.a libcompat.a
+test_get_path_LDADD = libcommon.a libfstree.a libutil.a libcompat.a
test_fstree_sort_SOURCES = lib/fstree/test/fstree_sort.c
-test_fstree_sort_LDADD = libfstree.a libio.a libutil.a libcompat.a
-
-test_fstree_init_SOURCES = lib/fstree/test/fstree_init.c
-test_fstree_init_LDADD = libfstree.a libio.a libutil.a libcompat.a
+test_fstree_sort_LDADD = libcommon.a libfstree.a libio.a libutil.a libcompat.a
FSTREE_TESTS = \
test_mknode_simple test_mknode_slink \
test_mknode_reg test_mknode_dir test_gen_inode_numbers \
- test_add_by_path test_get_path test_fstree_sort \
- test_fstree_init
+ test_add_by_path test_get_path test_fstree_sort
check_PROGRAMS += $(FSTREE_TESTS)
TESTS += $(FSTREE_TESTS)
diff --git a/lib/fstree/src/fstree.c b/lib/fstree/src/fstree.c
index d44a8ae..19bd997 100644
--- a/lib/fstree/src/fstree.c
+++ b/lib/fstree/src/fstree.c
@@ -7,89 +7,10 @@
#include "config.h"
#include "fstree.h"
-#include "util/util.h"
-
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
-enum {
- DEF_UID = 0,
- DEF_GID,
- DEF_MODE,
- DEF_MTIME,
-};
-
-static const char *defaults[] = {
- [DEF_UID] = "uid",
- [DEF_GID] = "gid",
- [DEF_MODE] = "mode",
- [DEF_MTIME] = "mtime",
- NULL
-};
-
-static int process_defaults(struct stat *sb, char *subopts)
-{
- char *value;
- long lval;
- int i;
-
- while (*subopts != '\0') {
- i = getsubopt(&subopts, (char *const *)defaults, &value);
-
- if (value == NULL) {
- fprintf(stderr, "Missing value for option %s\n",
- defaults[i]);
- return -1;
- }
-
- switch (i) {
- case DEF_UID:
- lval = strtol(value, NULL, 0);
- if (lval < 0)
- goto fail_uv;
- if (lval > (long)INT32_MAX)
- goto fail_ov;
- sb->st_uid = lval;
- break;
- case DEF_GID:
- lval = strtol(value, NULL, 0);
- if (lval < 0)
- goto fail_uv;
- if (lval > (long)INT32_MAX)
- goto fail_ov;
- sb->st_gid = lval;
- break;
- case DEF_MODE:
- lval = strtol(value, NULL, 0);
- if (lval < 0)
- goto fail_uv;
- if (lval > 07777)
- goto fail_ov;
- sb->st_mode = S_IFDIR | (sqfs_u16)lval;
- break;
- case DEF_MTIME:
- lval = strtol(value, NULL, 0);
- if (lval < 0)
- goto fail_uv;
- if (lval > (long)INT32_MAX)
- goto fail_ov;
- sb->st_mtime = lval;
- break;
- default:
- fprintf(stderr, "Unknown option '%s'\n", value);
- return -1;
- }
- }
- return 0;
-fail_uv:
- fprintf(stderr, "%s: value must be positive\n", defaults[i]);
- return -1;
-fail_ov:
- fprintf(stderr, "%s: value too large\n", defaults[i]);
- return -1;
-}
-
static void free_recursive(tree_node_t *n)
{
tree_node_t *it;
@@ -106,17 +27,20 @@ static void free_recursive(tree_node_t *n)
free(n);
}
-int fstree_init(fstree_t *fs, char *defaults)
+int fstree_init(fstree_t *fs, const fstree_defaults_t *defaults)
{
+ struct stat sb;
+
memset(fs, 0, sizeof(*fs));
- fs->defaults.st_mode = S_IFDIR | 0755;
- fs->defaults.st_blksize = 512;
- fs->defaults.st_mtime = get_source_date_epoch();
+ fs->defaults = *defaults;
- if (defaults != NULL && process_defaults(&fs->defaults, defaults) != 0)
- return -1;
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFDIR | (defaults->mode & 07777);
+ sb.st_uid = defaults->uid;
+ sb.st_gid = defaults->gid;
+ sb.st_mtime = defaults->mtime;
- fs->root = fstree_mknode(NULL, "", 0, NULL, &fs->defaults);
+ fs->root = fstree_mknode(NULL, "", 0, NULL, &sb);
if (fs->root == NULL) {
perror("initializing file system tree");
diff --git a/lib/fstree/src/get_by_path.c b/lib/fstree/src/get_by_path.c
index 8742892..f5e7374 100644
--- a/lib/fstree/src/get_by_path.c
+++ b/lib/fstree/src/get_by_path.c
@@ -56,12 +56,20 @@ tree_node_t *fstree_get_node_by_path(fstree_t *fs, tree_node_t *root,
n = child_by_name(root, path, len);
if (n == NULL) {
+ struct stat sb;
+
if (!create_implicitly) {
errno = ENOENT;
return NULL;
}
- n = fstree_mknode(root, path, len, NULL, &fs->defaults);
+ memset(&sb, 0, sizeof(sb));
+ sb.st_mode = S_IFDIR | (fs->defaults.mode & 07777);
+ sb.st_uid = fs->defaults.uid;
+ sb.st_gid = fs->defaults.gid;
+ sb.st_mtime = fs->defaults.mtime;
+
+ n = fstree_mknode(root, path, len, NULL, &sb);
if (n == NULL)
return NULL;
diff --git a/lib/fstree/test/add_by_path.c b/lib/fstree/test/add_by_path.c
index 97e5a60..76bae6d 100644
--- a/lib/fstree/test/add_by_path.c
+++ b/lib/fstree/test/add_by_path.c
@@ -7,10 +7,12 @@
#include "config.h"
#include "fstree.h"
+#include "common.h"
#include "util/test.h"
int main(int argc, char **argv)
{
+ fstree_defaults_t fsd;
tree_node_t *a, *b;
struct stat sb;
fstree_t fs;
@@ -18,8 +20,9 @@ int main(int argc, char **argv)
(void)argc; (void)argv;
opts = strdup("mode=0755,uid=21,gid=42");
- TEST_ASSERT(fstree_init(&fs, opts) == 0);
+ TEST_ASSERT(parse_fstree_defaults(&fsd, opts) == 0);
free(opts);
+ TEST_ASSERT(fstree_init(&fs, &fsd) == 0);
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFDIR | 0750;
diff --git a/lib/fstree/test/fstree_init.c b/lib/fstree/test/fstree_init.c
deleted file mode 100644
index 186f25b..0000000
--- a/lib/fstree/test/fstree_init.c
+++ /dev/null
@@ -1,50 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-or-later */
-/*
- * fstree_init.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#include "config.h"
-
-#include "fstree.h"
-#include "util/test.h"
-#include "util/util.h"
-
-int main(int argc, char **argv)
-{
- fstree_t fs;
- char *str;
- (void)argc; (void)argv;
-
- str = strdup("mtime=1337,uid=1000,gid=100,mode=0321");
- TEST_NOT_NULL(str);
- TEST_ASSERT(fstree_init(&fs, str) == 0);
- free(str);
- TEST_EQUAL_UI(fs.defaults.st_mtime, 1337);
- TEST_EQUAL_UI(fs.defaults.st_uid, 1000);
- TEST_EQUAL_UI(fs.defaults.st_gid, 100);
- TEST_EQUAL_UI(fs.defaults.st_mode, S_IFDIR | 0321);
- fstree_cleanup(&fs);
-
- TEST_ASSERT(fstree_init(&fs, NULL) == 0);
- if (fs.defaults.st_mtime != 0) {
- TEST_EQUAL_UI(fs.defaults.st_mtime, get_source_date_epoch());
- }
- TEST_EQUAL_UI(fs.defaults.st_uid, 0);
- TEST_EQUAL_UI(fs.defaults.st_gid, 0);
- TEST_EQUAL_UI(fs.defaults.st_mode, S_IFDIR | 0755);
- fstree_cleanup(&fs);
-
- str = strdup("mode=07777");
- TEST_NOT_NULL(str);
- TEST_ASSERT(fstree_init(&fs, str) == 0);
- free(str);
- fstree_cleanup(&fs);
-
- str = strdup("mode=017777");
- TEST_NOT_NULL(str);
- TEST_ASSERT(fstree_init(&fs, str) != 0);
- free(str);
-
- return EXIT_SUCCESS;
-}
diff --git a/lib/fstree/test/fstree_sort.c b/lib/fstree/test/fstree_sort.c
index 7df85f5..0d18ac7 100644
--- a/lib/fstree/test/fstree_sort.c
+++ b/lib/fstree/test/fstree_sort.c
@@ -7,11 +7,13 @@
#include "config.h"
#include "fstree.h"
+#include "common.h"
#include "util/test.h"
int main(int argc, char **argv)
{
tree_node_t *a, *b, *c, *d;
+ fstree_defaults_t fsd;
struct stat sb;
fstree_t fs;
int ret;
@@ -22,7 +24,8 @@ int main(int argc, char **argv)
sb.st_rdev = 1337;
/* in order */
- ret = fstree_init(&fs, NULL);
+ TEST_ASSERT(parse_fstree_defaults(&fsd, NULL) == 0);
+ ret = fstree_init(&fs, &fsd);
TEST_EQUAL_I(ret, 0);
a = fstree_mknode(fs.root, "a", 1, NULL, &sb);
@@ -54,7 +57,7 @@ int main(int argc, char **argv)
fstree_cleanup(&fs);
/* out-of-order */
- ret = fstree_init(&fs, NULL);
+ ret = fstree_init(&fs, &fsd);
TEST_EQUAL_I(ret, 0);
d = fstree_mknode(fs.root, "d", 1, NULL, &sb);
diff --git a/lib/fstree/test/gen_inode_numbers.c b/lib/fstree/test/gen_inode_numbers.c
index 5403580..bb8c976 100644
--- a/lib/fstree/test/gen_inode_numbers.c
+++ b/lib/fstree/test/gen_inode_numbers.c
@@ -7,6 +7,7 @@
#include "config.h"
#include "fstree.h"
+#include "common.h"
#include "util/test.h"
static tree_node_t *gen_node(tree_node_t *parent, const char *name)
@@ -47,18 +48,20 @@ static void check_children_continuous(tree_node_t *root)
int main(int argc, char **argv)
{
tree_node_t *a, *b, *c;
+ fstree_defaults_t fsd;
fstree_t fs;
(void)argc; (void)argv;
// inode table for the empty tree
- TEST_ASSERT(fstree_init(&fs, NULL) == 0);
+ TEST_ASSERT(parse_fstree_defaults(&fsd, NULL) == 0);
+ TEST_ASSERT(fstree_init(&fs, &fsd) == 0);
fstree_post_process(&fs);
TEST_EQUAL_UI(fs.unique_inode_count, 1);
TEST_EQUAL_UI(fs.root->inode_num, 1);
fstree_cleanup(&fs);
// tree with 2 levels under root, fan out 3
- TEST_ASSERT(fstree_init(&fs, NULL) == 0);
+ TEST_ASSERT(fstree_init(&fs, &fsd) == 0);
a = gen_node(fs.root, "a");
b = gen_node(fs.root, "b");
diff --git a/lib/fstree/test/get_path.c b/lib/fstree/test/get_path.c
index 61001e9..572cde7 100644
--- a/lib/fstree/test/get_path.c
+++ b/lib/fstree/test/get_path.c
@@ -7,17 +7,20 @@
#include "config.h"
#include "fstree.h"
+#include "common.h"
#include "util/test.h"
int main(int argc, char **argv)
{
tree_node_t *a, *b, *c, *d;
+ fstree_defaults_t fsd;
struct stat sb;
fstree_t fs;
char *str;
(void)argc; (void)argv;
- TEST_ASSERT(fstree_init(&fs, NULL) == 0);
+ TEST_ASSERT(parse_fstree_defaults(&fsd, NULL) == 0);
+ TEST_ASSERT(fstree_init(&fs, &fsd) == 0);
memset(&sb, 0, sizeof(sb));
sb.st_mode = S_IFDIR | 0750;