From c81e80bf35874696d9735f70e5c8327f92b5aca4 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 16 Jun 2023 01:41:06 +0200 Subject: libsquashfs: merge windows & unix file I/O primitives By adding additional close/duplicate primitives, the remaining, mostly identical istream/ostream/file code is mostly identical between Windows and Unix and be merged, mostly without stitches. Signed-off-by: David Oberhollenzer --- lib/sqfs/Makemodule.am | 9 +- lib/sqfs/src/io/file.c | 286 +++++++++++++++++++++++++++++++++++++++++++ lib/sqfs/src/io/istream.c | 203 ++++++++++++++++++++++++++++++ lib/sqfs/src/io/ostream.c | 221 +++++++++++++++++++++++++++++++++ lib/sqfs/src/io/unix.c | 87 +++++++++++++ lib/sqfs/src/io/win32.c | 120 ++++++++++++++++++ lib/sqfs/src/unix/io_file.c | 222 --------------------------------- lib/sqfs/src/unix/istream.c | 180 --------------------------- lib/sqfs/src/unix/ostream.c | 209 ------------------------------- lib/sqfs/src/win32/io_file.c | 271 ---------------------------------------- lib/sqfs/src/win32/istream.c | 190 ---------------------------- lib/sqfs/src/win32/ostream.c | 201 ------------------------------ 12 files changed, 921 insertions(+), 1278 deletions(-) create mode 100644 lib/sqfs/src/io/file.c create mode 100644 lib/sqfs/src/io/istream.c create mode 100644 lib/sqfs/src/io/ostream.c create mode 100644 lib/sqfs/src/io/unix.c create mode 100644 lib/sqfs/src/io/win32.c delete mode 100644 lib/sqfs/src/unix/io_file.c delete mode 100644 lib/sqfs/src/unix/istream.c delete mode 100644 lib/sqfs/src/unix/ostream.c delete mode 100644 lib/sqfs/src/win32/io_file.c delete mode 100644 lib/sqfs/src/win32/istream.c delete mode 100644 lib/sqfs/src/win32/ostream.c (limited to 'lib') diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index 83ca478..c722f16 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -30,7 +30,8 @@ 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/istream.c + lib/sqfs/src/misc.c lib/sqfs/src/istream.c lib/sqfs/src/io/istream.c \ + lib/sqfs/src/io/ostream.c lib/sqfs/src/io/file.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) @@ -57,15 +58,13 @@ 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 \ - lib/sqfs/src/win32/ostream.c lib/sqfs/src/win32/istream.c +libsquashfs_la_SOURCES += lib/sqfs/src/io/win32.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 \ - lib/sqfs/src/unix/ostream.c lib/sqfs/src/unix/istream.c +libsquashfs_la_SOURCES += lib/sqfs/src/io/unix.c endif if HAVE_PTHREAD diff --git a/lib/sqfs/src/io/file.c b/lib/sqfs/src/io/file.c new file mode 100644 index 0000000..881730d --- /dev/null +++ b/lib/sqfs/src/io/file.c @@ -0,0 +1,286 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * file.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" +#include "compat.h" + +#include +#include + +#if defined(_WIN32) || defined(__WINDOWS__) +static int get_file_size(HANDLE fd, sqfs_u64 *out) +{ + LARGE_INTEGER size; + if (!GetFileSizeEx(fd, &size)) + return -1; + *out = size.QuadPart; + return 0; +} +#else +#include +#include +#include + +static int get_file_size(int fd, sqfs_u64 *out) +{ + struct stat sb; + if (fstat(fd, &sb)) + return -1; + *out = sb.st_size; + return 0; +} +#endif + +typedef struct { + sqfs_file_t base; + + bool readonly; + sqfs_u64 size; + sqfs_file_handle_t fd; + + char name[]; +} sqfs_file_stdio_t; + + +static void stdio_destroy(sqfs_object_t *base) +{ + sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; + + sqfs_close_native_file(file->fd); + free(file); +} + +static sqfs_object_t *stdio_copy(const sqfs_object_t *base) +{ + const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base; + sqfs_file_stdio_t *copy; + size_t size; + + if (!file->readonly) { +#if defined(_WIN32) || defined(__WINDOWS__) + SetLastError(ERROR_NOT_SUPPORTED); +#else + errno = ENOTSUP; +#endif + return NULL; + } + + size = sizeof(*file) + strlen(file->name) + 1; + copy = calloc(1, size); + if (copy == NULL) + return NULL; + + memcpy(copy, file, size); + + if (sqfs_duplicate_native_file(file->fd, ©->fd)) { + os_error_t error = get_os_error_state(); + free(copy); + copy = NULL; + set_os_error_state(error); + } + + return (sqfs_object_t *)copy; +} + +#if defined(_WIN32) || defined(__WINDOWS__) +static int stdio_read_at(sqfs_file_t *base, sqfs_u64 offset, + void *buffer, size_t size) +{ + sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; + DWORD actually_read; + int ret; + + if (offset >= file->size) + return SQFS_ERROR_OUT_OF_BOUNDS; + + if (size == 0) + return 0; + + if ((offset + size - 1) >= file->size) + return SQFS_ERROR_OUT_OF_BOUNDS; + + ret = sqfs_seek_native_file(file->fd, offset, SQFS_FILE_SEEK_START); + if (ret) + return ret; + + while (size > 0) { + if (!ReadFile(file->fd, buffer, size, &actually_read, NULL)) + return SQFS_ERROR_IO; + + size -= actually_read; + buffer = (char *)buffer + actually_read; + } + + return 0; +} + +static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset, + const void *buffer, size_t size) +{ + sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; + DWORD actually_read; + int ret; + + if (size == 0) + return 0; + + ret = sqfs_seek_native_file(file->fd, offset, SQFS_FILE_SEEK_START); + if (ret) + return ret; + + while (size > 0) { + if (!WriteFile(file->fd, buffer, size, &actually_read, NULL)) + return SQFS_ERROR_IO; + + size -= actually_read; + buffer = (char *)buffer + actually_read; + offset += actually_read; + + if (offset > file->size) + file->size = offset; + } + + return 0; +} +#else +static int stdio_read_at(sqfs_file_t *base, sqfs_u64 offset, + void *buffer, size_t size) +{ + sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; + ssize_t ret; + + while (size > 0) { + ret = pread(file->fd, buffer, size, offset); + + if (ret < 0) { + if (errno == EINTR) + continue; + return SQFS_ERROR_IO; + } + + if (ret == 0) + return SQFS_ERROR_OUT_OF_BOUNDS; + + buffer = (char *)buffer + ret; + size -= ret; + offset += ret; + } + + return 0; +} + +static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset, + const void *buffer, size_t size) +{ + sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; + ssize_t ret; + + while (size > 0) { + ret = pwrite(file->fd, buffer, size, offset); + + if (ret < 0) { + if (errno == EINTR) + continue; + return SQFS_ERROR_IO; + } + + if (ret == 0) + return SQFS_ERROR_OUT_OF_BOUNDS; + + buffer = (const char *)buffer + ret; + size -= ret; + offset += ret; + } + + if (offset >= file->size) + file->size = offset; + + return 0; +} +#endif + +static sqfs_u64 stdio_get_size(const sqfs_file_t *base) +{ + const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base; + + return file->size; +} + +static int stdio_truncate(sqfs_file_t *base, sqfs_u64 size) +{ + sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; + int ret; + + ret = sqfs_seek_native_file(file->fd, size, SQFS_FILE_SEEK_START | + SQFS_FILE_SEEK_TRUNCATE); + if (ret) + return ret; + + file->size = size; + return 0; +} + +static const char *stdio_get_filename(sqfs_file_t *file) +{ + return ((sqfs_file_stdio_t *)file)->name; +} + +sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) +{ + bool file_opened = false; + sqfs_file_stdio_t *file; + size_t size, namelen; + sqfs_file_t *base; + os_error_t err; + + namelen = strlen(filename); + size = sizeof(*file) + 1; + + if (SZ_ADD_OV(size, namelen, &size)) + goto fail_no_mem; + + file = calloc(1, size); + base = (sqfs_file_t *)file; + if (file == NULL) + goto fail_no_mem; + + sqfs_object_init(file, stdio_destroy, stdio_copy); + memcpy(file->name, filename, namelen); + + if (sqfs_open_native_file(&file->fd, filename, flags)) + goto fail; + + file_opened = true; + if (get_file_size(file->fd, &file->size)) + goto fail; + + file->readonly = (flags & SQFS_FILE_OPEN_READ_ONLY) != 0; + + base->read_at = stdio_read_at; + base->write_at = stdio_write_at; + base->get_size = stdio_get_size; + base->truncate = stdio_truncate; + base->get_filename = stdio_get_filename; + return base; +fail: + err = get_os_error_state(); + if (file_opened) + sqfs_close_native_file(file->fd); + free(file); + set_os_error_state(err); + return NULL; +fail_no_mem: +#if defined(_WIN32) || defined(__WINDOWS__) + SetLastError(ERROR_NOT_ENOUGH_MEMORY); +#else + errno = ENOMEM; +#endif + return NULL; +} diff --git a/lib/sqfs/src/io/istream.c b/lib/sqfs/src/io/istream.c new file mode 100644 index 0000000..c3399fe --- /dev/null +++ b/lib/sqfs/src/io/istream.c @@ -0,0 +1,203 @@ +/* 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 "compat.h" + +#include +#include +#include +#include + +#if defined(_WIN32) || defined(__WINDOWS__) +static SSIZE_T read(HANDLE fd, void *ptr, size_t size) +{ + DWORD ret; + + if (!ReadFile(fd, ptr, size, &ret, NULL)) { + os_error_t error = get_os_error_state(); + set_os_error_state(error); + + if (error.w32_errno == ERROR_HANDLE_EOF || + error.w32_errno == ERROR_BROKEN_PIPE) { + return 0; + } + + return -1; + } + + return ret; +} +#endif + +#define BUFSZ (131072) + +typedef struct { + sqfs_istream_t base; + char *path; + sqfs_file_handle_t 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 !defined(_WIN32) && !defined(__WINDOWS__) + if (errno == EINTR) + continue; +#endif + 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; + + sqfs_close_native_file(file->fd); + free(file->path); + free(file); +} + +int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, + sqfs_file_handle_t fd, sqfs_u32 flags) +{ + file_istream_t *file; + sqfs_istream_t *strm; + int ret; + + if (flags & ~(SQFS_FILE_OPEN_ALL_FLAGS)) + return SQFS_ERROR_UNSUPPORTED; + + file = calloc(1, sizeof(*file)); + 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) { + os_error_t err = get_os_error_state(); + free(file); + set_os_error_state(err); + return SQFS_ERROR_ALLOC; + } + + ret = sqfs_duplicate_native_file(fd, &file->fd); + if (ret) { + os_error_t err = get_os_error_state(); + free(file->path); + free(file); + set_os_error_state(err); + return ret; + } + sqfs_close_native_file(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_u32 flags) +{ + sqfs_file_handle_t fd; + int ret; + + flags |= SQFS_FILE_OPEN_READ_ONLY; + + if (flags & (SQFS_FILE_OPEN_OVERWRITE | SQFS_FILE_OPEN_NO_SPARSE)) + return SQFS_ERROR_ARG_INVALID; + + ret = sqfs_open_native_file(&fd, path, flags); + if (ret) + return ret; + + ret = sqfs_istream_open_handle(out, path, fd, flags); + if (ret != 0) { + os_error_t err = get_os_error_state(); + sqfs_close_native_file(fd); + set_os_error_state(err); + } + + return ret; +} diff --git a/lib/sqfs/src/io/ostream.c b/lib/sqfs/src/io/ostream.c new file mode 100644 index 0000000..ba8e29a --- /dev/null +++ b/lib/sqfs/src/io/ostream.c @@ -0,0 +1,221 @@ +/* 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 "compat.h" + +#include +#include +#include + +typedef struct { + sqfs_ostream_t base; + char *path; + sqfs_u32 flags; + sqfs_file_handle_t fd; + + sqfs_u64 sparse_count; + sqfs_u64 size; +} file_ostream_t; + +static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size) +{ + while (size > 0) { +#if defined(_WIN32) || defined(__WINDOWS__) + DWORD ret; + if (!WriteFile(file->fd, data, size, &ret, NULL)) + return SQFS_ERROR_IO; +#else + 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; + } +#endif + 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 > bufsz ? + bufsz : file->sparse_count; + + ret = write_all(file, buffer, diff); + if (ret) { + os_error_t err = get_os_error_state(); + free(buffer); + set_os_error_state(err); + return ret; + } + + file->sparse_count -= diff; + } + + free(buffer); + } else { + ret = sqfs_seek_native_file(file->fd, file->sparse_count, + SQFS_FILE_SEEK_CURRENT | + SQFS_FILE_SEEK_TRUNCATE); + if (ret) + return ret; + + 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 defined(_WIN32) || defined(__WINDOWS__) + if (!FlushFileBuffers(file->fd)) + return SQFS_ERROR_IO; +#else + if (fsync(file->fd) != 0 && errno != EINVAL) + return SQFS_ERROR_IO; +#endif + return 0; +} + +static void file_destroy(sqfs_object_t *obj) +{ + file_ostream_t *file = (file_ostream_t *)obj; + + sqfs_close_native_file(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, sqfs_u32 flags) +{ + file_ostream_t *file; + sqfs_ostream_t *strm; + int ret; + + *out = NULL; + if (flags & ~(SQFS_FILE_OPEN_ALL_FLAGS)) + return SQFS_ERROR_UNSUPPORTED; + + file = calloc(1, sizeof(*file)); + strm = (sqfs_ostream_t *)file; + if (file == NULL) + return SQFS_ERROR_ALLOC; + + sqfs_object_init(file, file_destroy, NULL); + + file->path = strdup(path); + if (file->path == NULL) { + os_error_t err = get_os_error_state(); + free(file); + set_os_error_state(err); + return SQFS_ERROR_ALLOC; + } + + ret = sqfs_duplicate_native_file(fd, &file->fd); + if (ret) { + os_error_t err = get_os_error_state(); + free(file->path); + free(file); + set_os_error_state(err); + return ret; + } + + sqfs_close_native_file(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, + sqfs_u32 flags) +{ + sqfs_file_handle_t fd; + int ret; + + *out = NULL; + if (flags & SQFS_FILE_OPEN_READ_ONLY) + return SQFS_ERROR_ARG_INVALID; + + ret = sqfs_open_native_file(&fd, path, flags); + if (ret) + return ret; + + ret = sqfs_ostream_open_handle(out, path, fd, flags); + if (ret) { + os_error_t err = get_os_error_state(); + sqfs_close_native_file(fd); + set_os_error_state(err); + return ret; + } + + return 0; +} diff --git a/lib/sqfs/src/io/unix.c b/lib/sqfs/src/io/unix.c new file mode 100644 index 0000000..45af2ce --- /dev/null +++ b/lib/sqfs/src/io/unix.c @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * unix.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" + +#include +#include +#include + +int sqfs_open_native_file(sqfs_file_handle_t *out, const char *filename, + sqfs_u32 flags) +{ + int open_mode; + + *out = -1; + + if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) + return SQFS_ERROR_UNSUPPORTED; + + if (flags & SQFS_FILE_OPEN_READ_ONLY) { + open_mode = O_RDONLY; + } else { + open_mode = O_CREAT | O_RDWR; + + if (flags & SQFS_FILE_OPEN_OVERWRITE) { + open_mode |= O_TRUNC; + } else { + open_mode |= O_EXCL; + } + } + + *out = open(filename, open_mode, 0644); + + return (*out < 0) ? SQFS_ERROR_IO : 0; +} + +void sqfs_close_native_file(sqfs_file_handle_t fd) +{ + while (close(fd) != 0 && errno == EINTR) + ; +} + +int sqfs_duplicate_native_file(sqfs_file_handle_t in, sqfs_file_handle_t *out) +{ + *out = dup(in); + return (*out < 0) ? SQFS_ERROR_IO : 0; +} + +int sqfs_seek_native_file(sqfs_file_handle_t fd, + sqfs_s64 offset, sqfs_u32 flags) +{ + int whence; + off_t off; + + switch (flags & SQFS_FILE_SEEK_TYPE_MASK) { + case SQFS_FILE_SEEK_START: whence = SEEK_SET; break; + case SQFS_FILE_SEEK_CURRENT: whence = SEEK_CUR; break; + case SQFS_FILE_SEEK_END: whence = SEEK_END; break; + default: return SQFS_ERROR_UNSUPPORTED; + } + + if (flags & ~(SQFS_FILE_SEEK_FLAG_MASK | SQFS_FILE_SEEK_TYPE_MASK)) + return SQFS_ERROR_UNSUPPORTED; + + off = lseek(fd, offset, whence); + if (off == ((off_t)-1)) { + if (errno == ESPIPE) + return SQFS_ERROR_UNSUPPORTED; + return SQFS_ERROR_IO; + } + + if (flags & SQFS_FILE_SEEK_TRUNCATE) { + while (ftruncate(fd, off) != 0) { + if (errno != EINTR) + return SQFS_ERROR_IO; + } + } + + return 0; +} diff --git a/lib/sqfs/src/io/win32.c b/lib/sqfs/src/io/win32.c new file mode 100644 index 0000000..decfd23 --- /dev/null +++ b/lib/sqfs/src/io/win32.c @@ -0,0 +1,120 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * win32.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" +#include "compat.h" + +#define WIN32_LEAN_AND_MEAN +#include + +int sqfs_open_native_file(sqfs_file_handle_t *out, const char *filename, + sqfs_u32 flags) +{ + int access_flags, creation_mode, share_mode; + WCHAR *wpath = NULL; + os_error_t err; + DWORD length; + + *out = INVALID_HANDLE_VALUE; + + if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) { + SetLastError(ERROR_INVALID_PARAMETER); + return SQFS_ERROR_UNSUPPORTED; + } + + if (flags & SQFS_FILE_OPEN_READ_ONLY) { + access_flags = GENERIC_READ; + creation_mode = OPEN_EXISTING; + share_mode = FILE_SHARE_READ; + } else { + access_flags = GENERIC_READ | GENERIC_WRITE; + share_mode = 0; + + if (flags & SQFS_FILE_OPEN_OVERWRITE) { + creation_mode = CREATE_ALWAYS; + } else { + creation_mode = CREATE_NEW; + } + } + + if (flags & SQFS_FILE_OPEN_NO_CHARSET_XFRM) { + *out = CreateFileA(filename, access_flags, share_mode, NULL, + creation_mode, FILE_ATTRIBUTE_NORMAL, NULL); + } else { + length = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); + if (length <= 0) + return SQFS_ERROR_INTERNAL; + + wpath = calloc(sizeof(wpath[0]), length + 1); + if (wpath == NULL) { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return SQFS_ERROR_ALLOC; + } + + MultiByteToWideChar(CP_UTF8, 0, filename, -1, + wpath, length + 1); + wpath[length] = '\0'; + + *out = CreateFileW(wpath, access_flags, share_mode, NULL, + creation_mode, FILE_ATTRIBUTE_NORMAL, + NULL); + + err = get_os_error_state(); + free(wpath); + set_os_error_state(err); + } + + return (*out == INVALID_HANDLE_VALUE) ? SQFS_ERROR_IO : 0; +} + +void sqfs_close_native_file(sqfs_file_handle_t hnd) +{ + CloseHandle(hnd); +} + +int sqfs_duplicate_native_file(sqfs_file_handle_t in, sqfs_file_handle_t *out) +{ + HANDLE hProc = GetCurrentProcess(); + BOOL ret = DuplicateHandle(hProc, in, hProc, out, 0, + FALSE, DUPLICATE_SAME_ACCESS); + + return ret ? 0 : SQFS_ERROR_IO; +} + +int sqfs_seek_native_file(sqfs_file_handle_t fd, + sqfs_s64 offset, sqfs_u32 flags) +{ + LARGE_INTEGER pos; + DWORD whence; + + switch (type & SQFS_FILE_SEEK_TYPE_MASK) { + case SQFS_FILE_SEEK_START: whence = FILE_BEGIN; break; + case SQFS_FILE_SEEK_CURRENT: whence = FILE_CURRENT; break; + case SQFS_FILE_SEEK_END: whence = FILE_END; break; + default: return SQFS_ERROR_UNSUPPORTED; + } + + if (flags & ~(SQFS_FILE_SEEK_FLAG_MASK | SQFS_FILE_SEEK_TYPE_MASK)) + return SQFS_ERROR_UNSUPPORTED; + + if (GetFileType(fd) != FILE_TYPE_DISK) + return SQFS_ERROR_UNSUPPORTED; + + pos.QuadPart = offset; + if (!SetFilePointerEx(fd, pos, NULL, whence)) + return SQFS_ERROR_IO; + + if (flags & SQFS_FILE_SEEK_TRUNCATE) { + if (!SetEndOfFile(fd)) + return SQFS_ERROR_IO; + } + + return 0; +} diff --git a/lib/sqfs/src/unix/io_file.c b/lib/sqfs/src/unix/io_file.c deleted file mode 100644 index f9b602a..0000000 --- a/lib/sqfs/src/unix/io_file.c +++ /dev/null @@ -1,222 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * io_file.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#define SQFS_BUILDING_DLL -#include "config.h" - -#include "sqfs/io.h" -#include "sqfs/error.h" -#include "compat.h" - -#include -#include -#include -#include -#include -#include - - -typedef struct { - sqfs_file_t base; - - bool readonly; - sqfs_u64 size; - sqfs_file_handle_t fd; - - char name[]; -} sqfs_file_stdio_t; - - -static void stdio_destroy(sqfs_object_t *base) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - - close(file->fd); - free(file); -} - -static sqfs_object_t *stdio_copy(const sqfs_object_t *base) -{ - const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base; - sqfs_file_stdio_t *copy; - size_t size; - int err; - - if (!file->readonly) { - errno = ENOTSUP; - return NULL; - } - - size = sizeof(*file) + strlen(file->name) + 1; - copy = calloc(1, size); - if (copy == NULL) - return NULL; - - memcpy(copy, file, size); - - copy->fd = dup(file->fd); - if (copy->fd < 0) { - err = errno; - free(copy); - copy = NULL; - errno = err; - } - - return (sqfs_object_t *)copy; -} - -static int stdio_read_at(sqfs_file_t *base, sqfs_u64 offset, - void *buffer, size_t size) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - ssize_t ret; - - while (size > 0) { - ret = pread(file->fd, buffer, size, offset); - - if (ret < 0) { - if (errno == EINTR) - continue; - return SQFS_ERROR_IO; - } - - if (ret == 0) - return SQFS_ERROR_OUT_OF_BOUNDS; - - buffer = (char *)buffer + ret; - size -= ret; - offset += ret; - } - - return 0; -} - -static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset, - const void *buffer, size_t size) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - ssize_t ret; - - while (size > 0) { - ret = pwrite(file->fd, buffer, size, offset); - - if (ret < 0) { - if (errno == EINTR) - continue; - return SQFS_ERROR_IO; - } - - if (ret == 0) - return SQFS_ERROR_OUT_OF_BOUNDS; - - buffer = (const char *)buffer + ret; - size -= ret; - offset += ret; - } - - if (offset >= file->size) - file->size = offset; - - return 0; -} - -static sqfs_u64 stdio_get_size(const sqfs_file_t *base) -{ - const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base; - - return file->size; -} - -static int stdio_truncate(sqfs_file_t *base, sqfs_u64 size) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - - if (ftruncate(file->fd, size)) - return SQFS_ERROR_IO; - - file->size = size; - return 0; -} - -static const char *stdio_get_filename(sqfs_file_t *file) -{ - return ((sqfs_file_stdio_t *)file)->name; -} - -int sqfs_open_native_file(sqfs_file_handle_t *out, const char *filename, - sqfs_u32 flags) -{ - int open_mode; - - *out = -1; - - if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) - return SQFS_ERROR_UNSUPPORTED; - - if (flags & SQFS_FILE_OPEN_READ_ONLY) { - open_mode = O_RDONLY; - } else { - open_mode = O_CREAT | O_RDWR; - - if (flags & SQFS_FILE_OPEN_OVERWRITE) { - open_mode |= O_TRUNC; - } else { - open_mode |= O_EXCL; - } - } - - *out = open(filename, open_mode, 0644); - - return (*out < 0) ? SQFS_ERROR_IO : 0; -} - -sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) -{ - sqfs_file_stdio_t *file; - size_t size, namelen; - sqfs_file_t *base; - struct stat sb; - int temp; - - namelen = strlen(filename); - size = sizeof(*file) + 1; - - if (SZ_ADD_OV(size, namelen, &size)) { - errno = EOVERFLOW; - return NULL; - } - - file = calloc(1, size); - base = (sqfs_file_t *)file; - if (file == NULL) - return NULL; - - sqfs_object_init(file, stdio_destroy, stdio_copy); - memcpy(file->name, filename, namelen); - - if (sqfs_open_native_file(&file->fd, filename, flags)) - goto fail; - - if (fstat(file->fd, &sb)) - goto fail; - - file->size = sb.st_size; - file->readonly = (flags & SQFS_FILE_OPEN_READ_ONLY) != 0; - - base->read_at = stdio_read_at; - base->write_at = stdio_write_at; - base->get_size = stdio_get_size; - base->truncate = stdio_truncate; - base->get_filename = stdio_get_filename; - return base; -fail: - temp = errno; - if (file->fd >= 0) - close(file->fd); - free(file); - errno = temp; - return NULL; -} diff --git a/lib/sqfs/src/unix/istream.c b/lib/sqfs/src/unix/istream.c deleted file mode 100644 index a0a3469..0000000 --- a/lib/sqfs/src/unix/istream.c +++ /dev/null @@ -1,180 +0,0 @@ -/* 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, sqfs_u32 flags) -{ - file_istream_t *file; - sqfs_istream_t *strm; - - if (flags & ~(SQFS_FILE_OPEN_ALL_FLAGS)) - return SQFS_ERROR_UNSUPPORTED; - - file = calloc(1, sizeof(*file)); - 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_u32 flags) -{ - sqfs_file_handle_t fd; - int ret; - - flags |= SQFS_FILE_OPEN_READ_ONLY; - - if (flags & (SQFS_FILE_OPEN_OVERWRITE | SQFS_FILE_OPEN_NO_SPARSE)) - return SQFS_ERROR_ARG_INVALID; - - ret = sqfs_open_native_file(&fd, path, flags); - if (ret) - return ret; - - ret = sqfs_istream_open_handle(out, path, fd, flags); - 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 deleted file mode 100644 index efdc3cd..0000000 --- a/lib/sqfs/src/unix/ostream.c +++ /dev/null @@ -1,209 +0,0 @@ -/* 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, sqfs_u32 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, - sqfs_u32 flags) -{ - sqfs_file_handle_t fd; - int ret; - - *out = NULL; - if (flags & SQFS_FILE_OPEN_READ_ONLY) - return SQFS_ERROR_ARG_INVALID; - - 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/io_file.c b/lib/sqfs/src/win32/io_file.c deleted file mode 100644 index 4d76b94..0000000 --- a/lib/sqfs/src/win32/io_file.c +++ /dev/null @@ -1,271 +0,0 @@ -/* SPDX-License-Identifier: LGPL-3.0-or-later */ -/* - * io_file.c - * - * Copyright (C) 2019 David Oberhollenzer - */ -#define SQFS_BUILDING_DLL -#include "config.h" - -#include "sqfs/io.h" -#include "sqfs/error.h" -#include "compat.h" - -#include - -#define WIN32_LEAN_AND_MEAN -#include - - -typedef struct { - sqfs_file_t base; - - bool readonly; - sqfs_u64 size; - sqfs_file_handle_t fd; - - char name[]; -} sqfs_file_stdio_t; - - -static void stdio_destroy(sqfs_object_t *base) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - - CloseHandle(file->fd); - free(file); -} - -static sqfs_object_t *stdio_copy(const sqfs_object_t *base) -{ - const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base; - sqfs_file_stdio_t *copy; - size_t size; - BOOL ret; - - if (!file->readonly) { - SetLastError(ERROR_NOT_SUPPORTED); - return NULL; - } - - size = sizeof(*file) + strlen(file->name) + 1; - copy = calloc(1, size); - if (copy == NULL) - return NULL; - - memcpy(copy, file, size); - - ret = DuplicateHandle(GetCurrentProcess(), file->fd, - GetCurrentProcess(), ©->fd, - 0, FALSE, DUPLICATE_SAME_ACCESS); - - if (!ret) { - os_error_t err = get_os_error_state(); - free(copy); - set_os_error_state(err); - return NULL; - } - - return (sqfs_object_t *)copy; -} - -static int stdio_read_at(sqfs_file_t *base, sqfs_u64 offset, - void *buffer, size_t size) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - DWORD actually_read; - LARGE_INTEGER pos; - - if (offset >= file->size) - return SQFS_ERROR_OUT_OF_BOUNDS; - - if (size == 0) - return 0; - - if ((offset + size - 1) >= file->size) - return SQFS_ERROR_OUT_OF_BOUNDS; - - pos.QuadPart = offset; - - if (!SetFilePointerEx(file->fd, pos, NULL, FILE_BEGIN)) - return SQFS_ERROR_IO; - - while (size > 0) { - if (!ReadFile(file->fd, buffer, size, &actually_read, NULL)) - return SQFS_ERROR_IO; - - size -= actually_read; - buffer = (char *)buffer + actually_read; - } - - return 0; -} - -static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset, - const void *buffer, size_t size) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - DWORD actually_read; - LARGE_INTEGER pos; - - if (size == 0) - return 0; - - pos.QuadPart = offset; - - if (!SetFilePointerEx(file->fd, pos, NULL, FILE_BEGIN)) - return SQFS_ERROR_IO; - - while (size > 0) { - if (!WriteFile(file->fd, buffer, size, &actually_read, NULL)) - return SQFS_ERROR_IO; - - size -= actually_read; - buffer = (char *)buffer + actually_read; - offset += actually_read; - - if (offset > file->size) - file->size = offset; - } - - return 0; -} - -static sqfs_u64 stdio_get_size(const sqfs_file_t *base) -{ - const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base; - - return file->size; -} - -static int stdio_truncate(sqfs_file_t *base, sqfs_u64 size) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - LARGE_INTEGER pos; - - pos.QuadPart = size; - - if (!SetFilePointerEx(file->fd, pos, NULL, FILE_BEGIN)) - return SQFS_ERROR_IO; - - if (!SetEndOfFile(file->fd)) - return SQFS_ERROR_IO; - - file->size = size; - return 0; -} - -static const char *stdio_get_filename(sqfs_file_t *file) -{ - return ((sqfs_file_stdio_t *)file)->name; -} - -int sqfs_open_native_file(sqfs_file_handle_t *out, const char *filename, - sqfs_u32 flags) -{ - int access_flags, creation_mode, share_mode; - WCHAR *wpath = NULL; - os_error_t err; - DWORD length; - - *out = INVALID_HANDLE_VALUE; - - if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) { - SetLastError(ERROR_INVALID_PARAMETER); - return SQFS_ERROR_UNSUPPORTED; - } - - if (flags & SQFS_FILE_OPEN_READ_ONLY) { - access_flags = GENERIC_READ; - creation_mode = OPEN_EXISTING; - share_mode = FILE_SHARE_READ; - } else { - access_flags = GENERIC_READ | GENERIC_WRITE; - share_mode = 0; - - if (flags & SQFS_FILE_OPEN_OVERWRITE) { - creation_mode = CREATE_ALWAYS; - } else { - creation_mode = CREATE_NEW; - } - } - - if (flags & SQFS_FILE_OPEN_NO_CHARSET_XFRM) { - *out = CreateFileA(filename, access_flags, share_mode, NULL, - creation_mode, FILE_ATTRIBUTE_NORMAL, NULL); - } else { - length = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); - if (length <= 0) - return SQFS_ERROR_INTERNAL; - - wpath = calloc(sizeof(wpath[0]), length + 1); - if (wpath == NULL) { - SetLastError(ERROR_NOT_ENOUGH_MEMORY); - return SQFS_ERROR_ALLOC; - } - - MultiByteToWideChar(CP_UTF8, 0, filename, -1, - wpath, length + 1); - wpath[length] = '\0'; - - *out = CreateFileW(wpath, access_flags, share_mode, NULL, - creation_mode, FILE_ATTRIBUTE_NORMAL, - NULL); - - err = get_os_error_state(); - free(wpath); - set_os_error_state(err); - } - - return (*out == INVALID_HANDLE_VALUE) ? SQFS_ERROR_IO : 0; -} - -sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) -{ - sqfs_file_stdio_t *file; - size_t namelen, total; - LARGE_INTEGER size; - sqfs_file_t *base; - os_error_t err; - - namelen = strlen(filename); - total = sizeof(*file) + 1; - - if (SZ_ADD_OV(total, namelen, &total)) { - SetLastError(ERROR_NOT_ENOUGH_MEMORY); - return NULL; - } - - file = calloc(1, total); - base = (sqfs_file_t *)file; - if (file == NULL) { - SetLastError(ERROR_NOT_ENOUGH_MEMORY); - return NULL; - } - - sqfs_object_init(file, stdio_destroy, stdio_copy); - memcpy(file->name, filename, namelen); - - if (sqfs_open_native_file(&file->fd, filename, flags)) - goto fail; - - if (!GetFileSizeEx(file->fd, &size)) - goto fail; - - file->size = size.QuadPart; - file->readonly = (flags & SQFS_FILE_OPEN_READ_ONLY) != 0; - - base->read_at = stdio_read_at; - base->write_at = stdio_write_at; - base->get_size = stdio_get_size; - base->truncate = stdio_truncate; - base->get_filename = stdio_get_filename; - return base; -fail: - err = get_os_error_state(); - if (file->fd != INVALID_HANDLE_VALUE) - CloseHandle(file->fd); - free(file); - set_os_error_state(err); - return NULL; -} diff --git a/lib/sqfs/src/win32/istream.c b/lib/sqfs/src/win32/istream.c deleted file mode 100644 index aeb3352..0000000 --- a/lib/sqfs/src/win32/istream.c +++ /dev/null @@ -1,190 +0,0 @@ -/* 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)) { - os_error_t error = get_os_error_state(); - - if (error.w32_errno == ERROR_HANDLE_EOF || - error.w32_errno == ERROR_BROKEN_PIPE) { - file->eof = true; - break; - } - - set_os_error_state(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, sqfs_u32 flags) -{ - file_istream_t *file; - sqfs_istream_t *strm; - BOOL ret; - - if (flags & ~(SQFS_FILE_OPEN_ALL_FLAGS)) - return SQFS_ERROR_UNSUPPORTED; - - file = calloc(1, sizeof(*file)); - 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) { - os_error_t err = get_os_error_state(); - free(file); - set_os_error_state(err); - return SQFS_ERROR_ALLOC; - } - - ret = DuplicateHandle(GetCurrentProcess(), hnd, - GetCurrentProcess(), &file->hnd, - 0, FALSE, DUPLICATE_SAME_ACCESS); - if (!ret) { - os_error_t err = get_os_error_state(); - free(file->path); - free(file); - set_os_error_state(err); - 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_u32 flags) -{ - sqfs_file_handle_t hnd; - int ret; - - flags |= SQFS_FILE_OPEN_READ_ONLY; - - if (flags & (SQFS_FILE_OPEN_OVERWRITE | SQFS_FILE_OPEN_NO_SPARSE)) - return SQFS_ERROR_ARG_INVALID; - - ret = sqfs_open_native_file(&hnd, path, flags); - if (ret) - return ret; - - ret = sqfs_istream_open_handle(out, path, hnd, flags); - if (ret) { - os_error_t err = get_os_error_state(); - CloseHandle(hnd); - set_os_error_state(err); - return ret; - } - - return 0; -} diff --git a/lib/sqfs/src/win32/ostream.c b/lib/sqfs/src/win32/ostream.c deleted file mode 100644 index 2f85c29..0000000 --- a/lib/sqfs/src/win32/ostream.c +++ /dev/null @@ -1,201 +0,0 @@ -/* 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) { - os_error_t err = get_os_error_state(); - free(buffer); - set_os_error_state(err); - 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, sqfs_u32 flags) -{ - file_ostream_t *file; - sqfs_ostream_t *strm; - BOOL ret; - - if (flags & ~(SQFS_FILE_OPEN_ALL_FLAGS)) - return SQFS_ERROR_UNSUPPORTED; - - file = calloc(1, sizeof(*file)); - strm = (sqfs_ostream_t *)file; - 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) { - os_error_t err = get_os_error_state(); - free(file->path); - free(file); - set_os_error_state(err); - 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, - sqfs_u32 flags) -{ - sqfs_file_handle_t hnd; - int ret; - - *out = NULL; - if (flags & SQFS_FILE_OPEN_READ_ONLY) - return SQFS_ERROR_ARG_INVALID; - - ret = sqfs_open_native_file(&hnd, path, flags); - if (ret) - return ret; - - ret = sqfs_ostream_open_handle(out, path, hnd, flags); - if (ret) { - os_error_t err = get_os_error_state(); - CloseHandle(hnd); - set_os_error_state(err); - return SQFS_ERROR_IO; - } - - return 0; -} -- cgit v1.2.3