aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-16 01:41:06 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-16 03:51:11 +0200
commitc81e80bf35874696d9735f70e5c8327f92b5aca4 (patch)
tree5ef99ab43bde73674391d02ccc65d357874a34c1 /lib
parent8bff2715bfdfd2e6ce0c707f1310b169d4e454d0 (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib')
-rw-r--r--lib/sqfs/Makemodule.am9
-rw-r--r--lib/sqfs/src/io/file.c (renamed from lib/sqfs/src/unix/io_file.c)168
-rw-r--r--lib/sqfs/src/io/istream.c (renamed from lib/sqfs/src/unix/istream.c)53
-rw-r--r--lib/sqfs/src/io/ostream.c (renamed from lib/sqfs/src/unix/ostream.c)82
-rw-r--r--lib/sqfs/src/io/unix.c87
-rw-r--r--lib/sqfs/src/io/win32.c120
-rw-r--r--lib/sqfs/src/win32/io_file.c271
-rw-r--r--lib/sqfs/src/win32/istream.c190
-rw-r--r--lib/sqfs/src/win32/ostream.c201
9 files changed, 412 insertions, 769 deletions
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/unix/io_file.c b/lib/sqfs/src/io/file.c
index f9b602a..881730d 100644
--- a/lib/sqfs/src/unix/io_file.c
+++ b/lib/sqfs/src/io/file.c
@@ -1,6 +1,6 @@
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
- * io_file.c
+ * file.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
@@ -11,13 +11,32 @@
#include "sqfs/error.h"
#include "compat.h"
-#include <sys/stat.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
+
+#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 <sys/stat.h>
+#include <unistd.h>
#include <errno.h>
-#include <fcntl.h>
+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;
@@ -34,7 +53,7 @@ static void stdio_destroy(sqfs_object_t *base)
{
sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
- close(file->fd);
+ sqfs_close_native_file(file->fd);
free(file);
}
@@ -43,10 +62,13 @@ 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) {
+#if defined(_WIN32) || defined(__WINDOWS__)
+ SetLastError(ERROR_NOT_SUPPORTED);
+#else
errno = ENOTSUP;
+#endif
return NULL;
}
@@ -57,17 +79,77 @@ static sqfs_object_t *stdio_copy(const sqfs_object_t *base)
memcpy(copy, file, size);
- copy->fd = dup(file->fd);
- if (copy->fd < 0) {
- err = errno;
+ if (sqfs_duplicate_native_file(file->fd, &copy->fd)) {
+ os_error_t error = get_os_error_state();
free(copy);
copy = NULL;
- errno = err;
+ 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)
{
@@ -122,6 +204,7 @@ static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset,
return 0;
}
+#endif
static sqfs_u64 stdio_get_size(const sqfs_file_t *base)
{
@@ -133,9 +216,12 @@ static sqfs_u64 stdio_get_size(const sqfs_file_t *base)
static int stdio_truncate(sqfs_file_t *base, sqfs_u64 size)
{
sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+ int ret;
- if (ftruncate(file->fd, size))
- return SQFS_ERROR_IO;
+ 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;
@@ -146,53 +232,24 @@ 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)
{
+ bool file_opened = false;
sqfs_file_stdio_t *file;
size_t size, namelen;
sqfs_file_t *base;
- struct stat sb;
- int temp;
+ os_error_t err;
namelen = strlen(filename);
size = sizeof(*file) + 1;
- if (SZ_ADD_OV(size, namelen, &size)) {
- errno = EOVERFLOW;
- return NULL;
- }
+ if (SZ_ADD_OV(size, namelen, &size))
+ goto fail_no_mem;
file = calloc(1, size);
base = (sqfs_file_t *)file;
if (file == NULL)
- return NULL;
+ goto fail_no_mem;
sqfs_object_init(file, stdio_destroy, stdio_copy);
memcpy(file->name, filename, namelen);
@@ -200,10 +257,10 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags)
if (sqfs_open_native_file(&file->fd, filename, flags))
goto fail;
- if (fstat(file->fd, &sb))
+ file_opened = true;
+ if (get_file_size(file->fd, &file->size))
goto fail;
- file->size = sb.st_size;
file->readonly = (flags & SQFS_FILE_OPEN_READ_ONLY) != 0;
base->read_at = stdio_read_at;
@@ -213,10 +270,17 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags)
base->get_filename = stdio_get_filename;
return base;
fail:
- temp = errno;
- if (file->fd >= 0)
- close(file->fd);
+ err = get_os_error_state();
+ if (file_opened)
+ sqfs_close_native_file(file->fd);
free(file);
- errno = temp;
+ 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/unix/istream.c b/lib/sqfs/src/io/istream.c
index a0a3469..c3399fe 100644
--- a/lib/sqfs/src/unix/istream.c
+++ b/lib/sqfs/src/io/istream.c
@@ -9,20 +9,40 @@
#include "sqfs/io.h"
#include "sqfs/error.h"
+#include "compat.h"
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
-#include <fcntl.h>
+
+#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;
- int fd;
+ sqfs_file_handle_t fd;
bool eof;
size_t buffer_offset;
@@ -56,8 +76,10 @@ static int precache(sqfs_istream_t *strm)
}
if (ret < 0) {
+#if !defined(_WIN32) && !defined(__WINDOWS__)
if (errno == EINTR)
continue;
+#endif
return SQFS_ERROR_IO;
}
@@ -107,7 +129,7 @@ static void file_destroy(sqfs_object_t *obj)
{
file_istream_t *file = (file_istream_t *)obj;
- close(file->fd);
+ sqfs_close_native_file(file->fd);
free(file->path);
free(file);
}
@@ -117,6 +139,7 @@ int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path,
{
file_istream_t *file;
sqfs_istream_t *strm;
+ int ret;
if (flags & ~(SQFS_FILE_OPEN_ALL_FLAGS))
return SQFS_ERROR_UNSUPPORTED;
@@ -130,21 +153,21 @@ int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path,
file->path = strdup(path);
if (file->path == NULL) {
- int temp = errno;
+ os_error_t err = get_os_error_state();
free(file);
- errno = temp;
+ set_os_error_state(err);
return SQFS_ERROR_ALLOC;
}
- file->fd = dup(fd);
- if (file->fd < 0) {
- int temp = errno;
+ ret = sqfs_duplicate_native_file(fd, &file->fd);
+ if (ret) {
+ os_error_t err = get_os_error_state();
free(file->path);
free(file);
- errno = temp;
- return SQFS_ERROR_IO;
+ set_os_error_state(err);
+ return ret;
}
- close(fd);
+ sqfs_close_native_file(fd);
strm->get_buffered_data = file_get_buffered_data;
strm->advance_buffer = file_advance_buffer;
@@ -171,9 +194,9 @@ int sqfs_istream_open_file(sqfs_istream_t **out, const char *path,
ret = sqfs_istream_open_handle(out, path, fd, flags);
if (ret != 0) {
- int temp = errno;
- close(fd);
- errno = temp;
+ 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/unix/ostream.c b/lib/sqfs/src/io/ostream.c
index efdc3cd..ba8e29a 100644
--- a/lib/sqfs/src/unix/ostream.c
+++ b/lib/sqfs/src/io/ostream.c
@@ -9,26 +9,30 @@
#include "sqfs/io.h"
#include "sqfs/error.h"
+#include "compat.h"
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
#include <errno.h>
-#include <fcntl.h>
typedef struct {
sqfs_ostream_t base;
char *path;
- int flags;
- int fd;
+ sqfs_u32 flags;
+ sqfs_file_handle_t fd;
- off_t sparse_count;
- off_t size;
+ 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) {
@@ -41,7 +45,7 @@ static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size)
continue;
return SQFS_ERROR_IO;
}
-
+#endif
file->size += ret;
size -= ret;
data += ret;
@@ -66,14 +70,14 @@ static int realize_sparse(file_ostream_t *file)
return SQFS_ERROR_ALLOC;
while (file->sparse_count > 0) {
- diff = file->sparse_count > (off_t)bufsz ?
- bufsz : (size_t)file->sparse_count;
+ diff = file->sparse_count > bufsz ?
+ bufsz : file->sparse_count;
ret = write_all(file, buffer, diff);
if (ret) {
- int temp = errno;
+ os_error_t err = get_os_error_state();
free(buffer);
- errno = temp;
+ set_os_error_state(err);
return ret;
}
@@ -82,11 +86,11 @@ static int realize_sparse(file_ostream_t *file)
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;
+ 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;
}
@@ -121,11 +125,13 @@ static int file_flush(sqfs_ostream_t *strm)
if (ret)
return ret;
- if (fsync(file->fd) != 0) {
- if (errno != EINVAL)
- return SQFS_ERROR_IO;
- }
-
+#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;
}
@@ -133,7 +139,7 @@ static void file_destroy(sqfs_object_t *obj)
{
file_ostream_t *file = (file_ostream_t *)obj;
- close(file->fd);
+ sqfs_close_native_file(file->fd);
free(file->path);
free(file);
}
@@ -146,10 +152,16 @@ static const char *file_get_filename(sqfs_ostream_t *strm)
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;
+ 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;
@@ -157,22 +169,22 @@ int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path,
file->path = strdup(path);
if (file->path == NULL) {
- int temp = errno;
+ os_error_t err = get_os_error_state();
free(file);
- errno = temp;
+ set_os_error_state(err);
return SQFS_ERROR_ALLOC;
}
- file->fd = dup(fd);
- if (file->fd < 0) {
- int temp = errno;
+ ret = sqfs_duplicate_native_file(fd, &file->fd);
+ if (ret) {
+ os_error_t err = get_os_error_state();
free(file->path);
free(file);
- errno = temp;
- return SQFS_ERROR_IO;
+ set_os_error_state(err);
+ return ret;
}
- close(fd);
+ sqfs_close_native_file(fd);
file->flags = flags;
strm->append = file_append;
@@ -199,9 +211,9 @@ int sqfs_ostream_open_file(sqfs_ostream_t **out, const char *path,
ret = sqfs_ostream_open_handle(out, path, fd, flags);
if (ret) {
- int temp = errno;
- close(fd);
- errno = temp;
+ 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/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 <goliath@infraroot.at>
+ */
+#define SQFS_BUILDING_DLL
+#include "config.h"
+
+#include "sqfs/io.h"
+#include "sqfs/error.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+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 <goliath@infraroot.at>
+ */
+#define SQFS_BUILDING_DLL
+#include "config.h"
+
+#include "sqfs/io.h"
+#include "sqfs/error.h"
+#include "compat.h"
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+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/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 <goliath@infraroot.at>
- */
-#define SQFS_BUILDING_DLL
-#include "config.h"
-
-#include "sqfs/io.h"
-#include "sqfs/error.h"
-#include "compat.h"
-
-#include <stdlib.h>
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-
-
-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(), &copy->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 <goliath@infraroot.at>
- */
-#define SQFS_BUILDING_DLL
-#include "config.h"
-
-#include "sqfs/io.h"
-#include "sqfs/error.h"
-
-#define BUFSZ (131072)
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <assert.h>
-
-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 <goliath@infraroot.at>
- */
-#define SQFS_BUILDING_DLL
-#include "config.h"
-
-#include "sqfs/io.h"
-#include "sqfs/error.h"
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-
-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;
-}