From 2777dfe050359c359233b2f00dfb5c3b2dba4ed6 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 18 Dec 2019 15:55:03 +0100 Subject: Cleanup: merge the fstree post processing functions Instead of having 3 different functions for sorting the tree, numbering the nodes and generating a file list, that all have to be used in the right order, this commit merges them into a single "fstree_post_process" function. Signed-off-by: David Oberhollenzer --- include/fstree.h | 16 ++++---- lib/common/writer.c | 3 -- lib/fstree/Makemodule.am | 3 +- lib/fstree/fstree_sort.c | 10 ----- lib/fstree/gen_file_list.c | 43 ------------------- lib/fstree/gen_inode_numbers.c | 49 ---------------------- lib/fstree/post_process.c | 93 ++++++++++++++++++++++++++++++++++++++++++ mkfs/mkfs.c | 3 +- tar/tar2sqfs.c | 2 + tests/fstree_from_file.c | 2 +- tests/gen_inode_numbers.c | 4 +- 11 files changed, 107 insertions(+), 121 deletions(-) delete mode 100644 lib/fstree/gen_file_list.c delete mode 100644 lib/fstree/gen_inode_numbers.c create mode 100644 lib/fstree/post_process.c diff --git a/include/fstree.h b/include/fstree.h index ab44e3e..dd1440e 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -155,14 +155,15 @@ tree_node_t *fstree_add_generic(fstree_t *fs, const char *path, int fstree_from_file(fstree_t *fs, const char *filename, FILE *fp); /* - Allocates inode numbers for all nodes. Directory entries are numbered in - ascending order. + This function performs all the necessary post processing steps on the file + system tree, i.e. recursively sorting all directory entries by name, + allocating inode numbers and stringing all files nodes together into a + linked list. - The total inode count is stored in unique_inode_count. + The total inode count is stored in unique_inode_count. The head of the file + list is pointed to by fs->files. */ -void fstree_gen_inode_numbers(fstree_t *fs); - -void fstree_gen_file_list(fstree_t *fs); +void fstree_post_process(fstree_t *fs); /* Generate a string holding the full path of a node. Returned @@ -175,9 +176,6 @@ char *fstree_get_path(tree_node_t *node); /* ASCIIbetically sort a linked list of tree nodes */ tree_node_t *tree_node_list_sort(tree_node_t *head); -/* ASCIIbetically sort all sub directories recursively */ -void tree_node_sort_recursive(tree_node_t *root); - /* If the environment variable SOURCE_DATE_EPOCH is set to a parsable number that fits into an unsigned 32 bit value, return its value. Otherwise, diff --git a/lib/common/writer.c b/lib/common/writer.c index 087ee3c..1bbf6bd 100644 --- a/lib/common/writer.c +++ b/lib/common/writer.c @@ -213,9 +213,6 @@ int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg) if (!cfg->quiet) fputs("Writing inodes and directories...\n", stdout); - tree_node_sort_recursive(sqfs->fs.root); - fstree_gen_inode_numbers(&sqfs->fs); - sqfs->super.inode_count = sqfs->fs.unique_inode_count; if (sqfs_serialize_fstree(cfg->filename, sqfs)) diff --git a/lib/fstree/Makemodule.am b/lib/fstree/Makemodule.am index bb17d24..64c2650 100644 --- a/lib/fstree/Makemodule.am +++ b/lib/fstree/Makemodule.am @@ -1,10 +1,9 @@ libfstree_a_SOURCES = lib/fstree/fstree.c lib/fstree/fstree_from_file.c libfstree_a_SOURCES += lib/fstree/fstree_sort.c -libfstree_a_SOURCES += lib/fstree/gen_inode_numbers.c lib/fstree/get_path.c +libfstree_a_SOURCES += lib/fstree/post_process.c lib/fstree/get_path.c libfstree_a_SOURCES += lib/fstree/mknode.c libfstree_a_SOURCES += lib/fstree/add_by_path.c libfstree_a_SOURCES += include/fstree.h -libfstree_a_SOURCES += lib/fstree/gen_file_list.c libfstree_a_SOURCES += lib/fstree/source_date_epoch.c libfstree_a_SOURCES += lib/fstree/canonicalize_name.c libfstree_a_CFLAGS = $(AM_CFLAGS) diff --git a/lib/fstree/fstree_sort.c b/lib/fstree/fstree_sort.c index 9cb177b..124106e 100644 --- a/lib/fstree/fstree_sort.c +++ b/lib/fstree/fstree_sort.c @@ -60,13 +60,3 @@ tree_node_t *tree_node_list_sort(tree_node_t *head) return merge(tree_node_list_sort(head), tree_node_list_sort(half)); } - -void tree_node_sort_recursive(tree_node_t *n) -{ - n->data.dir.children = tree_node_list_sort(n->data.dir.children); - - 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 deleted file mode 100644 index f20c131..0000000 --- a/lib/fstree/gen_file_list.c +++ /dev/null @@ -1,43 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * gen_file_list.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" -#include "fstree.h" - -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; - } - - if (S_ISDIR(n->mode)) { - file_info_t *list = NULL, *last = NULL; - - for (n = n->data.dir.children; n != NULL; n = n->next) { - if (list == NULL) { - list = file_list_dfs(n); - if (list == NULL) - continue; - last = list; - } else { - last->next = file_list_dfs(n); - } - - while (last->next != NULL) - last = last->next; - } - - return list; - } - - return NULL; -} - -void fstree_gen_file_list(fstree_t *fs) -{ - fs->files = file_list_dfs(fs->root); -} diff --git a/lib/fstree/gen_inode_numbers.c b/lib/fstree/gen_inode_numbers.c deleted file mode 100644 index 35dbc7a..0000000 --- a/lib/fstree/gen_inode_numbers.c +++ /dev/null @@ -1,49 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * gen_inode_numbers.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" - -#include "fstree.h" - -#include -#include - -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) { - if (S_ISDIR(it->mode)) { - has_subdirs = true; - break; - } - } - - if (has_subdirs) { - 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) { - fs->unique_inode_count += 1; - - it->inode_num = *counter; - *counter += 1; - } -} - -void fstree_gen_inode_numbers(fstree_t *fs) -{ - size_t inum = 1; - - fs->unique_inode_count = 0; - map_child_nodes(fs, fs->root, &inum); - fs->root->inode_num = inum; - fs->unique_inode_count += 1; -} diff --git a/lib/fstree/post_process.c b/lib/fstree/post_process.c new file mode 100644 index 0000000..5e7f152 --- /dev/null +++ b/lib/fstree/post_process.c @@ -0,0 +1,93 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * post_process.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "fstree.h" + +#include +#include + +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) { + if (S_ISDIR(it->mode)) { + has_subdirs = true; + break; + } + } + + if (has_subdirs) { + 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) { + fs->unique_inode_count += 1; + + it->inode_num = *counter; + *counter += 1; + } +} + +static void sort_recursive(tree_node_t *n) +{ + n->data.dir.children = tree_node_list_sort(n->data.dir.children); + + for (n = n->data.dir.children; n != NULL; n = n->next) { + if (S_ISDIR(n->mode)) + sort_recursive(n); + } +} + +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; + } + + if (S_ISDIR(n->mode)) { + file_info_t *list = NULL, *last = NULL; + + for (n = n->data.dir.children; n != NULL; n = n->next) { + if (list == NULL) { + list = file_list_dfs(n); + if (list == NULL) + continue; + last = list; + } else { + last->next = file_list_dfs(n); + } + + while (last->next != NULL) + last = last->next; + } + + return list; + } + + return NULL; +} + +void fstree_post_process(fstree_t *fs) +{ + size_t inum = 1; + + sort_recursive(fs->root); + + fs->unique_inode_count = 0; + map_child_nodes(fs, fs->root, &inum); + fs->root->inode_num = inum; + fs->unique_inode_count += 1; + + fs->files = file_list_dfs(fs->root); +} diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index ea01452..89215c6 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -232,8 +232,7 @@ int main(int argc, char **argv) sehnd = NULL; } - tree_node_sort_recursive(sqfs.fs.root); - fstree_gen_file_list(&sqfs.fs); + fstree_post_process(&sqfs.fs); if (pack_files(sqfs.data, &sqfs.fs, &sqfs.stats, &opt)) goto out; diff --git a/tar/tar2sqfs.c b/tar/tar2sqfs.c index cca604a..b070db8 100644 --- a/tar/tar2sqfs.c +++ b/tar/tar2sqfs.c @@ -551,6 +551,8 @@ int main(int argc, char **argv) if (process_tar_ball()) goto out; + fstree_post_process(&sqfs.fs); + if (sqfs_writer_finish(&sqfs, &cfg)) goto out; diff --git a/tests/fstree_from_file.c b/tests/fstree_from_file.c index a0b6b2d..eccd1f4 100644 --- a/tests/fstree_from_file.c +++ b/tests/fstree_from_file.c @@ -42,7 +42,7 @@ int main(void) assert(fstree_init(&fs, NULL) == 0); assert(fstree_from_file(&fs, "testfile", fp) == 0); - tree_node_sort_recursive(fs.root); + fstree_post_process(&fs); n = fs.root->data.dir.children; assert(n->mode == (S_IFBLK | 0600)); diff --git a/tests/gen_inode_numbers.c b/tests/gen_inode_numbers.c index e16abbe..dc8ca4c 100644 --- a/tests/gen_inode_numbers.c +++ b/tests/gen_inode_numbers.c @@ -54,7 +54,7 @@ int main(void) // inode table for the empty tree assert(fstree_init(&fs, NULL) == 0); - fstree_gen_inode_numbers(&fs); + fstree_post_process(&fs); assert(fs.unique_inode_count == 1); assert(fs.root->inode_num == 1); fstree_cleanup(&fs); @@ -81,7 +81,7 @@ int main(void) assert(gen_node(c, "c_b") != NULL); assert(gen_node(c, "c_c") != NULL); - fstree_gen_inode_numbers(&fs); + fstree_post_process(&fs); assert(fs.unique_inode_count == 13); check_children_before_root(fs.root); -- cgit v1.2.3