Skip to content

Commit 865c2fa

Browse files
committed
Fix append-ness of new fd affecting old fd
This commit fixes a case where opening a new file descriptor with the append flag makes previously opened non-append fds append as well. The fix is relatively simple, since the append-ness of an fd is already tracked in the virtual fs implementation, there's no need to open the actual file with the append flag. This behavior is consistent across Linux and other Wasm runtimes. fixes #5161 Signed-off-by: Yage Hu <[email protected]>
1 parent b7375ea commit 865c2fa

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

lib/wasix/src/syscalls/wasi/fd_write.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@ pub(crate) fn fd_write_internal<M: MemorySize>(
163163
if !is_stdio {
164164
if fd_entry.flags.contains(Fdflags::APPEND) {
165165
// `fdflags::append` means we need to seek to the end before writing.
166-
offset = handle.size();
166+
offset = fd_entry.inode.stat.read().unwrap().st_size;
167167
fd_entry.offset.store(offset, Ordering::Release);
168168
}
169169

170+
println!("whoa {offset}");
171+
170172
handle
171173
.seek(std::io::SeekFrom::Start(offset))
172174
.await
@@ -206,6 +208,8 @@ pub(crate) fn fd_write_internal<M: MemorySize>(
206208
}
207209
}
208210

211+
println!("written {written} {}", handle.size());
212+
209213
if is_stdio {
210214
handle.flush().await.map_err(map_io_err)?;
211215
}

lib/wasix/src/syscalls/wasi/path_open.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pub(crate) fn path_open_internal(
237237
let open_options = open_options
238238
.write(minimum_rights.write)
239239
.create(minimum_rights.create)
240-
.append(minimum_rights.append)
240+
.append(false)
241241
.truncate(minimum_rights.truncate);
242242

243243
if minimum_rights.read {
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::{
2+
fs,
3+
io::{Seek, SeekFrom, Write},
4+
};
5+
6+
fn main() {
7+
let file = "fyi/fs_open_append_offset.dir/file";
8+
let mut f0 = fs::OpenOptions::new()
9+
.create(true)
10+
.truncate(true)
11+
.write(true)
12+
.open(file)
13+
.unwrap();
14+
15+
f0.write_all(b"abc").unwrap();
16+
f0.seek(SeekFrom::Start(1)).unwrap();
17+
18+
assert_eq!(fs::read_to_string(file).unwrap(), "abc");
19+
20+
// This open with append should not affect the offset of f0.
21+
let _f1 = fs::OpenOptions::new()
22+
.create(true)
23+
.write(true)
24+
.append(true)
25+
.open(file)
26+
.unwrap();
27+
28+
f0.write_all(b"d").unwrap();
29+
30+
assert_eq!(fs::read_to_string(file).unwrap(), "adc");
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
file

0 commit comments

Comments
 (0)