diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-09 11:45:26 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-09 12:03:15 +0200 |
commit | 8ef9f10b20e313db5edd0b9c11ecdd8690812b8d (patch) | |
tree | 2695e7026197e016702abfa963f4034ca72e9362 /lib/sqfshelper | |
parent | 526fd8b4969b2efe62e0fbc339a7b7dafefb7729 (diff) |
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 <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfshelper')
-rw-r--r-- | lib/sqfshelper/data_writer.c | 49 | ||||
-rw-r--r-- | lib/sqfshelper/serialize_fstree.c | 11 | ||||
-rw-r--r-- | lib/sqfshelper/write_export_table.c | 2 | ||||
-rw-r--r-- | lib/sqfshelper/write_xattr.c | 27 |
4 files changed, 26 insertions, 63 deletions
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); |