aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/src/unix/istream.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/istream.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/istream.c')
-rw-r--r--lib/sqfs/src/unix/istream.c180
1 files changed, 0 insertions, 180 deletions
diff --git a/lib/sqfs/src/unix/istream.c b/lib/sqfs/src/unix/istream.c
deleted file mode 100644
index a0a3469..0000000
--- a/lib/sqfs/src/unix/istream.c
+++ /dev/null
@@ -1,180 +0,0 @@
-/* SPDX-License-Identifier: LGPL-3.0-or-later */
-/*
- * istream.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 <assert.h>
-#include <errno.h>
-#include <fcntl.h>
-
-#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, sqfs_u32 flags)
-{
- 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;
-
- 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_u32 flags)
-{
- sqfs_file_handle_t fd;
- int ret;
-
- 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, flags);
- if (ret != 0) {
- int temp = errno;
- close(fd);
- errno = temp;
- }
-
- return ret;
-}