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
|
/* lib9p_util/static.c - Serve static files over 9P
*
* Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libmisc/assert.h>
#include <libmisc/macro.h>
#include <util9p/static.h>
LO_IMPLEMENTATION_C(lib9p_srv_file, struct util9p_static_dir, util9p_static_dir, static);
LO_IMPLEMENTATION_C(lib9p_srv_file, struct util9p_static_file, util9p_static_file, static);
LO_IMPLEMENTATION_H(lib9p_srv_dio, struct util9p_static_dir, util9p_static_dir);
LO_IMPLEMENTATION_C(lib9p_srv_dio, struct util9p_static_dir, util9p_static_dir, static);
LO_IMPLEMENTATION_H(lib9p_srv_fio, struct util9p_static_file, util9p_static_file);
LO_IMPLEMENTATION_C(lib9p_srv_fio, struct util9p_static_file, util9p_static_file, static);
/* dir ************************************************************************/
static void util9p_static_dir_free(struct util9p_static_dir *self) {
assert(self);
}
static struct lib9p_qid util9p_static_dir_qid(struct util9p_static_dir *self) {
assert(self);
return (struct lib9p_qid){
.type = LIB9P_QT_DIR,
.vers = 1,
.path = self->pathnum,
};
}
static struct lib9p_srv_stat util9p_static_dir_stat(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx) {
assert(self);
assert(ctx);
return (struct lib9p_srv_stat){
.qid = util9p_static_dir_qid(self),
.mode = LIB9P_DM_DIR | (self->perm & 0555),
.atime_sec = self->atime,
.mtime_sec = self->mtime,
.size = 0,
.name = lib9p_str(self->name),
.owner_uid = { .name = lib9p_str(self->u_name), .num = self->u_num },
.owner_gid = { .name = lib9p_str(self->g_name), .num = self->g_num },
.last_modifier_uid = { .name = lib9p_str(self->m_name), .num = self->m_num },
.extension = lib9p_str(NULL),
};
}
static void util9p_static_dir_wstat(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx,
struct lib9p_srv_stat) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
}
static void util9p_static_dir_remove(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
}
static lo_interface lib9p_srv_file util9p_static_dir_dwalk(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx,
struct lib9p_s childname) {
assert(self);
assert(ctx);
for (size_t i = 0; !LO_IS_NULL(self->members[i]); i++) {
lo_interface lib9p_srv_file file = self->members[i];
struct lib9p_srv_stat stat = LO_CALL(file, stat, ctx);
if (lib9p_ctx_has_error(&ctx->basectx))
break;
lib9p_srv_stat_assert(stat);
if (lib9p_str_eq(stat.name, childname))
return file;
}
lib9p_error(&ctx->basectx,
LIB9P_ERRNO_L_ENOENT, "no such file or directory");
return LO_NULL(lib9p_srv_file);
}
static lo_interface lib9p_srv_file util9p_static_dir_dcreate(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx,
struct lib9p_s LM_UNUSED(childname),
struct lib9p_srv_userid *LM_UNUSED(user),
struct lib9p_srv_userid *LM_UNUSED(group),
lib9p_dm_t LM_UNUSED(perm)) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
return LO_NULL(lib9p_srv_file);
}
LIB9P_SRV_NOTFILE(struct util9p_static_dir, util9p_static_dir);
static lo_interface lib9p_srv_dio util9p_static_dir_dopen(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx) {
assert(self);
assert(ctx);
return lo_box_util9p_static_dir_as_lib9p_srv_dio(self);
}
static void util9p_static_dir_iofree(struct util9p_static_dir *self) {
assert(self);
}
static struct lib9p_srv_dirent util9p_static_dir_dread(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx, size_t idx) {
assert(self);
assert(ctx);
lo_interface lib9p_srv_file file = self->members[idx];
if (LO_IS_NULL(file))
return (struct lib9p_srv_dirent){};
struct lib9p_srv_stat stat = LO_CALL(file, stat, ctx);
if (lib9p_ctx_has_error(&ctx->basectx))
return (struct lib9p_srv_dirent){};
lib9p_srv_stat_assert(stat);
return (struct lib9p_srv_dirent){
.qid = stat.qid,
.name = stat.name,
};
}
/* file ***********************************************************************/
static void util9p_static_file_free(struct util9p_static_file *self) {
assert(self);
}
static struct lib9p_qid util9p_static_file_qid(struct util9p_static_file *self) {
assert(self);
return (struct lib9p_qid){
.type = LIB9P_QT_FILE,
.vers = 1,
.path = self->pathnum,
};
}
static inline size_t util9p_static_file_size(struct util9p_static_file *file) {
assert(file);
#if __unix__
assert(file->data_start);
#endif
if (!file->data_end)
return file->data_size;
return (size_t)((uintptr_t)file->data_end - (uintptr_t)file->data_start);
}
static struct lib9p_srv_stat util9p_static_file_stat(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx) {
assert(self);
assert(ctx);
return (struct lib9p_srv_stat){
.qid = util9p_static_file_qid(self),
.mode = self->perm & 0444,
.atime_sec = self->atime,
.mtime_sec = self->mtime,
.size = (uint64_t)util9p_static_file_size(self),
.name = lib9p_str(self->name),
.owner_uid = { .name = lib9p_str(self->u_name), .num = self->u_num },
.owner_gid = { .name = lib9p_str(self->g_name), .num = self->g_num },
.last_modifier_uid = { .name = lib9p_str(self->m_name), .num = self->m_num },
.extension = lib9p_str(NULL),
};
}
static void util9p_static_file_wstat(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx,
struct lib9p_srv_stat) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
}
static void util9p_static_file_remove(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
}
LIB9P_SRV_NOTDIR(struct util9p_static_file, util9p_static_file);
static lo_interface lib9p_srv_fio util9p_static_file_fopen(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx,
bool rd, bool wr, bool trunc) {
assert(self);
assert(ctx);
assert(rd);
assert(!wr);
assert(!trunc);
return lo_box_util9p_static_file_as_lib9p_srv_fio(self);
}
static void util9p_static_file_iofree(struct util9p_static_file *self) {
assert(self);
}
static uint32_t util9p_static_file_iounit(struct util9p_static_file *self) {
assert(self);
return 0;
}
static void util9p_static_file_pread(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx,
uint32_t byte_count, uint64_t byte_offset,
struct iovec *ret) {
assert(self);
assert(ctx);
assert(ret);
size_t data_size = util9p_static_file_size(self);
if (byte_offset > (uint64_t)data_size) {
lib9p_error(&ctx->basectx,
LIB9P_ERRNO_L_EINVAL, "offset is past end-of-file length");
return;
}
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;
ret->iov_base = &self->data_start[beg_off];
ret->iov_len = end_off-beg_off;
}
static uint32_t util9p_static_file_pwrite(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx,
void *LM_UNUSED(buf),
uint32_t LM_UNUSED(byte_count),
uint64_t LM_UNUSED(byte_offset)) {
assert(self);
assert(ctx);
lib9p_error(&ctx->basectx, LIB9P_ERRNO_L_EROFS, "read-only part of filesystem");
return 0;
}
|