summaryrefslogtreecommitdiff
path: root/src/user/lib/fs/misc.c
blob: 95fef6041bbc9fbb5c4998010716a8e26649497e (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
#include <camellia/flags.h>
#include <camellia/syscalls.h>
#include <shared/mem.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <user/lib/fs/dir.h>
#include <user/lib/fs/misc.h>

bool fork2_n_mount(const char *path) {
	handle_t h;
	if (_syscall_fork(FORK_NEWFS, &h) > 0) { /* parent */
		_syscall_mount(h, path, strlen(path));
		close(h);
		return true;
	}
	return false;
}

void fs_passthru(const char *prefix) {
	struct fs_wait_response res;
	const size_t buf_len = 1024;
	char *buf = malloc(buf_len);
	int prefix_len = prefix ? strlen(prefix) : 0;
	if (!buf) exit(1);

	while (!_syscall_fs_wait(buf, buf_len, &res)) {
		switch (res.op) {
			case VFSOP_OPEN:
				if (prefix) {
					if (prefix_len + res.len > buf_len) {
						_syscall_fs_respond(NULL, -1, 0);
						break;
					}

					// TODO memmove
					char tmp[64];
					memcpy(tmp, buf, res.len);
					memcpy(buf + prefix_len, tmp, res.len);
					memcpy(buf, prefix, prefix_len);
					res.len += prefix_len;
				}
				_syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
				break;

			default:
				_syscall_fs_respond(NULL, -1, 0);
				break;
		}
	}
	exit(0);
}

void fs_whitelist(const char **list) {
	struct fs_wait_response res;
	const size_t buf_len = 1024;
	char *buf = malloc(buf_len);
	char *ipath;
	bool passthru, inject;
	if (!buf) exit(1);

	while (!_syscall_fs_wait(buf, buf_len, &res)) {
		ipath = res.id;
		switch (res.op) {
			case VFSOP_OPEN:
				passthru = false;
				inject = false;

				for (const char **iter = list; *iter; iter++) {
					size_t len = strlen(*iter);
					if (len <= res.len && !memcmp(buf, *iter, len)) {
						passthru = true;
						break;
					}
					if (res.len < len &&
						buf[res.len - 1] == '/' &&
						!memcmp(buf, *iter, res.len))
					{
						inject = true;
					}
				}
				if (passthru) {
					_syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
				} else if (inject) {
					// TODO all the inject points could be precomputed
					ipath = malloc(res.len + 1);
					memcpy(ipath, buf, res.len);
					ipath[res.len] = '\0';
					_syscall_fs_respond(ipath, 0, 0);
				} else {
					_syscall_fs_respond(NULL, -1, 0);
				}
				break;

			case VFSOP_READ:
				struct dirbuild db;
				dir_start(&db, res.offset, buf, buf_len);
				size_t blen = strlen(ipath);
				for (const char **iter = list; *iter; iter++) {
					// TODO could be precomputed too
					size_t len = strlen(*iter); // inefficient, whatever
					if (blen < len && !memcmp(ipath, *iter, blen))
					{
						/* inject up to the next slash */
						// TODO separate out into its own function
						int ilen = 0;
						while ((*iter)[blen + ilen]) {
							if ((*iter)[blen + ilen] == '/') {
								ilen++;
								break;
							}
							ilen++;
						}
						dir_appendl(&db, (*iter) + blen, ilen);
					}
				}
				_syscall_fs_respond(buf, dir_finish(&db), 0);
				break;

			case VFSOP_CLOSE:
				free(ipath);
				_syscall_fs_respond(NULL, 0, 0);
				break;

			default:
				_syscall_fs_respond(NULL, -1, 0);
				break;
		}
	}
	exit(0);
}


void fs_dir_inject(const char *path) {
	struct fs_dir_handle {
		const char *inject;
		int delegate, inject_len;
	};

	const size_t path_len = strlen(path);
	struct fs_wait_response res;
	struct fs_dir_handle *data;
	const size_t buf_len = 1024;
	char *buf = malloc(buf_len);
	int inject_len;
	struct dirbuild db;

	if (!buf) exit(1);

	while (!_syscall_fs_wait(buf, buf_len, &res)) {
		data = res.id;
		switch (res.op) {
			case VFSOP_OPEN:
				if (buf[res.len - 1] == '/' &&
						res.len < path_len && !memcmp(path, buf, res.len))
				{
					/* opening a directory that we're injecting into */

					data = malloc(sizeof *data);
					data->delegate = _syscall_open(buf, res.len, res.flags);
					data->inject = path + res.len;

					/* inject up to the next slash */
					inject_len = 0;
					while (data->inject[inject_len]) {
						if (data->inject[inject_len] == '/') {
							inject_len++; // include the slash
							break;
						}
						inject_len++;
					}
					data->inject_len = inject_len;

					_syscall_fs_respond(data, 0, 0);
				} else {
					_syscall_fs_respond(NULL, _syscall_open(buf, res.len, res.flags), FSR_DELEGATE);
				}
				break;

			case VFSOP_CLOSE:
				if (data->delegate >= 0)
					close(data->delegate);
				free(data);
				_syscall_fs_respond(NULL, 0, 0);
				break;

			case VFSOP_READ:
				// TODO optimization - min(buf_len, res.capacity)
				dir_start(&db, res.offset, buf, buf_len);
				dir_appendl(&db, data->inject, data->inject_len);
				if (data->delegate >= 0)
					dir_append_from(&db, data->delegate);
				_syscall_fs_respond(buf, dir_finish(&db), 0);
				break;

			default:
				_syscall_fs_respond(NULL, -1, 0);
				break;
		}
	}
	exit(0);
}