Skip to content

Commit 2cde8d9

Browse files
committed
Fix std::fs::copy on WASI by setting proper OpenOptions flags
When PR rust-lang#147572 switched WASI to use Unix-style filesystem APIs, the open_to_and_set_permissions function for WASI was implemented to call OpenOptions::new().open() without setting any access mode flags. This causes std::fs::copy to fail with the error: "must specify at least one of read, write, or append access" The fix is to explicitly set .write(true), .create(true), and .truncate(true) on the OpenOptions, matching the behavior of the non-WASI Unix implementation but without the permission handling that WASI doesn't support. Minimal reproduction: fn main() { std::fs::write("/src.txt", b"test").unwrap(); match std::fs::copy("/src.txt", "/dst.txt") { Ok(_) => println!("PASS: fs::copy works!"), Err(e) => println!("FAIL: {}", e), } } # Compile and run: rustc +nightly --target wasm32-wasip2 test.rs -o test.wasm wasmtime -S cli --dir . test.wasm # Before fix: FAIL: must specify at least one of read, write, or append access # After fix: PASS: fs::copy works! Note: The existing test library/std/src/fs/tests.rs::copy_file_ok would have caught this regression if the std test suite ran on WASI targets. Currently std tests don't compile for wasm32-wasip2 due to Unix-specific test code in library/std/src/sys/fd/unix/tests.rs. Fixes the regression introduced in nightly-2025-12-10.
1 parent 85d0cdf commit 2cde8d9

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

library/std/src/sys/fs/unix.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2296,7 +2296,11 @@ fn open_to_and_set_permissions(
22962296
_reader_metadata: &crate::fs::Metadata,
22972297
) -> io::Result<(crate::fs::File, crate::fs::Metadata)> {
22982298
use crate::fs::OpenOptions;
2299-
let writer = OpenOptions::new().open(to)?;
2299+
let writer = OpenOptions::new()
2300+
.write(true)
2301+
.create(true)
2302+
.truncate(true)
2303+
.open(to)?;
23002304
let writer_metadata = writer.metadata()?;
23012305
Ok((writer, writer_metadata))
23022306
}

0 commit comments

Comments
 (0)