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 --- bin/gensquashfs/src/filemap_xattr.c | 2 +- bin/gensquashfs/src/fstree_from_file.c | 2 +- bin/gensquashfs/src/mkfs.c | 2 +- include/sqfs/io.h | 39 ++++++++++++++++++---------------- lib/io/src/std.c | 2 +- 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 +++++++++++---- lib/tar/test/tar_big_file.c | 3 ++- lib/tar/test/tar_fuzz.c | 2 +- lib/tar/test/tar_iterator.c | 6 ++++-- lib/tar/test/tar_iterator2.c | 3 ++- lib/tar/test/tar_iterator3.c | 3 ++- lib/tar/test/tar_simple.c | 3 ++- lib/tar/test/tar_sparse.c | 2 +- lib/tar/test/tar_sparse_gnu.c | 2 +- lib/tar/test/tar_target_filled.c | 3 ++- lib/tar/test/tar_write_simple.c | 3 ++- lib/tar/test/tar_xattr.c | 3 ++- lib/tar/test/tar_xattr_bin.c | 3 ++- lib/util/test/str_table.c | 2 +- 22 files changed, 102 insertions(+), 54 deletions(-) diff --git a/bin/gensquashfs/src/filemap_xattr.c b/bin/gensquashfs/src/filemap_xattr.c index b9a8835..8aa2575 100644 --- a/bin/gensquashfs/src/filemap_xattr.c +++ b/bin/gensquashfs/src/filemap_xattr.c @@ -162,7 +162,7 @@ xattr_open_map_file(const char *path) { sqfs_istream_t *file = NULL; int ret; - ret = sqfs_istream_open_file(&file, path); + ret = sqfs_istream_open_file(&file, path, 0); if (ret) { sqfs_perror(path, NULL, ret); return NULL; diff --git a/bin/gensquashfs/src/fstree_from_file.c b/bin/gensquashfs/src/fstree_from_file.c index c0b4571..ce9fa77 100644 --- a/bin/gensquashfs/src/fstree_from_file.c +++ b/bin/gensquashfs/src/fstree_from_file.c @@ -313,7 +313,7 @@ int fstree_from_file(fstree_t *fs, const char *filename, const char *basepath) sqfs_istream_t *fp; int ret; - ret = sqfs_istream_open_file(&fp, filename); + ret = sqfs_istream_open_file(&fp, filename, 0); if (ret) { sqfs_perror(filename, NULL, ret); return -1; diff --git a/bin/gensquashfs/src/mkfs.c b/bin/gensquashfs/src/mkfs.c index 87ad6fc..39329a6 100644 --- a/bin/gensquashfs/src/mkfs.c +++ b/bin/gensquashfs/src/mkfs.c @@ -160,7 +160,7 @@ int main(int argc, char **argv) } if (opt.sortfile != NULL) { - int ret = sqfs_istream_open_file(&sortfile, opt.sortfile); + int ret = sqfs_istream_open_file(&sortfile, opt.sortfile, 0); if (ret) { sqfs_perror(opt.sortfile, NULL, ret); goto out; diff --git a/include/sqfs/io.h b/include/sqfs/io.h index eaf74a6..a697dc3 100644 --- a/include/sqfs/io.h +++ b/include/sqfs/io.h @@ -77,28 +77,23 @@ typedef enum { * @brief If set, do not try to apply any character set transformations * to the file path. * - * This flag instructs the @ref sqfs_open_file API to pass the file - * path to the OS dependent API as-is and not to attempt any character - * set transformation. By default, if the underlying OS has a concept - * of locale dependent file paths, the input path is UTF-8 and it is - * transformed as needed, in order to retain a level of portabillity - * and sanity. - * * This flag currently only affects the Windows implementation. On * Unix-like systems, the path is always passed to the OS API as-is * and this flag has no effect. * - * On Windows, the input path is converted from UTF-8 to UTF-16 and - * then passed on to the wide-char based file API. If this flag is set, - * the path is used as-is and passed on to the 8-bit codepage-whatever - * API instead. + * On Windows, the file paths are converted from UTF-8 to UTF-16 and + * then passed on to the wide-char API. If this flag is set, the path + * is used as-is and passed on to the 8-bit ANSI API instead, letting + * the OS decide how to convert and interpret the path. */ SQFS_FILE_OPEN_NO_CHARSET_XFRM = 0x04, /** * @brief Do not use sparse file I/O APIs, always fill in zero bytes * - * This flag currently has no effect on @ref sqfs_open_file. + * This flag currently has no effect on @ref sqfs_open_file, it changes + * the behavior of @ref sqfs_ostream_open_file and + * @ref sqfs_ostream_open_handle. */ SQFS_FILE_OPEN_NO_SPARSE = 0x08, @@ -341,12 +336,13 @@ SQFS_API sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags); * @param out Returns a pointer to an input stream on success. * @param path The name to associate with the handle. * @param fd A native file handle. + * @param flags A combination of @ref SQFS_FILE_OPEN_FLAGS flags. * * @return Zero on success, a negative @ref SQFS_ERROR number on failure */ SQFS_API int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, - sqfs_file_handle_t fd); + sqfs_file_handle_t fd, sqfs_u32 flags); /** * @brief Create an output stream that writes to an OS native file handle. @@ -361,24 +357,31 @@ int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, * @param out Returns a pointer to an output stream on success. * @param path The name to associate with the handle. * @param fd A native file handle. - * @param flags A combination of flags. + * @param flags A combination of @ref SQFS_FILE_OPEN_FLAGS flags. * * @return Zero on success, a negative @ref SQFS_ERROR number on failure */ SQFS_API int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path, - sqfs_file_handle_t hnd, int flags); + sqfs_file_handle_t hnd, sqfs_u32 flags); /** * @brief Create an input stream that reads from a file. * * @memberof sqfs_istream_t * + * The flag @ref SQFS_FILE_OPEN_READ_ONLY is implicitly assumed to be set, + * since the function constructs a read-only primitive. If either the flags + * @ref SQFS_FILE_OPEN_OVERWRITE or @ref SQFS_FILE_OPEN_NO_SPARSE are set, + * an error is returend. + * * @param out Returns a pointer to an input stream on success. * @param path A path to the file to open or create. + * @param flags A combination of @ref SQFS_FILE_OPEN_FLAGS flags. * * @return Zero on success, a negative @ref SQFS_ERROR number on failure */ -SQFS_API int sqfs_istream_open_file(sqfs_istream_t **out, const char *path); +SQFS_API int sqfs_istream_open_file(sqfs_istream_t **out, const char *path, + sqfs_u32 flags); /** * @brief Create an output stream that writes to a file. @@ -396,12 +399,12 @@ SQFS_API int sqfs_istream_open_file(sqfs_istream_t **out, const char *path); * * @param out Returns a pointer to an output stream on success. * @param path A path to the file to open or create. - * @param flags A combination of flags controling how to open/create the file. + * @param flags A combination of @ref SQFS_FILE_OPEN_FLAGS flags. * * @return Zero on success, a negative @ref SQFS_ERROR number on failure */ SQFS_API int sqfs_ostream_open_file(sqfs_ostream_t **out, - const char *path, int flags); + const char *path, sqfs_u32 flags); /** * @brief Read data from an input stream diff --git a/lib/io/src/std.c b/lib/io/src/std.c index c52b4c5..62635ec 100644 --- a/lib/io/src/std.c +++ b/lib/io/src/std.c @@ -23,7 +23,7 @@ int istream_open_stdin(sqfs_istream_t **out) { sqfs_file_handle_t hnd = GetStdHandle(STD_INPUT_HANDLE); - return sqfs_istream_open_handle(out, "stdin", hnd); + return sqfs_istream_open_handle(out, "stdin", hnd, 0); } int ostream_open_stdout(sqfs_ostream_t **out) 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; diff --git a/lib/tar/test/tar_big_file.c b/lib/tar/test/tar_big_file.c index 2c8a176..654b6ff 100644 --- a/lib/tar/test/tar_big_file.c +++ b/lib/tar/test/tar_big_file.c @@ -17,7 +17,8 @@ int main(int argc, char **argv) (void)argc; (void)argv; ret = sqfs_istream_open_file(&fp, - STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE), + 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_fuzz.c b/lib/tar/test/tar_fuzz.c index 1c4b52e..c09fd2f 100644 --- a/lib/tar/test/tar_fuzz.c +++ b/lib/tar/test/tar_fuzz.c @@ -24,7 +24,7 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - ret = sqfs_istream_open_file(&fp, argv[1]); + ret = sqfs_istream_open_file(&fp, argv[1], 0); if (ret) { sqfs_perror("stdint", NULL, ret); return EXIT_FAILURE; diff --git a/lib/tar/test/tar_iterator.c b/lib/tar/test/tar_iterator.c index d4c2a35..e59b210 100644 --- a/lib/tar/test/tar_iterator.c +++ b/lib/tar/test/tar_iterator.c @@ -41,7 +41,8 @@ int main(int argc, char **argv) /* Open the file, create an iterator */ iret = sqfs_istream_open_file(&fp, - STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE), + 0); TEST_EQUAL_I(iret, 0); TEST_NOT_NULL(fp); TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1); @@ -110,7 +111,8 @@ int main(int argc, char **argv) /* re-open the tar iterator */ iret = sqfs_istream_open_file(&fp, - STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE), + 0); TEST_EQUAL_I(iret, 0); TEST_NOT_NULL(fp); it = tar_open_stream(fp); diff --git a/lib/tar/test/tar_iterator2.c b/lib/tar/test/tar_iterator2.c index 8bbaf62..ac041fe 100644 --- a/lib/tar/test/tar_iterator2.c +++ b/lib/tar/test/tar_iterator2.c @@ -53,7 +53,8 @@ int main(int argc, char **argv) (void)argc; (void)argv; iret = sqfs_istream_open_file(&fp, - STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE), + 0); TEST_EQUAL_I(iret, 0); TEST_NOT_NULL(fp); it = tar_open_stream(fp); diff --git a/lib/tar/test/tar_iterator3.c b/lib/tar/test/tar_iterator3.c index 9fd5f59..214fe9c 100644 --- a/lib/tar/test/tar_iterator3.c +++ b/lib/tar/test/tar_iterator3.c @@ -23,7 +23,8 @@ int main(int argc, char **argv) TEST_ASSERT(chdir(TEST_PATH) == 0); - ret = sqfs_istream_open_file(&fp, "format-acceptance/link_filled.tar"); + ret = sqfs_istream_open_file(&fp, "format-acceptance/link_filled.tar", + 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_EQUAL_UI(((sqfs_object_t *)fp)->refcount, 1); diff --git a/lib/tar/test/tar_simple.c b/lib/tar/test/tar_simple.c index ad9fdde..21ab109 100644 --- a/lib/tar/test/tar_simple.c +++ b/lib/tar/test/tar_simple.c @@ -44,7 +44,8 @@ int main(int argc, char **argv) (void)argc; (void)argv; ret = sqfs_istream_open_file(&fp, - STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE), + 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_sparse.c b/lib/tar/test/tar_sparse.c index 49b2064..702db58 100644 --- a/lib/tar/test/tar_sparse.c +++ b/lib/tar/test/tar_sparse.c @@ -16,7 +16,7 @@ static void test_case_sparse(const char *path) sqfs_istream_t *fp; int ret; - ret = sqfs_istream_open_file(&fp, path); + ret = sqfs_istream_open_file(&fp, path, 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_sparse_gnu.c b/lib/tar/test/tar_sparse_gnu.c index a2f8332..1f928bd 100644 --- a/lib/tar/test/tar_sparse_gnu.c +++ b/lib/tar/test/tar_sparse_gnu.c @@ -19,7 +19,7 @@ int main(int argc, char **argv) TEST_ASSERT(chdir(TEST_PATH) == 0); - ret = sqfs_istream_open_file(&fp, "sparse-files/gnu-small.tar"); + ret = sqfs_istream_open_file(&fp, "sparse-files/gnu-small.tar", 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_target_filled.c b/lib/tar/test/tar_target_filled.c index 93c3b02..1279edf 100644 --- a/lib/tar/test/tar_target_filled.c +++ b/lib/tar/test/tar_target_filled.c @@ -19,7 +19,8 @@ int main(int argc, char **argv) TEST_ASSERT(chdir(TEST_PATH) == 0); - ret = sqfs_istream_open_file(&fp, "format-acceptance/link_filled.tar"); + ret = sqfs_istream_open_file(&fp, "format-acceptance/link_filled.tar", + 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); diff --git a/lib/tar/test/tar_write_simple.c b/lib/tar/test/tar_write_simple.c index bc1b93e..438ea37 100644 --- a/lib/tar/test/tar_write_simple.c +++ b/lib/tar/test/tar_write_simple.c @@ -188,7 +188,8 @@ int main(int argc, char **argv) TEST_EQUAL_UI(sizeof(rd_buffer), sizeof(wr_buffer)); ret = sqfs_istream_open_file(&fp, - STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE), + 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); diff --git a/lib/tar/test/tar_xattr.c b/lib/tar/test/tar_xattr.c index d074e31..ca15c66 100644 --- a/lib/tar/test/tar_xattr.c +++ b/lib/tar/test/tar_xattr.c @@ -19,7 +19,8 @@ int main(int argc, char **argv) (void)argc; (void)argv; ret = sqfs_istream_open_file(&fp, - STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE), + 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/tar/test/tar_xattr_bin.c b/lib/tar/test/tar_xattr_bin.c index 68f23fe..901b657 100644 --- a/lib/tar/test/tar_xattr_bin.c +++ b/lib/tar/test/tar_xattr_bin.c @@ -27,7 +27,8 @@ int main(int argc, char **argv) (void)argc; (void)argv; ret = sqfs_istream_open_file(&fp, - STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE)); + STRVALUE(TESTPATH) "/" STRVALUE(TESTFILE), + 0); TEST_EQUAL_I(ret, 0); TEST_NOT_NULL(fp); TEST_ASSERT(read_header(fp, &hdr) == 0); diff --git a/lib/util/test/str_table.c b/lib/util/test/str_table.c index 02e5e67..b146773 100644 --- a/lib/util/test/str_table.c +++ b/lib/util/test/str_table.c @@ -21,7 +21,7 @@ static int read_strings(void) char *line; int i; - i = sqfs_istream_open_file(&fp, "words.txt"); + i = sqfs_istream_open_file(&fp, "words.txt", 0); TEST_EQUAL_I(i, 0); TEST_NOT_NULL(fp); -- cgit v1.2.3