move more deps to cap-std-sync, define own SystemTimeSpec

This commit is contained in:
Pat Hickey
2021-01-21 12:35:55 -08:00
parent 61885b7071
commit fcd00f5de1
13 changed files with 160 additions and 167 deletions

View File

@@ -0,0 +1,46 @@
use cap_std::time::{Duration, Instant, SystemTime};
use cap_time_ext::{MonotonicClockExt, SystemClockExt};
use wasi_c2::clocks::{WasiClocks, WasiMonotonicClock, WasiSystemClock};
pub struct SystemClock(cap_std::time::SystemClock);
impl SystemClock {
pub unsafe fn new() -> Self {
SystemClock(cap_std::time::SystemClock::new())
}
}
impl WasiSystemClock for SystemClock {
fn resolution(&self) -> Duration {
self.0.resolution()
}
fn now(&self, precision: Duration) -> SystemTime {
self.0.now_with(precision)
}
}
pub struct MonotonicClock(cap_std::time::MonotonicClock);
impl MonotonicClock {
pub unsafe fn new() -> Self {
MonotonicClock(cap_std::time::MonotonicClock::new())
}
}
impl WasiMonotonicClock for MonotonicClock {
fn resolution(&self) -> Duration {
self.0.resolution()
}
fn now(&self, precision: Duration) -> Instant {
self.0.now_with(precision)
}
}
pub fn clocks() -> WasiClocks {
let system = Box::new(unsafe { SystemClock::new() });
let monotonic = unsafe { cap_std::time::MonotonicClock::new() };
let creation_time = monotonic.now();
let monotonic = Box::new(MonotonicClock(monotonic));
WasiClocks {
system,
monotonic,
creation_time,
}
}

View File

@@ -197,7 +197,7 @@ impl WasiDir for Dir {
fn hard_link(
&self,
src_path: &str,
symlink_follow: bool,
symlink_follow: bool, // XXX not in cap-std yet
target_dir: &dyn WasiDir,
target_path: &str,
) -> Result<(), Error> {
@@ -213,10 +213,22 @@ impl WasiDir for Dir {
fn set_times(
&self,
path: &str,
atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>,
atime: Option<wasi_c2::SystemTimeSpec>,
mtime: Option<wasi_c2::SystemTimeSpec>,
) -> Result<(), Error> {
self.0.set_times(Path::new(path), atime, mtime)?;
self.0.set_times(
Path::new(path),
convert_systimespec(atime),
convert_systimespec(mtime),
)?;
Ok(())
}
}
fn convert_systimespec(t: Option<wasi_c2::SystemTimeSpec>) -> Option<SystemTimeSpec> {
match t {
Some(wasi_c2::SystemTimeSpec::Absolute(t)) => Some(SystemTimeSpec::Absolute(t)),
Some(wasi_c2::SystemTimeSpec::SymbolicNow) => Some(SystemTimeSpec::SymbolicNow),
None => None,
}
}

View File

@@ -68,10 +68,11 @@ impl WasiFile for File {
}
fn set_times(
&self,
atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>,
atime: Option<wasi_c2::SystemTimeSpec>,
mtime: Option<wasi_c2::SystemTimeSpec>,
) -> Result<(), Error> {
self.0.set_times(atime, mtime)?;
self.0
.set_times(convert_systimespec(atime), convert_systimespec(mtime))?;
Ok(())
}
fn read_vectored(&self, bufs: &mut [io::IoSliceMut]) -> Result<u64, Error> {
@@ -93,9 +94,6 @@ impl WasiFile for File {
fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> {
Ok(self.0.seek(pos)?)
}
fn stream_position(&self) -> Result<u64, Error> {
Ok(self.0.stream_position()?)
}
fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
let n = self.0.peek(buf)?;
Ok(n.try_into().map_err(|_| Error::Overflow)?)
@@ -145,3 +143,10 @@ impl AsRawFd for File {
self.0.as_raw_fd()
}
}
pub fn convert_systimespec(t: Option<wasi_c2::SystemTimeSpec>) -> Option<SystemTimeSpec> {
match t {
Some(wasi_c2::SystemTimeSpec::Absolute(t)) => Some(SystemTimeSpec::Absolute(t.into_std())),
Some(wasi_c2::SystemTimeSpec::SymbolicNow) => Some(SystemTimeSpec::SymbolicNow),
None => None,
}
}

View File

@@ -1,3 +1,4 @@
pub mod clocks;
pub mod dir;
pub mod file;
pub mod sched;
@@ -7,7 +8,7 @@ use cap_rand::RngCore;
use std::cell::RefCell;
use std::path::Path;
use std::rc::Rc;
use wasi_c2::{clocks::WasiClocks, table::Table, Error, WasiCtx, WasiFile};
use wasi_c2::{table::Table, Error, WasiCtx, WasiFile};
pub struct WasiCtxBuilder(wasi_c2::WasiCtxBuilder);
@@ -15,7 +16,7 @@ impl WasiCtxBuilder {
pub fn new() -> Self {
WasiCtxBuilder(WasiCtx::builder(
random(),
clocks(),
clocks::clocks(),
Box::new(sched::SyncSched),
Rc::new(RefCell::new(Table::new())),
))
@@ -51,18 +52,6 @@ impl WasiCtxBuilder {
}
}
pub fn clocks() -> WasiClocks {
let system = Box::new(unsafe { cap_std::time::SystemClock::new() });
let monotonic = unsafe { cap_std::time::MonotonicClock::new() };
let creation_time = monotonic.now();
let monotonic = Box::new(monotonic);
WasiClocks {
system,
monotonic,
creation_time,
}
}
pub fn random() -> RefCell<Box<dyn RngCore>> {
RefCell::new(Box::new(unsafe { cap_rand::rngs::OsRng::default() }))
}

View File

@@ -1,3 +1,4 @@
use crate::file::convert_systimespec;
use fs_set_times::SetTimes;
use std::any::Any;
use std::convert::TryInto;
@@ -87,19 +88,16 @@ macro_rules! wasi_file_impl {
fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> {
Ok(self.0.seek(pos)?)
}
fn stream_position(&self) -> Result<u64, Error> {
Ok(self.0.stream_position()?)
}
fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
let n = self.0.peek(buf)?;
Ok(n.try_into().map_err(|_| Error::Overflow)?)
}
fn set_times(
&self,
atime: Option<fs_set_times::SystemTimeSpec>,
mtime: Option<fs_set_times::SystemTimeSpec>,
atime: Option<wasi_c2::SystemTimeSpec>,
mtime: Option<wasi_c2::SystemTimeSpec>,
) -> Result<(), Error> {
self.0.set_times(atime, mtime)?;
self.0.set_times(convert_systimespec(atime), convert_systimespec(mtime))?;
Ok(())
}
$additional