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

static unsigned char old_buf[MAX_WINDOW_SIZE];
static unsigned char new_buf[MAX_WINDOW_SIZE];

static int open_file(int dirfd, const char *prefix, const char *path)
{
	int fd = openat(dirfd, path, O_RDONLY);

	if (fd < 0) {
		fprintf(stderr, "open %s/%s: %s\n",
			prefix, path, strerror(errno));
	}

	return fd;
}

static int read_blob(const char *prefix, const char *path, bool is_dir,
		     int fd, data_reader_t *rd, file_info_t *fi, void *buffer,
		     off_t offset, size_t size)
{
	ssize_t ret;

	if (is_dir) {
		ret = read_data_at(path, offset, fd, buffer, size);
	} else {
		ret = data_reader_read(rd, fi, offset, buffer, size);
		ret = (ret < 0 || (size_t)ret < size) ? -1 : 0;
	}

	if (ret) {
		fprintf(stderr, "Failed to read %s from %s\n",
			path, prefix);
		return -1;
	}

	return 0;
}

int compare_files(sqfsdiff_t *sd, file_info_t *old, file_info_t *new,
		  const char *path)
{
	int old_fd = -1, new_fd = -1, status = 0, ret;
	uint64_t offset, diff;

	if (old->size != new->size)
		goto out_different;

	if (sd->compare_flags & COMPARE_NO_CONTENTS)
		return 0;

	if (sd->old_is_dir) {
		old_fd = open_file(sd->old_fd, sd->old_path, path);
		if (old_fd < 0)
			goto fail;
	}

	if (sd->new_is_dir) {
		new_fd = open_file(sd->new_fd, sd->new_path, path);
		if (new_fd < 0)
			goto fail;
	}

	for (offset = 0; offset < old->size; offset += diff) {
		diff = old->size - offset;

		if (diff > MAX_WINDOW_SIZE)
			diff = MAX_WINDOW_SIZE;

		ret = read_blob(sd->old_path, path, sd->old_is_dir, old_fd,
				sd->sqfs_old.data, old, old_buf, offset, diff);
		if (ret)
			goto fail;

		ret = read_blob(sd->new_path, path, sd->new_is_dir, new_fd,
				sd->sqfs_new.data, new, new_buf, offset, diff);
		if (ret)
			goto fail;

		if (memcmp(old_buf, new_buf, diff) != 0)
			goto out_different;
	}
out:
	if (old_fd >= 0)
		close(old_fd);
	if (new_fd >= 0)
		close(new_fd);
	return status;
fail:
	status = -1;
	goto out;
out_different:
	if (sd->compare_flags & COMPARE_EXTRACT_FILES) {
		if (extract_files(sd, old, new, path))
			goto fail;
	}
	status = 1;
	goto out;
}