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

static char *get_file_path(tree_node_t *n, const char *name)
{
	char *ptr, *new;
	int ret;

	if (n->parent == NULL) {
		ptr = strdup(name);
		if (ptr == NULL)
			goto fail;
		return ptr;
	}

	ptr = fstree_get_path(n);
	if (ptr == NULL)
		goto fail;

	ret = canonicalize_name(ptr);
	assert(ret == 0);

	new = realloc(ptr, strlen(ptr) + strlen(name) + 2);
	if (new == NULL)
		goto fail;

	ptr = new;
	strcat(ptr, "/");
	strcat(ptr, name);
	return ptr;
fail:
	perror("getting absolute file path");
	free(ptr);
	return NULL;
}

#ifdef HAVE_SYS_XATTR_H
static int populate_xattr(sqfs_xattr_writer_t *xwr, tree_node_t *node)
{
	char *key, *value = NULL, *buffer = NULL;
	ssize_t buflen, vallen, keylen;

	buflen = listxattr(node->name, NULL, 0);

	if (buflen < 0) {
		perror("listxattr");
		return -1;
	}

	if (buflen == 0)
		return 0;

	buffer = malloc(buflen);
	if (buffer == NULL) {
		perror("xattr name buffer");
		return -1;
	}

	buflen = listxattr(node->name, buffer, buflen);
	if (buflen == -1) {
		perror("listxattr");
		goto fail;
	}

	key = buffer;
	while (buflen > 0) {
		vallen = getxattr(node->name, key, NULL, 0);
		if (vallen == -1)
			goto fail;

		if (vallen > 0) {
			value = calloc(1, vallen);
			if (value == NULL) {
				perror("xattr value buffer");
				goto fail;
			}

			vallen = getxattr(node->name, key, value, vallen);
			if (vallen == -1) {
				perror("getxattr");
				goto fail;
			}

			if (sqfs_xattr_writer_add(xwr, key, value, vallen)) {
				fputs("Error storing xattr key-value pair\n",
				      stderr);
				goto fail;
			}

			free(value);
			value = NULL;
		}

		keylen = strlen(key) + 1;
		buflen -= keylen;
		key += keylen;
	}

	free(buffer);
	return 0;
fail:
	free(value);
	free(buffer);
	return -1;
}
#endif

static int populate_dir(fstree_t *fs, tree_node_t *root, dev_t devstart,
			void *selinux_handle, sqfs_xattr_writer_t *xwr,
			unsigned int flags)
{
	char *extra = NULL, *path;
	struct dirent *ent;
	struct stat sb;
	tree_node_t *n;
	DIR *dir;

	dir = opendir(".");
	if (dir == NULL) {
		perror("opendir");
		return -1;
	}

	for (;;) {
		errno = 0;
		ent = readdir(dir);

		if (ent == NULL) {
			if (errno) {
				perror("readdir");
				goto fail;
			}
			break;
		}

		if (!strcmp(ent->d_name, "..") || !strcmp(ent->d_name, "."))
			continue;

		if (fstatat(AT_FDCWD, ent->d_name, &sb, AT_SYMLINK_NOFOLLOW)) {
			perror(ent->d_name);
			goto fail;
		}

		if ((flags & DIR_SCAN_ONE_FILESYSTEM) && sb.st_dev != devstart)
			continue;

		if (S_ISLNK(sb.st_mode)) {
			extra = calloc(1, sb.st_size + 1);
			if (extra == NULL)
				goto fail_rdlink;

			if (readlink(ent->d_name, extra, sb.st_size) < 0)
				goto fail_rdlink;

			extra[sb.st_size] = '\0';
		} else if (S_ISREG(sb.st_mode)) {
			extra = get_file_path(root, ent->d_name);
			if (extra == NULL)
				goto fail;
		}

		if (!(flags & DIR_SCAN_KEEP_TIME))
			sb.st_mtim = fs->defaults.st_mtim;

		n = fstree_mknode(root, ent->d_name, strlen(ent->d_name),
				  extra, &sb);
		if (n == NULL) {
			perror("creating tree node");
			goto fail;
		}

		if (sqfs_xattr_writer_begin(xwr)) {
			fputs("error recoding xattr key-value pairs\n", stderr);
			return -1;
		}

#ifdef HAVE_SYS_XATTR_H
		if (flags & DIR_SCAN_READ_XATTR) {
			if (populate_xattr(xwr, n))
				goto fail;
		}
#endif
		if (selinux_handle != NULL) {
			path = fstree_get_path(n);
			if (path == NULL) {
				perror("getting full path for "
				       "SELinux relabeling");
				goto fail;
			}

			if (selinux_relable_node(selinux_handle, xwr,
						 n, path)) {
				free(path);
				goto fail;
			}

			free(path);
		}

		if (sqfs_xattr_writer_end(xwr, &n->xattr_idx)) {
			fputs("error generating xattr index\n", stderr);
			return -1;
		}

		free(extra);
		extra = NULL;
	}

	closedir(dir);

	for (n = root->data.dir.children; n != NULL; n = n->next) {
		if (S_ISDIR(n->mode)) {
			if (pushd(n->name))
				return -1;

			if (populate_dir(fs, n, devstart, selinux_handle,
					 xwr, flags)) {
				return -1;
			}

			if (popd())
				return -1;
		}
	}

	return 0;
fail_rdlink:
	perror("readlink");
fail:
	closedir(dir);
	free(extra);
	return -1;
}

int fstree_from_dir(fstree_t *fs, const char *path, void *selinux_handle,
		    sqfs_xattr_writer_t *xwr, unsigned int flags)
{
	struct stat sb;
	int ret;

	if (stat(path, &sb)) {
		perror(path);
		return -1;
	}

	if (pushd(path))
		return -1;

	ret = populate_dir(fs, fs->root, sb.st_dev, selinux_handle,
			   xwr, flags);

	if (popd())
		ret = -1;

	return ret;
}