summaryrefslogtreecommitdiff
path: root/lib9p_util
diff options
context:
space:
mode:
Diffstat (limited to 'lib9p_util')
-rw-r--r--lib9p_util/CMakeLists.txt13
-rw-r--r--lib9p_util/include/util9p/static.h47
-rw-r--r--lib9p_util/static.c241
3 files changed, 301 insertions, 0 deletions
diff --git a/lib9p_util/CMakeLists.txt b/lib9p_util/CMakeLists.txt
new file mode 100644
index 0000000..7a42c0c
--- /dev/null
+++ b/lib9p_util/CMakeLists.txt
@@ -0,0 +1,13 @@
+# lib9p_util/CMakeLists.txt - TODO
+#
+# Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+# SPDX-License-Identifier: AGPL-3.0-or-later
+
+add_library(lib9p_util INTERFACE)
+target_include_directories(lib9p_util SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
+target_sources(lib9p_util INTERFACE
+ static.c
+)
+target_link_libraries(lib9p_util INTERFACE
+ lib9p
+)
diff --git a/lib9p_util/include/util9p/static.h b/lib9p_util/include/util9p/static.h
new file mode 100644
index 0000000..9ec03ef
--- /dev/null
+++ b/lib9p_util/include/util9p/static.h
@@ -0,0 +1,47 @@
+/* util9p/static.h - Serve static files over 9P
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#ifndef _UTIL9P_STATIC_H_
+#define _UTIL9P_STATIC_H_
+
+#include <lib9p/srv.h>
+
+typedef struct {
+ implements_lib9p_srv_file;
+
+ char *u_name;
+ uint32_t u_num;
+ char *g_name;
+ uint32_t g_num;
+ char *m_name;
+ uint32_t m_num;
+
+ uint64_t pathnum;
+ char *name;
+ lib9p_dm_t perm;
+ uint32_t atime, mtime;
+} _util9p_static_common;
+
+struct util9p_static_dir {
+ _util9p_static_common;
+
+ /* NULL-terminated */
+ implements_lib9p_srv_file *members[];
+};
+
+
+struct util9p_static_file {
+ _util9p_static_common;
+
+ char *data_start; /* must not be NULL */
+ char *data_end; /* may be NULL, in which case data_size is used */
+ size_t data_size; /* only used if .data_end==NULL */
+};
+
+extern struct lib9p_srv_file_vtable util9p_static_dir_vtable;
+extern struct lib9p_srv_file_vtable util9p_static_file_vtable;
+
+#endif /* _UTIL9P_STATIC_H_ */
diff --git a/lib9p_util/static.c b/lib9p_util/static.c
new file mode 100644
index 0000000..fd6f154
--- /dev/null
+++ b/lib9p_util/static.c
@@ -0,0 +1,241 @@
+/* lib9p_util/static.c - Serve static files over 9P
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libmisc/assert.h>
+#include <libmisc/vcall.h>
+
+#include <util9p/static.h>
+
+#define UNUSED(name)
+#define p9_str(cstr) ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr })
+#define p9_nulstr ((struct lib9p_s){ .len = 0, .utf8 = NULL })
+
+/* common *********************************************************************/
+
+static implements_lib9p_srv_file *util9p_static_common_clone(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ _util9p_static_common *self = VCALL_SELF(_util9p_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ return self;
+}
+
+static void util9p_static_common_free(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ _util9p_static_common *self = VCALL_SELF(_util9p_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ /* do nothing */
+}
+
+static uint32_t util9p_static_common_io(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, lib9p_o_t UNUSED(flags)) {
+ _util9p_static_common *self = VCALL_SELF(_util9p_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ return 0;
+}
+
+static void util9p_static_common_wstat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
+ struct lib9p_stat UNUSED(new)) {
+ _util9p_static_common *self = VCALL_SELF(_util9p_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
+}
+
+static void util9p_static_common_remove(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ _util9p_static_common *self = VCALL_SELF(_util9p_static_common, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
+}
+
+/* dir ************************************************************************/
+
+static struct lib9p_stat util9p_static_dir_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ struct util9p_static_dir *self = VCALL_SELF(struct util9p_static_dir, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ return (struct lib9p_stat){
+ .kern_type = 0,
+ .kern_dev = 0,
+ .file_qid = {
+ .type = LIB9P_QT_DIR,
+ .vers = 1,
+ .path = self->pathnum,
+ },
+ .file_mode = LIB9P_DM_DIR | (self->perm & 0555),
+ .file_atime = self->atime,
+ .file_mtime = self->mtime,
+ .file_size = 0,
+ .file_name = p9_str(self->name),
+ .file_owner_uid = p9_str(self->u_name),
+ .file_owner_gid = p9_str(self->g_name),
+ .file_last_modified_uid = p9_str(self->m_name),
+ .file_extension = p9_nulstr,
+ .file_owner_n_uid = self->u_num,
+ .file_owner_n_gid = self->g_num,
+ .file_last_modified_n_uid = self->m_num,
+ };
+}
+
+static implements_lib9p_srv_file *util9p_static_dir_dopen(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
+ char *childname) {
+ struct util9p_static_dir *self = VCALL_SELF(struct util9p_static_dir, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ for (size_t i = 0; self->members[i]; i++) {
+ implements_lib9p_srv_file *filep = self->members[i];
+ struct lib9p_stat stat = VCALL(filep, stat, ctx);
+ 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 implements_lib9p_srv_file *util9p_static_dir_dcreate(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
+ char *UNUSED(childname),
+ lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) {
+ struct util9p_static_dir *self = VCALL_SELF(struct util9p_static_dir, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
+ return NULL;
+}
+
+static size_t util9p_static_dir_dread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
+ uint8_t *buf,
+ uint32_t byte_count,
+ size_t _obj_offset) {
+ struct util9p_static_dir *self = VCALL_SELF(struct util9p_static_dir, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ uint32_t byte_offset = 0;
+ size_t obj_offset = _obj_offset;
+ while (self->members[obj_offset]) {
+ implements_lib9p_srv_file *filep = self->members[obj_offset];
+ struct lib9p_stat stat = VCALL(filep, stat, ctx);
+ 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 util9p_static_dir_vtable = {
+ .clone = util9p_static_common_clone,
+ .free = util9p_static_common_free,
+
+ .io = util9p_static_common_io,
+ .stat = util9p_static_dir_stat,
+ .wstat = util9p_static_common_wstat,
+ .remove = util9p_static_common_remove,
+
+ .dopen = util9p_static_dir_dopen,
+ .dcreate = util9p_static_dir_dcreate,
+
+ .dread = util9p_static_dir_dread,
+};
+
+/* file ***********************************************************************/
+
+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_stat util9p_static_file_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) {
+ struct util9p_static_file *self = VCALL_SELF(struct util9p_static_file, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ return (struct lib9p_stat){
+ .kern_type = 0,
+ .kern_dev = 0,
+ .file_qid = {
+ .type = LIB9P_QT_FILE,
+ .vers = 1,
+ .path = self->pathnum,
+ },
+ .file_mode = self->perm & 0444,
+ .file_atime = self->atime,
+ .file_mtime = self->mtime,
+ .file_size = (uint64_t)util9p_static_file_size(self),
+ .file_name = p9_str(self->name),
+ .file_owner_uid = p9_str(self->u_name),
+ .file_owner_gid = p9_str(self->g_name),
+ .file_last_modified_uid = p9_str(self->m_name),
+ .file_extension = p9_nulstr,
+ .file_owner_n_uid = self->u_num,
+ .file_owner_n_gid = self->g_num,
+ .file_last_modified_n_uid = self->m_num,
+ };
+}
+
+static uint32_t util9p_static_file_pread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx,
+ void *buf,
+ uint32_t byte_count,
+ uint64_t byte_offset) {
+ struct util9p_static_file *self = VCALL_SELF(struct util9p_static_file, implements_lib9p_srv_file, _self);
+ assert(self);
+ assert(ctx);
+
+ size_t data_size = util9p_static_file_size(self);
+
+ 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, &self->data_start[beg_off], end_off-beg_off);
+ return (uint32_t)(end_off-beg_off);
+}
+
+struct lib9p_srv_file_vtable util9p_static_file_vtable = {
+ .clone = util9p_static_common_clone,
+ .free = util9p_static_common_free,
+
+ .io = util9p_static_common_io,
+ .stat = util9p_static_file_stat,
+ .wstat = util9p_static_common_wstat,
+ .remove = util9p_static_common_remove,
+
+ .pread = util9p_static_file_pread,
+ .pwrite = NULL,
+};