From 8b5a2d64fa95cb964540b950ca237545d9e51822 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 9 Sep 2023 00:06:08 +0200 Subject: libcommon: get rid of write_data_from_file Modify gensquashfs to pack data using an istream wrapper, similar to tar2sqfs. To implement the no-tail-pack option, we need the file size, so we simply open a raw handle first, and query it (using libsquashfs API) and then create the stream wrapper. For the output side, we create a block-processor ostream wrapper and splice it. Since gensquashfs is the only, remaining user of the pack-a-file functon, we can then remove it from libcommon. Signed-off-by: David Oberhollenzer --- bin/gensquashfs/src/mkfs.c | 66 +++++++++++++++++++++++++++++++------------- include/common.h | 4 --- lib/common/Makemodule.am | 3 +- lib/common/src/data_writer.c | 54 ------------------------------------ 4 files changed, 48 insertions(+), 79 deletions(-) delete mode 100644 lib/common/src/data_writer.c diff --git a/bin/gensquashfs/src/mkfs.c b/bin/gensquashfs/src/mkfs.c index 6d04040..248eb74 100644 --- a/bin/gensquashfs/src/mkfs.c +++ b/bin/gensquashfs/src/mkfs.c @@ -6,13 +6,56 @@ */ #include "mkfs.h" +static int pack_file(sqfs_block_processor_t *data, + const char *path, tree_node_t *n, const options_t *opt) +{ + sqfs_ostream_t *out = NULL; + sqfs_istream_t *in = NULL; + sqfs_file_handle_t hnd; + sqfs_u64 filesize; + int ret, flags; + + ret = sqfs_native_file_open(&hnd, path, SQFS_FILE_OPEN_READ_ONLY); + if (ret) + goto done; + + ret = sqfs_native_file_get_size(hnd, &filesize); + if (ret) + goto done; + + ret = sqfs_istream_open_handle(&in, path, hnd, 0); + if (ret) + goto done; + + flags = n->data.file.flags; + if (opt->no_tail_packing && filesize > opt->cfg.block_size) + flags |= SQFS_BLK_DONT_FRAGMENT; + + ret = sqfs_block_processor_create_ostream(&out, path, data, + &(n->data.file.inode), flags); + if (ret) + goto done; + + do { + ret = sqfs_istream_splice(in, out, opt->cfg.block_size); + } while (ret > 0); + + if (ret == 0) + ret = out->flush(out); +done: + if (ret) + sqfs_perror(path, NULL, ret); + if (in == NULL) + sqfs_native_file_close(hnd); + sqfs_drop(out); + sqfs_drop(in); + return ret; +} + static int pack_files(sqfs_block_processor_t *data, fstree_t *fs, options_t *opt) { - sqfs_u64 filesize; - sqfs_file_t *file; tree_node_t *node; - int flags; int ret; if (opt->packdir != NULL && chdir(opt->packdir) != 0) { @@ -40,22 +83,7 @@ static int pack_files(sqfs_block_processor_t *data, fstree_t *fs, if (!opt->cfg.quiet) printf("packing %s\n", path); - ret = sqfs_file_open(&file, path, SQFS_FILE_OPEN_READ_ONLY); - if (ret) { - sqfs_perror(path, "open", ret); - free(node_path); - return -1; - } - - flags = node->data.file.flags; - filesize = file->get_size(file); - - if (opt->no_tail_packing && filesize > opt->cfg.block_size) - flags |= SQFS_BLK_DONT_FRAGMENT; - - ret = write_data_from_file(path, data, &(node->data.file.inode), - file, flags); - sqfs_drop(file); + ret = pack_file(data, path, node, opt); free(node_path); if (ret) diff --git a/include/common.h b/include/common.h index 707d46b..bf3f34c 100644 --- a/include/common.h +++ b/include/common.h @@ -33,10 +33,6 @@ typedef struct sqfs_hard_link_t { char *target; } sqfs_hard_link_t; -int write_data_from_file(const char *filename, sqfs_block_processor_t *data, - sqfs_inode_generic_t **inode, - sqfs_file_t *file, int flags); - void sqfs_perror(const char *file, const char *action, int error_code); int sqfs_tree_find_hard_links(const sqfs_tree_node_t *root, diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index e0cc551..a50ddcb 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -1,8 +1,7 @@ libcommon_a_SOURCES = include/common.h include/simple_writer.h \ include/compress_cli.h \ lib/common/src/hardlink.c lib/common/src/print_version.c \ - lib/common/src/compress.c \ - lib/common/src/comp_opt.c lib/common/src/data_writer.c \ + lib/common/src/compress.c lib/common/src/comp_opt.c \ lib/common/src/parse_size.c lib/common/src/print_size.c \ lib/common/src/writer/init.c lib/common/src/writer/cleanup.c \ lib/common/src/writer/serialize_fstree.c lib/common/src/writer/finish.c\ diff --git a/lib/common/src/data_writer.c b/lib/common/src/data_writer.c deleted file mode 100644 index ceccaac..0000000 --- a/lib/common/src/data_writer.c +++ /dev/null @@ -1,54 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * data_writer.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "common.h" - -static sqfs_u8 buffer[4096]; - -int write_data_from_file(const char *filename, sqfs_block_processor_t *data, - sqfs_inode_generic_t **inode, sqfs_file_t *file, - int flags) -{ - sqfs_u64 filesz, offset; - size_t diff; - int ret; - - ret = sqfs_block_processor_begin_file(data, inode, NULL, flags); - if (ret) { - sqfs_perror(filename, "beginning file data blocks", ret); - return -1; - } - - filesz = file->get_size(file); - - for (offset = 0; offset < filesz; offset += diff) { - if (filesz - offset > sizeof(buffer)) { - diff = sizeof(buffer); - } else { - diff = filesz - offset; - } - - ret = file->read_at(file, offset, buffer, diff); - if (ret) { - sqfs_perror(filename, "reading file range", ret); - return -1; - } - - ret = sqfs_block_processor_append(data, buffer, diff); - if (ret) { - sqfs_perror(filename, "packing file data", ret); - return -1; - } - } - - ret = sqfs_block_processor_end_file(data); - if (ret) { - sqfs_perror(filename, "finishing file data", ret); - return -1; - } - - return 0; -} -- cgit v1.2.3