Move preopen_dir handlers from wasmtime to wasi-common

This commit is contained in:
Jakub Konka
2019-06-04 08:02:01 +02:00
committed by Dan Gohman
parent 9a66400cd8
commit c113ff32e6
3 changed files with 29 additions and 2 deletions

View File

@@ -29,3 +29,4 @@ pub mod memory;
pub mod wasm32;
pub use ctx::{WasiCtx, WasiCtxBuilder};
pub use sys::preopen_dir;

View File

@@ -2,6 +2,14 @@ pub mod fdentry;
mod host_impl;
pub mod hostcalls;
pub fn dev_null() -> std::fs::File {
std::fs::File::open("/dev/null").expect("failed to open /dev/null")
use std::fs::File;
use std::io;
use std::path::Path;
pub fn dev_null() -> File {
File::open("/dev/null").expect("failed to open /dev/null")
}
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
File::open(path)
}

View File

@@ -3,7 +3,25 @@ mod host_impl;
pub mod hostcalls;
use std::fs::File;
use std::io;
use std::path::Path;
pub fn dev_null() -> File {
File::open("NUL").expect("failed to open NUL")
}
pub fn preopen_dir<P: AsRef<Path>>(path: P) -> io::Result<File> {
use std::fs::OpenOptions;
use std::os::windows::fs::OpenOptionsExt;
use winapi::um::winbase::FILE_FLAG_BACKUP_SEMANTICS;
// To open a directory using CreateFile, specify the
// FILE_FLAG_BACKUP_SEMANTICS flag as part of dwFileFlags...
// cf. https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfile2
OpenOptions::new()
.create(false)
.write(true)
.read(true)
.attributes(FILE_FLAG_BACKUP_SEMANTICS)
.open(path)
}