Change ValueType trait and add basic fs

This commit is contained in:
Lachlan Sneff 2019-04-01 15:52:35 -07:00
parent c12c7d558c
commit ce35e5794f
8 changed files with 186 additions and 217 deletions

100
Cargo.lock generated
View File

@ -121,6 +121,11 @@ dependencies = [
"which 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "bitflags"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "bitflags"
version = "1.0.4"
@ -552,6 +557,18 @@ dependencies = [
"syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "env_logger"
version = "0.5.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
"humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "env_logger"
version = "0.6.1"
@ -684,6 +701,14 @@ name = "gcc"
version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "generational-arena"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "generic-array"
version = "0.12.0"
@ -692,6 +717,14 @@ dependencies = [
"typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "getopts"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "glob"
version = "0.2.11"
@ -948,6 +981,16 @@ dependencies = [
"cfg-if 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lz4"
version = "1.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
"lz4-sys 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"skeptic 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lz4"
version = "1.23.1"
@ -957,6 +1000,15 @@ dependencies = [
"lz4-sys 1.8.3 (git+https://github.com/zboxfs/lz4-rs.git)",
]
[[package]]
name = "lz4-sys"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lz4-sys"
version = "1.8.3"
@ -1295,6 +1347,15 @@ dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "pulldown-cmark"
version = "0.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "quick-error"
version = "1.2.2"
@ -1720,6 +1781,15 @@ name = "siphasher"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "skeptic"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
"tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "slab"
version = "0.4.2"
@ -2360,9 +2430,14 @@ dependencies = [
name = "wasmer-wasi"
version = "0.1.0"
dependencies = [
"byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"generational-arena 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"hashbrown 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
"wasmer-runtime-core 0.2.1",
"zbox 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -2485,6 +2560,22 @@ dependencies = [
"tar 0.4.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "zbox"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)",
"linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"lz4 1.22.0 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rmp-serde 0.13.7 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "zstd"
version = "0.4.22+zstd.1.3.8"
@ -2528,6 +2619,7 @@ dependencies = [
"checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6"
"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
"checksum bindgen 0.46.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8f7f7f0701772b17de73e4f5cbcb1dd6926f4706cba4c1ab62c5367f8bdc94e1"
"checksum bitflags 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4f67931368edf3a9a51d29886d245f1c3db2f1ef0dcc9e35ff70341b78c10d23"
"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12"
"checksum blake2b_simd 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce2571a6cd634670daa2977cc894c1cc2ba57c563c498e5a82c35446f34d056e"
"checksum blob 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "19803aa44ff8b43123bbe369efaddcb638ea7dc332e543972dd95ac7cb148b92"
@ -2575,6 +2667,7 @@ dependencies = [
"checksum either 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c67353c641dc847124ea1902d69bd753dee9bb3beff9aa3662ecf86c971d1fac"
"checksum encoding_rs 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)" = "4155785c79f2f6701f185eb2e6b4caf0555ec03477cb4c70db67b465311620ed"
"checksum enum-methods 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7798e7da2d4cb0d6d6fc467e8d6b5bf247e9e989f786dde1732d79899c32bb10"
"checksum env_logger 0.5.13 (registry+https://github.com/rust-lang/crates.io-index)" = "15b0a4d2e39f8420210be8b27eeda28029729e2fd4291019455016c348240c38"
"checksum env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b61fa891024a945da30a9581546e8cfaf5602c7b3f4c137a2805cf388f92075a"
"checksum errno 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c2a071601ed01b988f896ab14b95e67335d1eeb50190932a1320f7fe3cadc84e"
"checksum errno-dragonfly 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "14ca354e36190500e1e1fb267c647932382b54053c50b14970856c0b00a35067"
@ -2592,7 +2685,9 @@ dependencies = [
"checksum futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)" = "49e7653e374fe0d0c12de4250f0bdb60680b8c80eed558c5c7538eec9c89e21b"
"checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4"
"checksum gcc 0.3.55 (registry+https://github.com/rust-lang/crates.io-index)" = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2"
"checksum generational-arena 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4024f96ffa0ebaaf36aa589cd41f2fd69f3a5e6fd02c86e11e12cdf41d5b46a3"
"checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592"
"checksum getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7292d30132fb5424b354f5dc02512a86e4c516fe544bb7a25e7f266951b797"
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
"checksum goblin 0.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "84473a5302fa5094d3d9911c2f312f522f9a37462a777f195f63fae1bf7faf4d"
"checksum h2 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "910a5e7be6283a9c91b3982fa5188368c8719cce2a3cf3b86048673bf9d9c36b"
@ -2621,7 +2716,9 @@ dependencies = [
"checksum llvm-sys 70.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60a9ee82fe0fa72ae6ef6d018b407296085863836451c7a97384f84ed7e26b9f"
"checksum lock_api 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "62ebf1391f6acad60e5c8b43706dde4582df75c06698ab44511d15016bc2442c"
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
"checksum lz4 1.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fe55d2ebbc2e4fc987e6fbfc13f416d97b06d06e50bc1124d613aa790842f80c"
"checksum lz4 1.23.1 (git+https://github.com/zboxfs/lz4-rs.git)" = "<none>"
"checksum lz4-sys 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a59044c3ba3994f3d2aa2270ddd6c5947922219501e67efde5604d36aad462b5"
"checksum lz4-sys 1.8.3 (git+https://github.com/zboxfs/lz4-rs.git)" = "<none>"
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
"checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39"
@ -2660,6 +2757,7 @@ dependencies = [
"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c"
"checksum plain 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6"
"checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915"
"checksum pulldown-cmark 0.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "1058d7bb927ca067656537eec4e02c2b4b70eaaa129664c5b90c111e20326f41"
"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0"
"checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
"checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1"
@ -2707,6 +2805,7 @@ dependencies = [
"checksum serde_test 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "70807e147558b5253cb70f55d343db1d07204d773087c96d0f35fced295dba82"
"checksum serde_urlencoded 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d48f9f99cd749a2de71d29da5f948de7f2764cc5a9d7f3c97e3514d4ee6eabf2"
"checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac"
"checksum skeptic 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dd7d8dc1315094150052d0ab767840376335a98ac66ef313ff911cdf439a5b69"
"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
"checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be"
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
@ -2776,6 +2875,7 @@ dependencies = [
"checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
"checksum xattr 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "244c3741f4240ef46274860397c7c74e50eb23624996930e484c16679633a54c"
"checksum zbox 0.6.1 (git+https://github.com/wasmerio/zbox?branch=bundle-libsodium)" = "<none>"
"checksum zbox 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4a969b504de4ee47ac68bbd035b0c5a53818edb1edd6737131de001de2a09e64"
"checksum zstd 0.4.22+zstd.1.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6f042dd18d52854d302d3d92f66d0a63c2d520d7b7034a9d43cde7441d1b4ddd"
"checksum zstd-safe 1.4.7+zstd.1.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "63febf0b0dcd076db81e6b3110ed254cfb8ed54378a4c3cfbb68956e839d4f59"
"checksum zstd-sys 1.4.8+zstd.1.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "4cb187d624025a7d9878ecf13437491869423426183ded2fa40d4651b85f7ae7"

View File

@ -126,34 +126,14 @@ unsafe impl WasmExternType for f64 {
// fn swap(&self, other: Self::Primitive) -> Self::Primitive;
// }
pub enum ValueError {
BufferTooSmall,
}
pub trait ValueType: Copy
pub unsafe trait ValueType: Copy
where
Self: Sized,
{
fn into_le(self, buffer: &mut [u8]);
fn from_le(buffer: &[u8]) -> Result<Self, ValueError>;
}
{}
macro_rules! convert_value_impl {
($t:ty) => {
impl ValueType for $t {
fn into_le(self, buffer: &mut [u8]) {
buffer[..mem::size_of::<Self>()].copy_from_slice(&self.to_le_bytes());
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<Self>() {
let mut array = [0u8; mem::size_of::<Self>()];
array.copy_from_slice(&buffer[..mem::size_of::<Self>()]);
Ok(Self::from_le_bytes(array))
} else {
Err(ValueError::BufferTooSmall)
}
}
}
unsafe impl ValueType for $t {}
};
( $($t:ty),* ) => {
$(
@ -162,25 +142,7 @@ macro_rules! convert_value_impl {
};
}
convert_value_impl!(u8, i8, u16, i16, u32, i32, u64, i64);
impl ValueType for f32 {
fn into_le(self, buffer: &mut [u8]) {
self.to_bits().into_le(buffer);
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
Ok(f32::from_bits(<u32 as ValueType>::from_le(buffer)?))
}
}
impl ValueType for f64 {
fn into_le(self, buffer: &mut [u8]) {
self.to_bits().into_le(buffer);
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
Ok(f64::from_bits(<u64 as ValueType>::from_le(buffer)?))
}
}
convert_value_impl!(u8, i8, u16, i16, u32, i32, u64, i64, f32, f64);
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ElementType {

View File

@ -13,3 +13,4 @@ hashbrown = "0.1.8"
generational-arena = "0.2.2"
zbox = "0.6.1"
log = "0.4.6"
byteorder = "1.3.1"

View File

@ -1,4 +1,3 @@
#[macro_use]
extern crate log;
@ -7,7 +6,7 @@ mod state;
mod syscalls;
mod utils;
use self::state::{WasiState, WasiFs};
use self::state::{WasiFs, WasiState};
use self::syscalls::*;
use std::ffi::c_void;

View File

@ -1,7 +1,7 @@
use std::{cell::Cell, fmt, marker::PhantomData, mem};
use wasmer_runtime_core::{
memory::Memory,
types::{Type, ValueError, ValueType, WasmExternType},
types::{Type, ValueType, WasmExternType},
};
pub struct Array;
@ -68,23 +68,7 @@ unsafe impl<T: Copy, Ty> WasmExternType for WasmPtr<T, Ty> {
const TYPE: Type = Type::I32;
}
impl<T: Copy, Ty> ValueType for WasmPtr<T, Ty> {
fn into_le(self, buffer: &mut [u8]) {
buffer[..mem::size_of::<u32>()].copy_from_slice(&self.offset.to_le_bytes());
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<Self>() {
let mut array = [0u8; mem::size_of::<u32>()];
array.copy_from_slice(&buffer[..mem::size_of::<u32>()]);
Ok(Self {
offset: u32::from_le_bytes(array),
_phantom: PhantomData,
})
} else {
Err(ValueError::BufferTooSmall)
}
}
}
unsafe impl<T: Copy, Ty> ValueType for WasmPtr<T, Ty> {}
impl<T: Copy, Ty> Clone for WasmPtr<T, Ty> {
fn clone(&self) -> Self {

View File

@ -2,22 +2,16 @@
// vfs::Vfs,
// file_like::{FileLike, Metadata};
// };
use crate::syscalls::types::*;
use generational_arena::{Arena, Index as Inode};
use hashbrown::hash_map::{Entry, HashMap};
use std::{
cell::{Cell, RefCell},
ops::{Index, IndexMut},
rc::Rc,
time::SystemTime,
};
use generational_arena::{Arena, Index as Inode};
use hashbrown::hash_map::{HashMap, Entry};
use zbox::{
RepoOpener,
Repo,
File,
FileType,
OpenOptions,
};
use crate::syscalls::types::*;
use zbox::{File, FileType, OpenOptions, Repo, RepoOpener};
pub const MAX_SYMLINKS: usize = 100;
@ -64,7 +58,10 @@ pub struct WasiFs {
impl WasiFs {
pub fn new() -> Result<Self, String> {
Ok(Self {
repo: RepoOpener::new().create(true).open("mem://📂", "very unsafe pwd").map_err(|e| e.to_string())?,
repo: RepoOpener::new()
.create(true)
.open("mem://📂", "very unsafe pwd")
.map_err(|e| e.to_string())?,
name_map: HashMap::new(),
inodes: Arena::new(),
fd_map: HashMap::new(),
@ -77,12 +74,17 @@ impl WasiFs {
Some(match self.name_map.entry(path.to_string()) {
Entry::Occupied(o) => *o.get(),
Entry::Vacant(v) => {
let file = if let Ok(file) = OpenOptions::new().read(true).write(true).create(false).open(&mut self.repo, path) {
let file = if let Ok(file) = OpenOptions::new()
.read(true)
.write(true)
.create(false)
.open(&mut self.repo, path)
{
file
} else {
return None;
};
let metadata = file.metadata().unwrap();
let inode_index = {
let index = self.inode_counter.get();
@ -90,7 +92,9 @@ impl WasiFs {
};
let systime_to_nanos = |systime: SystemTime| {
let duration = systime.duration_since(SystemTime::UNIX_EPOCH).expect("should always be after unix epoch");
let duration = systime
.duration_since(SystemTime::UNIX_EPOCH)
.expect("should always be after unix epoch");
duration.as_nanos() as u64
};
@ -111,9 +115,7 @@ impl WasiFs {
is_preopened: false,
name: path.to_string(),
kind: match metadata.file_type() {
FileType::File => Kind::File {
handle: file,
},
FileType::File => Kind::File { handle: file },
FileType::Dir => Kind::Dir {
handle: file,
entries: Vec::new(),
@ -122,19 +124,28 @@ impl WasiFs {
});
v.insert(inode);
inode
},
}
})
}
fn filestat_inode(&self, inode: Inode, flags: __wasi_lookupflags_t) -> Result<__wasi_filestat_t, __wasi_errno_t> {
fn filestat_inode(
&self,
inode: Inode,
flags: __wasi_lookupflags_t,
) -> Result<__wasi_filestat_t, __wasi_errno_t> {
let inode_val = &self.inodes[inode];
if let (true, Kind::Symlink { mut forwarded }) = (flags & __WASI_LOOKUP_SYMLINK_FOLLOW != 0, &inode_val.kind) {
if let (true, Kind::Symlink { mut forwarded }) =
(flags & __WASI_LOOKUP_SYMLINK_FOLLOW != 0, &inode_val.kind)
{
// Time to follow the symlink.
let mut counter = 0;
while counter <= MAX_SYMLINKS {
let inode_val = &self.inodes[forwarded];
if let &Kind::Symlink { forwarded: new_forwarded } = &inode_val.kind {
if let &Kind::Symlink {
forwarded: new_forwarded,
} = &inode_val.kind
{
counter += 1;
forwarded = new_forwarded;
} else {
@ -171,7 +182,27 @@ impl WasiFs {
return Err(__WASI_EBADF);
};
self.filestat_inode(fd.inode, 0)
Ok(self.inodes[fd.inode].stat)
}
pub fn fdstat(&self, fd: __wasi_fd_t) -> Result<__wasi_fdstat_t, __wasi_errno_t> {
let fd = if let Some(fd) = self.fd_map.get(&fd) {
fd
} else {
return Err(__WASI_EBADF);
};
Ok(__wasi_fdstat_t {
fs_filetype: match self.inodes[fd.inode].kind {
Kind::File { .. } => __WASI_FILETYPE_REGULAR_FILE,
Kind::Dir { .. } => __WASI_FILETYPE_DIRECTORY,
Kind::Symlink { .. } => __WASI_FILETYPE_SYMBOLIC_LINK,
_ => __WASI_FILETYPE_UNKNOWN,
},
fs_flags: fd.flags,
fs_rights_base: fd.rights,
fs_rights_inheriting: fd.rights, // TODO(lachlan): Is this right?
})
}
}

View File

@ -229,7 +229,7 @@ pub fn fd_fdstat_get(
let mut state = get_wasi_state(ctx);
let memory = ctx.memory(0);
let stat = match state.fs.filestat_fd(fd) {
let stat = match state.fs.fdstat(fd) {
Ok(stat) => stat,
Err(errno) => return errno,
};
@ -261,7 +261,20 @@ pub fn fd_filestat_get(
fd: __wasi_fd_t,
buf: WasmPtr<__wasi_filestat_t>,
) -> __wasi_errno_t {
unimplemented!()
let mut state = get_wasi_state(ctx);
let memory = ctx.memory(0);
let stat = match state.fs.filestat_fd(fd) {
Ok(stat) => stat,
Err(errno) => return errno,
};
if let Some(buf) = buf.deref(memory) {
buf.set(stat);
__WASI_ESUCCESS
} else {
__WASI_EFAULT
}
}
pub fn fd_filestat_set_size(
ctx: &mut Ctx,

View File

@ -1,8 +1,9 @@
#![allow(non_camel_case_types)]
use crate::ptr::{Array, WasmPtr};
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
use std::mem;
use wasmer_runtime_core::types::{ValueError, ValueType};
use wasmer_runtime_core::types::ValueType;
pub type __wasi_advice_t = u8;
pub const __WASI_ADVICE_DONTNEED: u8 = 0;
@ -19,24 +20,7 @@ pub struct __wasi_ciovec_t {
pub buf_len: u32,
}
impl ValueType for __wasi_ciovec_t {
fn into_le(self, buffer: &mut [u8]) {
self.buf
.into_le(&mut buffer[..mem::size_of::<WasmPtr<u8, Array>>()]);
self.buf_len
.into_le(&mut buffer[mem::size_of::<WasmPtr<u8, Array>>()..]);
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<__wasi_ciovec_t>() {
let buf = ValueType::from_le(&buffer[..mem::size_of::<WasmPtr<u8, Array>>()])?;
let buf_len = ValueType::from_le(&buffer[mem::size_of::<WasmPtr<u8, Array>>()..])?;
Ok(Self { buf, buf_len })
} else {
Err(ValueError::BufferTooSmall)
}
}
}
unsafe impl ValueType for __wasi_ciovec_t {}
pub type __wasi_clockid_t = u32;
pub const __WASI_CLOCK_MONOTONIC: u32 = 0;
@ -190,21 +174,7 @@ pub struct __wasi_prestat_u_dir_t {
pr_name_len: u32,
}
impl ValueType for __wasi_prestat_u_dir_t {
fn into_le(self, buffer: &mut [u8]) {
self.pr_name_len
.into_le(&mut buffer[..mem::size_of::<u32>()]);
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<__wasi_prestat_u_dir_t>() {
let pr_name_len = ValueType::from_le(&buffer[..mem::size_of::<u32>()])?;
Ok(Self { pr_name_len })
} else {
Err(ValueError::BufferTooSmall)
}
}
}
unsafe impl ValueType for __wasi_prestat_u_dir_t {}
#[derive(Copy, Clone)]
#[repr(C)]
@ -212,23 +182,7 @@ pub union __wasi_prestat_u {
dir: __wasi_prestat_u_dir_t,
}
impl ValueType for __wasi_prestat_u {
fn into_le(self, buffer: &mut [u8]) {
unsafe {
self.dir
.into_le(&mut buffer[..mem::size_of::<__wasi_prestat_u_dir_t>()])
};
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<__wasi_prestat_u>() {
let dir = ValueType::from_le(&buffer[..mem::size_of::<__wasi_prestat_u_dir_t>()])?;
Ok(Self { dir })
} else {
Err(ValueError::BufferTooSmall)
}
}
}
unsafe impl ValueType for __wasi_prestat_u {}
#[derive(Copy, Clone)]
#[repr(C)]
@ -237,24 +191,7 @@ pub struct __wasi_prestat_t {
u: __wasi_prestat_u,
}
impl ValueType for __wasi_prestat_t {
fn into_le(self, buffer: &mut [u8]) {
self.pr_type
.into_le(&mut buffer[..mem::size_of::<__wasi_preopentype_t>()]);
self.u
.into_le(&mut buffer[mem::size_of::<__wasi_preopentype_t>()..]);
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<__wasi_prestat_t>() {
let pr_type = ValueType::from_le(&buffer[..mem::size_of::<__wasi_preopentype_t>()])?;
let u = ValueType::from_le(&buffer[mem::size_of::<__wasi_preopentype_t>()..])?;
Ok(Self { pr_type, u })
} else {
Err(ValueError::BufferTooSmall)
}
}
}
unsafe impl ValueType for __wasi_prestat_t {}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(C)]
@ -265,50 +202,7 @@ pub struct __wasi_fdstat_t {
pub fs_rights_inheriting: __wasi_rights_t,
}
impl ValueType for __wasi_fdstat_t {
fn into_le(self, buffer: &mut [u8]) {
self.fs_filetype
.into_le(&mut buffer[..mem::size_of::<__wasi_filetype_t>()]);
self.fs_flags.into_le(
&mut buffer[mem::size_of::<__wasi_filetype_t>()..mem::size_of::<__wasi_fdflags_t>()],
);
self.fs_rights_base.into_le(
&mut buffer[(mem::size_of::<__wasi_filetype_t>() + mem::size_of::<__wasi_fdflags_t>())
..mem::size_of::<__wasi_rights_t>()],
);
self.fs_rights_inheriting.into_le(
&mut buffer[(mem::size_of::<__wasi_filetype_t>()
+ mem::size_of::<__wasi_fdflags_t>()
+ mem::size_of::<__wasi_rights_t>())..],
);
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<__wasi_fdstat_t>() {
let fs_filetype = ValueType::from_le(&buffer[..mem::size_of::<__wasi_filetype_t>()])?;
let fs_flags = ValueType::from_le(
&buffer[mem::size_of::<__wasi_filetype_t>()..mem::size_of::<__wasi_fdflags_t>()],
)?;
let fs_rights_base = ValueType::from_le(
&buffer[(mem::size_of::<__wasi_filetype_t>() + mem::size_of::<__wasi_fdflags_t>())
..mem::size_of::<__wasi_rights_t>()],
)?;
let fs_rights_inheriting = ValueType::from_le(
&buffer[(mem::size_of::<__wasi_filetype_t>()
+ mem::size_of::<__wasi_fdflags_t>()
+ mem::size_of::<__wasi_rights_t>())..],
)?;
Ok(Self {
fs_filetype,
fs_flags,
fs_rights_base,
fs_rights_inheriting,
})
} else {
Err(ValueError::BufferTooSmall)
}
}
}
unsafe impl ValueType for __wasi_fdstat_t {}
pub type __wasi_filedelta_t = i64;
@ -327,6 +221,8 @@ pub struct __wasi_filestat_t {
pub st_ctim: __wasi_timestamp_t,
}
unsafe impl ValueType for __wasi_filestat_t {}
pub type __wasi_filetype_t = u8;
pub const __WASI_FILETYPE_UNKNOWN: u8 = 0;
pub const __WASI_FILETYPE_BLOCK_DEVICE: u8 = 1;
@ -352,24 +248,7 @@ pub struct __wasi_iovec_t {
pub buf_len: u32,
}
impl ValueType for __wasi_iovec_t {
fn into_le(self, buffer: &mut [u8]) {
self.buf
.into_le(&mut buffer[..mem::size_of::<WasmPtr<u8, Array>>()]);
self.buf_len
.into_le(&mut buffer[mem::size_of::<WasmPtr<u8, Array>>()..]);
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<__wasi_iovec_t>() {
let buf = ValueType::from_le(&buffer[..mem::size_of::<WasmPtr<u8, Array>>()])?;
let buf_len = ValueType::from_le(&buffer[mem::size_of::<WasmPtr<u8, Array>>()..])?;
Ok(Self { buf, buf_len })
} else {
Err(ValueError::BufferTooSmall)
}
}
}
unsafe impl ValueType for __wasi_iovec_t {}
pub type __wasi_linkcount_t = u32;