diff options
Diffstat (limited to 'lib9p_util')
-rw-r--r-- | lib9p_util/include/util9p/static.h | 32 | ||||
-rw-r--r-- | lib9p_util/static.c | 240 |
2 files changed, 123 insertions, 149 deletions
diff --git a/lib9p_util/include/util9p/static.h b/lib9p_util/include/util9p/static.h index 0b391b8..4bb24c4 100644 --- a/lib9p_util/include/util9p/static.h +++ b/lib9p_util/include/util9p/static.h @@ -9,18 +9,12 @@ #include <lib9p/srv.h> -#define util9p_box(nam, obj) \ - ((struct lib9p_srv_file){ \ - .self = obj, \ - .vtable = (void*)&_lo_##nam##_lib9p_srv_file_vtable, \ - }) - #define UTIL9P_ATIME 1728337905 #define UTIL9P_MTIME 1728337904 /* Common *********************************************************************/ -typedef struct { +struct _util9p_static_common { char *u_name; uint32_t u_num; char *g_name; @@ -32,7 +26,7 @@ typedef struct { char *name; lib9p_dm_t perm; uint32_t atime, mtime; -} _util9p_static_common; +}; #define UTIL9P_STATIC_COMMON(PATH, STRNAME, MODE) \ { \ @@ -50,36 +44,34 @@ typedef struct { /* Dir ************************************************************************/ struct util9p_static_dir { - _util9p_static_common; + struct _util9p_static_common c; /* NULL-terminated */ lo_interface lib9p_srv_file members[]; }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct util9p_static_dir, util9p_static_dir); -#define lo_box_util9p_static_dir_as_lib9p_srv_file(obj) util9p_box(util9p_static_dir, obj) -#define UTIL9P_STATIC_DIR(PATH, STRNAME, ...) \ - lo_box_util9p_static_dir_as_lib9p_srv_file(&((struct util9p_static_dir){ \ - ._util9p_static_common = UTIL9P_STATIC_COMMON(PATH, STRNAME, 0555), \ - .members = { __VA_ARGS__ LO_NULL(lib9p_srv_file) }, \ +#define UTIL9P_STATIC_DIR(PATH, STRNAME, ...) \ + lo_box_util9p_static_dir_as_lib9p_srv_file(&((struct util9p_static_dir){ \ + .c = UTIL9P_STATIC_COMMON(PATH, STRNAME, 0555), \ + .members = { __VA_ARGS__ LO_NULL(lib9p_srv_file) }, \ })) /* File ***********************************************************************/ struct util9p_static_file { - _util9p_static_common; + struct _util9p_static_common c; 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 */ }; LO_IMPLEMENTATION_H(lib9p_srv_file, struct util9p_static_file, util9p_static_file); -#define lo_box_util9p_static_file_as_lib9p_srv_file(obj) util9p_box(util9p_static_file, obj) -#define UTIL9P_STATIC_FILE(PATH, STRNAME, ...) \ - lo_box_util9p_static_file_as_lib9p_srv_file(&((struct util9p_static_file){ \ - ._util9p_static_common = UTIL9P_STATIC_COMMON(PATH, STRNAME, 0444), \ - __VA_ARGS__ \ +#define UTIL9P_STATIC_FILE(PATH, STRNAME, ...) \ + lo_box_util9p_static_file_as_lib9p_srv_file(&((struct util9p_static_file){ \ + .c = UTIL9P_STATIC_COMMON(PATH, STRNAME, 0444), \ + __VA_ARGS__ \ })) #endif /* _UTIL9P_STATIC_H_ */ diff --git a/lib9p_util/static.c b/lib9p_util/static.c index 4fba35a..338e9c9 100644 --- a/lib9p_util/static.c +++ b/lib9p_util/static.c @@ -9,146 +9,134 @@ #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_C(lib9p_srv_file, struct util9p_static_dir, util9p_static_dir); +LO_IMPLEMENTATION_C(lib9p_srv_file, struct util9p_static_file, util9p_static_file); -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); +LO_IMPLEMENTATION_STATIC(lib9p_srv_dio, struct util9p_static_dir, util9p_static_dio); +LO_IMPLEMENTATION_STATIC(lib9p_srv_fio, struct util9p_static_file, util9p_static_fio); /* dir ************************************************************************/ -static void util9p_static_dir_free(struct util9p_static_dir *self) { +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) { +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, + .path = self->c.pathnum, }; } -static struct lib9p_stat util9p_static_dir_stat(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx) { +error util9p_static_dir_stat(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx, struct lib9p_srv_stat *out) { assert(self); assert(ctx); - return (struct lib9p_stat){ - .kern_type = 0, - .kern_dev = 0, - .file_qid = util9p_static_dir_qid(self), - .file_mode = LIB9P_DM_DIR | (self->perm & 0555), - .file_atime = self->atime, - .file_mtime = self->mtime, - .file_size = 0, - .file_name = lib9p_str(self->name), - .file_owner_uid = lib9p_str(self->u_name), - .file_owner_gid = lib9p_str(self->g_name), - .file_last_modified_uid = lib9p_str(self->m_name), - .file_extension = lib9p_str(NULL), - .file_owner_n_uid = self->u_num, - .file_owner_n_gid = self->g_num, - .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) { + *out = ((struct lib9p_srv_stat){ + .qid = util9p_static_dir_qid(self), + .mode = LIB9P_DM_DIR | (self->c.perm & 0555), + .atime_sec = self->c.atime, + .mtime_sec = self->c.mtime, + .size = 0, + .name = lib9p_str(self->c.name), + .owner_uid = { .name = lib9p_str(self->c.u_name), .num = self->c.u_num }, + .owner_gid = { .name = lib9p_str(self->c.g_name), .num = self->c.g_num }, + .last_modifier_uid = { .name = lib9p_str(self->c.m_name), .num = self->c.m_num }, + .extension = lib9p_str(NULL), + }); + return ERROR_NULL; +} +error 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"); + return error_new(E_POSIX_EROFS, "read-only part of filesystem"); } -static void util9p_static_dir_remove(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx) { +error 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"); + return error_new(E_POSIX_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) { +lib9p_srv_file_or_error 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_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 file; + struct lib9p_srv_stat stat; + error err = LO_CALL(file, stat, ctx, &stat); + if (!ERROR_IS_NULL(err)) + return ERROR_NEW_ERR(lib9p_srv_file, err); + lib9p_srv_stat_assert(&stat); + if (lib9p_str_eq(stat.name, childname)) + return ERROR_NEW_VAL(lib9p_srv_file, file); } - lib9p_error(&ctx->basectx, - LIB9P_ERRNO_L_ENOENT, "no such file or directory"); - return LO_NULL(lib9p_srv_file); + return ERROR_NEW_ERR(lib9p_srv_file, error_new(E_POSIX_ENOENT, "no such file or directory")); } -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)) { +lib9p_srv_file_or_error 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); + return ERROR_NEW_ERR(lib9p_srv_file, error_new(E_POSIX_EROFS, "read-only part of filesystem")); } -LIB9P_SRV_NOTFILE(struct util9p_static_dir, util9p_static_dir); +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) { +lib9p_srv_dio_or_error 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); + return ERROR_NEW_VAL(lib9p_srv_dio, LO_BOX(lib9p_srv_dio, self)); +} + +static struct lib9p_qid util9p_static_dio_ioqid(struct util9p_static_dir *self) { + return util9p_static_dir_qid(self); } -static void util9p_static_dir_iofree(struct util9p_static_dir *self) { +static void util9p_static_dio_iofree(struct util9p_static_dir *self) { assert(self); } -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) { +static lib9p_srv_dirent_or_error util9p_static_dio_dread(struct util9p_static_dir *self, struct lib9p_srv_ctx *ctx, size_t idx) { assert(self); assert(ctx); - uint32_t byte_offset = 0; - size_t obj_offset = _obj_offset; - 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); - uint32_t nbytes = lib9p_stat_marshal(&ctx->basectx, byte_count-byte_offset, &stat, - &buf[byte_offset]); - if (!nbytes) { - if (obj_offset == _obj_offset) - lib9p_error(&ctx->basectx, - LIB9P_ERRNO_L_ERANGE, "stat object does not fit into negotiated max message size"); - break; - } - byte_offset += nbytes; - obj_offset++; - } - return obj_offset - _obj_offset; + lo_interface lib9p_srv_file file = self->members[idx]; + if (LO_IS_NULL(file)) + return ERROR_NEW_VAL(lib9p_srv_dirent, (struct lib9p_srv_dirent){}); + + struct lib9p_srv_stat stat; + error err = LO_CALL(file, stat, ctx, &stat); + if (!ERROR_IS_NULL(err)) + return ERROR_NEW_ERR(lib9p_srv_dirent, err); + lib9p_srv_stat_assert(&stat); + + return ERROR_NEW_VAL(lib9p_srv_dirent, ((struct lib9p_srv_dirent){ + .qid = stat.qid, + .name = stat.name, + })); } /* file ***********************************************************************/ -static void util9p_static_file_free(struct util9p_static_file *self) { +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) { +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, + .path = self->c.pathnum, }; } @@ -164,89 +152,83 @@ static inline size_t util9p_static_file_size(struct util9p_static_file *file) { } -static struct lib9p_stat util9p_static_file_stat(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx) { +error util9p_static_file_stat(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx, struct lib9p_srv_stat *out) { assert(self); assert(ctx); - - return (struct lib9p_stat){ - .kern_type = 0, - .kern_dev = 0, - .file_qid = util9p_static_file_qid(self), - .file_mode = self->perm & 0444, - .file_atime = self->atime, - .file_mtime = self->mtime, - .file_size = (uint64_t)util9p_static_file_size(self), - .file_name = lib9p_str(self->name), - .file_owner_uid = lib9p_str(self->u_name), - .file_owner_gid = lib9p_str(self->g_name), - .file_last_modified_uid = lib9p_str(self->m_name), - .file_extension = lib9p_str(NULL), - .file_owner_n_uid = self->u_num, - .file_owner_n_gid = self->g_num, - .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(out); + + *out = ((struct lib9p_srv_stat){ + .qid = util9p_static_file_qid(self), + .mode = self->c.perm & 0444, + .atime_sec = self->c.atime, + .mtime_sec = self->c.mtime, + .size = (uint64_t)util9p_static_file_size(self), + .name = lib9p_str(self->c.name), + .owner_uid = { .name = lib9p_str(self->c.u_name), .num = self->c.u_num }, + .owner_gid = { .name = lib9p_str(self->c.g_name), .num = self->c.g_num }, + .last_modifier_uid = { .name = lib9p_str(self->c.m_name), .num = self->c.m_num }, + .extension = lib9p_str(NULL), + }); + return ERROR_NULL; +} +error 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"); + return error_new(E_POSIX_EROFS, "read-only part of filesystem"); } -static void util9p_static_file_remove(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx) { +error 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"); + return error_new(E_POSIX_EROFS, "read-only part of filesystem"); } -LIB9P_SRV_NOTDIR(struct util9p_static_file, util9p_static_file); +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) { +lib9p_srv_fio_or_error 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); + return ERROR_NEW_VAL(lib9p_srv_fio, LO_BOX(lib9p_srv_fio, self)); } -static void util9p_static_file_iofree(struct util9p_static_file *self) { + +static struct lib9p_qid util9p_static_fio_ioqid(struct util9p_static_file *self) { + return util9p_static_file_qid(self); +} +static void util9p_static_fio_iofree(struct util9p_static_file *self) { assert(self); } -static uint32_t util9p_static_file_iounit(struct util9p_static_file *self) { +static uint32_t util9p_static_fio_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) { +static error util9p_static_fio_pread(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx, + lo_interface io_writer dst, uint64_t byte_offset, uint32_t byte_count) { 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; - } + if (byte_offset > (uint64_t)data_size) + return error_new(E_POSIX_EINVAL, "offset is past end-of-file length"); 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; + return io_write(dst, &self->data_start[beg_off], end_off-beg_off).err; } -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)) { +static uint32_t_or_error util9p_static_fio_pwrite(struct util9p_static_file *self, struct lib9p_srv_ctx *ctx, + const 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; + return ERROR_NEW_ERR(uint32_t, error_new(E_POSIX_EROFS, "read-only part of filesystem")); } |