diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-02-10 00:06:34 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-02-10 00:06:34 -0700 |
commit | 6cd125e1ffd44fdf62c44d22c519561a8c9d7268 (patch) | |
tree | bf2b4a8225fe0d6f00084a1577c70bb564a20600 /lib9p_util | |
parent | f466cff948ec638f26a9a77a391904ebe03c4dfb (diff) | |
parent | a51875001eb672d73c9d84d44bb32abce327b931 (diff) |
Merge branch 'lukeshu/libobj-simple'
Diffstat (limited to 'lib9p_util')
-rw-r--r-- | lib9p_util/include/util9p/static.h | 12 | ||||
-rw-r--r-- | lib9p_util/static.c | 177 |
2 files changed, 87 insertions, 102 deletions
diff --git a/lib9p_util/include/util9p/static.h b/lib9p_util/include/util9p/static.h index 9ec03ef..4afdb51 100644 --- a/lib9p_util/include/util9p/static.h +++ b/lib9p_util/include/util9p/static.h @@ -1,6 +1,6 @@ /* util9p/static.h - Serve static files over 9P * - * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -10,8 +10,6 @@ #include <lib9p/srv.h> typedef struct { - implements_lib9p_srv_file; - char *u_name; uint32_t u_num; char *g_name; @@ -29,9 +27,9 @@ struct util9p_static_dir { _util9p_static_common; /* NULL-terminated */ - implements_lib9p_srv_file *members[]; + lo_interface lib9p_srv_file members[]; }; - +LO_IMPLEMENTATION_H(lib9p_srv_file, struct util9p_static_dir, util9p_static_dir); struct util9p_static_file { _util9p_static_common; @@ -40,8 +38,6 @@ struct util9p_static_file { 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; +LO_IMPLEMENTATION_H(lib9p_srv_file, struct util9p_static_file, util9p_static_file); #endif /* _UTIL9P_STATIC_H_ */ diff --git a/lib9p_util/static.c b/lib9p_util/static.c index 0f0efad..a6ea8f6 100644 --- a/lib9p_util/static.c +++ b/lib9p_util/static.c @@ -6,49 +6,18 @@ #include <libmisc/assert.h> #include <libmisc/macro.h> -#include <libmisc/vcall.h> #include <util9p/static.h> -/* common *********************************************************************/ +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); -static void util9p_static_common_free(implements_lib9p_srv_file *_self) { - _util9p_static_common *self = VCALL_SELF(_util9p_static_common, implements_lib9p_srv_file, _self); - assert(self); - - /* do nothing */ -} - -static uint32_t util9p_static_common_chio(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - bool LM_UNUSED(rd), bool LM_UNUSED(wr), bool LM_UNUSED(trunc)) { - _util9p_static_common *self = VCALL_SELF(_util9p_static_common, implements_lib9p_srv_file, _self); - assert(self); - assert(ctx); - - return 0; -} +/* dir ************************************************************************/ -static void util9p_static_common_wstat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - struct lib9p_stat LM_UNUSED(new)) { - _util9p_static_common *self = VCALL_SELF(_util9p_static_common, implements_lib9p_srv_file, _self); +static void util9p_static_dir_free(struct util9p_static_dir *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_qid util9p_static_dir_qid(implements_lib9p_srv_file *_self) { - struct util9p_static_dir *self = VCALL_SELF(struct util9p_static_dir, implements_lib9p_srv_file, _self); +static struct lib9p_qid util9p_static_dir_qid(struct util9p_static_dir *self) { assert(self); return (struct lib9p_qid){ @@ -57,9 +26,13 @@ static struct lib9p_qid util9p_static_dir_qid(implements_lib9p_srv_file *_self) .path = self->pathnum, }; } +static uint32_t util9p_static_dir_chio(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx, bool, bool, bool) { + assert(self); + assert(ctx); + return 0; +} -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); +static struct lib9p_stat util9p_static_dir_stat(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx) { assert(self); assert(ctx); @@ -81,51 +54,61 @@ static struct lib9p_stat util9p_static_dir_stat(implements_lib9p_srv_file *_self .file_last_modified_n_uid = self->m_num, }; } +static void util9p_static_dir_wstat(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx, + struct lib9p_stat) { + assert(self); + assert(ctx); -static implements_lib9p_srv_file *util9p_static_dir_dopen(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - struct lib9p_s childname) { - struct util9p_static_dir *self = VCALL_SELF(struct util9p_static_dir, implements_lib9p_srv_file, _self); + lib9p_error(&ctx->basectx, LINUX_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); - 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); + lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); +} + +static lo_interface lib9p_srv_file util9p_static_dir_dopen(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_stat stat = LO_CALL(file, stat, ctx); if (lib9p_ctx_has_error(&ctx->basectx)) break; lib9p_stat_assert(stat); if (lib9p_str_eq(stat.file_name, childname)) - return filep; + return file; } lib9p_error(&ctx->basectx, LINUX_ENOENT, "no such file or directory"); - return NULL; + return LO_NULL(lib9p_srv_file); } -static implements_lib9p_srv_file *util9p_static_dir_dcreate(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - struct lib9p_s LM_UNUSED(childname), - lib9p_dm_t LM_UNUSED(perm), lib9p_o_t LM_UNUSED(flags)) { - struct util9p_static_dir *self = VCALL_SELF(struct util9p_static_dir, implements_lib9p_srv_file, _self); +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), + lib9p_dm_t LM_UNUSED(perm), lib9p_o_t LM_UNUSED(flags)) { assert(self); assert(ctx); lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); - return NULL; + return LO_NULL(lib9p_srv_file); } -static size_t util9p_static_dir_dread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, +static size_t util9p_static_dir_dread(struct util9p_static_dir *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); + while (!LO_IS_NULL(self->members[obj_offset])) { + lo_interface lib9p_srv_file file = self->members[obj_offset]; + struct lib9p_stat stat = LO_CALL(file, stat, ctx); if (lib9p_ctx_has_error(&ctx->basectx)) break; lib9p_stat_assert(stat); @@ -143,22 +126,27 @@ static size_t util9p_static_dir_dread(implements_lib9p_srv_file *_self, struct l return obj_offset - _obj_offset; } -struct lib9p_srv_file_vtable util9p_static_dir_vtable = { - .free = util9p_static_common_free, - .qid = util9p_static_dir_qid, - .chio = util9p_static_common_chio, - - .stat = util9p_static_dir_stat, - .wstat = util9p_static_common_wstat, - .remove = util9p_static_common_remove, +LIB9P_SRV_NOTFILE(struct util9p_static_dir, util9p_static_dir) - .dopen = util9p_static_dir_dopen, - .dcreate = util9p_static_dir_dcreate, +/* file ***********************************************************************/ - .dread = util9p_static_dir_dread, -}; +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); -/* file ***********************************************************************/ + return (struct lib9p_qid){ + .type = LIB9P_QT_FILE, + .vers = 1, + .path = self->pathnum, + }; +} +static uint32_t util9p_static_file_chio(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx, bool, bool, bool) { + assert(self); + assert(ctx); + return 0; +} static inline size_t util9p_static_file_size(struct util9p_static_file *file) { assert(file); @@ -171,19 +159,8 @@ static inline size_t util9p_static_file_size(struct util9p_static_file *file) { return (size_t)((uintptr_t)file->data_end - (uintptr_t)file->data_start); } -static struct lib9p_qid util9p_static_file_qid(implements_lib9p_srv_file *_self) { - struct util9p_static_file *self = VCALL_SELF(struct util9p_static_file, implements_lib9p_srv_file, _self); - assert(self); - - return (struct lib9p_qid){ - .type = LIB9P_QT_FILE, - .vers = 1, - .path = self->pathnum, - }; -} -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); +static struct lib9p_stat util9p_static_file_stat(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx) { assert(self); assert(ctx); @@ -205,12 +182,26 @@ static struct lib9p_stat util9p_static_file_stat(implements_lib9p_srv_file *_sel .file_last_modified_n_uid = self->m_num, }; } +static void util9p_static_file_wstat(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx, + struct lib9p_stat) { + assert(self); + assert(ctx); + + lib9p_error(&ctx->basectx, LINUX_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); -static uint32_t util9p_static_file_pread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, + lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); +} + +LIB9P_SRV_NOTDIR(struct util9p_static_file, util9p_static_file) + +static uint32_t util9p_static_file_pread(struct util9p_static_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); @@ -230,15 +221,13 @@ static uint32_t util9p_static_file_pread(implements_lib9p_srv_file *_self, struc return (uint32_t)(end_off-beg_off); } -struct lib9p_srv_file_vtable util9p_static_file_vtable = { - .free = util9p_static_common_free, - .qid = util9p_static_file_qid, - .chio = util9p_static_common_chio, - - .stat = util9p_static_file_stat, - .wstat = util9p_static_common_wstat, - .remove = util9p_static_common_remove, +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); - .pread = util9p_static_file_pread, - .pwrite = NULL, -}; + lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); + return 0; +} |