From 9940efe053263478c5f29367b11d6f7ed1276aa4 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 2 Apr 2023 17:22:24 +0200 Subject: Move fstree CLI code to libcommon Signed-off-by: David Oberhollenzer --- lib/fstree/src/fstree.c | 96 +++++--------------------------------------- lib/fstree/src/get_by_path.c | 10 ++++- 2 files changed, 19 insertions(+), 87 deletions(-) (limited to 'lib/fstree/src') 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 #include #include -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; -- cgit v1.2.3