From 3a851dfe87c88ac1d4dddc2a26cc48b037f852f9 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 8 Sep 2019 14:53:30 +0200 Subject: Replace direct file I/O with abstraction layer This should make it easier to use libsquashfs with custom setups that embedd a squashfs image inside something else. Also, it should make it easier to port to non unix-like platforms. Signed-off-by: David Oberhollenzer --- lib/sqfs/comp/compressor.c | 22 ++++++++++++---------- lib/sqfs/comp/gzip.c | 8 ++++---- lib/sqfs/comp/internal.h | 6 ++++-- lib/sqfs/comp/lz4.c | 8 ++++---- lib/sqfs/comp/lzo.c | 8 ++++---- lib/sqfs/comp/xz.c | 8 ++++---- lib/sqfs/comp/zstd.c | 9 ++++----- 7 files changed, 36 insertions(+), 33 deletions(-) (limited to 'lib/sqfs/comp') diff --git a/lib/sqfs/comp/compressor.c b/lib/sqfs/comp/compressor.c index 4d073f2..c938fe2 100644 --- a/lib/sqfs/comp/compressor.c +++ b/lib/sqfs/comp/compressor.c @@ -43,29 +43,31 @@ static const char *names[] = { [SQFS_COMP_ZSTD] = "zstd", }; -int sqfs_generic_write_options(int fd, const void *data, size_t size) +int sqfs_generic_write_options(sqfs_file_t *file, const void *data, size_t size) { uint8_t buffer[size + 2]; + int ret; *((uint16_t *)buffer) = htole16(0x8000 | size); memcpy(buffer + 2, data, size); - if (write_data("writing compressor options", - fd, buffer, sizeof(buffer))) { - return SQFS_ERROR_IO; - } + ret = file->write_at(file, sizeof(sqfs_super_t), + buffer, sizeof(buffer)); + if (ret) + return ret; return sizeof(buffer); } -int sqfs_generic_read_options(int fd, void *data, size_t size) +int sqfs_generic_read_options(sqfs_file_t *file, void *data, size_t size) { uint8_t buffer[size + 2]; + int ret; - if (read_data_at("reading compressor options", sizeof(sqfs_super_t), - fd, buffer, sizeof(buffer))) { - return SQFS_ERROR_IO; - } + ret = file->read_at(file, sizeof(sqfs_super_t), + buffer, sizeof(buffer)); + if (ret) + return ret; if (le16toh(*((uint16_t *)buffer)) != (0x8000 | size)) return SQFS_ERROR_CORRUPTED; diff --git a/lib/sqfs/comp/gzip.c b/lib/sqfs/comp/gzip.c index 484901a..bec217a 100644 --- a/lib/sqfs/comp/gzip.c +++ b/lib/sqfs/comp/gzip.c @@ -44,7 +44,7 @@ static void gzip_destroy(sqfs_compressor_t *base) free(gzip); } -static int gzip_write_options(sqfs_compressor_t *base, int fd) +static int gzip_write_options(sqfs_compressor_t *base, sqfs_file_t *file) { gzip_compressor_t *gzip = (gzip_compressor_t *)base; gzip_options_t opt; @@ -59,16 +59,16 @@ static int gzip_write_options(sqfs_compressor_t *base, int fd) opt.window = htole16(gzip->opt.window); opt.strategies = htole16(gzip->opt.strategies); - return sqfs_generic_write_options(fd, &opt, sizeof(opt)); + return sqfs_generic_write_options(file, &opt, sizeof(opt)); } -static int gzip_read_options(sqfs_compressor_t *base, int fd) +static int gzip_read_options(sqfs_compressor_t *base, sqfs_file_t *file) { gzip_compressor_t *gzip = (gzip_compressor_t *)base; gzip_options_t opt; int ret; - ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + ret = sqfs_generic_read_options(file, &opt, sizeof(opt)); if (ret) return ret; diff --git a/lib/sqfs/comp/internal.h b/lib/sqfs/comp/internal.h index d2410fc..29b875f 100644 --- a/lib/sqfs/comp/internal.h +++ b/lib/sqfs/comp/internal.h @@ -12,13 +12,15 @@ #include "sqfs/predef.h" #include "sqfs/compress.h" #include "sqfs/error.h" +#include "sqfs/io.h" #include "util.h" SQFS_INTERNAL -int sqfs_generic_write_options(int fd, const void *data, size_t size); +int sqfs_generic_write_options(sqfs_file_t *file, const void *data, + size_t size); SQFS_INTERNAL -int sqfs_generic_read_options(int fd, void *data, size_t size); +int sqfs_generic_read_options(sqfs_file_t *file, void *data, size_t size); SQFS_INTERNAL sqfs_compressor_t *xz_compressor_create(const sqfs_compressor_config_t *cfg); diff --git a/lib/sqfs/comp/lz4.c b/lib/sqfs/comp/lz4.c index 446b83e..e96ab45 100644 --- a/lib/sqfs/comp/lz4.c +++ b/lib/sqfs/comp/lz4.c @@ -28,7 +28,7 @@ typedef struct { #define LZ4LEGACY 1 -static int lz4_write_options(sqfs_compressor_t *base, int fd) +static int lz4_write_options(sqfs_compressor_t *base, sqfs_file_t *file) { lz4_compressor_t *lz4 = (lz4_compressor_t *)base; lz4_options opt = { @@ -37,16 +37,16 @@ static int lz4_write_options(sqfs_compressor_t *base, int fd) SQFS_COMP_FLAG_LZ4_HC : 0), }; - return sqfs_generic_write_options(fd, &opt, sizeof(opt)); + return sqfs_generic_write_options(file, &opt, sizeof(opt)); } -static int lz4_read_options(sqfs_compressor_t *base, int fd) +static int lz4_read_options(sqfs_compressor_t *base, sqfs_file_t *file) { lz4_options opt; int ret; (void)base; - ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + ret = sqfs_generic_read_options(file, &opt, sizeof(opt)); if (ret) return ret; diff --git a/lib/sqfs/comp/lzo.c b/lib/sqfs/comp/lzo.c index b4cfebf..e4a7ca6 100644 --- a/lib/sqfs/comp/lzo.c +++ b/lib/sqfs/comp/lzo.c @@ -59,7 +59,7 @@ typedef struct { uint32_t level; } lzo_options_t; -static int lzo_write_options(sqfs_compressor_t *base, int fd) +static int lzo_write_options(sqfs_compressor_t *base, sqfs_file_t *file) { lzo_compressor_t *lzo = (lzo_compressor_t *)base; lzo_options_t opt; @@ -77,16 +77,16 @@ static int lzo_write_options(sqfs_compressor_t *base, int fd) opt.level = 0; } - return sqfs_generic_write_options(fd, &opt, sizeof(opt)); + return sqfs_generic_write_options(file, &opt, sizeof(opt)); } -static int lzo_read_options(sqfs_compressor_t *base, int fd) +static int lzo_read_options(sqfs_compressor_t *base, sqfs_file_t *file) { lzo_compressor_t *lzo = (lzo_compressor_t *)base; lzo_options_t opt; int ret; - ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + ret = sqfs_generic_read_options(file, &opt, sizeof(opt)); if (ret) return ret; diff --git a/lib/sqfs/comp/xz.c b/lib/sqfs/comp/xz.c index c626f92..24602ec 100644 --- a/lib/sqfs/comp/xz.c +++ b/lib/sqfs/comp/xz.c @@ -36,7 +36,7 @@ static bool is_dict_size_valid(size_t size) return size == (x | (x >> 1)); } -static int xz_write_options(sqfs_compressor_t *base, int fd) +static int xz_write_options(sqfs_compressor_t *base, sqfs_file_t *file) { xz_compressor_t *xz = (xz_compressor_t *)base; xz_options_t opt; @@ -47,16 +47,16 @@ static int xz_write_options(sqfs_compressor_t *base, int fd) opt.dict_size = htole32(xz->dict_size); opt.flags = htole32(xz->flags); - return sqfs_generic_write_options(fd, &opt, sizeof(opt)); + return sqfs_generic_write_options(file, &opt, sizeof(opt)); } -static int xz_read_options(sqfs_compressor_t *base, int fd) +static int xz_read_options(sqfs_compressor_t *base, sqfs_file_t *file) { xz_compressor_t *xz = (xz_compressor_t *)base; xz_options_t opt; int ret; - ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + ret = sqfs_generic_read_options(file, &opt, sizeof(opt)); if (ret) return ret; diff --git a/lib/sqfs/comp/zstd.c b/lib/sqfs/comp/zstd.c index dc2387d..5434e1f 100644 --- a/lib/sqfs/comp/zstd.c +++ b/lib/sqfs/comp/zstd.c @@ -25,26 +25,25 @@ typedef struct { uint32_t level; } zstd_options_t; -static int zstd_write_options(sqfs_compressor_t *base, int fd) +static int zstd_write_options(sqfs_compressor_t *base, sqfs_file_t *file) { zstd_compressor_t *zstd = (zstd_compressor_t *)base; zstd_options_t opt; - (void)fd; if (zstd->level == SQFS_ZSTD_DEFAULT_LEVEL) return 0; opt.level = htole32(zstd->level); - return sqfs_generic_write_options(fd, &opt, sizeof(opt)); + return sqfs_generic_write_options(file, &opt, sizeof(opt)); } -static int zstd_read_options(sqfs_compressor_t *base, int fd) +static int zstd_read_options(sqfs_compressor_t *base, sqfs_file_t *file) { zstd_options_t opt; int ret; (void)base; - ret = sqfs_generic_read_options(fd, &opt, sizeof(opt)); + ret = sqfs_generic_read_options(file, &opt, sizeof(opt)); if (ret) return ret; -- cgit v1.2.3