diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-23 18:51:51 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-09-23 18:53:26 +0200 |
commit | c169887cc5d3ab83e7383234f46ad8963bc22b98 (patch) | |
tree | d00668257fe390250211278d0d9552cafa9431d0 | |
parent | 41d33212c94d3f9efd43e0c40dd166c2136512a8 (diff) |
Cleanup fragment handling in the data reader a little
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r-- | lib/sqfshelper/data_writer.c | 77 |
1 files changed, 42 insertions, 35 deletions
diff --git a/lib/sqfshelper/data_writer.c b/lib/sqfshelper/data_writer.c index 3163c4e..5bce527 100644 --- a/lib/sqfshelper/data_writer.c +++ b/lib/sqfshelper/data_writer.c @@ -50,20 +50,52 @@ static int flush_fragment_block(data_writer_t *data) { int ret; - data->frag_block->index = data->num_fragments++; - ret = sqfs_block_processor_enqueue(data->proc, data->frag_block); data->frag_block = NULL; return ret; } +static int store_fragment(data_writer_t *data, sqfs_block_t *frag, + uint64_t signature) +{ + size_t new_sz; + void *new; + + if (data->frag_list_num == data->frag_list_max) { + new_sz = data->frag_list_max * 2; + new = realloc(data->frag_list, + sizeof(data->frag_list[0]) * new_sz); + + if (new == NULL) { + perror("growing fragment checksum table"); + return -1; + } + + data->frag_list = new; + data->frag_list_max = new_sz; + } + + data->frag_list[data->frag_list_num].index = data->frag_block->index; + data->frag_list[data->frag_list_num].offset = data->frag_block->size; + data->frag_list[data->frag_list_num].signature = signature; + data->frag_list_num += 1; + + sqfs_inode_set_frag_location(frag->inode, data->frag_block->index, + data->frag_block->size); + + memcpy(data->frag_block->data + data->frag_block->size, + frag->data, frag->size); + + data->frag_block->flags |= (frag->flags & SQFS_BLK_DONT_COMPRESS); + data->frag_block->size += frag->size; + return 0; +} + static int handle_fragment(data_writer_t *data, sqfs_block_t *frag) { - size_t i, size, new_sz; uint64_t signature; - void *new; + size_t i, size; - frag->checksum = crc32(0, frag->data, frag->size); signature = MK_BLK_SIG(frag->checksum, frag->size); for (i = 0; i < data->frag_list_num; ++i) { @@ -72,8 +104,6 @@ static int handle_fragment(data_writer_t *data, sqfs_block_t *frag) data->frag_list[i].index, data->frag_list[i].offset); free(frag); - - data->stats.frag_dup += 1; return 0; } } @@ -96,39 +126,14 @@ static int handle_fragment(data_writer_t *data, sqfs_block_t *frag) goto fail; } + data->frag_block->index = data->num_fragments++; data->frag_block->flags = SQFS_BLK_FRAGMENT_BLOCK; } - if (data->frag_list_num == data->frag_list_max) { - new_sz = data->frag_list_max * 2; - new = realloc(data->frag_list, - sizeof(data->frag_list[0]) * new_sz); - - if (new == NULL) { - perror("growing fragment checksum table"); - return -1; - } - - data->frag_list = new; - data->frag_list_max = new_sz; - } - - data->frag_list[data->frag_list_num].index = data->num_fragments; - data->frag_list[data->frag_list_num].offset = data->frag_block->size; - data->frag_list[data->frag_list_num].signature = signature; - data->frag_list_num += 1; - - sqfs_inode_set_frag_location(frag->inode, data->num_fragments, - data->frag_block->size); + if (store_fragment(data, frag, signature)) + goto fail; - data->frag_block->flags |= (frag->flags & SQFS_BLK_DONT_COMPRESS); - memcpy(data->frag_block->data + data->frag_block->size, - frag->data, frag->size); - - data->frag_block->size += frag->size; free(frag); - - data->stats.frag_count += 1; return 0; fail: free(frag); @@ -220,6 +225,8 @@ int write_data_from_file_condensed(data_writer_t *data, sqfs_file_t *file, } } + blk->checksum = crc32(0, blk->data, blk->size); + if (handle_fragment(data, blk)) return -1; } else { |