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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* fstree_from_file.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#include "fstree.h"
#include "fstream.h"
#include <fnmatch.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
struct glob_context {
const char *filename;
size_t line_num;
struct stat *basic;
unsigned int glob_flags;
char *name_pattern;
};
enum {
GLOB_MODE_FROM_SRC = 0x01,
GLOB_UID_FROM_SRC = 0x02,
GLOB_GID_FROM_SRC = 0x04,
GLOB_FLAG_PATH = 0x08,
};
static const struct {
const char *name;
unsigned int clear_flag;
unsigned int set_flag;
} glob_scan_flags[] = {
{ "-type b", DIR_SCAN_NO_BLK, 0 },
{ "-type c", DIR_SCAN_NO_CHR, 0 },
{ "-type d", DIR_SCAN_NO_DIR, 0 },
{ "-type p", DIR_SCAN_NO_FIFO, 0 },
{ "-type f", DIR_SCAN_NO_FILE, 0 },
{ "-type l", DIR_SCAN_NO_SLINK, 0 },
{ "-type s", DIR_SCAN_NO_SOCK, 0 },
{ "-xdev", 0, DIR_SCAN_ONE_FILESYSTEM },
{ "-mount", 0, DIR_SCAN_ONE_FILESYSTEM },
{ "-keeptime", 0, DIR_SCAN_KEEP_TIME },
{ "-nonrecursive", 0, DIR_SCAN_NO_RECURSION },
};
static int add_generic(fstree_t *fs, const char *filename, size_t line_num,
const char *path, struct stat *sb,
const char *basepath, unsigned int glob_flags,
const char *extra)
{
(void)basepath;
(void)glob_flags;
if (fstree_add_generic(fs, path, sb, extra) == NULL) {
fprintf(stderr, "%s: " PRI_SZ ": %s: %s\n",
filename, line_num, path, strerror(errno));
return -1;
}
return 0;
}
static int add_device(fstree_t *fs, const char *filename, size_t line_num,
const char *path, struct stat *sb, const char *basepath,
unsigned int glob_flags, const char *extra)
{
unsigned int maj, min;
char c;
if (sscanf(extra, "%c %u %u", &c, &maj, &min) != 3) {
fprintf(stderr, "%s: " PRI_SZ ": "
"expected '<c|b> major minor'\n",
filename, line_num);
return -1;
}
if (c == 'c' || c == 'C') {
sb->st_mode |= S_IFCHR;
} else if (c == 'b' || c == 'B') {
sb->st_mode |= S_IFBLK;
} else {
fprintf(stderr, "%s: " PRI_SZ ": unknown device type '%c'\n",
filename, line_num, c);
return -1;
}
sb->st_rdev = makedev(maj, min);
return add_generic(fs, filename, line_num, path, sb, basepath,
glob_flags, NULL);
}
static int add_file(fstree_t *fs, const char *filename, size_t line_num,
const char *path, struct stat *basic, const char *basepath,
unsigned int glob_flags, const char *extra)
{
if (extra == NULL || *extra == '\0')
extra = path;
return add_generic(fs, filename, line_num, path, basic,
basepath, glob_flags, extra);
}
static int add_hard_link(fstree_t *fs, const char *filename, size_t line_num,
const char *path, struct stat *basic,
const char *basepath, unsigned int glob_flags,
const char *extra)
{
(void)basepath;
(void)glob_flags;
(void)basic;
if (fstree_add_hard_link(fs, path, extra) == NULL) {
fprintf(stderr, "%s: " PRI_SZ ": %s\n",
filename, line_num, strerror(errno));
return -1;
}
return 0;
}
static int glob_node_callback(void *user, fstree_t *fs, tree_node_t *node)
{
struct glob_context *ctx = user;
char *path;
int ret;
(void)fs;
if (!(ctx->glob_flags & GLOB_MODE_FROM_SRC)) {
node->mode &= ~(07777);
node->mode |= ctx->basic->st_mode & 07777;
}
if (!(ctx->glob_flags & GLOB_UID_FROM_SRC))
node->uid = ctx->basic->st_uid;
if (!(ctx->glob_flags & GLOB_GID_FROM_SRC))
node->gid = ctx->basic->st_gid;
if (ctx->name_pattern != NULL) {
if (ctx->glob_flags & GLOB_FLAG_PATH) {
path = fstree_get_path(node);
if (path == NULL) {
fprintf(stderr, "%s: " PRI_SZ ": %s\n",
ctx->filename, ctx->line_num,
strerror(errno));
return -1;
}
ret = fnmatch(ctx->name_pattern, path, FNM_PATHNAME);
free(path);
} else {
ret = fnmatch(ctx->name_pattern, node->name, 0);
}
if (ret != 0)
return 1;
}
return 0;
}
static size_t name_string_length(const char *str)
{
size_t len = 0;
int start;
if (*str == '"' || *str == '\'') {
start = *str;
++len;
while (str[len] != '\0' && str[len] != start)
++len;
if (str[len] == start)
++len;
} else {
while (str[len] != '\0' && !isspace(str[len]))
++len;
}
return len;
}
static void quote_remove(char *str)
{
char *dst = str;
int start = *(str++);
if (start != '\'' && start != '"')
return;
while (*str != start && *str != '\0')
*(dst++) = *(str++);
*(dst++) = '\0';
}
static int glob_files(fstree_t *fs, const char *filename, size_t line_num,
const char *path, struct stat *basic,
const char *basepath, unsigned int glob_flags,
const char *extra)
{
unsigned int scan_flags = 0, all_flags;
struct glob_context ctx;
bool first_clear_flag;
size_t i, count, len;
tree_node_t *root;
int ret;
memset(&ctx, 0, sizeof(ctx));
ctx.filename = filename;
ctx.line_num = line_num;
ctx.basic = basic;
ctx.glob_flags = glob_flags;
/* fetch the actual target node */
root = fstree_get_node_by_path(fs, fs->root, path, true, false);
if (root == NULL) {
fprintf(stderr, "%s: " PRI_SZ ": %s: %s\n",
filename, line_num, path, strerror(errno));
return -1;
}
/* process options */
first_clear_flag = true;
all_flags = DIR_SCAN_NO_BLK | DIR_SCAN_NO_CHR | DIR_SCAN_NO_DIR |
DIR_SCAN_NO_FIFO | DIR_SCAN_NO_FILE | DIR_SCAN_NO_SLINK |
DIR_SCAN_NO_SOCK;
while (*extra != '\0') {
count = sizeof(glob_scan_flags) / sizeof(glob_scan_flags[0]);
for (i = 0; i < count; ++i) {
len = strlen(glob_scan_flags[i].name);
if (strncmp(extra, glob_scan_flags[i].name, len) != 0)
continue;
if (isspace(extra[len])) {
extra += len;
while (isspace(*extra))
++extra;
break;
}
}
if (i < count) {
if (glob_scan_flags[i].clear_flag != 0 &&
first_clear_flag) {
scan_flags |= all_flags;
first_clear_flag = false;
}
scan_flags &= ~(glob_scan_flags[i].clear_flag);
scan_flags |= glob_scan_flags[i].set_flag;
continue;
}
if (strncmp(extra, "-name", 5) == 0 && isspace(extra[5])) {
for (extra += 5; isspace(*extra); ++extra)
;
len = name_string_length(extra);
free(ctx.name_pattern);
ctx.name_pattern = strndup(extra, len);
extra += len;
while (isspace(*extra))
++extra;
quote_remove(ctx.name_pattern);
continue;
}
if (strncmp(extra, "-path", 5) == 0 && isspace(extra[5])) {
for (extra += 5; isspace(*extra); ++extra)
;
len = name_string_length(extra);
free(ctx.name_pattern);
ctx.name_pattern = strndup(extra, len);
extra += len;
while (isspace(*extra))
++extra;
quote_remove(ctx.name_pattern);
ctx.glob_flags |= GLOB_FLAG_PATH;
continue;
}
break;
}
if (*extra == '\0') {
fprintf(stderr, "%s: " PRI_SZ ": glob path missing.\n",
filename, line_num);
free(ctx.name_pattern);
return -1;
}
/* do the scan */
if (basepath == NULL) {
ret = fstree_from_dir(fs, root, extra, glob_node_callback,
&ctx, scan_flags);
} else {
ret = fstree_from_subdir(fs, root, basepath, extra,
glob_node_callback, &ctx,
scan_flags);
}
free(ctx.name_pattern);
return ret;
}
static const struct callback_t {
const char *keyword;
unsigned int mode;
bool need_extra;
bool is_glob;
int (*callback)(fstree_t *fs, const char *filename, size_t line_num,
const char *path, struct stat *sb,
const char *basepath, unsigned int glob_flags,
const char *extra);
} file_list_hooks[] = {
{ "dir", S_IFDIR, false, false, add_generic },
{ "slink", S_IFLNK, true, false, add_generic },
{ "link", 0, true, false, add_hard_link },
{ "nod", 0, true, false, add_device },
{ "pipe", S_IFIFO, false, false, add_generic },
{ "sock", S_IFSOCK, false, false, add_generic },
{ "file", S_IFREG, false, false, add_file },
{ "glob", 0, true, true, glob_files },
};
#define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0]))
static char *skip_space(char *str)
{
if (!isspace(*str))
return NULL;
while (isspace(*str))
++str;
return str;
}
static char *read_u32(char *str, sqfs_u32 *out, sqfs_u32 base)
{
*out = 0;
if (!isdigit(*str))
return NULL;
while (isdigit(*str)) {
sqfs_u32 x = *(str++) - '0';
if (x >= base || (*out) > (0xFFFFFFFF - x) / base)
return NULL;
(*out) = (*out) * base + x;
}
return str;
}
static char *read_str(char *str, char **out)
{
*out = str;
if (*str == '"') {
char *ptr = str++;
while (*str != '\0' && *str != '"') {
if (str[0] == '\\' &&
(str[1] == '"' || str[1] == '\\')) {
*(ptr++) = str[1];
str += 2;
} else {
*(ptr++) = *(str++);
}
}
if (str[0] != '"' || !isspace(str[1]))
return NULL;
*ptr = '\0';
++str;
} else {
while (*str != '\0' && !isspace(*str))
++str;
if (!isspace(*str))
return NULL;
*(str++) = '\0';
}
while (isspace(*str))
++str;
return str;
}
static int handle_line(fstree_t *fs, const char *filename,
size_t line_num, char *line,
const char *basepath)
{
const char *extra = NULL, *msg = NULL;
const struct callback_t *cb = NULL;
unsigned int glob_flags = 0;
sqfs_u32 uid, gid, mode;
struct stat sb;
char *path;
for (size_t i = 0; i < NUM_HOOKS; ++i) {
size_t len = strlen(file_list_hooks[i].keyword);
if (strncmp(file_list_hooks[i].keyword, line, len) != 0)
continue;
if (isspace(line[len])) {
cb = file_list_hooks + i;
line = skip_space(line + len);
break;
}
}
if (cb == NULL)
goto fail_kw;
if ((line = read_str(line, &path)) == NULL)
goto fail_ent;
if (canonicalize_name(path) || *path == '\0')
goto fail_ent;
if (cb->is_glob && *line == '*') {
++line;
mode = 0;
glob_flags |= GLOB_MODE_FROM_SRC;
} else {
if ((line = read_u32(line, &mode, 8)) == NULL || mode > 07777)
goto fail_mode;
}
if ((line = skip_space(line)) == NULL)
goto fail_ent;
if (cb->is_glob && *line == '*') {
++line;
uid = 0;
glob_flags |= GLOB_UID_FROM_SRC;
} else {
if ((line = read_u32(line, &uid, 10)) == NULL)
goto fail_uid_gid;
}
if ((line = skip_space(line)) == NULL)
goto fail_ent;
if (cb->is_glob && *line == '*') {
++line;
gid = 0;
glob_flags |= GLOB_GID_FROM_SRC;
} else {
if ((line = read_u32(line, &gid, 10)) == NULL)
goto fail_uid_gid;
}
if ((line = skip_space(line)) != NULL && *line != '\0')
extra = line;
if (cb->need_extra && extra == NULL)
goto fail_no_extra;
/* forward to callback */
memset(&sb, 0, sizeof(sb));
sb.st_mtime = fs->defaults.st_mtime;
sb.st_mode = mode | cb->mode;
sb.st_uid = uid;
sb.st_gid = gid;
return cb->callback(fs, filename, line_num, path,
&sb, basepath, glob_flags, extra);
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(fstree_t *fs, const char *filename, const char *basepath)
{
size_t line_num = 1;
istream_t *fp;
char *line;
int ret;
fp = istream_open_file(filename);
if (fp == NULL)
return -1;
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] != '#') {
if (handle_line(fs, filename, line_num,
line, basepath)) {
goto fail_line;
}
}
free(line);
++line_num;
}
sqfs_destroy(fp);
return 0;
fail_line:
free(line);
sqfs_destroy(fp);
return -1;
}
|