diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-04-02 17:22:24 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-04-02 17:22:24 +0200 |
commit | 9940efe053263478c5f29367b11d6f7ed1276aa4 (patch) | |
tree | 99d1c9e9712c73ba4e6e8e145f007cb74294f12a /lib/common | |
parent | 32eb57dd9a19254565a0792ab9b627a3dac319f9 (diff) |
Move fstree CLI code to libcommon
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/common')
-rw-r--r-- | lib/common/Makemodule.am | 12 | ||||
-rw-r--r-- | lib/common/src/fstree_cli.c | 97 | ||||
-rw-r--r-- | lib/common/src/writer/init.c | 8 | ||||
-rw-r--r-- | lib/common/test/fstree_cli.c | 47 |
4 files changed, 161 insertions, 3 deletions
diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index 74b924e..63fa602 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -6,7 +6,8 @@ libcommon_a_SOURCES = include/common.h include/simple_writer.h \ lib/common/src/data_writer_ostream.c lib/common/src/perror.c \ lib/common/src/parse_size.c lib/common/src/print_size.c \ lib/common/src/writer/init.c lib/common/src/writer/cleanup.c \ - lib/common/src/writer/serialize_fstree.c lib/common/src/writer/finish.c + lib/common/src/writer/serialize_fstree.c lib/common/src/writer/finish.c\ + lib/common/src/fstree_cli.c libcommon_a_CFLAGS = $(AM_CFLAGS) $(LZO_CFLAGS) if WITH_LZO @@ -14,3 +15,12 @@ libcommon_a_SOURCES += lib/common/src/comp_lzo.c endif noinst_LIBRARIES += libcommon.a + +test_fstree_cli_SOURCES = lib/common/test/fstree_cli.c +test_fstree_cli_LDADD = libcommon.a libio.a libutil.a libcompat.a + +LIBCOMMON_TESTS = \ + test_fstree_cli + +check_PROGRAMS += $(LIBCOMMON_TESTS) +TESTS += $(LIBCOMMON_TESTS) diff --git a/lib/common/src/fstree_cli.c b/lib/common/src/fstree_cli.c new file mode 100644 index 0000000..179a1f5 --- /dev/null +++ b/lib/common/src/fstree_cli.c @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * fstree_cli.c + * + * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" +#include "common.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 +}; + +int parse_fstree_defaults(fstree_defaults_t *sb, char *subopts) +{ + char *value; + long lval; + int i; + + memset(sb, 0, sizeof(*sb)); + sb->mode = S_IFDIR | 0755; + sb->mtime = get_source_date_epoch(); + + if (subopts == NULL) + return 0; + + 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->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->gid = lval; + break; + case DEF_MODE: + lval = strtol(value, NULL, 0); + if (lval < 0) + goto fail_uv; + if (lval > 07777) + goto fail_ov; + sb->mode = S_IFDIR | (sqfs_u16)lval; + break; + case DEF_MTIME: + lval = strtol(value, NULL, 0); + if (lval < 0) + goto fail_uv; + if (lval > (long)UINT32_MAX) + goto fail_ov; + sb->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; +} diff --git a/lib/common/src/writer/init.c b/lib/common/src/writer/init.c index fe5fcc5..60d7a12 100644 --- a/lib/common/src/writer/init.c +++ b/lib/common/src/writer/init.c @@ -46,6 +46,7 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) { sqfs_block_processor_desc_t blkdesc; sqfs_compressor_config_t cfg; + fstree_defaults_t fsd; int ret, flags; sqfs->filename = wrcfg->filename; @@ -62,7 +63,10 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) return -1; } - if (fstree_init(&sqfs->fs, wrcfg->fs_defaults)) + if (parse_fstree_defaults(&fsd, wrcfg->fs_defaults)) + goto fail_file; + + if (fstree_init(&sqfs->fs, &fsd)) goto fail_file; ret = sqfs_compressor_create(&cfg, &sqfs->cmp); @@ -99,7 +103,7 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg) } ret = sqfs_super_init(&sqfs->super, wrcfg->block_size, - sqfs->fs.defaults.st_mtime, wrcfg->comp_id); + sqfs->fs.defaults.mtime, wrcfg->comp_id); if (ret) { sqfs_perror(wrcfg->filename, "initializing super block", ret); goto fail_uncmp; diff --git a/lib/common/test/fstree_cli.c b/lib/common/test/fstree_cli.c new file mode 100644 index 0000000..a582adb --- /dev/null +++ b/lib/common/test/fstree_cli.c @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * fstree_cli.c + * + * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> + */ +#include "config.h" + +#include "common.h" +#include "util/test.h" +#include "util/util.h" + +int main(int argc, char **argv) +{ + fstree_defaults_t fs; + char *str; + (void)argc; (void)argv; + + str = strdup("mtime=1337,uid=1000,gid=100,mode=0321"); + TEST_NOT_NULL(str); + TEST_ASSERT(parse_fstree_defaults(&fs, str) == 0); + free(str); + TEST_EQUAL_UI(fs.mtime, 1337); + TEST_EQUAL_UI(fs.uid, 1000); + TEST_EQUAL_UI(fs.gid, 100); + TEST_EQUAL_UI(fs.mode, S_IFDIR | 0321); + + TEST_ASSERT(parse_fstree_defaults(&fs, NULL) == 0); + if (fs.mtime != 0) { + TEST_EQUAL_UI(fs.mtime, get_source_date_epoch()); + } + TEST_EQUAL_UI(fs.uid, 0); + TEST_EQUAL_UI(fs.gid, 0); + TEST_EQUAL_UI(fs.mode, S_IFDIR | 0755); + + str = strdup("mode=07777"); + TEST_NOT_NULL(str); + TEST_ASSERT(parse_fstree_defaults(&fs, str) == 0); + free(str); + + str = strdup("mode=017777"); + TEST_NOT_NULL(str); + TEST_ASSERT(parse_fstree_defaults(&fs, str) != 0); + free(str); + + return EXIT_SUCCESS; +} |