From d2e72c040b198a8bd5e8913686c6163fe463a8af Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Tue, 14 May 2024 15:48:25 +0330 Subject: [PATCH 1/2] fix seek when is in append mode --- lib/wasix/src/syscalls/wasi/fd_seek.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/wasix/src/syscalls/wasi/fd_seek.rs b/lib/wasix/src/syscalls/wasi/fd_seek.rs index 65d60514933..527ed185dcd 100644 --- a/lib/wasix/src/syscalls/wasi/fd_seek.rs +++ b/lib/wasix/src/syscalls/wasi/fd_seek.rs @@ -62,6 +62,9 @@ pub(crate) fn fd_seek_internal( if !fd_entry.rights.contains(Rights::FD_SEEK) { return Ok(Err(Errno::Access)); } + if fd_entry.flags.contains(Fdflags::APPEND) { + return Ok(Ok(fd_entry.offset.load(Ordering::Acquire))); + } // TODO: handle case if fd is a dir? let new_offset = match whence { From ff3ae4521ba7fffb36e4038cbc89144a59681d84 Mon Sep 17 00:00:00 2001 From: "M.Amin Rayej" Date: Tue, 14 May 2024 15:48:42 +0330 Subject: [PATCH 2/2] add a test to wasi-fyi tess --- tests/wasi-fyi/fs_seek_append_mode.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/wasi-fyi/fs_seek_append_mode.rs diff --git a/tests/wasi-fyi/fs_seek_append_mode.rs b/tests/wasi-fyi/fs_seek_append_mode.rs new file mode 100644 index 00000000000..7e1d54321f0 --- /dev/null +++ b/tests/wasi-fyi/fs_seek_append_mode.rs @@ -0,0 +1,24 @@ +use std::fs::OpenOptions; +use std::io::prelude::*; +use std::io::SeekFrom; + +fn main() { + let mut file = OpenOptions::new() + .append(true) + .read(true) + .create(true) + .open("file") + .unwrap(); + + // file offset must be 1 now + write!(file, "{}", "a").unwrap(); + + // rewind should not work on file in append mode + // since the offset must always be at the end of the file + let _ = file.rewind(); + + // file offset must be 2 now + write!(file, "{}", "b").unwrap(); + + assert_eq!(file.seek(SeekFrom::Current(0)).unwrap(), 2); +}