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
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* fstree_from_file.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "mkfs.h"
static int read_u32(const char *str, sqfs_u32 *out, sqfs_u32 base)
{
*out = 0;
if (!isdigit(*str))
return -1;
while (isdigit(*str)) {
sqfs_u32 x = *(str++) - '0';
if (x >= base || (*out) > (0xFFFFFFFF - x) / base)
return -1;
(*out) = (*out) * base + x;
}
if (*str != '\0')
return -1;
return 0;
}
static int add_generic(fstree_t *fs, const char *filename, size_t line_num,
sqfs_dir_entry_t *ent, split_line_t *line)
{
const char *msg = NULL, *arg = line->count > 0 ? line->args[0] : NULL;
if (line->count > 1) {
msg = "too many arguments";
goto fail;
}
if (fstree_add_generic(fs, ent, arg) == NULL) {
msg = strerror(errno);
goto fail;
}
return 0;
fail:
fprintf(stderr, "%s: " PRI_SZ ": %s: %s\n",
filename, line_num, ent->name, msg);
return -1;
}
static int add_device(fstree_t *fs, const char *filename, size_t line_num,
sqfs_dir_entry_t *ent, split_line_t *line)
{
sqfs_u32 maj, min;
if (line->count != 3)
goto fail_args;
if (!strcmp(line->args[0], "c") || !strcmp(line->args[0], "C")) {
ent->mode |= S_IFCHR;
} else if (!strcmp(line->args[0], "b") || !strcmp(line->args[0], "B")) {
ent->mode |= S_IFBLK;
} else {
goto fail_type;
}
if (read_u32(line->args[1], &maj, 10))
goto fail_num;
if (read_u32(line->args[2], &min, 10))
goto fail_num;
ent->rdev = makedev(maj, min);
split_line_remove_front(line, 3);
return add_generic(fs, filename, line_num, ent, line);
fail_args:
fprintf(stderr, "%s: " PRI_SZ ": wrong number of arguments.\n",
filename, line_num);
goto fail_generic;
fail_type:
fprintf(stderr, "%s: " PRI_SZ ": unknown device type `%s`\n",
filename, line_num, line->args[0]);
goto fail_generic;
fail_num:
fprintf(stderr, "%s: " PRI_SZ ": error parsing device number\n",
filename, line_num);
goto fail_generic;
fail_generic:
fputs("expected syntax: `nod <c|b> <major> <minor>`\n", stderr);
return -1;
}
static int add_file(fstree_t *fs, const char *filename, size_t line_num,
sqfs_dir_entry_t *ent, split_line_t *line)
{
if (line->count == 0)
line->args[line->count++] = ent->name;
return add_generic(fs, filename, line_num, ent, line);
}
static const struct callback_t {
const char *keyword;
unsigned int mode;
unsigned int flags;
bool need_extra;
bool allow_root;
int (*callback)(fstree_t *fs, const char *filename, size_t line_num,
sqfs_dir_entry_t *ent, split_line_t *line);
} file_list_hooks[] = {
{ "dir", S_IFDIR, 0, false, true, add_generic },
{ "slink", S_IFLNK, 0, true, false, add_generic },
{ "link", S_IFLNK, SQFS_DIR_ENTRY_FLAG_HARD_LINK, true, false, add_generic },
{ "nod", 0, 0, true, false, add_device },
{ "pipe", S_IFIFO, 0, false, false, add_generic },
{ "sock", S_IFSOCK, 0, false, false, add_generic },
{ "file", S_IFREG, 0, false, false, add_file },
};
#define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0]))
static int handle_line(fstree_t *fs, const char *filename, size_t line_num,
split_line_t *line, const char *basepath)
{
const struct callback_t *cb = NULL;
unsigned int glob_flags = 0;
sqfs_u32 uid, gid, mode;
sqfs_dir_entry_t *ent = NULL;
const char *msg = NULL;
bool is_glob = false;
char *path;
int ret;
if (line->count < 5)
goto fail_ent;
for (size_t i = 0; i < NUM_HOOKS; ++i) {
if (!strcmp(file_list_hooks[i].keyword, line->args[0])) {
cb = file_list_hooks + i;
break;
}
}
if (cb == NULL) {
if (!strcmp("glob", line->args[0])) {
is_glob = true;
} else {
goto fail_kw;
}
}
path = line->args[1];
if (canonicalize_name(path))
goto fail_ent;
if (*path == '\0' && !(is_glob || cb->allow_root))
goto fail_root;
if (is_glob && !strcmp(line->args[2], "*")) {
mode = 0;
glob_flags |= DIR_SCAN_KEEP_MODE;
} else {
if (read_u32(line->args[2], &mode, 8) || mode > 07777)
goto fail_mode;
}
if (is_glob && !strcmp(line->args[3], "*")) {
uid = 0;
glob_flags |= DIR_SCAN_KEEP_UID;
} else {
if (read_u32(line->args[3], &uid, 10))
goto fail_uid_gid;
}
if (is_glob && !strcmp(line->args[4], "*")) {
gid = 0;
glob_flags |= DIR_SCAN_KEEP_GID;
} else {
if (read_u32(line->args[4], &gid, 10))
goto fail_uid_gid;
}
if (!is_glob && cb->need_extra && line->count <= 5)
goto fail_no_extra;
split_line_remove_front(line, 5);
/* forward to callback */
ent = alloc_flex(sizeof(*ent), 1, strlen(path) + 1);
if (ent == NULL)
goto fail_alloc;
strcpy(ent->name, path);
ent->mtime = fs->defaults.mtime;
ent->mode = mode | (is_glob ? 0 : cb->mode);
ent->uid = uid;
ent->gid = gid;
if (is_glob) {
ret = glob_files(fs, filename, line_num, ent,
basepath, glob_flags, line);
} else {
ret = cb->callback(fs, filename, line_num, ent, line);
}
free(ent);
return ret;
fail_alloc:
fprintf(stderr, "%s: " PRI_SZ ": out of memory\n",
filename, line_num);
return -1;
fail_root:
fprintf(stderr, "%s: " PRI_SZ ": cannot use / as argument for %s.\n",
filename, line_num, is_glob ? "glob" : cb->keyword);
return -1;
fail_no_extra:
fprintf(stderr, "%s: " PRI_SZ ": missing argument for %s.\n",
filename, line_num, cb->keyword);
return -1;
fail_uid_gid:
msg = "uid & gid must be decimal numbers < 2^32";
goto out_desc;
fail_mode:
msg = "mode must be an octal number <= 07777";
goto out_desc;
fail_kw:
msg = "unknown entry type";
goto out_desc;
fail_ent:
msg = "error in entry description";
goto out_desc;
out_desc:
fprintf(stderr, "%s: " PRI_SZ ": %s.\n", filename, line_num, msg);
fputs("expected: <type> <path> <mode> <uid> <gid> [<extra>]\n",
stderr);
return -1;
}
int fstree_from_file_stream(fstree_t *fs, sqfs_istream_t *fp,
const char *basepath)
{
const char *filename;
size_t line_num = 1;
split_line_t *sep;
char *line;
int ret;
filename = fp->get_filename(fp);
for (;;) {
ret = istream_get_line(fp, &line, &line_num,
ISTREAM_LINE_LTRIM | ISTREAM_LINE_SKIP_EMPTY);
if (ret < 0)
return -1;
if (ret > 0)
break;
if (line[0] == '#') {
free(line);
++line_num;
continue;
}
switch (split_line(line, strlen(line), " \t", &sep)) {
case SPLIT_LINE_OK: break;
case SPLIT_LINE_ALLOC: goto fail_alloc;
case SPLIT_LINE_UNMATCHED_QUOTE: goto fail_quote;
case SPLIT_LINE_ESCAPE: goto fail_esc;
default: goto fail_split;
}
ret = handle_line(fs, filename, line_num, sep, basepath);
free(sep);
free(line);
++line_num;
if (ret)
return -1;
}
return 0;
fail_alloc:
fprintf(stderr, "%s: " PRI_SZ ": out of memory\n", filename, line_num);
goto fail_line;
fail_quote:
fprintf(stderr, "%s: " PRI_SZ ": missing `\"`.\n", filename, line_num);
goto fail_line;
fail_esc:
fprintf(stderr, "%s: " PRI_SZ ": broken escape sequence.\n",
filename, line_num);
goto fail_line;
fail_split:
fprintf(stderr, "[BUG] %s: " PRI_SZ ": unknown error parsing line.\n",
filename, line_num);
goto fail_line;
fail_line:
free(line);
return -1;
}
int fstree_from_file(fstree_t *fs, const char *filename, const char *basepath)
{
sqfs_istream_t *fp;
int ret;
ret = sqfs_istream_open_file(&fp, filename, 0);
if (ret) {
sqfs_perror(filename, NULL, ret);
return -1;
}
ret = fstree_from_file_stream(fs, fp, basepath);
sqfs_drop(fp);
return ret;
}
|