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

static int extract(sqfs_data_reader_t *data, const sqfs_inode_generic_t *inode,
		   const char *prefix, const char *path, size_t block_size)
{
	char *ptr, *temp;
	sqfs_ostream_t *fp;
	sqfs_istream_t *in;
	int ret;

	temp = alloca(strlen(prefix) + strlen(path) + 2);
	sprintf(temp, "%s/%s", prefix, path);

	ptr = strrchr(temp, '/');
	if (ptr != NULL) {
		*ptr = '\0';
		if (mkdir_p(temp))
			return -1;
		*ptr = '/';
	}

	ret = sqfs_ostream_open_file(&fp, temp, SQFS_FILE_OPEN_OVERWRITE);
	if (ret) {
		sqfs_perror(temp, NULL, ret);
		return -1;
	}

	ret = sqfs_data_reader_create_stream(data, inode, path, &in);
	if (ret) {
		sqfs_drop(fp);
		return -1;
	}

	do {
		ret = sqfs_istream_splice(in, fp, block_size);
		if (ret < 0)
			sqfs_perror(path, "splicing data", ret);
	} while (ret > 0);

	sqfs_drop(in);

	if (ret == 0)
		ret = fp->flush(fp);

	if (ret) {
		sqfs_perror(fp->get_filename(fp), NULL, ret);
		sqfs_drop(fp);
		return -1;
	}

	sqfs_drop(fp);
	return 0;
}

int extract_files(sqfsdiff_t *sd, const sqfs_inode_generic_t *old,
		  const sqfs_inode_generic_t *new,
		  const char *path)
{
	if (old != NULL) {
		if (extract(sd->sqfs_old.data, old, "old",
			    path, sd->sqfs_old.super.block_size))
			return -1;
	}

	if (new != NULL) {
		if (extract(sd->sqfs_new.data, new, "new",
			    path, sd->sqfs_new.super.block_size))
			return -1;
	}

	return 0;
}