diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-05-05 01:25:43 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-05-05 01:26:06 +0200 |
commit | 7ce4b36d517ac5fade36240d293ff784ef6a9305 (patch) | |
tree | f0c246595251d27f62010aca770b25bb5f7ea336 /lib | |
parent | 7a39921d7fff089c87ac183d3c0d6e42e5cbaa04 (diff) |
Internalize fstree_mknode, consolidate fstree functionality
The fstree_mknode function is only used internally, remove the
declaration from the header and internalize it. The code using it is
consolidated into fstree.c.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fstree/Makemodule.am | 4 | ||||
-rw-r--r-- | lib/fstree/src/add_by_path.c | 54 | ||||
-rw-r--r-- | lib/fstree/src/fstree.c | 217 | ||||
-rw-r--r-- | lib/fstree/src/get_by_path.c | 84 | ||||
-rw-r--r-- | lib/fstree/src/mknode.c | 101 | ||||
-rw-r--r-- | lib/fstree/test/fstree_sort.c | 16 | ||||
-rw-r--r-- | lib/fstree/test/gen_inode_numbers.c | 28 | ||||
-rw-r--r-- | lib/fstree/test/mknode_dir.c | 27 | ||||
-rw-r--r-- | lib/fstree/test/mknode_reg.c | 13 | ||||
-rw-r--r-- | lib/fstree/test/mknode_simple.c | 47 | ||||
-rw-r--r-- | lib/fstree/test/mknode_slink.c | 24 |
11 files changed, 301 insertions, 314 deletions
diff --git a/lib/fstree/Makemodule.am b/lib/fstree/Makemodule.am index 833fe51..109af8e 100644 --- a/lib/fstree/Makemodule.am +++ b/lib/fstree/Makemodule.am @@ -1,8 +1,6 @@ libfstree_a_SOURCES = include/fstree.h lib/fstree/src/fstree.c \ lib/fstree/src/post_process.c lib/fstree/src/get_path.c \ - lib/fstree/src/mknode.c lib/fstree/src/hardlink.c \ - lib/fstree/src/add_by_path.c lib/fstree/src/get_by_path.c \ - lib/fstree/src/stats.c + lib/fstree/src/hardlink.c lib/fstree/src/stats.c noinst_LIBRARIES += libfstree.a diff --git a/lib/fstree/src/add_by_path.c b/lib/fstree/src/add_by_path.c deleted file mode 100644 index 344b586..0000000 --- a/lib/fstree/src/add_by_path.c +++ /dev/null @@ -1,54 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * add_by_path.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "config.h" - -#include "fstree.h" - -#include <string.h> -#include <assert.h> -#include <errno.h> - -tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, - const struct stat *sb, const char *extra) -{ - tree_node_t *child, *parent; - const char *name; - - if (*path == '\0') { - child = fs->root; - assert(child != NULL); - goto out; - } - - parent = fstree_get_node_by_path(fs, fs->root, path, true, true); - if (parent == NULL) - return NULL; - - name = strrchr(path, '/'); - name = (name == NULL ? path : (name + 1)); - - child = parent->data.children; - while (child != NULL && strcmp(child->name, name) != 0) - child = child->next; -out: - if (child != NULL) { - if (!S_ISDIR(child->mode) || !S_ISDIR(sb->st_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->flags &= ~FLAG_DIR_CREATED_IMPLICITLY; - return child; - } - - return fstree_mknode(parent, name, strlen(name), extra, sb); -} diff --git a/lib/fstree/src/fstree.c b/lib/fstree/src/fstree.c index bf84c17..43a6ccc 100644 --- a/lib/fstree/src/fstree.c +++ b/lib/fstree/src/fstree.c @@ -9,7 +9,9 @@ #include <string.h> #include <stdlib.h> +#include <assert.h> #include <stdio.h> +#include <errno.h> static void free_recursive(tree_node_t *n) { @@ -27,26 +29,120 @@ static void free_recursive(tree_node_t *n) free(n); } -int fstree_init(fstree_t *fs, const fstree_defaults_t *defaults) +static tree_node_t *child_by_name(tree_node_t *root, const char *name, + size_t len) { - struct stat sb; + tree_node_t *n = root->data.children; - memset(fs, 0, sizeof(*fs)); - fs->defaults = *defaults; + while (n != NULL) { + if (strncmp(n->name, name, len) == 0 && n->name[len] == '\0') + break; + + n = n->next; + } + + return n; +} + +static void insert_sorted(tree_node_t *root, tree_node_t *n) +{ + tree_node_t *it = root->data.children, *prev = NULL; + + while (it != NULL && strcmp(it->name, n->name) < 0) { + prev = it; + it = it->next; + } + + n->parent = root; + n->next = it; + + if (prev == NULL) { + root->data.children = n; + } else { + prev->next = n; + } +} + +static tree_node_t *mknode(tree_node_t *parent, const char *name, + size_t name_len, const char *extra, + const struct stat *sb) +{ + tree_node_t *n; + size_t size; + char *ptr; + + size = sizeof(tree_node_t) + name_len + 1; + if (extra != NULL) + size += strlen(extra) + 1; + + n = calloc(1, size); + if (n == NULL) + 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->link_count = 1; + n->name = (char *)n->payload; + memcpy(n->name, name, name_len); + + if (extra != NULL) { + ptr = n->name + name_len + 1; + strcpy(ptr, extra); + } else { + ptr = NULL; + } + + switch (sb->st_mode & S_IFMT) { + case S_IFREG: + n->data.file.input_file = ptr; + break; + case S_IFLNK: + n->mode = S_IFLNK | 0777; + n->data.target = ptr; + break; + case S_IFBLK: + case S_IFCHR: + n->data.devno = sb->st_rdev; + break; + case S_IFDIR: + n->link_count = 2; + break; + default: + break; + } - 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; + if (parent->link_count == 0xFFFFFFFF) { + free(n); + errno = EMLINK; + return NULL; + } + + insert_sorted(parent, n); + parent->link_count++; + return n; +} - fs->root = fstree_mknode(NULL, "", 0, NULL, &sb); +int fstree_init(fstree_t *fs, const fstree_defaults_t *defaults) +{ + memset(fs, 0, sizeof(*fs)); + fs->defaults = *defaults; + fs->root = calloc(1, sizeof(tree_node_t) + 1); if (fs->root == NULL) { perror("initializing file system tree"); return -1; } + fs->root->xattr_idx = 0xFFFFFFFF; + fs->root->uid = defaults->uid; + fs->root->gid = defaults->gid; + fs->root->mode = S_IFDIR | (defaults->mode & 07777); + fs->root->mod_time = defaults->mtime; + fs->root->link_count = 2; + fs->root->name = (char *)fs->root->payload; fs->root->flags |= FLAG_DIR_CREATED_IMPLICITLY; return 0; } @@ -57,3 +153,104 @@ void fstree_cleanup(fstree_t *fs) free(fs->inodes); memset(fs, 0, sizeof(*fs)); } + +tree_node_t *fstree_get_node_by_path(fstree_t *fs, tree_node_t *root, + const char *path, bool create_implicitly, + bool stop_at_parent) +{ + const char *end; + tree_node_t *n; + size_t len; + + while (*path != '\0') { + while (*path == '/') + ++path; + + if (!S_ISDIR(root->mode)) { + errno = ENOTDIR; + return NULL; + } + + end = strchr(path, '/'); + if (end == NULL) { + if (stop_at_parent) + break; + + len = strlen(path); + } else { + len = end - path; + } + + n = child_by_name(root, path, len); + + if (n == NULL) { + struct stat sb; + + 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; + + n = mknode(root, path, len, NULL, &sb); + if (n == NULL) + return NULL; + + n->flags |= FLAG_DIR_CREATED_IMPLICITLY; + } + + root = n; + path = path + len; + } + + return root; +} + +tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, + const struct stat *sb, const char *extra) +{ + tree_node_t *child, *parent; + const char *name; + + if (S_ISLNK(sb->st_mode) && extra == NULL) { + errno = EINVAL; + return NULL; + } + + if (*path == '\0') { + child = fs->root; + assert(child != NULL); + goto out; + } + + parent = fstree_get_node_by_path(fs, fs->root, path, true, true); + if (parent == NULL) + return NULL; + + name = strrchr(path, '/'); + name = (name == NULL ? path : (name + 1)); + + child = child_by_name(parent, name, strlen(name)); +out: + if (child != NULL) { + if (!S_ISDIR(child->mode) || !S_ISDIR(sb->st_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->flags &= ~FLAG_DIR_CREATED_IMPLICITLY; + return child; + } + + return mknode(parent, name, strlen(name), extra, sb); +} diff --git a/lib/fstree/src/get_by_path.c b/lib/fstree/src/get_by_path.c deleted file mode 100644 index 133f412..0000000 --- a/lib/fstree/src/get_by_path.c +++ /dev/null @@ -1,84 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * get_by_path.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "config.h" - -#include "fstree.h" - -#include <string.h> -#include <errno.h> - -static tree_node_t *child_by_name(tree_node_t *root, const char *name, - size_t len) -{ - tree_node_t *n = root->data.children; - - while (n != NULL) { - if (strncmp(n->name, name, len) == 0 && n->name[len] == '\0') - break; - - n = n->next; - } - - return n; -} - -tree_node_t *fstree_get_node_by_path(fstree_t *fs, tree_node_t *root, - const char *path, bool create_implicitly, - bool stop_at_parent) -{ - const char *end; - tree_node_t *n; - size_t len; - - while (*path != '\0') { - while (*path == '/') - ++path; - - if (!S_ISDIR(root->mode)) { - errno = ENOTDIR; - return NULL; - } - - end = strchr(path, '/'); - if (end == NULL) { - if (stop_at_parent) - break; - - len = strlen(path); - } else { - len = end - path; - } - - n = child_by_name(root, path, len); - - if (n == NULL) { - struct stat sb; - - 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; - - n = fstree_mknode(root, path, len, NULL, &sb); - if (n == NULL) - return NULL; - - n->flags |= FLAG_DIR_CREATED_IMPLICITLY; - } - - root = n; - path = path + len; - } - - return root; -} diff --git a/lib/fstree/src/mknode.c b/lib/fstree/src/mknode.c deleted file mode 100644 index a48cf06..0000000 --- a/lib/fstree/src/mknode.c +++ /dev/null @@ -1,101 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * mknode.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "config.h" -#include "fstree.h" - -#include <string.h> -#include <stdlib.h> -#include <errno.h> - -static void insert_sorted(tree_node_t *root, tree_node_t *n) -{ - tree_node_t *it = root->data.children, *prev = NULL; - - while (it != NULL && strcmp(it->name, n->name) < 0) { - prev = it; - it = it->next; - } - - n->parent = root; - n->next = it; - - if (prev == NULL) { - root->data.children = n; - } else { - prev->next = n; - } -} - -tree_node_t *fstree_mknode(tree_node_t *parent, const char *name, - size_t name_len, const char *extra, - const struct stat *sb) -{ - tree_node_t *n; - size_t size; - char *ptr; - - if (S_ISLNK(sb->st_mode) && extra == NULL) { - errno = EINVAL; - return NULL; - } - - size = sizeof(tree_node_t) + name_len + 1; - if (extra != NULL) - size += strlen(extra) + 1; - - n = calloc(1, size); - if (n == NULL) - 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->link_count = 1; - n->name = (char *)n->payload; - memcpy(n->name, name, name_len); - - if (extra != NULL) { - ptr = n->name + name_len + 1; - strcpy(ptr, extra); - } else { - ptr = NULL; - } - - switch (sb->st_mode & S_IFMT) { - case S_IFREG: - n->data.file.input_file = ptr; - break; - case S_IFLNK: - n->mode = S_IFLNK | 0777; - n->data.target = ptr; - break; - case S_IFBLK: - case S_IFCHR: - n->data.devno = sb->st_rdev; - break; - case S_IFDIR: - n->link_count = 2; - break; - default: - break; - } - - if (parent != NULL) { - if (parent->link_count == 0xFFFFFFFF) { - free(n); - errno = EMLINK; - return NULL; - } - - insert_sorted(parent, n); - parent->link_count++; - } - - return n; -} diff --git a/lib/fstree/test/fstree_sort.c b/lib/fstree/test/fstree_sort.c index fc44899..135976d 100644 --- a/lib/fstree/test/fstree_sort.c +++ b/lib/fstree/test/fstree_sort.c @@ -28,25 +28,25 @@ int main(int argc, char **argv) ret = fstree_init(&fs, &fsd); TEST_EQUAL_I(ret, 0); - a = fstree_mknode(fs.root, "a", 1, NULL, &sb); + a = fstree_add_generic(&fs, "a", &sb, NULL); TEST_NOT_NULL(a); TEST_ASSERT(fs.root->data.children == a); TEST_NULL(a->next); - b = fstree_mknode(fs.root, "b", 1, NULL, &sb); + b = fstree_add_generic(&fs, "b", &sb, NULL); TEST_NOT_NULL(a); TEST_ASSERT(fs.root->data.children == a); TEST_ASSERT(a->next == b); TEST_NULL(b->next); - c = fstree_mknode(fs.root, "c", 1, NULL, &sb); + c = fstree_add_generic(&fs, "c", &sb, NULL); 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_mknode(fs.root, "d", 1, NULL, &sb); + d = fstree_add_generic(&fs, "d", &sb, NULL); TEST_NOT_NULL(d); TEST_ASSERT(fs.root->data.children == a); TEST_ASSERT(a->next == b); @@ -60,25 +60,25 @@ int main(int argc, char **argv) ret = fstree_init(&fs, &fsd); TEST_EQUAL_I(ret, 0); - d = fstree_mknode(fs.root, "d", 1, NULL, &sb); + d = fstree_add_generic(&fs, "d", &sb, NULL); TEST_NOT_NULL(d); TEST_ASSERT(fs.root->data.children == d); TEST_NULL(d->next); - c = fstree_mknode(fs.root, "c", 1, NULL, &sb); + c = fstree_add_generic(&fs, "c", &sb, NULL); TEST_NOT_NULL(c); TEST_ASSERT(fs.root->data.children == c); TEST_ASSERT(c->next == d); TEST_NULL(d->next); - b = fstree_mknode(fs.root, "b", 1, NULL, &sb); + b = fstree_add_generic(&fs, "b", &sb, NULL); 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_mknode(fs.root, "a", 1, NULL, &sb); + a = fstree_add_generic(&fs, "a", &sb, NULL); 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 b65f64b..9e046a8 100644 --- a/lib/fstree/test/gen_inode_numbers.c +++ b/lib/fstree/test/gen_inode_numbers.c @@ -10,14 +10,14 @@ #include "common.h" #include "util/test.h" -static tree_node_t *gen_node(tree_node_t *parent, const char *name) +static tree_node_t *gen_node(fstree_t *fs, const char *path) { struct stat sb; memset(&sb, 0, sizeof(sb)); sb.st_mode = S_IFDIR | 0755; - return fstree_mknode(parent, name, strlen(name), NULL, &sb); + return fstree_add_generic(fs, path, &sb, NULL); } static void check_children_before_root(tree_node_t *root) @@ -63,24 +63,24 @@ int main(int argc, char **argv) // tree with 2 levels under root, fan out 3 TEST_ASSERT(fstree_init(&fs, &fsd) == 0); - a = gen_node(fs.root, "a"); - b = gen_node(fs.root, "b"); - c = gen_node(fs.root, "c"); + a = gen_node(&fs, "a"); + b = gen_node(&fs, "b"); + c = gen_node(&fs, "c"); TEST_NOT_NULL(a); TEST_NOT_NULL(b); TEST_NOT_NULL(c); - TEST_NOT_NULL(gen_node(a, "a_a")); - TEST_NOT_NULL(gen_node(a, "a_b")); - TEST_NOT_NULL(gen_node(a, "a_c")); + TEST_NOT_NULL(gen_node(&fs, "a/a_a")); + TEST_NOT_NULL(gen_node(&fs, "a/a_b")); + TEST_NOT_NULL(gen_node(&fs, "a/a_c")); - TEST_NOT_NULL(gen_node(b, "b_a")); - TEST_NOT_NULL(gen_node(b, "b_b")); - TEST_NOT_NULL(gen_node(b, "b_c")); + TEST_NOT_NULL(gen_node(&fs, "b/b_a")); + TEST_NOT_NULL(gen_node(&fs, "b/b_b")); + TEST_NOT_NULL(gen_node(&fs, "b/b_c")); - TEST_NOT_NULL(gen_node(c, "c_a")); - TEST_NOT_NULL(gen_node(c, "c_b")); - TEST_NOT_NULL(gen_node(c, "c_c")); + TEST_NOT_NULL(gen_node(&fs, "c/c_a")); + TEST_NOT_NULL(gen_node(&fs, "c/c_b")); + TEST_NOT_NULL(gen_node(&fs, "c/c_c")); fstree_post_process(&fs); TEST_EQUAL_UI(fs.unique_inode_count, 13); diff --git a/lib/fstree/test/mknode_dir.c b/lib/fstree/test/mknode_dir.c index a5f6d9a..c2812ac 100644 --- a/lib/fstree/test/mknode_dir.c +++ b/lib/fstree/test/mknode_dir.c @@ -11,12 +11,17 @@ int main(int argc, char **argv) { + fstree_defaults_t defaults; tree_node_t *root, *a, *b; struct stat sb; fstree_t fs; + int ret; (void)argc; (void)argv; - memset(&fs, 0, sizeof(fs)); + memset(&defaults, 0, sizeof(defaults)); + 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; @@ -24,7 +29,9 @@ int main(int argc, char **argv) sb.st_rdev = 789; sb.st_size = 4096; - root = fstree_mknode(NULL, "rootdir", 7, NULL, &sb); + root = fstree_add_generic(&fs, "rootdir", &sb, NULL); + 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); @@ -32,19 +39,20 @@ int main(int argc, char **argv) TEST_ASSERT(root->name >= (char *)root->payload); TEST_STR_EQUAL(root->name, "rootdir"); TEST_NULL(root->data.children); - TEST_NULL(root->parent); TEST_NULL(root->next); - a = fstree_mknode(root, "adir", 4, NULL, &sb); + a = fstree_add_generic(&fs, "rootdir/adir", &sb, NULL); + TEST_NOT_NULL(a); TEST_ASSERT(a->parent == root); TEST_NULL(a->next); TEST_EQUAL_UI(a->link_count, 2); TEST_EQUAL_UI(root->link_count, 3); TEST_ASSERT(root->data.children == a); - TEST_NULL(root->parent); + TEST_ASSERT(root->parent == fs.root); TEST_NULL(root->next); - b = fstree_mknode(root, "bdir", 4, NULL, &sb); + b = fstree_add_generic(&fs, "rootdir/bdir", &sb, NULL); + TEST_NOT_NULL(b); TEST_ASSERT(a->parent == root); TEST_ASSERT(b->parent == root); TEST_EQUAL_UI(b->link_count, 2); @@ -52,12 +60,9 @@ int main(int argc, char **argv) TEST_ASSERT(a->next == b); TEST_EQUAL_UI(root->link_count, 4); TEST_NULL(b->next); - TEST_NULL(root->parent); + TEST_ASSERT(root->parent == fs.root); TEST_NULL(root->next); - free(root); - free(a); - free(b); - + fstree_cleanup(&fs); return EXIT_SUCCESS; } diff --git a/lib/fstree/test/mknode_reg.c b/lib/fstree/test/mknode_reg.c index 368720f..3dc6eb3 100644 --- a/lib/fstree/test/mknode_reg.c +++ b/lib/fstree/test/mknode_reg.c @@ -11,12 +11,16 @@ 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(&fs, 0, sizeof(fs)); + 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; @@ -25,18 +29,19 @@ int main(int argc, char **argv) sb.st_rdev = 789; sb.st_size = 4096; - node = fstree_mknode(NULL, "filename", 8, "input", &sb); + 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_NULL(node->parent); 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"); - free(node); + fstree_cleanup(&fs); return EXIT_SUCCESS; } diff --git a/lib/fstree/test/mknode_simple.c b/lib/fstree/test/mknode_simple.c index c7efb49..a96ee42 100644 --- a/lib/fstree/test/mknode_simple.c +++ b/lib/fstree/test/mknode_simple.c @@ -11,12 +11,17 @@ 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(&fs, 0, sizeof(fs)); + memset(&defaults, 0, sizeof(defaults)); + 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; @@ -24,19 +29,22 @@ int main(int argc, char **argv) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "sockfile", 8, NULL, &sb); + node = fstree_add_generic(&fs, "/sockfile", &sb, "target"); + 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->link_count, 1); - TEST_NULL(node->parent); TEST_NULL(node->data.target); TEST_EQUAL_UI(node->data.devno, 0); - free(node); - memset(&fs, 0, sizeof(fs)); + fstree_cleanup(&fs); + 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; @@ -44,19 +52,22 @@ int main(int argc, char **argv) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "fifo", 4, NULL, &sb); + node = fstree_add_generic(&fs, "/fifo", &sb, "target"); + 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->link_count, 1); - TEST_NULL(node->parent); TEST_NULL(node->data.target); TEST_EQUAL_UI(node->data.devno, 0); - free(node); - memset(&fs, 0, sizeof(fs)); + fstree_cleanup(&fs); + 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; @@ -64,7 +75,9 @@ int main(int argc, char **argv) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "blkdev", 6, NULL, &sb); + node = fstree_add_generic(&fs, "/blkdev", &sb, NULL); + 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); @@ -72,10 +85,11 @@ int main(int argc, char **argv) TEST_EQUAL_UI(node->mode, sb.st_mode); TEST_EQUAL_UI(node->link_count, 1); TEST_EQUAL_UI(node->data.devno, sb.st_rdev); - TEST_NULL(node->parent); - free(node); - memset(&fs, 0, sizeof(fs)); + 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; @@ -83,7 +97,9 @@ int main(int argc, char **argv) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "chardev", 7, NULL, &sb); + node = fstree_add_generic(&fs, "/chardev", &sb, NULL); + 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); @@ -91,8 +107,7 @@ int main(int argc, char **argv) TEST_EQUAL_UI(node->mode, sb.st_mode); TEST_EQUAL_UI(node->link_count, 1); TEST_EQUAL_UI(node->data.devno, sb.st_rdev); - TEST_NULL(node->parent); - free(node); + fstree_cleanup(&fs); return EXIT_SUCCESS; } diff --git a/lib/fstree/test/mknode_slink.c b/lib/fstree/test/mknode_slink.c index c50a0ba..d741746 100644 --- a/lib/fstree/test/mknode_slink.c +++ b/lib/fstree/test/mknode_slink.c @@ -11,12 +11,17 @@ 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(&fs, 0, sizeof(fs)); + 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; @@ -24,31 +29,32 @@ int main(int argc, char **argv) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "symlink", 7, "target", &sb); + 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_NULL(node->parent); 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"); - free(node); - node = fstree_mknode(NULL, "symlink", 7, "", &sb); + 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_NULL(node->parent); 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_ASSERT(node->data.target >= node->name + 3); + TEST_STR_EQUAL(node->name, "foo"); TEST_STR_EQUAL(node->data.target, ""); - free(node); + fstree_cleanup(&fs); return EXIT_SUCCESS; } |