aboutsummaryrefslogtreecommitdiff
path: root/bin/gensquashfs/src/glob.c
blob: 73f078b1de819720877238c07f16ff8d9297c4c0 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * glob.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "mkfs.h"

static const struct {
	const char *name;
	unsigned int flag;
} glob_types[] = {
	{ "b", DIR_SCAN_NO_BLK },   { "B", DIR_SCAN_NO_BLK },
	{ "c", DIR_SCAN_NO_CHR },   { "C", DIR_SCAN_NO_CHR },
	{ "d", DIR_SCAN_NO_DIR },   { "D", DIR_SCAN_NO_DIR },
	{ "p", DIR_SCAN_NO_FIFO },  { "P", DIR_SCAN_NO_FIFO },
	{ "f", DIR_SCAN_NO_FILE },  { "F", DIR_SCAN_NO_FILE },
	{ "l", DIR_SCAN_NO_SLINK }, { "L", DIR_SCAN_NO_SLINK },
	{ "s", DIR_SCAN_NO_SOCK },  { "S", DIR_SCAN_NO_SOCK },
};

static bool apply_type_flag(bool is_first, const char *arg,
			    unsigned int *flags)
{
	size_t count = sizeof(glob_types) / sizeof(glob_types[0]);

	if (is_first) {
		for (size_t i = 0; i < count; ++i)
			(*flags) |= glob_types[i].flag;
	}

	for (size_t i = 0; i < count; ++i) {
		if (!strcmp(glob_types[i].name, arg)) {
			(*flags) &= ~(glob_types[i].flag);
			return true;
		}
	}

	return false;
}

static const struct {
	const char *name;
	unsigned int flag;
} glob_scan_flags[] = {
	{ "-xdev", DIR_SCAN_ONE_FILESYSTEM },
	{ "-mount", DIR_SCAN_ONE_FILESYSTEM },
	{ "-keeptime", DIR_SCAN_KEEP_TIME },
	{ "-nonrecursive", DIR_SCAN_NO_RECURSION },
	{ "-nohardlinks", DIR_SCAN_NO_HARDLINKS },
};

static bool set_scan_flag(const char *arg, dir_tree_cfg_t *cfg)
{
	size_t count = sizeof(glob_scan_flags) / sizeof(glob_scan_flags[0]);

	for (size_t i = 0; i < count; ++i) {
		if (!strcmp(arg, glob_scan_flags[i].name)) {
			cfg->flags |= glob_scan_flags[i].flag;
			return true;
		}
	}

	return false;
}

int scan_directory(fstree_t *fs, sqfs_dir_iterator_t *dir,
		   size_t prefix_len, const char *file_prefix)
{
	for (;;) {
		sqfs_dir_entry_t *ent = NULL;
		tree_node_t *n = NULL;
		char *extra = NULL;

		int ret = dir->next(dir, &ent);
		if (ret > 0)
			break;
		if (ret < 0) {
			sqfs_perror("readdir", NULL, ret);
			return -1;
		}

		n = fstree_get_node_by_path(fs, fs->root, ent->name,
					    false, true);
		if (n == NULL) {
			if (S_ISDIR(ent->mode))
				dir->ignore_subdir(dir);
			free(ent);
			continue;
		}

		if (S_ISLNK(ent->mode)) {
			ret = dir->read_link(dir, &extra);
			if (ret) {
				sqfs_perror("readlink", ent->name, ret);
				free(ent);
				return -1;
			}
		} else if (S_ISREG(ent->mode) &&
			   (prefix_len > 0 || file_prefix != NULL)) {
			const char *src;

			/* skip the prefix, get the name actually
			   returned by the nested iterator */
			src = ent->name + prefix_len;
			while (*src == '/')
				++src;

			/* reconstruct base path relative file path */
			if (file_prefix == NULL) {
				extra = strdup(src);
			} else {
				size_t fxlen = strlen(file_prefix) + 1;
				size_t srclen = strlen(src) + 1;

				extra = malloc(fxlen + srclen);
				if (extra != NULL) {
					memcpy(extra, file_prefix, fxlen);
					memcpy(extra + fxlen, src, srclen);
					extra[fxlen - 1] = '/';
				}
			}

			if (extra == NULL) {
				free(ent);
				fputs("out-of-memory\n", stderr);
				return -1;
			}
		}

		n = fstree_add_generic(fs, ent, extra);
		free(extra);
		free(ent);

		if (n == NULL) {
			perror("creating tree node");
			return -1;
		}
	}

	return 0;
}

