From de4fc4b7e7b4302388cc1f778025f0843ef4ab24 Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Wed, 25 Sep 2019 13:40:48 +0200
Subject: Do the statistics acocunting through data writer hooks

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 lib/sqfshelper/data_writer.c | 51 ++++++++++++++++++++++++++++++++++++++++++++
 lib/sqfshelper/statistics.c  |  5 ++---
 2 files changed, 53 insertions(+), 3 deletions(-)

(limited to 'lib')

diff --git a/lib/sqfshelper/data_writer.c b/lib/sqfshelper/data_writer.c
index 4134035..96cd9ee 100644
--- a/lib/sqfshelper/data_writer.c
+++ b/lib/sqfshelper/data_writer.c
@@ -28,6 +28,55 @@ struct data_writer_t {
 	data_writer_stats_t stats;
 };
 
+
+static void post_block_write(void *user, const sqfs_block_t *block,
+			     sqfs_file_t *file)
+{
+	data_writer_t *data = user;
+	(void)file;
+
+	if (block->flags & SQFS_BLK_FRAGMENT_BLOCK) {
+		data->stats.frag_blocks_written += 1;
+	} else {
+		data->stats.blocks_written += 1;
+	}
+
+	data->stats.bytes_written += block->size;
+}
+
+static void pre_fragment_store(void *user, sqfs_block_t *block)
+{
+	data_writer_t *data = user;
+	(void)block;
+
+	data->stats.frag_count += 1;
+}
+
+static void notify_blocks_erased(void *user, size_t count, uint64_t bytes)
+{
+	data_writer_t *data = user;
+	(void)bytes;
+
+	data->stats.bytes_written -= bytes;
+	data->stats.blocks_written -= count;
+	data->stats.duplicate_blocks += count;
+}
+
+static void notify_fragment_discard(void *user, const sqfs_block_t *block)
+{
+	data_writer_t *data = user;
+	(void)block;
+
+	data->stats.frag_dup += 1;
+}
+
+static const sqfs_block_hooks_t hooks = {
+	.post_block_write = post_block_write,
+	.pre_fragment_store = pre_fragment_store,
+	.notify_blocks_erased = notify_blocks_erased,
+	.notify_fragment_discard = notify_fragment_discard,
+};
+
 static bool is_zero_block(unsigned char *ptr, size_t size)
 {
 	return ptr[0] == 0 && memcmp(ptr, ptr + 1, size - 1) == 0;
@@ -166,6 +215,8 @@ data_writer_t *data_writer_create(sqfs_super_t *super, sqfs_compressor_t *cmp,
 		return NULL;
 	}
 
+	sqfs_data_writer_set_hooks(data->proc, data, &hooks);
+
 	data->cmp = cmp;
 	data->super = super;
 	return data;
diff --git a/lib/sqfshelper/statistics.c b/lib/sqfshelper/statistics.c
index fda4c3f..87efc30 100644
--- a/lib/sqfshelper/statistics.c
+++ b/lib/sqfshelper/statistics.c
@@ -11,11 +11,10 @@
 
 void sqfs_print_statistics(sqfs_super_t *super, data_writer_stats_t *stats)
 {
-	uint64_t bytes_written = super->inode_table_start - sizeof(*super);
 	size_t ratio;
 
-	if (bytes_written > 0) {
-		ratio = (100 * bytes_written) / stats->bytes_read;
+	if (stats->bytes_written > 0) {
+		ratio = (100 * stats->bytes_written) / stats->bytes_read;
 	} else {
 		ratio = 100;
 	}
-- 
cgit v1.2.3