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

static sqfs_hard_link_t *links = NULL;
static unsigned int record_counter;

static sqfs_hard_link_t *find_hard_link(const char *name, sqfs_u32 inum)
{
	sqfs_hard_link_t *lnk = NULL;

	for (lnk = links; lnk != NULL; lnk = lnk->next) {
		if (lnk->inode_number == inum) {
			if (strcmp(name, lnk->target) == 0)
				lnk = NULL;
			break;
		}
	}

	return lnk;
}

static void inode_stat(const sqfs_tree_node_t *node, struct stat *sb)
{
	memset(sb, 0, sizeof(*sb));

	sb->st_mode = node->inode->base.mode;
	sb->st_uid = node->uid;
	sb->st_gid = node->gid;
	sb->st_mtime = node->inode->base.mod_time;

	switch (node->inode->base.type) {
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
		sb->st_rdev = node->inode->data.dev.devno;
		break;
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
		sb->st_rdev = node->inode->data.dev_ext.devno;
		break;
	case SQFS_INODE_SLINK:
		sb->st_size = node->inode->data.slink.target_size;
		break;
	case SQFS_INODE_EXT_SLINK:
		sb->st_size = node->inode->data.slink_ext.target_size;
		break;
	case SQFS_INODE_FILE:
		sb->st_size = node->inode->data.file.file_size;
		break;
	case SQFS_INODE_EXT_FILE:
		sb->st_size = node->inode->data.file_ext.file_size;
		break;
	case SQFS_INODE_DIR:
		sb->st_size = node->inode->data.dir.size;
		break;
	case SQFS_INODE_EXT_DIR:
		sb->st_size = node->inode->data.dir_ext.size;
		break;
	default:
		break;
	}
}

static int write_tree_dfs(const sqfs_tree_node_t *n)
{
	sqfs_hard_link_t *lnk = NULL;
	tar_xattr_t *xattr = NULL;
	char *name, *target;
	struct stat sb;
	size_t len;
	int ret;

	inode_stat(n, &sb);

	if (n->parent == NULL) {
		if (root_becomes == NULL)
			goto skip_hdr;

		len = strlen(root_becomes);
		name = malloc(len + 2);
		if (name == NULL) {
			perror("creating root directory");
			return -1;
		}

		memcpy(name, root_becomes, len);
		name[len] = '/';
		name[len + 1] = '\0';
	} else {
		if (!is_filename_sane((const char *)n->name, false)) {
			fprintf(stderr, "Found a file named '%s', skipping.\n",
				n->name);
			if (dont_skip) {
				fputs("Not allowed to skip files, aborting!\n",
				      stderr);
				return -1;
			}
			return 0;
		}

		name = sqfs_tree_node_get_path(n);
		if (name == NULL) {
			perror("resolving tree node path");
			return -1;
		}

		if (canonicalize_name(name))
			goto out_skip;

		name = assemble_tar_path(name, S_ISDIR(sb.st_mode));
		if (name == NULL)
			return -1;

		lnk = find_hard_link(name, n->inode->base.inode_number);
		if (lnk != NULL) {
			ret = write_hard_link(out_file, &sb, name, lnk->target,
					      record_counter++);
			free(name);
			return ret;
		}
	}

	if (!no_xattr) {
		if (get_xattrs(name, n->inode, &xattr)) {
			free(name);
			return -1;
		}
	}

	target = S_ISLNK(sb.st_mode) ? (char *)n->inode->extra : NULL;
	ret = write_tar_header(out_file, &sb, name, target, xattr,
			       record_counter++);
	free_xattr_list(xattr);

	if (ret > 0)
		goto out_skip;

	if (ret < 0) {
		free(name);
		return -1;
	}

	if (S_ISREG(sb.st_mode)) {
		if (sqfs_data_reader_dump(name, data, n->inode, out_file,
					  super.block_size)) {
			free(name);
			return -1;
		}

		if (padd_file(out_file, sb.st_size)) {
			free(name);
			return -1;
		}
	}

	free(name);
skip_hdr:
	for (n = n->children; n != NULL; n = n->next) {
		if (write_tree_dfs(n))
			return -1;
	}
	return 0;
out_skip:
	if (dont_skip) {
		fputs("Not allowed to skip files, aborting!\n", stderr);
		ret = -1;
	} else {
		fprintf(stderr, "Skipping %s\n", name);
		ret = 0;
	}
	free(name);
	return ret;
}

int write_tree(const sqfs_tree_node_t *n)
{
	sqfs_hard_link_t *lnk;
	int status = -1;

	if (!no_links) {
		if (sqfs_tree_find_hard_links(n, &links))
			return -1;

		for (lnk = links; lnk != NULL; lnk = lnk->next) {
			lnk->target = assemble_tar_path(lnk->target, false);

			if (lnk->target == NULL)
				goto out_links;
		}
	}

	status = write_tree_dfs(n);
out_links:
	while (links != NULL) {
		lnk = links;
		links = links->next;
		free(lnk->target);
		free(lnk);
	}
	return status;
}