aboutsummaryrefslogtreecommitdiff
path: root/unpack/restore_fstree.c
blob: 2ee4c08bd57b4074db24870c5be605297985e518 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * restore_fstree.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "rdsquashfs.h"

static int create_node(tree_node_t *n, int flags)
{
	tree_node_t *c;
	int fd, ret;
	char *name;

	if (!(flags & UNPACK_QUIET)) {
		name = fstree_get_path(n);
		printf("creating %s\n", name);
		free(name);
	}

	switch (n->mode & S_IFMT) {
	case S_IFDIR:
		if (mkdir(n->name, 0755) && errno != EEXIST) {
			fprintf(stderr, "mkdir %s: %s\n",
				n->name, strerror(errno));
			return -1;
		}

		if (pushd(n->name))
			return -1;

		for (c = n->data.dir->children; c != NULL; c = c->next) {
			if (create_node(c, flags))
				return -1;
		}

		if (popd())
			return -1;
		break;
	case S_IFLNK:
		if (symlink(n->data.slink_target, 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 (mknod(n->name, (n->mode & S_IFMT) | 0700, 0)) {
			fprintf(stderr, "creating %s: %s\n",
				n->name, strerror(errno));
			return -1;
		}
		break;
	case S_IFBLK:
	case S_IFCHR:
		if (mknod(n->name, n->mode & S_IFMT, n->data.devno)) {
			fprintf(stderr, "creating device %s: %s\n",
				n->name, strerror(errno));
			return -1;
		}
		break;
	case S_IFREG:
		fd = open(n->name, O_WRONLY | O_CREAT | O_EXCL, 0600);
		if (fd < 0) {
			fprintf(stderr, "creating %s: %s\n",
				n->name, strerror(errno));
			return -1;
		}

		close(fd);

		if (n->parent != NULL) {
			n->data.file->input_file = fstree_get_path(n);
		} else {
			n->data.file->input_file = strdup(n->name);
		}

		if (n->data.file->input_file == NULL) {
			perror("restoring file path");
			return -1;
		}

		ret = canonicalize_name(n->data.file->input_file);
		assert(ret == 0);
		break;
	default:
		break;
	}

	return 0;
}

static int set_attribs(fstree_t *fs, tree_node_t *n, int flags)
{
	tree_node_t *c;

	if (S_ISDIR(n->mode)) {
		if (pushd(n->name))
			return -1;

		for (c = n->data.dir->children; c != NULL; c = c->next) {
			if (set_attribs(fs, c, flags))
				return -1;
		}

		if (popd())
			return -1;
	}

#ifdef HAVE_SYS_XATTR_H
	if ((flags & UNPACK_SET_XATTR) && n->xattr != NULL) {
		size_t i, len, kidx, vidx;
		const char *key, *value;

		for (i = 0; i < n->xattr->num_attr; ++i) {
			kidx = n->xattr->attr[i].key_index;
			vidx = n->xattr->attr[i].value_index;

			key = str_table_get_string(&fs->xattr_keys, kidx);
			value = str_table_get_string(&fs->xattr_values, vidx);
			len = strlen(value);

			if (lsetxattr(n->name, key, value, len, 0)) {
				fprintf(stderr,
					"setting xattr '%s' on %s: %s\n",
					key, n->name, strerror(errno));
				return -1;
			}
		}
	}
#else
	(void)fs;
#endif

	if (flags & UNPACK_SET_TIMES) {
		struct timespec times[2];

		memset(times, 0, sizeof(times));
		times[0].tv_sec = n->mod_time;
		times[1].tv_sec = n->mod_time;

		if (utimensat(AT_FDCWD, n->name, times, AT_SYMLINK_NOFOLLOW)) {
			fprintf(stderr, "setting timestamp on %s: %s\n",
				n->name, strerror(errno));
			return -1;
		}
	}

	if (flags & UNPACK_CHOWN) {
		if (fchownat(AT_FDCWD, n->name, n->uid, n->gid,
			     AT_SYMLINK_NOFOLLOW)) {
			fprintf(stderr, "chown %s: %s\n",
				n->name, strerror(errno));
			return -1;
		}
	}

	if (flags & UNPACK_CHMOD) {
		if (fchmodat(AT_FDCWD, n->name, n->mode,
			     AT_SYMLINK_NOFOLLOW)) {
			fprintf(stderr, "chmod %s: %s\n",
				n->name, strerror(errno));
			return -1;
		}
	}
	return 0;
}

int restore_fstree(tree_node_t *root, int flags)
{
	tree_node_t *n, *old_parent;

	/* make sure fstree_get_path() stops at this node */
	old_parent = root->parent;
	root->parent = NULL;

	if (S_ISDIR(root->mode)) {
		for (n = root->data.dir->children; n != NULL; n = n->next) {
			if (create_node(n, flags))
				return -1;
		}
	} else {
		if (create_node(root, flags))
			return -1;
	}

	root->parent = old_parent;
	return 0;
}

int update_tree_attribs(fstree_t *fs, tree_node_t *root, int flags)
{
	if ((flags & (UNPACK_CHOWN | UNPACK_CHMOD | UNPACK_SET_TIMES)) == 0)
		return 0;

	return set_attribs(fs, root, flags);
}