From 625368eb5bcb9954ad190af50962e6b7c2fd9c4c Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 27 Sep 2019 16:43:11 +0200 Subject: Cleanup: remove most of the payload pointer magic from libfstree Now that dir_info_t and file_info_t have reasonably small, use them in tree_node_t directly instead of doing pointer arithmetic magic on the payload area. Signed-off-by: David Oberhollenzer --- lib/fstree/add_by_path.c | 8 ++--- lib/fstree/fstree.c | 6 ++-- lib/fstree/fstree_from_dir.c | 2 +- lib/fstree/fstree_sort.c | 4 +-- lib/fstree/gen_file_list.c | 6 ++-- lib/fstree/gen_inode_table.c | 8 ++--- lib/fstree/mknode.c | 73 +++++++++++++------------------------------- 7 files changed, 38 insertions(+), 69 deletions(-) (limited to 'lib/fstree') diff --git a/lib/fstree/add_by_path.c b/lib/fstree/add_by_path.c index 87fa6ff..bd47a76 100644 --- a/lib/fstree/add_by_path.c +++ b/lib/fstree/add_by_path.c @@ -14,7 +14,7 @@ static tree_node_t *child_by_name(tree_node_t *root, const char *name, size_t len) { - tree_node_t *n = root->data.dir->children; + tree_node_t *n = root->data.dir.children; while (n != NULL) { if (strncmp(n->name, name, len) == 0 && n->name[len] == '\0') @@ -50,7 +50,7 @@ static tree_node_t *get_parent_node(fstree_t *fs, tree_node_t *root, if (n == NULL) return NULL; - n->data.dir->created_implicitly = true; + n->data.dir.created_implicitly = true; } root = n; @@ -76,7 +76,7 @@ tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, child = child_by_name(parent, name, strlen(name)); if (child != NULL) { if (!S_ISDIR(child->mode) || !S_ISDIR(sb->st_mode) || - !child->data.dir->created_implicitly) { + !child->data.dir.created_implicitly) { errno = EEXIST; return NULL; } @@ -85,7 +85,7 @@ tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, child->gid = sb->st_gid; child->mode = sb->st_mode; child->mod_time = sb->st_mtime; - child->data.dir->created_implicitly = false; + child->data.dir.created_implicitly = false; return child; } diff --git a/lib/fstree/fstree.c b/lib/fstree/fstree.c index febf8af..c7c4a23 100644 --- a/lib/fstree/fstree.c +++ b/lib/fstree/fstree.c @@ -95,9 +95,9 @@ static void free_recursive(tree_node_t *n) tree_node_t *it; if (S_ISDIR(n->mode)) { - while (n->data.dir->children != NULL) { - it = n->data.dir->children; - n->data.dir->children = it->next; + while (n->data.dir.children != NULL) { + it = n->data.dir.children; + n->data.dir.children = it->next; free_recursive(it); } diff --git a/lib/fstree/fstree_from_dir.c b/lib/fstree/fstree_from_dir.c index 86f7fd8..c31b1a0 100644 --- a/lib/fstree/fstree_from_dir.c +++ b/lib/fstree/fstree_from_dir.c @@ -200,7 +200,7 @@ static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart, closedir(dir); - for (n = root->data.dir->children; n != NULL; n = n->next) { + for (n = root->data.dir.children; n != NULL; n = n->next) { if (S_ISDIR(n->mode)) { if (pushd(n->name)) return -1; diff --git a/lib/fstree/fstree_sort.c b/lib/fstree/fstree_sort.c index 53fb58a..9cb177b 100644 --- a/lib/fstree/fstree_sort.c +++ b/lib/fstree/fstree_sort.c @@ -63,9 +63,9 @@ tree_node_t *tree_node_list_sort(tree_node_t *head) void tree_node_sort_recursive(tree_node_t *n) { - n->data.dir->children = tree_node_list_sort(n->data.dir->children); + n->data.dir.children = tree_node_list_sort(n->data.dir.children); - for (n = n->data.dir->children; n != NULL; n = n->next) { + for (n = n->data.dir.children; n != NULL; n = n->next) { if (S_ISDIR(n->mode)) tree_node_sort_recursive(n); } diff --git a/lib/fstree/gen_file_list.c b/lib/fstree/gen_file_list.c index 6b10b00..f20c131 100644 --- a/lib/fstree/gen_file_list.c +++ b/lib/fstree/gen_file_list.c @@ -10,14 +10,14 @@ static file_info_t *file_list_dfs(tree_node_t *n) { if (S_ISREG(n->mode)) { - n->data.file->next = NULL; - return n->data.file; + n->data.file.next = NULL; + return &n->data.file; } if (S_ISDIR(n->mode)) { file_info_t *list = NULL, *last = NULL; - for (n = n->data.dir->children; n != NULL; n = n->next) { + for (n = n->data.dir.children; n != NULL; n = n->next) { if (list == NULL) { list = file_list_dfs(n); if (list == NULL) diff --git a/lib/fstree/gen_inode_table.c b/lib/fstree/gen_inode_table.c index 04f68da..80686d6 100644 --- a/lib/fstree/gen_inode_table.c +++ b/lib/fstree/gen_inode_table.c @@ -14,7 +14,7 @@ static size_t count_nodes(tree_node_t *root) { - tree_node_t *n = root->data.dir->children; + tree_node_t *n = root->data.dir.children; size_t count = 1; while (n != NULL) { @@ -34,7 +34,7 @@ static void map_child_nodes(fstree_t *fs, tree_node_t *root, size_t *counter) bool has_subdirs = false; tree_node_t *it; - for (it = root->data.dir->children; it != NULL; it = it->next) { + for (it = root->data.dir.children; it != NULL; it = it->next) { if (S_ISDIR(it->mode)) { has_subdirs = true; break; @@ -42,13 +42,13 @@ static void map_child_nodes(fstree_t *fs, tree_node_t *root, size_t *counter) } if (has_subdirs) { - for (it = root->data.dir->children; it != NULL; it = it->next) { + for (it = root->data.dir.children; it != NULL; it = it->next) { if (S_ISDIR(it->mode)) map_child_nodes(fs, it, counter); } } - for (it = root->data.dir->children; it != NULL; it = it->next) { + for (it = root->data.dir.children; it != NULL; it = it->next) { it->inode_num = *counter; *counter += 1; diff --git a/lib/fstree/mknode.c b/lib/fstree/mknode.c index 19df268..f90bd00 100644 --- a/lib/fstree/mknode.c +++ b/lib/fstree/mknode.c @@ -7,7 +7,6 @@ #include "config.h" #include "fstree.h" -#include "util.h" #include #include @@ -17,50 +16,26 @@ tree_node_t *fstree_mknode(tree_node_t *parent, const char *name, size_t name_len, const char *extra, const struct stat *sb) { - size_t size = sizeof(tree_node_t), total; tree_node_t *n; + size_t size; char *ptr; - switch (sb->st_mode & S_IFMT) { - case S_IFLNK: - if (extra == NULL) { - errno = EINVAL; - return NULL; - } - if (SZ_ADD_OV(size, strlen(extra), &size) || - SZ_ADD_OV(size, 1, &size)) { - goto fail_ov; - } - break; - case S_IFDIR: - if (SZ_ADD_OV(size, sizeof(*n->data.dir), &size)) - goto fail_ov; - break; - case S_IFREG: - if (SZ_ADD_OV(size, sizeof(*n->data.file), &size)) - goto fail_ov; - - if (extra != NULL) { - if (SZ_ADD_OV(size, strlen(extra), &size) || - SZ_ADD_OV(size, 1, &size)) { - goto fail_ov; - } - } - break; + if (S_ISLNK(sb->st_mode) && extra == NULL) { + errno = EINVAL; + return NULL; } - if (SZ_ADD_OV(size, name_len, &total) || - SZ_ADD_OV(total, 1, &total)) { - goto fail_ov; - } + size = sizeof(tree_node_t) + name_len + 1; + if (extra != NULL) + size += strlen(extra) + 1; - n = calloc(1, total); + n = calloc(1, size); if (n == NULL) return NULL; if (parent != NULL) { - n->next = parent->data.dir->children; - parent->data.dir->children = n; + n->next = parent->data.dir.children; + parent->data.dir.children = n; n->parent = parent; } @@ -68,24 +43,23 @@ tree_node_t *fstree_mknode(tree_node_t *parent, const char *name, n->gid = sb->st_gid; n->mode = sb->st_mode; n->mod_time = sb->st_mtime; + 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_IFDIR: - n->data.dir = (dir_info_t *)n->payload; - break; case S_IFREG: - n->data.file = (file_info_t *)n->payload; - if (extra != NULL) { - ptr = (char *)n->payload + sizeof(file_info_t); - n->data.file->input_file = ptr; - strcpy(n->data.file->input_file, extra); - } + n->data.file.input_file = ptr; break; case S_IFLNK: n->mode = S_IFLNK | 0777; - n->data.slink_target = (char *)n->payload; - if (extra != NULL) - strcpy(n->data.slink_target, extra); + n->data.slink_target = ptr; break; case S_IFBLK: case S_IFCHR: @@ -93,10 +67,5 @@ tree_node_t *fstree_mknode(tree_node_t *parent, const char *name, break; } - n->name = (char *)n + size; - memcpy(n->name, name, name_len); return n; -fail_ov: - errno = EOVERFLOW; - return NULL; } -- cgit v1.2.3