Skip to content

Commit 5fc30e6

Browse files
committed
Fix preopening dirs on Windows
1 parent 031f03f commit 5fc30e6

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ errno = "0.2.4"
4545
[target.'cfg(unix)'.dependencies]
4646
wasmtime-wasi-c = { path = "wasmtime-wasi-c" }
4747

48+
[target.'cfg(windows)'.dependencies]
49+
winapi = "0.3"
50+
4851
[workspace]
4952

5053
[features]

src/wasmtime.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,38 @@ fn read_wasm(path: PathBuf) -> Result<Vec<u8>, String> {
118118
})
119119
}
120120

121+
fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
122+
#[cfg(windows)]
123+
{
124+
use std::fs::OpenOptions;
125+
use std::os::windows::fs::OpenOptionsExt;
126+
use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;
127+
128+
// To open a directory using CreateFile2, specify the
129+
// FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFileFlags...
130+
// cf. https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfile2
131+
OpenOptions::new()
132+
.create(false)
133+
.write(true)
134+
.read(true)
135+
.attributes(FILE_FLAG_BACKUP_SEMANTICS)
136+
.open(path)
137+
}
138+
#[cfg(unix)]
139+
{
140+
File::open(path)
141+
}
142+
#[cfg(not(any(windows, unix)))]
143+
{
144+
unimplemented!("this OS is currently not supported by Wasmtime")
145+
}
146+
}
147+
121148
fn compute_preopen_dirs(flag_dir: &[String], flag_mapdir: &[String]) -> Vec<(String, File)> {
122149
let mut preopen_dirs = Vec::new();
123150

124151
for dir in flag_dir {
125-
let preopen_dir = File::open(dir).unwrap_or_else(|err| {
152+
let preopen_dir = preopen_dir(dir).unwrap_or_else(|err| {
126153
println!("error while pre-opening directory {}: {}", dir, err);
127154
exit(1);
128155
});
@@ -136,7 +163,7 @@ fn compute_preopen_dirs(flag_dir: &[String], flag_mapdir: &[String]) -> Vec<(Str
136163
exit(1);
137164
}
138165
let (key, value) = (parts[0], parts[1]);
139-
let preopen_dir = File::open(value).unwrap_or_else(|err| {
166+
let preopen_dir = preopen_dir(value).unwrap_or_else(|err| {
140167
println!("error while pre-opening directory {}: {}", value, err);
141168
exit(1);
142169
});

0 commit comments

Comments
 (0)