clean up wasi fs public api changes

This commit is contained in:
Mark McCaskey 2019-07-31 09:58:39 +09:00
parent fba09bef1a
commit 620a521690
4 changed files with 22 additions and 11 deletions

View File

@ -5,6 +5,7 @@ All PRs to the Wasmer repository must add to this file.
Blocks of changes will separated by version increments.
## **[Unreleased]**
- [#595](https://github.com/wasmerio/wasmer/pull/595) Add unstable public API for interfacing with the WASI file system in plugin-like usecases
- [#579](https://github.com/wasmerio/wasmer/pull/579) Fix bug in caching with LLVM and Singlepass backends.
Add `default-backend-singlepass`, `default-backend-llvm`, and `default-backend-cranelift` features to `wasmer-runtime`
to control the `default_compiler()` function (this is a breaking change). Add `compiler_for_backend` function in `wasmer-runtime`

View File

@ -6,11 +6,12 @@ extern crate winapi;
#[macro_use]
mod macros;
mod ptr;
mod state;
pub mod state;
mod syscalls;
mod utils;
use self::state::{WasiFs, WasiState};
pub use self::syscalls::types;
use self::syscalls::*;
use std::ffi::c_void;

View File

@ -1,3 +1,6 @@
//! WARNING: the API exposed here is unstable and very experimental. Certain thins will not
//! yet and may be broken in patch releases. If you're using this and have any specific needs,
//! please let us know here https://github.com/wasmerio/wasmer/issues/583 or by filing an issue.
// use wasmer_runtime_abi::vfs::{
// vfs::Vfs,
// file_like::{FileLike, Metadata};
@ -14,7 +17,12 @@ use std::{
path::{Path, PathBuf},
time::SystemTime,
};
use wasmer_runtime_core::debug;
use wasmer_runtime_core::{debug, vm::Ctx};
/// Get WasiState from a Ctx
pub unsafe fn get_wasi_state(ctx: &mut Ctx) -> &mut WasiState {
&mut *(ctx.data as *mut WasiState)
}
/// A completely aribtrary "big enough" number used as the upper limit for
/// the number of symlinks that can be traversed when resolving a path
@ -54,8 +62,6 @@ impl WasiFsError {
}
/// This trait relies on your file closing when it goes out of scope via `Drop`
/// it does not require `Drop` because `std::fs::File` does not implement it
/// (TODO: figure out how that makes sense, because it clearly does)
pub trait WasiFile: std::fmt::Debug + Write + Read + Seek {
/// the last time the file was accessed in nanoseconds as a UNIX timestamp
fn last_accessed(&self) -> u64;
@ -392,6 +398,8 @@ pub struct Fd {
}
#[derive(Debug)]
/// Warning, modifying these fields directly may cause invariants to break and
/// should be considered unsafe. These fields may be made private in a future release
pub struct WasiFs {
//pub repo: Repo,
pub preopen_fds: Vec<u32>,
@ -875,9 +883,9 @@ impl WasiFs {
pub fn flush(&mut self, fd: __wasi_fd_t) -> Result<(), __wasi_errno_t> {
match fd {
0 => (),
1 => io::stdout().flush().map_err(|_| __WASI_EIO)?,
2 => io::stderr().flush().map_err(|_| __WASI_EIO)?,
__WASI_STDIN_FILENO => (),
__WASI_STDOUT_FILENO => self.stdout.flush().map_err(|_| __WASI_EIO)?,
__WASI_STDERR_FILENO => self.stderr.flush().map_err(|_| __WASI_EIO)?,
_ => {
let fd = self.fd_map.get(&fd).ok_or(__WASI_EBADF)?;
if fd.rights & __WASI_RIGHT_FD_DATASYNC == 0 {
@ -926,7 +934,7 @@ impl WasiFs {
rights_inheriting: __wasi_rights_t,
flags: __wasi_fdflags_t,
inode: Inode,
) -> Result<u32, __wasi_errno_t> {
) -> Result<__wasi_fd_t, __wasi_errno_t> {
let idx = self.next_fd.get();
self.next_fd.set(idx + 1);
self.fd_map.insert(

View File

@ -9,7 +9,7 @@ use self::types::*;
use crate::{
ptr::{Array, WasmPtr},
state::{
host_file_type_to_wasi_file_type, Fd, Inode, InodeVal, Kind, WasiFile, WasiState,
self, host_file_type_to_wasi_file_type, Fd, Inode, InodeVal, Kind, WasiFile, WasiState,
MAX_SYMLINKS,
},
ExitCode,
@ -27,9 +27,10 @@ pub use unix::*;
#[cfg(any(target_os = "windows"))]
pub use windows::*;
/// This function is not safe
#[allow(clippy::mut_from_ref)]
fn get_wasi_state(ctx: &Ctx) -> &mut WasiState {
unsafe { &mut *(ctx.data as *mut WasiState) }
pub(crate) fn get_wasi_state(ctx: &Ctx) -> &mut WasiState {
unsafe { state::get_wasi_state(&mut *(ctx as *const Ctx as *mut Ctx)) }
}
fn write_bytes<T: Write>(