diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-28 12:41:26 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-28 15:07:34 +0200 |
commit | 4e017928c7b5b590d2c7e04e42cb497eb3a4f8cf (patch) | |
tree | 28f649af1074ab74576a9a715991c610b210f1c0 /lib/sqfs/data_writer.c | |
parent | d89d7e3d7f5b21fe53ea0cbf65134bdc3df30950 (diff) |
Add support for packing sparse files
This commit adds support for packing sparse files into squashfs images
as follows:
- In the data writer: simply detect zero blocks and write a zero to the
block size field and don't emit any data. Record the number of bytes
saved this way. For fragments, set the fragment offset to invalid.
- In the inode writer: write out the number of bytes saved for sparse
files. If there should be a fragment but there is none, append a block
count of 0.
- In the data reader: if the block size is 0, read nothing from disk and
emit an empty block. Do the same if the fragment is missing.
- In the inode reader: restore the number of bytes saved for sparse files.
The sparse files can be packed and unpacked, but the unpacking will not
create sparse files for now.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs/data_writer.c')
-rw-r--r-- | lib/sqfs/data_writer.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/sqfs/data_writer.c b/lib/sqfs/data_writer.c index 753e5d4..70fe884 100644 --- a/lib/sqfs/data_writer.c +++ b/lib/sqfs/data_writer.c @@ -78,6 +78,11 @@ static int grow_fragment_table(data_writer_t *data) return 0; } +static bool is_zero_block(unsigned char *ptr, size_t size) +{ + return ptr[0] == 0 && memcmp(ptr, ptr + 1, size - 1) == 0; +} + int data_writer_flush_fragments(data_writer_t *data) { uint64_t offset; @@ -115,6 +120,7 @@ int write_data_from_fd(data_writer_t *data, file_info_t *fi, int infd) size_t diff; fi->startblock = data->super->bytes_used; + fi->sparse = 0; while (count != 0) { diff = count > (uint64_t)data->super->block_size ? @@ -126,6 +132,18 @@ int write_data_from_fd(data_writer_t *data, file_info_t *fi, int infd) if ((size_t)ret < diff) goto fail_trunc; + if (is_zero_block(data->block, diff)) { + if (diff < data->super->block_size) { + fi->fragment_offset = 0xFFFFFFFF; + fi->fragment = 0xFFFFFFFF; + } else { + fi->blocksizes[blk_idx++] = 0; + } + fi->sparse += diff; + count -= diff; + continue; + } + if (diff < data->super->block_size) { if (data->frag_offset + diff > data->super->block_size) { if (data_writer_flush_fragments(data)) |