From cdccc69c62579b0c13b35fad0728079652b8f3c9 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Tue, 31 Jan 2023 11:21:30 +0100 Subject: Move library source into src sub-directory Signed-off-by: David Oberhollenzer --- lib/common/src/data_writer.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lib/common/src/data_writer.c (limited to 'lib/common/src/data_writer.c') diff --git a/lib/common/src/data_writer.c b/lib/common/src/data_writer.c new file mode 100644 index 0000000..ceccaac --- /dev/null +++ b/lib/common/src/data_writer.c @@ -0,0 +1,54 @@ +/* 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