Skip to content

Commit

Permalink
fix append-ness of new fd affecting old fds (#5162)
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej authored Oct 24, 2024
2 parents 4b726d7 + 4a71c5c commit 6472633
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/wasix/src/syscalls/wasi/fd_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub(crate) fn fd_write_internal<M: MemorySize>(
if !is_stdio {
if fd_entry.flags.contains(Fdflags::APPEND) {
// `fdflags::append` means we need to seek to the end before writing.
offset = handle.size();
offset = fd_entry.inode.stat.read().unwrap().st_size;
fd_entry.offset.store(offset, Ordering::Release);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/wasix/src/syscalls/wasi/path_open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub(crate) fn path_open_internal(
let open_options = open_options
.write(minimum_rights.write)
.create(minimum_rights.create)
.append(minimum_rights.append)
.append(false)
.truncate(minimum_rights.truncate);

if minimum_rights.read {
Expand Down
31 changes: 31 additions & 0 deletions tests/wasi-fyi/fs_open_append_offset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::{
fs,
io::{Seek, SeekFrom, Write},
};

fn main() {
let file = "fyi/fs_open_append_offset.dir/file";
let mut f0 = fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(file)
.unwrap();

f0.write_all(b"abc").unwrap();
f0.seek(SeekFrom::Start(1)).unwrap();

assert_eq!(fs::read_to_string(file).unwrap(), "abc");

// This open with append should not affect the offset of f0.
let _f1 = fs::OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open(file)
.unwrap();

f0.write_all(b"d").unwrap();

assert_eq!(fs::read_to_string(file).unwrap(), "adc");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file

0 comments on commit 6472633

Please sign in to comment.