From 484cd01590ec488cca1f8a5c7c76cd223609e299 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Thu, 15 Jun 2023 15:16:03 +0200 Subject: Migrate file istream/ostream from libio to libsquashfs Signed-off-by: David Oberhollenzer --- lib/sqfs/src/unix/istream.c | 169 ++++++++++++++++++++++++++++++++++++ lib/sqfs/src/unix/ostream.c | 205 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 374 insertions(+) create mode 100644 lib/sqfs/src/unix/istream.c create mode 100644 lib/sqfs/src/unix/ostream.c (limited to 'lib/sqfs/src/unix') diff --git a/lib/sqfs/src/unix/istream.c b/lib/sqfs/src/unix/istream.c new file mode 100644 index 0000000..fd2f120 --- /dev/null +++ b/lib/sqfs/src/unix/istream.c @@ -0,0 +1,169 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * istream.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" + +#include +#include +#include +#include +#include +#include + +#define BUFSZ (131072) + +typedef struct { + sqfs_istream_t base; + char *path; + int fd; + + 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; + + 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 < BUFSZ) { + ssize_t ret = read(file->fd, file->buffer + file->buffer_used, + BUFSZ - file->buffer_used); + + if (ret == 0) { + file->eof = true; + break; + } + + if (ret < 0) { + if (errno == EINTR) + continue; + return SQFS_ERROR_IO; + } + + file->buffer_used += ret; + } + + 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; + + close(file->fd); + free(file->path); + free(file); +} + +int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path, + sqfs_file_handle_t fd) +{ + file_istream_t *file = calloc(1, sizeof(*file)); + sqfs_istream_t *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) { + int temp = errno; + free(file); + errno = temp; + return SQFS_ERROR_ALLOC; + } + + file->fd = dup(fd); + if (file->fd < 0) { + int temp = errno; + free(file->path); + free(file); + errno = temp; + return SQFS_ERROR_IO; + } + close(fd); + + 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_file_handle_t fd; + int ret; + + ret = sqfs_open_native_file(&fd, path, SQFS_FILE_OPEN_READ_ONLY); + if (ret) + return ret; + + ret = sqfs_istream_open_handle(out, path, fd); + if (ret != 0) { + int temp = errno; + close(fd); + errno = temp; + } + + return ret; +} diff --git a/lib/sqfs/src/unix/ostream.c b/lib/sqfs/src/unix/ostream.c new file mode 100644 index 0000000..89982c1 --- /dev/null +++ b/lib/sqfs/src/unix/ostream.c @@ -0,0 +1,205 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * ostream.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#define SQFS_BUILDING_DLL +#include "config.h" + +#include "sqfs/io.h" +#include "sqfs/error.h" + +#include +#include +#include +#include +#include + +typedef struct { + sqfs_ostream_t base; + char *path; + int flags; + int fd; + + off_t sparse_count; + off_t size; +} file_ostream_t; + +static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size) +{ + while (size > 0) { + ssize_t ret = write(file->fd, data, size); + + if (ret == 0) { + errno = EPIPE; + return SQFS_ERROR_IO; + } + + if (ret < 0) { + if (errno == EINTR) + continue; + return SQFS_ERROR_IO; + } + + file->size += ret; + size -= ret; + data += ret; + } + + return 0; +} + +static int realize_sparse(file_ostream_t *file) +{ + unsigned char *buffer; + size_t diff, bufsz; + 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 > (off_t)bufsz ? + bufsz : (size_t)file->sparse_count; + + ret = write_all(file, buffer, diff); + if (ret) { + int temp = errno; + free(buffer); + errno = temp; + return ret; + } + + file->sparse_count -= diff; + } + + 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; + + 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; + file->size += size; + return 0; + } + + ret = realize_sparse(file); + if (ret) + return ret; + + return write_all(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 (fsync(file->fd) != 0) { + if (errno != EINVAL) + return SQFS_ERROR_IO; + } + + return 0; +} + +static void file_destroy(sqfs_object_t *obj) +{ + file_ostream_t *file = (file_ostream_t *)obj; + + close(file->fd); + 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 fd, int flags) +{ + file_ostream_t *file = calloc(1, sizeof(*file)); + sqfs_ostream_t *strm = (sqfs_ostream_t *)file; + + *out = NULL; + if (file == NULL) + return SQFS_ERROR_ALLOC; + + sqfs_object_init(file, file_destroy, NULL); + + file->path = strdup(path); + if (file->path == NULL) { + int temp = errno; + free(file); + errno = temp; + return SQFS_ERROR_ALLOC; + } + + file->fd = dup(fd); + if (file->fd < 0) { + int temp = errno; + free(file->path); + free(file); + errno = temp; + return SQFS_ERROR_IO; + } + + close(fd); + + 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, int flags) +{ + sqfs_file_handle_t fd; + int ret; + + *out = NULL; + ret = sqfs_open_native_file(&fd, path, flags); + if (ret) + return ret; + + ret = sqfs_ostream_open_handle(out, path, fd, flags); + if (ret) { + int temp = errno; + close(fd); + errno = temp; + return ret; + } + + return 0; +} -- cgit v1.2.3