From 849e6718448793b12d7c6641d59779ca12a2ba08 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 15 Jun 2023 16:21:05 +0200 Subject: libsquashfs: cleanup the flag situation on istream/ostream functions - The ostream creation functions already have flag arguments, but make them an sqfs_u32 instead of int. - Add flag arguments to the istream functions, sanitzie and forward them when opening the handle. Signed-off-by: David Oberhollenzer --- lib/sqfs/src/unix/istream.c | 23 +++++++++++++++++------ lib/sqfs/src/unix/ostream.c | 8 ++++++-- lib/sqfs/src/win32/istream.c | 23 +++++++++++++++++------ lib/sqfs/src/win32/ostream.c | 17 +++++++++++++---- 4 files changed, 53 insertions(+), 18 deletions(-) (limited to 'lib/sqfs/src') diff --git a/lib/sqfs/src/unix/istream.c b/lib/sqfs/src/unix/istream.c index fd2f120..a0a3469 100644 --- a/lib/sqfs/src/unix/istream.c +++ b/lib/sqfs/src/unix/istream.c @@ -113,11 +113,16 @@ static void file_destroy(sqfs_object_t *obj) } int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, - sqfs_file_handle_t fd) + sqfs_file_handle_t fd, sqfs_u32 flags) { - file_istream_t *file = calloc(1, sizeof(*file)); - sqfs_istream_t *strm = (sqfs_istream_t *)file; + 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; @@ -149,16 +154,22 @@ int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, return 0; } -int sqfs_istream_open_file(sqfs_istream_t **out, const char *path) +int sqfs_istream_open_file(sqfs_istream_t **out, const char *path, + sqfs_u32 flags) { sqfs_file_handle_t fd; int ret; - ret = sqfs_open_native_file(&fd, path, SQFS_FILE_OPEN_READ_ONLY); + 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); + ret = sqfs_istream_open_handle(out, path, fd, flags); if (ret != 0) { int temp = errno; close(fd); diff --git a/lib/sqfs/src/unix/ostream.c b/lib/sqfs/src/unix/ostream.c index 89982c1..efdc3cd 100644 --- a/lib/sqfs/src/unix/ostream.c +++ b/lib/sqfs/src/unix/ostream.c @@ -144,7 +144,7 @@ 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, int flags) + sqfs_file_handle_t fd, sqfs_u32 flags) { file_ostream_t *file = calloc(1, sizeof(*file)); sqfs_ostream_t *strm = (sqfs_ostream_t *)file; @@ -183,12 +183,16 @@ int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path, return 0; } -int sqfs_ostream_open_file(sqfs_ostream_t **out, const char *path, int flags) +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; diff --git a/lib/sqfs/src/win32/istream.c b/lib/sqfs/src/win32/istream.c index ba44c0b..3f7eb83 100644 --- a/lib/sqfs/src/win32/istream.c +++ b/lib/sqfs/src/win32/istream.c @@ -118,12 +118,17 @@ static void file_destroy(sqfs_object_t *obj) } int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, - sqfs_file_handle_t hnd) + sqfs_file_handle_t hnd, sqfs_u32 flags) { - file_istream_t *file = calloc(1, sizeof(*file)); - sqfs_istream_t *strm = (sqfs_istream_t *)file; + 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; @@ -158,16 +163,22 @@ int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, return 0; } -int sqfs_istream_open_file(sqfs_istream_t **out, const char *path) +int sqfs_istream_open_file(sqfs_istream_t **out, const char *path, + sqfs_u32 flags) { sqfs_file_handle_t hnd; int ret; - ret = sqfs_open_native_file(&hnd, path, SQFS_FILE_OPEN_READ_ONLY); + 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); + ret = sqfs_istream_open_handle(out, path, hnd, flags); if (ret) { DWORD temp = GetLastError(); CloseHandle(hnd); diff --git a/lib/sqfs/src/win32/ostream.c b/lib/sqfs/src/win32/ostream.c index 3e6a8a7..3ebeb8b 100644 --- a/lib/sqfs/src/win32/ostream.c +++ b/lib/sqfs/src/win32/ostream.c @@ -131,12 +131,17 @@ 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 hnd, int flags) + sqfs_file_handle_t hnd, 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; 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; @@ -170,12 +175,16 @@ int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path, return 0; } -int sqfs_ostream_open_file(sqfs_ostream_t **out, const char *path, int flags) +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; -- cgit v1.2.3