Skip to content

Commit

Permalink
fix offset in append mode (#4685)
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej authored May 14, 2024
2 parents d65b674 + ff3ae45 commit 28c0186
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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);
}

0 comments on commit 28c0186

Please sign in to comment.