summaryrefslogtreecommitdiff
path: root/bin/rdsquashfs/restore_fstree.c
blob: bfe5d868fb9f3aabf2ac69d63131204e0abf311d (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * restore_fstree.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "rdsquashfs.h"

#ifdef _WIN32
static int create_node(const sqfs_tree_node_t *n, const char *name, int flags)
{
	WCHAR *wpath;
	HANDLE fh;
	(void)flags;

	wpath = path_to_windows(name);
	if (wpath == NULL)
		return -1;

	switch (n->inode->base.mode & S_IFMT) {
	case S_IFDIR:
		if (!CreateDirectoryW(wpath, NULL)) {
			if (GetLastError() != ERROR_ALREADY_EXISTS)
				goto fail;
		}
		break;
	case S_IFREG:
		fh = CreateFileW(wpath, GENERIC_READ,
				 FILE_SHARE_READ | FILE_SHARE_WRITE,
				 NULL, CREATE_NEW, 0, NULL);

		if (fh == INVALID_HANDLE_VALUE)
			goto fail;

		CloseHandle(fh);
		break;
	default:
		break;
	}

	free(wpath);
	return 0;
fail: {
	DWORD err = GetLastError();
	free(wpath);
	SetLastError(err);
	w32_perror(name);

	if (err == ERROR_FILE_EXISTS) {
		fputs("\nHINT: this could be caused by case "
		      "sensitivity on Windows.\n", stderr);
	}
	return -1;
}
}
#else
static int create_node(const sqfs_tree_node_t *n, const char *name, int flags)
{
	sqfs_u32 devno;
	int fd, mode;

	switch (n->inode->base.mode & S_IFMT) {
	case S_IFDIR:
		if (mkdir(name, 0755) && errno != EEXIST) {
			fprintf(stderr, "mkdir %s: %s\n",
				name, strerror(errno));
			return -1;
		}
		break;
	case S_IFLNK:
		if (symlink((const char *)n->inode->extra, name)) {
			fprintf(stderr, "ln -s %s %s: %s\n",
				(const char *)n->inode->extra, name,
				strerror(errno));
			return -1;
		}
		break;
	case S_IFSOCK:
	case S_IFIFO:
		if (mknod(name, (n->inode->base.mode & S_IFMT) | 0700, 0)) {
			fprintf(stderr, "creating %s: %s\n",
				name, strerror(errno));
			return -1;
		}
		break;
	case S_IFBLK:
	case S_IFCHR:
		if (n->inode->base.type == SQFS_INODE_EXT_BDEV ||
		    n->inode->base.type == SQFS_INODE_EXT_CDEV) {
			devno = n->inode->data.dev_ext.devno;
		} else {
			devno = n->inode->data.dev.devno;
		}

		if (mknod(name, n->inode->base.mode & S_IFMT, devno)) {
			fprintf(stderr, "creating device %s: %s\n",
				name, strerror(errno));
			return -1;
		}
		break;
	case S_IFREG:
		if (flags & UNPACK_CHMOD) {
			mode = (n->inode->base.mode & ~S_IFMT) | 0200;
		} else {
			mode = 0644;
		}

		fd = open(name, O_WRONLY | O_CREAT | O_EXCL, mode);

		if (fd < 0) {
			fprintf(stderr, "creating %s: %s\n",
				name, strerror(errno));
			return -1;
		}

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

	return 0;
}
#endif

static int create_node_dfs(const sqfs_tree_node_t *n, int flags)
{
	const sqfs_tree_node_t *c;
	char *name;
	int ret;

	if (!is_filename_sane((const char *)n->name)) {
		fprintf(stderr, "Found an entry named '%s', skipping.\n",
			n->name);
		return 0;
	}

	ret = sqfs_tree_node_get_path(n, &name);
	if (ret != 0) {
		sqfs_perror((const char *)n->name,
			    "constructing full path", ret);
		return -1;
	}

	ret = canonicalize_name(name);
	assert(ret == 0);

	if (!(flags & UNPACK_QUIET))
		printf("creating %s\n", name);

	ret = create_node(n, name, flags);
	sqfs_free(name);
	if (ret)
		return -1;

	if (S_ISDIR(n->inode->base.mode)) {
		for (c = n->children; c != NULL; c = c->next) {
			if (create_node_dfs(c, flags))
				return -1;
		}
	}
	return 0;
}

#ifdef HAVE_SYS_XATTR_H
static int set_xattr(const char *path, sqfs_xattr_reader_t *xattr,
		     const sqfs_tree_node_t *n)
{
	sqfs_xattr_value_t *value;
	sqfs_xattr_entry_t *key;
	sqfs_xattr_id_t desc;
	sqfs_u32 index;
	size_t i;
	int ret;

	sqfs_inode_get_xattr_index(n->inode, &index);

	if (index == 0xFFFFFFFF)
		return 0;

	if (sqfs_xattr_reader_get_desc(xattr, index, &desc)) {
		fputs("Error resolving xattr index\n", stderr);
		return -1;
	}

	if (sqfs_xattr_reader_seek_kv(xattr, &desc)) {
		fputs("Error locating xattr key-value pairs\n", stderr);
		return -1;
	}

	for (i = 0; i < desc.count; ++i) {
		if (sqfs_xattr_reader_read_key(xattr, &key)) {
			fputs("Error reading xattr key\n", stderr);
			return -1;
		}

		if (sqfs_xattr_reader_read_value(xattr, key, &value)) {
			fputs("Error reading xattr value\n", stderr);
			sqfs_free(key);
			return -1;
		}

		ret = lsetxattr(path, (const char *)key->key,
				value->value, value->size, 0);
		if (ret) {
			fprintf(stderr, "setting xattr '%s' on %s: %s\n",
				key->key, path, strerror(errno));
		}

		sqfs_free(key);
		sqfs_free(value);
		if (ret)
			return -1;
	}

	return 0;
}
#endif

static int set_attribs(sqfs_xattr_reader_t *xattr,
		       const sqfs_tree_node_t *n, int flags)
{
	const sqfs_tree_node_t *c;
	char *path;
	int ret;

	if (!is_filename_sane((const char *)n->name))
		return 0;

	if (S_ISDIR(n->inode->base.mode)) {
		for (c = n->children; c != NULL; c = c->next) {
			if (set_attribs(xattr, c, flags))
				return -1;
		}
	}

	ret = sqfs_tree_node_get_path(n, &path);
	if (ret != 0) {
		sqfs_perror(NULL, "reconstructing full path", ret);
		return -1;
	}

	ret = canonicalize_name(path);
	assert(ret == 0);

#ifdef HAVE_SYS_XATTR_H
	if ((flags & UNPACK_SET_XATTR) && xattr != NULL) {
		if (set_xattr(path, xattr, n))
			goto fail;
	}
#endif

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

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

		if (utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW)) {
			fprintf(stderr, "setting timestamp on %s: %s\n",
				path, strerror(errno));
			goto fail;
		}
	}
#endif
	if (flags & UNPACK_CHOWN) {
		if (fchownat(AT_FDCWD, path, n->uid, n->gid,
			     AT_SYMLINK_NOFOLLOW)) {
			fprintf(stderr, "chown %s: %s\n",
				path, strerror(errno));
			goto fail;
		}
	}

	if (flags & UNPACK_CHMOD && !S_ISLNK(n->inode->base.mode)) {
		if (fchmodat(AT_FDCWD, path,
			     n->inode->base.mode & ~S_IFMT, 0)) {
			fprintf(stderr, "chmod %s: %s\n",
				path, strerror(errno));
			goto fail;
		}
	}

	sqfs_free(path);
	return 0;
fail:
	sqfs_free(path);
	return -1;
}

int restore_fstree(sqfs_tree_node_t *root, int flags)
{
	sqfs_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->inode->base.mode)) {
		for (n = root->children; n != NULL; n = n->next) {
			if (create_node_dfs(n, flags))
				return -1;
		}
	} else {
		if (create_node_dfs(root, flags))
			return -1;
	}

	root->parent = old_parent;
	return 0;
}

int update_tree_attribs(sqfs_xattr_reader_t *xattr,
			const sqfs_tree_node_t *root, int flags)
{
	const sqfs_tree_node_t *n;

	if ((flags & (UNPACK_CHOWN | UNPACK_CHMOD |
		      UNPACK_SET_TIMES | UNPACK_SET_XATTR)) == 0) {
		return 0;
	}

	if (S_ISDIR(root->inode->base.mode)) {
		for (n = root->children; n != NULL; n = n->next) {
			if (set_attribs(xattr, n, flags))
				return -1;
		}
	} else {
		if (set_attribs(xattr, root, flags))
			return -1;
	}

	return 0;
}