summaryrefslogtreecommitdiff
path: root/lib/sqfs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqfs')
-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
-rw-r--r--lib/sqfs/id_table.c12
-rw-r--r--lib/sqfs/io.c151
-rw-r--r--lib/sqfs/meta_reader.c23
-rw-r--r--lib/sqfs/meta_writer.c23
-rw-r--r--lib/sqfs/read_super.c10
-rw-r--r--lib/sqfs/read_table.c14
-rw-r--r--lib/sqfs/super.c14
-rw-r--r--lib/sqfs/write_table.c19
-rw-r--r--lib/sqfs/xattr_reader.c35
16 files changed, 263 insertions, 107 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;
diff --git a/lib/sqfs/id_table.c b/lib/sqfs/id_table.c
index 496049e..34db8f7 100644
--- a/lib/sqfs/id_table.c
+++ b/lib/sqfs/id_table.c
@@ -73,8 +73,8 @@ int sqfs_id_table_index_to_id(const sqfs_id_table_t *tbl, uint16_t index,
return 0;
}
-int sqfs_id_table_read(sqfs_id_table_t *tbl, int fd, sqfs_super_t *super,
- sqfs_compressor_t *cmp)
+int sqfs_id_table_read(sqfs_id_table_t *tbl, sqfs_file_t *file,
+ sqfs_super_t *super, sqfs_compressor_t *cmp)
{
uint64_t upper_limit, lower_limit;
void *raw_ids;
@@ -106,7 +106,7 @@ int sqfs_id_table_read(sqfs_id_table_t *tbl, int fd, sqfs_super_t *super,
tbl->num_ids = super->id_count;
tbl->max_ids = super->id_count;
- ret = sqfs_read_table(fd, cmp, tbl->num_ids * sizeof(uint32_t),
+ ret = sqfs_read_table(file, cmp, tbl->num_ids * sizeof(uint32_t),
super->id_table_start, lower_limit,
upper_limit, &raw_ids);
if (ret)
@@ -120,8 +120,8 @@ int sqfs_id_table_read(sqfs_id_table_t *tbl, int fd, sqfs_super_t *super,
return 0;
}
-int sqfs_id_table_write(sqfs_id_table_t *tbl, int outfd, sqfs_super_t *super,
- sqfs_compressor_t *cmp)
+int sqfs_id_table_write(sqfs_id_table_t *tbl, sqfs_file_t *file,
+ sqfs_super_t *super, sqfs_compressor_t *cmp)
{
uint64_t start;
size_t i;
@@ -132,7 +132,7 @@ int sqfs_id_table_write(sqfs_id_table_t *tbl, int outfd, sqfs_super_t *super,
super->id_count = tbl->num_ids;
- ret = sqfs_write_table(outfd, super, cmp, tbl->ids,
+ ret = sqfs_write_table(file, super, cmp, tbl->ids,
sizeof(tbl->ids[0]) * tbl->num_ids, &start);
super->id_table_start = start;
diff --git a/lib/sqfs/io.c b/lib/sqfs/io.c
new file mode 100644
index 0000000..db60964
--- /dev/null
+++ b/lib/sqfs/io.c
@@ -0,0 +1,151 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * io.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 <errno.h>
+#include <fcntl.h>
+
+
+typedef struct {
+ sqfs_file_t base;
+
+ uint64_t size;
+ int fd;
+} sqfs_file_stdio_t;
+
+
+static void stdio_destroy(sqfs_file_t *base)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+
+ close(file->fd);
+ free(file);
+}
+
+static int stdio_read_at(sqfs_file_t *base, uint64_t offset,
+ void *buffer, size_t size)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+ ssize_t ret;
+
+ while (size > 0) {
+ ret = pread(file->fd, buffer, size, offset);
+
+ if (ret < 0) {
+ if (errno == EINTR)
+ continue;
+ return SQFS_ERROR_IO;
+ }
+
+ if (ret == 0)
+ return SQFS_ERROR_OUT_OF_BOUNDS;
+
+ buffer = (char *)buffer + ret;
+ size -= ret;
+ offset += ret;
+ }
+
+ return 0;
+}
+
+static int stdio_write_at(sqfs_file_t *base, uint64_t offset,
+ const void *buffer, size_t size)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+ ssize_t ret;
+
+ while (size > 0) {
+ ret = pwrite(file->fd, buffer, size, offset);
+
+ if (ret < 0) {
+ if (errno == EINTR)
+ continue;
+ return SQFS_ERROR_IO;
+ }
+
+ if (ret == 0)
+ return SQFS_ERROR_OUT_OF_BOUNDS;
+
+ buffer = (const char *)buffer + ret;
+ size -= ret;
+ offset += ret;
+ }
+
+ if (offset >= file->size)
+ file->size = offset;
+
+ return 0;
+}
+
+static uint64_t stdio_get_size(sqfs_file_t *base)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+
+ return file->size;
+}
+
+static int stdio_truncate(sqfs_file_t *base, uint64_t size)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+
+ if (ftruncate(file->fd, size))
+ return SQFS_ERROR_IO;
+
+ file->size = size;
+ return 0;
+}
+
+
+sqfs_file_t *sqfs_open_file(const char *filename, int flags)
+{
+ sqfs_file_stdio_t *file;
+ int open_mode, temp;
+ sqfs_file_t *base;
+
+ if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ file = calloc(1, sizeof(*file));
+ base = (sqfs_file_t *)file;
+ if (file == NULL)
+ return NULL;
+
+ if (flags & SQFS_FILE_OPEN_READ_ONLY) {
+ open_mode = O_RDONLY;
+ } else {
+ open_mode = O_CREAT | O_RDWR;
+
+ if (flags & SQFS_FILE_OPEN_OVERWRITE) {
+ open_mode |= O_TRUNC;
+ } else {
+ open_mode |= O_EXCL;
+ }
+ }
+
+ file->fd = open(filename, open_mode, 0600);
+ if (file->fd < 0) {
+ temp = errno;
+ free(file);
+ errno = temp;
+ return NULL;
+ }
+
+ base->destroy = stdio_destroy;
+ base->read_at = stdio_read_at;
+ base->write_at = stdio_write_at;
+ base->get_size = stdio_get_size;
+ base->truncate = stdio_truncate;
+ return base;
+}
diff --git a/lib/sqfs/meta_reader.c b/lib/sqfs/meta_reader.c
index 295d0de..7d56869 100644
--- a/lib/sqfs/meta_reader.c
+++ b/lib/sqfs/meta_reader.c
@@ -11,6 +11,7 @@
#include "sqfs/compress.h"
#include "sqfs/error.h"
#include "sqfs/data.h"
+#include "sqfs/io.h"
#include "util.h"
#include <stdlib.h>
@@ -32,7 +33,7 @@ struct sqfs_meta_reader_t {
size_t offset;
/* The underlying file descriptor to read from */
- int fd;
+ sqfs_file_t *file;
/* A pointer to the compressor to use for extracting data */
sqfs_compressor_t *cmp;
@@ -44,7 +45,8 @@ struct sqfs_meta_reader_t {
uint8_t scratch[SQFS_META_BLOCK_SIZE];
};
-sqfs_meta_reader_t *sqfs_meta_reader_create(int fd, sqfs_compressor_t *cmp,
+sqfs_meta_reader_t *sqfs_meta_reader_create(sqfs_file_t *file,
+ sqfs_compressor_t *cmp,
uint64_t start, uint64_t limit)
{
sqfs_meta_reader_t *m = calloc(1, sizeof(*m));
@@ -54,7 +56,7 @@ sqfs_meta_reader_t *sqfs_meta_reader_create(int fd, sqfs_compressor_t *cmp,
m->start = start;
m->limit = limit;
- m->fd = fd;
+ m->file = file;
m->cmp = cmp;
return m;
}
@@ -71,6 +73,7 @@ int sqfs_meta_reader_seek(sqfs_meta_reader_t *m, uint64_t block_start,
uint16_t header;
ssize_t ret;
size_t size;
+ int err;
if (block_start < m->start || block_start >= m->limit)
return SQFS_ERROR_OUT_OF_BOUNDS;
@@ -83,10 +86,9 @@ int sqfs_meta_reader_seek(sqfs_meta_reader_t *m, uint64_t block_start,
return 0;
}
- if (read_data_at("reading meta data header", block_start,
- m->fd, &header, 2)) {
- return SQFS_ERROR_IO;
- }
+ err = m->file->read_at(m->file, block_start, &header, 2);
+ if (err)
+ return err;
header = le16toh(header);
compressed = (header & 0x8000) == 0;
@@ -98,10 +100,9 @@ int sqfs_meta_reader_seek(sqfs_meta_reader_t *m, uint64_t block_start,
if ((block_start + 2 + size) > m->limit)
return SQFS_ERROR_OUT_OF_BOUNDS;
- if (read_data_at("reading meta data block", block_start + 2,
- m->fd, m->data, size)) {
- return SQFS_ERROR_IO;
- }
+ err = m->file->read_at(m->file, block_start + 2, m->data, size);
+ if (err)
+ return err;
if (compressed) {
ret = m->cmp->do_block(m->cmp, m->data, size,
diff --git a/lib/sqfs/meta_writer.c b/lib/sqfs/meta_writer.c
index 46f9618..4bd7e56 100644
--- a/lib/sqfs/meta_writer.c
+++ b/lib/sqfs/meta_writer.c
@@ -11,6 +11,7 @@
#include "sqfs/compress.h"
#include "sqfs/error.h"
#include "sqfs/data.h"
+#include "sqfs/io.h"
#include "util.h"
#include <string.h>
@@ -32,7 +33,7 @@ struct sqfs_meta_writer_t {
size_t block_offset;
/* The underlying file descriptor to write to */
- int outfd;
+ sqfs_file_t *file;
/* A pointer to the compressor to use for compressing the data */
sqfs_compressor_t *cmp;
@@ -45,15 +46,16 @@ struct sqfs_meta_writer_t {
meta_block_t *list_end;
};
-static int write_block(int fd, meta_block_t *outblk)
+static int write_block(sqfs_file_t *file, meta_block_t *outblk)
{
size_t count = le16toh(((uint16_t *)outblk->data)[0]) & 0x7FFF;
+ uint64_t off = file->get_size(file);
- return write_data("writing meta data block", fd,
- outblk->data, count + 2);
+ return file->write_at(file, off, outblk->data, count + 2);
}
-sqfs_meta_writer_t *sqfs_meta_writer_create(int fd, sqfs_compressor_t *cmp,
+sqfs_meta_writer_t *sqfs_meta_writer_create(sqfs_file_t *file,
+ sqfs_compressor_t *cmp,
bool keep_in_mem)
{
sqfs_meta_writer_t *m = calloc(1, sizeof(*m));
@@ -62,7 +64,7 @@ sqfs_meta_writer_t *sqfs_meta_writer_create(int fd, sqfs_compressor_t *cmp,
return NULL;
m->cmp = cmp;
- m->outfd = fd;
+ m->file = file;
m->keep_in_mem = keep_in_mem;
return m;
}
@@ -119,8 +121,7 @@ int sqfs_meta_writer_flush(sqfs_meta_writer_t *m)
}
m->list_end = outblk;
} else {
- if (write_block(m->outfd, outblk))
- ret = SQFS_ERROR_IO;
+ ret = write_block(m->file, outblk);
free(outblk);
}
@@ -178,12 +179,14 @@ void sqfs_meta_writer_reset(sqfs_meta_writer_t *m)
int sqfs_meta_write_write_to_file(sqfs_meta_writer_t *m)
{
meta_block_t *blk;
+ int ret;
while (m->list != NULL) {
blk = m->list;
- if (write_block(m->outfd, blk))
- return SQFS_ERROR_IO;
+ ret = write_block(m->file, blk);
+ if (ret)
+ return ret;
m->list = blk->next;
free(blk);
diff --git a/lib/sqfs/read_super.c b/lib/sqfs/read_super.c
index 9f7425c..23909e0 100644
--- a/lib/sqfs/read_super.c
+++ b/lib/sqfs/read_super.c
@@ -9,19 +9,21 @@
#include "sqfs/super.h"
#include "sqfs/error.h"
+#include "sqfs/io.h"
#include "util.h"
#include <endian.h>
#include <string.h>
-int sqfs_super_read(sqfs_super_t *super, int fd)
+int sqfs_super_read(sqfs_super_t *super, sqfs_file_t *file)
{
size_t block_size = 0;
sqfs_super_t temp;
- int i;
+ int i, ret;
- if (read_data_at("reading super block", 0, fd, &temp, sizeof(temp)))
- return SQFS_ERROR_IO;
+ ret = file->read_at(file, 0, &temp, sizeof(temp));
+ if (ret)
+ return ret;
temp.magic = le32toh(temp.magic);
temp.inode_count = le32toh(temp.inode_count);
diff --git a/lib/sqfs/read_table.c b/lib/sqfs/read_table.c
index 843247e..129ecaf 100644
--- a/lib/sqfs/read_table.c
+++ b/lib/sqfs/read_table.c
@@ -11,13 +11,14 @@
#include "sqfs/error.h"
#include "sqfs/table.h"
#include "sqfs/data.h"
+#include "sqfs/io.h"
#include "util.h"
#include <endian.h>
#include <stdlib.h>
-int sqfs_read_table(int fd, sqfs_compressor_t *cmp, size_t table_size,
- uint64_t location, uint64_t lower_limit,
+int sqfs_read_table(sqfs_file_t *file, sqfs_compressor_t *cmp,
+ size_t table_size, uint64_t location, uint64_t lower_limit,
uint64_t upper_limit, void **out)
{
size_t diff, block_count, blk_idx = 0;
@@ -43,14 +44,13 @@ int sqfs_read_table(int fd, sqfs_compressor_t *cmp, size_t table_size,
goto fail_data;
}
- if (read_data_at("reading table locations", location,
- fd, locations, sizeof(uint64_t) * block_count)) {
- err = SQFS_ERROR_IO;
+ err = file->read_at(file, location, locations,
+ sizeof(uint64_t) * block_count);
+ if (err)
goto fail_idx;
- }
/* Read the actual data */
- m = sqfs_meta_reader_create(fd, cmp, lower_limit, upper_limit);
+ m = sqfs_meta_reader_create(file, cmp, lower_limit, upper_limit);
if (m == NULL) {
err = SQFS_ERROR_ALLOC;
goto fail_idx;
diff --git a/lib/sqfs/super.c b/lib/sqfs/super.c
index 0489079..b3984d2 100644
--- a/lib/sqfs/super.c
+++ b/lib/sqfs/super.c
@@ -9,6 +9,7 @@
#include "sqfs/super.h"
#include "sqfs/error.h"
+#include "sqfs/io.h"
#include "util.h"
#include <endian.h>
@@ -48,7 +49,7 @@ int sqfs_super_init(sqfs_super_t *super, size_t block_size, uint32_t mtime,
return 0;
}
-int sqfs_super_write(sqfs_super_t *super, int fd)
+int sqfs_super_write(sqfs_super_t *super, sqfs_file_t *file)
{
sqfs_super_t copy;
@@ -72,14 +73,5 @@ int sqfs_super_write(sqfs_super_t *super, int fd)
copy.fragment_table_start = htole64(super->fragment_table_start);
copy.export_table_start = htole64(super->export_table_start);
- if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
- return SQFS_ERROR_IO;
-
- if (write_data("writing super block", fd, &copy, sizeof(copy)))
- return SQFS_ERROR_IO;
-
- if (lseek(fd, 0, SEEK_END) == (off_t)-1)
- return SQFS_ERROR_IO;
-
- return 0;
+ return file->write_at(file, 0, &copy, sizeof(copy));
}
diff --git a/lib/sqfs/write_table.c b/lib/sqfs/write_table.c
index c264ebd..70c7d19 100644
--- a/lib/sqfs/write_table.c
+++ b/lib/sqfs/write_table.c
@@ -12,16 +12,18 @@
#include "sqfs/super.h"
#include "sqfs/table.h"
#include "sqfs/data.h"
+#include "sqfs/io.h"
#include "util.h"
#include <endian.h>
#include <stdlib.h>
-int sqfs_write_table(int outfd, sqfs_super_t *super, sqfs_compressor_t *cmp,
- const void *data, size_t table_size, uint64_t *start)
+int sqfs_write_table(sqfs_file_t *file, sqfs_super_t *super,
+ sqfs_compressor_t *cmp, const void *data,
+ size_t table_size, uint64_t *start)
{
size_t block_count, list_size, diff, blkidx = 0;
- uint64_t block, *locations;
+ uint64_t off, block, *locations;
sqfs_meta_writer_t *m;
uint32_t offset;
int ret;
@@ -36,7 +38,7 @@ int sqfs_write_table(int outfd, sqfs_super_t *super, sqfs_compressor_t *cmp,
return SQFS_ERROR_ALLOC;
/* Write actual data */
- m = sqfs_meta_writer_create(outfd, cmp, false);
+ m = sqfs_meta_writer_create(file, cmp, false);
if (m == NULL) {
ret = SQFS_ERROR_ALLOC;
goto out_idx;
@@ -69,11 +71,12 @@ int sqfs_write_table(int outfd, sqfs_super_t *super, sqfs_compressor_t *cmp,
*start = super->bytes_used;
list_size = sizeof(uint64_t) * block_count;
- if (write_data("writing table locations", outfd,
- locations, list_size)) {
- ret = SQFS_ERROR_IO;
+
+ off = file->get_size(file);
+
+ ret = file->write_at(file, off, locations, list_size);
+ if (ret)
goto out;
- }
super->bytes_used += list_size;
diff --git a/lib/sqfs/xattr_reader.c b/lib/sqfs/xattr_reader.c
index 80731ba..1634be8 100644
--- a/lib/sqfs/xattr_reader.c
+++ b/lib/sqfs/xattr_reader.c
@@ -12,6 +12,7 @@
#include "sqfs/xattr.h"
#include "sqfs/error.h"
#include "sqfs/data.h"
+#include "sqfs/io.h"
#include "util.h"
#include <stdlib.h>
@@ -29,7 +30,7 @@ struct sqfs_xattr_reader_t {
sqfs_meta_reader_t *idrd;
sqfs_meta_reader_t *kvrd;
sqfs_super_t *super;
- int sqfsfd;
+ sqfs_file_t *file;
};
int sqfs_xattr_reader_load_locations(sqfs_xattr_reader_t *xr)
@@ -47,11 +48,10 @@ int sqfs_xattr_reader_load_locations(sqfs_xattr_reader_t *xr)
if (xr->super->xattr_id_table_start >= xr->super->bytes_used)
return SQFS_ERROR_OUT_OF_BOUNDS;
- if (read_data_at("reading xattr ID location table",
- xr->super->xattr_id_table_start,
- xr->sqfsfd, &idtbl, sizeof(idtbl))) {
- return SQFS_ERROR_IO;
- }
+ err = xr->file->read_at(xr->file, xr->super->xattr_id_table_start,
+ &idtbl, sizeof(idtbl));
+ if (err)
+ return err;
xr->xattr_start = le64toh(idtbl.xattr_table_start);
xr->num_ids = le32toh(idtbl.xattr_ids);
@@ -68,13 +68,12 @@ int sqfs_xattr_reader_load_locations(sqfs_xattr_reader_t *xr)
return SQFS_ERROR_ALLOC;
}
- if (read_data_at("reading xattr ID block locations",
- xr->super->xattr_id_table_start + sizeof(idtbl),
- xr->sqfsfd, xr->id_block_starts,
- sizeof(uint64_t) * xr->num_id_blocks)) {
- err = SQFS_ERROR_IO;
+ err = xr->file->read_at(xr->file,
+ xr->super->xattr_id_table_start + sizeof(idtbl),
+ xr->id_block_starts,
+ sizeof(uint64_t) * xr->num_id_blocks);
+ if (err)
goto fail;
- }
for (i = 0; i < xr->num_id_blocks; ++i) {
xr->id_block_starts[i] = le64toh(xr->id_block_starts[i]);
@@ -254,7 +253,8 @@ void sqfs_xattr_reader_destroy(sqfs_xattr_reader_t *xr)
free(xr);
}
-sqfs_xattr_reader_t *sqfs_xattr_reader_create(int sqfsfd, sqfs_super_t *super,
+sqfs_xattr_reader_t *sqfs_xattr_reader_create(sqfs_file_t *file,
+ sqfs_super_t *super,
sqfs_compressor_t *cmp)
{
sqfs_xattr_reader_t *xr = calloc(1, sizeof(*xr));
@@ -262,26 +262,27 @@ sqfs_xattr_reader_t *sqfs_xattr_reader_create(int sqfsfd, sqfs_super_t *super,
if (xr == NULL)
return NULL;
+ xr->file = file;
+ xr->super = super;
+
if (super->flags & SQFS_FLAG_NO_XATTRS)
return xr;
if (super->xattr_id_table_start == 0xFFFFFFFFFFFFFFFF)
return xr;
- xr->idrd = sqfs_meta_reader_create(sqfsfd, cmp,
+ xr->idrd = sqfs_meta_reader_create(file, cmp,
super->id_table_start,
super->bytes_used);
if (xr->idrd == NULL)
goto fail;
- xr->kvrd = sqfs_meta_reader_create(sqfsfd, cmp,
+ xr->kvrd = sqfs_meta_reader_create(file, cmp,
super->id_table_start,
super->bytes_used);
if (xr->kvrd == NULL)
goto fail;
- xr->sqfsfd = sqfsfd;
- xr->super = super;
return xr;
fail:
sqfs_xattr_reader_destroy(xr);