aboutsummaryrefslogtreecommitdiff
path: root/bin/sqfsdiff/node_compare.c
blob: d8f0878dda98e75589c134e7dd678ab4a7aefa97 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* 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, sqfs_tree_node_t *a, sqfs_tree_node_t *b)
{
	char *path = sqfs_tree_node_get_path(a);
	sqfs_tree_node_t *ait, *bit;
	bool promoted, demoted;
	int ret, status = 0;

	if (path == NULL) {
		perror("constructing absolute file path");
		return -1;
	}

	if (a->inode->base.type != b->inode->base.type) {
		promoted = demoted = false;

		switch (a->inode->base.type) {
		case SQFS_INODE_DIR:
			if (b->inode->base.type == SQFS_INODE_EXT_DIR)
				promoted = true;
			break;
		case SQFS_INODE_FILE:
			if (b->inode->base.type == SQFS_INODE_EXT_FILE)
				promoted = true;
			break;
		case SQFS_INODE_SLINK:
			if (b->inode->base.type == SQFS_INODE_EXT_SLINK)
				promoted = true;
			break;
		case SQFS_INODE_BDEV:
			if (b->inode->base.type == SQFS_INODE_EXT_BDEV)
				promoted = true;
			break;
		case SQFS_INODE_CDEV:
			if (b->inode->base.type == SQFS_INODE_EXT_CDEV)
				promoted = true;
			break;
		case SQFS_INODE_FIFO:
			if (b->inode->base.type == SQFS_INODE_EXT_FIFO)
				promoted = true;
			break;
		case SQFS_INODE_SOCKET:
			if (b->inode->base.type == SQFS_INODE_EXT_SOCKET)
				promoted = true;
			break;
		case SQFS_INODE_EXT_DIR:
			if (b->inode->base.type == SQFS_INODE_DIR)
				demoted = true;
			break;
		case SQFS_INODE_EXT_FILE:
			if (b->inode->base.type == SQFS_INODE_FILE)
				demoted = true;
			break;
		case SQFS_INODE_EXT_SLINK:
			if (b->inode->base.type == SQFS_INODE_SLINK)
				demoted = true;
			break;
		case SQFS_INODE_EXT_BDEV:
			if (b->inode->base.type == SQFS_INODE_BDEV)
				demoted = true;
			break;
		case SQFS_INODE_EXT_CDEV:
			if (b->inode->base.type == SQFS_INODE_CDEV)
				demoted = true;
			break;
		case SQFS_INODE_EXT_FIFO:
			if (b->inode->base.type == SQFS_INODE_FIFO)
				demoted = true;
			break;
		case SQFS_INODE_EXT_SOCKET:
			if (b->inode->base.type == SQFS_INODE_SOCKET)
				demoted = true;
			break;
		default:
			break;
		}

		if (promoted) {
			fprintf(stdout, "%s has an extended type\n", path);
			status = 1;
		} else if (demoted) {
			fprintf(stdout, "%s has a basic type\n", path);
			status = 1;
		} else {
			fprintf(stdout, "%s has a different type\n", path);
			free(path);
			return 1;
		}
	}

	if (!(sd->compare_flags & COMPARE_NO_PERM)) {
		if ((a->inode->base.mode & ~S_IFMT) !=
		    (b->inode->base.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->inode->base.mod_time != b->inode->base.mod_time) {
			fprintf(stdout, "%s has a different timestamp\n", path);
			status = 1;
		}
	}

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

	switch (a->inode->base.type) {
	case SQFS_INODE_SOCKET:
	case SQFS_INODE_EXT_SOCKET:
	case SQFS_INODE_FIFO:
	case SQFS_INODE_EXT_FIFO:
		break;
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
		if (a->inode->data.dev.devno != b->inode->data.dev.devno) {
			fprintf(stdout, "%s has different device number\n",
				path);
			status = 1;
		}
		break;
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
		if (a->inode->data.dev_ext.devno !=
		    b->inode->data.dev_ext.devno) {
			fprintf(stdout, "%s has different device number\n",
				path);
			status = 1;
		}
		break;
	case SQFS_INODE_SLINK:
	case SQFS_INODE_EXT_SLINK:
		if (strcmp((const char *)a->inode->extra,
			   (const char *)b->inode->extra)) {
			fprintf(stdout, "%s has a different link target\n",
				path);
		}
		break;
	case SQFS_INODE_DIR:
	case SQFS_INODE_EXT_DIR:
		ret = compare_dir_entries(sd, a, b);
		if (ret < 0) {
			status = -1;
			break;
		}
		if (ret > 0)
			status = 1;

		free(path);
		path = NULL;

		ait = a->children;
		bit = b->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 SQFS_INODE_FILE:
	case SQFS_INODE_EXT_FILE:
		ret = compare_files(sd, a->inode, b->inode, 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;
}