summaryrefslogtreecommitdiff
path: root/cmd/srv9p/static9p.c
blob: f508ff1b32db1b17265e94426eaaa701232977ac (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
/* srv9p/static9p.c - Serve static files over 9P
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-Licence-Identifier: AGPL-3.0-or-later
 */

#include <assert.h>

#include "static9p.h"

#define UNUSED(name) /* name __attribute__((unused)) */
#define p9_str(cstr)    ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr })
#define p9_nulstr       ((struct lib9p_s){ .len = 0, .utf8 = NULL })

/* common metadata ************************************************************/

static struct lib9p_srv_file *static_metadata_clone(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_metadata *file = (struct static_metadata *)_file;
	assert(file);

	return &file->header;
}

static void static_metadata_free(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_metadata *file = (struct static_metadata *)_file;
	assert(file);

	/* do nothing */
}

static uint32_t static_metadata_io(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, lib9p_o_t UNUSED(flags)) {
	assert(ctx);
	struct static_metadata *file = (struct static_metadata *)_file;
	assert(file);

	return 0;
}

static void static_metadata_wstat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file,
                              struct lib9p_stat UNUSED(new)) {
	assert(ctx);
	struct static_metadata *file = (struct static_metadata *)_file;
	assert(file);

	lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
}

static void static_metadata_remove(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_metadata *file = (struct static_metadata *)_file;
	assert(file);

	lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
}

/* dir ************************************************************************/

static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_dir *file = (struct static_dir *)_file;
	assert(file);

	return (struct lib9p_stat){
		.kern_type                      = 0,
		.kern_dev                       = 0,
		.file_qid                       = {
			.type = LIB9P_QT_DIR,
			.vers = 1,
			.path = file->metadata.pathnum,
		},
		.file_mode                = LIB9P_DM_DIR | (file->metadata.perm & 0555),
		.file_atime               = file->metadata.atime,
		.file_mtime               = file->metadata.mtime,
		.file_size                = 0,
		.file_name                = p9_str(file->metadata.name),
		.file_owner_uid           = p9_str(file->metadata.u_name),
		.file_owner_gid           = p9_str(file->metadata.g_name),
		.file_last_modified_uid   = p9_str(file->metadata.m_name),
		.file_extension           = p9_nulstr,
		.file_owner_n_uid         = file->metadata.u_num,
		.file_owner_n_gid         = file->metadata.g_num,
		.file_last_modified_n_uid = file->metadata.m_num,
	};
}

static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir,
                                               char *childname) {
	assert(ctx);
	struct static_dir *dir = (struct static_dir *)_dir;
	assert(dir);

	for (size_t i = 0; dir->members[i]; i++) {
		struct lib9p_srv_file *filep = dir->members[i];
		struct lib9p_stat stat = filep->vtable->stat(ctx, filep);
		if (lib9p_ctx_has_error(&ctx->basectx))
			break;
		lib9p_assert_stat(stat);
		if (strcmp(stat.file_name.utf8, childname) == 0)
			return filep;
	}
	lib9p_error(&ctx->basectx,
	            LINUX_ENOENT, "no such file or directory");
	return NULL;
}

static struct lib9p_srv_file *static_dir_dcreate(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir,
                                                 char *UNUSED(childname),
                                                 lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) {
	assert(ctx);
	struct static_dir *dir = (struct static_dir *)_dir;
	assert(dir);

	lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
	return NULL;
}

static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir,
                               uint8_t *buf,
                               uint32_t byte_count,
                               size_t _obj_offset) {
	assert(ctx);
	struct static_dir *dir = (struct static_dir *)_dir;
	assert(dir);

	uint32_t byte_offset = 0;
	size_t obj_offset = _obj_offset;
	while (dir->members[obj_offset]) {
		struct lib9p_srv_file *filep = dir->members[obj_offset];
		struct lib9p_stat stat = filep->vtable->stat(ctx, filep);
		if (lib9p_ctx_has_error(&ctx->basectx))
			break;
		lib9p_assert_stat(stat);
		uint32_t nbytes = lib9p_marshal_stat(&ctx->basectx, byte_count-byte_offset, &stat,
		                                     &buf[byte_offset]);
		if (!nbytes) {
			if (obj_offset == _obj_offset)
				lib9p_error(&ctx->basectx,
				            LINUX_ERANGE, "stat object does not fit into negotiated max message size");
			break;
		}
		byte_offset += nbytes;
		obj_offset++;
	}
	return obj_offset - _obj_offset;
}

struct lib9p_srv_file_vtable static_dir_vtable = {
	.clone   = static_metadata_clone,
	.free    = static_metadata_free,

	.io      = static_metadata_io,
	.stat    = static_dir_stat,
	.wstat   = static_metadata_wstat,
	.remove  = static_metadata_remove,

	.dopen   = static_dir_dopen,
	.dcreate = static_dir_dcreate,

	.dread   = static_dir_dread,
};

/* file ***********************************************************************/

static inline size_t static_file_size(struct static_file *file) {
	assert(file);

	assert(file->data_start);
	if (!file->data_end)
		return file->data_size;
	return (size_t)((uintptr_t)file->data_end - (uintptr_t)file->data_start);
}

static struct lib9p_stat static_file_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_file *file = (struct static_file *)_file;
	assert(file);

	return (struct lib9p_stat){
		.kern_type                      = 0,
		.kern_dev                       = 0,
		.file_qid                       = {
			.type = LIB9P_QT_FILE,
			.vers = 1,
			.path = file->metadata.pathnum,
		},
		.file_mode                = file->metadata.perm & 0444,
		.file_atime               = file->metadata.atime,
		.file_mtime               = file->metadata.mtime,
		.file_size                = (uint64_t)static_file_size(file),
		.file_name                = p9_str(file->metadata.name),
		.file_owner_uid           = p9_str(file->metadata.u_name),
		.file_owner_gid           = p9_str(file->metadata.g_name),
		.file_last_modified_uid   = p9_str(file->metadata.m_name),
		.file_extension           = p9_nulstr,
		.file_owner_n_uid         = file->metadata.u_num,
		.file_owner_n_gid         = file->metadata.g_num,
		.file_last_modified_n_uid = file->metadata.m_num,
	};
}

static uint32_t static_file_pread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file,
                                void *buf,
                                uint32_t byte_count,
                                uint64_t byte_offset) {
	assert(ctx);
	struct static_file *file = (struct static_file *)_file;
	assert(file);

	size_t data_size = static_file_size(file);

	if (byte_offset > (uint64_t)data_size) {
		lib9p_error(&ctx->basectx,
		            LINUX_EINVAL, "offset is past end-of-file length");
		return 0;
	}

	size_t beg_off = (size_t)byte_offset;
	size_t end_off = beg_off + (size_t)byte_count;
	if (end_off > data_size)
		end_off = data_size;
	memcpy(buf, &file->data_start[beg_off], end_off-beg_off);
	return (uint32_t)(end_off-beg_off);
}

struct lib9p_srv_file_vtable static_file_vtable = {
	.clone   = static_metadata_clone,
	.free    = static_metadata_free,

	.io      = static_metadata_io,
	.stat    = static_file_stat,
	.wstat   = static_metadata_wstat,
	.remove  = static_metadata_remove,

	.pread   = static_file_pread,
	.pwrite  = NULL,
};