diff options
Diffstat (limited to 'lib/io/src')
-rw-r--r-- | lib/io/src/ostream.c | 35 | ||||
-rw-r--r-- | lib/io/src/unix/ostream.c | 102 | ||||
-rw-r--r-- | lib/io/src/win32/ostream.c | 74 | ||||
-rw-r--r-- | lib/io/src/xfrm/ostream.c | 8 |
4 files changed, 135 insertions, 84 deletions
diff --git a/lib/io/src/ostream.c b/lib/io/src/ostream.c deleted file mode 100644 index 35f98d4..0000000 --- a/lib/io/src/ostream.c +++ /dev/null @@ -1,35 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * ostream.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "internal.h" - - -static int append_sparse_fallback(ostream_t *strm, size_t size) -{ - char buffer[512]; - size_t diff; - - memset(buffer, 0, sizeof(buffer)); - - while (size > 0) { - diff = size < sizeof(buffer) ? size : sizeof(buffer); - - if (strm->append(strm, buffer, diff)) - return -1; - - size -= diff; - } - - return 0; -} - -int ostream_append_sparse(ostream_t *strm, size_t size) -{ - if (strm->append_sparse == NULL) - return append_sparse_fallback(strm, size); - - return strm->append_sparse(strm, size); -} diff --git a/lib/io/src/unix/ostream.c b/lib/io/src/unix/ostream.c index 294a15e..702a354 100644 --- a/lib/io/src/unix/ostream.c +++ b/lib/io/src/unix/ostream.c @@ -9,81 +9,115 @@ typedef struct { ostream_t base; char *path; + int flags; int fd; off_t sparse_count; off_t size; } file_ostream_t; -static int file_append(ostream_t *strm, const void *data, size_t size) +static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size) { - file_ostream_t *file = (file_ostream_t *)strm; - ssize_t ret; - - if (size == 0) - return 0; - - if (file->sparse_count > 0) { - if (lseek(file->fd, file->sparse_count, SEEK_CUR) == (off_t)-1) - goto fail_errno; - - file->sparse_count = 0; - } - while (size > 0) { - ret = write(file->fd, data, size); + ssize_t ret = write(file->fd, data, size); if (ret == 0) { - fprintf(stderr, "%s: truncated data write.\n", - file->path); + fprintf(stderr, "%s: truncated write.\n", file->path); return -1; } if (ret < 0) { if (errno == EINTR) continue; - goto fail_errno; + perror(file->path); + return -1; } file->size += ret; size -= ret; - data = (const char *)data + ret; + data += ret; + } + + return 0; +} + +static int realize_sparse(file_ostream_t *file) +{ + unsigned char *buffer; + size_t diff, bufsz; + + 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 { + bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count; + buffer = calloc(1, bufsz); + if (buffer == NULL) + goto fail; + + while (file->sparse_count > 0) { + diff = file->sparse_count > (off_t)bufsz ? + bufsz : (size_t)file->sparse_count; + + if (write_all(file, buffer, diff)) { + free(buffer); + return -1; + } + + file->sparse_count -= diff; + } + + free(buffer); } return 0; -fail_errno: +fail: perror(file->path); return -1; } -static int file_append_sparse(ostream_t *strm, size_t size) +static int file_append(ostream_t *strm, const void *data, size_t size) { file_ostream_t *file = (file_ostream_t *)strm; - file->sparse_count += size; - file->size += size; - return 0; + if (size == 0) + return 0; + + if (data == NULL) { + file->sparse_count += size; + file->size += size; + return 0; + } + + if (realize_sparse(file)) + return -1; + + return write_all(file, data, size); } static int file_flush(ostream_t *strm) { file_ostream_t *file = (file_ostream_t *)strm; - if (file->sparse_count > 0) { - if (ftruncate(file->fd, file->size) != 0) - goto fail; - } + if (realize_sparse(file)) + return -1; if (fsync(file->fd) != 0) { if (errno == EINVAL) return 0; - goto fail; + perror(file->path); + return -1; } return 0; -fail: - perror(file->path); - return -1; } static void file_destroy(sqfs_object_t *obj) @@ -128,9 +162,7 @@ ostream_t *ostream_open_handle(const char *path, int fd, int flags) close(fd); - if (flags & OSTREAM_OPEN_SPARSE) - strm->append_sparse = file_append_sparse; - + file->flags = flags; strm->append = file_append; strm->flush = file_flush; strm->get_filename = file_get_filename; diff --git a/lib/io/src/win32/ostream.c b/lib/io/src/win32/ostream.c index d18130f..9b488d8 100644 --- a/lib/io/src/win32/ostream.c +++ b/lib/io/src/win32/ostream.c @@ -11,13 +11,14 @@ typedef struct { ostream_t base; + sqfs_u64 sparse_count; char *path; HANDLE hnd; + int flags; } file_ostream_t; -static int file_append(ostream_t *strm, const void *data, size_t size) +static int write_data(file_ostream_t *file, const void *data, size_t size) { - file_ostream_t *file = (file_ostream_t *)strm; DWORD diff; while (size > 0) { @@ -33,18 +34,48 @@ static int file_append(ostream_t *strm, const void *data, size_t size) return 0; } -static int file_append_sparse(ostream_t *strm, size_t size) +static int realize_sparse(file_ostream_t *file) { - file_ostream_t *file = (file_ostream_t *)strm; + size_t bufsz, diff; LARGE_INTEGER pos; + void *buffer; + + if (file->sparse_count == 0) + return 0; - pos.QuadPart = size; + if (file->flags & OSTREAM_OPEN_SPARSE) { + pos.QuadPart = file->sparse_count; - if (!SetFilePointerEx(file->hnd, pos, NULL, FILE_CURRENT)) - goto fail; + if (!SetFilePointerEx(file->hnd, pos, NULL, FILE_CURRENT)) + goto fail; - if (!SetEndOfFile(file->hnd)) - goto fail; + if (!SetEndOfFile(file->hnd)) + goto fail; + + file->sparse_count = 0; + } else { + bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count; + buffer = calloc(1, bufsz); + + if (buffer == NULL) { + fputs("out-of-memory\n", stderr); + return -1; + } + + while (file->sparse_count > 0) { + diff = file->sparse_count > bufsz ? + bufsz : file->sparse_count; + + if (write_data(file, buffer, diff)) { + free(buffer); + return -1; + } + + file->sparse_count -= diff; + } + + free(buffer); + } return 0; fail: @@ -52,10 +83,31 @@ fail: return -1; } +static int file_append(ostream_t *strm, const void *data, size_t size) +{ + file_ostream_t *file = (file_ostream_t *)strm; + + if (size == 0) + return 0; + + if (data == NULL) { + file->sparse_count += size; + return 0; + } + + if (realize_sparse(file)) + return -1; + + return write_data(file, data, size); +} + static int file_flush(ostream_t *strm) { file_ostream_t *file = (file_ostream_t *)strm; + if (realize_sparse(file)) + return -1; + if (!FlushFileBuffers(file->hnd)) { w32_perror(file->path); return -1; @@ -109,9 +161,7 @@ ostream_t *ostream_open_handle(const char *path, HANDLE hnd, int flags) CloseHandle(hnd); - if (flags & OSTREAM_OPEN_SPARSE) - strm->append_sparse = file_append_sparse; - + file->flags = flags; strm->append = file_append; strm->flush = file_flush; strm->get_filename = file_get_filename; diff --git a/lib/io/src/xfrm/ostream.c b/lib/io/src/xfrm/ostream.c index 79ed49a..e55e38c 100644 --- a/lib/io/src/xfrm/ostream.c +++ b/lib/io/src/xfrm/ostream.c @@ -82,10 +82,14 @@ static int xfrm_append(ostream_t *strm, const void *data, size_t size) if (diff > size) diff = size; - memcpy(xfrm->inbuf + xfrm->inbuf_used, data, diff); + if (data == NULL) { + memset(xfrm->inbuf + xfrm->inbuf_used, 0, diff); + } else { + memcpy(xfrm->inbuf + xfrm->inbuf_used, data, diff); + data = (const char *)data + diff; + } xfrm->inbuf_used += diff; - data = (const char *)data + diff; size -= diff; } |