From 4d79f55f4a626a3cfd8bd18673aa29b48b16e137 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 25 Sep 2019 17:20:45 +0200 Subject: Cleanup: move the stdin sqfs_file_t wrapper out of libsquashfs Signed-off-by: David Oberhollenzer --- include/highlevel.h | 2 + include/sqfs/io.h | 14 ------ lib/sqfs/Makemodule.am | 2 +- lib/sqfs/io_stdin.c | 115 ------------------------------------------- lib/sqfshelper/Makemodule.am | 2 +- lib/sqfshelper/io_stdin.c | 114 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 131 deletions(-) delete mode 100644 lib/sqfs/io_stdin.c create mode 100644 lib/sqfshelper/io_stdin.c diff --git a/include/highlevel.h b/include/highlevel.h index b2bbd82..ed0f4bf 100644 --- a/include/highlevel.h +++ b/include/highlevel.h @@ -83,4 +83,6 @@ int sqfs_data_reader_dump(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, int outfd, size_t block_size, bool allow_sparse); +sqfs_file_t *sqfs_get_stdin_file(uint64_t size); + #endif /* HIGHLEVEL_H */ diff --git a/include/sqfs/io.h b/include/sqfs/io.h index f1a5ef0..d7a923c 100644 --- a/include/sqfs/io.h +++ b/include/sqfs/io.h @@ -156,20 +156,6 @@ extern "C" { */ SQFS_API sqfs_file_t *sqfs_open_file(const char *filename, int flags); -/** - * @brief Get a read-only file implementation that represents standard input - * - * This function creates a read-only file that represents STDIN. The file - * supports reading up to a specified number of bytes and only allows - * reading sequentially. - * - * @param size The alleged "size" of the file. - * - * @return A pointer to a file object on success, NULL on allocation failure. - */ -SQFS_API sqfs_file_t *sqfs_get_stdin_file(uint64_t size); - - /** * @brief Read a chunk from a file and turn it into a block that can be * fed to a block processor. diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index b07cc5b..36a8cc8 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -15,7 +15,7 @@ libsquashfs_la_SOURCES += lib/sqfs/read_super.c lib/sqfs/meta_reader.c libsquashfs_la_SOURCES += lib/sqfs/read_inode.c lib/sqfs/write_inode.c libsquashfs_la_SOURCES += lib/sqfs/dir_writer.c lib/sqfs/xattr_reader.c libsquashfs_la_SOURCES += lib/sqfs/read_table.c lib/sqfs/comp/compressor.c -libsquashfs_la_SOURCES += lib/sqfs/io_stdin.c lib/sqfs/comp/internal.h +libsquashfs_la_SOURCES += lib/sqfs/comp/internal.h libsquashfs_la_SOURCES += lib/sqfs/dir_reader.c lib/sqfs/read_tree.c libsquashfs_la_SOURCES += lib/sqfs/inode.c lib/sqfs/data_writer/fragment.c libsquashfs_la_SOURCES += lib/sqfs/data_writer/block.c lib/sqfs/io.c diff --git a/lib/sqfs/io_stdin.c b/lib/sqfs/io_stdin.c deleted file mode 100644 index a65fa0f..0000000 --- a/lib/sqfs/io_stdin.c +++ /dev/null @@ -1,115 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * io_stdin.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#define SQFS_BUILDING_DLL -#include "config.h" - -#include "sqfs/io.h" -#include "sqfs/error.h" - -#include -#include -#include - - -typedef struct { - sqfs_file_t base; - - uint64_t offset; - uint64_t size; -} sqfs_file_stdin_t; - - -static void stdin_destroy(sqfs_file_t *base) -{ - free(base); -} - -static int stdin_read_at(sqfs_file_t *base, uint64_t offset, - void *buffer, size_t size) -{ - sqfs_file_stdin_t *file = (sqfs_file_stdin_t *)base; - size_t temp_size = 0; - uint8_t *temp = NULL; - uint64_t diff; - ssize_t ret; - - if (offset < file->offset) - return SQFS_ERROR_IO; - - if (offset > file->offset) { - temp_size = 1024; - temp = alloca(temp_size); - } - - if (offset >= file->size || (offset + size) > file->size) - return SQFS_ERROR_OUT_OF_BOUNDS; - - while (size > 0) { - if (offset > file->offset) { - diff = file->offset - offset; - diff = diff > (uint64_t)temp_size ? temp_size : diff; - - ret = read(STDIN_FILENO, temp, diff); - } else { - ret = read(STDIN_FILENO, buffer, size); - } - - if (ret < 0) { - if (errno == EINTR) - continue; - return SQFS_ERROR_IO; - } - - if (ret == 0) - return SQFS_ERROR_OUT_OF_BOUNDS; - - if (offset <= file->offset) { - buffer = (char *)buffer + ret; - size -= ret; - offset += ret; - } - - file->offset += ret; - } - - return 0; -} - -static int stdin_write_at(sqfs_file_t *base, uint64_t offset, - const void *buffer, size_t size) -{ - (void)base; (void)offset; (void)buffer; (void)size; - return SQFS_ERROR_IO; -} - -static uint64_t stdin_get_size(const sqfs_file_t *base) -{ - return ((const sqfs_file_stdin_t *)base)->size; -} - -static int stdin_truncate(sqfs_file_t *base, uint64_t size) -{ - (void)base; (void)size; - return SQFS_ERROR_IO; -} - -sqfs_file_t *sqfs_get_stdin_file(uint64_t size) -{ - sqfs_file_stdin_t *file = calloc(1, sizeof(*file)); - sqfs_file_t *base = (sqfs_file_t *)file; - - if (file == NULL) - return NULL; - - file->size = size; - base->destroy = stdin_destroy; - base->read_at = stdin_read_at; - base->write_at = stdin_write_at; - base->get_size = stdin_get_size; - base->truncate = stdin_truncate; - return base; -} diff --git a/lib/sqfshelper/Makemodule.am b/lib/sqfshelper/Makemodule.am index b85e6a5..8a8deaa 100644 --- a/lib/sqfshelper/Makemodule.am +++ b/lib/sqfshelper/Makemodule.am @@ -8,6 +8,6 @@ libsqfshelper_a_SOURCES += lib/sqfshelper/data_reader_dump.c libsqfshelper_a_SOURCES += lib/sqfshelper/compress.c lib/sqfshelper/comp_opt.c libsqfshelper_a_SOURCES += include/data_writer.h lib/sqfshelper/data_writer.c libsqfshelper_a_SOURCES += lib/sqfshelper/write_xattr.c include/highlevel.h -libsqfshelper_a_SOURCES += lib/sqfshelper/get_path.c +libsqfshelper_a_SOURCES += lib/sqfshelper/get_path.c lib/sqfshelper/io_stdin.c noinst_LIBRARIES += libsqfshelper.a diff --git a/lib/sqfshelper/io_stdin.c b/lib/sqfshelper/io_stdin.c new file mode 100644 index 0000000..6cb45d4 --- /dev/null +++ b/lib/sqfshelper/io_stdin.c @@ -0,0 +1,114 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* + * io_stdin.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "highlevel.h" +#include "sqfs/error.h" + +#include +#include +#include + + +typedef struct { + sqfs_file_t base; + + uint64_t offset; + uint64_t size; +} sqfs_file_stdin_t; + + +static void stdin_destroy(sqfs_file_t *base) +{ + free(base); +} + +static int stdin_read_at(sqfs_file_t *base, uint64_t offset, + void *buffer, size_t size) +{ + sqfs_file_stdin_t *file = (sqfs_file_stdin_t *)base; + size_t temp_size = 0; + uint8_t *temp = NULL; + uint64_t diff; + ssize_t ret; + + if (offset < file->offset) + return SQFS_ERROR_IO; + + if (offset > file->offset) { + temp_size = 1024; + temp = alloca(temp_size); + } + + if (offset >= file->size || (offset + size) > file->size) + return SQFS_ERROR_OUT_OF_BOUNDS; + + while (size > 0) { + if (offset > file->offset) { + diff = file->offset - offset; + diff = diff > (uint64_t)temp_size ? temp_size : diff; + + ret = read(STDIN_FILENO, temp, diff); + } else { + ret = read(STDIN_FILENO, buffer, size); + } + + if (ret < 0) { + if (errno == EINTR) + continue; + return SQFS_ERROR_IO; + } + + if (ret == 0) + return SQFS_ERROR_OUT_OF_BOUNDS; + + if (offset <= file->offset) { + buffer = (char *)buffer + ret; + size -= ret; + offset += ret; + } + + file->offset += ret; + } + + return 0; +} + +static int stdin_write_at(sqfs_file_t *base, uint64_t offset, + const void *buffer, size_t size) +{ + (void)base; (void)offset; (void)buffer; (void)size; + return SQFS_ERROR_IO; +} + +static uint64_t stdin_get_size(const sqfs_file_t *base) +{ + return ((const sqfs_file_stdin_t *)base)->size; +} + +static int stdin_truncate(sqfs_file_t *base, uint64_t size) +{ + (void)base; (void)size; + return SQFS_ERROR_IO; +} + +sqfs_file_t *sqfs_get_stdin_file(uint64_t size) +{ + sqfs_file_stdin_t *file = calloc(1, sizeof(*file)); + sqfs_file_t *base = (sqfs_file_t *)file; + + if (file == NULL) + return NULL; + + file->size = size; + base->destroy = stdin_destroy; + base->read_at = stdin_read_at; + base->write_at = stdin_write_at; + base->get_size = stdin_get_size; + base->truncate = stdin_truncate; + return base; +} -- cgit v1.2.3