diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-12-11 10:22:19 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-12-11 10:22:19 -0700 |
commit | 4b918e6f721f34e4014fa3f0b5032037991e321f (patch) | |
tree | 8340bed315d952937e97fcc6838b127692a12062 | |
parent | 6fd807721b4a662c57fcd363607de17a1e86d6be (diff) |
Pull out lib9p_util from srv9p
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | cmd/srv9p/CMakeLists.txt | 2 | ||||
-rw-r--r-- | cmd/srv9p/main.c | 28 | ||||
-rw-r--r-- | lib9p_util/CMakeLists.txt | 13 | ||||
-rw-r--r-- | lib9p_util/include/util9p/static.h (renamed from cmd/srv9p/static9p.h) | 22 | ||||
-rw-r--r-- | lib9p_util/static.c (renamed from cmd/srv9p/static9p.c) | 108 |
6 files changed, 94 insertions, 80 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 3948eea..e7b4682 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -128,6 +128,7 @@ add_subdirectory(libhw) add_subdirectory(libdhcp) add_subdirectory(libusb) add_subdirectory(lib9p) +add_subdirectory(lib9p_util) add_subdirectory(cmd/sbc_harness) add_subdirectory(cmd/srv9p) diff --git a/cmd/srv9p/CMakeLists.txt b/cmd/srv9p/CMakeLists.txt index 4b6ec32..0d8e320 100644 --- a/cmd/srv9p/CMakeLists.txt +++ b/cmd/srv9p/CMakeLists.txt @@ -9,7 +9,6 @@ if (PICO_PLATFORM STREQUAL "host") add_library(srv9p_objs OBJECT main.c - static9p.c ) target_include_directories(srv9p_objs PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/config) target_include_directories(srv9p_objs PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) @@ -18,6 +17,7 @@ target_link_libraries(srv9p_objs libcr_ipc libmisc lib9p + lib9p_util ) # Analyze the stack ############################################################ diff --git a/cmd/srv9p/main.c b/cmd/srv9p/main.c index 05368e5..394bf84 100644 --- a/cmd/srv9p/main.c +++ b/cmd/srv9p/main.c @@ -11,8 +11,8 @@ #include <libhw/generic/net.h> #include <libhw/generic/alarmclock.h> #include <libhw/host_net.h> +#include <util9p/static.h> -#include "static9p.h" #include "static.h" /* configuration **************************************************************/ @@ -29,8 +29,8 @@ /* file tree ******************************************************************/ -#define FILE_COMMON(NAME) { \ - .vtable = &static_file_vtable, \ +#define FILE_COMMON(NAME) { \ + .vtable = &util9p_static_file_vtable, \ \ .u_name = "root", .u_num = 0, /* owner user */ \ .g_name = "root", .g_num = 0, /* owner group */ \ @@ -43,8 +43,8 @@ .mtime = 1728337904, \ } -#define DIR_COMMON(NAME) { \ - .vtable = &static_dir_vtable, \ +#define DIR_COMMON(NAME) { \ + .vtable = &util9p_static_dir_vtable, \ \ .u_name = "root", .u_num = 0, /* owner user */ \ .g_name = "root", .g_num = 0, /* owner group */ \ @@ -57,18 +57,18 @@ .mtime = 1728337904, \ } -#define STATIC_FILE(STRNAME, SYMNAME) \ - &((static struct static_file){ \ - ._static_common = FILE_COMMON(STRNAME), \ - .data_start = _binary_static_##SYMNAME##_start, \ - .data_end = _binary_static_##SYMNAME##_end, \ +#define STATIC_FILE(STRNAME, SYMNAME) \ + &((static struct util9p_static_file){ \ + ._util9p_static_common = FILE_COMMON(STRNAME), \ + .data_start = _binary_static_##SYMNAME##_start, \ + .data_end = _binary_static_##SYMNAME##_end, \ }) -static struct static_dir root = { - ._static_common = DIR_COMMON(""), +static struct util9p_static_dir root = { + ._util9p_static_common = DIR_COMMON(""), .members = { - &((static struct static_dir){ - ._static_common = DIR_COMMON("Documentation"), + &((static struct util9p_static_dir){ + ._util9p_static_common = DIR_COMMON("Documentation"), .members = { STATIC_FILE("x", Documentation_x), NULL 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/cmd/srv9p/static9p.h b/lib9p_util/include/util9p/static.h index 7a3d476..9ec03ef 100644 --- a/cmd/srv9p/static9p.h +++ b/lib9p_util/include/util9p/static.h @@ -1,11 +1,11 @@ -/* srv9p/static9p.h - Serve static files over 9P +/* 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 _SRV9P_STATIC9P_H_ -#define _SRV9P_STATIC9P_H_ +#ifndef _UTIL9P_STATIC_H_ +#define _UTIL9P_STATIC_H_ #include <lib9p/srv.h> @@ -23,25 +23,25 @@ typedef struct { char *name; lib9p_dm_t perm; uint32_t atime, mtime; -} _static_common; +} _util9p_static_common; -struct static_dir { - _static_common; +struct util9p_static_dir { + _util9p_static_common; /* NULL-terminated */ implements_lib9p_srv_file *members[]; }; -struct static_file { - _static_common; +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 static_dir_vtable; -extern struct lib9p_srv_file_vtable static_file_vtable; +extern struct lib9p_srv_file_vtable util9p_static_dir_vtable; +extern struct lib9p_srv_file_vtable util9p_static_file_vtable; -#endif /* _SRV9P_STATIC9P_H_ */ +#endif /* _UTIL9P_STATIC_H_ */ diff --git a/cmd/srv9p/static9p.c b/lib9p_util/static.c index b5b18e0..fd6f154 100644 --- a/cmd/srv9p/static9p.c +++ b/lib9p_util/static.c @@ -1,4 +1,4 @@ -/* srv9p/static9p.c - Serve static files over 9P +/* 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 @@ -7,7 +7,7 @@ #include <libmisc/assert.h> #include <libmisc/vcall.h> -#include "static9p.h" +#include <util9p/static.h> #define UNUSED(name) #define p9_str(cstr) ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr }) @@ -15,41 +15,41 @@ /* common *********************************************************************/ -static implements_lib9p_srv_file *static_common_clone(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); +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 static_common_free(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _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 static_common_io(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, lib9p_o_t UNUSED(flags)) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); +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 static_common_wstat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, +static void util9p_static_common_wstat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, struct lib9p_stat UNUSED(new)) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + _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 static_common_remove(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); +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); @@ -58,8 +58,8 @@ static void static_common_remove(implements_lib9p_srv_file *_self, struct lib9p_ /* dir ************************************************************************/ -static struct lib9p_stat static_dir_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); +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); @@ -86,9 +86,9 @@ static struct lib9p_stat static_dir_stat(implements_lib9p_srv_file *_self, struc }; } -static implements_lib9p_srv_file *static_dir_dopen(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - char *childname) { - struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); +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); @@ -106,10 +106,10 @@ static implements_lib9p_srv_file *static_dir_dopen(implements_lib9p_srv_file *_s return NULL; } -static implements_lib9p_srv_file *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 static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); +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); @@ -117,11 +117,11 @@ static implements_lib9p_srv_file *static_dir_dcreate(implements_lib9p_srv_file * return NULL; } -static size_t 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 static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); +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); @@ -147,24 +147,24 @@ static size_t static_dir_dread(implements_lib9p_srv_file *_self, struct lib9p_sr return obj_offset - _obj_offset; } -struct lib9p_srv_file_vtable static_dir_vtable = { - .clone = static_common_clone, - .free = static_common_free, +struct lib9p_srv_file_vtable util9p_static_dir_vtable = { + .clone = util9p_static_common_clone, + .free = util9p_static_common_free, - .io = static_common_io, - .stat = static_dir_stat, - .wstat = static_common_wstat, - .remove = static_common_remove, + .io = util9p_static_common_io, + .stat = util9p_static_dir_stat, + .wstat = util9p_static_common_wstat, + .remove = util9p_static_common_remove, - .dopen = static_dir_dopen, - .dcreate = static_dir_dcreate, + .dopen = util9p_static_dir_dopen, + .dcreate = util9p_static_dir_dcreate, - .dread = static_dir_dread, + .dread = util9p_static_dir_dread, }; /* file ***********************************************************************/ -static inline size_t static_file_size(struct static_file *file) { +static inline size_t util9p_static_file_size(struct util9p_static_file *file) { assert(file); #if __unix__ @@ -175,8 +175,8 @@ static inline size_t static_file_size(struct static_file *file) { return (size_t)((uintptr_t)file->data_end - (uintptr_t)file->data_start); } -static struct lib9p_stat static_file_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { - struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self); +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); @@ -191,7 +191,7 @@ static struct lib9p_stat static_file_stat(implements_lib9p_srv_file *_self, stru .file_mode = self->perm & 0444, .file_atime = self->atime, .file_mtime = self->mtime, - .file_size = (uint64_t)static_file_size(self), + .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), @@ -203,15 +203,15 @@ static struct lib9p_stat static_file_stat(implements_lib9p_srv_file *_self, stru }; } -static uint32_t static_file_pread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, - void *buf, - uint32_t byte_count, - uint64_t byte_offset) { - struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self); +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 = static_file_size(self); + size_t data_size = util9p_static_file_size(self); if (byte_offset > (uint64_t)data_size) { lib9p_error(&ctx->basectx, @@ -227,15 +227,15 @@ static uint32_t static_file_pread(implements_lib9p_srv_file *_self, struct lib9p return (uint32_t)(end_off-beg_off); } -struct lib9p_srv_file_vtable static_file_vtable = { - .clone = static_common_clone, - .free = static_common_free, +struct lib9p_srv_file_vtable util9p_static_file_vtable = { + .clone = util9p_static_common_clone, + .free = util9p_static_common_free, - .io = static_common_io, - .stat = static_file_stat, - .wstat = static_common_wstat, - .remove = static_common_remove, + .io = util9p_static_common_io, + .stat = util9p_static_file_stat, + .wstat = util9p_static_common_wstat, + .remove = util9p_static_common_remove, - .pread = static_file_pread, + .pread = util9p_static_file_pread, .pwrite = NULL, }; |