From ce8c29db9421f69828d2e7504b1e932cef4a6d84 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fl.fischer@fau.de>
Date: Thu, 28 Jan 2021 22:59:17 +0100
Subject: [PATCH] [IO] make default offset for read/write more intuitive

The current default offset is 0 which means read/write will not
work like POSIX read/write and update the file position and instead will
always work at the beginning of the file.

Since linux v5.5 (ba04291eb66ed895f194ae5abd3748d72bf8aaea) io_uring
supports offset=-1 which behaves like pvread2/pvwrite2.
This feature is reported by IORING_FEAT_RW_CUR_POS.

By using the default offset -1 emper::io::readFile and emper::io::writeFile
will work like write/read but does also support pwrite/pread.
---
 emper/io.hpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/emper/io.hpp b/emper/io.hpp
index 76da837b..fc154f36 100644
--- a/emper/io.hpp
+++ b/emper/io.hpp
@@ -219,7 +219,7 @@ inline auto acceptAndWait(int socket, struct sockaddr *address, socklen_t *addre
  *
  * @return Future object which signals the completion of the read request
  */
-inline auto readFile(int fildes, void *buf, size_t nbyte, off_t offset = 0, bool read_all = false)
+inline auto readFile(int fildes, void *buf, size_t nbyte, off_t offset = -1, bool read_all = false)
 		-> std::unique_ptr<Future> {
 	auto future = std::make_unique<ReadFuture>(fildes, buf, nbyte, offset, read_all);
 	future->submit();
@@ -241,7 +241,7 @@ inline auto readFile(int fildes, void *buf, size_t nbyte, off_t offset = 0, bool
  *
  * @return -1 on error, otherwise the number of bytes read
  */
-inline auto readFileAndWait(int fildes, void *buf, size_t nbyte, off_t offset = 0,
+inline auto readFileAndWait(int fildes, void *buf, size_t nbyte, off_t offset = -1,
 														bool read_all = false) -> ssize_t {
 	ReadFuture future(fildes, buf, nbyte, offset, read_all);
 	future.submit();
@@ -266,7 +266,7 @@ inline auto readFileAndWait(int fildes, void *buf, size_t nbyte, off_t offset =
  *
  * @return Future object which signals the completion of the write request
  */
-inline auto writeFile(int fildes, const void *buf, size_t nbyte, off_t offset = 0,
+inline auto writeFile(int fildes, const void *buf, size_t nbyte, off_t offset = -1,
 											bool write_all = true) -> std::unique_ptr<Future> {
 	auto future = std::make_unique<WriteFuture>(fildes, buf, nbyte, offset, write_all);
 	future->submit();
@@ -291,7 +291,7 @@ inline auto writeFile(int fildes, const void *buf, size_t nbyte, off_t offset =
  *
  * @return -1 on error, otherwise the number of bytes written
  */
-inline auto writeFileAndWait(int fildes, const void *buf, size_t nbyte, off_t offset = 0,
+inline auto writeFileAndWait(int fildes, const void *buf, size_t nbyte, off_t offset = -1,
 														 bool write_all = true) -> ssize_t {
 	WriteFuture future(fildes, buf, nbyte, offset, write_all);
 	future.submit();
-- 
GitLab