aboutsummaryrefslogtreecommitdiff
path: root/difftool/compare_dir.c
blob: 9ef2ba5eb4f0d8d122d43c81255343861dd2a959 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * compare_dir.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "sqfsdiff.h"

int compare_dir_entries(sqfsdiff_t *sd, tree_node_t *old, tree_node_t *new)
{
	tree_node_t *old_it = old->data.dir->children, *old_prev = NULL;
	tree_node_t *new_it = new->data.dir->children, *new_prev = NULL;
	int ret, result = 0;
	char *path;

	while (old_it != NULL || new_it != NULL) {
		if (old_it != NULL && new_it != NULL) {
			ret = strcmp(old_it->name, new_it->name);
		} else if (old_it == NULL) {
			ret = 1;
		} else {
			ret = -1;
		}

		if (ret < 0) {
			result = 1;
			path = node_path(old_it);
			if (path == NULL)
				return -1;

			if ((sd->compare_flags & COMPARE_EXTRACT_FILES) &&
			    S_ISREG(old_it->mode)) {
				if (extract_files(sd, old_it->data.file,
						  NULL, path)) {
					return -1;
				}
			}

			fprintf(stdout, "< %s\n", path);
			free(path);

			if (old_prev == NULL) {
				old->data.dir->children = old_it->next;
				free(old_it);
				old_it = old->data.dir->children;
			} else {
				old_prev->next = old_it->next;
				free(old_it);
				old_it = old_prev->next;
			}
		} else if (ret > 0) {
			result = 1;
			path = node_path(new_it);
			if (path == NULL)
				return -1;

			if ((sd->compare_flags & COMPARE_EXTRACT_FILES) &&
			    S_ISREG(new_it->mode)) {
				if (extract_files(sd, NULL, new_it->data.file,
						  path)) {
					return -1;
				}
			}

			fprintf(stdout, "> %s\n", path);
			free(path);

			if (new_prev == NULL) {
				new->data.dir->children = new_it->next;
				free(new_it);
				new_it = new->data.dir->children;
			} else {
				new_prev->next = new_it->next;
				free(new_it);
				new_it = new_prev->next;
			}
		} else {
			old_prev = old_it;
			old_it = old_it->next;

			new_prev = new_it;
			new_it = new_it->next;
		}
	}

	return result;
}