diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-12 21:21:40 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-15 13:38:25 +0200 |
commit | ba99ef34e7b073c03519ef74f017091de6c9bee8 (patch) | |
tree | 8c134f72990200550ac96e46bd47d4cc0ba85810 | |
parent | e811851deba9c45f3d9b3c5b4ad5eaa7945382d5 (diff) |
Move sqfs_istream_t & sqfs_ostream_t into libsquashfs
For now, only the interfaces and helper functions are moved, the
concrete implementations remain in libio.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
39 files changed, 243 insertions, 244 deletions
diff --git a/bin/tar2sqfs/src/process_tarball.c b/bin/tar2sqfs/src/process_tarball.c index 5e7f030..639adf8 100644 --- a/bin/tar2sqfs/src/process_tarball.c +++ b/bin/tar2sqfs/src/process_tarball.c @@ -28,7 +28,7 @@ static int write_file(sqfs_writer_t *sqfs, dir_iterator_t *it, } do { - ret = istream_splice(in, out, cfg.block_size); + ret = sqfs_istream_splice(in, out, cfg.block_size); } while (ret > 0); out->flush(out); diff --git a/include/common.h b/include/common.h index c289458..d2e3f73 100644 --- a/include/common.h +++ b/include/common.h @@ -17,10 +17,10 @@ #include "sqfs/block.h" #include "sqfs/xattr.h" #include "sqfs/dir.h" +#include "sqfs/io.h" #include "simple_writer.h" #include "compress_cli.h" -#include "io/ostream.h" #include "io/file.h" #include "io/std.h" #include "compat.h" diff --git a/include/io/file.h b/include/io/file.h index 1072b6a..fa0abc0 100644 --- a/include/io/file.h +++ b/include/io/file.h @@ -8,7 +8,6 @@ #define IO_FILE_H #include "io/istream.h" -#include "io/ostream.h" #if defined(_WIN32) || defined(__WINDOWS__) #include <handleapi.h> diff --git a/include/io/istream.h b/include/io/istream.h index f4ffcb2..f41be2e 100644 --- a/include/io/istream.h +++ b/include/io/istream.h @@ -8,66 +8,6 @@ #define IO_ISTREAM_H #include "sqfs/predef.h" -#include "io/ostream.h" - -typedef struct sqfs_istream_t sqfs_istream_t; - -/** - * @interface sqfs_istream_t - * - * @extends sqfs_object_t - * - * @brief A sequential, read-only data stream. - */ -struct sqfs_istream_t { - sqfs_object_t base; - - /** - * @brief Peek into the data buffered in an istream - * - * If the internal buffer is empty, the function tries to fetch more, - * which can block. It returns a positive return code if there is no - * more data to be read, a negative error code if reading failed. Since - * this and other functions can alter the buffer pointer and contents, - * do not store the pointers returned here across function calls. - * - * Higher level functions like @ref istream_read (providing a - * Unix read() style API) are built on top of this primitive. - * - * @param strm A pointer to an sqfs_istream_t implementation. - * @param out Returns a pointer into an internal buffer on success. - * @param size Returns the number of bytes available in the buffer. - * @param want A number of bytes that the reader would like to have. - * If there is less than this available, the implementation - * can choose to do a blocking precache. - * - * @return Zero on success, a negative error code on failure, - * a postive number on EOF. - */ - int (*get_buffered_data)(sqfs_istream_t *strm, const sqfs_u8 **out, - size_t *size, size_t want); - - /** - * @brief Mark a section of the internal buffer of an istream as used - * - * This marks the first `count` bytes of the internal buffer as used, - * forcing get_buffered_data to return data afterwards and potentially - * try to load more data. - * - * @param strm A pointer to an sqfs_istream_t implementation. - * @param count The number of bytes used up. - */ - void (*advance_buffer)(sqfs_istream_t *strm, size_t count); - - /** - * @brief Get the underlying filename of an input stream. - * - * @param strm The input stream to get the filename from. - * - * @return A string holding the underlying filename. - */ - const char *(*get_filename)(sqfs_istream_t *strm); -}; enum { ISTREAM_LINE_LTRIM = 0x01, @@ -108,48 +48,6 @@ extern "C" { SQFS_INTERNAL int istream_get_line(sqfs_istream_t *strm, char **out, size_t *line_num, int flags); -/** - * @brief Read data from an input stream - * - * @memberof sqfs_istream_t - * - * @param strm A pointer to an input stream. - * @param data A buffer to read into. - * @param size The number of bytes to read into the buffer. - * - * @return The number of bytes actually read on success, -1 on failure, - * 0 on end-of-file. - */ -SQFS_INTERNAL sqfs_s32 istream_read(sqfs_istream_t *strm, - void *data, size_t size); - -/** - * @brief Skip over a number of bytes in an input stream. - * - * @memberof sqfs_istream_t - * - * @param strm A pointer to an input stream. - * @param size The number of bytes to seek forward. - * - * @return Zero on success, -1 on failure. - */ -SQFS_INTERNAL int istream_skip(sqfs_istream_t *strm, sqfs_u64 size); - -/** - * @brief Dump data from an input stream to an output stream - * - * @memberof sqfs_istream_t - * - * @param in A pointer to an input stream to read from. - * @param out A pointer to an output stream to append to. - * @param size The number of bytes to copy over. - * - * @return The number of bytes copied on success, -1 on failure, - * 0 on end-of-file. - */ -SQFS_INTERNAL sqfs_s32 istream_splice(sqfs_istream_t *in, sqfs_ostream_t *out, - sqfs_u32 size); - #ifdef __cplusplus } #endif diff --git a/include/io/ostream.h b/include/io/ostream.h deleted file mode 100644 index d1781d2..0000000 --- a/include/io/ostream.h +++ /dev/null @@ -1,60 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * ostream.h - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#ifndef IO_OSTREAM_H -#define IO_OSTREAM_H - -#include "sqfs/predef.h" - -typedef struct sqfs_ostream_t sqfs_ostream_t; - -/** - * @interface sqfs_ostream_t - * - * @extends sqfs_object_t - * - * @brief An append-only data stream. - */ -struct sqfs_ostream_t { - sqfs_object_t base; - - /** - * @brief Append a block of data to an output stream. - * - * @param strm A pointer to an output stream. - * @param data A pointer to the data block to append. If NULL, - * synthesize a chunk of zero bytes. - * @param size The number of bytes to append. - * - * @return Zero on success, -1 on failure. - */ - int (*append)(sqfs_ostream_t *strm, const void *data, size_t size); - - /** - * @brief Process all pending, buffered data and flush it to disk. - * - * If the stream performs some kind of transformation (e.g. transparent - * data compression), flushing caues the wrapped format to insert a - * termination token. Only call this function when you are absolutely - * DONE appending data, shortly before destroying the stream. - * - * @param strm A pointer to an output stream. - * - * @return Zero on success, -1 on failure. - */ - int (*flush)(sqfs_ostream_t *strm); - - /** - * @brief Get the underlying filename of a output stream. - * - * @param strm The output stream to get the filename from. - * - * @return A string holding the underlying filename. - */ - const char *(*get_filename)(sqfs_ostream_t *strm); -}; - -#endif /* IO_OSTREAM_H */ diff --git a/include/io/std.h b/include/io/std.h index 3a8bbf8..f622491 100644 --- a/include/io/std.h +++ b/include/io/std.h @@ -8,7 +8,6 @@ #define IO_STD_H #include "io/istream.h" -#include "io/ostream.h" #ifdef __cplusplus extern "C" { diff --git a/include/io/xfrm.h b/include/io/xfrm.h index c54bc9f..f0ff1ce 100644 --- a/include/io/xfrm.h +++ b/include/io/xfrm.h @@ -8,7 +8,6 @@ #define IO_XFRM_H #include "io/istream.h" -#include "io/ostream.h" #include "xfrm/stream.h" #ifdef __cplusplus diff --git a/include/sqfs/io.h b/include/sqfs/io.h index a1fb169..402b6de 100644 --- a/include/sqfs/io.h +++ b/include/sqfs/io.h @@ -25,7 +25,12 @@ /** * @file io.h * - * @brief Contains the @ref sqfs_file_t interface for abstracting file I/O + * @brief Contains low-level interfaces for abstracting file I/O + * + * The @ref sqfs_file_t interface abstracts I/O on a random-acces sread/write + * file, @ref sqfs_ostream_t represents a buffered, sequential, append only + * data stream, @ref sqfs_istream_t represents a buffered, sequential, read only + * data stream. */ /** @@ -151,6 +156,109 @@ struct sqfs_file_t { int (*truncate)(sqfs_file_t *file, sqfs_u64 size); }; +/** + * @interface sqfs_istream_t + * + * @extends sqfs_object_t + * + * @brief A sequential, read-only data stream. + */ +struct sqfs_istream_t { + sqfs_object_t base; + + /** + * @brief Peek into the data buffered in an istream + * + * If the internal buffer is empty, the function tries to fetch more, + * which can block. It returns a positive return code if there is no + * more data to be read, a negative error code if reading failed. Since + * this and other functions can alter the buffer pointer and contents, + * do not store the pointers returned here across function calls. + * + * Higher level functions like @ref sqfs_istream_read (providing a + * Unix read() style API) are built on top of this primitive. + * + * @param strm A pointer to an sqfs_istream_t implementation. + * @param out Returns a pointer into an internal buffer on success. + * @param size Returns the number of bytes available in the buffer. + * @param want A number of bytes that the reader would like to have. + * If there is less than this available, the implementation + * can choose to do a blocking precache. + * + * @return Zero on success, a negative error code on failure, + * a postive number on EOF. + */ + int (*get_buffered_data)(sqfs_istream_t *strm, const sqfs_u8 **out, + size_t *size, size_t want); + + /** + * @brief Mark a section of the internal buffer of an istream as used + * + * This marks the first `count` bytes of the internal buffer as used, + * forcing get_buffered_data to return data afterwards and potentially + * try to load more data. + * + * @param strm A pointer to an sqfs_istream_t implementation. + * @param count The number of bytes used up. + */ + void (*advance_buffer)(sqfs_istream_t *strm, size_t count); + + /** + * @brief Get the underlying filename of an input stream. + * + * @param strm The input stream to get the filename from. + * + * @return A string holding the underlying filename. + */ + const char *(*get_filename)(sqfs_istream_t *strm); +}; + +/** + * @interface sqfs_ostream_t + * + * @extends sqfs_object_t + * + * @brief An append-only data stream. + */ +struct sqfs_ostream_t { + sqfs_object_t base; + + /** + * @brief Append a block of data to an output stream. + * + * @param strm A pointer to an output stream. + * @param data A pointer to the data block to append. If NULL, + * synthesize a chunk of zero bytes. + * @param size The number of bytes to append. + * + * @return Zero on success, -1 on failure. + */ + int (*append)(sqfs_ostream_t *strm, const void *data, size_t size); + + /** + * @brief Process all pending, buffered data and flush it to disk. + * + * If the stream performs some kind of transformation (e.g. transparent + * data compression), flushing caues the wrapped format to insert a + * termination token. Only call this function when you are absolutely + * DONE appending data, shortly before destroying the stream. + * + * @param strm A pointer to an output stream. + * + * @return Zero on success, -1 on failure. + */ + int (*flush)(sqfs_ostream_t *strm); + + /** + * @brief Get the underlying filename of a output stream. + * + * @param strm The output stream to get the filename from. + * + * @return A string holding the underlying filename. + */ + const char *(*get_filename)(sqfs_ostream_t *strm); +}; + #ifdef __cplusplus extern "C" { #endif @@ -174,6 +282,50 @@ extern "C" { */ SQFS_API sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags); +/** + * @brief Read data from an input stream + * + * @memberof sqfs_istream_t + * + * This function implements a Unix-style read() function on top of + * an @ref sqfs_istream_t, taking care of buffer management internally. + * + * @param strm A pointer to an input stream. + * @param data A buffer to read into. + * @param size The number of bytes to read into the buffer. + * + * @return The number of bytes actually read on success, a + * negative @ref SQFS_ERROR code on failure, 0 on end-of-file. + */ +SQFS_API sqfs_s32 sqfs_istream_read(sqfs_istream_t *strm, + void *data, size_t size); + +/** + * @brief Skip over a number of bytes in an input stream. + * + * @memberof sqfs_istream_t + * + * @param strm A pointer to an input stream. + * @param size The number of bytes to seek forward. + * + * @return Zero on success, a negative @ref SQFS_ERROR code on failure. + */ +SQFS_API int sqfs_istream_skip(sqfs_istream_t *strm, sqfs_u64 size); + +/** + * @brief Dump data from an input stream to an output stream + * + * @memberof sqfs_istream_t + * + * @param in A pointer to an input stream to read from. + * @param out A pointer to an output stream to append to. + * @param size The number of bytes to copy over. + * + * @return Zero on success, a negative @ref SQFS_ERROR code on failure. + */ +SQFS_API sqfs_s32 sqfs_istream_splice(sqfs_istream_t *in, sqfs_ostream_t *out, + sqfs_u32 size); + #ifdef __cplusplus } #endif diff --git a/include/sqfs/predef.h b/include/sqfs/predef.h index b0baf92..aae95e4 100644 --- a/include/sqfs/predef.h +++ b/include/sqfs/predef.h @@ -96,6 +96,8 @@ typedef struct sqfs_block_processor_stats_t sqfs_block_processor_stats_t; typedef struct sqfs_block_processor_desc_t sqfs_block_processor_desc_t; typedef struct sqfs_readdir_state_t sqfs_readdir_state_t; typedef struct sqfs_xattr_t sqfs_xattr_t; +typedef struct sqfs_istream_t sqfs_istream_t; +typedef struct sqfs_ostream_t sqfs_ostream_t; typedef struct sqfs_fragment_t sqfs_fragment_t; typedef struct sqfs_dir_header_t sqfs_dir_header_t; diff --git a/include/tar/tar.h b/include/tar/tar.h index 60c2a3d..1f1b1b4 100644 --- a/include/tar/tar.h +++ b/include/tar/tar.h @@ -10,8 +10,8 @@ #include "config.h" #include "compat.h" #include "io/istream.h" -#include "io/ostream.h" #include "io/dir_iterator.h" +#include "sqfs/io.h" #include <stdbool.h> #include <stdint.h> diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am index badaa99..9e442be 100644 --- a/lib/io/Makemodule.am +++ b/lib/io/Makemodule.am @@ -1,7 +1,7 @@ -libio_a_SOURCES = include/io/istream.h include/io/ostream.h include/io/xfrm.h \ +libio_a_SOURCES = include/io/istream.h include/io/xfrm.h \ include/io/file.h include/io/std.h include/io/dir_entry.h \ include/io/dir_iterator.h include/io/mem.h lib/io/src/internal.h \ - lib/io/src/istream.c lib/io/src/get_line.c lib/io/src/xfrm/ostream.c \ + lib/io/src/get_line.c lib/io/src/xfrm/ostream.c \ lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c \ lib/io/src/dir_entry.c lib/io/src/mem.c libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS) @@ -18,8 +18,7 @@ endif noinst_LIBRARIES += libio.a -LIBIO_TESTS = test_istream_mem test_get_line test_istream_read \ - test_istream_skip test_stream_splice test_dir_iterator \ +LIBIO_TESTS = test_istream_mem test_get_line test_dir_iterator \ test_dir_tree_iterator test_dir_tree_iterator2 test_dir_tree_iterator3 test_istream_mem_SOURCES = lib/io/test/istream_mem.c @@ -30,15 +29,6 @@ test_get_line_SOURCES = lib/io/test/get_line.c test_get_line_LDADD = libio.a libcompat.a test_get_line_CPPFLAGS = $(AM_CPPFLAGS) -test_istream_read_SOURCES = lib/io/test/istream_read.c -test_istream_read_LDADD = libio.a libutil.a libcompat.a - -test_istream_skip_SOURCES = lib/io/test/istream_skip.c -test_istream_skip_LDADD = libio.a libutil.a libcompat.a - -test_stream_splice_SOURCES = lib/io/test/stream_splice.c -test_stream_splice_LDADD = libio.a libutil.a libcompat.a - test_dir_iterator_SOURCES = lib/io/test/dir_iterator.c test_dir_iterator_LDADD = libio.a libutil.a libcompat.a test_dir_iterator_CPPFLAGS = $(AM_CPPFLAGS) @@ -61,7 +51,7 @@ test_dir_tree_iterator3_CPPFLAGS += -DTESTPATH=$(top_srcdir)/lib/io/test/testdir if WITH_XZ test_io_xfrm_xz_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_xz_LDADD = libio.a libxfrm.a libcompat.a $(XZ_LIBS) +test_io_xfrm_xz_LDADD = libsquashfs.la libio.a libxfrm.a libcompat.a $(XZ_LIBS) test_io_xfrm_xz_CPPFLAGS = $(AM_CPPFLAGS) -DDO_XZ=1 LIBIO_TESTS += test_io_xfrm_xz @@ -69,7 +59,8 @@ endif if WITH_BZIP2 test_io_xfrm_bzip2_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_bzip2_LDADD = libio.a libxfrm.a libcompat.a $(BZIP2_LIBS) +test_io_xfrm_bzip2_LDADD = libsquashfs.la libio.a libxfrm.a \ + libcompat.a $(BZIP2_LIBS) test_io_xfrm_bzip2_CPPFLAGS = $(AM_CPPFLAGS) -DDO_BZIP2=1 LIBIO_TESTS += test_io_xfrm_bzip2 @@ -77,7 +68,8 @@ endif if WITH_GZIP test_io_xfrm_gzip_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_gzip_LDADD = libio.a libxfrm.a libcompat.a $(ZLIB_LIBS) +test_io_xfrm_gzip_LDADD = libsquashfs.la libio.a libxfrm.a \ + libcompat.a $(ZLIB_LIBS) test_io_xfrm_gzip_CPPFLAGS = $(AM_CPPFLAGS) -DDO_GZIP=1 LIBIO_TESTS += test_io_xfrm_gzip @@ -86,7 +78,8 @@ endif if WITH_ZSTD if HAVE_ZSTD_STREAM test_io_xfrm_zstd_SOURCES = lib/io/test/xfrm.c -test_io_xfrm_zstd_LDADD = libio.a libxfrm.a libcompat.a $(ZSTD_LIBS) +test_io_xfrm_zstd_LDADD = libsquashfs.la libio.a libxfrm.a \ + libcompat.a $(ZSTD_LIBS) test_io_xfrm_zstd_CPPFLAGS = $(AM_CPPFLAGS) -DDO_ZSTD=1 LIBIO_TESTS += test_io_xfrm_zstd diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c index 9ab4928..ad37be6 100644 --- a/lib/io/src/get_line.c +++ b/lib/io/src/get_line.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> */ #include "internal.h" +#include "sqfs/io.h" static void ltrim(char *buffer) { diff --git a/lib/io/src/internal.h b/lib/io/src/internal.h index 1e51012..e010727 100644 --- a/lib/io/src/internal.h +++ b/lib/io/src/internal.h @@ -10,7 +10,6 @@ #include "config.h" #include "compat.h" #include "io/istream.h" -#include "io/ostream.h" #include "io/file.h" #include "io/xfrm.h" #include "io/std.h" diff --git a/lib/io/src/mem.c b/lib/io/src/mem.c index 435a169..7150d1f 100644 --- a/lib/io/src/mem.c +++ b/lib/io/src/mem.c @@ -7,6 +7,7 @@ #include "config.h" #include "io/mem.h" #include "compat.h" +#include "sqfs/io.h" #include <stdlib.h> #include <string.h> diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c index fc76bab..39d570f 100644 --- a/lib/io/src/unix/istream.c +++ b/lib/io/src/unix/istream.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> */ #include "../internal.h" +#include "sqfs/io.h" typedef struct { sqfs_istream_t base; diff --git a/lib/io/src/win32/istream.c b/lib/io/src/win32/istream.c index 223b20d..e6cb266 100644 --- a/lib/io/src/win32/istream.c +++ b/lib/io/src/win32/istream.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> */ #include "../internal.h" +#include "sqfs/io.h" #define WIN32_LEAN_AND_MEAN #include <windows.h> diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c index 5c23d28..c499f6c 100644 --- a/lib/io/src/xfrm/istream.c +++ b/lib/io/src/xfrm/istream.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> */ #include "../internal.h" +#include "sqfs/io.h" typedef struct istream_xfrm_t { sqfs_istream_t base; diff --git a/lib/io/src/xfrm/ostream.c b/lib/io/src/xfrm/ostream.c index 81c19dd..4c77f42 100644 --- a/lib/io/src/xfrm/ostream.c +++ b/lib/io/src/xfrm/ostream.c @@ -5,6 +5,7 @@ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> */ #include "../internal.h" +#include "sqfs/io.h" typedef struct ostream_xfrm_t { sqfs_ostream_t base; diff --git a/lib/io/test/istream_mem.c b/lib/io/test/istream_mem.c index f364fd3..d5e0c2c 100644 --- a/lib/io/test/istream_mem.c +++ b/lib/io/test/istream_mem.c @@ -8,6 +8,7 @@ #include "util/test.h" #include "io/mem.h" +#include "sqfs/io.h" static const size_t end0 = 449; /* region 1: filled with 'A' */ static const size_t end1 = 521; /* region 2: filled with 'B' */ diff --git a/lib/io/test/xfrm.c b/lib/io/test/xfrm.c index 1b48e6f..b871610 100644 --- a/lib/io/test/xfrm.c +++ b/lib/io/test/xfrm.c @@ -7,6 +7,7 @@ #include "xfrm/compress.h" #include "xfrm/stream.h" #include "util/test.h" +#include "sqfs/io.h" #include "io/xfrm.h" #include "io/mem.h" @@ -430,15 +431,15 @@ static void run_unpack_test(const void *blob, size_t size) TEST_EQUAL_UI(((sqfs_object_t *)mem_istream)->refcount, 2); for (i = 0; i < (sizeof(orig) - 1); ++i) { - ret = istream_read(istream, &c, 1); + ret = sqfs_istream_read(istream, &c, 1); TEST_EQUAL_I(ret, 1); TEST_EQUAL_I(c, orig[i]); } - ret = istream_read(istream, &c, 1); + ret = sqfs_istream_read(istream, &c, 1); TEST_EQUAL_I(ret, 0); - ret = istream_read(istream, &c, 1); + ret = sqfs_istream_read(istream, &c, 1); TEST_EQUAL_I(ret, 0); sqfs_drop(istream); diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index d54134c..2727b11 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -30,7 +30,7 @@ libsquashfs_la_SOURCES = $(LIBSQFS_HEARDS) lib/sqfs/src/id_table.c \ lib/sqfs/src/block_processor/block_processor.c \ lib/sqfs/src/block_processor/backend.c \ lib/sqfs/src/frag_table.c lib/sqfs/src/block_writer.c \ - lib/sqfs/src/misc.c + lib/sqfs/src/misc.c lib/sqfs/src/istream.c libsquashfs_la_CPPFLAGS = $(AM_CPPFLAGS) libsquashfs_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LIBSQUASHFS_SO_VERSION) libsquashfs_la_CFLAGS = $(AM_CFLAGS) $(PTHREAD_CFLAGS) $(ZLIB_CFLAGS) @@ -122,9 +122,18 @@ xattr_benchmark_LDADD = libcommon.a libsquashfs.la libcompat.a test_get_node_path_SOURCES = lib/sqfs/test/get_node_path.c test_get_node_path_LDADD = libcommon.a libsquashfs.la libcompat.a -LIBSQFS_TESTS = \ - test_abi test_xattr test_table test_xattr_writer test_get_node_path +test_istream_read_SOURCES = lib/sqfs/test/istream_read.c +test_istream_read_LDADD = libio.a libsquashfs.la libutil.a libcompat.a + +test_istream_skip_SOURCES = lib/sqfs/test/istream_skip.c +test_istream_skip_LDADD = libsquashfs.la libio.a libutil.a libcompat.a +test_stream_splice_SOURCES = lib/sqfs/test/stream_splice.c +test_stream_splice_LDADD = libsquashfs.la libio.a libutil.a libcompat.a + +LIBSQFS_TESTS = \ + test_abi test_xattr test_table test_xattr_writer test_get_node_path \ + test_istream_read test_istream_skip test_stream_splice noinst_PROGRAMS += xattr_benchmark check_PROGRAMS += $(LIBSQFS_TESTS) diff --git a/lib/io/src/istream.c b/lib/sqfs/src/istream.c index 6fbc67b..3d89461 100644 --- a/lib/io/src/istream.c +++ b/lib/sqfs/src/istream.c @@ -1,13 +1,17 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ /* * istream.c * * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> */ -#include "internal.h" +#define SQFS_BUILDING_DLL +#include "config.h" +#include "sqfs/io.h" -sqfs_s32 istream_read(sqfs_istream_t *strm, void *data, size_t size) +#include <string.h> + +sqfs_s32 sqfs_istream_read(sqfs_istream_t *strm, void *data, size_t size) { sqfs_s32 total = 0; @@ -38,7 +42,7 @@ sqfs_s32 istream_read(sqfs_istream_t *strm, void *data, size_t size) return total; } -int istream_skip(sqfs_istream_t *strm, sqfs_u64 size) +int sqfs_istream_skip(sqfs_istream_t *strm, sqfs_u64 size) { while (size > 0) { const sqfs_u8 *ptr; @@ -48,11 +52,8 @@ int istream_skip(sqfs_istream_t *strm, sqfs_u64 size) ret = strm->get_buffered_data(strm, &ptr, &diff, size); if (ret < 0) return ret; - if (ret > 0) { - fprintf(stderr, "%s: unexpected end-of-file\n", - strm->get_filename(strm)); - return -1; - } + if (ret > 0) + break; if ((sqfs_u64)diff > size) diff = size; @@ -64,7 +65,8 @@ int istream_skip(sqfs_istream_t *strm, sqfs_u64 size) return 0; } -sqfs_s32 istream_splice(sqfs_istream_t *in, sqfs_ostream_t *out, sqfs_u32 size) +sqfs_s32 sqfs_istream_splice(sqfs_istream_t *in, sqfs_ostream_t *out, + sqfs_u32 size) { sqfs_s32 total = 0; @@ -85,8 +87,9 @@ sqfs_s32 istream_splice(sqfs_istream_t *in, sqfs_ostream_t *out, sqfs_u32 size) if (diff > size) diff = size; - if (out->append(out, ptr, diff)) - return -1; + ret = out->append(out, ptr, diff); + if (ret) + return ret; total += diff; size -= diff; diff --git a/lib/io/test/istream_read.c b/lib/sqfs/test/istream_read.c index 66fec4b..f8facea 100644 --- a/lib/io/test/istream_read.c +++ b/lib/sqfs/test/istream_read.c @@ -6,8 +6,8 @@ */ #include "config.h" -#include "io/istream.h" #include "util/test.h" +#include "sqfs/io.h" #include "io/mem.h" static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */ @@ -50,7 +50,7 @@ int main(int argc, char **argv) if (read_diff > sizeof(read_buffer)) read_diff = sizeof(read_buffer); - int ret = istream_read(dummy, read_buffer, read_diff); + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); TEST_ASSERT(ret > 0); TEST_ASSERT((size_t)ret <= read_diff); @@ -68,7 +68,7 @@ int main(int argc, char **argv) if (read_diff > sizeof(read_buffer)) read_diff = sizeof(read_buffer); - int ret = istream_read(dummy, read_buffer, read_diff); + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); TEST_ASSERT(ret > 0); TEST_ASSERT((size_t)ret <= read_diff); @@ -83,7 +83,7 @@ int main(int argc, char **argv) for (;;) { size_t read_diff = sizeof(read_buffer); - int ret = istream_read(dummy, read_buffer, read_diff); + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); TEST_ASSERT(ret >= 0); TEST_ASSERT((size_t)ret <= read_diff); diff --git a/lib/io/test/istream_skip.c b/lib/sqfs/test/istream_skip.c index 0fc3e11..d8a81f2 100644 --- a/lib/io/test/istream_skip.c +++ b/lib/sqfs/test/istream_skip.c @@ -6,7 +6,7 @@ */ #include "config.h" -#include "io/istream.h" +#include "sqfs/io.h" #include "util/test.h" #include "io/mem.h" @@ -50,7 +50,7 @@ int main(int argc, char **argv) if (read_diff > sizeof(read_buffer)) read_diff = sizeof(read_buffer); - int ret = istream_read(dummy, read_buffer, read_diff); + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); TEST_ASSERT(ret > 0); TEST_ASSERT((size_t)ret <= read_diff); @@ -63,7 +63,7 @@ int main(int argc, char **argv) /* region 2 */ { - int ret = istream_skip(dummy, end2 - end1); + int ret = sqfs_istream_skip(dummy, end2 - end1); TEST_EQUAL_I(ret, 0); read_off += (end2 - end1); } @@ -72,7 +72,7 @@ int main(int argc, char **argv) for (;;) { size_t read_diff = sizeof(read_buffer); - int ret = istream_read(dummy, read_buffer, read_diff); + int ret = sqfs_istream_read(dummy, read_buffer, read_diff); TEST_ASSERT(ret >= 0); TEST_ASSERT((size_t)ret <= read_diff); diff --git a/lib/io/test/stream_splice.c b/lib/sqfs/test/stream_splice.c index a9edd76..6f40d1f 100644 --- a/lib/io/test/stream_splice.c +++ b/lib/sqfs/test/stream_splice.c @@ -6,10 +6,9 @@ */ #include "config.h" -#include "io/istream.h" -#include "io/ostream.h" #include "io/mem.h" #include "util/test.h" +#include "sqfs/io.h" static const sqfs_u64 end0 = 449; /* region 1: filled with 'A' */ static const sqfs_u64 end1 = 521; /* region 2: filled with 'B' */ @@ -43,7 +42,6 @@ static sqfs_ostream_t out = { out_append, NULL, NULL, - NULL, }; static int out_append(sqfs_ostream_t *strm, const void *data, size_t size) @@ -79,7 +77,7 @@ int main(int argc, char **argv) TEST_NOT_NULL(in); for (;;) { - ret = istream_splice(in, &out, 211); + ret = sqfs_istream_splice(in, &out, 211); TEST_ASSERT(ret >= 0); if (ret == 0) diff --git a/lib/tar/src/iterator.c b/lib/tar/src/iterator.c index 5920b46..93c931c 100644 --- a/lib/tar/src/iterator.c +++ b/lib/tar/src/iterator.c @@ -177,13 +177,13 @@ static int it_next(dir_iterator_t *it, dir_entry_t **out) return tar->state; retry: if (tar->record_size > 0) { - ret = istream_skip(tar->stream, tar->record_size); + ret = sqfs_istream_skip(tar->stream, tar->record_size); if (ret) goto fail; } if (tar->padding > 0) { - ret = istream_skip(tar->stream, tar->padding); + ret = sqfs_istream_skip(tar->stream, tar->padding); if (ret) goto fail; } diff --git a/lib/tar/src/read_header.c b/lib/tar/src/read_header.c index 3117d8a..16fc9d7 100644 --- a/lib/tar/src/read_header.c +++ b/lib/tar/src/read_header.c @@ -175,7 +175,7 @@ int read_header(sqfs_istream_t *fp, tar_header_decoded_t *out) memset(out, 0, sizeof(*out)); for (;;) { - ret = istream_read(fp, &hdr, sizeof(hdr)); + ret = sqfs_istream_read(fp, &hdr, sizeof(hdr)); if (ret < 0) goto fail; @@ -226,7 +226,7 @@ int read_header(sqfs_istream_t *fp, tar_header_decoded_t *out) goto fail; if (pax_size % 512) pax_size += 512 - (pax_size % 512); - istream_skip(fp, pax_size); + sqfs_istream_skip(fp, pax_size); continue; case TAR_TYPE_PAX: clear_header(out); diff --git a/lib/tar/src/read_sparse_map_new.c b/lib/tar/src/read_sparse_map_new.c index a1f37fd..4e317a8 100644 --- a/lib/tar/src/read_sparse_map_new.c +++ b/lib/tar/src/read_sparse_map_new.c @@ -41,7 +41,7 @@ sparse_map_t *read_gnu_new_sparse(sqfs_istream_t *fp, tar_header_decoded_t *out) if (out->record_size < 512) goto fail_format; - ret = istream_read(fp, buffer, 512); + ret = sqfs_istream_read(fp, buffer, 512); if (ret < 0) goto fail; @@ -68,7 +68,7 @@ sparse_map_t *read_gnu_new_sparse(sqfs_istream_t *fp, tar_header_decoded_t *out) if (out->record_size < 512) goto fail_format; - ret = istream_read(fp, buffer + 512, 512); + ret = sqfs_istream_read(fp, buffer + 512, 512); if (ret < 0) goto fail; diff --git a/lib/tar/src/read_sparse_map_old.c b/lib/tar/src/read_sparse_map_old.c index 832329b..1794073 100644 --- a/lib/tar/src/read_sparse_map_old.c +++ b/lib/tar/src/read_sparse_map_old.c @@ -59,7 +59,7 @@ sparse_map_t *read_gnu_old_sparse(sqfs_istream_t *fp, tar_header_t *hdr) return list; do { - ret = istream_read(fp, &sph, sizeof(sph)); + ret = sqfs_istream_read(fp, &sph, sizeof(sph)); if (ret < 0) goto fail; diff --git a/lib/tar/src/record_to_memory.c b/lib/tar/src/record_to_memory.c index 1bd31aa..597d6f8 100644 --- a/lib/tar/src/record_to_memory.c +++ b/lib/tar/src/record_to_memory.c @@ -18,7 +18,7 @@ char *record_to_memory(sqfs_istream_t *fp, size_t size) if (buffer == NULL) goto fail_errno; - ret = istream_read(fp, buffer, size); + ret = sqfs_istream_read(fp, buffer, size); if (ret < 0) goto fail; @@ -28,7 +28,7 @@ char *record_to_memory(sqfs_istream_t *fp, size_t size) } if (size % 512) { - if (istream_skip(fp, 512 - (size % 512))) + if (sqfs_istream_skip(fp, 512 - (size % 512))) goto fail; } diff --git a/lib/tar/test/tar_fuzz.c b/lib/tar/test/tar_fuzz.c index bdea98e..92c0952 100644 --- a/lib/tar/test/tar_fuzz.c +++ b/lib/tar/test/tar_fuzz.c @@ -34,7 +34,7 @@ int main(int argc, char **argv) if (ret < 0) goto fail; - ret = istream_skip(fp, hdr.record_size); + ret = sqfs_istream_skip(fp, hdr.record_size); clear_header(&hdr); if (ret < 0) diff --git a/lib/tar/test/tar_iterator.c b/lib/tar/test/tar_iterator.c index 25e1389..f51ecd6 100644 --- a/lib/tar/test/tar_iterator.c +++ b/lib/tar/test/tar_iterator.c @@ -83,12 +83,12 @@ int main(int argc, char **argv) TEST_EQUAL_UI(((sqfs_object_t *)ti)->refcount, 1); /* read the data from the stream */ - ret = istream_read(ti, buffer, sizeof(buffer)); + ret = sqfs_istream_read(ti, buffer, sizeof(buffer)); TEST_EQUAL_I(ret, 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - ret = istream_read(ti, buffer, sizeof(buffer)); + ret = sqfs_istream_read(ti, buffer, sizeof(buffer)); TEST_EQUAL_I(ret, 0); sqfs_drop(ti); diff --git a/lib/tar/test/tar_iterator2.c b/lib/tar/test/tar_iterator2.c index 6f472ae..7483e81 100644 --- a/lib/tar/test/tar_iterator2.c +++ b/lib/tar/test/tar_iterator2.c @@ -76,7 +76,7 @@ int main(int argc, char **argv) offset = 0; for (;;) { - ret = istream_read(ti, buffer, sizeof(buffer)); + ret = sqfs_istream_read(ti, buffer, sizeof(buffer)); TEST_ASSERT(ret >= 0); if (ret == 0) diff --git a/lib/tar/test/tar_iterator3.c b/lib/tar/test/tar_iterator3.c index 39e7b42..11c2fd2 100644 --- a/lib/tar/test/tar_iterator3.c +++ b/lib/tar/test/tar_iterator3.c @@ -86,10 +86,10 @@ int main(int argc, char **argv) TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(ti); - TEST_ASSERT(istream_read(ti, buffer, sizeof(buffer)) == 5); + TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - TEST_ASSERT(istream_read(ti, buffer, sizeof(buffer)) == 0); + TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 0); ti = sqfs_drop(ti); ret = it->next(it, &ent); @@ -105,10 +105,10 @@ int main(int argc, char **argv) TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(ti); - TEST_ASSERT(istream_read(ti, buffer, sizeof(buffer)) == 5); + TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - TEST_ASSERT(istream_read(ti, buffer, sizeof(buffer)) == 0); + TEST_ASSERT(sqfs_istream_read(ti, buffer, sizeof(buffer)) == 0); ti = sqfs_drop(ti); /* "deep" directory hierarchy containg a hard link */ diff --git a/lib/tar/test/tar_simple.c b/lib/tar/test/tar_simple.c index 656e59b..a666eb5 100644 --- a/lib/tar/test/tar_simple.c +++ b/lib/tar/test/tar_simple.c @@ -55,7 +55,7 @@ int main(int argc, char **argv) TEST_STR_EQUAL(hdr.name, fname); TEST_ASSERT(!hdr.unknown_record); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); clear_header(&hdr); diff --git a/lib/tar/test/tar_target_filled.c b/lib/tar/test/tar_target_filled.c index 815728a..4f061c0 100644 --- a/lib/tar/test/tar_target_filled.c +++ b/lib/tar/test/tar_target_filled.c @@ -50,10 +50,10 @@ int main(int argc, char **argv) "20_characters_here03/20_characters_here04/" "errored_file_tst"); TEST_EQUAL_UI(hdr.actual_size, 5); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - TEST_ASSERT(istream_skip(fp, 512 - 5) == 0); + TEST_ASSERT(sqfs_istream_skip(fp, 512 - 5) == 0); clear_header(&hdr); TEST_ASSERT(read_header(fp, &hdr) == 0); @@ -62,10 +62,10 @@ int main(int argc, char **argv) "20_characters_here03/20_characters_here04/" "some_test_file"); TEST_EQUAL_UI(hdr.actual_size, 5); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); - TEST_ASSERT(istream_skip(fp, 512 - 5) == 0); + TEST_ASSERT(sqfs_istream_skip(fp, 512 - 5) == 0); clear_header(&hdr); /* "deep" directory hierarchy containg a hard link */ diff --git a/lib/tar/test/tar_write_simple.c b/lib/tar/test/tar_write_simple.c index 98ac25b..ca8d1e5 100644 --- a/lib/tar/test/tar_write_simple.c +++ b/lib/tar/test/tar_write_simple.c @@ -6,7 +6,7 @@ */ #include "config.h" #include "tar/tar.h" -#include "io/ostream.h" +#include "sqfs/io.h" #include "io/file.h" #include "util/test.h" #include "sqfs/xattr.h" @@ -191,11 +191,11 @@ int main(int argc, char **argv) fp = istream_open_file(STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_NOT_NULL(fp); - ret = istream_read(fp, rd_buffer, sizeof(rd_buffer)); + ret = sqfs_istream_read(fp, rd_buffer, sizeof(rd_buffer)); TEST_ASSERT(ret > 0); TEST_EQUAL_UI(ret, sizeof(rd_buffer)); - ret = istream_read(fp, rd_buffer, sizeof(rd_buffer)); + ret = sqfs_istream_read(fp, rd_buffer, sizeof(rd_buffer)); TEST_EQUAL_I(ret, 0); sqfs_drop(fp); diff --git a/lib/tar/test/tar_xattr.c b/lib/tar/test/tar_xattr.c index 657b73d..26a2cd0 100644 --- a/lib/tar/test/tar_xattr.c +++ b/lib/tar/test/tar_xattr.c @@ -27,7 +27,7 @@ int main(int argc, char **argv) TEST_EQUAL_UI(hdr.mtime, 1543094477); TEST_STR_EQUAL(hdr.name, "input.txt"); TEST_ASSERT(!hdr.unknown_record); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); diff --git a/lib/tar/test/tar_xattr_bin.c b/lib/tar/test/tar_xattr_bin.c index f897bce..94c8d76 100644 --- a/lib/tar/test/tar_xattr_bin.c +++ b/lib/tar/test/tar_xattr_bin.c @@ -35,7 +35,7 @@ int main(int argc, char **argv) TEST_EQUAL_UI(hdr.mtime, 1543094477); TEST_STR_EQUAL(hdr.name, "input.txt"); TEST_ASSERT(!hdr.unknown_record); - TEST_ASSERT(istream_read(fp, buffer, 5) == 5); + TEST_ASSERT(sqfs_istream_read(fp, buffer, 5) == 5); buffer[5] = '\0'; TEST_STR_EQUAL(buffer, "test\n"); |