From: Matthias Kruk Date: Sat, 15 Aug 2020 06:22:01 +0000 (+0900) Subject: sys/io: Implement _pipe_read() and _pipe_write() X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=ee32af83b17ee044342a18b8d3cecf9803599fb7;p=corax sys/io: Implement _pipe_read() and _pipe_write() --- diff --git a/sys/io/pipe.c b/sys/io/pipe.c index e3a6ab5..1749c52 100644 --- a/sys/io/pipe.c +++ b/sys/io/pipe.c @@ -21,7 +21,7 @@ static struct file_ops _pipe_ops = { }; struct pipe { - char data[PIPE_BUFSIZE]; + char p_data[PIPE_BUFSIZE]; size_t p_offset; size_t p_len; @@ -30,24 +30,72 @@ struct pipe { static ssize_t _pipe_read(struct file *file, void *dst, const size_t dst_size) { + struct pipe *pipe; char buf[128]; + ssize_t ret_val; int len; len = snprintf(buf, sizeof(buf), "%s(%p, %p, %lu)\n", __func__, file, dst, dst_size); debug(buf, len); - return(-ENOSYS); + ret_val = -EBADFD; + pipe = (struct pipe*)file_get_priv(file); + + if(pipe) { + ret_val = pipe->p_len < dst_size ? pipe->p_len : dst_size; + + if(ret_val > 0) { + memcpy(dst, pipe->p_data + pipe->p_offset, ret_val); + + pipe->p_offset += ret_val; + pipe->p_len -= ret_val; + + if(!pipe->p_len) { + pipe->p_offset = 0; + } + } else { + ret_val = -EAGAIN; + } + } + + return(ret_val); } -static ssize_t _pipe_write(struct file *file, const void *src, const size_t src_size) +static ssize_t _pipe_write(struct file *file, const void *src, const size_t src_len) { + struct pipe *pipe; + ssize_t ret_val; char buf[128]; int len; - len = snprintf(buf, sizeof(buf), "%s(%p, %p, %lu)\n", __func__, file, src, src_size); + len = snprintf(buf, sizeof(buf), "%s(%p, %p, %lu)\n", __func__, file, src, src_len); debug(buf, len); - return(-ENOSYS); + ret_val = -EBADFD; + pipe = (struct pipe*)file_get_priv(file); + + if(pipe) { + if(pipe->p_len && pipe->p_offset) { + memcpy(pipe->p_data, pipe->p_data + pipe->p_offset, len); + pipe->p_offset = 0; + } + + ret_val = sizeof(pipe->p_data) - pipe->p_len; + + if(src_len < ret_val) { + ret_val = src_len; + } + + if(ret_val > 0) { + memcpy(pipe->p_data + pipe->p_len, src, ret_val); + pipe->p_len += ret_val; + } else if(src_len > 0) { + /* there wasn't enough space to write any data */ + ret_val = -EAGAIN; + } + } + + return(ret_val); } int pipe_new(const int flags, struct file **dst) @@ -134,7 +182,8 @@ void sys_pipe(pid_t pid, struct cxio_pipe *req) fd[i] = err; - len = snprintf(buf, sizeof(buf), "%s: pipe[%d]=%d fd=%p file=%p\n", __func__, i, fd[i], fildes[i], file); + len = snprintf(buf, sizeof(buf), "%s: pipe[%d]=%d fd=%p file=%p\n", + __func__, i, fd[i], fildes[i], file); debug(buf, len); }