summaryrefslogtreecommitdiff
path: root/lib/sqfshelper/data_reader_dump.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqfshelper/data_reader_dump.c')
-rw-r--r--lib/sqfshelper/data_reader_dump.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/lib/sqfshelper/data_reader_dump.c b/lib/sqfshelper/data_reader_dump.c
new file mode 100644
index 0000000..23cd482
--- /dev/null
+++ b/lib/sqfshelper/data_reader_dump.c
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * data_reader_dump.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include "sqfs/block_processor.h"
+#include "data_reader.h"
+#include "highlevel.h"
+#include "util.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdio.h>
+
+int data_reader_dump(data_reader_t *data, const sqfs_inode_generic_t *inode,
+ int outfd, size_t block_size, bool allow_sparse)
+{
+ sqfs_block_t *blk;
+ uint64_t filesz;
+ size_t i, diff;
+ int err;
+
+ if (inode->base.type == SQFS_INODE_EXT_FILE) {
+ filesz = inode->data.file_ext.file_size;
+ } else {
+ filesz = inode->data.file.file_size;
+ }
+
+ if (allow_sparse && ftruncate(outfd, filesz))
+ goto fail_sparse;
+
+ for (i = 0; i < inode->num_file_blocks; ++i) {
+ if (SQFS_IS_SPARSE_BLOCK(inode->block_sizes[i]) &&
+ allow_sparse) {
+ if (filesz < block_size) {
+ diff = filesz;
+ filesz = 0;
+ } else {
+ diff = block_size;
+ filesz -= block_size;
+ }
+
+ if (lseek(outfd, diff, SEEK_CUR) == (off_t)-1)
+ goto fail_sparse;
+ } else {
+ err = data_reader_get_block(data, inode, i, &blk);
+ if (err) {
+ fprintf(stderr, "error reading "
+ "data block: %d\n", err);
+ return -1;
+ }
+
+ if (write_data("writing uncompressed block",
+ outfd, blk->data, blk->size)) {
+ free(blk);
+ return -1;
+ }
+
+ free(blk);
+ filesz -= blk->size;
+ }
+ }
+
+ if (filesz > 0) {
+ if (data_reader_get_fragment(data, inode, &blk)) {
+ fputs("error reading fragment block", stderr);
+ return -1;
+ }
+
+ if (write_data("writing uncompressed fragment", outfd,
+ blk->data, blk->size)) {
+ free(blk);
+ return -1;
+ }
+
+ free(blk);
+ }
+
+ return 0;
+fail_sparse:
+ perror("creating sparse output file");
+ return -1;
+}