From 8ef9f10b20e313db5edd0b9c11ecdd8690812b8d Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Mon, 9 Sep 2019 11:45:26 +0200 Subject: Remove ad-hoc image size accounting Much simpler to just set the correct size in the end, before writing the super block. Signed-off-by: David Oberhollenzer --- lib/sqfs/id_table.c | 2 +- lib/sqfs/write_table.c | 18 ++++---------- lib/sqfshelper/data_writer.c | 49 +++++++++---------------------------- lib/sqfshelper/serialize_fstree.c | 11 +++------ lib/sqfshelper/write_export_table.c | 2 +- lib/sqfshelper/write_xattr.c | 27 +++++++++----------- 6 files changed, 32 insertions(+), 77 deletions(-) (limited to 'lib') diff --git a/lib/sqfs/id_table.c b/lib/sqfs/id_table.c index 34db8f7..92a0039 100644 --- a/lib/sqfs/id_table.c +++ b/lib/sqfs/id_table.c @@ -132,7 +132,7 @@ int sqfs_id_table_write(sqfs_id_table_t *tbl, sqfs_file_t *file, super->id_count = tbl->num_ids; - ret = sqfs_write_table(file, super, cmp, tbl->ids, + ret = sqfs_write_table(file, cmp, tbl->ids, sizeof(tbl->ids[0]) * tbl->num_ids, &start); super->id_table_start = start; diff --git a/lib/sqfs/write_table.c b/lib/sqfs/write_table.c index 70c7d19..8d20a74 100644 --- a/lib/sqfs/write_table.c +++ b/lib/sqfs/write_table.c @@ -18,14 +18,12 @@ #include #include -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) +int sqfs_write_table(sqfs_file_t *file, 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 off, block, *locations; + uint64_t off, *locations; sqfs_meta_writer_t *m; - uint32_t offset; int ret; block_count = table_size / SQFS_META_BLOCK_SIZE; @@ -45,8 +43,7 @@ int sqfs_write_table(sqfs_file_t *file, sqfs_super_t *super, } while (table_size > 0) { - sqfs_meta_writer_get_position(m, &block, &offset); - locations[blkidx++] = htole64(super->bytes_used + block); + locations[blkidx++] = htole64(file->get_size(file)); diff = SQFS_META_BLOCK_SIZE; if (diff > table_size) @@ -64,11 +61,8 @@ int sqfs_write_table(sqfs_file_t *file, sqfs_super_t *super, if (ret) goto out; - sqfs_meta_writer_get_position(m, &block, &offset); - super->bytes_used += block; - /* write location list */ - *start = super->bytes_used; + *start = file->get_size(file); list_size = sizeof(uint64_t) * block_count; @@ -78,8 +72,6 @@ int sqfs_write_table(sqfs_file_t *file, sqfs_super_t *super, if (ret) goto out; - super->bytes_used += list_size; - /* cleanup */ ret = 0; out: diff --git a/lib/sqfshelper/data_writer.c b/lib/sqfshelper/data_writer.c index b94bdb6..463eb60 100644 --- a/lib/sqfshelper/data_writer.c +++ b/lib/sqfshelper/data_writer.c @@ -25,8 +25,7 @@ struct data_writer_t { size_t max_fragments; size_t devblksz; - uint64_t bytes_written; - off_t start; + uint64_t start; sqfs_block_processor_t *proc; sqfs_compressor_t *cmp; @@ -42,36 +41,10 @@ enum { BLK_FRAGMENT_BLOCK = SQFS_BLK_USER << 3, }; -static int save_position(data_writer_t *data) -{ - data->bytes_written = data->super->bytes_used; - data->start = data->file->get_size(data->file); - return 0; -} - -static int restore_position(data_writer_t *data) -{ - if (data->file->truncate(data->file, data->start)) { - perror("truncating squashfs image after file deduplication"); - return -1; - } - - data->super->bytes_used = data->bytes_written; - return 0; -} - static int allign_file(data_writer_t *data) { - size_t diff = data->super->bytes_used % data->devblksz; - - if (diff == 0) - return 0; - - if (padd_sqfs(data->file, data->super->bytes_used, data->devblksz)) - return -1; - - data->super->bytes_used += data->devblksz - diff; - return 0; + return padd_sqfs(data->file, data->file->get_size(data->file), + data->devblksz); } static int block_callback(void *user, sqfs_block_t *blk) @@ -82,13 +55,12 @@ static int block_callback(void *user, sqfs_block_t *blk) uint32_t out; if (blk->flags & BLK_FIRST_BLOCK) { - if (save_position(data)) - return -1; + data->start = data->file->get_size(data->file); if ((blk->flags & BLK_ALLIGN) && allign_file(data) != 0) return -1; - fi->startblock = data->super->bytes_used; + fi->startblock = data->file->get_size(data->file); } if (blk->size != 0) { @@ -97,7 +69,7 @@ static int block_callback(void *user, sqfs_block_t *blk) out |= 1 << 24; if (blk->flags & BLK_FRAGMENT_BLOCK) { - offset = htole64(data->super->bytes_used); + offset = htole64(data->file->get_size(data->file)); data->fragments[blk->index].start_offset = offset; data->fragments[blk->index].pad0 = 0; data->fragments[blk->index].size = htole32(out); @@ -115,8 +87,6 @@ static int block_callback(void *user, sqfs_block_t *blk) blk->data, blk->size)) { return -1; } - - data->super->bytes_used += blk->size; } if (blk->flags & BLK_LAST_BLOCK) { @@ -129,8 +99,11 @@ static int block_callback(void *user, sqfs_block_t *blk) fi->startblock = ref; fi->flags |= FILE_FLAG_BLOCKS_ARE_DUPLICATE; - if (restore_position(data)) + if (data->file->truncate(data->file, data->start)) { + perror("truncating squashfs image after " + "file deduplication"); return -1; + } } } @@ -518,7 +491,7 @@ int data_writer_write_fragment_table(data_writer_t *data) } size = sizeof(data->fragments[0]) * data->num_fragments; - ret = sqfs_write_table(data->file, data->super, data->cmp, + ret = sqfs_write_table(data->file, data->cmp, data->fragments, size, &start); if (ret) return -1; diff --git a/lib/sqfshelper/serialize_fstree.c b/lib/sqfshelper/serialize_fstree.c index ea3d479..1953e8d 100644 --- a/lib/sqfshelper/serialize_fstree.c +++ b/lib/sqfshelper/serialize_fstree.c @@ -64,6 +64,8 @@ int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs, if (dirwr == NULL) goto out_dm; + super->inode_table_start = file->get_size(file); + for (i = 2; i < fs->inode_tbl_size; ++i) { if (S_ISDIR(fs->inode_table[i]->mode)) { if (write_dir_entries(dirwr, fs->inode_table[i])) @@ -104,14 +106,7 @@ int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs, goto out; super->root_inode_ref = fs->root->inode_ref; - - sqfs_meta_writer_get_position(im, &block, &offset); - super->inode_table_start = super->bytes_used; - super->bytes_used += block; - - sqfs_meta_writer_get_position(dm, &block, &offset); - super->directory_table_start = super->bytes_used; - super->bytes_used += block; + super->directory_table_start = file->get_size(file); if (sqfs_meta_write_write_to_file(dm)) goto out; diff --git a/lib/sqfshelper/write_export_table.c b/lib/sqfshelper/write_export_table.c index f4c6658..b320381 100644 --- a/lib/sqfshelper/write_export_table.c +++ b/lib/sqfshelper/write_export_table.c @@ -38,7 +38,7 @@ int write_export_table(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, } size = sizeof(uint64_t) * (fs->inode_tbl_size - 1); - ret = sqfs_write_table(file, super, cmp, table, size, &start); + ret = sqfs_write_table(file, cmp, table, size, &start); super->export_table_start = start; super->flags |= SQFS_FLAG_EXPORTABLE; diff --git a/lib/sqfshelper/write_xattr.c b/lib/sqfshelper/write_xattr.c index c4fb86e..8103e29 100644 --- a/lib/sqfshelper/write_xattr.c +++ b/lib/sqfshelper/write_xattr.c @@ -170,7 +170,7 @@ static uint64_t *create_ool_locations_table(fstree_t *fs) int write_xattr(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, sqfs_compressor_t *cmp) { - uint64_t kv_start, id_start, block, *tbl, *ool_locations; + uint64_t kv_start, id_start, block, off, *tbl, *ool_locations; size_t i = 0, count = 0, blocks; sqfs_xattr_id_table_t idtbl; sqfs_xattr_id_t id_ent; @@ -190,7 +190,7 @@ int write_xattr(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, goto fail_ool; /* write xattr key-value pairs */ - kv_start = super->bytes_used; + kv_start = file->get_size(file); for (it = fs->xattr; it != NULL; it = it->next) { sqfs_meta_writer_get_position(mw, &it->block, &it->offset); @@ -205,11 +205,8 @@ int write_xattr(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, if (sqfs_meta_writer_flush(mw)) goto fail_mw; - sqfs_meta_writer_get_position(mw, &block, &offset); sqfs_meta_writer_reset(mw); - super->bytes_used += block; - /* allocate location table */ blocks = (count * sizeof(id_ent)) / SQFS_META_BLOCK_SIZE; @@ -225,7 +222,8 @@ int write_xattr(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, /* write ID table referring to key value pairs, record offsets */ id_start = 0; - tbl[i++] = htole64(super->bytes_used); + off = file->get_size(file); + tbl[i++] = htole64(off); for (it = fs->xattr; it != NULL; it = it->next) { id_ent.xattr = htole64((it->block << 16) | it->offset); @@ -239,36 +237,33 @@ int write_xattr(sqfs_file_t *file, fstree_t *fs, sqfs_super_t *super, if (block != id_start) { id_start = block; - tbl[i++] = htole64(super->bytes_used + id_start); + tbl[i++] = htole64(off + id_start); } } if (sqfs_meta_writer_flush(mw)) goto fail_tbl; - sqfs_meta_writer_get_position(mw, &block, &offset); - super->bytes_used += block; - /* write offset table */ idtbl.xattr_table_start = htole64(kv_start); idtbl.xattr_ids = htole32(count); idtbl.unused = 0; - if (file->write_at(file, file->get_size(file), &idtbl, sizeof(idtbl))) { + super->xattr_id_table_start = file->get_size(file); + super->flags &= ~SQFS_FLAG_NO_XATTRS; + + if (file->write_at(file, super->xattr_id_table_start, + &idtbl, sizeof(idtbl))) { perror("writing xattr ID table"); goto fail_tbl; } - if (file->write_at(file, file->get_size(file), + if (file->write_at(file, super->xattr_id_table_start + sizeof(idtbl), tbl, sizeof(tbl[0]) * blocks)) { perror("writing xattr ID table"); goto fail_tbl; } - super->xattr_id_table_start = super->bytes_used; - super->bytes_used += sizeof(idtbl) + sizeof(tbl[0]) * blocks; - super->flags &= ~SQFS_FLAG_NO_XATTRS; - free(tbl); sqfs_meta_writer_destroy(mw); free(ool_locations); -- cgit v1.2.3