aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-20 18:43:43 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-04-20 18:44:15 +0200
commit2831c51d3386d12a97a6ce5f3d795cb51e02beec (patch)
treecb8f2f5f885fd59d6a031adeea267f219f41c228
parentc5f0334e87deea2b54f33f62ee879455a814633c (diff)
Collect and print statistics about the kind of files we are packing
Using depth-first search, we collect some crude statistics about directory tree types (e.g. regular files, directories, device special files and so on) and print them out after serializing the tree. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
-rw-r--r--include/fstree.h12
-rw-r--r--lib/common/src/writer/finish.c21
-rw-r--r--lib/fstree/Makemodule.am3
-rw-r--r--lib/fstree/src/stats.c49
4 files changed, 81 insertions, 4 deletions
diff --git a/include/fstree.h b/include/fstree.h
index d08ccae..96ddf8f 100644
--- a/include/fstree.h
+++ b/include/fstree.h
@@ -21,6 +21,7 @@
typedef struct fstree_defaults_t fstree_defaults_t;
typedef struct tree_node_t tree_node_t;
typedef struct fstree_t fstree_t;
+typedef struct fstree_stats_t fstree_stats_t;
enum {
FLAG_DIR_CREATED_IMPLICITLY = 0x01,
@@ -80,6 +81,15 @@ struct tree_node_t {
sqfs_u8 payload[];
};
+struct fstree_stats_t {
+ size_t num_ipc;
+ size_t num_links;
+ size_t num_slinks;
+ size_t num_files;
+ size_t num_devices;
+ size_t num_dirs;
+};
+
/* Default settings for new nodes */
struct fstree_defaults_t {
sqfs_u32 uid;
@@ -201,4 +211,6 @@ tree_node_t *fstree_add_hard_link(fstree_t *fs, const char *path,
*/
int fstree_resolve_hard_links(fstree_t *fs);
+void fstree_collect_stats(const fstree_t *fs, fstree_stats_t *out);
+
#endif /* FSTREE_H */
diff --git a/lib/common/src/writer/finish.c b/lib/common/src/writer/finish.c
index c539579..60b3aa5 100644
--- a/lib/common/src/writer/finish.c
+++ b/lib/common/src/writer/finish.c
@@ -11,7 +11,8 @@
static void print_statistics(const sqfs_super_t *super,
const sqfs_block_processor_t *blk,
- const sqfs_block_writer_t *wr)
+ const sqfs_block_writer_t *wr,
+ const fstree_stats_t *fs_stat)
{
const sqfs_block_processor_stats_t *proc_stats;
sqfs_u64 bytes_written, blocks_written;
@@ -38,6 +39,14 @@ static void print_statistics(const sqfs_super_t *super,
printf("Data compression ratio: " PRI_SZ "%%\n", ratio);
fputc('\n', stdout);
+ printf("Files: " PRI_SZ "\n", fs_stat->num_files);
+ printf("Directories: " PRI_SZ "\n", fs_stat->num_dirs);
+ printf("IPC files: " PRI_SZ "\n", fs_stat->num_ipc);
+ printf("Device files: " PRI_SZ "\n", fs_stat->num_devices);
+ printf("Hard links: " PRI_SZ "\n", fs_stat->num_links);
+ printf("Soft links: " PRI_SZ "\n", fs_stat->num_slinks);
+ fputc('\n', stdout);
+
printf("Data blocks written: " PRI_U64 "\n", blocks_written);
printf("Out of which were fragment blocks: " PRI_U64 "\n",
proc_stats->frag_block_count);
@@ -169,8 +178,14 @@ int sqfs_writer_finish(sqfs_writer_t *sqfs, const sqfs_writer_cfg_t *cfg)
return -1;
}
- if (!cfg->quiet)
- print_statistics(&sqfs->super, sqfs->data, sqfs->blkwr);
+ if (!cfg->quiet) {
+ fstree_stats_t fs_stat;
+
+ fstree_collect_stats(&sqfs->fs, &fs_stat);
+
+ print_statistics(&sqfs->super, sqfs->data,
+ sqfs->blkwr, &fs_stat);
+ }
return 0;
}
diff --git a/lib/fstree/Makemodule.am b/lib/fstree/Makemodule.am
index 4d553f4..833fe51 100644
--- a/lib/fstree/Makemodule.am
+++ b/lib/fstree/Makemodule.am
@@ -1,7 +1,8 @@
libfstree_a_SOURCES = include/fstree.h lib/fstree/src/fstree.c \
lib/fstree/src/post_process.c lib/fstree/src/get_path.c \
lib/fstree/src/mknode.c lib/fstree/src/hardlink.c \
- lib/fstree/src/add_by_path.c lib/fstree/src/get_by_path.c
+ lib/fstree/src/add_by_path.c lib/fstree/src/get_by_path.c \
+ lib/fstree/src/stats.c
noinst_LIBRARIES += libfstree.a
diff --git a/lib/fstree/src/stats.c b/lib/fstree/src/stats.c
new file mode 100644
index 0000000..2dbf043
--- /dev/null
+++ b/lib/fstree/src/stats.c
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * stats.c
+ *
+ * Copyright (C) 2023 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "config.h"
+
+#include "fstree.h"
+#include <string.h>
+
+static void count_dfs(const tree_node_t *n, fstree_stats_t *out)
+{
+ switch (n->mode & S_IFMT) {
+ case S_IFSOCK:
+ case S_IFIFO:
+ out->num_ipc += 1;
+ break;
+ case S_IFLNK:
+ if (n->flags & FLAG_LINK_IS_HARD) {
+ out->num_links += 1;
+ } else {
+ out->num_slinks += 1;
+ }
+ break;
+ case S_IFREG:
+ out->num_files += 1;
+ break;
+ case S_IFBLK:
+ case S_IFCHR:
+ out->num_devices += 1;
+ break;
+ case S_IFDIR:
+ out->num_dirs += 1;
+ for (n = n->data.children; n != NULL; n = n->next) {
+ count_dfs(n, out);
+ }
+ break;
+ default:
+ break;
+ }
+}
+
+void fstree_collect_stats(const fstree_t *fs, fstree_stats_t *out)
+{
+ memset(out, 0, sizeof(*out));
+
+ count_dfs(fs->root, out);
+}