diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-27 16:43:11 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-27 16:43:11 +0200 |
commit | 625368eb5bcb9954ad190af50962e6b7c2fd9c4c (patch) | |
tree | 48779d7e54b4fa3df83df39fff76b54d93aacedf | |
parent | 720023d968b24fe358fd4cfb002d8572f6cc96e7 (diff) |
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 <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | include/fstree.h | 4 | ||||
-rw-r--r-- | lib/fstree/add_by_path.c | 8 | ||||
-rw-r--r-- | lib/fstree/fstree.c | 6 | ||||
-rw-r--r-- | lib/fstree/fstree_from_dir.c | 2 | ||||
-rw-r--r-- | lib/fstree/fstree_sort.c | 4 | ||||
-rw-r--r-- | lib/fstree/gen_file_list.c | 6 | ||||
-rw-r--r-- | lib/fstree/gen_inode_table.c | 8 | ||||
-rw-r--r-- | lib/fstree/mknode.c | 73 | ||||
-rw-r--r-- | lib/sqfshelper/serialize_fstree.c | 2 | ||||
-rw-r--r-- | lib/sqfshelper/tree_node_to_inode.c | 4 | ||||
-rw-r--r-- | tar/tar2sqfs.c | 2 | ||||
-rw-r--r-- | tests/add_by_path.c | 14 | ||||
-rw-r--r-- | tests/fstree_from_file.c | 20 | ||||
-rw-r--r-- | tests/gen_inode_table.c | 8 | ||||
-rw-r--r-- | tests/mknode_dir.c | 15 | ||||
-rw-r--r-- | tests/mknode_reg.c | 7 | ||||
-rw-r--r-- | tests/mknode_simple.c | 12 | ||||
-rw-r--r-- | tests/mknode_slink.c | 4 |
18 files changed, 81 insertions, 118 deletions
diff --git a/include/fstree.h b/include/fstree.h index c9ff574..11da854 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -124,8 +124,8 @@ struct tree_node_t { /* Type specific data. Pointers are into payload area blow. */ union { - dir_info_t *dir; - file_info_t *file; + dir_info_t dir; + file_info_t file; char *slink_target; uint64_t devno; } data; 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 <string.h> #include <stdlib.h> @@ -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; } diff --git a/lib/sqfshelper/serialize_fstree.c b/lib/sqfshelper/serialize_fstree.c index e012902..296f123 100644 --- a/lib/sqfshelper/serialize_fstree.c +++ b/lib/sqfshelper/serialize_fstree.c @@ -26,7 +26,7 @@ static sqfs_inode_generic_t *write_dir_entries(sqfs_dir_writer_t *dirw, if (sqfs_dir_writer_begin(dirw)) return NULL; - for (it = node->data.dir->children; it != NULL; it = it->next) { + for (it = node->data.dir.children; it != NULL; it = it->next) { ret = sqfs_dir_writer_add_entry(dirw, it->name, it->inode_num, it->inode_ref, it->mode); if (ret) diff --git a/lib/sqfshelper/tree_node_to_inode.c b/lib/sqfshelper/tree_node_to_inode.c index 5cb04a4..edcdee3 100644 --- a/lib/sqfshelper/tree_node_to_inode.c +++ b/lib/sqfshelper/tree_node_to_inode.c @@ -52,8 +52,8 @@ sqfs_inode_generic_t *tree_node_to_inode(sqfs_id_table_t *idtbl, size_t extra = 0; if (S_ISREG(node->mode)) { - inode = node->data.file->user_ptr; - node->data.file->user_ptr = NULL; + inode = node->data.file.user_ptr; + node->data.file.user_ptr = NULL; } else { if (S_ISLNK(node->mode)) extra = strlen(node->data.slink_target); diff --git a/tar/tar2sqfs.c b/tar/tar2sqfs.c index 2eef4dc..45b71bb 100644 --- a/tar/tar2sqfs.c +++ b/tar/tar2sqfs.c @@ -326,7 +326,7 @@ static int create_node_and_repack_data(tar_header_decoded_t *hdr, fstree_t *fs, } if (S_ISREG(hdr->sb.st_mode)) { - if (write_file(hdr, node->data.file, data, hdr->sb.st_size)) + if (write_file(hdr, &node->data.file, data, hdr->sb.st_size)) return -1; } diff --git a/tests/add_by_path.c b/tests/add_by_path.c index 610cb79..afed438 100644 --- a/tests/add_by_path.c +++ b/tests/add_by_path.c @@ -37,8 +37,8 @@ int main(void) assert(a->gid == sb.st_gid); assert(a->parent == fs.root); assert(a->next == NULL); - assert(fs.root->data.dir->children == a); - assert(!a->data.dir->created_implicitly); + assert(fs.root->data.dir.children == a); + assert(!a->data.dir.created_implicitly); memset(&sb, 0, sizeof(sb)); sb.st_mode = S_IFBLK | 0640; @@ -54,7 +54,7 @@ int main(void) assert(b->parent == fs.root); assert(b->data.devno == sb.st_rdev); assert(b->next == a); - assert(fs.root->data.dir->children == b); + assert(fs.root->data.dir.children == b); assert(fstree_add_generic(&fs, "blkdev/foo", &sb, NULL) == NULL); assert(errno == ENOTDIR); @@ -77,7 +77,7 @@ int main(void) assert(b->parent == a); assert(b->data.devno == sb.st_rdev); assert(b->next == NULL); - assert(a->data.dir->children == b); + assert(a->data.dir.children == b); b = fstree_add_generic(&fs, "dir/foo/chrdev", &sb, NULL); assert(b != NULL); @@ -89,10 +89,10 @@ int main(void) assert(b->parent->parent == a); assert(b->data.devno == sb.st_rdev); assert(b->next == NULL); - assert(a->data.dir->children != b); + assert(a->data.dir.children != b); b = b->parent; - assert(b->data.dir->created_implicitly); + assert(b->data.dir.created_implicitly); assert(b->mode == (S_IFDIR | 0755)); assert(b->uid == 21); assert(b->gid == 42); @@ -105,7 +105,7 @@ int main(void) a = fstree_add_generic(&fs, "dir/foo", &sb, NULL); assert(a != NULL); assert(a == b); - assert(!a->data.dir->created_implicitly); + assert(!a->data.dir.created_implicitly); assert(a->mode == sb.st_mode); assert(a->uid == sb.st_uid); assert(a->gid == sb.st_gid); diff --git a/tests/fstree_from_file.c b/tests/fstree_from_file.c index 1b9fd76..a8e05c3 100644 --- a/tests/fstree_from_file.c +++ b/tests/fstree_from_file.c @@ -42,7 +42,7 @@ int main(void) assert(fstree_from_file(&fs, "testfile", fp) == 0); tree_node_sort_recursive(fs.root); - n = fs.root->data.dir->children; + n = fs.root->data.dir.children; assert(n->mode == (S_IFBLK | 0600)); assert(n->uid == 8); @@ -62,21 +62,21 @@ int main(void) assert(n->uid == 4); assert(n->gid == 5); assert(strcmp(n->name, "dir") == 0); - assert(n->data.dir->children == NULL); + assert(n->data.dir.children == NULL); n = n->next; assert(n->mode == (S_IFDIR | 0755)); assert(n->uid == 0); assert(n->gid == 0); assert(strcmp(n->name, "foo bar") == 0); - assert(n->data.dir->children != NULL); - - assert(n->data.dir->children->next == NULL); - assert(n->data.dir->children->mode == (S_IFDIR | 0755)); - assert(n->data.dir->children->uid == 0); - assert(n->data.dir->children->gid == 0); - assert(strcmp(n->data.dir->children->name, " test \"") == 0); - assert(n->data.dir->children->data.dir->children == NULL); + assert(n->data.dir.children != NULL); + + assert(n->data.dir.children->next == NULL); + assert(n->data.dir.children->mode == (S_IFDIR | 0755)); + assert(n->data.dir.children->uid == 0); + assert(n->data.dir.children->gid == 0); + assert(strcmp(n->data.dir.children->name, " test \"") == 0); + assert(n->data.dir.children->data.dir.children == NULL); n = n->next; assert(n->mode == (S_IFIFO | 0644)); diff --git a/tests/gen_inode_table.c b/tests/gen_inode_table.c index 190f393..cf140c8 100644 --- a/tests/gen_inode_table.c +++ b/tests/gen_inode_table.c @@ -26,10 +26,10 @@ static void check_children_before_root(tree_node_t *root) { tree_node_t *n; - for (n = root->data.dir->children; n != NULL; n = n->next) + for (n = root->data.dir.children; n != NULL; n = n->next) assert(n->inode_num < root->inode_num); - for (n = root->data.dir->children; n != NULL; n = n->next) + for (n = root->data.dir.children; n != NULL; n = n->next) check_children_before_root(n); } @@ -37,13 +37,13 @@ static void check_children_continuous(tree_node_t *root) { tree_node_t *n; - for (n = root->data.dir->children; n != NULL; n = n->next) { + for (n = root->data.dir.children; n != NULL; n = n->next) { if (n->next != NULL) { assert(n->next->inode_num == (n->inode_num + 1)); } } - for (n = root->data.dir->children; n != NULL; n = n->next) + for (n = root->data.dir.children; n != NULL; n = n->next) check_children_continuous(n); } diff --git a/tests/mknode_dir.c b/tests/mknode_dir.c index 2ca189d..3b6aeda 100644 --- a/tests/mknode_dir.c +++ b/tests/mknode_dir.c @@ -26,29 +26,28 @@ int main(void) sb.st_rdev = 789; sb.st_size = 4096; - root = fstree_mknode(NULL, "rootdir", 7, (void *)0x100, &sb); + root = fstree_mknode(NULL, "rootdir", 7, NULL, &sb); assert(root->uid == sb.st_uid); assert(root->gid == sb.st_gid); assert(root->mode == sb.st_mode); assert((char *)root->name >= (char *)root->payload); - assert((char *)root->data.dir >= (char *)root->payload); - assert(root->name >= (char *)(root->data.dir + 1)); + assert(root->name >= (char *)root->payload); assert(strcmp(root->name, "rootdir") == 0); - assert(root->data.dir->children == NULL); + assert(root->data.dir.children == NULL); assert(root->parent == NULL); assert(root->next == NULL); - a = fstree_mknode(root, "adir", 4, (void *)0x100, &sb); + a = fstree_mknode(root, "adir", 4, NULL, &sb); assert(a->parent == root); assert(a->next == NULL); - assert(root->data.dir->children == a); + assert(root->data.dir.children == a); assert(root->parent == NULL); assert(root->next == NULL); - b = fstree_mknode(root, "bdir", 4, (void *)0x100, &sb); + b = fstree_mknode(root, "bdir", 4, NULL, &sb); assert(a->parent == root); assert(b->parent == root); - assert(root->data.dir->children == b); + assert(root->data.dir.children == b); assert(b->next == a); assert(a->next == NULL); assert(root->parent == NULL); diff --git a/tests/mknode_reg.c b/tests/mknode_reg.c index b9c81e0..9d5267f 100644 --- a/tests/mknode_reg.c +++ b/tests/mknode_reg.c @@ -34,11 +34,10 @@ int main(void) assert(node->mode == sb.st_mode); assert(node->parent == NULL); assert((char *)node->name >= (char *)node->payload); - assert((char *)node->data.file >= (char *)node->payload); - assert(node->data.file->input_file >= (char *)(node->data.file + 1)); - assert(node->name >= node->data.file->input_file + 6); + assert(node->data.file.input_file >= (char *)node->payload); + assert(node->data.file.input_file >= node->name + 8); assert(strcmp(node->name, "filename") == 0); - assert(strcmp(node->data.file->input_file, "input") == 0); + assert(strcmp(node->data.file.input_file, "input") == 0); free(node); return EXIT_SUCCESS; diff --git a/tests/mknode_simple.c b/tests/mknode_simple.c index e872369..e77f4ff 100644 --- a/tests/mknode_simple.c +++ b/tests/mknode_simple.c @@ -26,15 +26,13 @@ int main(void) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "sockfile", 8, (void *)0x1000, &sb); + node = fstree_mknode(NULL, "sockfile", 8, NULL, &sb); assert((char *)node->name >= (char *)node->payload); assert(strcmp(node->name, "sockfile") == 0); assert(node->uid == sb.st_uid); assert(node->gid == sb.st_gid); assert(node->mode == sb.st_mode); assert(node->parent == NULL); - assert(node->data.dir == NULL); - assert(node->data.file == NULL); assert(node->data.slink_target == NULL); assert(node->data.devno == 0); free(node); @@ -47,15 +45,13 @@ int main(void) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "fifo", 4, (void *)0x1000, &sb); + node = fstree_mknode(NULL, "fifo", 4, NULL, &sb); assert((char *)node->name >= (char *)node->payload); assert(strcmp(node->name, "fifo") == 0); assert(node->uid == sb.st_uid); assert(node->gid == sb.st_gid); assert(node->mode == sb.st_mode); assert(node->parent == NULL); - assert(node->data.dir == NULL); - assert(node->data.file == NULL); assert(node->data.slink_target == NULL); assert(node->data.devno == 0); free(node); @@ -68,7 +64,7 @@ int main(void) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "blkdev", 6, (void *)0x1000, &sb); + node = fstree_mknode(NULL, "blkdev", 6, NULL, &sb); assert((char *)node->name >= (char *)node->payload); assert(strcmp(node->name, "blkdev") == 0); assert(node->uid == sb.st_uid); @@ -86,7 +82,7 @@ int main(void) sb.st_rdev = 789; sb.st_size = 1337; - node = fstree_mknode(NULL, "chardev", 7, (void *)0x1000, &sb); + node = fstree_mknode(NULL, "chardev", 7, NULL, &sb); assert((char *)node->name >= (char *)node->payload); assert(strcmp(node->name, "chardev") == 0); assert(node->uid == sb.st_uid); diff --git a/tests/mknode_slink.c b/tests/mknode_slink.c index bbf7045..20cf38a 100644 --- a/tests/mknode_slink.c +++ b/tests/mknode_slink.c @@ -33,7 +33,7 @@ int main(void) assert(node->parent == NULL); assert((char *)node->name >= (char *)node->payload); assert(node->data.slink_target >= (char *)node->payload); - assert(node->name >= node->data.slink_target + 7); + assert(node->data.slink_target >= node->name + 8); assert(strcmp(node->name, "symlink") == 0); assert(strcmp(node->data.slink_target, "target") == 0); free(node); @@ -45,7 +45,7 @@ int main(void) assert(node->parent == NULL); assert((char *)node->name >= (char *)node->payload); assert(node->data.slink_target >= (char *)node->payload); - assert(node->name >= node->data.slink_target + 1); + assert(node->data.slink_target >= node->name + 8); assert(strcmp(node->name, "symlink") == 0); assert(node->data.slink_target[0] == '\0'); free(node); |