From acb4156bd5c730d0c62585eb78b2b6ff21a12fe4 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Fri, 11 Oct 2019 12:53:47 +0200 Subject: Add Windows implementation for sqfs_file_t This commit moves the generic unix implementation into a "unix" subdirectory and adds a "win32" subdirectory with a winapi based implementation. Signed-off-by: David Oberhollenzer --- lib/sqfs/Makemodule.am | 8 ++- lib/sqfs/io_file.c | 163 --------------------------------------------- lib/sqfs/unix/io_file.c | 163 +++++++++++++++++++++++++++++++++++++++++++++ lib/sqfs/win32/io_file.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 340 insertions(+), 164 deletions(-) delete mode 100644 lib/sqfs/io_file.c create mode 100644 lib/sqfs/unix/io_file.c create mode 100644 lib/sqfs/win32/io_file.c (limited to 'lib/sqfs') diff --git a/lib/sqfs/Makemodule.am b/lib/sqfs/Makemodule.am index 3742f7f..821f22a 100644 --- a/lib/sqfs/Makemodule.am +++ b/lib/sqfs/Makemodule.am @@ -10,7 +10,7 @@ LIBSQFS_HEARDS = include/sqfs/meta_writer.h \ include/sqfs/xattr_reader.h include/sqfs/xattr_writer.h libsquashfs_la_SOURCES = $(LIBSQFS_HEARDS) lib/sqfs/id_table.c lib/sqfs/super.c -libsquashfs_la_SOURCES += lib/sqfs/readdir.c lib/sqfs/io_file.c lib/sqfs/xattr.c +libsquashfs_la_SOURCES += lib/sqfs/readdir.c lib/sqfs/xattr.c libsquashfs_la_SOURCES += lib/sqfs/write_table.c lib/sqfs/meta_writer.c libsquashfs_la_SOURCES += lib/sqfs/read_super.c lib/sqfs/meta_reader.c libsquashfs_la_SOURCES += lib/sqfs/read_inode.c lib/sqfs/write_inode.c @@ -30,6 +30,12 @@ libsquashfs_la_CFLAGS += $(ZSTD_CFLAGS) $(PTHREAD_CFLAGS) libsquashfs_la_LIBADD = $(XZ_LIBS) $(ZLIB_LIBS) $(LZO_LIBS) $(LZ4_LIBS) libsquashfs_la_LIBADD += $(ZSTD_LIBS) $(PTHREAD_LIBS) libutil.la +if WINDOWS +libsquashfs_la_SOURCES += lib/sqfs/win32/io_file.c +else +libsquashfs_la_SOURCES += lib/sqfs/unix/io_file.c +endif + if HAVE_PTHREAD libsquashfs_la_SOURCES += lib/sqfs/data_writer/pthread.c libsquashfs_la_CPPFLAGS += -DWITH_PTHREAD diff --git a/lib/sqfs/io_file.c b/lib/sqfs/io_file.c deleted file mode 100644 index f93af4d..0000000 --- a/lib/sqfs/io_file.c +++ /dev/null @@ -1,163 +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 -#include -#include -#include -#include - - -typedef struct { - sqfs_file_t base; - - sqfs_u64 size; - int fd; -} sqfs_file_stdio_t; - - -static void stdio_destroy(sqfs_file_t *base) -{ - sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; - - close(file->fd); - free(file); -} - -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; -} - - -sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) -{ - sqfs_file_stdio_t *file; - int open_mode, temp; - sqfs_file_t *base; - struct stat sb; - - if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) { - errno = EINVAL; - return NULL; - } - - file = calloc(1, sizeof(*file)); - base = (sqfs_file_t *)file; - if (file == NULL) - return NULL; - - 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; - } - } - - file->fd = open(filename, open_mode, 0600); - if (file->fd < 0) { - temp = errno; - free(file); - errno = temp; - return NULL; - } - - if (fstat(file->fd, &sb)) { - temp = errno; - close(file->fd); - free(file); - errno = temp; - return NULL; - } - - file->size = sb.st_size; - - base->destroy = stdio_destroy; - base->read_at = stdio_read_at; - base->write_at = stdio_write_at; - base->get_size = stdio_get_size; - base->truncate = stdio_truncate; - return base; -} diff --git a/lib/sqfs/unix/io_file.c b/lib/sqfs/unix/io_file.c new file mode 100644 index 0000000..f93af4d --- /dev/null +++ b/lib/sqfs/unix/io_file.c @@ -0,0 +1,163 @@ +/* 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 +#include +#include +#include +#include + + +typedef struct { + sqfs_file_t base; + + sqfs_u64 size; + int fd; +} sqfs_file_stdio_t; + + +static void stdio_destroy(sqfs_file_t *base) +{ + sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; + + close(file->fd); + free(file); +} + +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; +} + + +sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) +{ + sqfs_file_stdio_t *file; + int open_mode, temp; + sqfs_file_t *base; + struct stat sb; + + if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) { + errno = EINVAL; + return NULL; + } + + file = calloc(1, sizeof(*file)); + base = (sqfs_file_t *)file; + if (file == NULL) + return NULL; + + 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; + } + } + + file->fd = open(filename, open_mode, 0600); + if (file->fd < 0) { + temp = errno; + free(file); + errno = temp; + return NULL; + } + + if (fstat(file->fd, &sb)) { + temp = errno; + close(file->fd); + free(file); + errno = temp; + return NULL; + } + + file->size = sb.st_size; + + base->destroy = stdio_destroy; + base->read_at = stdio_read_at; + base->write_at = stdio_write_at; + base->get_size = stdio_get_size; + base->truncate = stdio_truncate; + return base; +} diff --git a/lib/sqfs/win32/io_file.c b/lib/sqfs/win32/io_file.c new file mode 100644 index 0000000..c3de1fd --- /dev/null +++ b/lib/sqfs/win32/io_file.c @@ -0,0 +1,170 @@ +/* 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 + +#define WIN32_LEAN_AND_MEAN +#include + + +typedef struct { + sqfs_file_t base; + + sqfs_u64 size; + HANDLE fd; +} sqfs_file_stdio_t; + + +static void stdio_destroy(sqfs_file_t *base) +{ + sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base; + + CloseHandle(file->fd); + free(file); +} + +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; +} + + +sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) +{ + int access_flags, creation_mode; + sqfs_file_stdio_t *file; + LARGE_INTEGER size; + sqfs_file_t *base; + + if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) + return NULL; + + file = calloc(1, sizeof(*file)); + base = (sqfs_file_t *)file; + if (file == NULL) + return NULL; + + if (flags & SQFS_FILE_OPEN_READ_ONLY) { + access_flags = GENERIC_READ; + creation_mode = OPEN_EXISTING; + } else { + access_flags = GENERIC_READ | GENERIC_WRITE; + + if (flags & SQFS_FILE_OPEN_OVERWRITE) { + creation_mode = TRUNCATE_EXISTING; + } else { + creation_mode = CREATE_NEW; + } + } + + file->fd = CreateFile(filename, access_flags, 0, NULL, creation_mode, + FILE_ATTRIBUTE_NORMAL, NULL); + + if (file->fd == INVALID_HANDLE_VALUE) { + free(file); + return NULL; + } + + if (!GetFileSizeEx(file->fd, &size)) { + free(file); + return NULL; + } + + file->size = size.QuadPart; + base->destroy = stdio_destroy; + base->read_at = stdio_read_at; + base->write_at = stdio_write_at; + base->get_size = stdio_get_size; + base->truncate = stdio_truncate; + return base; +} -- cgit v1.2.3