summaryrefslogtreecommitdiff
path: root/difftool/compare_dir.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-05 15:00:08 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2019-08-07 10:41:49 +0200
commit73b4ec8392541a27815bccbaeccbdf1cdd5e19dd (patch)
tree670e0b3f6e64ba7e957e29454e1f17261faf2ffe /difftool/compare_dir.c
parent1fff8f3a1326bd82f8140a61d969994e635834fe (diff)
Add a helper utility to compare filesystem trees
The intended use case is to compare two mounted or unpacke squashfs images, so a repacked test image can be compared against its original or an image unpacked with unsquashfs can be compared with an image unpacked by rdsquashfs or sqfs2tar. Since the tool is only intended to aid development (specifically automated testing), it is not installed by `make install`. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'difftool/compare_dir.c')
-rw-r--r--difftool/compare_dir.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/difftool/compare_dir.c b/difftool/compare_dir.c
new file mode 100644
index 0000000..8d4a186
--- /dev/null
+++ b/difftool/compare_dir.c
@@ -0,0 +1,97 @@
+/* SPDX-License-Identifier: GPL-3.0-or-later */
+/*
+ * compare_dir.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#include "difftool.h"
+
+int compare_dir_entries(tree_node_t *a, tree_node_t *b)
+{
+ tree_node_t *ait = a->data.dir->children, *aprev = NULL;
+ tree_node_t *bit = b->data.dir->children, *bprev = NULL;
+ int ret, result = 0;
+ char *path, arrow;
+
+ while (ait != NULL && bit != NULL) {
+ ret = strcmp(ait->name, bit->name);
+
+ if (ret < 0) {
+ result = 1;
+ path = node_path(ait);
+ if (path == NULL)
+ return -1;
+ fprintf(stdout, "< %s\n", path);
+ free(path);
+
+ if (aprev == NULL) {
+ a->data.dir->children = ait->next;
+ free(ait);
+ ait = a->data.dir->children;
+ } else {
+ aprev->next = ait->next;
+ free(ait);
+ ait = aprev->next;
+ }
+ } else if (ret > 0) {
+ result = 1;
+ path = node_path(bit);
+ if (path == NULL)
+ return -1;
+ fprintf(stdout, "> %s\n", path);
+ free(path);
+
+ if (bprev == NULL) {
+ b->data.dir->children = bit->next;
+ free(bit);
+ bit = b->data.dir->children;
+ } else {
+ bprev->next = bit->next;
+ free(bit);
+ bit = bprev->next;
+ }
+ } else {
+ aprev = ait;
+ ait = ait->next;
+
+ bprev = bit;
+ bit = bit->next;
+ }
+ }
+
+ if (ait != NULL || bit != NULL) {
+ result = 1;
+
+ if (ait != NULL) {
+ if (aprev == NULL) {
+ a->data.dir->children = NULL;
+ } else {
+ aprev->next = NULL;
+ }
+ arrow = '<';
+ } else {
+ if (bprev == NULL) {
+ b->data.dir->children = NULL;
+ } else {
+ bprev->next = NULL;
+ }
+ arrow = '>';
+ ait = bit;
+ }
+
+ while (ait != NULL) {
+ path = node_path(ait);
+ if (path == NULL) {
+ result = -1;
+ } else {
+ fprintf(stdout, "%c %s\n", arrow, path);
+ free(path);
+ }
+ aprev = ait;
+ ait = ait->next;
+ free(aprev);
+ }
+ }
+
+ return result;
+}