From c2e01ed987e942005fa66f11d96b8edb1d930c99 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 20 May 2019 11:01:10 +0200 Subject: cleanup: internalize meta reader/writer implementations Signed-off-by: David Oberhollenzer --- lib/sqfs/meta_reader.c | 23 +++++++++++++++++++++++ lib/sqfs/meta_writer.c | 33 +++++++++++++++++++++++++++++++++ lib/sqfs/table.c | 12 ++++++++---- 3 files changed, 64 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/sqfs/meta_reader.c b/lib/sqfs/meta_reader.c index 1a70238..e9b599f 100644 --- a/lib/sqfs/meta_reader.c +++ b/lib/sqfs/meta_reader.c @@ -7,6 +7,29 @@ #include #include +struct meta_reader_t { + /* The location of the current block in the image */ + uint64_t block_offset; + + /* The location of the next block after the current one */ + uint64_t next_block; + + /* A byte offset into the uncompressed data of the current block */ + size_t offset; + + /* The underlying file descriptor to read from */ + int fd; + + /* A pointer to the compressor to use for extracting data */ + compressor_t *cmp; + + /* The raw data read from the input file */ + uint8_t data[SQFS_META_BLOCK_SIZE]; + + /* The uncompressed data read from the input file */ + uint8_t scratch[SQFS_META_BLOCK_SIZE]; +}; + meta_reader_t *meta_reader_create(int fd, compressor_t *cmp) { meta_reader_t *m = calloc(1, sizeof(*m)); diff --git a/lib/sqfs/meta_writer.c b/lib/sqfs/meta_writer.c index 77ceeb9..2dacaa0 100644 --- a/lib/sqfs/meta_writer.c +++ b/lib/sqfs/meta_writer.c @@ -8,6 +8,26 @@ #include #include +struct meta_writer_t { + /* A byte offset into the uncompressed data of the current block */ + size_t offset; + + /* The location of the current block in the file */ + size_t block_offset; + + /* The underlying file descriptor to write to */ + int outfd; + + /* A pointer to the compressor to use for compressing the data */ + compressor_t *cmp; + + /* The raw data chunk that data is appended to */ + uint8_t data[SQFS_META_BLOCK_SIZE + 2]; + + /* Scratch buffer for compressing data */ + uint8_t scratch[SQFS_META_BLOCK_SIZE + 2]; +}; + meta_writer_t *meta_writer_create(int fd, compressor_t *cmp) { meta_writer_t *m = calloc(1, sizeof(*m)); @@ -95,3 +115,16 @@ int meta_writer_append(meta_writer_t *m, const void *data, size_t size) return 0; } + +void meta_writer_get_position(const meta_writer_t *m, uint64_t *block_start, + uint32_t *offset) +{ + *block_start = m->block_offset; + *offset = m->offset; +} + +void meta_writer_reset(meta_writer_t *m) +{ + m->block_offset = 0; + m->offset = 0; +} diff --git a/lib/sqfs/table.c b/lib/sqfs/table.c index 882d9ba..4e0f9d1 100644 --- a/lib/sqfs/table.c +++ b/lib/sqfs/table.c @@ -11,9 +11,10 @@ int sqfs_write_table(int outfd, sqfs_super_t *super, const void *data, compressor_t *cmp) { size_t ent_per_blocks = SQFS_META_BLOCK_SIZE / entsize; - uint64_t blocks[count / ent_per_blocks + 1]; + uint64_t blocks[count / ent_per_blocks + 1], block; size_t i, blkidx = 0, tblsize; meta_writer_t *m; + uint32_t offset; ssize_t ret; /* Write actual data. Whenever we cross a block boundary, remember @@ -23,8 +24,10 @@ int sqfs_write_table(int outfd, sqfs_super_t *super, const void *data, return -1; for (i = 0; i < count; ++i) { - if (blkidx == 0 || m->block_offset > blocks[blkidx - 1]) - blocks[blkidx++] = m->block_offset; + meta_writer_get_position(m, &block, &offset); + + if (blkidx == 0 || block > blocks[blkidx - 1]) + blocks[blkidx++] = block; if (meta_writer_append(m, data, entsize)) goto fail; @@ -38,7 +41,8 @@ int sqfs_write_table(int outfd, sqfs_super_t *super, const void *data, for (i = 0; i < blkidx; ++i) blocks[i] = htole64(blocks[i] + super->bytes_used); - super->bytes_used += m->block_offset; + meta_writer_get_position(m, &block, &offset); + super->bytes_used += block; meta_writer_destroy(m); /* write new index table */ -- cgit v1.2.3