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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "rdsquashfs.h"
static int should_skip(int type, int flags)
{
switch (type) {
case SQFS_INODE_BDEV:
case SQFS_INODE_CDEV:
case SQFS_INODE_EXT_CDEV:
case SQFS_INODE_EXT_BDEV:
return (flags & UNPACK_NO_DEVICES);
case SQFS_INODE_SLINK:
case SQFS_INODE_EXT_SLINK:
return (flags & UNPACK_NO_SLINKS);
case SQFS_INODE_SOCKET:
case SQFS_INODE_EXT_SOCKET:
return(flags & UNPACK_NO_SOCKETS);
case SQFS_INODE_FIFO:
case SQFS_INODE_EXT_FIFO:
return (flags & UNPACK_NO_FIFO);
}
return 0;
}
static int fill_dir(meta_reader_t *ir, meta_reader_t *dr, tree_node_t *root,
sqfs_super_t *super, id_table_t *idtbl, int flags)
{
sqfs_inode_generic_t *inode;
sqfs_dir_header_t hdr;
sqfs_dir_entry_t *ent;
tree_node_t *n, *prev;
uint64_t block_start;
size_t size, diff;
uint32_t i;
block_start = root->data.dir->start_block;
block_start += super->directory_table_start;
if (meta_reader_seek(dr, block_start, root->data.dir->block_offset))
return -1;
size = root->data.dir->size;
while (size != 0) {
if (meta_reader_read_dir_header(dr, &hdr))
return -1;
size -= sizeof(hdr) > size ? size : sizeof(hdr);
for (i = 0; i <= hdr.count; ++i) {
ent = meta_reader_read_dir_ent(dr);
if (ent == NULL)
return -1;
diff = sizeof(*ent) + strlen((char *)ent->name);
size -= diff > size ? size : diff;
if (should_skip(ent->type, flags)) {
free(ent);
continue;
}
inode = meta_reader_read_inode(ir, super,
hdr.start_block,
ent->offset);
if (inode == NULL) {
free(ent);
return -1;
}
n = tree_node_from_inode(inode, idtbl,
(char *)ent->name,
super->block_size);
free(ent);
free(inode);
if (n == NULL)
return -1;
n->parent = root;
n->next = root->data.dir->children;
root->data.dir->children = n;
}
}
n = root->data.dir->children;
prev = NULL;
while (n != NULL) {
if (S_ISDIR(n->mode)) {
if (fill_dir(ir, dr, n, super, idtbl, flags))
return -1;
if (n->data.dir->children == NULL &&
(flags & UNPACK_NO_EMPTY)) {
if (prev == NULL) {
root->data.dir->children = n->next;
free(n);
n = root->data.dir->children;
} else {
prev->next = n->next;
free(n);
n = prev->next;
}
continue;
}
}
prev = n;
n = n->next;
}
return 0;
}
int read_fstree(fstree_t *out, int fd, sqfs_super_t *super, compressor_t *cmp,
int flags)
{
sqfs_inode_generic_t *root;
meta_reader_t *ir, *dr;
uint64_t block_start;
id_table_t idtbl;
int status = -1;
size_t offset;
ir = meta_reader_create(fd, cmp);
if (ir == NULL)
return -1;
dr = meta_reader_create(fd, cmp);
if (dr == NULL)
goto out_ir;
if (id_table_init(&idtbl))
goto out_dr;
if (id_table_read(&idtbl, fd, super, cmp))
goto out_id;
block_start = super->root_inode_ref >> 16;
offset = super->root_inode_ref & 0xFFFF;
root = meta_reader_read_inode(ir, super, block_start, offset);
if (root == NULL)
goto out_id;
if (root->base.type != SQFS_INODE_DIR &&
root->base.type != SQFS_INODE_EXT_DIR) {
free(root);
fputs("File system root inode is not a directory inode!\n",
stderr);
goto out_id;
}
memset(out, 0, sizeof(*out));
out->block_size = super->block_size;
out->default_uid = 0;
out->default_gid = 0;
out->default_mode = 0755;
out->default_mtime = super->modification_time;
out->root = tree_node_from_inode(root, &idtbl, "", super->block_size);
free(root);
root = NULL;
if (out->root == NULL)
goto out_id;
if (fill_dir(ir, dr, out->root, super, &idtbl, flags))
goto fail_fs;
fstree_sort(out);
status = 0;
out_id:
id_table_cleanup(&idtbl);
out_dr:
meta_reader_destroy(dr);
out_ir:
meta_reader_destroy(ir);
return status;
fail_fs:
fstree_cleanup(out);
goto out_id;
}
|