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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* read_tree.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "util/util.h"
#include "common.h"
#include <string.h>
#include <stdlib.h>
static int should_skip(int type, unsigned int flags)
{
switch (type) {
case SQFS_INODE_BDEV:
case SQFS_INODE_CDEV:
case SQFS_INODE_EXT_CDEV:
case SQFS_INODE_EXT_BDEV:
return (flags & SQFS_TREE_NO_DEVICES);
case SQFS_INODE_SLINK:
case SQFS_INODE_EXT_SLINK:
return (flags & SQFS_TREE_NO_SLINKS);
case SQFS_INODE_SOCKET:
case SQFS_INODE_EXT_SOCKET:
return(flags & SQFS_TREE_NO_SOCKETS);
case SQFS_INODE_FIFO:
case SQFS_INODE_EXT_FIFO:
return (flags & SQFS_TREE_NO_FIFO);
default:
break;
}
return 0;
}
static bool would_be_own_parent(sqfs_tree_node_t *parent, sqfs_tree_node_t *n)
{
sqfs_u32 inum = n->inode->base.inode_number;
while (parent != NULL) {
if (parent->inode->base.inode_number == inum)
return true;
parent = parent->parent;
}
return false;
}
static sqfs_tree_node_t *create_node(sqfs_inode_generic_t *inode,
const char *name)
{
sqfs_tree_node_t *n;
n = alloc_flex(sizeof(*n), 1, strlen(name) + 1);
if (n == NULL)
return NULL;
n->inode = inode;
strcpy((char *)n->name, name);
return n;
}
static int fill_dir(sqfs_dir_reader_t *dr, sqfs_tree_node_t *root,
sqfs_dir_reader_state_t *state,
unsigned int flags)
{
sqfs_tree_node_t *n, *prev, **tail;
sqfs_inode_generic_t *inode;
sqfs_dir_node_t *ent;
int err;
tail = &root->children;
for (;;) {
err = sqfs_dir_reader_read(dr, state, &ent);
if (err > 0)
break;
if (err < 0)
return err;
if (should_skip(ent->type, flags)) {
free(ent);
continue;
}
err = sqfs_dir_reader_get_inode(dr, state->ent_ref, &inode);
if (err) {
free(ent);
return err;
}
n = create_node(inode, (const char *)ent->name);
free(ent);
if (n == NULL) {
free(inode);
return SQFS_ERROR_ALLOC;
}
if (would_be_own_parent(root, n)) {
free(n);
free(inode);
return SQFS_ERROR_LINK_LOOP;
}
*tail = n;
tail = &n->next;
n->parent = root;
}
n = root->children;
prev = NULL;
while (n != NULL) {
if (n->inode->base.type == SQFS_INODE_DIR ||
n->inode->base.type == SQFS_INODE_EXT_DIR) {
sqfs_dir_reader_state_t nstate;
if (!(flags & SQFS_TREE_NO_RECURSE)) {
err = sqfs_dir_reader_open_dir(dr, n->inode,
&nstate,
SQFS_DIR_OPEN_NO_DOT_ENTRIES);
if (err)
return err;
err = fill_dir(dr, n, &nstate, flags);
if (err)
return err;
}
if (n->children == NULL &&
(flags & SQFS_TREE_NO_EMPTY)) {
free(n->inode);
if (prev == NULL) {
root->children = root->children->next;
free(n);
n = root->children;
} else {
prev->next = n->next;
free(n);
n = prev->next;
}
continue;
}
}
prev = n;
n = n->next;
}
return 0;
}
static int resolve_ids(sqfs_tree_node_t *root, const sqfs_id_table_t *idtbl)
{
sqfs_tree_node_t *it;
int err;
for (it = root->children; it != NULL; it = it->next)
resolve_ids(it, idtbl);
err = sqfs_id_table_index_to_id(idtbl, root->inode->base.uid_idx,
&root->uid);
if (err)
return err;
return sqfs_id_table_index_to_id(idtbl, root->inode->base.gid_idx,
&root->gid);
}
int sqfs_dir_reader_get_full_hierarchy(sqfs_dir_reader_t *rd,
const sqfs_id_table_t *idtbl,
const char *path, unsigned int flags,
sqfs_tree_node_t **out)
{
sqfs_tree_node_t *root, *tail, *new;
sqfs_inode_generic_t *inode;
sqfs_dir_node_t *ent;
const char *ptr;
int ret;
if (flags & ~SQFS_TREE_ALL_FLAGS)
return SQFS_ERROR_UNSUPPORTED;
ret = sqfs_dir_reader_get_root_inode(rd, &inode);
if (ret)
return ret;
root = tail = create_node(inode, "");
if (root == NULL) {
free(inode);
return SQFS_ERROR_ALLOC;
}
inode = NULL;
while (path != NULL && *path != '\0') {
sqfs_dir_reader_state_t state;
if (*path == '/') {
while (*path == '/')
++path;
continue;
}
ret = sqfs_dir_reader_open_dir(rd, tail->inode, &state,
SQFS_DIR_OPEN_NO_DOT_ENTRIES);
if (ret)
goto fail;
ptr = strchrnul(path, '/');
for (;;) {
ret = sqfs_dir_reader_read(rd, &state, &ent);
if (ret < 0)
goto fail;
if (ret > 0) {
ret = SQFS_ERROR_NO_ENTRY;
goto fail;
}
ret = strncmp((const char *)ent->name,
path, ptr - path);
if (ret == 0 && ent->name[ptr - path] == '\0')
break;
free(ent);
}
ret = sqfs_dir_reader_get_inode(rd, state.ent_ref, &inode);
if (ret) {
free(ent);
goto fail;
}
new = create_node(inode, (const char *)ent->name);
free(ent);
if (new == NULL) {
free(inode);
ret = SQFS_ERROR_ALLOC;
goto fail;
}
inode = NULL;
path = ptr;
if (flags & SQFS_TREE_STORE_PARENTS) {
tail->children = new;
new->parent = tail;
tail = new;
} else {
sqfs_dir_tree_destroy(root);
root = tail = new;
}
}
if (tail->inode->base.type == SQFS_INODE_DIR ||
tail->inode->base.type == SQFS_INODE_EXT_DIR) {
sqfs_dir_reader_state_t state;
ret = sqfs_dir_reader_open_dir(rd, tail->inode, &state,
SQFS_DIR_OPEN_NO_DOT_ENTRIES);
if (ret)
goto fail;
ret = fill_dir(rd, tail, &state, flags);
if (ret)
goto fail;
}
ret = resolve_ids(root, idtbl);
if (ret)
goto fail;
*out = root;
return 0;
fail:
sqfs_dir_tree_destroy(root);
return ret;
}
|