From 484cd01590ec488cca1f8a5c7c76cd223609e299 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 15 Jun 2023 15:16:03 +0200 Subject: Migrate file istream/ostream from libio to libsquashfs Signed-off-by: David Oberhollenzer --- bin/gensquashfs/src/filemap_xattr.c | 2 +- bin/gensquashfs/src/fstree_from_file.c | 2 +- bin/gensquashfs/src/mkfs.c | 2 +- bin/gensquashfs/src/mkfs.h | 1 - bin/rdsquashfs/src/fill_files.c | 2 +- bin/sqfsdiff/src/extract.c | 2 +- include/common.h | 1 - include/io/file.h | 94 --------------- include/sqfs/io.h | 74 ++++++++++++ lib/io/Makemodule.am | 4 +- lib/io/src/internal.h | 1 - lib/io/src/std.c | 7 +- lib/io/src/unix/istream.c | 155 ------------------------- lib/io/src/unix/ostream.c | 197 ------------------------------- lib/io/src/win32/istream.c | 173 ---------------------------- lib/io/src/win32/ostream.c | 190 ------------------------------ lib/sqfs/Makemodule.am | 7 +- lib/sqfs/src/unix/istream.c | 169 +++++++++++++++++++++++++++ lib/sqfs/src/unix/ostream.c | 205 +++++++++++++++++++++++++++++++++ lib/sqfs/src/win32/istream.c | 179 ++++++++++++++++++++++++++++ lib/sqfs/src/win32/ostream.c | 192 ++++++++++++++++++++++++++++++ lib/tar/test/tar_big_file.c | 5 +- lib/tar/test/tar_fuzz.c | 4 +- lib/tar/test/tar_iterator.c | 6 +- lib/tar/test/tar_iterator2.c | 4 +- lib/tar/test/tar_iterator3.c | 4 +- lib/tar/test/tar_simple.c | 5 +- lib/tar/test/tar_sparse.c | 4 +- lib/tar/test/tar_sparse_gnu.c | 4 +- lib/tar/test/tar_target_filled.c | 4 +- lib/tar/test/tar_write_simple.c | 4 +- lib/tar/test/tar_xattr.c | 5 +- lib/tar/test/tar_xattr_bin.c | 5 +- lib/util/test/str_table.c | 6 +- 34 files changed, 865 insertions(+), 854 deletions(-) delete mode 100644 include/io/file.h delete mode 100644 lib/io/src/unix/istream.c delete mode 100644 lib/io/src/unix/ostream.c delete mode 100644 lib/io/src/win32/istream.c delete mode 100644 lib/io/src/win32/ostream.c create mode 100644 lib/sqfs/src/unix/istream.c create mode 100644 lib/sqfs/src/unix/ostream.c create mode 100644 lib/sqfs/src/win32/istream.c create mode 100644 lib/sqfs/src/win32/ostream.c diff --git a/bin/gensquashfs/src/filemap_xattr.c b/bin/gensquashfs/src/filemap_xattr.c index 8843c46..b9a8835 100644 --- a/bin/gensquashfs/src/filemap_xattr.c +++ b/bin/gensquashfs/src/filemap_xattr.c @@ -162,7 +162,7 @@ xattr_open_map_file(const char *path) { sqfs_istream_t *file = NULL; int ret; - ret = istream_open_file(&file, path); + ret = sqfs_istream_open_file(&file, path); if (ret) { sqfs_perror(path, NULL, ret); return NULL; diff --git a/bin/gensquashfs/src/fstree_from_file.c b/bin/gensquashfs/src/fstree_from_file.c index f392b34..c0b4571 100644 --- a/bin/gensquashfs/src/fstree_from_file.c +++ b/bin/gensquashfs/src/fstree_from_file.c @@ -313,7 +313,7 @@ int fstree_from_file(fstree_t *fs, const char *filename, const char *basepath) sqfs_istream_t *fp; int ret; - ret = istream_open_file(&fp, filename); + ret = sqfs_istream_open_file(&fp, filename); if (ret) { sqfs_perror(filename, NULL, ret); return -1; diff --git a/bin/gensquashfs/src/mkfs.c b/bin/gensquashfs/src/mkfs.c index de291fa..87ad6fc 100644 --- a/bin/gensquashfs/src/mkfs.c +++ b/bin/gensquashfs/src/mkfs.c @@ -160,7 +160,7 @@ int main(int argc, char **argv) } if (opt.sortfile != NULL) { - int ret = istream_open_file(&sortfile, opt.sortfile); + int ret = sqfs_istream_open_file(&sortfile, opt.sortfile); if (ret) { sqfs_perror(opt.sortfile, NULL, ret); goto out; diff --git a/bin/gensquashfs/src/mkfs.h b/bin/gensquashfs/src/mkfs.h index 5c97750..fd6de7f 100644 --- a/bin/gensquashfs/src/mkfs.h +++ b/bin/gensquashfs/src/mkfs.h @@ -13,7 +13,6 @@ #include "common.h" #include "io/dir_iterator.h" #include "util/util.h" -#include "io/file.h" #ifdef HAVE_SYS_XATTR_H #include diff --git a/bin/rdsquashfs/src/fill_files.c b/bin/rdsquashfs/src/fill_files.c index f0ad7e2..7087883 100644 --- a/bin/rdsquashfs/src/fill_files.c +++ b/bin/rdsquashfs/src/fill_files.c @@ -145,7 +145,7 @@ static int fill_files(sqfs_data_reader_t *data, int flags) openflags |= SQFS_FILE_OPEN_NO_SPARSE; for (i = 0; i < num_files; ++i) { - ret = ostream_open_file(&fp, files[i].path, openflags); + ret = sqfs_ostream_open_file(&fp, files[i].path, openflags); if (ret) { sqfs_perror(files[i].path, NULL, ret); return -1; diff --git a/bin/sqfsdiff/src/extract.c b/bin/sqfsdiff/src/extract.c index d51096f..cbc4381 100644 --- a/bin/sqfsdiff/src/extract.c +++ b/bin/sqfsdiff/src/extract.c @@ -22,7 +22,7 @@ static int extract(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, return -1; *ptr = '/'; - ret = ostream_open_file(&fp, temp, SQFS_FILE_OPEN_OVERWRITE); + ret = sqfs_ostream_open_file(&fp, temp, SQFS_FILE_OPEN_OVERWRITE); if (ret) { sqfs_perror(temp, NULL, ret); return -1; diff --git a/include/common.h b/include/common.h index d2e3f73..d4827a8 100644 --- a/include/common.h +++ b/include/common.h @@ -21,7 +21,6 @@ #include "simple_writer.h" #include "compress_cli.h" -#include "io/file.h" #include "io/std.h" #include "compat.h" #include "fstree.h" diff --git a/include/io/file.h b/include/io/file.h deleted file mode 100644 index e7ffb0d..0000000 --- a/include/io/file.h +++ /dev/null @@ -1,94 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * file.h - * - * Copyright (C) 2019 David Oberhollenzer - */ -#ifndef IO_FILE_H -#define IO_FILE_H - -#include "sqfs/io.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Create an input stream for an OS native file handle. - * - * @memberof sqfs_istream_t - * - * The functions takes up ownership of the file handle and takes care - * of cleaning it up. On failure, the handle remains usable, and ownership - * remains with the caller. - * - * @param out Returns a pointer to an input stream on success. - * @param path The name to associate with the handle. - * @param fd A native file handle. - * - * @return Zero on success, a negative @ref SQFS_ERROR number on failure - */ -SQFS_INTERNAL -int istream_open_handle(sqfs_istream_t **out, const char *path, - sqfs_file_handle_t fd); - -/** - * @brief Create an output stream that writes to an OS native file handle. - * - * @memberof sqfs_ostream_t - * - * If the flag SQFS_FILE_OPEN_NO_SPARSE is set, the underlying implementation - * always writes chunks of zero bytes when passing a NULL pointer to append. - * Otherwise, it tries to use seek/truncate style APIs to create sparse output - * files. - * - * @param out Returns a pointer to an output stream on success. - * @param path The name to associate with the handle. - * @param fd A native file handle. - * @param flags A combination of flags. - * - * @return Zero on success, a negative @ref SQFS_ERROR number on failure - */ -SQFS_INTERNAL int ostream_open_handle(sqfs_ostream_t **out, const char *path, - sqfs_file_handle_t hnd, int flags); - -/** - * @brief Create an input stream that reads from a file. - * - * @memberof sqfs_istream_t - * - * @param out Returns a pointer to an input stream on success. - * @param path A path to the file to open or create. - * - * @return Zero on success, a negative @ref SQFS_ERROR number on failure - */ -SQFS_INTERNAL int istream_open_file(sqfs_istream_t **out, const char *path); - -/** - * @brief Create an output stream that writes to a file. - * - * @memberof sqfs_ostream_t - * - * If the file does not yet exist, it is created. If it does exist this - * function fails, unless the flag SQFS_FILE_OPEN_OVERWRITE is set, in which - * case the file is opened and its contents are discarded. - * - * If the flag SQFS_FILE_OPEN_NO_SPARSE is set, the underlying implementation - * always writes chunks of zero bytes when passing a NULL pointer to append. - * Otherwise, it tries to use seek/truncate style APIs to create sparse output - * files. - * - * @param out Returns a pointer to an output stream on success. - * @param path A path to the file to open or create. - * @param flags A combination of flags controling how to open/create the file. - * - * @return Zero on success, a negative @ref SQFS_ERROR number on failure - */ -SQFS_INTERNAL int ostream_open_file(sqfs_ostream_t **out, - const char *path, int flags); - -#ifdef __cplusplus -} -#endif - -#endif /* IO_FILE_H */ diff --git a/include/sqfs/io.h b/include/sqfs/io.h index 1138fc0..eaf74a6 100644 --- a/include/sqfs/io.h +++ b/include/sqfs/io.h @@ -329,6 +329,80 @@ SQFS_API int sqfs_open_native_file(sqfs_file_handle_t *out, */ SQFS_API sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags); +/** + * @brief Create an input stream for an OS native file handle. + * + * @memberof sqfs_istream_t + * + * The functions takes up ownership of the file handle and takes care + * of cleaning it up. On failure, the handle remains usable, and ownership + * remains with the caller. + * + * @param out Returns a pointer to an input stream on success. + * @param path The name to associate with the handle. + * @param fd A native file handle. + * + * @return Zero on success, a negative @ref SQFS_ERROR number on failure + */ +SQFS_API +int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, + sqfs_file_handle_t fd); + +/** + * @brief Create an output stream that writes to an OS native file handle. + * + * @memberof sqfs_ostream_t + * + * If the flag SQFS_FILE_OPEN_NO_SPARSE is set, the underlying implementation + * always writes chunks of zero bytes when passing a NULL pointer to append. + * Otherwise, it tries to use seek/truncate style APIs to create sparse output + * files. + * + * @param out Returns a pointer to an output stream on success. + * @param path The name to associate with the handle. + * @param fd A native file handle. + * @param flags A combination of flags. + * + * @return Zero on success, a negative @ref SQFS_ERROR number on failure + */ +SQFS_API int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path, + sqfs_file_handle_t hnd, int flags); + +/** + * @brief Create an input stream that reads from a file. + * + * @memberof sqfs_istream_t + * + * @param out Returns a pointer to an input stream on success. + * @param path A path to the file to open or create. + * + * @return Zero on success, a negative @ref SQFS_ERROR number on failure + */ +SQFS_API int sqfs_istream_open_file(sqfs_istream_t **out, const char *path); + +/** + * @brief Create an output stream that writes to a file. + * + * @memberof sqfs_ostream_t + * + * If the file does not yet exist, it is created. If it does exist this + * function fails, unless the flag SQFS_FILE_OPEN_OVERWRITE is set, in which + * case the file is opened and its contents are discarded. + * + * If the flag SQFS_FILE_OPEN_NO_SPARSE is set, the underlying implementation + * always writes chunks of zero bytes when passing a NULL pointer to append. + * Otherwise, it tries to use seek/truncate style APIs to create sparse output + * files. + * + * @param out Returns a pointer to an output stream on success. + * @param path A path to the file to open or create. + * @param flags A combination of flags controling how to open/create the file. + * + * @return Zero on success, a negative @ref SQFS_ERROR number on failure + */ +SQFS_API int sqfs_ostream_open_file(sqfs_ostream_t **out, + const char *path, int flags); + /** * @brief Read data from an input stream * diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am index e128eef..4f2c180 100644 --- a/lib/io/Makemodule.am +++ b/lib/io/Makemodule.am @@ -1,5 +1,5 @@ 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/std.h include/io/dir_entry.h \ include/io/dir_iterator.h include/io/mem.h lib/io/src/internal.h \ 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 \ @@ -8,11 +8,9 @@ libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS) libio_a_CFLAGS += $(ZSTD_CFLAGS) $(BZIP2_CFLAGS) if WINDOWS -libio_a_SOURCES += lib/io/src/win32/ostream.c lib/io/src/win32/istream.c libio_a_SOURCES += lib/io/src/win32/dir_iterator.c libio_a_CFLAGS += -DWINVER=0x0600 -D_WIN32_WINNT=0x0600 else -libio_a_SOURCES += lib/io/src/unix/ostream.c lib/io/src/unix/istream.c libio_a_SOURCES += lib/io/src/unix/dir_iterator.c endif diff --git a/lib/io/src/internal.h b/lib/io/src/internal.h index e010727..a618ed3 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/file.h" #include "io/xfrm.h" #include "io/std.h" #include "xfrm/compress.h" diff --git a/lib/io/src/std.c b/lib/io/src/std.c index 212fa6f..c52b4c5 100644 --- a/lib/io/src/std.c +++ b/lib/io/src/std.c @@ -6,7 +6,6 @@ */ #include "config.h" #include "compat.h" -#include "io/file.h" #include "io/std.h" #include "sqfs/io.h" @@ -24,13 +23,13 @@ int istream_open_stdin(sqfs_istream_t **out) { sqfs_file_handle_t hnd = GetStdHandle(STD_INPUT_HANDLE); - return istream_open_handle(out, "stdin", hnd); + return sqfs_istream_open_handle(out, "stdin", hnd); } int ostream_open_stdout(sqfs_ostream_t **out) { sqfs_file_handle_t hnd = GetStdHandle(STD_OUTPUT_HANDLE); - return ostream_open_handle(out, "stdout", hnd, - SQFS_FILE_OPEN_NO_SPARSE); + return sqfs_ostream_open_handle(out, "stdout", hnd, + SQFS_FILE_OPEN_NO_SPARSE); } diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c deleted file mode 100644 index 55b467e..0000000 --- a/lib/io/src/unix/istream.c +++ /dev/null @@ -1,155 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "../internal.h" -#include "sqfs/io.h" -#include "sqfs/error.h" - -typedef struct { - sqfs_istream_t base; - char *path; - int fd; - - bool eof; - size_t buffer_offset; - size_t buffer_used; - sqfs_u8 buffer[BUFSZ]; -} file_istream_t; - -static int precache(sqfs_istream_t *strm) -{ - file_istream_t *file = (file_istream_t *)strm; - - if (file->eof) - return 0; - - if (file->buffer_offset > 0 && - file->buffer_offset < file->buffer_used) { - memmove(file->buffer, file->buffer + file->buffer_offset, - file->buffer_used - file->buffer_offset); - } - - file->buffer_used -= file->buffer_offset; - file->buffer_offset = 0; - - while (file->buffer_used < BUFSZ) { - ssize_t ret = read(file->fd, file->buffer + file->buffer_used, - BUFSZ - file->buffer_used); - - if (ret == 0) { - file->eof = true; - break; - } - - if (ret < 0) { - if (errno == EINTR) - continue; - return SQFS_ERROR_IO; - } - - file->buffer_used += ret; - } - - return 0; -} - -static int file_get_buffered_data(sqfs_istream_t *strm, const sqfs_u8 **out, - size_t *size, size_t want) -{ - file_istream_t *file = (file_istream_t *)strm; - - if (want > BUFSZ) - want = BUFSZ; - - if (file->buffer_used == 0 || - (file->buffer_used - file->buffer_offset) < want) { - int ret = precache(strm); - if (ret) - return ret; - } - - *out = file->buffer + file->buffer_offset; - *size = file->buffer_used - file->buffer_offset; - return (file->eof && *size == 0) ? 1 : 0; -} - -static void file_advance_buffer(sqfs_istream_t *strm, size_t count) -{ - file_istream_t *file = (file_istream_t *)strm; - - assert(count <= file->buffer_used); - - file->buffer_offset += count; - - assert(file->buffer_offset <= file->buffer_used); -} - -static const char *file_get_filename(sqfs_istream_t *strm) -{ - return ((file_istream_t *)strm)->path; -} - -static void file_destroy(sqfs_object_t *obj) -{ - file_istream_t *file = (file_istream_t *)obj; - - close(file->fd); - free(file->path); - free(file); -} - -int istream_open_handle(sqfs_istream_t **out, const char *path, - sqfs_file_handle_t fd) -{ - file_istream_t *file = calloc(1, sizeof(*file)); - sqfs_istream_t *strm = (sqfs_istream_t *)file; - - if (file == NULL) - return SQFS_ERROR_ALLOC; - - sqfs_object_init(file, file_destroy, NULL); - - file->path = strdup(path); - if (file->path == NULL) { - int temp = errno; - free(file); - errno = temp; - return SQFS_ERROR_ALLOC; - } - - file->fd = dup(fd); - if (file->fd < 0) { - int temp = errno; - free(file->path); - free(file); - errno = temp; - return SQFS_ERROR_IO; - } - close(fd); - - strm->get_buffered_data = file_get_buffered_data; - strm->advance_buffer = file_advance_buffer; - strm->get_filename = file_get_filename; - - *out = strm; - return 0; -} - -int istream_open_file(sqfs_istream_t **out, const char *path) -{ - sqfs_file_handle_t fd; - int ret; - - ret = sqfs_open_native_file(&fd, path, SQFS_FILE_OPEN_READ_ONLY); - if (ret) - return ret; - - ret = istream_open_handle(out, path, fd); - if (ret != 0) - close(fd); - - return ret; -} diff --git a/lib/io/src/unix/ostream.c b/lib/io/src/unix/ostream.c deleted file mode 100644 index 57467f5..0000000 --- a/lib/io/src/unix/ostream.c +++ /dev/null @@ -1,197 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * ostream.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "../internal.h" -#include "sqfs/io.h" -#include "sqfs/error.h" - -typedef struct { - sqfs_ostream_t base; - char *path; - int flags; - int fd; - - off_t sparse_count; - off_t size; -} file_ostream_t; - -static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size) -{ - while (size > 0) { - ssize_t ret = write(file->fd, data, size); - - if (ret == 0) { - errno = EPIPE; - return SQFS_ERROR_IO; - } - - if (ret < 0) { - if (errno == EINTR) - continue; - return SQFS_ERROR_IO; - } - - file->size += ret; - size -= ret; - data += ret; - } - - return 0; -} - -static int realize_sparse(file_ostream_t *file) -{ - unsigned char *buffer; - size_t diff, bufsz; - int ret; - - if (file->sparse_count == 0) - return 0; - - if (file->flags & SQFS_FILE_OPEN_NO_SPARSE) { - bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count; - buffer = calloc(1, bufsz); - if (buffer == NULL) - return SQFS_ERROR_ALLOC; - - while (file->sparse_count > 0) { - diff = file->sparse_count > (off_t)bufsz ? - bufsz : (size_t)file->sparse_count; - - ret = write_all(file, buffer, diff); - if (ret) { - int temp = errno; - free(buffer); - errno = temp; - return ret; - } - - file->sparse_count -= diff; - } - - free(buffer); - } else { - if (lseek(file->fd, file->sparse_count, SEEK_CUR) == (off_t)-1) - return SQFS_ERROR_IO; - - if (ftruncate(file->fd, file->size) != 0) - return SQFS_ERROR_IO; - - file->sparse_count = 0; - } - - return 0; -} - -static int file_append(sqfs_ostream_t *strm, const void *data, size_t size) -{ - file_ostream_t *file = (file_ostream_t *)strm; - int ret; - - if (size == 0 || data == NULL) { - file->sparse_count += size; - file->size += size; - return 0; - } - - ret = realize_sparse(file); - if (ret) - return ret; - - return write_all(file, data, size); -} - -static int file_flush(sqfs_ostream_t *strm) -{ - file_ostream_t *file = (file_ostream_t *)strm; - int ret; - - ret = realize_sparse(file); - if (ret) - return ret; - - if (fsync(file->fd) != 0) { - if (errno != EINVAL) - return SQFS_ERROR_IO; - } - - return 0; -} - -static void file_destroy(sqfs_object_t *obj) -{ - file_ostream_t *file = (file_ostream_t *)obj; - - close(file->fd); - free(file->path); - free(file); -} - -static const char *file_get_filename(sqfs_ostream_t *strm) -{ - return ((file_ostream_t *)strm)->path; -} - -int ostream_open_handle(sqfs_ostream_t **out, const char *path, - sqfs_file_handle_t fd, int flags) -{ - file_ostream_t *file = calloc(1, sizeof(*file)); - sqfs_ostream_t *strm = (sqfs_ostream_t *)file; - - *out = NULL; - if (file == NULL) - return SQFS_ERROR_ALLOC; - - sqfs_object_init(file, file_destroy, NULL); - - file->path = strdup(path); - if (file->path == NULL) { - int temp = errno; - free(file); - errno = temp; - return SQFS_ERROR_ALLOC; - } - - file->fd = dup(fd); - if (file->fd < 0) { - int temp = errno; - free(file->path); - free(file); - errno = temp; - return SQFS_ERROR_IO; - } - - close(fd); - - file->flags = flags; - strm->append = file_append; - strm->flush = file_flush; - strm->get_filename = file_get_filename; - - *out = strm; - return 0; -} - -int ostream_open_file(sqfs_ostream_t **out, const char *path, int flags) -{ - sqfs_file_handle_t fd; - int ret; - - *out = NULL; - ret = sqfs_open_native_file(&fd, path, flags); - if (ret) - return ret; - - ret = ostream_open_handle(out, path, fd, flags); - if (ret) { - int temp = errno; - close(fd); - errno = temp; - return ret; - } - - return 0; -} diff --git a/lib/io/src/win32/istream.c b/lib/io/src/win32/istream.c deleted file mode 100644 index a09f3c3..0000000 --- a/lib/io/src/win32/istream.c +++ /dev/null @@ -1,173 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "../internal.h" -#include "sqfs/io.h" -#include "sqfs/error.h" - -#define WIN32_LEAN_AND_MEAN -#include - -typedef struct { - sqfs_istream_t base; - char *path; - HANDLE hnd; - - bool eof; - size_t buffer_offset; - size_t buffer_used; - sqfs_u8 buffer[BUFSZ]; -} file_istream_t; - -static int precache(sqfs_istream_t *strm) -{ - file_istream_t *file = (file_istream_t *)strm; - DWORD diff, actual; - - if (file->eof) - return 0; - - if (file->buffer_offset > 0 && - file->buffer_offset < file->buffer_used) { - memmove(file->buffer, file->buffer + file->buffer_offset, - file->buffer_used - file->buffer_offset); - } - - file->buffer_used -= file->buffer_offset; - file->buffer_offset = 0; - - while (file->buffer_used < sizeof(file->buffer)) { - diff = sizeof(file->buffer) - file->buffer_used; - - if (!ReadFile(file->hnd, file->buffer + file->buffer_used, - diff, &actual, NULL)) { - DWORD error = GetLastError(); - - if (error == ERROR_HANDLE_EOF || - error == ERROR_BROKEN_PIPE) { - file->eof = true; - break; - } - - SetLastError(error); - return SQFS_ERROR_IO; - } - - if (actual == 0) { - file->eof = true; - break; - } - - file->buffer_used += actual; - } - - return 0; -} - -static int file_get_buffered_data(sqfs_istream_t *strm, const sqfs_u8 **out, - size_t *size, size_t want) -{ - file_istream_t *file = (file_istream_t *)strm; - - if (want > BUFSZ) - want = BUFSZ; - - if (file->buffer_used == 0 || - (file->buffer_used - file->buffer_offset) < want) { - int ret = precache(strm); - if (ret) - return ret; - } - - *out = file->buffer + file->buffer_offset; - *size = file->buffer_used - file->buffer_offset; - return (file->eof && *size == 0) ? 1 : 0; -} - -static void file_advance_buffer(sqfs_istream_t *strm, size_t count) -{ - file_istream_t *file = (file_istream_t *)strm; - - assert(count <= file->buffer_used); - - file->buffer_offset += count; - - assert(file->buffer_offset <= file->buffer_used); -} - -static const char *file_get_filename(sqfs_istream_t *strm) -{ - return ((file_istream_t *)strm)->path; -} - -static void file_destroy(sqfs_object_t *obj) -{ - file_istream_t *file = (file_istream_t *)obj; - - CloseHandle(file->hnd); - free(file->path); - free(file); -} - -int istream_open_handle(sqfs_istream_t **out, const char *path, HANDLE hnd) -{ - file_istream_t *file = calloc(1, sizeof(*file)); - sqfs_istream_t *strm = (sqfs_istream_t *)file; - BOOL ret; - - if (file == NULL) - return SQFS_ERROR_ALLOC; - - sqfs_object_init(file, file_destroy, NULL); - - file->path = strdup(path); - if (file->path == NULL) { - DWORD temp = GetLastError(); - free(file); - SetLastError(temp); - return SQFS_ERROR_ALLOC; - } - - ret = DuplicateHandle(GetCurrentProcess(), hnd, - GetCurrentProcess(), &file->hnd, - 0, FALSE, DUPLICATE_SAME_ACCESS); - if (!ret) { - DWORD temp = GetLastError(); - free(file->path); - free(file); - SetLastError(temp); - return SQFS_ERROR_IO; - } - - CloseHandle(hnd); - - strm->get_buffered_data = file_get_buffered_data; - strm->advance_buffer = file_advance_buffer; - strm->get_filename = file_get_filename; - - *out = strm; - return 0; -} - -int istream_open_file(sqfs_istream_t **out, const char *path) -{ - sqfs_file_handle_t hnd; - int ret; - - ret = sqfs_open_native_file(&hnd, path, SQFS_FILE_OPEN_READ_ONLY); - if (ret) - return ret; - - ret = istream_open_handle(out, path, hnd); - if (ret) { - DWORD temp = GetLastError(); - CloseHandle(hnd); - SetLastError(temp); - return ret; - } - - return 0; -} diff --git a/lib/io/src/win32/ostream.c b/lib/io/src/win32/ostream.c deleted file mode 100644 index b7f0e06..0000000 --- a/lib/io/src/win32/ostream.c +++ /dev/null @@ -1,190 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * ostream.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#include "../internal.h" -#include "sqfs/io.h" -#include "sqfs/error.h" - -#define WIN32_LEAN_AND_MEAN -#include - -typedef struct { - sqfs_ostream_t base; - sqfs_u64 sparse_count; - char *path; - HANDLE hnd; - int flags; -} file_ostream_t; - -static int write_data(file_ostream_t *file, const void *data, size_t size) -{ - DWORD diff; - - while (size > 0) { - if (!WriteFile(file->hnd, data, size, &diff, NULL)) - return SQFS_ERROR_IO; - - size -= diff; - data = (const char *)data + diff; - } - - return 0; -} - -static int realize_sparse(file_ostream_t *file) -{ - size_t bufsz, diff; - LARGE_INTEGER pos; - void *buffer; - int ret; - - if (file->sparse_count == 0) - return 0; - - if (file->flags & SQFS_FILE_OPEN_NO_SPARSE) { - bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count; - buffer = calloc(1, bufsz); - - if (buffer == NULL) - return SQFS_ERROR_ALLOC; - - while (file->sparse_count > 0) { - diff = file->sparse_count > bufsz ? - bufsz : file->sparse_count; - - ret = write_data(file, buffer, diff); - if (ret) { - DWORD temp = GetLastError(); - free(buffer); - SetLastError(temp); - return ret; - } - - file->sparse_count -= diff; - } - - free(buffer); - } else { - pos.QuadPart = file->sparse_count; - - if (!SetFilePointerEx(file->hnd, pos, NULL, FILE_CURRENT)) - return SQFS_ERROR_IO; - - if (!SetEndOfFile(file->hnd)) - return SQFS_ERROR_IO; - - file->sparse_count = 0; - } - - return 0; -} - -static int file_append(sqfs_ostream_t *strm, const void *data, size_t size) -{ - file_ostream_t *file = (file_ostream_t *)strm; - int ret; - - if (size == 0 || data == NULL) { - file->sparse_count += size; - return 0; - } - - ret = realize_sparse(file); - if (ret) - return ret; - - return write_data(file, data, size); -} - -static int file_flush(sqfs_ostream_t *strm) -{ - file_ostream_t *file = (file_ostream_t *)strm; - int ret; - - ret = realize_sparse(file); - if (ret) - return ret; - - if (!FlushFileBuffers(file->hnd)) - return SQFS_ERROR_IO; - - return 0; -} - -static void file_destroy(sqfs_object_t *obj) -{ - file_ostream_t *file = (file_ostream_t *)obj; - - CloseHandle(file->hnd); - free(file->path); - free(file); -} - -static const char *file_get_filename(sqfs_ostream_t *strm) -{ - return ((file_ostream_t *)strm)->path; -} - -int ostream_open_handle(sqfs_ostream_t **out, const char *path, - sqfs_file_handle_t hnd, int flags) -{ - file_ostream_t *file = calloc(1, sizeof(*file)); - sqfs_ostream_t *strm = (sqfs_ostream_t *)file; - BOOL ret; - - if (file == NULL) - return SQFS_ERROR_ALLOC; - - sqfs_object_init(file, file_destroy, NULL); - - file->path = strdup(path); - if (file->path == NULL) { - free(file); - return SQFS_ERROR_ALLOC; - } - - ret = DuplicateHandle(GetCurrentProcess(), hnd, - GetCurrentProcess(), &file->hnd, - 0, FALSE, DUPLICATE_SAME_ACCESS); - if (!ret) { - DWORD temp = GetLastError(); - free(file->path); - free(file); - SetLastError(temp); - return SQFS_ERROR_IO; - } - - CloseHandle(hnd); - - file->flags = flags; - strm->append = file_append; - strm->flush = file_flush; - strm->get_filename = file_get_filename; - - *out = strm; - return 0; -} - -int ostream_open_file(sqfs_ostream_t **out, const char *path, int flags) -{ - sqfs_file_handle_t hnd; - int ret; - - *out = NULL; - ret = sqfs_open_native_file(&hnd, path, flags); - if (ret) - return ret; - - ret = ostream_open_handle(out, path, hnd, flags); - if (ret) { - DWORD temp = GetLastError(); - CloseHandle(hnd); - SetLastError(temp); - return SQFS_ERROR_IO; - } - - return 0; -} diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index 2727b11..83ca478 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -57,12 +57,15 @@ libsquashfs_la_SOURCES += lib/util/src/mempool.c include/util/mempool.h endif if WINDOWS -libsquashfs_la_SOURCES += lib/sqfs/src/win32/io_file.c +libsquashfs_la_SOURCES += lib/sqfs/src/win32/io_file.c \ + lib/sqfs/src/win32/ostream.c lib/sqfs/src/win32/istream.c + libsquashfs_la_CFLAGS += -DWINVER=0x0600 -D_WIN32_WINNT=0x0600 libsquashfs_la_CFLAGS += -Wc,-static-libgcc libsquashfs_la_LDFLAGS += -no-undefined -avoid-version else -libsquashfs_la_SOURCES += lib/sqfs/src/unix/io_file.c +libsquashfs_la_SOURCES += lib/sqfs/src/unix/io_file.c \ + lib/sqfs/src/unix/ostream.c lib/sqfs/src/unix/istream.c endif if HAVE_PTHREAD diff --git a/lib/sqfs/src/unix/istream.c b/lib/sqfs/src/unix/istream.c new file mode 100644 index 0000000..fd2f120 --- /dev/null +++ b/lib/sqfs/src/unix/istream.c @@ -0,0 +1,169 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * istream.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" + +#include +#include +#include +#include +#include +#include + +#define BUFSZ (131072) + +typedef struct { + sqfs_istream_t base; + char *path; + int fd; + + bool eof; + size_t buffer_offset; + size_t buffer_used; + sqfs_u8 buffer[BUFSZ]; +} file_istream_t; + +static int precache(sqfs_istream_t *strm) +{ + file_istream_t *file = (file_istream_t *)strm; + + if (file->eof) + return 0; + + if (file->buffer_offset > 0 && + file->buffer_offset < file->buffer_used) { + memmove(file->buffer, file->buffer + file->buffer_offset, + file->buffer_used - file->buffer_offset); + } + + file->buffer_used -= file->buffer_offset; + file->buffer_offset = 0; + + while (file->buffer_used < BUFSZ) { + ssize_t ret = read(file->fd, file->buffer + file->buffer_used, + BUFSZ - file->buffer_used); + + if (ret == 0) { + file->eof = true; + break; + } + + if (ret < 0) { + if (errno == EINTR) + continue; + return SQFS_ERROR_IO; + } + + file->buffer_used += ret; + } + + return 0; +} + +static int file_get_buffered_data(sqfs_istream_t *strm, const sqfs_u8 **out, + size_t *size, size_t want) +{ + file_istream_t *file = (file_istream_t *)strm; + + if (want > BUFSZ) + want = BUFSZ; + + if (file->buffer_used == 0 || + (file->buffer_used - file->buffer_offset) < want) { + int ret = precache(strm); + if (ret) + return ret; + } + + *out = file->buffer + file->buffer_offset; + *size = file->buffer_used - file->buffer_offset; + return (file->eof && *size == 0) ? 1 : 0; +} + +static void file_advance_buffer(sqfs_istream_t *strm, size_t count) +{ + file_istream_t *file = (file_istream_t *)strm; + + assert(count <= file->buffer_used); + + file->buffer_offset += count; + + assert(file->buffer_offset <= file->buffer_used); +} + +static const char *file_get_filename(sqfs_istream_t *strm) +{ + return ((file_istream_t *)strm)->path; +} + +static void file_destroy(sqfs_object_t *obj) +{ + file_istream_t *file = (file_istream_t *)obj; + + close(file->fd); + free(file->path); + free(file); +} + +int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, + sqfs_file_handle_t fd) +{ + file_istream_t *file = calloc(1, sizeof(*file)); + sqfs_istream_t *strm = (sqfs_istream_t *)file; + + if (file == NULL) + return SQFS_ERROR_ALLOC; + + sqfs_object_init(file, file_destroy, NULL); + + file->path = strdup(path); + if (file->path == NULL) { + int temp = errno; + free(file); + errno = temp; + return SQFS_ERROR_ALLOC; + } + + file->fd = dup(fd); + if (file->fd < 0) { + int temp = errno; + free(file->path); + free(file); + errno = temp; + return SQFS_ERROR_IO; + } + close(fd); + + strm->get_buffered_data = file_get_buffered_data; + strm->advance_buffer = file_advance_buffer; + strm->get_filename = file_get_filename; + + *out = strm; + return 0; +} + +int sqfs_istream_open_file(sqfs_istream_t **out, const char *path) +{ + sqfs_file_handle_t fd; + int ret; + + ret = sqfs_open_native_file(&fd, path, SQFS_FILE_OPEN_READ_ONLY); + if (ret) + return ret; + + ret = sqfs_istream_open_handle(out, path, fd); + if (ret != 0) { + int temp = errno; + close(fd); + errno = temp; + } + + return ret; +} diff --git a/lib/sqfs/src/unix/ostream.c b/lib/sqfs/src/unix/ostream.c new file mode 100644 index 0000000..89982c1 --- /dev/null +++ b/lib/sqfs/src/unix/ostream.c @@ -0,0 +1,205 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * ostream.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" + +#include +#include +#include +#include +#include + +typedef struct { + sqfs_ostream_t base; + char *path; + int flags; + int fd; + + off_t sparse_count; + off_t size; +} file_ostream_t; + +static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size) +{ + while (size > 0) { + ssize_t ret = write(file->fd, data, size); + + if (ret == 0) { + errno = EPIPE; + return SQFS_ERROR_IO; + } + + if (ret < 0) { + if (errno == EINTR) + continue; + return SQFS_ERROR_IO; + } + + file->size += ret; + size -= ret; + data += ret; + } + + return 0; +} + +static int realize_sparse(file_ostream_t *file) +{ + unsigned char *buffer; + size_t diff, bufsz; + int ret; + + if (file->sparse_count == 0) + return 0; + + if (file->flags & SQFS_FILE_OPEN_NO_SPARSE) { + bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count; + buffer = calloc(1, bufsz); + if (buffer == NULL) + return SQFS_ERROR_ALLOC; + + while (file->sparse_count > 0) { + diff = file->sparse_count > (off_t)bufsz ? + bufsz : (size_t)file->sparse_count; + + ret = write_all(file, buffer, diff); + if (ret) { + int temp = errno; + free(buffer); + errno = temp; + return ret; + } + + file->sparse_count -= diff; + } + + free(buffer); + } else { + if (lseek(file->fd, file->sparse_count, SEEK_CUR) == (off_t)-1) + return SQFS_ERROR_IO; + + if (ftruncate(file->fd, file->size) != 0) + return SQFS_ERROR_IO; + + file->sparse_count = 0; + } + + return 0; +} + +static int file_append(sqfs_ostream_t *strm, const void *data, size_t size) +{ + file_ostream_t *file = (file_ostream_t *)strm; + int ret; + + if (size == 0 || data == NULL) { + file->sparse_count += size; + file->size += size; + return 0; + } + + ret = realize_sparse(file); + if (ret) + return ret; + + return write_all(file, data, size); +} + +static int file_flush(sqfs_ostream_t *strm) +{ + file_ostream_t *file = (file_ostream_t *)strm; + int ret; + + ret = realize_sparse(file); + if (ret) + return ret; + + if (fsync(file->fd) != 0) { + if (errno != EINVAL) + return SQFS_ERROR_IO; + } + + return 0; +} + +static void file_destroy(sqfs_object_t *obj) +{ + file_ostream_t *file = (file_ostream_t *)obj; + + close(file->fd); + free(file->path); + free(file); +} + +static const char *file_get_filename(sqfs_ostream_t *strm) +{ + return ((file_ostream_t *)strm)->path; +} + +int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path, + sqfs_file_handle_t fd, int flags) +{ + file_ostream_t *file = calloc(1, sizeof(*file)); + sqfs_ostream_t *strm = (sqfs_ostream_t *)file; + + *out = NULL; + if (file == NULL) + return SQFS_ERROR_ALLOC; + + sqfs_object_init(file, file_destroy, NULL); + + file->path = strdup(path); + if (file->path == NULL) { + int temp = errno; + free(file); + errno = temp; + return SQFS_ERROR_ALLOC; + } + + file->fd = dup(fd); + if (file->fd < 0) { + int temp = errno; + free(file->path); + free(file); + errno = temp; + return SQFS_ERROR_IO; + } + + close(fd); + + file->flags = flags; + strm->append = file_append; + strm->flush = file_flush; + strm->get_filename = file_get_filename; + + *out = strm; + return 0; +} + +int sqfs_ostream_open_file(sqfs_ostream_t **out, const char *path, int flags) +{ + sqfs_file_handle_t fd; + int ret; + + *out = NULL; + ret = sqfs_open_native_file(&fd, path, flags); + if (ret) + return ret; + + ret = sqfs_ostream_open_handle(out, path, fd, flags); + if (ret) { + int temp = errno; + close(fd); + errno = temp; + return ret; + } + + return 0; +} diff --git a/lib/sqfs/src/win32/istream.c b/lib/sqfs/src/win32/istream.c new file mode 100644 index 0000000..ba44c0b --- /dev/null +++ b/lib/sqfs/src/win32/istream.c @@ -0,0 +1,179 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * istream.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" + +#define BUFSZ (131072) + +#define WIN32_LEAN_AND_MEAN +#include +#include + +typedef struct { + sqfs_istream_t base; + char *path; + HANDLE hnd; + + bool eof; + size_t buffer_offset; + size_t buffer_used; + sqfs_u8 buffer[BUFSZ]; +} file_istream_t; + +static int precache(sqfs_istream_t *strm) +{ + file_istream_t *file = (file_istream_t *)strm; + DWORD diff, actual; + + if (file->eof) + return 0; + + if (file->buffer_offset > 0 && + file->buffer_offset < file->buffer_used) { + memmove(file->buffer, file->buffer + file->buffer_offset, + file->buffer_used - file->buffer_offset); + } + + file->buffer_used -= file->buffer_offset; + file->buffer_offset = 0; + + while (file->buffer_used < sizeof(file->buffer)) { + diff = sizeof(file->buffer) - file->buffer_used; + + if (!ReadFile(file->hnd, file->buffer + file->buffer_used, + diff, &actual, NULL)) { + DWORD error = GetLastError(); + + if (error == ERROR_HANDLE_EOF || + error == ERROR_BROKEN_PIPE) { + file->eof = true; + break; + } + + SetLastError(error); + return SQFS_ERROR_IO; + } + + if (actual == 0) { + file->eof = true; + break; + } + + file->buffer_used += actual; + } + + return 0; +} + +static int file_get_buffered_data(sqfs_istream_t *strm, const sqfs_u8 **out, + size_t *size, size_t want) +{ + file_istream_t *file = (file_istream_t *)strm; + + if (want > BUFSZ) + want = BUFSZ; + + if (file->buffer_used == 0 || + (file->buffer_used - file->buffer_offset) < want) { + int ret = precache(strm); + if (ret) + return ret; + } + + *out = file->buffer + file->buffer_offset; + *size = file->buffer_used - file->buffer_offset; + return (file->eof && *size == 0) ? 1 : 0; +} + +static void file_advance_buffer(sqfs_istream_t *strm, size_t count) +{ + file_istream_t *file = (file_istream_t *)strm; + + assert(count <= file->buffer_used); + + file->buffer_offset += count; + + assert(file->buffer_offset <= file->buffer_used); +} + +static const char *file_get_filename(sqfs_istream_t *strm) +{ + return ((file_istream_t *)strm)->path; +} + +static void file_destroy(sqfs_object_t *obj) +{ + file_istream_t *file = (file_istream_t *)obj; + + CloseHandle(file->hnd); + free(file->path); + free(file); +} + +int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, + sqfs_file_handle_t hnd) +{ + file_istream_t *file = calloc(1, sizeof(*file)); + sqfs_istream_t *strm = (sqfs_istream_t *)file; + BOOL ret; + + if (file == NULL) + return SQFS_ERROR_ALLOC; + + sqfs_object_init(file, file_destroy, NULL); + + file->path = strdup(path); + if (file->path == NULL) { + DWORD temp = GetLastError(); + free(file); + SetLastError(temp); + return SQFS_ERROR_ALLOC; + } + + ret = DuplicateHandle(GetCurrentProcess(), hnd, + GetCurrentProcess(), &file->hnd, + 0, FALSE, DUPLICATE_SAME_ACCESS); + if (!ret) { + DWORD temp = GetLastError(); + free(file->path); + free(file); + SetLastError(temp); + return SQFS_ERROR_IO; + } + + CloseHandle(hnd); + + strm->get_buffered_data = file_get_buffered_data; + strm->advance_buffer = file_advance_buffer; + strm->get_filename = file_get_filename; + + *out = strm; + return 0; +} + +int sqfs_istream_open_file(sqfs_istream_t **out, const char *path) +{ + sqfs_file_handle_t hnd; + int ret; + + ret = sqfs_open_native_file(&hnd, path, SQFS_FILE_OPEN_READ_ONLY); + if (ret) + return ret; + + ret = sqfs_istream_open_handle(out, path, hnd); + if (ret) { + DWORD temp = GetLastError(); + CloseHandle(hnd); + SetLastError(temp); + return ret; + } + + return 0; +} diff --git a/lib/sqfs/src/win32/ostream.c b/lib/sqfs/src/win32/ostream.c new file mode 100644 index 0000000..3e6a8a7 --- /dev/null +++ b/lib/sqfs/src/win32/ostream.c @@ -0,0 +1,192 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * ostream.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" + +#define WIN32_LEAN_AND_MEAN +#include + +typedef struct { + sqfs_ostream_t base; + sqfs_u64 sparse_count; + char *path; + HANDLE hnd; + int flags; +} file_ostream_t; + +static int write_data(file_ostream_t *file, const void *data, size_t size) +{ + DWORD diff; + + while (size > 0) { + if (!WriteFile(file->hnd, data, size, &diff, NULL)) + return SQFS_ERROR_IO; + + size -= diff; + data = (const char *)data + diff; + } + + return 0; +} + +static int realize_sparse(file_ostream_t *file) +{ + size_t bufsz, diff; + LARGE_INTEGER pos; + void *buffer; + int ret; + + if (file->sparse_count == 0) + return 0; + + if (file->flags & SQFS_FILE_OPEN_NO_SPARSE) { + bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count; + buffer = calloc(1, bufsz); + + if (buffer == NULL) + return SQFS_ERROR_ALLOC; + + while (file->sparse_count > 0) { + diff = file->sparse_count > bufsz ? + bufsz : file->sparse_count; + + ret = write_data(file, buffer, diff); + if (ret) { + DWORD temp = GetLastError(); + free(buffer); + SetLastError(temp); + return ret; + } + + file->sparse_count -= diff; + } + + free(buffer); + } else { + pos.QuadPart = file->sparse_count; + + if (!SetFilePointerEx(file->hnd, pos, NULL, FILE_CURRENT)) + return SQFS_ERROR_IO; + + if (!SetEndOfFile(file->hnd)) + return SQFS_ERROR_IO; + + file->sparse_count = 0; + } + + return 0; +} + +static int file_append(sqfs_ostream_t *strm, const void *data, size_t size) +{ + file_ostream_t *file = (file_ostream_t *)strm; + int ret; + + if (size == 0 || data == NULL) { + file->sparse_count += size; + return 0; + } + + ret = realize_sparse(file); + if (ret) + return ret; + + return write_data(file, data, size); +} + +static int file_flush(sqfs_ostream_t *strm) +{ + file_ostream_t *file = (file_ostream_t *)strm; + int ret; + + ret = realize_sparse(file); + if (ret) + return ret; + + if (!FlushFileBuffers(file->hnd)) + return SQFS_ERROR_IO; + + return 0; +} + +static void file_destroy(sqfs_object_t *obj) +{ + file_ostream_t *file = (file_ostream_t *)obj; + + CloseHandle(file->hnd); + free(file->path); + free(file); +} + +static const char *file_get_filename(sqfs_ostream_t *strm) +{ + return ((file_ostream_t *)strm)->path; +} + +int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path, + sqfs_file_handle_t hnd, int flags) +{ + file_ostream_t *file = calloc(1, sizeof(*file)); + sqfs_ostream_t *strm = (sqfs_ostream_t *)file; + BOOL ret; + + if (file == NULL) + return SQFS_ERROR_ALLOC; + + sqfs_object_init(file, file_destroy, NULL); + + file->path = strdup(path); + if (file->path == NULL) { + free(file); + return SQFS_ERROR_ALLOC; + } + + ret = DuplicateHandle(GetCurrentProcess(), hnd, + GetCurrentProcess(), &file->hnd, + 0, FALSE, DUPLICATE_SAME_ACCESS); + if (!ret) { + DWORD temp = GetLastError(); + free(file->path); + free(file); + SetLastError(temp); + return SQFS_ERROR_IO; + } + + CloseHandle(hnd); + + file->flags = flags; + strm->append = file_append; + strm->flush = file_flush; + strm->get_filename = file_get_filename; + + *out = strm; + return 0; +} + +int sqfs_ostream_open_file(sqfs_ostream_t **out, const char *path, int flags) +{ + sqfs_file_handle_t hnd; + int ret; + + *out = NULL; + ret = sqfs_open_native_file(&hnd, path, flags); + if (ret) + return ret; + + ret = sqfs_ostream_open_handle(out, path, hnd, flags); + if (ret) { + DWORD temp = GetLastError(); + CloseHandle(hnd); + SetLastError(temp); + return SQFS_ERROR_IO; + } + + return 0; +} diff --git a/lib/tar/test/tar_big_file.c b/lib/tar/test/tar_big_file.c index 6a88d4c..2c8a176 100644 --- a/lib/tar/test/tar_big_file.c +++ b/lib/tar/test/tar_big_file.c @@ -6,8 +6,8 @@ */ #include "config.h" #include "tar/tar.h" -#include "io/file.h" #include "util/test.h" +#include "sqfs/io.h" int main(int argc, char **argv) { @@ -16,7 +16,8 @@ int main(int argc, char **argv) int ret; (void)argc; (void)argv; - ret = istream_open_file(&fp, STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + ret = sqfs_istream_open_file(&fp, + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_fuzz.c b/lib/tar/test/tar_fuzz.c index 42fe73d..1c4b52e 100644 --- a/lib/tar/test/tar_fuzz.c +++ b/lib/tar/test/tar_fuzz.c @@ -6,7 +6,7 @@ */ #include "config.h" -#include "io/file.h" +#include "sqfs/io.h" #include "tar/tar.h" #include "common.h" @@ -24,7 +24,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - ret = istream_open_file(&fp, argv[1]); + ret = sqfs_istream_open_file(&fp, argv[1]); if (ret) { sqfs_perror("stdint", NULL, ret); return EXIT_FAILURE; diff --git a/lib/tar/test/tar_iterator.c b/lib/tar/test/tar_iterator.c index 8de50ed..d4c2a35 100644 --- a/lib/tar/test/tar_iterator.c +++ b/lib/tar/test/tar_iterator.c @@ -5,10 +5,10 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" #include "sqfs/error.h" +#include "sqfs/io.h" #ifndef TESTUID #define TESTUID 1000 @@ -40,7 +40,7 @@ int main(int argc, char **argv) (void)argc; (void)argv; /* Open the file, create an iterator */ - iret = istream_open_file(&fp, + iret = sqfs_istream_open_file(&fp, STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_EQUAL_I(iret, 0); TEST_NOT_NULL(fp); @@ -109,7 +109,7 @@ int main(int argc, char **argv) sqfs_drop(fp); /* re-open the tar iterator */ - iret = istream_open_file(&fp, + iret = sqfs_istream_open_file(&fp, STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_EQUAL_I(iret, 0); TEST_NOT_NULL(fp); diff --git a/lib/tar/test/tar_iterator2.c b/lib/tar/test/tar_iterator2.c index 8826762..8bbaf62 100644 --- a/lib/tar/test/tar_iterator2.c +++ b/lib/tar/test/tar_iterator2.c @@ -5,9 +5,9 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" +#include "sqfs/io.h" static const struct { uint64_t offset; @@ -52,7 +52,7 @@ int main(int argc, char **argv) int iret; (void)argc; (void)argv; - iret = istream_open_file(&fp, + iret = sqfs_istream_open_file(&fp, STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_EQUAL_I(iret, 0); TEST_NOT_NULL(fp); diff --git a/lib/tar/test/tar_iterator3.c b/lib/tar/test/tar_iterator3.c index 0aee0e3..9fd5f59 100644 --- a/lib/tar/test/tar_iterator3.c +++ b/lib/tar/test/tar_iterator3.c @@ -6,10 +6,10 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" #include "sqfs/error.h" +#include "sqfs/io.h" int main(int argc, char **argv) { @@ -23,7 +23,7 @@ int main(int argc, char **argv) TEST_ASSERT(chdir(TEST_PATH) == 0); - ret = istream_open_file(&fp, "format-acceptance/link_filled.tar"); + ret = sqfs_istream_open_file(&fp, "format-acceptance/link_filled.tar"); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1); diff --git a/lib/tar/test/tar_simple.c b/lib/tar/test/tar_simple.c index 8ee5b84..ad9fdde 100644 --- a/lib/tar/test/tar_simple.c +++ b/lib/tar/test/tar_simple.c @@ -5,9 +5,9 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" +#include "sqfs/io.h" #ifndef TESTUID #define TESTUID 1000 @@ -43,7 +43,8 @@ int main(int argc, char **argv) int ret; (void)argc; (void)argv; - ret = istream_open_file(&fp, STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + ret = sqfs_istream_open_file(&fp, + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_sparse.c b/lib/tar/test/tar_sparse.c index d93482d..49b2064 100644 --- a/lib/tar/test/tar_sparse.c +++ b/lib/tar/test/tar_sparse.c @@ -5,9 +5,9 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" +#include "sqfs/io.h" static void test_case_sparse(const char *path) { @@ -16,7 +16,7 @@ static void test_case_sparse(const char *path) sqfs_istream_t *fp; int ret; - ret = istream_open_file(&fp, path); + ret = sqfs_istream_open_file(&fp, path); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_sparse_gnu.c b/lib/tar/test/tar_sparse_gnu.c index 6effb52..a2f8332 100644 --- a/lib/tar/test/tar_sparse_gnu.c +++ b/lib/tar/test/tar_sparse_gnu.c @@ -5,9 +5,9 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" +#include "sqfs/io.h" int main(int argc, char **argv) { @@ -19,7 +19,7 @@ int main(int argc, char **argv) TEST_ASSERT(chdir(TEST_PATH) == 0); - ret = istream_open_file(&fp, "sparse-files/gnu-small.tar"); + ret = sqfs_istream_open_file(&fp, "sparse-files/gnu-small.tar"); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_target_filled.c b/lib/tar/test/tar_target_filled.c index 85a1af4..93c3b02 100644 --- a/lib/tar/test/tar_target_filled.c +++ b/lib/tar/test/tar_target_filled.c @@ -5,9 +5,9 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" +#include "sqfs/io.h" int main(int argc, char **argv) { @@ -19,7 +19,7 @@ int main(int argc, char **argv) TEST_ASSERT(chdir(TEST_PATH) == 0); - ret = istream_open_file(&fp, "format-acceptance/link_filled.tar"); + ret = sqfs_istream_open_file(&fp, "format-acceptance/link_filled.tar"); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); diff --git a/lib/tar/test/tar_write_simple.c b/lib/tar/test/tar_write_simple.c index 28f199f..bc1b93e 100644 --- a/lib/tar/test/tar_write_simple.c +++ b/lib/tar/test/tar_write_simple.c @@ -7,7 +7,6 @@ #include "config.h" #include "tar/tar.h" #include "sqfs/io.h" -#include "io/file.h" #include "util/test.h" #include "sqfs/xattr.h" #include "compat.h" @@ -188,7 +187,8 @@ int main(int argc, char **argv) TEST_EQUAL_UI(wr_offset, sizeof(wr_buffer)); TEST_EQUAL_UI(sizeof(rd_buffer), sizeof(wr_buffer)); - ret = istream_open_file(&fp, STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + ret = sqfs_istream_open_file(&fp, + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); diff --git a/lib/tar/test/tar_xattr.c b/lib/tar/test/tar_xattr.c index 0e0579c..d074e31 100644 --- a/lib/tar/test/tar_xattr.c +++ b/lib/tar/test/tar_xattr.c @@ -5,10 +5,10 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" #include "sqfs/xattr.h" +#include "sqfs/io.h" int main(int argc, char **argv) { @@ -18,7 +18,8 @@ int main(int argc, char **argv) int ret; (void)argc; (void)argv; - ret = istream_open_file(&fp, STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + ret = sqfs_istream_open_file(&fp, + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_xattr_bin.c b/lib/tar/test/tar_xattr_bin.c index 617c356..68f23fe 100644 --- a/lib/tar/test/tar_xattr_bin.c +++ b/lib/tar/test/tar_xattr_bin.c @@ -5,10 +5,10 @@ * Copyright (C) 2019 David Oberhollenzer */ #include "config.h" -#include "io/file.h" #include "tar/tar.h" #include "util/test.h" #include "sqfs/xattr.h" +#include "sqfs/io.h" static const uint8_t value[] = { 0x00, 0x00, 0x00, 0x02, @@ -26,7 +26,8 @@ int main(int argc, char **argv) int ret; (void)argc; (void)argv; - ret = istream_open_file(&fp, STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + ret = sqfs_istream_open_file(&fp, + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/util/test/str_table.c b/lib/util/test/str_table.c index 4a62b59..02e5e67 100644 --- a/lib/util/test/str_table.c +++ b/lib/util/test/str_table.c @@ -7,10 +7,10 @@ #include "config.h" #include "util/str_table.h" -#include "io/istream.h" -#include "io/file.h" #include "compat.h" +#include "io/istream.h" #include "util/test.h" +#include "sqfs/io.h" static char *strings[1000]; @@ -21,7 +21,7 @@ static int read_strings(void) char *line; int i; - i = istream_open_file(&fp, "words.txt"); + i = sqfs_istream_open_file(&fp, "words.txt"); TEST_EQUAL_I(i, 0); TEST_NOT_NULL(fp); -- cgit v1.2.3