Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix WASI rename issue: #2297 #2397

Merged
merged 10 commits into from
Jun 23, 2021
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C
- [#2157](https://github.com/wasmerio/wasmer/pull/2157) Simplify the code behind `WasmPtr`

### Fixed
- [#2397](https://github.com/wasmerio/wasmer/pull/2397) Fix WASI rename temporary file issue.
- [#2327](https://github.com/wasmerio/wasmer/pull/2327) Fix memory leak preventing internal instance memory from being freed when a WasmerEnv contained an exported extern (e.g. Memory, etc.).
- [#2247](https://github.com/wasmerio/wasmer/pull/2247) Internal WasiFS logic updated to be closer to what WASI libc does when finding a preopened fd for a path.
- [#2241](https://github.com/wasmerio/wasmer/pull/2241) Fix Undefined Behavior in setting memory in emscripten `EmEnv`.
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,7 @@ pub fn path_rename(
return __WASI_EEXIST;
}
let mut out_path = path.clone();
out_path.push(target_path);
out_path.push(target_path.file_name().unwrap());
chenyukang marked this conversation as resolved.
Show resolved Hide resolved
out_path
}
Kind::Root { .. } => return __WASI_ENOTCAPABLE,
Expand Down
46 changes: 45 additions & 1 deletion tests/wasi-wast/wasi/tests/path_rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fs;
use std::io::{Read, Write};
use std::path::PathBuf;

fn main() {
fn run_with_toplevel_dir() {
#[cfg(not(target_os = "wasi"))]
let mut base = PathBuf::from("test_fs");
#[cfg(target_os = "wasi")]
Expand Down Expand Up @@ -73,3 +73,47 @@ fn main() {
println!("{}", test_str);
std::fs::remove_file(file_to_rename_to).unwrap();
}

fn run_with_sub_dir() {
chenyukang marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(target_os = "wasi"))]
let base = PathBuf::from("test_fs");
#[cfg(target_os = "wasi")]
let mut base = PathBuf::from("temp");

//make a sub-directory
fs::create_dir(base.join("sub"));

let file_to_create = base.join("sub/path_rename_file.txt");
let file_to_rename_to = base.join("sub/path_renamed_file.txt");

{
let mut f = std::fs::OpenOptions::new()
.create_new(true)
.write(true)
.open(&file_to_create)
.unwrap();

let string = "Hello world";
let bytes: Vec<u8> = string.bytes().collect();
f.write_all(&bytes[..]).unwrap();
}

std::fs::rename(&file_to_create, &file_to_rename_to).unwrap();
let mut file = fs::File::open(&file_to_rename_to).expect("Could not open file");
if file_to_create.exists() {
println!("The original file still exists!");
return;
} else {
println!("The original file does not still exist!");
}

if !file_to_rename_to.exists() {
println!("The moved file does not exist!");
return;
}
}

fn main() {
run_with_toplevel_dir();
run_with_sub_dir();
}