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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* process_tarball.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "tar2sqfs.h"
static int write_file(sqfs_writer_t *sqfs, dir_iterator_t *it,
const dir_entry_t *ent, tree_node_t *n)
{
int flags = 0, ret = 0;
sqfs_ostream_t *out;
sqfs_istream_t *in;
if (no_tail_pack && ent->size > cfg.block_size)
flags |= SQFS_BLK_DONT_FRAGMENT;
out = data_writer_ostream_create(ent->name, sqfs->data,
&(n->data.file.inode), flags);
if (out == NULL)
return -1;
ret = it->open_file_ro(it, &in);
if (ret != 0) {
sqfs_drop(out);
return ret;
}
do {
ret = sqfs_istream_splice(in, out, cfg.block_size);
} while (ret > 0);
out->flush(out);
sqfs_drop(out);
sqfs_drop(in);
return ret;
}
static int copy_xattr(sqfs_writer_t *sqfs, const char *filename,
tree_node_t *node, dir_iterator_t *it)
{
sqfs_xattr_t *xattr, *list;
int ret;
ret = it->read_xattr(it, &list);
if (ret) {
sqfs_perror(filename, "reading xattrs", ret);
return -1;
}
ret = sqfs_xattr_writer_begin(sqfs->xwr, 0);
if (ret) {
sqfs_perror(filename, "beginning xattr block", ret);
goto fail;
}
for (xattr = list; xattr != NULL; xattr = xattr->next) {
ret = sqfs_xattr_writer_add(sqfs->xwr, xattr);
if (ret == SQFS_ERROR_UNSUPPORTED) {
fprintf(stderr, "%s: squashfs does not "
"support xattr prefix of %s\n",
dont_skip ? "ERROR" : "WARNING",
xattr->key);
if (dont_skip)
goto fail;
continue;
}
if (ret) {
sqfs_perror(filename, "storing xattr key-value pair",
ret);
goto fail;
}
}
ret = sqfs_xattr_writer_end(sqfs->xwr, &node->xattr_idx);
if (ret) {
sqfs_perror(filename, "completing xattr block", ret);
goto fail;
}
sqfs_xattr_list_free(list);
return 0;
fail:
sqfs_xattr_list_free(list);
return -1;
}
static int create_node_and_repack_data(sqfs_writer_t *sqfs, dir_iterator_t *it,
const dir_entry_t *ent, const char *link)
{
tree_node_t *node;
node = fstree_add_generic(&sqfs->fs, ent, link);
if (node == NULL)
goto fail_errno;
if (!cfg.quiet) {
if (ent->flags & DIR_ENTRY_FLAG_HARD_LINK) {
printf("Hard link %s -> %s\n", ent->name, link);
} else {
printf("Packing %s\n", ent->name);
}
}
if (!cfg.no_xattr) {
if (copy_xattr(sqfs, ent->name, node, it))
return -1;
}
if (S_ISREG(ent->mode)) {
int ret = write_file(sqfs, it, ent, node);
if (ret != 0) {
sqfs_perror(ent->name, "packing data", ret);
return -1;
}
}
return 0;
fail_errno:
perror(ent->name);
return -1;
}
static int set_root_attribs(sqfs_writer_t *sqfs, dir_iterator_t *it,
const dir_entry_t *ent)
{
if ((ent->flags & DIR_ENTRY_FLAG_HARD_LINK) || !S_ISDIR(ent->mode)) {
fprintf(stderr, "'%s' is not a directory!\n", ent->name);
return -1;
}
sqfs->fs.root->uid = ent->uid;
sqfs->fs.root->gid = ent->gid;
sqfs->fs.root->mode = ent->mode;
if (keep_time)
sqfs->fs.root->mod_time = ent->mtime;
if (!cfg.no_xattr) {
if (copy_xattr(sqfs, "/", sqfs->fs.root, it))
return -1;
}
return 0;
}
int process_tarball(dir_iterator_t *it, sqfs_writer_t *sqfs)
{
size_t rootlen = root_becomes == NULL ? 0 : strlen(root_becomes);
for (;;) {
bool skip = false, is_root = false, is_prefixed = true;
dir_entry_t *ent = NULL;
char *link = NULL;
int ret;
ret = it->next(it, &ent);
if (ret > 0)
break;
if (ret < 0)
return -1;
if (ent->mtime < 0)
ent->mtime = 0;
if ((sqfs_u64)ent->mtime > 0x0FFFFFFFFUL)
ent->mtime = 0x0FFFFFFFFUL;
if (S_ISLNK(ent->mode)) {
ret = it->read_link(it, &link);
if (ret != 0) {
sqfs_perror(ent->name, "read link", ret);
free(ent);
return -1;
}
}
if (root_becomes != NULL) {
if (strncmp(ent->name, root_becomes, rootlen) == 0) {
if (ent->name[rootlen] == '\0') {
is_root = true;
} else if (ent->name[rootlen] != '/') {
is_prefixed = false;
}
} else {
is_prefixed = false;
}
if (!is_prefixed) {
free(ent);
free(link);
continue;
}
if (!is_root) {
memmove(ent->name, ent->name + rootlen + 1,
strlen(ent->name + rootlen + 1) + 1);
}
if (link != NULL &&
((ent->flags & DIR_ENTRY_FLAG_HARD_LINK) ||
!no_symlink_retarget)) {
if (canonicalize_name(link) == 0 &&
!strncmp(link, root_becomes, rootlen) &&
link[rootlen] == '/') {
memmove(link, link + rootlen,
strlen(link + rootlen) + 1);
}
}
} else if (ent->name[0] == '\0') {
is_root = true;
}
if (!keep_time)
ent->mtime = sqfs->fs.defaults.mtime;
if (is_root) {
ret = set_root_attribs(sqfs, it, ent);
} else if (skip) {
ret = dont_skip ? -1 : 0;
} else {
ret = create_node_and_repack_data(sqfs, it, ent, link);
}
free(ent);
free(link);
if (ret)
return -1;
}
return 0;
}
|