aboutsummaryrefslogtreecommitdiff
path: root/lib/fstree
diff options
context:
space:
mode:
Diffstat (limited to 'lib/fstree')
-rw-r--r--lib/fstree/Makemodule.am3
-rw-r--r--lib/fstree/src/stats.c49
2 files changed, 51 insertions, 1 deletions
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);
+}