From 060970feee2beea37e837e0a2d557e878e5f8290 Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Mon, 10 Feb 2020 21:21:57 +0100
Subject: Cleanup: Use stat structs instead of hooks in tar2sqfs/gensquashfs

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 include/common.h        | 20 ++---------
 lib/common/statistics.c | 91 ++++++++++++-------------------------------------
 lib/common/writer.c     |  5 +--
 mkfs/mkfs.c             |  7 ++--
 tar/tar2sqfs.c          |  3 --
 5 files changed, 27 insertions(+), 99 deletions(-)

diff --git a/include/common.h b/include/common.h
index f906c27..a3055dc 100644
--- a/include/common.h
+++ b/include/common.h
@@ -34,18 +34,6 @@
 
 #include <stddef.h>
 
-typedef struct {
-	size_t file_count;
-	size_t blocks_written;
-	size_t frag_blocks_written;
-	size_t duplicate_blocks;
-	size_t sparse_blocks;
-	size_t frag_count;
-	size_t frag_dup;
-	sqfs_u64 bytes_written;
-	sqfs_u64 bytes_read;
-} block_processor_stats_t;
-
 typedef struct {
 	sqfs_block_writer_t *blkwr;
 	sqfs_frag_table_t *fragtbl;
@@ -58,7 +46,6 @@ typedef struct {
 	sqfs_file_t *outfile;
 	sqfs_super_t super;
 	fstree_t fs;
-	block_processor_stats_t stats;
 	sqfs_xattr_writer_t *xwr;
 } sqfs_writer_t;
 
@@ -103,7 +90,9 @@ typedef struct sqfs_hard_link_t {
 int sqfs_serialize_fstree(const char *filename, sqfs_writer_t *wr);
 
 /* Print out fancy statistics for squashfs packing tools */
-void sqfs_print_statistics(sqfs_super_t *super, block_processor_stats_t *stats);
+void sqfs_print_statistics(const sqfs_super_t *super,
+			   const sqfs_block_processor_t *blk,
+			   const sqfs_block_writer_t *wr);
 
 void compressor_print_available(void);
 
@@ -126,9 +115,6 @@ int sqfs_data_reader_dump(const char *name, sqfs_data_reader_t *data,
 sqfs_file_t *sqfs_get_stdin_file(FILE *fp, const sparse_map_t *map,
 				 sqfs_u64 size);
 
-void register_stat_hooks(sqfs_block_processor_t *data,
-			 block_processor_stats_t *stats);
-
 int write_data_from_file(const char *filename, sqfs_block_processor_t *data,
 			 sqfs_inode_generic_t *inode,
 			 sqfs_file_t *file, int flags);
diff --git a/lib/common/statistics.c b/lib/common/statistics.c
index b41cd22..eabe037 100644
--- a/lib/common/statistics.c
+++ b/lib/common/statistics.c
@@ -8,84 +8,35 @@
 
 #include <stdio.h>
 
-static void post_block_write(void *user, const sqfs_block_t *block,
-			     sqfs_file_t *file)
-{
-	block_processor_stats_t *stats = user;
-	(void)file;
-
-	if (block->size == 0)
-		return;
-
-	if (block->flags & SQFS_BLK_FRAGMENT_BLOCK) {
-		stats->frag_blocks_written += 1;
-	} else {
-		stats->blocks_written += 1;
-	}
-
-	stats->bytes_written += block->size;
-}
-
-static void pre_fragment_store(void *user, sqfs_block_t *block)
-{
-	block_processor_stats_t *stats = user;
-	(void)block;
-
-	stats->frag_count += 1;
-}
-
-static void notify_blocks_erased(void *user, size_t count, sqfs_u64 bytes)
-{
-	block_processor_stats_t *stats = user;
-
-	stats->bytes_written -= bytes;
-	stats->blocks_written -= count;
-	stats->duplicate_blocks += count;
-}
-
-static void notify_fragment_discard(void *user, const sqfs_block_t *block)
-{
-	block_processor_stats_t *stats = user;
-	(void)block;
-
-	stats->frag_dup += 1;
-}
-
-static const sqfs_block_hooks_t hooks = {
-	.size = sizeof(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,
-};
-
-void register_stat_hooks(sqfs_block_processor_t *data,
-			 block_processor_stats_t *stats)
-{
-	sqfs_block_processor_set_hooks(data, stats, &hooks);
-}
-
-void sqfs_print_statistics(sqfs_super_t *super, block_processor_stats_t *stats)
+void sqfs_print_statistics(const sqfs_super_t *super,
+			   const sqfs_block_processor_t *blk,
+			   const sqfs_block_writer_t *wr)
 {
+	const sqfs_block_processor_stats_t *proc_stats;
+	const sqfs_block_writer_stats_t *wr_stats;
 	size_t ratio;
 
-	if (stats->bytes_written > 0) {
-		ratio = (100 * stats->bytes_written) / stats->bytes_read;
+	proc_stats = sqfs_block_processor_get_stats(blk);
+	wr_stats = sqfs_block_writer_get_stats(wr);
+
+	if (proc_stats->input_bytes_read > 0) {
+		ratio = (100 * wr_stats->bytes_written) /
+			proc_stats->input_bytes_read;
 	} else {
 		ratio = 100;
 	}
 
 	fputs("---------------------------------------------------\n", stdout);
-	printf("Input files processed: " PRI_SZ"\n", stats->file_count);
-	printf("Data blocks actually written: " PRI_SZ "\n",
-	       stats->blocks_written);
-	printf("Fragment blocks written: " PRI_SZ "\n",
-	       stats->frag_blocks_written);
-	printf("Duplicate data blocks omitted: " PRI_SZ "\n",
-	       stats->duplicate_blocks);
-	printf("Sparse blocks omitted: " PRI_SZ "\n", stats->sparse_blocks);
-	printf("Fragments actually written: " PRI_SZ "\n", stats->frag_count);
-	printf("Duplicated fragments omitted: " PRI_SZ"\n", stats->frag_dup);
+	printf("Input files processed: %lu\n", proc_stats->input_bytes_read);
+	printf("Data blocks actually written: %lu\n", wr_stats->blocks_written);
+	printf("Fragment blocks written: %lu\n", proc_stats->frag_block_count);
+	printf("Duplicate data blocks omitted: %lu\n",
+	       wr_stats->blocks_submitted - wr_stats->blocks_written);
+	printf("Sparse blocks omitted: %lu\n", proc_stats->sparse_block_count);
+	printf("Fragments actually written: %lu\n",
+	       proc_stats->actual_frag_count);
+	printf("Duplicated fragments omitted: %lu\n",
+	       proc_stats->total_frag_count - proc_stats->actual_frag_count);
 	printf("Total number of inodes: %u\n", super->inode_count);
 	printf("Number of unique group/user IDs: %u\n", super->id_count);
 	printf("Data compression ratio: " PRI_SZ "%%\n", ratio);
diff --git a/lib/common/writer.c b/lib/common/writer.c
index 7f69752..84a40d2 100644
--- a/lib/common/writer.c
+++ b/lib/common/writer.c
@@ -145,9 +145,6 @@ int sqfs_writer_init(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *wrcfg)
 		goto fail_fragtbl;
 	}
 
-	memset(&sqfs->stats, 0, sizeof(sqfs->stats));
-	register_stat_hooks(sqfs->data, &sqfs->stats);
-
 	sqfs->idtbl = sqfs_id_table_create(0);
 	if (sqfs->idtbl == NULL) {
 		sqfs_perror(wrcfg->filename, "creating ID table",
@@ -294,7 +291,7 @@ int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg)
 	}
 
 	if (!cfg->quiet)
-		sqfs_print_statistics(&sqfs->super, &sqfs->stats);
+		sqfs_print_statistics(&sqfs->super, sqfs->data, sqfs->blkwr);
 
 	return 0;
 }
diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c
index bccbd79..5e350c8 100644
--- a/mkfs/mkfs.c
+++ b/mkfs/mkfs.c
@@ -40,7 +40,7 @@ static int set_working_dir(options_t *opt)
 }
 
 static int pack_files(sqfs_block_processor_t *data, fstree_t *fs,
-		      block_processor_stats_t *stats, options_t *opt)
+		      options_t *opt)
 {
 	sqfs_inode_generic_t *inode;
 	size_t max_blk_count;
@@ -126,9 +126,6 @@ static int pack_files(sqfs_block_processor_t *data, fstree_t *fs,
 
 		if (ret)
 			return -1;
-
-		stats->file_count += 1;
-		stats->bytes_read += filesize;
 	}
 
 	return 0;
@@ -234,7 +231,7 @@ int main(int argc, char **argv)
 	if (fstree_post_process(&sqfs.fs))
 		goto out;
 
-	if (pack_files(sqfs.data, &sqfs.fs, &sqfs.stats, &opt))
+	if (pack_files(sqfs.data, &sqfs.fs, &opt))
 		goto out;
 
 	if (sqfs_writer_finish(&sqfs, &opt.cfg))
diff --git a/tar/tar2sqfs.c b/tar/tar2sqfs.c
index b180739..856a1f4 100644
--- a/tar/tar2sqfs.c
+++ b/tar/tar2sqfs.c
@@ -305,9 +305,6 @@ static int write_file(tar_header_decoded_t *hdr, file_info_t *fi,
 	ret = write_data_from_file(hdr->name, sqfs.data, inode, file, 0);
 	file->destroy(file);
 
-	sqfs.stats.bytes_read += filesize;
-	sqfs.stats.file_count += 1;
-
 	if (ret)
 		return -1;
 
-- 
cgit v1.2.3