From fd7497d3dbcac782da38628349d40104437cba7d Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Sun, 15 Sep 2019 21:16:29 +0200
Subject: Fix data block deduplication

Because deduplication can now cross file boundaries, we have to include
the fragment blocks in-between.

Also, allow self-overlapping deduplication.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 lib/sqfshelper/data_writer.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

(limited to 'lib/sqfshelper')

diff --git a/lib/sqfshelper/data_writer.c b/lib/sqfshelper/data_writer.c
index 137fe06..3a27e06 100644
--- a/lib/sqfshelper/data_writer.c
+++ b/lib/sqfshelper/data_writer.c
@@ -153,13 +153,12 @@ static int block_callback(void *user, sqfs_block_t *blk)
 		} else {
 			fi->block_size[blk->index] = htole32(out);
 
-			if (store_block_location(data, offset, out,
-						 blk->checksum))
-				return -1;
-
 			data->stats.blocks_written += 1;
 		}
 
+		if (store_block_location(data, offset, out, blk->checksum))
+			return -1;
+
 		if (data->file->write_at(data->file, offset,
 					 blk->data, blk->size)) {
 			return -1;
@@ -175,10 +174,17 @@ static int block_callback(void *user, sqfs_block_t *blk)
 
 		fi->startblock = data->blocks[start].offset;
 
-		if (start + count < data->file_start) {
-			data->num_blocks = data->file_start;
+		if (start < data->file_start) {
+			offset = start + count;
 
-			data->stats.duplicate_blocks += count;
+			if (offset >= data->file_start) {
+				data->num_blocks = offset;
+				data->stats.duplicate_blocks +=
+					offset - data->num_blocks;
+			} else {
+				data->num_blocks = data->file_start;
+				data->stats.duplicate_blocks += count;
+			}
 
 			if (data->file->truncate(data->file, data->start)) {
 				perror("truncating squashfs image after "
-- 
cgit v1.2.3