summaryrefslogtreecommitdiff
path: root/lib/sqfs/comp
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-08 14:53:30 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-09-08 14:53:30 +0200
commit3a851dfe87c88ac1d4dddc2a26cc48b037f852f9 (patch)
treea8a8f34291aa58b25737088d247a91a7f60b4fec /lib/sqfs/comp
parent60064dd0412a149fe00cfc4e2f2361c22656db57 (diff)
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/comp')
-rw-r--r--lib/sqfs/comp/compressor.c22
-rw-r--r--lib/sqfs/comp/gzip.c8
-rw-r--r--lib/sqfs/comp/internal.h6
-rw-r--r--lib/sqfs/comp/lz4.c8
-rw-r--r--lib/sqfs/comp/lzo.c8
-rw-r--r--lib/sqfs/comp/xz.c8
-rw-r--r--lib/sqfs/comp/zstd.c9
7 files changed, 36 insertions, 33 deletions
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;