aboutsummaryrefslogtreecommitdiff
path: root/bin/gensquashfs/src/filemap_xattr.c
blob: 8843c4645367908c9a8df36521138efc23bd5d30 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * filemap_xattr.c
 *
 * Copyright (C) 2022 Enno Boland <mail@eboland.de>
 */
#include "mkfs.h"

#define NEW_FILE_START "# file: "

static void print_error(const char *filename, size_t line_num, const char *err)
{
	fprintf(stderr, "%s: " PRI_SZ ": %s\n", filename, line_num, err);
}

// Taken from attr-2.5.1/tools/setfattr.c
static sqfs_u8 *decode(const char *filename, size_t line_num,
		       const char *value, size_t *size)
{
	sqfs_u8 *decoded = NULL;

	if (*size == 0) {
		decoded = (sqfs_u8 *)strdup("");
		if (decoded == NULL)
			goto fail_alloc;
		return decoded;
	}

	if (value[0] == '0' && (value[1] == 'x' || value[1] == 'X')) {
		*size = ((*size) - 2) / 2;

		decoded = calloc(1, (*size) + 1);
		if (decoded == NULL)
			goto fail_alloc;

		if (hex_decode(value + 2, (*size) * 2, decoded, *size))
			goto fail_encode;
	} else if (value[0] == '0' && (value[1] == 's' || value[1] == 'S')) {
		size_t input_len = *size - 2;

		*size = (input_len / 4) * 3;

		decoded = calloc(1, (*size) + 1);
		if (decoded == NULL)
			goto fail_alloc;

		if (base64_decode(value + 2, input_len, decoded, size))
			goto fail_encode;
	} else {
		const char *v = value, *end = value + *size;
		sqfs_u8 *d;

		if (end > v + 1 && *v == '"' && *(end - 1) == '"') {
			v++;
			end--;
		}

		decoded = calloc(1, (*size) + 1);
		if (decoded == NULL)
			goto fail_alloc;

		d = decoded;

		while (v < end) {
			if (v[0] == '\\') {
				if (v[1] == '\\' || v[1] == '"') {
					*d++ = *++v;
					v++;
				} else if (v[1] >= '0' && v[1] <= '7') {
					int c = 0;
					v++;
					c = (*v++ - '0');
					if (*v >= '0' && *v <= '7')
						c = (c << 3) + (*v++ - '0');
					if (*v >= '0' && *v <= '7')
						c = (c << 3) + (*v++ - '0');
					*d++ = c;
				} else
					*d++ = *v++;
			} else
				*d++ = *v++;
		}
		*size = d - decoded;
	}
	return decoded;
fail_alloc:
	fprintf(stderr, "out of memory\n");
	return NULL;
fail_encode:
	print_error(filename, line_num, "bad input encoding");
	free(decoded);
	return NULL;
}

static int parse_file_name(const char *filename, size_t line_num,
			   char *line, struct XattrMap *map)
{
	struct XattrMapPattern *current_file;
	char *file_name = strdup(line + strlen(NEW_FILE_START));

	if (file_name == NULL)
		goto fail_alloc;

	current_file = calloc(1, sizeof(struct XattrMapPattern));
	if (current_file == NULL)
		goto fail_alloc;

	current_file->next = map->patterns;
	map->patterns = current_file;

	if (canonicalize_name(file_name)) {
		print_error(filename, line_num, "invalid absolute path");
		free(current_file);
		free(file_name);
		return -1;
	}

	current_file->path = file_name;
	return 0;
fail_alloc:
	fprintf(stderr, "out of memory\n");
	free(file_name);
	return -1;
}

static int parse_xattr(const char *filename, size_t line_num, char *key_start,
		       char *value_start, struct XattrMap *map)
{
	size_t len;
	struct XattrMapPattern *current_pattern = map->patterns;
	sqfs_xattr_t *current_entry;
	sqfs_u8 *value;

	if (current_pattern == NULL) {
		print_error(filename, line_num, "no file specified yet");
		return -1;
	}

	len = strlen(value_start);
	value = decode(filename, line_num, value_start, &len);
	if (value == NULL)
		return -1;

	current_entry = sqfs_xattr_create(key_start, value, len);
	free(value);
	if (current_entry == NULL) {
		print_error(filename, line_num, "out-of-memory");
		return -1;
	}

	current_entry->next = current_pattern->entries;
	current_pattern->entries = current_entry;

	return 0;
}

void *
xattr_open_map_file(const char *path) {
	struct XattrMap *map;
	size_t line_num = 1;
	char *p = NULL;
	sqfs_istream_t *file = NULL;
	int ret;

	ret = istream_open_file(&file, path);
	if (ret) {
		sqfs_perror(path, NULL, ret);
		return NULL;
	}

	map = calloc(1, sizeof(struct XattrMap));
	if (map == NULL)
		goto fail_close;

	for (;;) {
		char *line = NULL;
		int ret = istream_get_line(file, &line, &line_num,
					   ISTREAM_LINE_LTRIM |
					   ISTREAM_LINE_RTRIM |
					   ISTREAM_LINE_SKIP_EMPTY);
		if (ret < 0)
			goto fail;
		if (ret > 0)
			break;

		if (strncmp(NEW_FILE_START, line, strlen(NEW_FILE_START)) == 0) {
			ret = parse_file_name(path, line_num, line, map);
		} else if ((p = strchr(line, '='))) {
			*(p++) = '\0';
			ret = parse_xattr(path, line_num, line, p, map);
		} else if (line[0] != '#') {
			print_error(path, line_num, "not a key-value pair");
			ret = -1;
		}

		++line_num;
		free(line);
		if (ret < 0)
			goto fail;
	}

	sqfs_drop(file);
	return map;
fail:
	xattr_close_map_file(map);
fail_close:
	sqfs_drop(file);
	return NULL;
}

void
xattr_close_map_file(void *xattr_map) {
	struct XattrMap *map = xattr_map;
	while (map->patterns != NULL) {
		struct XattrMapPattern *file = map->patterns;
		map->patterns = file->next;

		sqfs_xattr_list_free(file->entries);
		free(file->path);
		free(file);
	}
	free(xattr_map);
}

int
xattr_apply_map_file(char *path, void *map, sqfs_xattr_writer_t *xwr) {
	struct XattrMap *xattr_map = map;
	int ret = 0;
	const struct XattrMapPattern *pat;
	const sqfs_xattr_t *entry;

	for (pat = xattr_map->patterns; pat != NULL; pat = pat->next) {
		char *patstr = pat->path;
		const char *stripped = path;

		if (patstr[0] != '/' && stripped[0] == '/') {
			stripped++;
		}

		if (strcmp(patstr, stripped) == 0) {
			printf("Applying xattrs for %s", path);
			for (entry = pat->entries; entry != NULL; entry = entry->next) {
				printf("  %s = \n", entry->key);
				fwrite(entry->value, entry->value_len, 1, stdout);
				puts("\n");
				ret = sqfs_xattr_writer_add(xwr, entry);
				if (ret < 0) {
					return ret;
				}
			}
		}
	}
	return ret;
}