summaryrefslogtreecommitdiff
path: root/lib/fstree/fstree_from_file.c
blob: 66d71a2da2066d2c19b5facbfa70ab55a8516856 (plain)
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "fstree.h"

#include <sys/sysmacros.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>

static int add_dir(fstree_t *fs, const char *filename, size_t line_num,
		   const char *path, uint16_t mode, uint32_t uid,
		   uint32_t gid, const char *extra)
{
	if (extra != NULL && *extra != '\0') {
		fprintf(stderr, "%s: %zu: WARNING: ignoring extra arguments\n",
			filename, line_num);
	}

	if (fstree_add(fs, path, S_IFDIR | mode, uid, gid, 0) == NULL) {
		fprintf(stderr, "%s: %zu: mkdir -p %s: %s\n",
			filename, line_num, path, strerror(errno));
		return -1;
	}

	return 0;
}

static int add_slink(fstree_t *fs, const char *filename, size_t line_num,
		     const char *path, uint16_t mode, uint32_t uid,
		     uint32_t gid, const char *extra)
{
	tree_node_t *node;
	(void)mode;

	if (extra == NULL || *extra == '\0') {
		fprintf(stderr, "%s: %zu: missing symlink target\n",
			filename, line_num);
		return -1;
	}

	node = fstree_add(fs, path, S_IFLNK | 0777, uid, gid,
			  strlen(extra) + 1);

	if (node == NULL) {
		fprintf(stderr, "%s: %zu: ln -s %s %s\n",
			filename, line_num, extra, path);
		return -1;
	}

	strcpy(node->data.slink_target, extra);
	return 0;
}

static int add_device(fstree_t *fs, const char *filename, size_t line_num,
		      const char *path, uint16_t mode, uint32_t uid,
		      uint32_t gid, const char *extra)
{
	unsigned int maj = 0, min = 0;
	tree_node_t *node;

	if (extra == NULL || *extra == '\0') {
		fprintf(stderr, "%s: %zu: missing device type\n",
			filename, line_num);
		return -1;
	}

	if ((*extra == 'c' || *extra == 'C') && isspace(extra[1])) {
		mode |= S_IFCHR;
	} else if ((*extra == 'b' || *extra == 'B') && isspace(extra[1])) {
		mode |= S_IFBLK;
	} else {
		fprintf(stderr, "%s: %zu: unsupported device type",
			filename, line_num);
		return -1;
	}

	++extra;
	while (isspace(*extra))
		++extra;

	if (!isdigit(*extra))
		goto fail_devno;

	while (isdigit(*extra))
		maj = maj * 10 + *(extra++) - '0';

	if (*(extra++) != ':')
		goto fail_devno;

	if (!isdigit(*extra))
		goto fail_devno;

	while (isdigit(*extra))
		min = min * 10 + *(extra++) - '0';

	while (isspace(*extra))
		++extra;

	if (*extra != '\0') {
		fprintf(stderr, "%s: %zu: WARNING: ignoring extra arguments\n",
			filename, line_num);
	}

	node = fstree_add(fs, path, mode, uid, gid, 0);
	if (node == NULL) {
		fprintf(stderr, "%s: %zu: mknod %s %c %u %u: %s\n",
			filename, line_num, path, S_ISCHR(mode) ? 'c' : 'b',
			maj, min, strerror(errno));
		return -1;
	}

	node->data.devno = makedev(maj, min);
	return 0;
fail_devno:
	fprintf(stderr, "%s: %zu: error in device number format\n",
		filename, line_num);
	return -1;
}

static int add_pipe(fstree_t *fs, const char *filename, size_t line_num,
		    const char *path, uint16_t mode, uint32_t uid,
		    uint32_t gid, const char *extra)
{
	if (extra != NULL && *extra != '\0') {
		fprintf(stderr, "%s: %zu: WARNING: ignoring extra arguments\n",
			filename, line_num);
	}

	if (fstree_add(fs, path, S_IFIFO | mode, uid, gid, 0) == NULL) {
		fprintf(stderr, "%s: %zu: mkfifo %s: %s\n",
			filename, line_num, path, strerror(errno));
		return -1;
	}

	return 0;
}

static int add_socket(fstree_t *fs, const char *filename, size_t line_num,
		      const char *path, uint16_t mode, uint32_t uid,
		      uint32_t gid, const char *extra)
{
	if (extra != NULL && *extra != '\0') {
		fprintf(stderr, "%s: %zu: WARNING: ignoring extra arguments\n",
			filename, line_num);
	}

	if (fstree_add(fs, path, S_IFSOCK | mode, uid, gid, 0) == NULL) {
		fprintf(stderr, "%s: %zu: creating Unix socket %s: %s\n",
			filename, line_num, path, strerror(errno));
		return -1;
	}

	return 0;
}

static int add_file(fstree_t *fs, const char *filename, size_t line_num,
		    const char *path, uint16_t mode, uint32_t uid,
		    uint32_t gid, const char *extra)
{
	char *infile = NULL;
	tree_node_t *node;
	const char *ptr;
	struct stat sb;
	int ret;

	ptr = strrchr(filename, '/');

	if (extra == NULL || *extra == '\0') {
		if (ptr == NULL) {
			extra = path;
		} else {
			ret = asprintf(&infile, "%.*s/%s",
				       (int)(filename - ptr), filename, path);
			if (ret < 0)
				goto fail_asprintf;
		}
	} else if (*extra != '/' && ptr != NULL) {
		ret = asprintf(&infile, "%.*s/%s",
			       (int)(filename - ptr), filename, extra);
		if (ret < 0)
			goto fail_asprintf;
	}

	if (infile != NULL)
		extra = infile;

	if (stat(extra, &sb) != 0) {
		fprintf(stderr, "%s: %zu: stat %s: %s\n", filename, line_num,
			extra, strerror(errno));
		goto fail;
	}

	node = fstree_add_file(fs, path, mode, uid, gid, sb.st_size, extra);

	if (node == NULL) {
		fprintf(stderr, "%s: %zu: adding %s as %s: %s\n",
			filename, line_num, extra, path, strerror(errno));
		goto fail;
	}

	free(infile);
	return 0;
fail:
	free(infile);
	return -1;
fail_asprintf:
	fprintf(stderr, "%s: %zu: pasting together file path: %s\n",
		filename, line_num, strerror(errno));
	return -1;
}

