From 1f506a17903f5eeaded9065e42726c1a09dc6f89 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 12 Jun 2023 19:11:35 +0200 Subject: libio: flip the meaning of the ostream_t sparse flag Instead of "open sparse", make that the default and turn it into a "no sparse" flag. Signed-off-by: David Oberhollenzer --- bin/rdsquashfs/src/fill_files.c | 2 +- bin/sqfsdiff/src/extract.c | 3 +-- include/io/file.h | 16 +++++++++------- lib/io/src/unix/ostream.c | 21 +++++++++++---------- lib/io/src/win32/ostream.c | 24 ++++++++++++------------ 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/bin/rdsquashfs/src/fill_files.c b/bin/rdsquashfs/src/fill_files.c index afc790e..372752a 100644 --- a/bin/rdsquashfs/src/fill_files.c +++ b/bin/rdsquashfs/src/fill_files.c @@ -142,7 +142,7 @@ static int fill_files(sqfs_data_reader_t *data, int flags) openflags = OSTREAM_OPEN_OVERWRITE; if (flags & UNPACK_NO_SPARSE) - openflags |= OSTREAM_OPEN_SPARSE; + openflags |= OSTREAM_OPEN_NO_SPARSE; for (i = 0; i < num_files; ++i) { fp = ostream_open_file(files[i].path, openflags); diff --git a/bin/sqfsdiff/src/extract.c b/bin/sqfsdiff/src/extract.c index 7e745cf..a956480 100644 --- a/bin/sqfsdiff/src/extract.c +++ b/bin/sqfsdiff/src/extract.c @@ -21,8 +21,7 @@ static int extract(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode, return -1; *ptr = '/'; - fp = ostream_open_file(temp, OSTREAM_OPEN_OVERWRITE | - OSTREAM_OPEN_SPARSE); + fp = ostream_open_file(temp, OSTREAM_OPEN_OVERWRITE); if (fp == NULL) { perror(temp); return -1; diff --git a/include/io/file.h b/include/io/file.h index 44fd224..297047f 100644 --- a/include/io/file.h +++ b/include/io/file.h @@ -20,7 +20,7 @@ typedef int os_file_t; enum { OSTREAM_OPEN_OVERWRITE = 0x01, - OSTREAM_OPEN_SPARSE = 0x02, + OSTREAM_OPEN_NO_SPARSE = 0x02, }; #ifdef __cplusplus @@ -48,9 +48,10 @@ SQFS_INTERNAL istream_t *istream_open_handle(const char *path, os_file_t fd); * * @memberof ostream_t * - * If the flag OSTREAM_OPEN_SPARSE is set, the underlying implementation tries - * to use seek/truncate style API to create sparse output files. If the flag - * is not set, holes will always be filled with zero bytes. + * If the flag OSTREAM_OPEN_NO_SPARSE is set, the underlying implementation + * always writes chunks of zero bytes when passing a NULL pointer to append. + * Otherwise, it tries to use seek/truncate style APIs to create sparse output + * files. * * @param path The name to associate with the handle. * @param fd A native file handle. @@ -81,9 +82,10 @@ SQFS_INTERNAL istream_t *istream_open_file(const char *path); * function fails, unless the flag OSTREAM_OPEN_OVERWRITE is set, in which * case the file is opened and its contents are discarded. * - * If the flag OSTREAM_OPEN_SPARSE is set, the underlying implementation tries - * to support sparse output files. If the flag is not set, holes will always - * be filled with zero bytes. + * If the flag OSTREAM_OPEN_NO_SPARSE is set, the underlying implementation + * always writes chunks of zero bytes when passing a NULL pointer to append. + * Otherwise, it tries to use seek/truncate style APIs to create sparse output + * files. * * @param path A path to the file to open or create. * @param flags A combination of flags controling how to open/create the file. diff --git a/lib/io/src/unix/ostream.c b/lib/io/src/unix/ostream.c index 702a354..bc69f32 100644 --- a/lib/io/src/unix/ostream.c +++ b/lib/io/src/unix/ostream.c @@ -49,15 +49,7 @@ static int realize_sparse(file_ostream_t *file) if (file->sparse_count == 0) return 0; - if (file->flags & OSTREAM_OPEN_SPARSE) { - if (lseek(file->fd, file->sparse_count, SEEK_CUR) == (off_t)-1) - goto fail; - - if (ftruncate(file->fd, file->size) != 0) - goto fail; - - file->sparse_count = 0; - } else { + if (file->flags & OSTREAM_OPEN_NO_SPARSE) { bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count; buffer = calloc(1, bufsz); if (buffer == NULL) @@ -76,6 +68,14 @@ static int realize_sparse(file_ostream_t *file) } free(buffer); + } else { + if (lseek(file->fd, file->sparse_count, SEEK_CUR) == (off_t)-1) + goto fail; + + if (ftruncate(file->fd, file->size) != 0) + goto fail; + + file->sparse_count = 0; } return 0; @@ -194,5 +194,6 @@ ostream_t *ostream_open_file(const char *path, int flags) ostream_t *ostream_open_stdout(void) { - return ostream_open_handle("stdout", STDOUT_FILENO, 0); + return ostream_open_handle("stdout", STDOUT_FILENO, + OSTREAM_OPEN_NO_SPARSE); } diff --git a/lib/io/src/win32/ostream.c b/lib/io/src/win32/ostream.c index 9b488d8..edbefc7 100644 --- a/lib/io/src/win32/ostream.c +++ b/lib/io/src/win32/ostream.c @@ -43,17 +43,7 @@ static int realize_sparse(file_ostream_t *file) if (file->sparse_count == 0) return 0; - if (file->flags & OSTREAM_OPEN_SPARSE) { - pos.QuadPart = file->sparse_count; - - if (!SetFilePointerEx(file->hnd, pos, NULL, FILE_CURRENT)) - goto fail; - - if (!SetEndOfFile(file->hnd)) - goto fail; - - file->sparse_count = 0; - } else { + if (file->flags & OSTREAM_OPEN_NO_SPARSE) { bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count; buffer = calloc(1, bufsz); @@ -75,6 +65,16 @@ static int realize_sparse(file_ostream_t *file) } free(buffer); + } else { + pos.QuadPart = file->sparse_count; + + if (!SetFilePointerEx(file->hnd, pos, NULL, FILE_CURRENT)) + goto fail; + + if (!SetEndOfFile(file->hnd)) + goto fail; + + file->sparse_count = 0; } return 0; @@ -213,5 +213,5 @@ ostream_t *ostream_open_stdout(void) { HANDLE hnd = GetStdHandle(STD_OUTPUT_HANDLE); - return ostream_open_handle("stdout", hnd, 0); + return ostream_open_handle("stdout", hnd, OSTREAM_OPEN_NO_SPARSE); } -- cgit v1.2.3