From 0abe1816da2706f87432c4e04918ad0eec902479 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 16 Jun 2023 19:46:46 +0200 Subject: Move data writer ostream into libsquashfs It is mainly a very thin wrapper on top of the block processor so far. Signed-off-by: David Oberhollenzer --- bin/tar2sqfs/src/process_tarball.c | 12 +++-- include/common.h | 5 -- include/sqfs/block_processor.h | 21 ++++++++ lib/common/Makemodule.am | 3 +- lib/common/src/data_writer_ostream.c | 91 ------------------------------- lib/sqfs/Makemodule.am | 1 + lib/sqfs/src/block_processor/ostream.c | 99 ++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 103 deletions(-) delete mode 100644 lib/common/src/data_writer_ostream.c create mode 100644 lib/sqfs/src/block_processor/ostream.c diff --git a/bin/tar2sqfs/src/process_tarball.c b/bin/tar2sqfs/src/process_tarball.c index 639adf8..a2a4360 100644 --- a/bin/tar2sqfs/src/process_tarball.c +++ b/bin/tar2sqfs/src/process_tarball.c @@ -16,10 +16,10 @@ static int write_file(sqfs_writer_t *sqfs, dir_iterator_t *it, if (no_tail_pack && ent->size > cfg.block_size) flags |= SQFS_BLK_DONT_FRAGMENT; - out = data_writer_ostream_create(ent->name, sqfs->data, - &(n->data.file.inode), flags); - if (out == NULL) - return -1; + ret = sqfs_block_processor_create_ostream(&out, ent->name, sqfs->data, + &(n->data.file.inode), flags); + if (ret) + return ret; ret = it->open_file_ro(it, &in); if (ret != 0) { @@ -31,7 +31,9 @@ static int write_file(sqfs_writer_t *sqfs, dir_iterator_t *it, ret = sqfs_istream_splice(in, out, cfg.block_size); } while (ret > 0); - out->flush(out); + if (ret == 0) + ret = out->flush(out); + sqfs_drop(out); sqfs_drop(in); return ret; diff --git a/include/common.h b/include/common.h index d4827a8..8e24d03 100644 --- a/include/common.h +++ b/include/common.h @@ -63,11 +63,6 @@ int parse_size(const char *what, size_t *out, const char *str, void print_size(sqfs_u64 size, char *buffer, bool round_to_int); -sqfs_ostream_t *data_writer_ostream_create(const char *filename, - sqfs_block_processor_t *proc, - sqfs_inode_generic_t **inode, - int flags); - /* Parse a comma separated list (e.g. "uid=...,gid=..." of defaults for fstree nodes. Used for command line parsing. Returns 0 on success, diff --git a/include/sqfs/block_processor.h b/include/sqfs/block_processor.h index a5eed33..cc40605 100644 --- a/include/sqfs/block_processor.h +++ b/include/sqfs/block_processor.h @@ -381,6 +381,27 @@ SQFS_API int sqfs_block_processor_finish(sqfs_block_processor_t *proc); SQFS_API const sqfs_block_processor_stats_t *sqfs_block_processor_get_stats(const sqfs_block_processor_t *proc); +/** + * @brief Create an @ref sqfs_ostream_t that writes to a data writer + * + * @memberof sqfs_block_processor_t + * + * @param out Receives a pointer to an @ref sqfs_ostream_t on success + * @param filename A filename that the ostream returns when queried. + * @param proc A pointer to a data writer object. + * @param inode An optional pointer to a pointer to an inode. If not NULL, the + * block processor creates a file inode and stores a pointer to + * it here and keeps updating the inode as the file grows. + * @param flags A combination of @ref SQFS_BLK_FLAGS that can be used to + * micro manage how the data is processed. + * + * @return Zero on sucess, a negative @ref SQFS_ERROR value on failure. + */ +SQFS_API int sqfs_block_processor_create_ostream(sqfs_ostream_t **out, + const char *filename, + sqfs_block_processor_t *proc, + sqfs_inode_generic_t **inode, + sqfs_u32 flags); #ifdef __cplusplus } #endif diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am index 63fa602..61d87d4 100644 --- a/lib/common/Makemodule.am +++ b/lib/common/Makemodule.am @@ -3,11 +3,10 @@ libcommon_a_SOURCES = include/common.h include/simple_writer.h \ lib/common/src/hardlink.c lib/common/src/print_version.c \ lib/common/src/data_reader_dump.c lib/common/src/compress.c \ lib/common/src/comp_opt.c lib/common/src/data_writer.c \ - lib/common/src/data_writer_ostream.c lib/common/src/perror.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\ - lib/common/src/fstree_cli.c + lib/common/src/fstree_cli.c lib/common/src/perror.c libcommon_a_CFLAGS = $(AM_CFLAGS) $(LZO_CFLAGS) if WITH_LZO diff --git a/lib/common/src/data_writer_ostream.c b/lib/common/src/data_writer_ostream.c deleted file mode 100644 index 59abd7a..0000000 --- a/lib/common/src/data_writer_ostream.c +++ /dev/null @@ -1,91 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * data_writer_ostream.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "config.h" -#include "common.h" - -#include - -typedef struct{ - sqfs_ostream_t base; - - sqfs_block_processor_t *proc; - const char *filename; -} data_writer_ostream_t; - -static int stream_append(sqfs_ostream_t *base, const void *data, size_t size) -{ - data_writer_ostream_t *strm = (data_writer_ostream_t *)base; - int ret; - - ret = sqfs_block_processor_append(strm->proc, data, size); - - if (ret != 0) { - sqfs_perror(strm->filename, NULL, ret); - return -1; - } - - return 0; -} - -static int stream_flush(sqfs_ostream_t *base) -{ - data_writer_ostream_t *strm = (data_writer_ostream_t *)base; - int ret; - - ret = sqfs_block_processor_end_file(strm->proc); - - if (ret != 0) { - sqfs_perror(strm->filename, NULL, ret); - return -1; - } - - return 0; -} - -static const char *stream_get_filename(sqfs_ostream_t *base) -{ - data_writer_ostream_t *strm = (data_writer_ostream_t *)base; - - return strm->filename; -} - -static void stream_destroy(sqfs_object_t *base) -{ - free(base); -} - -sqfs_ostream_t *data_writer_ostream_create(const char *filename, - sqfs_block_processor_t *proc, - sqfs_inode_generic_t **inode, - int flags) -{ - data_writer_ostream_t *strm = calloc(1, sizeof(*strm)); - sqfs_ostream_t *base = (sqfs_ostream_t *)strm; - int ret; - - if (strm == NULL) { - perror(filename); - return NULL; - } - - sqfs_object_init(strm, stream_destroy, NULL); - - ret = sqfs_block_processor_begin_file(proc, inode, NULL, flags); - - if (ret != 0) { - sqfs_perror(filename, NULL, ret); - free(strm); - return NULL; - } - - strm->proc = proc; - strm->filename = filename; - base->append = stream_append; - base->flush = stream_flush; - base->get_filename = stream_get_filename; - return base; -} diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index 170cf6b..e929286 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -29,6 +29,7 @@ libsquashfs_la_SOURCES = $(LIBSQFS_HEARDS) lib/sqfs/src/id_table.c \ lib/sqfs/src/block_processor/frontend.c \ lib/sqfs/src/block_processor/block_processor.c \ lib/sqfs/src/block_processor/backend.c \ + lib/sqfs/src/block_processor/ostream.c \ lib/sqfs/src/frag_table.c lib/sqfs/src/block_writer.c \ lib/sqfs/src/misc.c lib/sqfs/src/io/istream.c \ lib/sqfs/src/io/ostream.c lib/sqfs/src/io/file.c \ diff --git a/lib/sqfs/src/block_processor/ostream.c b/lib/sqfs/src/block_processor/ostream.c new file mode 100644 index 0000000..b734e08 --- /dev/null +++ b/lib/sqfs/src/block_processor/ostream.c @@ -0,0 +1,99 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * ostream.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "internal.h" + +#include + +typedef struct{ + sqfs_ostream_t base; + + sqfs_block_processor_t *proc; + char filename[]; +} block_processor_ostream_t; + +static int stream_append(sqfs_ostream_t *base, const void *data, size_t size) +{ + block_processor_ostream_t *strm = (block_processor_ostream_t *)base; + + if (strm->proc == NULL) + return SQFS_ERROR_SEQUENCE; + + return sqfs_block_processor_append(strm->proc, data, size); +} + +static int stream_flush(sqfs_ostream_t *base) +{ + block_processor_ostream_t *strm = (block_processor_ostream_t *)base; + int ret; + + if (strm->proc == NULL) + return SQFS_ERROR_SEQUENCE; + + ret = sqfs_block_processor_end_file(strm->proc); + strm->proc = sqfs_drop(strm->proc); + + return ret; +} + +static const char *stream_get_filename(sqfs_ostream_t *strm) +{ + return ((block_processor_ostream_t *)strm)->filename; +} + +static void stream_destroy(sqfs_object_t *base) +{ + block_processor_ostream_t *strm = (block_processor_ostream_t *)base; + + if (strm->proc != NULL) { + sqfs_block_processor_end_file(strm->proc); + sqfs_drop(strm->proc); + } + + free(strm); +} + +int sqfs_block_processor_create_ostream(sqfs_ostream_t **out, + const char *filename, + sqfs_block_processor_t *proc, + sqfs_inode_generic_t **inode, + sqfs_u32 flags) +{ + block_processor_ostream_t *strm; + sqfs_ostream_t *base; + size_t size, namelen; + int ret; + + *out = NULL; + + namelen = strlen(filename); + size = sizeof(*strm) + 1; + if (SZ_ADD_OV(namelen, size, &size)) + return SQFS_ERROR_ALLOC; + + strm = calloc(1, size); + base = (sqfs_ostream_t *)strm; + if (strm == NULL) + return SQFS_ERROR_ALLOC; + + sqfs_object_init(strm, stream_destroy, NULL); + memcpy(strm->filename, filename, namelen); + + ret = sqfs_block_processor_begin_file(proc, inode, NULL, flags); + if (ret != 0) { + free(strm); + return ret; + } + + strm->proc = sqfs_grab(proc); + base->append = stream_append; + base->flush = stream_flush; + base->get_filename = stream_get_filename; + + *out = base; + return 0; +} -- cgit v1.2.3