summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-13 20:30:40 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2020-09-16 09:34:35 +0200
commit4b994ac359757098ebc09263fff9e2290a58de71 (patch)
treef88df7f3d5359c8abf2fd781b77f0962bdbb3694 /lib
parent1e255d0f6c472bb3c710aea1ea8dc5d27c0fba4a (diff)
Implement a data writer based output stream
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/common/Makemodule.am2
-rw-r--r--lib/common/data_writer_ostream.c91
2 files changed, 92 insertions, 1 deletions
diff --git a/lib/common/Makemodule.am b/lib/common/Makemodule.am
index c59ab4c..df08894 100644
--- a/lib/common/Makemodule.am
+++ b/lib/common/Makemodule.am
@@ -6,7 +6,7 @@ libcommon_a_SOURCES += lib/common/data_writer.c include/common.h
libcommon_a_SOURCES += lib/common/get_path.c lib/common/io_stdin.c
libcommon_a_SOURCES += lib/common/writer.c lib/common/perror.c
libcommon_a_SOURCES += lib/common/mkdir_p.c lib/common/parse_size.c
-libcommon_a_SOURCES += lib/common/print_size.c
+libcommon_a_SOURCES += lib/common/print_size.c lib/common/data_writer_ostream.c
libcommon_a_CFLAGS = $(AM_CFLAGS) $(LZO_CFLAGS)
if WITH_LZO
diff --git a/lib/common/data_writer_ostream.c b/lib/common/data_writer_ostream.c
new file mode 100644
index 0000000..94a04f2
--- /dev/null
+++ b/lib/common/data_writer_ostream.c
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * data_writer_ostream.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+#include "common.h"
+
+#include <stdlib.h>
+
+typedef struct{
+ ostream_t base;
+
+ sqfs_block_processor_t *proc;
+ const char *filename;
+} data_writer_ostream_t;
+
+static int stream_append(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(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(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);
+}
+
+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_object_t *obj = (sqfs_object_t *)strm;
+ ostream_t *base = (ostream_t *)strm;
+ int ret;
+
+ if (strm == NULL) {
+ perror(filename);
+ return 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;
+ obj->destroy = stream_destroy;
+ return base;
+}