From 21ba652babc3d95e6ee13764cabf4993054261c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Fri, 6 Nov 2020 20:01:14 +0100 Subject: [PATCH] kernel/fs: fix previous commit logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #16598 Change-Id: Ie34c1563bd34dbe9c6cda92e41a2d5a96d20c3b1 Reviewed-on: https://review.haiku-os.org/c/haiku/+/3383 Reviewed-by: Jacob Secunda Reviewed-by: Jérôme Duval --- src/system/kernel/fs/fd.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/system/kernel/fs/fd.cpp b/src/system/kernel/fs/fd.cpp index ac94ff77e3..a15b3b6e3b 100644 --- a/src/system/kernel/fs/fd.cpp +++ b/src/system/kernel/fs/fd.cpp @@ -748,7 +748,7 @@ common_user_io(int fd, off_t pos, void* buffer, size_t length, bool write) } bool movePosition = false; - if (pos == -1 && (!write || (descriptor->open_mode & O_APPEND) == 0)) { + if (pos == -1) { pos = descriptor->pos; movePosition = true; } @@ -768,10 +768,10 @@ common_user_io(int fd, off_t pos, void* buffer, size_t length, bool write) if (status != B_OK) return status; - if (movePosition) - descriptor->pos = pos + length; - else if (pos == -1) - descriptor->pos = descriptor->ops->fd_seek(descriptor, 0, SEEK_END); + if (movePosition) { + descriptor->pos = write && (descriptor->open_mode & O_APPEND) != 0 + ? descriptor->ops->fd_seek(descriptor, 0, SEEK_END) : pos + length; + } return length <= SSIZE_MAX ? (ssize_t)length : SSIZE_MAX; } @@ -810,7 +810,7 @@ common_user_vector_io(int fd, off_t pos, const iovec* userVecs, size_t count, return B_BAD_ADDRESS; bool movePosition = false; - if (pos == -1 && (!write || (descriptor->open_mode & O_APPEND) == 0)) { + if (pos == -1) { pos = descriptor->pos; movePosition = true; } @@ -854,17 +854,16 @@ common_user_vector_io(int fd, off_t pos, const iovec* userVecs, size_t count, else bytesTransferred += (ssize_t)length; - if (pos > -1) - pos += length; + pos += length; if (length < vecs[i].iov_len) break; } - if (movePosition) - descriptor->pos = pos; - else if (pos == -1) - descriptor->pos = descriptor->ops->fd_seek(descriptor, 0, SEEK_END); + if (movePosition) { + descriptor->pos = write && (descriptor->open_mode & O_APPEND) != 0 + ? descriptor->ops->fd_seek(descriptor, 0, SEEK_END) : pos; + } return bytesTransferred; }