aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/src/unix/ostream.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-16 01:41:06 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-16 03:51:11 +0200
commitc81e80bf35874696d9735f70e5c8327f92b5aca4 (patch)
tree5ef99ab43bde73674391d02ccc65d357874a34c1 /lib/sqfs/src/unix/ostream.c
parent8bff2715bfdfd2e6ce0c707f1310b169d4e454d0 (diff)
libsquashfs: merge windows & unix file I/O primitives
By adding additional close/duplicate primitives, the remaining, mostly identical istream/ostream/file code is mostly identical between Windows and Unix and be merged, mostly without stitches. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/src/unix/ostream.c')
-rw-r--r--lib/sqfs/src/unix/ostream.c209
1 files changed, 0 insertions, 209 deletions
diff --git a/lib/sqfs/src/unix/ostream.c b/lib/sqfs/src/unix/ostream.c
deleted file mode 100644
index efdc3cd..0000000
--- a/lib/sqfs/src/unix/ostream.c
+++ /dev/null
@@ -1,209 +0,0 @@
-/* SPDX-License-Identifier: LGPL-3.0-or-later */
-/*
- * ostream.c
- *
- * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
- */
-#define SQFS_BUILDING_DLL
-#include "config.h"
-
-#include "sqfs/io.h"
-#include "sqfs/error.h"
-
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-#include <fcntl.h>
-
-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, sqfs_u32 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,
- 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;
-
- ret = sqfs_ostream_open_handle(out, path, fd, flags);
- if (ret) {
- int temp = errno;
- close(fd);
- errno = temp;
- return ret;
- }
-
- return 0;
-}