From 60a897d464342fd945073c36d0b01b3dac9183e4 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 24 Dec 2021 17:18:33 +0000 Subject: [PATCH] Implemented fio_read, fio_write, and fio_flush on Horizon --- photon/libc/sys/horizon/fio.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/photon/libc/sys/horizon/fio.c b/photon/libc/sys/horizon/fio.c index cfc3d55..c910f84 100644 --- a/photon/libc/sys/horizon/fio.c +++ b/photon/libc/sys/horizon/fio.c @@ -1,5 +1,6 @@ #include #include +#include #include "sys/types.h" #include "__fio.h" @@ -55,12 +56,28 @@ int __fileno(struct __io_file *f) unsigned int __fio_read(struct __io_file *f, char *buf, unsigned int sz) { - return 0; + ssize_t err = read(f->fd, buf, sz); + if (err < 0) { + f->err = 1; + return 0; + } + + return err; } unsigned int __fio_write(struct __io_file *f, const char *buf, unsigned int sz) { - mx_console_write(buf, sz); + for (unsigned int i = 0; i < sz; i++) { + if (f->buf_idx >= __FIO_BUFSZ) { + __fio_flush(f); + } + + f->buf[f->buf_idx++] = buf[i]; + if (buf[i] == '\n') { + __fio_flush(f); + } + } + return sz; } @@ -71,7 +88,14 @@ int __fio_ungetc(struct __io_file *f, char c) unsigned int __fio_flush(struct __io_file *f) { - return 0; + ssize_t res = write(f->fd, f->buf, f->buf_idx); + if (res != f->buf_idx) { + f->err = 1; + } + + size_t flushed = f->buf_idx; + f->buf_idx = 0; + return flushed; } int __fio_error(struct __io_file *f)