summaryrefslogtreecommitdiff
path: root/difftool/sqfsdiff.c
blob: 05a7bfe5894840eacca73602b0a33bd374cdf49f (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * sqfsdiff.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "sqfsdiff.h"

int main(int argc, char **argv)
{
	int status, dirscan_flags = 0, ret = 0;
	sqfsdiff_t sd;

	memset(&sd, 0, sizeof(sd));
	process_options(&sd, argc, argv);

	if (sd.extract_dir != NULL) {
		if (mkdir_p(sd.extract_dir))
			return 2;
	}

	if (sd.compare_flags & COMPARE_TIMESTAMP)
		dirscan_flags |= DIR_SCAN_READ_XATTR;

	/* open first source */
	sd.old_fd = open(sd.old_path, O_DIRECTORY | O_PATH | O_RDONLY);
	if (sd.old_fd < 0) {
		if (errno == ENOTDIR) {
			sd.old_is_dir = false;

			if (sqfs_reader_open(&sd.sqfs_old, sd.old_path, 0))
				return 2;
		} else {
			perror(sd.old_path);
			return 2;
		}
	} else {
		sd.old_is_dir = true;
		if (fstree_init(&sd.sqfs_old.fs, 512, NULL))
			return 2;

		if (fstree_from_dir(&sd.sqfs_old.fs, sd.old_path,
				    dirscan_flags)) {
			fstree_cleanup(&sd.sqfs_old.fs);
			return 2;
		}

		tree_node_sort_recursive(sd.sqfs_old.fs.root);
	}

	/* open second source */
	sd.new_fd = open(sd.new_path, O_DIRECTORY | O_PATH | O_RDONLY);
	if (sd.new_fd < 0) {
		if (errno == ENOTDIR) {
			sd.new_is_dir = false;

			if (sqfs_reader_open(&sd.sqfs_new, sd.new_path, 0)) {
				status = 2;
				goto out_sqfs_old;
			}
		} else {
			status = 2;
			perror(sd.new_path);
			goto out_sqfs_old;
		}
	} else {
		sd.new_is_dir = true;
		if (fstree_init(&sd.sqfs_new.fs, 512, NULL)) {
			status = 2;
			goto out_sqfs_old;
		}

		if (fstree_from_dir(&sd.sqfs_new.fs, sd.new_path,
				    dirscan_flags)) {
			status = 2;
			fstree_cleanup(&sd.sqfs_old.fs);
			goto out_sqfs_old;
		}

		tree_node_sort_recursive(sd.sqfs_new.fs.root);
	}

	if (sd.extract_dir != NULL) {
		if (chdir(sd.extract_dir)) {
			perror(sd.extract_dir);
			ret = -1;
			goto out;
		}
	}

	ret = node_compare(&sd, sd.sqfs_old.fs.root, sd.sqfs_new.fs.root);
	if (ret != 0)
		goto out;

	if (sd.compare_super && !sd.old_is_dir && !sd.new_is_dir) {
		ret = compare_super_blocks(&sd.sqfs_old.super,
					   &sd.sqfs_new.super);
		if (ret != 0)
			goto out;
	}
out:
	if (ret < 0) {
		status = 2;
	} else if (ret > 0) {
		status = 1;
	} else {
		status = 0;
	}
	if (sd.new_is_dir) {
		close(sd.new_fd);
		fstree_cleanup(&sd.sqfs_new.fs);
	} else {
		sqfs_reader_close(&sd.sqfs_new);
	}
out_sqfs_old:
	if (sd.old_is_dir) {
		close(sd.old_fd);
		fstree_cleanup(&sd.sqfs_old.fs);
	} else {
		sqfs_reader_close(&sd.sqfs_old);
	}
	return status;
}