From 733f0d24666aa04340ee0f68dee025b1a548c202 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sat, 15 Aug 2020 15:24:03 +0900 Subject: [PATCH] sys/io: Add fd_read() and fd_write() wrapper functions for performing reads and writes on a fd's file --- sys/io/filedesc.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++ sys/io/filedesc.h | 3 +++ 2 files changed, 58 insertions(+) diff --git a/sys/io/filedesc.c b/sys/io/filedesc.c index 9c7f40e..78cd570 100644 --- a/sys/io/filedesc.c +++ b/sys/io/filedesc.c @@ -21,6 +21,7 @@ #include #include #include "filedesc.h" +#include "file.h" struct file; @@ -236,3 +237,57 @@ int fd_unref(struct filedesc *fd) return(ret_val); } + +/* + * fd_read() - Read data from a file descriptor + * + * SYNOPSIS + * ssize_t fd_read(struct filedesc *fd, void *dst, const size_t dst_size); + * + * DESCRIPTION + * The fd_read() function attempts to read up to `dst_size' bytes of data from the file + * descriptor pointed to by `fd' and writes it to the buffer pointed to by `dst'. + * + * This function is the abstract interface to the file-specific read function. For implementation + * details, please refer to the documentation of the more specific read function (for example + * _pipe_read() for pipes). + * + * RETURN VALUE + * Upon success, the number of bytes read is returned. Otherwise, a negative error number is + * returned. + * + * ERRORS + * For possible errors, please refer to the documentation of the read function for the respective + * file type. + */ +ssize_t fd_read(struct filedesc *fd, void *dst, const size_t dst_size) +{ + return(file_read(fd->fd_file, dst, dst_size)); +} + +/* + * fd_write() - Write data to a file descriptor + * + * SYNOPSIS + * ssize_t fd_write(struct filedesc *fd, const void *src, const size_t src_len); + * + * DESCRIPTION + * The fd_write() function attempts to write up to `src_len' bytes from the buffer pointed to by + * `src' and write that data to the file descriptor pointed to by `fd'. + * + * This function is the abstract interface to the file-specific write function. For + * implementation details, please refer to the documentation of the more specific write function + * (for example _pipe_write() for pipes). + * + * RETURN VALUE + * Upon success, the number of bytes read is returned. Otherwise a negative error number is + * returned. + * + * ERRORS + * For possible errors, please refer to the documentation of the write function for the + * respective file type. + */ +ssize_t fd_write(struct filedesc *fd, const void *src, const size_t src_len) +{ + return(file_write(fd->fd_file, src, src_len)); +} diff --git a/sys/io/filedesc.h b/sys/io/filedesc.h index 3f4d84b..f4b08e8 100644 --- a/sys/io/filedesc.h +++ b/sys/io/filedesc.h @@ -30,4 +30,7 @@ int fd_set_file(struct filedesc*, struct file*); int fd_ref(struct filedesc*); int fd_unref(struct filedesc*); +ssize_t fd_read(struct filedesc*, void*, const size_t); +ssize_t fd_write(struct filedesc*, const void*, const size_t); + #endif /* FILEDESC_H */ -- 2.47.3