int glob_files(fstree_t *fs, const char *filename, size_t line_num,
	       const sqfs_dir_entry_t *ent,
	       const char *basepath, unsigned int glob_flags,
	       split_line_t *sep)
{
	bool first_type_flag = true;
	sqfs_dir_iterator_t *dir = NULL;
	const char *file_prefix = NULL;
	char *prefix = NULL;
	dir_tree_cfg_t cfg;
	tree_node_t *root;
	int ret;

	/* fetch the actual target node */
	root = fstree_get_node_by_path(fs, fs->root, ent->name, true, false);
	if (root == NULL)
		goto fail_path;

	if (!S_ISDIR(root->mode))
		goto fail_not_dir;

	prefix = fstree_get_path(root);
	if (prefix == NULL)
		goto fail_alloc;

	if (canonicalize_name(prefix) != 0)
		goto fail_prefix;

	/* process options */
	memset(&cfg, 0, sizeof(cfg));
	cfg.def_mtime = ent->mtime;
	cfg.def_uid = ent->uid;
	cfg.def_gid = ent->gid;
	cfg.def_mode = ent->mode;
	cfg.prefix = prefix;
	cfg.flags = glob_flags;

#if defined(_WIN32) || defined(__WINDOWS__)
	cfg.flags |= DIR_SCAN_NO_HARDLINKS;
#endif

	while (sep->count != 0) {
		if (sep->args[0][0] != '-')
			break;

		if (!strcmp(sep->args[0], "--")) {
			split_line_remove_front(sep, 1);
			break;
		}

		if (set_scan_flag(sep->args[0], &cfg)) {
			split_line_remove_front(sep, 1);
		} else if (!strcmp(sep->args[0], "-type")) {
			if (sep->count < 2)
				goto fail_no_arg;

			if (!apply_type_flag(first_type_flag, sep->args[1],
					     &cfg.flags)) {
				goto fail_type;
			}

			first_type_flag = false;
			split_line_remove_front(sep, 2);
		} else if (!strcmp(sep->args[0], "-name")) {
			if (sep->count < 2)
				goto fail_no_arg;
			cfg.name_pattern = sep->args[1];
			split_line_remove_front(sep, 2);
		} else if (!strcmp(sep->args[0], "-path")) {
			if (sep->count < 2)
				goto fail_no_arg;
			cfg.name_pattern = sep->args[1];
			cfg.flags |= DIR_SCAN_MATCH_FULL_PATH;
			split_line_remove_front(sep, 2);
		} else {
			goto fail_unknown;
		}
	}

	/* do the scan */
	if (sep->count == 0) {
		dir = dir_tree_iterator_create(basepath, &cfg);
	} else {
		size_t plen = strlen(basepath);
		size_t slen = strlen(sep->args[0]);
		char *temp = calloc(1, plen + 1 + slen + 1);

		if (temp == NULL)
			goto fail_alloc;

		memcpy(temp, basepath, plen);
		temp[plen] = '/';
		memcpy(temp + plen + 1, sep->args[0], slen);
		temp[plen + 1 + slen] = '\0';

		dir = dir_tree_iterator_create(temp, &cfg);
		free(temp);

		file_prefix = sep->args[0];
	}

	if (dir == NULL)
		goto fail;

	ret = scan_directory(fs, dir, strlen(cfg.prefix), file_prefix);
	sqfs_drop(dir);

	free(prefix);
	return ret;
fail_unknown:
	fprintf(stderr, "%s: " PRI_SZ ": unknown glob option: %s.\n",
		filename, line_num, sep->args[0]);
	goto fail;
fail_path:
	fprintf(stderr, "%s: " PRI_SZ ": %s: %s\n",
		filename, line_num, ent->name, strerror(errno));
	goto fail;
fail_not_dir:
	fprintf(stderr, "%s: " PRI_SZ ": %s is not a directoy!\n",
		filename, line_num, ent->name);
	goto fail;
fail_prefix:
	fprintf(stderr, "%s: " PRI_SZ ": error cannonicalizing `%s`!\n",
		filename, line_num, prefix);
	goto fail;
fail_alloc:
	fprintf(stderr, "%s: " PRI_SZ ": out of memory\n",
		filename, line_num);
	goto fail;
fail_type:
	fprintf(stderr, "%s: " PRI_SZ ": unknown file type `%s`\n",
		filename, line_num, sep->args[1]);
	goto fail;
fail_no_arg:
	fprintf(stderr, "%s: " PRI_SZ ": missing argument for `%s`\n",
		filename, line_num, sep->args[0]);
	goto fail;
fail:
	free(prefix);
	return -1;
}