From da7eb3445cab5936455b8a119cb656113ca3d402 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 3 Jul 2019 18:50:33 +0200 Subject: cleanup: move tree node from path function to libfstree.a Signed-off-by: David Oberhollenzer --- include/fstree.h | 3 +++ lib/Makemodule.am | 2 +- lib/fstree/node_from_path.c | 39 +++++++++++++++++++++++++++++++++++++++ unpack/rdsquashfs.c | 35 +---------------------------------- 4 files changed, 44 insertions(+), 35 deletions(-) create mode 100644 lib/fstree/node_from_path.c diff --git a/include/fstree.h b/include/fstree.h index 6c2ec0b..ea7cf88 100644 --- a/include/fstree.h +++ b/include/fstree.h @@ -265,4 +265,7 @@ 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); +/* resolve a path to a tree node. Returns NULL on failure and sets errno */ +tree_node_t *fstree_node_from_path(fstree_t *fs, const char *path); + #endif /* FSTREE_H */ diff --git a/lib/Makemodule.am b/lib/Makemodule.am index 6418568..23ce70d 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -3,7 +3,7 @@ libfstree_a_SOURCES += lib/fstree/fstree_sort.c lib/fstree/fstree_from_dir.c libfstree_a_SOURCES += lib/fstree/gen_inode_table.c lib/fstree/get_path.c libfstree_a_SOURCES += lib/fstree/node_stat.c lib/fstree/mknode.c libfstree_a_SOURCES += lib/fstree/add_by_path.c lib/fstree/xattr.c -libfstree_a_SOURCES += include/fstree.h +libfstree_a_SOURCES += lib/fstree/node_from_path.c include/fstree.h libfstree_a_CFLAGS = $(AM_CFLAGS) libfstree_a_CPPFLAGS = $(AM_CPPFLAGS) diff --git a/lib/fstree/node_from_path.c b/lib/fstree/node_from_path.c new file mode 100644 index 0000000..e617059 --- /dev/null +++ b/lib/fstree/node_from_path.c @@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "fstree.h" + +#include +#include + +tree_node_t *fstree_node_from_path(fstree_t *fs, const char *path) +{ + tree_node_t *n = fs->root; + const char *end; + size_t len; + + while (path != NULL && *path != '\0') { + if (!S_ISDIR(n->mode)) { + errno = ENOTDIR; + return NULL; + } + + end = strchrnul(path, '/'); + len = end - path; + + for (n = n->data.dir->children; n != NULL; n = n->next) { + if (strncmp(path, n->name, len) != 0) + continue; + if (n->name[len] != '\0') + continue; + break; + } + + if (n == NULL) { + errno = ENOENT; + return NULL; + } + + path = *end ? (end + 1) : end; + } + + return n; +} diff --git a/unpack/rdsquashfs.c b/unpack/rdsquashfs.c index 42bddff..4a0f12b 100644 --- a/unpack/rdsquashfs.c +++ b/unpack/rdsquashfs.c @@ -1,39 +1,6 @@ /* SPDX-License-Identifier: GPL-3.0-or-later */ #include "rdsquashfs.h" -static tree_node_t *find_node(tree_node_t *n, const char *path) -{ - const char *end; - size_t len; - - while (path != NULL && *path != '\0') { - if (!S_ISDIR(n->mode)) { - errno = ENOTDIR; - return NULL; - } - - end = strchrnul(path, '/'); - len = end - path; - - for (n = n->data.dir->children; n != NULL; n = n->next) { - if (strncmp(path, n->name, len) != 0) - continue; - if (n->name[len] != '\0') - continue; - break; - } - - if (n == NULL) { - errno = ENOENT; - return NULL; - } - - path = *end ? (end + 1) : end; - } - - return n; -} - int main(int argc, char **argv) { int status = EXIT_FAILURE; @@ -76,7 +43,7 @@ int main(int argc, char **argv) goto out_cmp; if (opt.cmdpath != NULL) { - n = find_node(fs.root, opt.cmdpath); + n = fstree_node_from_path(&fs, opt.cmdpath); if (n == NULL) { perror(opt.cmdpath); goto out_fs; -- cgit v1.2.3