diff options
-rw-r--r-- | bin/gensquashfs/src/fstree_from_dir.c | 18 | ||||
-rw-r--r-- | bin/gensquashfs/src/fstree_from_file.c | 83 | ||||
-rw-r--r-- | bin/gensquashfs/src/glob.c | 16 | ||||
-rw-r--r-- | bin/gensquashfs/src/mkfs.h | 2 | ||||
-rw-r--r-- | bin/tar2sqfs/src/process_tarball.c | 32 | ||||
-rw-r--r-- | include/fstree.h | 11 | ||||
-rw-r--r-- | lib/fstree/Makemodule.am | 13 | ||||
-rw-r--r-- | lib/fstree/src/fstree.c | 84 | ||||
-rw-r--r-- | lib/fstree/src/hardlink.c | 27 | ||||
-rw-r--r-- | lib/fstree/test/add_by_path.c | 107 | ||||
-rw-r--r-- | lib/fstree/test/fstree_sort.c | 38 | ||||
-rw-r--r-- | lib/fstree/test/gen_inode_numbers.c | 12 | ||||
-rw-r--r-- | lib/fstree/test/get_path.c | 35 | ||||
-rw-r--r-- | lib/fstree/test/mknode_dir.c | 41 | ||||
-rw-r--r-- | lib/fstree/test/mknode_reg.c | 47 | ||||
-rw-r--r-- | lib/fstree/test/mknode_simple.c | 148 | ||||
-rw-r--r-- | lib/fstree/test/mknode_slink.c | 60 |
17 files changed, 370 insertions, 404 deletions
diff --git a/bin/gensquashfs/src/fstree_from_dir.c b/bin/gensquashfs/src/fstree_from_dir.c index 6c37ee8..e2558bc 100644 --- a/bin/gensquashfs/src/fstree_from_dir.c +++ b/bin/gensquashfs/src/fstree_from_dir.c @@ -12,22 +12,12 @@ #include <string.h> #include <errno.h> -static sqfs_u32 clamp_timestamp(sqfs_s64 ts) -{ - if (ts < 0) - return 0; - if (ts > 0x0FFFFFFFFLL) - return 0xFFFFFFFF; - return ts; -} - int fstree_from_dir(fstree_t *fs, dir_iterator_t *dir) { for (;;) { dir_entry_t *ent = NULL; tree_node_t *n = NULL; char *extra = NULL; - struct stat sb; int ret = dir->next(dir, &ent); if (ret > 0) @@ -55,13 +45,7 @@ int fstree_from_dir(fstree_t *fs, dir_iterator_t *dir) } } - memset(&sb, 0, sizeof(sb)); - sb.st_uid = ent->uid; - sb.st_gid = ent->gid; - sb.st_mode = ent->mode; - sb.st_mtime = clamp_timestamp(ent->mtime); - - n = fstree_add_generic(fs, ent->name, &sb, extra); + n = fstree_add_generic(fs, ent, extra); free(extra); free(ent); diff --git a/bin/gensquashfs/src/fstree_from_file.c b/bin/gensquashfs/src/fstree_from_file.c index d4d77a7..dc7181f 100644 --- a/bin/gensquashfs/src/fstree_from_file.c +++ b/bin/gensquashfs/src/fstree_from_file.c @@ -7,11 +7,11 @@ #include "mkfs.h" static int add_generic(fstree_t *fs, const char *filename, size_t line_num, - const char *path, struct stat *sb, const char *extra) + dir_entry_t *ent, const char *extra) { - if (fstree_add_generic(fs, path, sb, extra) == NULL) { + if (fstree_add_generic(fs, ent, extra) == NULL) { fprintf(stderr, "%s: " PRI_SZ ": %s: %s\n", - filename, line_num, path, strerror(errno)); + filename, line_num, ent->name, strerror(errno)); return -1; } @@ -19,7 +19,7 @@ static int add_generic(fstree_t *fs, const char *filename, size_t line_num, } static int add_device(fstree_t *fs, const char *filename, size_t line_num, - const char *path, struct stat *sb, const char *extra) + dir_entry_t *ent, const char *extra) { unsigned int maj, min; char c; @@ -32,57 +32,44 @@ static int add_device(fstree_t *fs, const char *filename, size_t line_num, } if (c == 'c' || c == 'C') { - sb->st_mode |= S_IFCHR; + ent->mode |= S_IFCHR; } else if (c == 'b' || c == 'B') { - sb->st_mode |= S_IFBLK; + ent->mode |= S_IFBLK; } else { fprintf(stderr, "%s: " PRI_SZ ": unknown device type '%c'\n", filename, line_num, c); return -1; } - sb->st_rdev = makedev(maj, min); - return add_generic(fs, filename, line_num, path, sb, NULL); + ent->rdev = makedev(maj, min); + return add_generic(fs, filename, line_num, ent, NULL); } static int add_file(fstree_t *fs, const char *filename, size_t line_num, - const char *path, struct stat *basic, const char *extra) + dir_entry_t *ent, const char *extra) { if (extra == NULL || *extra == '\0') - extra = path; + extra = ent->name; - return add_generic(fs, filename, line_num, path, basic, extra); -} - -static int add_hard_link(fstree_t *fs, const char *filename, size_t line_num, - const char *path, struct stat *basic, - const char *extra) -{ - (void)basic; - - if (fstree_add_hard_link(fs, path, extra) == NULL) { - fprintf(stderr, "%s: " PRI_SZ ": %s\n", - filename, line_num, strerror(errno)); - return -1; - } - return 0; + return add_generic(fs, filename, line_num, ent, extra); } static const struct callback_t { const char *keyword; unsigned int mode; + unsigned int flags; bool need_extra; bool allow_root; int (*callback)(fstree_t *fs, const char *filename, size_t line_num, - const char *path, struct stat *sb, const char *extra); + dir_entry_t *ent, const char *extra); } file_list_hooks[] = { - { "dir", S_IFDIR, false, true, add_generic }, - { "slink", S_IFLNK, true, false, add_generic }, - { "link", 0, true, false, add_hard_link }, - { "nod", 0, true, false, add_device }, - { "pipe", S_IFIFO, false, false, add_generic }, - { "sock", S_IFSOCK, false, false, add_generic }, - { "file", S_IFREG, false, false, add_file }, + { "dir", S_IFDIR, 0, false, true, add_generic }, + { "slink", S_IFLNK, 0, true, false, add_generic }, + { "link", S_IFLNK, DIR_ENTRY_FLAG_HARD_LINK, true, false, add_generic }, + { "nod", 0, 0, true, false, add_device }, + { "pipe", S_IFIFO, 0, false, false, add_generic }, + { "sock", S_IFSOCK, 0, false, false, add_generic }, + { "file", S_IFREG, 0, false, false, add_file }, }; #define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0])) @@ -161,9 +148,10 @@ static int handle_line(fstree_t *fs, const char *filename, const struct callback_t *cb = NULL; unsigned int glob_flags = 0; sqfs_u32 uid, gid, mode; + dir_entry_t *ent = NULL; bool is_glob = false; - struct stat sb; char *path; + int ret; for (size_t i = 0; i < NUM_HOOKS; ++i) { size_t len = strlen(file_list_hooks[i].keyword); @@ -235,18 +223,29 @@ static int handle_line(fstree_t *fs, const char *filename, goto fail_no_extra; /* forward to callback */ - memset(&sb, 0, sizeof(sb)); - sb.st_mtime = fs->defaults.mtime; - sb.st_mode = mode | (is_glob ? 0 : cb->mode); - sb.st_uid = uid; - sb.st_gid = gid; + ent = alloc_flex(sizeof(*ent), 1, strlen(path) + 1); + if (ent == NULL) + goto fail_alloc; + + strcpy(ent->name, path); + ent->mtime = fs->defaults.mtime; + ent->mode = mode | (is_glob ? 0 : cb->mode); + ent->uid = uid; + ent->gid = gid; if (is_glob) { - return glob_files(fs, filename, line_num, path, &sb, - basepath, glob_flags, extra); + ret = glob_files(fs, filename, line_num, ent, + basepath, glob_flags, extra); + } else { + ret = cb->callback(fs, filename, line_num, ent, extra); } - return cb->callback(fs, filename, line_num, path, &sb, extra); + free(ent); + return ret; +fail_alloc: + fprintf(stderr, "%s: " PRI_SZ ": out of memory\n", + filename, line_num); + return -1; fail_root: fprintf(stderr, "%s: " PRI_SZ ": cannot use / as argument for %s.\n", filename, line_num, is_glob ? "glob" : cb->keyword); diff --git a/bin/gensquashfs/src/glob.c b/bin/gensquashfs/src/glob.c index 8540d81..853ba42 100644 --- a/bin/gensquashfs/src/glob.c +++ b/bin/gensquashfs/src/glob.c @@ -79,7 +79,7 @@ static bool match_option(const char **line, const char *candidate) } int glob_files(fstree_t *fs, const char *filename, size_t line_num, - const char *path, struct stat *basic, + const dir_entry_t *ent, const char *basepath, unsigned int glob_flags, const char *extra) { @@ -92,7 +92,7 @@ int glob_files(fstree_t *fs, const char *filename, size_t line_num, int ret; /* fetch the actual target node */ - root = fstree_get_node_by_path(fs, fs->root, path, true, false); + root = fstree_get_node_by_path(fs, fs->root, ent->name, true, false); if (root == NULL) goto fail_path; @@ -165,10 +165,10 @@ int glob_files(fstree_t *fs, const char *filename, size_t line_num, /* do the scan */ memset(&cfg, 0, sizeof(cfg)); cfg.flags = scan_flags | glob_flags; - cfg.def_mtime = basic->st_mtime; - cfg.def_uid = basic->st_uid; - cfg.def_gid = basic->st_gid; - cfg.def_mode = basic->st_mode; + cfg.def_mtime = ent->mtime; + cfg.def_uid = ent->uid; + cfg.def_gid = ent->gid; + cfg.def_mode = ent->mode; cfg.prefix = prefix; cfg.name_pattern = name_pattern; @@ -207,11 +207,11 @@ fail_unknown: goto fail; fail_path: fprintf(stderr, "%s: " PRI_SZ ": %s: %s\n", - filename, line_num, path, strerror(errno)); + filename, line_num, ent->name, strerror(errno)); goto fail; fail_not_dir: fprintf(stderr, "%s: " PRI_SZ ": %s is not a directoy!\n", - filename, line_num, path); + filename, line_num, ent->name); goto fail; fail_prefix: fprintf(stderr, "%s: " PRI_SZ ": error cannonicalizing `%s`!\n", diff --git a/bin/gensquashfs/src/mkfs.h b/bin/gensquashfs/src/mkfs.h index 6c95588..9cb289d 100644 --- a/bin/gensquashfs/src/mkfs.h +++ b/bin/gensquashfs/src/mkfs.h @@ -125,7 +125,7 @@ int fstree_from_dir(fstree_t *fs, dir_iterator_t *dir); int fstree_sort_files(fstree_t *fs, istream_t *sortfile); int glob_files(fstree_t *fs, const char *filename, size_t line_num, - const char *path, struct stat *basic, + const dir_entry_t *ent, const char *basepath, unsigned int glob_flags, const char *extra); diff --git a/bin/tar2sqfs/src/process_tarball.c b/bin/tar2sqfs/src/process_tarball.c index 2399dfa..ef49d20 100644 --- a/bin/tar2sqfs/src/process_tarball.c +++ b/bin/tar2sqfs/src/process_tarball.c @@ -93,31 +93,18 @@ static int create_node_and_repack_data(sqfs_writer_t *sqfs, dir_iterator_t *it, const dir_entry_t *ent, const char *link) { tree_node_t *node; - struct stat sb; - if (ent->flags & DIR_ENTRY_FLAG_HARD_LINK) { - node = fstree_add_hard_link(&sqfs->fs, ent->name, link); - if (node == NULL) - goto fail_errno; - - if (!cfg.quiet) - printf("Hard link %s -> %s\n", ent->name, link); - return 0; - } - - memset(&sb, 0, sizeof(sb)); - sb.st_mode = ent->mode; - sb.st_uid = ent->uid; - sb.st_gid = ent->gid; - sb.st_rdev = ent->rdev; - sb.st_mtime = keep_time ? ent->mtime : sqfs->fs.defaults.mtime; - - node = fstree_add_generic(&sqfs->fs, ent->name, &sb, link); + node = fstree_add_generic(&sqfs->fs, ent, link); if (node == NULL) goto fail_errno; - if (!cfg.quiet) - printf("Packing %s\n", ent->name); + if (!cfg.quiet) { + if (ent->flags & DIR_ENTRY_FLAG_HARD_LINK) { + printf("Hard link %s -> %s\n", ent->name, link); + } else { + printf("Packing %s\n", ent->name); + } + } if (!cfg.no_xattr) { if (copy_xattr(sqfs, ent->name, node, it)) @@ -234,6 +221,9 @@ int process_tarball(istream_t *input_file, sqfs_writer_t *sqfs) is_root = true; } + if (!keep_time) + ent->mtime = sqfs->fs.defaults.mtime; + if (is_root) { ret = set_root_attribs(sqfs, it, ent); } else if (skip) { diff --git a/include/fstree.h b/include/fstree.h index 83b7737..55f0928 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -15,6 +15,7 @@ #include <stdio.h> #include "sqfs/predef.h" +#include "io/dir_iterator.h" #include "io/istream.h" #include "compat.h" @@ -141,8 +142,8 @@ void fstree_cleanup(fstree_t *fs); This function does not print anything to stderr, instead it sets an appropriate errno value. Internally it uses fstree_mknode to create the node. */ -tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, - const struct stat *sb, const char *extra); +tree_node_t *fstree_add_generic(fstree_t *fs, const dir_entry_t *ent, + const char *extra); /* This function performs all the necessary post processing steps on the file @@ -183,12 +184,6 @@ tree_node_t *fstree_get_node_by_path(fstree_t *fs, tree_node_t *root, bool stop_at_parent); /* - Add a hard link node. Returns NULL on failure and sets errno. - */ -tree_node_t *fstree_add_hard_link(fstree_t *fs, const char *path, - const char *target); - -/* Try to resolve all hard links in the tree. Returns 0 on success. On failure, errno is set. diff --git a/lib/fstree/Makemodule.am b/lib/fstree/Makemodule.am index 109af8e..965251b 100644 --- a/lib/fstree/Makemodule.am +++ b/lib/fstree/Makemodule.am @@ -5,16 +5,10 @@ libfstree_a_SOURCES = include/fstree.h lib/fstree/src/fstree.c \ noinst_LIBRARIES += libfstree.a test_mknode_simple_SOURCES = lib/fstree/test/mknode_simple.c -test_mknode_simple_LDADD = libfstree.a libcompat.a - -test_mknode_slink_SOURCES = lib/fstree/test/mknode_slink.c -test_mknode_slink_LDADD = libfstree.a libcompat.a - -test_mknode_reg_SOURCES = lib/fstree/test/mknode_reg.c -test_mknode_reg_LDADD = libfstree.a libcompat.a +test_mknode_simple_LDADD = libfstree.a libutil.a libcompat.a test_mknode_dir_SOURCES = lib/fstree/test/mknode_dir.c -test_mknode_dir_LDADD = libfstree.a libcompat.a +test_mknode_dir_LDADD = libfstree.a libutil.a libcompat.a test_gen_inode_numbers_SOURCES = lib/fstree/test/gen_inode_numbers.c test_gen_inode_numbers_LDADD = libcommon.a libfstree.a libutil.a libcompat.a @@ -29,8 +23,7 @@ test_fstree_sort_SOURCES = lib/fstree/test/fstree_sort.c 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_mknode_simple test_mknode_dir test_gen_inode_numbers \ test_add_by_path test_get_path test_fstree_sort check_PROGRAMS += $(FSTREE_TESTS) diff --git a/lib/fstree/src/fstree.c b/lib/fstree/src/fstree.c index 43a6ccc..1421d44 100644 --- a/lib/fstree/src/fstree.c +++ b/lib/fstree/src/fstree.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> */ #include "config.h" +#include "util/util.h" #include "fstree.h" #include <string.h> @@ -13,6 +14,15 @@ #include <stdio.h> #include <errno.h> +static sqfs_u32 clamp_timestamp(sqfs_s64 ts) +{ + if (ts < 0) + return 0; + if (ts > 0x0FFFFFFFFLL) + return 0xFFFFFFFF; + return ts; +} + static void free_recursive(tree_node_t *n) { tree_node_t *it; @@ -63,9 +73,9 @@ static void insert_sorted(tree_node_t *root, tree_node_t *n) } } -static tree_node_t *mknode(tree_node_t *parent, const char *name, +static tree_node_t *mknode(fstree_t *fs, tree_node_t *parent, const char *name, size_t name_len, const char *extra, - const struct stat *sb) + const dir_entry_t *ent) { tree_node_t *n; size_t size; @@ -80,10 +90,10 @@ static tree_node_t *mknode(tree_node_t *parent, const char *name, return NULL; n->xattr_idx = 0xFFFFFFFF; - n->uid = sb->st_uid; - n->gid = sb->st_gid; - n->mode = sb->st_mode; - n->mod_time = sb->st_mtime; + n->uid = ent->uid; + n->gid = ent->gid; + n->mode = ent->mode; + n->mod_time = clamp_timestamp(ent->mtime); n->link_count = 1; n->name = (char *)n->payload; memcpy(n->name, name, name_len); @@ -91,11 +101,24 @@ static tree_node_t *mknode(tree_node_t *parent, const char *name, if (extra != NULL) { ptr = n->name + name_len + 1; strcpy(ptr, extra); + + if (ent->flags & DIR_ENTRY_FLAG_HARD_LINK) { + if (canonicalize_name(ptr)) { + free(n); + errno = EINVAL; + return NULL; + } + } } else { ptr = NULL; } - switch (sb->st_mode & S_IFMT) { + if (ent->flags & DIR_ENTRY_FLAG_HARD_LINK) { + n->mode = S_IFLNK | 0777; + n->flags |= FLAG_LINK_IS_HARD; + } + + switch (n->mode & S_IFMT) { case S_IFREG: n->data.file.input_file = ptr; break; @@ -105,7 +128,7 @@ static tree_node_t *mknode(tree_node_t *parent, const char *name, break; case S_IFBLK: case S_IFCHR: - n->data.devno = sb->st_rdev; + n->data.devno = ent->rdev; break; case S_IFDIR: n->link_count = 2; @@ -120,6 +143,11 @@ static tree_node_t *mknode(tree_node_t *parent, const char *name, return NULL; } + if (ent->flags & DIR_ENTRY_FLAG_HARD_LINK) { + n->next_by_type = fs->links_unresolved; + fs->links_unresolved = n; + } + insert_sorted(parent, n); parent->link_count++; return n; @@ -184,20 +212,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; + dir_entry_t ent; if (!create_implicitly) { errno = ENOENT; return NULL; } - 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; + memset(&ent, 0, sizeof(ent)); + ent.mode = S_IFDIR | (fs->defaults.mode & 07777); + ent.uid = fs->defaults.uid; + ent.gid = fs->defaults.gid; + ent.mtime = fs->defaults.mtime; - n = mknode(root, path, len, NULL, &sb); + n = mknode(fs, root, path, len, NULL, &ent); if (n == NULL) return NULL; @@ -211,46 +239,46 @@ tree_node_t *fstree_get_node_by_path(fstree_t *fs, tree_node_t *root, return root; } -tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, - const struct stat *sb, const char *extra) +tree_node_t *fstree_add_generic(fstree_t *fs, const dir_entry_t *ent, + const char *extra) { tree_node_t *child, *parent; const char *name; - if (S_ISLNK(sb->st_mode) && extra == NULL) { + if (S_ISLNK(ent->mode) && extra == NULL) { errno = EINVAL; return NULL; } - if (*path == '\0') { + if (ent->name[0] == '\0') { child = fs->root; assert(child != NULL); goto out; } - parent = fstree_get_node_by_path(fs, fs->root, path, true, true); + parent = fstree_get_node_by_path(fs, fs->root, ent->name, true, true); if (parent == NULL) return NULL; - name = strrchr(path, '/'); - name = (name == NULL ? path : (name + 1)); + name = strrchr(ent->name, '/'); + name = (name == NULL ? ent->name : (name + 1)); child = child_by_name(parent, name, strlen(name)); out: if (child != NULL) { - if (!S_ISDIR(child->mode) || !S_ISDIR(sb->st_mode) || + if (!S_ISDIR(child->mode) || !S_ISDIR(ent->mode) || !(child->flags & FLAG_DIR_CREATED_IMPLICITLY)) { errno = EEXIST; return NULL; } - child->uid = sb->st_uid; - child->gid = sb->st_gid; - child->mode = sb->st_mode; - child->mod_time = sb->st_mtime; + child->uid = ent->uid; + child->gid = ent->gid; + child->mode = ent->mode; + child->mod_time = ent->mtime; child->flags &= ~FLAG_DIR_CREATED_IMPLICITLY; return child; } - return mknode(parent, name, strlen(name), extra, sb); + return mknode(fs, parent, name, strlen(name), extra, ent); } diff --git a/lib/fstree/src/hardlink.c b/lib/fstree/src/hardlink.c index b6467e2..849ec1c 100644 --- a/lib/fstree/src/hardlink.c +++ b/lib/fstree/src/hardlink.c @@ -6,7 +6,6 @@ */ #include "config.h" -#include "util/util.h" #include "fstree.h" #include <string.h> @@ -54,32 +53,6 @@ static int resolve_link(fstree_t *fs, tree_node_t *node) return 0; } -tree_node_t *fstree_add_hard_link(fstree_t *fs, const char *path, - const char *target) -{ - struct stat sb; - tree_node_t *n; - - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFLNK | 0777; - - n = fstree_add_generic(fs, path, &sb, target); - if (n != NULL) { - if (canonicalize_name(n->data.target)) { - free(n); - errno = EINVAL; - return NULL; - } - - n->flags |= FLAG_LINK_IS_HARD; - } - - n->next_by_type = fs->links_unresolved; - fs->links_unresolved = n; - - return n; -} - int fstree_resolve_hard_links(fstree_t *fs) { while (fs->links_unresolved != NULL) { diff --git a/lib/fstree/test/add_by_path.c b/lib/fstree/test/add_by_path.c index 7df1e55..7fb9d37 100644 --- a/lib/fstree/test/add_by_path.c +++ b/lib/fstree/test/add_by_path.c @@ -10,11 +10,24 @@ #include "common.h" #include "util/test.h" +static dir_entry_t *mkentry(const char *name, sqfs_u16 mode, sqfs_u32 uid, + sqfs_u32 gid) +{ + dir_entry_t *ent = calloc(1, sizeof(*ent) + strlen(name) + 1); + + TEST_NOT_NULL(ent); + strcpy(ent->name, name); + ent->mode = mode; + ent->uid = uid; + ent->gid = gid; + return ent; +} + int main(int argc, char **argv) { fstree_defaults_t fsd; tree_node_t *a, *b; - struct stat sb; + dir_entry_t *ent; fstree_t fs; char *opts; (void)argc; (void)argv; @@ -23,20 +36,16 @@ int main(int argc, char **argv) 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; - sb.st_uid = 1000; - sb.st_gid = 100; - TEST_EQUAL_UI(fs.root->link_count, 2); - a = fstree_add_generic(&fs, "dir", &sb, NULL); + ent = mkentry("dir", S_IFDIR | 0750, 1000, 100); + a = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(a); TEST_STR_EQUAL(a->name, "dir"); - TEST_EQUAL_UI(a->mode, sb.st_mode); - TEST_EQUAL_UI(a->uid, sb.st_uid); - TEST_EQUAL_UI(a->gid, sb.st_gid); + TEST_EQUAL_UI(a->mode, (S_IFDIR | 0750)); + TEST_EQUAL_UI(a->uid, 1000); + TEST_EQUAL_UI(a->gid, 100); TEST_ASSERT(a->parent == fs.root); TEST_EQUAL_UI(a->link_count, 2); TEST_NULL(a->next); @@ -44,61 +53,68 @@ int main(int argc, char **argv) TEST_EQUAL_UI(fs.root->link_count, 3); TEST_ASSERT(!(a->flags & FLAG_DIR_CREATED_IMPLICITLY)); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFBLK | 0640; - sb.st_rdev = 1234; - - b = fstree_add_generic(&fs, "blkdev", &sb, NULL); + ent = mkentry("blkdev", S_IFBLK | 0640, 0, 0); + ent->rdev = 1234; + b = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(b); TEST_ASSERT(b != a); TEST_STR_EQUAL(b->name, "blkdev"); - TEST_EQUAL_UI(b->mode, sb.st_mode); - TEST_EQUAL_UI(b->uid, sb.st_uid); - TEST_EQUAL_UI(b->gid, sb.st_gid); + TEST_EQUAL_UI(b->mode, (S_IFBLK | 0640)); + TEST_EQUAL_UI(b->uid, 0); + TEST_EQUAL_UI(b->gid, 0); TEST_ASSERT(b->parent == fs.root); TEST_EQUAL_UI(b->link_count, 1); - TEST_EQUAL_UI(b->data.devno, sb.st_rdev); + TEST_EQUAL_UI(b->data.devno, 1234); TEST_ASSERT(b->next == a); TEST_EQUAL_UI(fs.root->link_count, 4); TEST_ASSERT(fs.root->data.children == b); - TEST_NULL(fstree_add_generic(&fs, "blkdev/foo", &sb, NULL)); + ent = mkentry("blkdev/foo", S_IFBLK | 0640, 0, 0); + TEST_NULL(fstree_add_generic(&fs, ent, NULL)); TEST_EQUAL_UI(errno, ENOTDIR); + free(ent); - TEST_NULL(fstree_add_generic(&fs, "dir", &sb, NULL)); + ent = mkentry("dir", S_IFBLK | 0640, 0, 0); + TEST_NULL(fstree_add_generic(&fs, ent, NULL)); TEST_EQUAL_UI(errno, EEXIST); + free(ent); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFDIR | 0755; - TEST_NULL(fstree_add_generic(&fs, "dir", &sb, NULL)); + ent = mkentry("dir", S_IFDIR | 0755, 0, 0); + TEST_NULL(fstree_add_generic(&fs, ent, NULL)); TEST_EQUAL_UI(errno, EEXIST); + free(ent); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFCHR | 0444; - b = fstree_add_generic(&fs, "dir/chrdev", &sb, NULL); + ent = mkentry("dir/chrdev", S_IFCHR | 0444, 0, 0); + ent->rdev = 5678; + b = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(b); - TEST_EQUAL_UI(b->mode, sb.st_mode); - TEST_EQUAL_UI(b->uid, sb.st_uid); - TEST_EQUAL_UI(b->gid, sb.st_gid); + TEST_EQUAL_UI(b->mode, (S_IFCHR | 0444)); + TEST_EQUAL_UI(b->uid, 0); + TEST_EQUAL_UI(b->gid, 0); TEST_EQUAL_UI(b->link_count, 1); TEST_ASSERT(b->parent == a); - TEST_EQUAL_UI(b->data.devno, sb.st_rdev); + TEST_EQUAL_UI(b->data.devno, 5678); TEST_NULL(b->next); TEST_ASSERT(a->data.children == b); TEST_EQUAL_UI(a->link_count, 3); TEST_EQUAL_UI(fs.root->link_count, 4); - b = fstree_add_generic(&fs, "dir/foo/chrdev", &sb, NULL); + ent = mkentry("dir/foo/chrdev", S_IFCHR | 0444, 0, 0); + ent->rdev = 91011; + b = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(b); TEST_NULL(b->next); - TEST_EQUAL_UI(b->mode, sb.st_mode); - TEST_EQUAL_UI(b->uid, sb.st_uid); - TEST_EQUAL_UI(b->gid, sb.st_gid); + TEST_EQUAL_UI(b->mode, (S_IFCHR | 0444)); + TEST_EQUAL_UI(b->uid, 0); + TEST_EQUAL_UI(b->gid, 0); TEST_EQUAL_UI(b->link_count, 1); TEST_ASSERT(b->parent != a); TEST_ASSERT(b->parent->parent == a); - TEST_EQUAL_UI(b->data.devno, sb.st_rdev); + TEST_EQUAL_UI(b->data.devno, 91011); TEST_NULL(b->next); TEST_EQUAL_UI(a->link_count, 4); @@ -112,25 +128,22 @@ int main(int argc, char **argv) TEST_EQUAL_UI(b->gid, 42); TEST_EQUAL_UI(b->link_count, 3); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFDIR | 0750; - sb.st_uid = 1000; - sb.st_gid = 100; - - a = fstree_add_generic(&fs, "dir/foo", &sb, NULL); + ent = mkentry("dir/foo", S_IFDIR | 0755, 1000, 100); + a = fstree_add_generic(&fs, ent, NULL); TEST_NOT_NULL(a); TEST_ASSERT(a == b); TEST_ASSERT(!(a->flags & FLAG_DIR_CREATED_IMPLICITLY)); - TEST_EQUAL_UI(a->mode, sb.st_mode); - TEST_EQUAL_UI(a->uid, sb.st_uid); - TEST_EQUAL_UI(a->gid, sb.st_gid); + TEST_EQUAL_UI(a->mode, (S_IFDIR | 0755)); + TEST_EQUAL_UI(a->uid, 1000); + TEST_EQUAL_UI(a->gid, 100); TEST_EQUAL_UI(a->link_count, 3); TEST_EQUAL_UI(a->parent->link_count, 4); TEST_EQUAL_UI(fs.root->link_count, 4); - TEST_NULL(fstree_add_generic(&fs, "dir/foo", &sb, NULL)); + TEST_NULL(fstree_add_generic(&fs, ent, NULL)); TEST_EQUAL_UI(errno, EEXIST); + free(ent); fstree_cleanup(&fs); return EXIT_SUCCESS; diff --git a/lib/fstree/test/fstree_sort.c b/lib/fstree/test/fstree_sort.c index 135976d..82d6e9d 100644 --- a/lib/fstree/test/fstree_sort.c +++ b/lib/fstree/test/fstree_sort.c @@ -10,43 +10,55 @@ #include "common.h" #include "util/test.h" +static tree_node_t *mkentry(fstree_t *fs, const char *name) +{ + dir_entry_t *ent = calloc(1, sizeof(*ent) + strlen(name) + 1); + tree_node_t *out; + + TEST_NOT_NULL(ent); + strcpy(ent->name, name); + ent->mode = S_IFBLK | 0600; + ent->rdev = 1337; + + out = fstree_add_generic(fs, ent, NULL); + free(ent); + TEST_NOT_NULL(out); + + return out; +} + 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; (void)argc; (void)argv; - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFBLK | 0600; - sb.st_rdev = 1337; - /* in order */ TEST_ASSERT(parse_fstree_defaults(&fsd, NULL) == 0); ret = fstree_init(&fs, &fsd); TEST_EQUAL_I(ret, 0); - a = fstree_add_generic(&fs, "a", &sb, NULL); + a = mkentry(&fs, "a"); TEST_NOT_NULL(a); TEST_ASSERT(fs.root->data.children == a); TEST_NULL(a->next); - b = fstree_add_generic(&fs, "b", &sb, NULL); + b = mkentry(&fs, "b"); TEST_NOT_NULL(a); TEST_ASSERT(fs.root->data.children == a); TEST_ASSERT(a->next == b); TEST_NULL(b->next); - c = fstree_add_generic(&fs, "c", &sb, NULL); + c = mkentry(&fs, "c"); TEST_NOT_NULL(c); TEST_ASSERT(fs.root->data.children == a); TEST_ASSERT(a->next == b); TEST_ASSERT(b->next == c); TEST_NULL(c->next); - d = fstree_add_generic(&fs, "d", &sb, NULL); + d = mkentry(&fs, "d"); TEST_NOT_NULL(d); TEST_ASSERT(fs.root->data.children == a); TEST_ASSERT(a->next == b); @@ -60,25 +72,25 @@ int main(int argc, char **argv) ret = fstree_init(&fs, &fsd); TEST_EQUAL_I(ret, 0); - d = fstree_add_generic(&fs, "d", &sb, NULL); + d = mkentry(&fs, "d"); TEST_NOT_NULL(d); TEST_ASSERT(fs.root->data.children == d); TEST_NULL(d->next); - c = fstree_add_generic(&fs, "c", &sb, NULL); + c = mkentry(&fs, "c"); TEST_NOT_NULL(c); TEST_ASSERT(fs.root->data.children == c); TEST_ASSERT(c->next == d); TEST_NULL(d->next); - b = fstree_add_generic(&fs, "b", &sb, NULL); + b = mkentry(&fs, "b"); TEST_NOT_NULL(b); TEST_ASSERT(fs.root->data.children == b); TEST_ASSERT(b->next == c); TEST_ASSERT(c->next == d); TEST_NULL(d->next); - a = fstree_add_generic(&fs, "a", &sb, NULL); + a = mkentry(&fs, "a"); TEST_NOT_NULL(a); TEST_ASSERT(fs.root->data.children == a); TEST_ASSERT(a->next == b); diff --git a/lib/fstree/test/gen_inode_numbers.c b/lib/fstree/test/gen_inode_numbers.c index 9e046a8..ae7198e 100644 --- a/lib/fstree/test/gen_inode_numbers.c +++ b/lib/fstree/test/gen_inode_numbers.c @@ -12,12 +12,16 @@ static tree_node_t *gen_node(fstree_t *fs, const char *path) { - struct stat sb; + dir_entry_t *ent = calloc(1, sizeof(*ent) + strlen(path) + 1); + tree_node_t *ret; - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFDIR | 0755; + TEST_NOT_NULL(ent); + strcpy(ent->name, path); + ent->mode = S_IFDIR | 0755; - return fstree_add_generic(fs, path, &sb, NULL); + ret = fstree_add_generic(fs, ent, NULL); + free(ent); + return ret; } static void check_children_before_root(tree_node_t *root) diff --git a/lib/fstree/test/get_path.c b/lib/fstree/test/get_path.c index 572cde7..a158764 100644 --- a/lib/fstree/test/get_path.c +++ b/lib/fstree/test/get_path.c @@ -10,11 +10,23 @@ #include "common.h" #include "util/test.h" +static dir_entry_t *mkentry(const char *name) +{ + dir_entry_t *ent = calloc(1, sizeof(*ent) + strlen(name) + 1); + TEST_NOT_NULL(ent); + + strcpy(ent->name, name); + ent->mode = S_IFDIR | 0750; + ent->uid = 1000; + ent->gid = 100; + return ent; +} + int main(int argc, char **argv) { tree_node_t *a, *b, *c, *d; fstree_defaults_t fsd; - struct stat sb; + dir_entry_t *ent; fstree_t fs; char *str; (void)argc; (void)argv; @@ -22,15 +34,18 @@ int main(int argc, char **argv) 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; - sb.st_uid = 1000; - sb.st_gid = 100; - - a = fstree_add_generic(&fs, "foo", &sb, NULL); - b = fstree_add_generic(&fs, "foo/bar", &sb, NULL); - c = fstree_add_generic(&fs, "foo/bar/baz", &sb, NULL); - d = fstree_add_generic(&fs, "foo/bar/baz/dir", &sb, NULL); + ent = mkentry("foo"); + a = fstree_add_generic(&fs, ent, NULL); + free(ent); + ent = mkentry("foo/bar"); + b = fstree_add_generic(&fs, ent, NULL); + free(ent); + ent = mkentry("foo/bar/baz"); + c = fstree_add_generic(&fs, ent, NULL); + free(ent); + ent = mkentry("foo/bar/baz/dir"); + d = fstree_add_generic(&fs, ent, NULL); + free(ent); str = fstree_get_path(fs.root); TEST_NOT_NULL(str); diff --git a/lib/fstree/test/mknode_dir.c b/lib/fstree/test/mknode_dir.c index c2812ac..20cba45 100644 --- a/lib/fstree/test/mknode_dir.c +++ b/lib/fstree/test/mknode_dir.c @@ -9,11 +9,25 @@ #include "fstree.h" #include "util/test.h" +static dir_entry_t *mkentry(const char *name) +{ + dir_entry_t *ent = calloc(1, sizeof(*ent) + strlen(name) + 1); + + TEST_NOT_NULL(ent); + strcpy(ent->name, name); + ent->mode = S_IFDIR | 0654; + ent->uid = 123; + ent->gid = 456; + ent->rdev = 789; + ent->size = 4096; + return ent; +} + int main(int argc, char **argv) { fstree_defaults_t defaults; tree_node_t *root, *a, *b; - struct stat sb; + dir_entry_t *ent; fstree_t fs; int ret; (void)argc; (void)argv; @@ -22,26 +36,23 @@ int main(int argc, char **argv) ret = fstree_init(&fs, &defaults); TEST_EQUAL_I(ret, 0); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFDIR | 0654; - sb.st_uid = 123; - sb.st_gid = 456; - sb.st_rdev = 789; - sb.st_size = 4096; - - root = fstree_add_generic(&fs, "rootdir", &sb, NULL); + ent = mkentry("rootdir"); + root = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(root); TEST_ASSERT(root->parent == fs.root); - TEST_EQUAL_UI(root->uid, sb.st_uid); - TEST_EQUAL_UI(root->gid, sb.st_gid); - TEST_EQUAL_UI(root->mode, sb.st_mode); + TEST_EQUAL_UI(root->uid, 123); + TEST_EQUAL_UI(root->gid, 456); + TEST_EQUAL_UI(root->mode, (S_IFDIR | 0654)); TEST_EQUAL_UI(root->link_count, 2); TEST_ASSERT(root->name >= (char *)root->payload); TEST_STR_EQUAL(root->name, "rootdir"); TEST_NULL(root->data.children); TEST_NULL(root->next); - a = fstree_add_generic(&fs, "rootdir/adir", &sb, NULL); + ent = mkentry("rootdir/adir"); + a = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(a); TEST_ASSERT(a->parent == root); TEST_NULL(a->next); @@ -51,7 +62,9 @@ int main(int argc, char **argv) TEST_ASSERT(root->parent == fs.root); TEST_NULL(root->next); - b = fstree_add_generic(&fs, "rootdir/bdir", &sb, NULL); + ent = mkentry("rootdir/bdir"); + b = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(b); TEST_ASSERT(a->parent == root); TEST_ASSERT(b->parent == root); diff --git a/lib/fstree/test/mknode_reg.c b/lib/fstree/test/mknode_reg.c deleted file mode 100644 index 3dc6eb3..0000000 --- a/lib/fstree/test/mknode_reg.c +++ /dev/null @@ -1,47 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * mknode_reg.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "config.h" - -#include "fstree.h" -#include "util/test.h" - -int main(int argc, char **argv) -{ - fstree_defaults_t defaults; - tree_node_t *node; - struct stat sb; - fstree_t fs; - int ret; - (void)argc; (void)argv; - - memset(&defaults, 0, sizeof(defaults)); - ret = fstree_init(&fs, &defaults); - TEST_EQUAL_I(ret, 0); - - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFREG | 0654; - sb.st_uid = 123; - sb.st_gid = 456; - sb.st_rdev = 789; - sb.st_size = 4096; - - node = fstree_add_generic(&fs, "filename", &sb, "input"); - TEST_NOT_NULL(node); - TEST_ASSERT(node->parent == fs.root); - TEST_EQUAL_UI(node->uid, sb.st_uid); - TEST_EQUAL_UI(node->gid, sb.st_gid); - TEST_EQUAL_UI(node->mode, sb.st_mode); - TEST_EQUAL_UI(node->link_count, 1); - TEST_ASSERT((char *)node->name >= (char *)node->payload); - TEST_ASSERT(node->data.file.input_file >= (char *)node->payload); - TEST_ASSERT(node->data.file.input_file >= node->name + 8); - TEST_STR_EQUAL(node->name, "filename"); - TEST_STR_EQUAL(node->data.file.input_file, "input"); - - fstree_cleanup(&fs); - return EXIT_SUCCESS; -} diff --git a/lib/fstree/test/mknode_simple.c b/lib/fstree/test/mknode_simple.c index a96ee42..2a18121 100644 --- a/lib/fstree/test/mknode_simple.c +++ b/lib/fstree/test/mknode_simple.c @@ -9,11 +9,25 @@ #include "fstree.h" #include "util/test.h" +static dir_entry_t *mkentry(const char *name, sqfs_u16 mode) +{ + dir_entry_t *ent = calloc(1, sizeof(*ent) + strlen(name) + 1); + + TEST_NOT_NULL(ent); + strcpy(ent->name, name); + ent->mode = mode | 0654; + ent->uid = 123; + ent->gid = 456; + ent->rdev = 789; + ent->size = 1337; + return ent; +} + int main(int argc, char **argv) { fstree_defaults_t defaults; tree_node_t *node; - struct stat sb; + dir_entry_t *ent; fstree_t fs; int ret; (void)argc; (void)argv; @@ -22,21 +36,16 @@ int main(int argc, char **argv) ret = fstree_init(&fs, &defaults); TEST_EQUAL_I(ret, 0); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFSOCK | 0654; - sb.st_uid = 123; - sb.st_gid = 456; - sb.st_rdev = 789; - sb.st_size = 1337; - - node = fstree_add_generic(&fs, "/sockfile", &sb, "target"); + ent = mkentry("/sockfile", S_IFSOCK); + node = fstree_add_generic(&fs, ent, "target"); + free(ent); TEST_NOT_NULL(node); TEST_ASSERT(node->parent == fs.root); TEST_ASSERT((char *)node->name >= (char *)node->payload); TEST_STR_EQUAL(node->name, "sockfile"); - TEST_EQUAL_UI(node->uid, sb.st_uid); - TEST_EQUAL_UI(node->gid, sb.st_gid); - TEST_EQUAL_UI(node->mode, sb.st_mode); + TEST_EQUAL_UI(node->uid, 123); + TEST_EQUAL_UI(node->gid, 456); + TEST_EQUAL_UI(node->mode, (S_IFSOCK | 0654)); TEST_EQUAL_UI(node->link_count, 1); TEST_NULL(node->data.target); TEST_EQUAL_UI(node->data.devno, 0); @@ -45,21 +54,16 @@ int main(int argc, char **argv) ret = fstree_init(&fs, &defaults); TEST_EQUAL_I(ret, 0); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFIFO | 0654; - sb.st_uid = 123; - sb.st_gid = 456; - sb.st_rdev = 789; - sb.st_size = 1337; - - node = fstree_add_generic(&fs, "/fifo", &sb, "target"); + ent = mkentry("/fifo", S_IFIFO); + node = fstree_add_generic(&fs, ent, "target"); + free(ent); TEST_NOT_NULL(node); TEST_ASSERT(node->parent == fs.root); TEST_ASSERT((char *)node->name >= (char *)node->payload); TEST_STR_EQUAL(node->name, "fifo"); - TEST_EQUAL_UI(node->uid, sb.st_uid); - TEST_EQUAL_UI(node->gid, sb.st_gid); - TEST_EQUAL_UI(node->mode, sb.st_mode); + TEST_EQUAL_UI(node->uid, 123); + TEST_EQUAL_UI(node->gid, 456); + TEST_EQUAL_UI(node->mode, (S_IFIFO | 0654)); TEST_EQUAL_UI(node->link_count, 1); TEST_NULL(node->data.target); TEST_EQUAL_UI(node->data.devno, 0); @@ -68,45 +72,95 @@ int main(int argc, char **argv) ret = fstree_init(&fs, &defaults); TEST_EQUAL_I(ret, 0); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFBLK | 0654; - sb.st_uid = 123; - sb.st_gid = 456; - sb.st_rdev = 789; - sb.st_size = 1337; - - node = fstree_add_generic(&fs, "/blkdev", &sb, NULL); + ent = mkentry("/blkdev", S_IFBLK); + node = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(node); TEST_ASSERT(node->parent == fs.root); TEST_ASSERT((char *)node->name >= (char *)node->payload); TEST_STR_EQUAL(node->name, "blkdev"); - TEST_EQUAL_UI(node->uid, sb.st_uid); - TEST_EQUAL_UI(node->gid, sb.st_gid); - TEST_EQUAL_UI(node->mode, sb.st_mode); + TEST_EQUAL_UI(node->uid, 123); + TEST_EQUAL_UI(node->gid, 456); + TEST_EQUAL_UI(node->mode, (S_IFBLK | 0654)); TEST_EQUAL_UI(node->link_count, 1); - TEST_EQUAL_UI(node->data.devno, sb.st_rdev); + TEST_EQUAL_UI(node->data.devno, 789); fstree_cleanup(&fs); ret = fstree_init(&fs, &defaults); TEST_EQUAL_I(ret, 0); - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFCHR | 0654; - sb.st_uid = 123; - sb.st_gid = 456; - sb.st_rdev = 789; - sb.st_size = 1337; - - node = fstree_add_generic(&fs, "/chardev", &sb, NULL); + ent = mkentry("/chardev", S_IFCHR); + node = fstree_add_generic(&fs, ent, NULL); + free(ent); TEST_NOT_NULL(node); TEST_ASSERT(node->parent == fs.root); TEST_ASSERT((char *)node->name >= (char *)node->payload); TEST_STR_EQUAL(node->name, "chardev"); - TEST_EQUAL_UI(node->uid, sb.st_uid); - TEST_EQUAL_UI(node->gid, sb.st_gid); - TEST_EQUAL_UI(node->mode, sb.st_mode); + TEST_EQUAL_UI(node->uid, 123); + TEST_EQUAL_UI(node->gid, 456); + TEST_EQUAL_UI(node->mode, (S_IFCHR | 0654)); + TEST_EQUAL_UI(node->link_count, 1); + TEST_EQUAL_UI(node->data.devno, 789); + fstree_cleanup(&fs); + + memset(&defaults, 0, sizeof(defaults)); + ret = fstree_init(&fs, &defaults); + TEST_EQUAL_I(ret, 0); + + ent = mkentry("/symlink", S_IFLNK); + node = fstree_add_generic(&fs, ent, "target"); + free(ent); + TEST_NOT_NULL(node); + TEST_ASSERT(node->parent == fs.root); + TEST_EQUAL_UI(node->uid, 123); + TEST_EQUAL_UI(node->gid, 456); + TEST_EQUAL_UI(node->mode, (S_IFLNK | 0777)); + TEST_EQUAL_UI(node->link_count, 1); + TEST_ASSERT((char *)node->name >= (char *)node->payload); + TEST_ASSERT(node->data.target >= (char *)node->payload); + TEST_ASSERT(node->data.target >= node->name + 8); + TEST_STR_EQUAL(node->name, "symlink"); + TEST_STR_EQUAL(node->data.target, "target"); + fstree_cleanup(&fs); + + memset(&defaults, 0, sizeof(defaults)); + ret = fstree_init(&fs, &defaults); + TEST_EQUAL_I(ret, 0); + + ent = mkentry("/foo", S_IFLNK); + node = fstree_add_generic(&fs, ent, ""); + free(ent); + TEST_NOT_NULL(node); + TEST_ASSERT(node->parent == fs.root); + TEST_EQUAL_UI(node->uid, 123); + TEST_EQUAL_UI(node->gid, 456); + TEST_EQUAL_UI(node->mode, (S_IFLNK | 0777)); TEST_EQUAL_UI(node->link_count, 1); - TEST_EQUAL_UI(node->data.devno, sb.st_rdev); + TEST_ASSERT((char *)node->name >= (char *)node->payload); + TEST_ASSERT(node->data.target >= (char *)node->payload); + TEST_ASSERT(node->data.target >= node->name + 3); + TEST_STR_EQUAL(node->name, "foo"); + TEST_STR_EQUAL(node->data.target, ""); + fstree_cleanup(&fs); + + memset(&defaults, 0, sizeof(defaults)); + ret = fstree_init(&fs, &defaults); + TEST_EQUAL_I(ret, 0); + + ent = mkentry("/filename", S_IFREG); + node = fstree_add_generic(&fs, ent, "input"); + free(ent); + TEST_NOT_NULL(node); + TEST_ASSERT(node->parent == fs.root); + TEST_EQUAL_UI(node->uid, 123); + TEST_EQUAL_UI(node->gid, 456); + TEST_EQUAL_UI(node->mode, (S_IFREG | 0654)); + TEST_EQUAL_UI(node->link_count, 1); + TEST_ASSERT((char *)node->name >= (char *)node->payload); + TEST_ASSERT(node->data.file.input_file >= (char *)node->payload); + TEST_ASSERT(node->data.file.input_file >= node->name + 8); + TEST_STR_EQUAL(node->name, "filename"); + TEST_STR_EQUAL(node->data.file.input_file, "input"); fstree_cleanup(&fs); return EXIT_SUCCESS; diff --git a/lib/fstree/test/mknode_slink.c b/lib/fstree/test/mknode_slink.c deleted file mode 100644 index d741746..0000000 --- a/lib/fstree/test/mknode_slink.c +++ /dev/null @@ -1,60 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * mknode_slink.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "config.h" - -#include "fstree.h" -#include "util/test.h" - -int main(int argc, char **argv) -{ - fstree_defaults_t defaults; - tree_node_t *node; - struct stat sb; - fstree_t fs; - int ret; - (void)argc; (void)argv; - - memset(&defaults, 0, sizeof(defaults)); - ret = fstree_init(&fs, &defaults); - TEST_EQUAL_I(ret, 0); - - memset(&sb, 0, sizeof(sb)); - sb.st_mode = S_IFLNK | 0654; - sb.st_uid = 123; - sb.st_gid = 456; - sb.st_rdev = 789; - sb.st_size = 1337; - - node = fstree_add_generic(&fs, "/symlink", &sb, "target"); - TEST_NOT_NULL(node); - TEST_ASSERT(node->parent == fs.root); - TEST_EQUAL_UI(node->uid, sb.st_uid); - TEST_EQUAL_UI(node->gid, sb.st_gid); - TEST_EQUAL_UI(node->mode, S_IFLNK | 0777); - TEST_EQUAL_UI(node->link_count, 1); - TEST_ASSERT((char *)node->name >= (char *)node->payload); - TEST_ASSERT(node->data.target >= (char *)node->payload); - TEST_ASSERT(node->data.target >= node->name + 8); - TEST_STR_EQUAL(node->name, "symlink"); - TEST_STR_EQUAL(node->data.target, "target"); - - node = fstree_add_generic(&fs, "/foo", &sb, ""); - TEST_NOT_NULL(node); - TEST_ASSERT(node->parent == fs.root); - TEST_EQUAL_UI(node->uid, sb.st_uid); - TEST_EQUAL_UI(node->gid, sb.st_gid); - TEST_EQUAL_UI(node->mode, S_IFLNK | 0777); - TEST_EQUAL_UI(node->link_count, 1); - TEST_ASSERT((char *)node->name >= (char *)node->payload); - TEST_ASSERT(node->data.target >= (char *)node->payload); - TEST_ASSERT(node->data.target >= node->name + 3); - TEST_STR_EQUAL(node->name, "foo"); - TEST_STR_EQUAL(node->data.target, ""); - - fstree_cleanup(&fs); - return EXIT_SUCCESS; -} |