diff options
Diffstat (limited to 'cmd/sbc_harness/fs_harness_uptime_txt.c')
-rw-r--r-- | cmd/sbc_harness/fs_harness_uptime_txt.c | 65 |
1 files changed, 43 insertions, 22 deletions
diff --git a/cmd/sbc_harness/fs_harness_uptime_txt.c b/cmd/sbc_harness/fs_harness_uptime_txt.c index 021a8bd..9b03b46 100644 --- a/cmd/sbc_harness/fs_harness_uptime_txt.c +++ b/cmd/sbc_harness/fs_harness_uptime_txt.c @@ -11,23 +11,27 @@ #include "fs_harness_uptime_txt.h" -LO_IMPLEMENTATION_C(lib9p_srv_file, struct uptime_file, uptime_file, static); +LO_IMPLEMENTATION_C(lib9p_srv_file, struct uptime_file, uptime_file); struct uptime_fio { struct uptime_file *parent; size_t buf_len; - char buf[24]; /* len(str(UINT64_MAX)+"ns\n\0") */ + /* The maximum length (UINT64_MAX) string is 52 bytes, not + * including a nul-terminator: + * + * "18446744073709551615ns\n" # 22+1 + * "584y343d 23h34m33.709551615s\n" # 28+1 + */ + char buf[52]; }; - -LO_IMPLEMENTATION_H(lib9p_srv_fio, struct uptime_fio, uptime_fio); -LO_IMPLEMENTATION_C(lib9p_srv_fio, struct uptime_fio, uptime_fio, static); +LO_IMPLEMENTATION_STATIC(lib9p_srv_fio, struct uptime_fio, uptime_fio); /* srv_file *******************************************************************/ -static void uptime_file_free(struct uptime_file *self) { +void uptime_file_free(struct uptime_file *self) { assert(self); } -static struct lib9p_qid uptime_file_qid(struct uptime_file *self) { +struct lib9p_qid uptime_file_qid(struct uptime_file *self) { assert(self); return (struct lib9p_qid){ @@ -37,7 +41,7 @@ static struct lib9p_qid uptime_file_qid(struct uptime_file *self) { }; } -static lib9p_srv_stat_or_error uptime_file_stat(struct uptime_file *self, struct lib9p_srv_ctx *ctx) { +lib9p_srv_stat_or_error uptime_file_stat(struct uptime_file *self, struct lib9p_srv_ctx *ctx) { assert(self); assert(ctx); @@ -64,24 +68,23 @@ static lib9p_srv_stat_or_error uptime_file_stat(struct uptime_file *self, struct .extension = lib9p_str(NULL), })); } -static error uptime_file_wstat(struct uptime_file *self, struct lib9p_srv_ctx *ctx, - struct lib9p_srv_stat) { +error uptime_file_wstat(struct uptime_file *self, struct lib9p_srv_ctx *ctx, struct lib9p_srv_stat) { assert(self); assert(ctx); return error_new(E_POSIX_EROFS, "read-only part of filesystem"); } -static error uptime_file_remove(struct uptime_file *self, struct lib9p_srv_ctx *ctx) { +error uptime_file_remove(struct uptime_file *self, struct lib9p_srv_ctx *ctx) { assert(self); assert(ctx); return error_new(E_POSIX_EROFS, "read-only part of filesystem"); } -LIB9P_SRV_NOTDIR(struct uptime_file, uptime_file); +LIB9P_SRV_NOTDIR(, struct uptime_file, uptime_file); -static lib9p_srv_fio_or_error uptime_file_fopen(struct uptime_file *self, struct lib9p_srv_ctx *ctx, - bool LM_UNUSED(rd), bool LM_UNUSED(wr), bool LM_UNUSED(trunc)) { +lib9p_srv_fio_or_error uptime_file_fopen(struct uptime_file *self, struct lib9p_srv_ctx *ctx, + bool LM_UNUSED(rd), bool LM_UNUSED(wr), bool LM_UNUSED(trunc)) { assert(self); assert(ctx); @@ -104,33 +107,51 @@ static void uptime_fio_iofree(struct uptime_fio *self) { free(self); } -static struct lib9p_qid uptime_fio_qid(struct uptime_fio *self) { +static struct lib9p_qid uptime_fio_ioqid(struct uptime_fio *self) { assert(self); assert(self->parent); return uptime_file_qid(self->parent); } -static iovec_or_error uptime_fio_pread(struct uptime_fio *self, struct lib9p_srv_ctx *ctx, - uint32_t byte_count, uint64_t byte_offset) { +#define NS_PER_M (NS_PER_S*60) +#define NS_PER_H (NS_PER_S*60*60) +#define NS_PER_D (NS_PER_S*60*60*24) +#define NS_PER_Y (NS_PER_S*60*60*24*365) + +static error uptime_fio_pread(struct uptime_fio *self, struct lib9p_srv_ctx *ctx, + lo_interface io_writer dst, uint64_t byte_offset, uint32_t byte_count) { assert(self); assert(ctx); if (byte_offset == 0 || self->buf_len == 0) { uint64_t now = LO_CALL(bootclock, get_time_ns); self->buf_len = fmt_snprint(self->buf, sizeof(self->buf), now, "ns\n"); + + uint64_t ns = now; + uint64_t y = ns/NS_PER_Y; ns -= y*NS_PER_Y; + uint64_t d = ns/NS_PER_D; ns -= d*NS_PER_D; + uint64_t h = ns/NS_PER_H; ns -= h*NS_PER_H; + uint64_t m = ns/NS_PER_M; ns -= m*NS_PER_M; + uint64_t s = ns/NS_PER_S; ns -= s*NS_PER_S; + if (y) + self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, y, "y"); + if (y || d) + self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, d, "d "); + if (y || d || h) + self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, h, "h"); + if (y || d || h || m) + self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, m, "m"); + self->buf_len += fmt_snprint(&self->buf[self->buf_len], sizeof(self->buf)-self->buf_len, s, ".", (rjust, 9, '0', ns), "s\n"); } if (byte_offset > (uint64_t)self->buf_len) - return ERROR_NEW_ERR(iovec, error_new(E_POSIX_EINVAL, "offset is past end-of-file length")); + 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 > self->buf_len) end_off = self->buf_len; - return ERROR_NEW_VAL(iovec, ((struct iovec){ - .iov_base = &self->buf[beg_off], - .iov_len = end_off-beg_off, - })); + return io_write(dst, &self->buf[beg_off], end_off-beg_off).err; } static uint32_t_or_error uptime_fio_pwrite(struct uptime_fio *self, struct lib9p_srv_ctx *ctx, |