Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix offset in append mode #4685

Merged
merged 2 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/wasix/src/syscalls/wasi/fd_seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 24 additions & 0 deletions tests/wasi-fyi/fs_seek_append_mode.rs
Original file line number Diff line number Diff line change
@@ -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);
}
Loading