aboutsummaryrefslogtreecommitdiff
path: root/difftool/node_compare.c
blob: 504c9cfbd7f91a651ac41ce2a674ce122bba50a9 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * node_compare.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "sqfsdiff.h"

int node_compare(sqfsdiff_t *sd, tree_node_t *a, tree_node_t *b)
{
	char *path = node_path(a);
	tree_node_t *ait, *bit;
	int ret, status = 0;

	if (path == NULL)
		return -1;

	if ((a->mode & S_IFMT) != (b->mode & S_IFMT)) {
		fprintf(stdout, "%s has a different type\n", path);
		free(path);
		return 1;
	}

	if (!(sd->compare_flags & COMPARE_NO_PERM)) {
		if ((a->mode & ~S_IFMT) != (b->mode & ~S_IFMT)) {
			fprintf(stdout, "%s has different permissions\n",
				path);
			status = 1;
		}
	}

	if (!(sd->compare_flags & COMPARE_NO_OWNER)) {
		if (a->uid != b->uid || a->gid != b->gid) {
			fprintf(stdout, "%s has different ownership\n", path);
			status = 1;
		}
	}

	if (sd->compare_flags & COMPARE_TIMESTAMP) {
		if (a->mod_time != b->mod_time) {
			fprintf(stdout, "%s has a different timestamp\n", path);
			status = 1;
		}
	}

	if (sd->compare_flags & COMPARE_INODE_NUM) {
		if (a->inode_num != b->inode_num) {
			fprintf(stdout, "%s has a different inode number\n",
				path);
			status = 1;
		}
	}

	switch (a->mode & S_IFMT) {
	case S_IFSOCK:
	case S_IFIFO:
		break;
	case S_IFBLK:
	case S_IFCHR:
		if (a->data.devno != b->data.devno) {
			fprintf(stdout, "%s has different device number\n",
				path);
			status = 1;
		}
		break;
	case S_IFLNK:
		if (strcmp(a->data.slink_target, b->data.slink_target) != 0) {
			fprintf(stdout, "%s has a different link target\n",
				path);
		}
		break;
	case S_IFDIR:
		ret = compare_dir_entries(sd, a, b);
		if (ret < 0) {
			status = -1;
			break;
		}
		if (ret > 0)
			status = 1;

		free(path);
		path = NULL;

		ait = a->data.dir->children;
		bit = b->data.dir->children;

		while (ait != NULL && bit != NULL) {
			ret = node_compare(sd, ait, bit);
			if (ret < 0)
				return -1;
			if (ret > 0)
				status = 1;

			ait = ait->next;
			bit = bit->next;
		}
		break;
	case S_IFREG:
		ret = compare_files(sd, a->data.file, b->data.file, path);
		if (ret < 0) {
			status = -1;
		} else if (ret > 0) {
			fprintf(stdout, "regular file %s differs\n", path);
			status = 1;
		}
		break;
	default:
		fprintf(stdout, "%s has unknown type, ignoring\n", path);
		break;
	}

	free(path);
	return status;
}