From 3946cf086183f8dd4d5d115f52ba1b87560b7ce4 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 4 Jul 2022 20:01:59 +0200 Subject: Move sqfs_tree_node_get_path to libsquashfs Signed-off-by: David Oberhollenzer --- bin/rdsquashfs/describe.c | 4 ++-- bin/rdsquashfs/fill_files.c | 4 ++-- bin/rdsquashfs/restore_fstree.c | 6 +++--- bin/sqfs2tar/write_tree.c | 16 +++++++-------- bin/sqfsdiff/node_compare.c | 6 +++--- bin/sqfsdiff/util.c | 2 +- include/common.h | 2 -- include/sqfs/dir_reader.h | 21 +++++++++++++++++++ lib/common/Makemodule.am | 2 +- lib/common/get_path.c | 45 ----------------------------------------- lib/common/hardlink.c | 2 +- lib/sqfs/Makemodule.am | 1 + lib/sqfs/dir_reader/get_path.c | 42 ++++++++++++++++++++++++++++++++++++++ 13 files changed, 85 insertions(+), 68 deletions(-) delete mode 100644 lib/common/get_path.c create mode 100644 lib/sqfs/dir_reader/get_path.c diff --git a/bin/rdsquashfs/describe.c b/bin/rdsquashfs/describe.c index e0e22bc..1e723bc 100644 --- a/bin/rdsquashfs/describe.c +++ b/bin/rdsquashfs/describe.c @@ -17,7 +17,7 @@ static int print_name(const sqfs_tree_node_t *n, bool dont_escape) if (canonicalize_name(name) != 0) { fprintf(stderr, "Error sanitizing file path '%s'\n", name); - free(name); + sqfs_free(name); return -1; } @@ -47,7 +47,7 @@ static int print_name(const sqfs_tree_node_t *n, bool dont_escape) fputc('"', stdout); } - free(name); + sqfs_free(name); return 0; } diff --git a/bin/rdsquashfs/fill_files.c b/bin/rdsquashfs/fill_files.c index 1afe6b8..8459c1e 100644 --- a/bin/rdsquashfs/fill_files.c +++ b/bin/rdsquashfs/fill_files.c @@ -89,7 +89,7 @@ static int add_file(const sqfs_tree_node_t *node) if (canonicalize_name(path)) { fprintf(stderr, "Invalid file path '%s'\n", path); - free(path); + sqfs_free(path); return -1; } @@ -104,7 +104,7 @@ static void clear_file_list(void) size_t i; for (i = 0; i < num_files; ++i) - free(files[i].path); + sqfs_free(files[i].path); free(files); files = NULL; diff --git a/bin/rdsquashfs/restore_fstree.c b/bin/rdsquashfs/restore_fstree.c index 57582c2..cf5bc2a 100644 --- a/bin/rdsquashfs/restore_fstree.c +++ b/bin/rdsquashfs/restore_fstree.c @@ -141,7 +141,7 @@ static int create_node_dfs(const sqfs_tree_node_t *n, int flags) printf("creating %s\n", name); ret = create_node(n, name, flags); - free(name); + sqfs_free(name); if (ret) return -1; @@ -276,10 +276,10 @@ static int set_attribs(sqfs_xattr_reader_t *xattr, } } - free(path); + sqfs_free(path); return 0; fail: - free(path); + sqfs_free(path); return -1; } diff --git a/bin/sqfs2tar/write_tree.c b/bin/sqfs2tar/write_tree.c index 5fa08e8..d4f8366 100644 --- a/bin/sqfs2tar/write_tree.c +++ b/bin/sqfs2tar/write_tree.c @@ -119,14 +119,14 @@ static int write_tree_dfs(const sqfs_tree_node_t *n) if (lnk != NULL) { ret = write_hard_link(out_file, &sb, name, lnk->target, record_counter++); - free(name); + sqfs_free(name); return ret; } } if (!no_xattr) { if (get_xattrs(name, n->inode, &xattr)) { - free(name); + sqfs_free(name); return -1; } } @@ -140,24 +140,24 @@ static int write_tree_dfs(const sqfs_tree_node_t *n) goto out_skip; if (ret < 0) { - free(name); + sqfs_free(name); return -1; } if (S_ISREG(sb.st_mode)) { if (sqfs_data_reader_dump(name, data, n->inode, out_file, super.block_size)) { - free(name); + sqfs_free(name); return -1; } if (padd_file(out_file, sb.st_size)) { - free(name); + sqfs_free(name); return -1; } } - free(name); + sqfs_free(name); skip_hdr: for (n = n->children; n != NULL; n = n->next) { if (write_tree_dfs(n)) @@ -172,7 +172,7 @@ out_skip: fprintf(stderr, "Skipping %s\n", name); ret = 0; } - free(name); + sqfs_free(name); return ret; } @@ -198,7 +198,7 @@ out_links: while (links != NULL) { lnk = links; links = links->next; - free(lnk->target); + sqfs_free(lnk->target); free(lnk); } return status; diff --git a/bin/sqfsdiff/node_compare.c b/bin/sqfsdiff/node_compare.c index d8f0878..6e8c2fa 100644 --- a/bin/sqfsdiff/node_compare.c +++ b/bin/sqfsdiff/node_compare.c @@ -90,7 +90,7 @@ int node_compare(sqfsdiff_t *sd, sqfs_tree_node_t *a, sqfs_tree_node_t *b) status = 1; } else { fprintf(stdout, "%s has a different type\n", path); - free(path); + sqfs_free(path); return 1; } } @@ -168,7 +168,7 @@ int node_compare(sqfsdiff_t *sd, sqfs_tree_node_t *a, sqfs_tree_node_t *b) if (ret > 0) status = 1; - free(path); + sqfs_free(path); path = NULL; ait = a->children; @@ -200,6 +200,6 @@ int node_compare(sqfsdiff_t *sd, sqfs_tree_node_t *a, sqfs_tree_node_t *b) break; } - free(path); + sqfs_free(path); return status; } diff --git a/bin/sqfsdiff/util.c b/bin/sqfsdiff/util.c index 5e9161a..ab6fa59 100644 --- a/bin/sqfsdiff/util.c +++ b/bin/sqfsdiff/util.c @@ -17,7 +17,7 @@ char *node_path(const sqfs_tree_node_t *n) if (canonicalize_name(path)) { fprintf(stderr, "failed to canonicalization '%s'\n", path); - free(path); + sqfs_free(path); return NULL; } diff --git a/include/common.h b/include/common.h index 5ef4e05..84ec3ca 100644 --- a/include/common.h +++ b/include/common.h @@ -34,8 +34,6 @@ typedef struct sqfs_hard_link_t { char *target; } sqfs_hard_link_t; -char *sqfs_tree_node_get_path(const sqfs_tree_node_t *node); - int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, ostream_t *fp, size_t block_size); diff --git a/include/sqfs/dir_reader.h b/include/sqfs/dir_reader.h index c528e33..3ae4ca4 100644 --- a/include/sqfs/dir_reader.h +++ b/include/sqfs/dir_reader.h @@ -384,6 +384,27 @@ SQFS_API int sqfs_dir_reader_get_full_hierarchy(sqfs_dir_reader_t *rd, */ SQFS_API void sqfs_dir_tree_destroy(sqfs_tree_node_t *root); +/** + * @brief Recursively destroy a tree of @ref sqfs_tree_node_t nodes + * + * @memberof sqfs_tree_node_t + * + * This function can be used to assemble an absolute path from a tree + * node returned by @ref sqfs_dir_reader_get_full_hierarchy. + * + * The function recursively walks up the tree to assemble a path string. It + * returns "/" for the root node and assembles paths beginning with "/" for + * non-root nodes. The resulting path is slash separated, but (except for + * the root) never ends with a slash. + * + * The returned string needs to be free'd with @ref sqfs_free. + * + * @param node A pointer to a tree node. + * + * @return A pointer to a string on success, NULL on allocation failure. + */ +SQFS_API char *sqfs_tree_node_get_path(const sqfs_tree_node_t *node); + #ifdef __cplusplus } #endif diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index f0616fc..b19ecd6 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -2,7 +2,7 @@ libcommon_a_SOURCES = lib/common/hardlink.c libcommon_a_SOURCES += lib/common/print_version.c lib/common/data_reader_dump.c libcommon_a_SOURCES += lib/common/compress.c lib/common/comp_opt.c libcommon_a_SOURCES += lib/common/data_writer.c include/common.h -libcommon_a_SOURCES += lib/common/get_path.c lib/common/data_writer_ostream.c +libcommon_a_SOURCES += lib/common/data_writer_ostream.c libcommon_a_SOURCES += lib/common/perror.c lib/common/parse_size.c libcommon_a_SOURCES += lib/common/print_size.c include/simple_writer.h libcommon_a_SOURCES += include/compress_cli.h diff --git a/lib/common/get_path.c b/lib/common/get_path.c deleted file mode 100644 index 72ed1bd..0000000 --- a/lib/common/get_path.c +++ /dev/null @@ -1,45 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * get_path.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "common.h" - -#include -#include - -char *sqfs_tree_node_get_path(const sqfs_tree_node_t *node) -{ - const sqfs_tree_node_t *it; - char *str, *ptr; - size_t len = 0; - - if (node->parent == NULL) { - if (node->name[0] != '\0') - return strdup((const char *)node->name); - - return strdup("/"); - } - - for (it = node; it != NULL && it->parent != NULL; it = it->parent) { - len += strlen((const char *)it->name) + 1; - } - - str = malloc(len + 1); - if (str == NULL) - return NULL; - - ptr = str + len; - *ptr = '\0'; - - for (it = node; it != NULL && it->parent != NULL; it = it->parent) { - len = strlen((const char *)it->name); - ptr -= len; - - memcpy(ptr, (const char *)it->name, len); - *(--ptr) = '/'; - } - - return str; -} diff --git a/lib/common/hardlink.c b/lib/common/hardlink.c index c64ec4a..031724b 100644 --- a/lib/common/hardlink.c +++ b/lib/common/hardlink.c @@ -57,7 +57,7 @@ static int map_nodes(rbtree_t *inumtree, sqfs_hard_link_t **out, lnk->next = (*out); (*out) = lnk; } else { - free(lnk->target); + sqfs_free(lnk->target); free(lnk); } return 0; diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index d49482e..ad3d42d 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -20,6 +20,7 @@ libsquashfs_la_SOURCES += lib/sqfs/read_table.c lib/sqfs/comp/compressor.c libsquashfs_la_SOURCES += lib/sqfs/comp/internal.h libsquashfs_la_SOURCES += lib/sqfs/dir_reader/dir_reader.c libsquashfs_la_SOURCES += lib/sqfs/dir_reader/read_tree.c +libsquashfs_la_SOURCES += lib/sqfs/dir_reader/get_path.c libsquashfs_la_SOURCES += lib/sqfs/dir_reader/internal.h libsquashfs_la_SOURCES += lib/sqfs/inode.c lib/sqfs/xattr/xattr_writer.c libsquashfs_la_SOURCES += lib/sqfs/xattr/xattr_writer_flush.c diff --git a/lib/sqfs/dir_reader/get_path.c b/lib/sqfs/dir_reader/get_path.c new file mode 100644 index 0000000..bdd8317 --- /dev/null +++ b/lib/sqfs/dir_reader/get_path.c @@ -0,0 +1,42 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * get_path.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "internal.h" + +#include +#include + +char *sqfs_tree_node_get_path(const sqfs_tree_node_t *node) +{ + const sqfs_tree_node_t *it; + char *str, *ptr; + size_t len = 0; + + if (node->parent == NULL) + return strdup("/"); + + for (it = node; it != NULL && it->parent != NULL; it = it->parent) { + len += strlen((const char *)it->name) + 1; + } + + str = malloc(len + 1); + if (str == NULL) + return NULL; + + ptr = str + len; + *ptr = '\0'; + + for (it = node; it != NULL && it->parent != NULL; it = it->parent) { + len = strlen((const char *)it->name); + ptr -= len; + + memcpy(ptr, (const char *)it->name, len); + *(--ptr) = '/'; + } + + return str; +} -- cgit v1.2.3