From affe77a2b068a9de236f0d7703092dbfafacff5e Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 29 Oct 2024 01:22:12 -0600 Subject: lib9p/9p.h: Tidy up --- lib9p/9p.c | 20 +++++++++++++++++++- lib9p/include/lib9p/9p.h | 47 ++++++++++++++++++++++------------------------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/lib9p/9p.c b/lib9p/9p.c index 9f0bd4c..9ea0c46 100644 --- a/lib9p/9p.c +++ b/lib9p/9p.c @@ -13,6 +13,21 @@ #include "internal.h" +/* ctx ************************************************************************/ + +void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) { + assert(ctx); +#ifdef CONFIG_9P_ENABLE_9P2000_u + ctx->err_num = 0; +#endif + ctx->err_msg[0] = '\0'; +} + +bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) { + assert(ctx); + return ctx->err_msg[0]; +} + int lib9p_error(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *msg) { if (lib9p_ctx_has_error(ctx)) return -1; @@ -49,12 +64,13 @@ int lib9p_errorf(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *fmt, . return -1; } - const char *lib9p_msg_type_str(struct lib9p_ctx *ctx, enum lib9p_msg_type typ) { assert(0 <= typ && typ <= 0xFF); return _lib9p_versions[ctx->version].msgs[typ].name; } +/* main message functions *****************************************************/ + ssize_t lib9p_validate(struct lib9p_ctx *ctx, uint8_t *net_bytes) { /* Inspect the first 5 bytes ourselves. */ struct _validate_ctx subctx = { @@ -115,6 +131,8 @@ bool lib9p_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, return table.marshal(&subctx, body); } +/* `struct lib9p_stat` helpers ************************************************/ + bool lib9p_validate_stat(struct lib9p_ctx *ctx, uint32_t net_size, uint8_t *net_bytes, uint32_t *ret_net_size, ssize_t *ret_host_size) { struct _validate_ctx subctx = { diff --git a/lib9p/include/lib9p/9p.h b/lib9p/include/lib9p/9p.h index bf304ac..171ad3b 100644 --- a/lib9p/include/lib9p/9p.h +++ b/lib9p/include/lib9p/9p.h @@ -24,13 +24,15 @@ #define CONFIG_9P_ENABLE_9P2000 #endif -/******************************************************************************/ +/* protocol definitions *******************************************************/ #include /* *after* config.h */ #define LIB9P_NOTAG ((uint16_t)~0U) #define LIB9P_NOFID ((uint32_t)~0U) +/* ctx ************************************************************************/ + struct lib9p_ctx { /* negotiated */ enum lib9p_version version; @@ -43,37 +45,19 @@ struct lib9p_ctx { char err_msg[CONFIG_9P_MAX_ERR_SIZE]; }; -static void lib9p_ctx_clear_error(struct lib9p_ctx *ctx) { - assert(ctx); -#ifdef CONFIG_9P_ENABLE_9P2000_u - ctx->err_num = 0; -#endif - ctx->err_msg[0] = '\0'; -} +void lib9p_ctx_clear_error(struct lib9p_ctx *ctx); -static bool lib9p_ctx_has_error(struct lib9p_ctx *ctx) { - assert(ctx); - return ctx->err_msg[0]; -} - -const char *lib9p_msg_type_str(struct lib9p_ctx *, enum lib9p_msg_type); - -/** Assert that a `struct lib9p_stat` object looks valid. */ -static inline void lib9p_assert_stat(struct lib9p_stat stat) { - assert( ((bool)(stat.file_mode & LIB9P_DM_DIR )) == ((bool)(stat.file_qid.type & LIB9P_QT_DIR )) ); - assert( ((bool)(stat.file_mode & LIB9P_DM_APPEND )) == ((bool)(stat.file_qid.type & LIB9P_QT_APPEND )) ); - assert( ((bool)(stat.file_mode & LIB9P_DM_EXCL )) == ((bool)(stat.file_qid.type & LIB9P_QT_EXCL )) ); - assert( ((bool)(stat.file_mode & _LIB9P_DM_PLAN9_MOUNT)) == ((bool)(stat.file_qid.type & _LIB9P_QT_PLAN9_MOUNT)) ); - assert( ((bool)(stat.file_mode & LIB9P_DM_AUTH )) == ((bool)(stat.file_qid.type & LIB9P_QT_AUTH )) ); - assert( ((bool)(stat.file_mode & LIB9P_DM_TMP )) == ((bool)(stat.file_qid.type & LIB9P_QT_TMP )) ); - assert( (stat.file_size == 0) || !(stat.file_mode & LIB9P_DM_DIR) ); -} +bool lib9p_ctx_has_error(struct lib9p_ctx *ctx); /** Write an static error into ctx, return -1. */ int lib9p_error(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *msg); /** Write a printf-style error into ctx, return -1. */ int lib9p_errorf(struct lib9p_ctx *ctx, uint32_t linux_errno, char const *fmt, ...); +const char *lib9p_msg_type_str(struct lib9p_ctx *, enum lib9p_msg_type); + +/* main message functions *****************************************************/ + /** * Validate a message's structure; it's size, string encodings, enums, * and bitfields. @@ -138,6 +122,19 @@ void lib9p_unmarshal(struct lib9p_ctx *ctx, uint8_t *net_bytes, bool lib9p_marshal(struct lib9p_ctx *ctx, enum lib9p_msg_type typ, void *body, uint8_t *ret_bytes); +/* `struct lib9p_stat` helpers ************************************************/ + +/** Assert that a `struct lib9p_stat` object looks valid. */ +static inline void lib9p_assert_stat(struct lib9p_stat stat) { + assert( ((bool)(stat.file_mode & LIB9P_DM_DIR )) == ((bool)(stat.file_qid.type & LIB9P_QT_DIR )) ); + assert( ((bool)(stat.file_mode & LIB9P_DM_APPEND )) == ((bool)(stat.file_qid.type & LIB9P_QT_APPEND )) ); + assert( ((bool)(stat.file_mode & LIB9P_DM_EXCL )) == ((bool)(stat.file_qid.type & LIB9P_QT_EXCL )) ); + assert( ((bool)(stat.file_mode & _LIB9P_DM_PLAN9_MOUNT)) == ((bool)(stat.file_qid.type & _LIB9P_QT_PLAN9_MOUNT)) ); + assert( ((bool)(stat.file_mode & LIB9P_DM_AUTH )) == ((bool)(stat.file_qid.type & LIB9P_QT_AUTH )) ); + assert( ((bool)(stat.file_mode & LIB9P_DM_TMP )) == ((bool)(stat.file_qid.type & LIB9P_QT_TMP )) ); + assert( (stat.file_size == 0) || !(stat.file_mode & LIB9P_DM_DIR) ); +} + /** * TODO * -- cgit v1.2.3-2-g168b