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

Support Windows Device Paths in WASI #766

Merged
merged 1 commit into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions crates/livesplit-auto-splitting/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ sysinfo = { version = "0.30.0", default-features = false, features = [
"multithread",
] }
time = { version = "0.3.3", default-features = false }
wasmtime = { version = "16.0.0", default-features = false, features = [
wasmtime = { version = "17.0.0", default-features = false, features = [
"cranelift",
"parallel-compilation",
] }
wasmtime-wasi = { version = "16.0.0", default-features = false, features = [
wasmtime-wasi = { version = "17.0.0", default-features = false, features = [
"sync",
] }
wasi-common = "16.0.0"
wasi-common = "17.0.0"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52.0", features = ["Win32_Storage_FileSystem"] }
Expand Down
7 changes: 4 additions & 3 deletions crates/livesplit-auto-splitting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,10 @@ support:

- `stdout` / `stderr` / `stdin` are unbound. Those streams currently do
nothing.
- The file system is currently almost entirely empty. The host's file system
is accessible through `/mnt`. It is entirely read-only. Windows paths are
mapped to `/mnt/c`, `/mnt/d`, etc. to match WSL.
- The file system is currently almost entirely empty. The host's file system is
accessible through `/mnt`. It is entirely read-only. Windows paths are mapped
to `/mnt/c`, `/mnt/d`, etc. to match WSL. Additionally `/mnt/device` maps to
`\\?\` on Windows to access additional paths.
- There are no environment variables.
- There are no command line arguments.
- There is no networking.
Expand Down
3 changes: 2 additions & 1 deletion crates/livesplit-auto-splitting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,8 @@
//! nothing.
//! - The file system is currently almost entirely empty. The host's file system
//! is accessible through `/mnt`. It is entirely read-only. Windows paths are
//! mapped to `/mnt/c`, `/mnt/d`, etc. to match WSL.
//! mapped to `/mnt/c`, `/mnt/d`, etc. to match WSL. Additionally
//! `/mnt/device` maps to `\\?\` on Windows to access additional paths.
//! - There are no environment variables.
//! - There are no command line arguments.
//! - There is no networking.
Expand Down
67 changes: 66 additions & 1 deletion crates/livesplit-auto-splitting/src/runtime/api/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn build(script_path: Option<&Path>) -> WasiCtx {
drives &= !(1 << drive_idx);
let drive = drive_idx as u8 + b'a';
if let Ok(path) = wasmtime_wasi::Dir::open_ambient_dir(
str::from_utf8(&[drive, b':', b'\\']).unwrap(),
str::from_utf8(&[b'\\', b'\\', b'?', b'\\', drive, b':', b'\\']).unwrap(),
ambient_authority(),
) {
wasi.push_dir(
Expand All @@ -43,6 +43,9 @@ pub fn build(script_path: Option<&Path>) -> WasiCtx {
.unwrap();
}
}

wasi.push_dir(Box::new(DeviceDir), PathBuf::from("/mnt/device"))
.unwrap();
}
#[cfg(not(windows))]
{
Expand Down Expand Up @@ -128,3 +131,65 @@ impl WasiDir for ReadOnlyDir {
self.0.get_path_filestat(path, follow_symlinks).await
}
}

#[cfg(windows)]
struct DeviceDir;

#[cfg(windows)]
#[async_trait::async_trait]
impl WasiDir for DeviceDir {
fn as_any(&self) -> &dyn std::any::Any {
self
}

async fn open_file(
&self,
symlink_follow: bool,
path: &str,
oflags: OFlags,
read: bool,
write: bool,
fdflags: FdFlags,
) -> Result<OpenResult, wasi_common::Error> {
let (dir, file) = device_path(path)?;
dir.open_file(symlink_follow, file, oflags, read, write, fdflags)
.await
}

// FIXME: cap-primitives/src/windows/fs/get_path tries to strip `\\?\`,
// which breaks paths that aren't valid without it, such as UNC paths:
// https://github.com/bytecodealliance/cap-std/issues/348

async fn read_link(&self, path: &str) -> Result<PathBuf, wasi_common::Error> {
let (dir, file) = device_path(path)?;
dir.read_link(file).await
}

async fn get_path_filestat(
&self,
path: &str,
follow_symlinks: bool,
) -> Result<Filestat, wasi_common::Error> {
let (dir, file) = device_path(path)?;
dir.get_path_filestat(file, follow_symlinks).await
}
}

#[cfg(windows)]
fn device_path(path: &str) -> Result<(ReadOnlyDir, &str), wasi_common::Error> {
let (parent, file) = path
.strip_suffix('/')
.unwrap_or(path)
.rsplit_once('/')
.ok_or_else(wasi_common::Error::not_supported)?;

let parent = wasi_path::to_native(&format!("/mnt/device/{parent}"), true)
.ok_or_else(wasi_common::Error::not_supported)?;

let dir = wasmtime_wasi::dir::Dir::from_cap_std(
wasmtime_wasi::Dir::open_ambient_dir(parent, ambient_authority())
.map_err(|_| wasi_common::Error::not_supported())?,
);

Ok((ReadOnlyDir(dir), file))
}
Loading
Loading