summaryrefslogtreecommitdiff
path: root/unpack/restore_fstree.c
blob: 56c3cc547cd0e5d496624cc720b912274f42d0c3 (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 */
#include "unsquashfs.h"

static int restore_directory(int dirfd, tree_node_t *n, compressor_t *cmp,
			     size_t block_size, frag_reader_t *frag,
			     int sqfsfd)
{
	int fd;

	for (n = n->data.dir->children; n != NULL; n = n->next) {
		switch (n->mode & S_IFMT) {
		case S_IFDIR:
			if (mkdirat(dirfd, n->name, n->mode) &&
			    errno != EEXIST) {
				fprintf(stderr, "mkdir %s: %s\n",
					n->name, strerror(errno));
				return -1;
			}

			fd = openat(dirfd, n->name, O_RDONLY | O_DIRECTORY);
			if (fd < 0) {
				fprintf(stderr, "open dir %s: %s\n",
					n->name, strerror(errno));
				return -1;
			}

			if (restore_directory(fd, n, cmp, block_size,
					      frag, sqfsfd)) {
				close(fd);
				return -1;
			}

			close(fd);
			break;
		case S_IFLNK:
			if (symlinkat(n->data.slink_target, dirfd, n->name)) {
				fprintf(stderr, "ln -s %s %s: %s\n",
					n->data.slink_target, n->name,
					strerror(errno));
				return -1;
			}
			break;
		case S_IFSOCK:
		case S_IFIFO:
			if (mknodat(dirfd, n->name, n->mode, 0)) {
				fprintf(stderr, "creating %s: %s\n",
					n->name, strerror(errno));
				return -1;
			}
			break;
		case S_IFBLK:
		case S_IFCHR:
			if (mknodat(dirfd, n->name, n->mode, n->data.devno)) {
				fprintf(stderr, "creating device %s: %s\n",
					n->name, strerror(errno));
				return -1;
			}
			break;
		case S_IFREG:
			fd = openat(dirfd, n->name,
				    O_WRONLY | O_CREAT | O_EXCL, n->mode);
			if (fd < 0) {
				fprintf(stderr, "creating %s: %s\n",
					n->name, strerror(errno));
				return -1;
			}

			if (extract_file(n->data.file, cmp, block_size,
					 frag, sqfsfd, fd)) {
				close(fd);
				return -1;
			}

			close(fd);
			break;
		default:
			break;
		}
	}

	return 0;
}

int restore_fstree(const char *rootdir, tree_node_t *root, compressor_t *cmp,
		   size_t block_size, frag_reader_t *frag, int sqfsfd)
{
	int dirfd;

	if (mkdir_p(rootdir))
		return -1;

	dirfd = open(rootdir, O_RDONLY | O_DIRECTORY);
	if (dirfd < 0) {
		perror(rootdir);
		return -1;
	}

	if (restore_directory(dirfd, root, cmp, block_size, frag, sqfsfd)) {
		close(dirfd);
		return -1;
	}

	close(dirfd);
	return 0;
}