static const struct {
	const char *keyword;
	int (*callback)(fstree_t *fs, const char *filename, size_t line_num,
			const char *path, uint16_t mode, uint32_t uid,
			uint32_t gid, const char *extra);
} file_list_hooks[] = {
	{ "dir", add_dir },
	{ "slink", add_slink },
	{ "nod", add_device },
	{ "pipe", add_pipe },
	{ "pipe", add_socket },
	{ "file", add_file },
};

#define NUM_HOOKS (sizeof(file_list_hooks) / sizeof(file_list_hooks[0]))

static void trim_line(char *line)
{
	size_t i;

	for (i = 0; isspace(line[i]); ++i)
		;

	if (line[i] == '#') {
		line[0] = '\0';
		return;
	}

	if (i > 0)
		memmove(line, line + i, strlen(line + i) + 1);

	i = strlen(line);
	while (i > 0 && isspace(line[i - 1]))
		--i;

	line[i] = '\0';
}

static int handle_line(fstree_t *fs, const char *filename,
		       size_t line_num, char *line)
{
	const char *path, *extra = NULL, *msg = NULL;
	unsigned int mode = 0, uid = 0, gid = 0, x;
	char keyword[16];
	size_t i;

	/* isolate keyword */
	for (i = 0; isalpha(line[i]); ++i)
		;

	if (i >= sizeof(keyword) || i == 0 || !isspace(line[i]))
		goto fail_ent;

	memcpy(keyword, line, i);
	keyword[i] = '\0';

	while (isspace(line[i]))
		++i;

	/* isolate path */
	path = line + i;

	for (; line[i] != '\0'; ++i) {
		/* TODO: escape sequences to support spaces in path */

		if (isspace(line[i]))
			break;
	}

	if (!isspace(line[i]))
		goto fail_ent;

	line[i++] = '\0';
	while (isspace(line[i]))
		++i;

	/* mode */
	if (!isdigit(line[i]))
		goto fail_mode;

	for (; isdigit(line[i]); ++i) {
		if (line[i] > '7')
			goto fail_mode;

		mode = (mode << 3) | (line[i] - '0');

		if (mode > 07777)
			goto fail_mode_bits;
	}

	if (!isspace(line[i]))
		goto fail_ent;

	while (isspace(line[i]))
		++i;

	/* uid */
	if (!isdigit(line[i]))
		goto fail_uid_gid;

	for (; isdigit(line[i]); ++i) {
		x = line[i] - '0';

		if (uid > (0xFFFFFFFF - x) / 10)
			goto fail_ov;

		uid = uid * 10 + x;
	}

	if (!isspace(line[i]))
		goto fail_ent;

	while (isspace(line[i]))
		++i;

	/* gid */
	if (!isdigit(line[i]))
		goto fail_uid_gid;

	for (; isdigit(line[i]); ++i) {
		x = line[i] - '0';

		if (gid > (0xFFFFFFFF - x) / 10)
			goto fail_ov;

		gid = gid * 10 + x;
	}

	/* extra */
	if (isspace(line[i])) {
		while (isspace(line[i]))
			++i;

		if (line[i] != '\0')
			extra = line + i;
	}

	/* forward to callback */
	for (i = 0; i < NUM_HOOKS; ++i) {
		if (strcmp(file_list_hooks[i].keyword, keyword) == 0) {
			return file_list_hooks[i].callback(fs, filename,
							   line_num, path,
							   mode, uid, gid,
							   extra);
		}
	}

	fprintf(stderr, "%s: %zu: unknown entry type '%s'.\n", filename,
		line_num, keyword);
	return -1;
fail_ov:
	msg = "numeric overflow";
	goto fail_ent;
fail_uid_gid:
	msg = "uid & gid must be decimal numbers";
	goto out_desc;
fail_mode:
	msg = "mode must be an octal number";
	goto out_desc;
fail_mode_bits:
	msg = "you can only set the permission bits in the mode";
	goto out_desc;
fail_ent:
	msg = "error in entry description";
	goto out_desc;
out_desc:
	fprintf(stderr, "%s: %zu: %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)
{
	FILE *fp = fopen(filename, "rb");
	size_t n, line_num = 0;
	ssize_t ret;
	char *line;

	if (fp == NULL) {
		perror(filename);
		return -1;
	}

	for (;;) {
		line = NULL;
		n = 0;
		errno = 0;

		ret = getline(&line, &n, fp);
		++line_num;

		if (ret < 0) {
			if (errno == 0) {
				free(line);
				break;
			}

			perror(filename);
			goto fail_line;
		}

		trim_line(line);

		if (line[0] == '\0') {
			free(line);
			continue;
		}

		if (handle_line(fs, filename, line_num, line))
			goto fail_line;

		free(line);
	}

	fclose(fp);
	return 0;
fail_line:
	free(line);
	fclose(fp);
	return -1;
}