Skip to content

Commit 8fb8bb4

Browse files
committed
Fix std::fs::copy on WASI target
Previously `std::fs::copy` on wasm32-wasi would reuse code from the `sys_common` module and would successfully copy contents of the file just to fail right before closing it. This was happening because `sys_common::copy` tries to copy permissions of the file, but permissions are not a thing in WASI (at least yet) and `set_permissions` is implemented as an unconditional runtime error. This change instead adds a custom working implementation of `std::fs::copy` (like Rust already has on some other targets) that doesn't try to call `set_permissions` and is essentially a thin wrapper around `std::io::copy`. Fixes #68560.
1 parent 2d2be57 commit 8fb8bb4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/libstd/sys/wasi/fs.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::sys::time::SystemTime;
1212
use crate::sys::unsupported;
1313
use crate::sys_common::FromInner;
1414

15-
pub use crate::sys_common::fs::copy;
1615
pub use crate::sys_common::fs::remove_dir_all;
1716

1817
pub struct File {
@@ -647,3 +646,12 @@ fn open_parent(p: &Path) -> io::Result<(ManuallyDrop<WasiFd>, PathBuf)> {
647646
pub fn osstr2str(f: &OsStr) -> io::Result<&str> {
648647
f.to_str().ok_or_else(|| io::Error::new(io::ErrorKind::Other, "input must be utf-8"))
649648
}
649+
650+
pub fn copy(from: &Path, to: &Path) -> io::Result<u64> {
651+
use crate::fs::File;
652+
653+
let mut reader = File::open(from)?;
654+
let mut writer = File::create(to)?;
655+
656+
io::copy(&mut reader, &mut writer)
657+
}

0 commit comments

Comments
 (0)