diff options
| author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-15 15:16:03 +0200 | 
|---|---|---|
| committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-15 15:16:03 +0200 | 
| commit | 484cd01590ec488cca1f8a5c7c76cd223609e299 (patch) | |
| tree | 0e3bc268688c2feb8773871a43381a998ce846e7 /lib/io | |
| parent | 8cf84b4995552bad4e77eb795e36c0b1d65a720a (diff) | |
Migrate file istream/ostream from libio to libsquashfs
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/io')
| -rw-r--r-- | lib/io/Makemodule.am | 4 | ||||
| -rw-r--r-- | lib/io/src/internal.h | 1 | ||||
| -rw-r--r-- | lib/io/src/std.c | 7 | ||||
| -rw-r--r-- | lib/io/src/unix/istream.c | 155 | ||||
| -rw-r--r-- | lib/io/src/unix/ostream.c | 197 | ||||
| -rw-r--r-- | lib/io/src/win32/istream.c | 173 | ||||
| -rw-r--r-- | lib/io/src/win32/ostream.c | 190 | 
7 files changed, 4 insertions, 723 deletions
diff --git a/lib/io/Makemodule.am b/lib/io/Makemodule.am index e128eef..4f2c180 100644 --- a/lib/io/Makemodule.am +++ b/lib/io/Makemodule.am @@ -1,5 +1,5 @@  libio_a_SOURCES = include/io/istream.h include/io/xfrm.h \ -	include/io/file.h include/io/std.h include/io/dir_entry.h \ +	include/io/std.h include/io/dir_entry.h \  	include/io/dir_iterator.h include/io/mem.h lib/io/src/internal.h \  	lib/io/src/get_line.c lib/io/src/xfrm/ostream.c \  	lib/io/src/xfrm/istream.c lib/io/src/dir_tree_iterator.c \ @@ -8,11 +8,9 @@ libio_a_CFLAGS = $(AM_CFLAGS) $(ZLIB_CFLAGS) $(XZ_CFLAGS)  libio_a_CFLAGS += $(ZSTD_CFLAGS) $(BZIP2_CFLAGS)  if WINDOWS -libio_a_SOURCES += lib/io/src/win32/ostream.c lib/io/src/win32/istream.c  libio_a_SOURCES += lib/io/src/win32/dir_iterator.c  libio_a_CFLAGS += -DWINVER=0x0600 -D_WIN32_WINNT=0x0600  else -libio_a_SOURCES += lib/io/src/unix/ostream.c lib/io/src/unix/istream.c  libio_a_SOURCES += lib/io/src/unix/dir_iterator.c  endif diff --git a/lib/io/src/internal.h b/lib/io/src/internal.h index e010727..a618ed3 100644 --- a/lib/io/src/internal.h +++ b/lib/io/src/internal.h @@ -10,7 +10,6 @@  #include "config.h"  #include "compat.h"  #include "io/istream.h" -#include "io/file.h"  #include "io/xfrm.h"  #include "io/std.h"  #include "xfrm/compress.h" diff --git a/lib/io/src/std.c b/lib/io/src/std.c index 212fa6f..c52b4c5 100644 --- a/lib/io/src/std.c +++ b/lib/io/src/std.c @@ -6,7 +6,6 @@   */  #include "config.h"  #include "compat.h" -#include "io/file.h"  #include "io/std.h"  #include "sqfs/io.h" @@ -24,13 +23,13 @@ int istream_open_stdin(sqfs_istream_t **out)  {  	sqfs_file_handle_t hnd = GetStdHandle(STD_INPUT_HANDLE); -	return istream_open_handle(out, "stdin", hnd); +	return sqfs_istream_open_handle(out, "stdin", hnd);  }  int ostream_open_stdout(sqfs_ostream_t **out)  {  	sqfs_file_handle_t hnd = GetStdHandle(STD_OUTPUT_HANDLE); -	return ostream_open_handle(out, "stdout", hnd, -				   SQFS_FILE_OPEN_NO_SPARSE); +	return sqfs_ostream_open_handle(out, "stdout", hnd, +					SQFS_FILE_OPEN_NO_SPARSE);  } diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c deleted file mode 100644 index 55b467e..0000000 --- a/lib/io/src/unix/istream.c +++ /dev/null @@ -1,155 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "../internal.h" -#include "sqfs/io.h" -#include "sqfs/error.h" - -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 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 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 = istream_open_handle(out, path, fd); -	if (ret != 0) -		close(fd); - -	return ret; -} diff --git a/lib/io/src/unix/ostream.c b/lib/io/src/unix/ostream.c deleted file mode 100644 index 57467f5..0000000 --- a/lib/io/src/unix/ostream.c +++ /dev/null @@ -1,197 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * ostream.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "../internal.h" -#include "sqfs/io.h" -#include "sqfs/error.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 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 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 = ostream_open_handle(out, path, fd, flags); -	if (ret) { -		int temp = errno; -		close(fd); -		errno = temp; -		return ret; -	} - -	return 0; -} diff --git a/lib/io/src/win32/istream.c b/lib/io/src/win32/istream.c deleted file mode 100644 index a09f3c3..0000000 --- a/lib/io/src/win32/istream.c +++ /dev/null @@ -1,173 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * istream.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "../internal.h" -#include "sqfs/io.h" -#include "sqfs/error.h" - -#define WIN32_LEAN_AND_MEAN -#include <windows.h> - -typedef struct { -	sqfs_istream_t base; -	char *path; -	HANDLE hnd; - -	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; -	DWORD diff, actual; - -	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 < sizeof(file->buffer)) { -		diff = sizeof(file->buffer) - file->buffer_used; - -		if (!ReadFile(file->hnd, file->buffer + file->buffer_used, -			      diff, &actual, NULL)) { -			DWORD error = GetLastError(); - -			if (error == ERROR_HANDLE_EOF || -			    error == ERROR_BROKEN_PIPE) { -				file->eof = true; -				break; -			} - -			SetLastError(error); -			return SQFS_ERROR_IO; -		} - -		if (actual == 0) { -			file->eof = true; -			break; -		} - -		file->buffer_used += actual; -	} - -	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; - -	CloseHandle(file->hnd); -	free(file->path); -	free(file); -} - -int istream_open_handle(sqfs_istream_t **out, const char *path, HANDLE hnd) -{ -	file_istream_t *file = calloc(1, sizeof(*file)); -	sqfs_istream_t *strm = (sqfs_istream_t *)file; -	BOOL ret; - -	if (file == NULL) -		return SQFS_ERROR_ALLOC; - -	sqfs_object_init(file, file_destroy, NULL); - -	file->path = strdup(path); -	if (file->path == NULL) { -		DWORD temp = GetLastError(); -		free(file); -		SetLastError(temp); -		return SQFS_ERROR_ALLOC; -	} - -	ret = DuplicateHandle(GetCurrentProcess(), hnd, -			      GetCurrentProcess(), &file->hnd, -			      0, FALSE, DUPLICATE_SAME_ACCESS); -	if (!ret) { -		DWORD temp = GetLastError(); -		free(file->path); -		free(file); -		SetLastError(temp); -		return SQFS_ERROR_IO; -	} - -	CloseHandle(hnd); - -	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 istream_open_file(sqfs_istream_t **out, const char *path) -{ -	sqfs_file_handle_t hnd; -	int ret; - -	ret = sqfs_open_native_file(&hnd, path, SQFS_FILE_OPEN_READ_ONLY); -	if (ret) -		return ret; - -	ret = istream_open_handle(out, path, hnd); -	if (ret) { -		DWORD temp = GetLastError(); -		CloseHandle(hnd); -		SetLastError(temp); -		return ret; -	} - -	return 0; -} diff --git a/lib/io/src/win32/ostream.c b/lib/io/src/win32/ostream.c deleted file mode 100644 index b7f0e06..0000000 --- a/lib/io/src/win32/ostream.c +++ /dev/null @@ -1,190 +0,0 @@ -/* SPDX-License-Identifier: GPL-3.0-or-later */ -/* - * ostream.c - * - * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at> - */ -#include "../internal.h" -#include "sqfs/io.h" -#include "sqfs/error.h" - -#define WIN32_LEAN_AND_MEAN -#include <windows.h> - -typedef struct { -	sqfs_ostream_t base; -	sqfs_u64 sparse_count; -	char *path; -	HANDLE hnd; -	int flags; -} file_ostream_t; - -static int write_data(file_ostream_t *file, const void *data, size_t size) -{ -	DWORD diff; - -	while (size > 0) { -		if (!WriteFile(file->hnd, data, size, &diff, NULL)) -			return SQFS_ERROR_IO; - -		size -= diff; -		data = (const char *)data + diff; -	} - -	return 0; -} - -static int realize_sparse(file_ostream_t *file) -{ -	size_t bufsz, diff; -	LARGE_INTEGER pos; -	void *buffer; -	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 > bufsz ? -				bufsz : file->sparse_count; - -			ret = write_data(file, buffer, diff); -			if (ret) { -				DWORD temp = GetLastError(); -				free(buffer); -				SetLastError(temp); -				return ret; -			} - -			file->sparse_count -= diff; -		} - -		free(buffer); -	} else { -		pos.QuadPart = file->sparse_count; - -		if (!SetFilePointerEx(file->hnd, pos, NULL, FILE_CURRENT)) -			return SQFS_ERROR_IO; - -		if (!SetEndOfFile(file->hnd)) -			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; -		return 0; -	} - -	ret = realize_sparse(file); -	if (ret) -		return ret; - -	return write_data(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 (!FlushFileBuffers(file->hnd)) -		return SQFS_ERROR_IO; - -	return 0; -} - -static void file_destroy(sqfs_object_t *obj) -{ -	file_ostream_t *file = (file_ostream_t *)obj; - -	CloseHandle(file->hnd); -	free(file->path); -	free(file); -} - -static const char *file_get_filename(sqfs_ostream_t *strm) -{ -	return ((file_ostream_t *)strm)->path; -} - -int ostream_open_handle(sqfs_ostream_t **out, const char *path, -			sqfs_file_handle_t hnd, int flags) -{ -	file_ostream_t *file = calloc(1, sizeof(*file)); -	sqfs_ostream_t *strm = (sqfs_ostream_t *)file; -	BOOL ret; - -	if (file == NULL) -		return SQFS_ERROR_ALLOC; - -	sqfs_object_init(file, file_destroy, NULL); - -	file->path = strdup(path); -	if (file->path == NULL) { -		free(file); -		return SQFS_ERROR_ALLOC; -	} - -	ret = DuplicateHandle(GetCurrentProcess(), hnd, -			      GetCurrentProcess(), &file->hnd, -			      0, FALSE, DUPLICATE_SAME_ACCESS); -	if (!ret) { -		DWORD temp = GetLastError(); -		free(file->path); -		free(file); -		SetLastError(temp); -		return SQFS_ERROR_IO; -	} - -	CloseHandle(hnd); - -	file->flags = flags; -	strm->append = file_append; -	strm->flush = file_flush; -	strm->get_filename = file_get_filename; - -	*out = strm; -	return 0; -} - -int ostream_open_file(sqfs_ostream_t **out, const char *path, int flags) -{ -	sqfs_file_handle_t hnd; -	int ret; - -	*out = NULL; -	ret = sqfs_open_native_file(&hnd, path, flags); -	if (ret) -		return ret; - -	ret = ostream_open_handle(out, path, hnd, flags); -	if (ret) { -		DWORD temp = GetLastError(); -		CloseHandle(hnd); -		SetLastError(temp); -		return SQFS_ERROR_IO; -	} - -	return 0; -}  | 
