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

@@ -1,34 +1,20 @@
use cap_std::time::{Duration, Instant, MonotonicClock, SystemClock, SystemTime};
use cap_time_ext::{MonotonicClockExt, SystemClockExt};
use cap_std::time::{Duration, Instant, SystemTime};
pub enum SystemTimeSpec {
SymbolicNow,
Absolute(SystemTime),
}
pub trait WasiSystemClock {
fn resolution(&self) -> Duration;
fn now(&self, precision: Duration) -> SystemTime;
}
impl WasiSystemClock for SystemClock {
fn resolution(&self) -> Duration {
SystemClockExt::resolution(self)
}
fn now(&self, precision: Duration) -> SystemTime {
self.now_with(precision)
}
}
pub trait WasiMonotonicClock {
fn resolution(&self) -> Duration;
fn now(&self, precision: Duration) -> Instant;
}
impl WasiMonotonicClock for MonotonicClock {
fn resolution(&self) -> Duration {
MonotonicClockExt::resolution(self)
}
fn now(&self, precision: Duration) -> Instant {
self.now_with(precision)
}
}
pub struct WasiClocks {
pub system: Box<dyn WasiSystemClock>,
pub monotonic: Box<dyn WasiMonotonicClock>,

View File

@@ -1,7 +1,6 @@
use crate::error::Error;
use crate::file::{FdFlags, FileCaps, FileType, Filestat, OFlags, WasiFile};
use crate::{Error, SystemTimeSpec};
use bitflags::bitflags;
use cap_fs_ext::SystemTimeSpec;
use std::any::Any;
use std::cell::Ref;
use std::ops::Deref;

View File

@@ -1,42 +1,43 @@
use crate::Error;
use crate::{Error, SystemTimeSpec};
use bitflags::bitflags;
use fs_set_times::SystemTimeSpec;
use std::any::Any;
use std::cell::Ref;
use std::ops::Deref;
pub trait WasiFile {
fn as_any(&self) -> &dyn Any;
fn datasync(&self) -> Result<(), Error>;
fn sync(&self) -> Result<(), Error>;
fn get_filetype(&self) -> Result<FileType, Error>;
fn get_fdflags(&self) -> Result<FdFlags, Error>;
fn datasync(&self) -> Result<(), Error>; // write op
fn sync(&self) -> Result<(), Error>; // file op
fn get_filetype(&self) -> Result<FileType, Error>; // file op
fn get_fdflags(&self) -> Result<FdFlags, Error>; // file op
fn set_fdflags(&self, _flags: FdFlags) -> Result<(), Error>;
fn get_filestat(&self) -> Result<Filestat, Error>;
fn set_filestat_size(&self, _size: u64) -> Result<(), Error>;
fn get_filestat(&self) -> Result<Filestat, Error>; // split out get_length as a read & write op, rest is a file op
fn set_filestat_size(&self, _size: u64) -> Result<(), Error>; // write op
fn advise(
&self,
offset: u64,
len: u64,
advice: system_interface::fs::Advice,
) -> Result<(), Error>;
fn allocate(&self, offset: u64, len: u64) -> Result<(), Error>;
) -> Result<(), Error>; // file op
fn allocate(&self, offset: u64, len: u64) -> Result<(), Error>; // write op
fn set_times(
&self,
atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>,
) -> Result<(), Error>;
fn read_vectored(&self, bufs: &mut [std::io::IoSliceMut]) -> Result<u64, Error>;
fn read_vectored(&self, bufs: &mut [std::io::IoSliceMut]) -> Result<u64, Error>; // read op
fn read_vectored_at(&self, bufs: &mut [std::io::IoSliceMut], offset: u64)
-> Result<u64, Error>;
fn write_vectored(&self, bufs: &[std::io::IoSlice]) -> Result<u64, Error>;
fn write_vectored_at(&self, bufs: &[std::io::IoSlice], offset: u64) -> Result<u64, Error>;
fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error>;
fn stream_position(&self) -> Result<u64, Error>;
fn peek(&self, buf: &mut [u8]) -> Result<u64, Error>;
fn num_ready_bytes(&self) -> Result<u64, Error>;
-> Result<u64, Error>; // file op
fn write_vectored(&self, bufs: &[std::io::IoSlice]) -> Result<u64, Error>; // write op
fn write_vectored_at(&self, bufs: &[std::io::IoSlice], offset: u64) -> Result<u64, Error>; // file op
fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error>; // file op that generates a new stream from a file will supercede this
fn peek(&self, buf: &mut [u8]) -> Result<u64, Error>; // read op
fn num_ready_bytes(&self) -> Result<u64, Error>; // read op
}
// XXX we will add pipes to wasi - lets add it to this internal enum and present them as
// Unknown to old wasis
// XXX put the enum variants in same order as WASI so conversion funcs are no-op
#[derive(Debug, Copy, Clone)]
pub enum FileType {
Directory,
@@ -74,7 +75,7 @@ pub struct Filestat {
pub inode: u64,
pub filetype: FileType,
pub nlink: u64,
pub size: u64,
pub size: u64, // this is a read field, the rest are file fields
pub atim: Option<std::time::SystemTime>,
pub mtim: Option<std::time::SystemTime>,
pub ctim: Option<std::time::SystemTime>,

View File

@@ -12,7 +12,7 @@ pub mod snapshots;
mod string_array;
pub mod table;
pub use cap_fs_ext::SystemTimeSpec;
pub use clocks::SystemTimeSpec;
pub use ctx::{WasiCtx, WasiCtxBuilder};
pub use dir::{DirCaps, ReaddirCursor, ReaddirEntity, WasiDir};
pub use error::Error;

View File

@@ -9,8 +9,10 @@
//! Some convenience constructors are included for common backing types like `Vec<u8>` and `String`,
//! but the virtual pipes can be instantiated with any `Read` or `Write` type.
//!
use crate::file::{FdFlags, FileType, Filestat, WasiFile};
use crate::Error;
use crate::{
file::{FdFlags, FileType, Filestat, WasiFile},
Error, SystemTimeSpec,
};
use std::any::Any;
use std::convert::TryInto;
use std::io::{self, Read, Write};
@@ -152,16 +154,13 @@ impl<R: Read + Any> WasiFile for ReadPipe<R> {
fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::Badf)
}
fn stream_position(&self) -> Result<u64, Error> {
Err(Error::Badf)
}
fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
Err(Error::Badf)
}
fn set_times(
&self,
atime: Option<fs_set_times::SystemTimeSpec>,
mtime: Option<fs_set_times::SystemTimeSpec>,
atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>,
) -> Result<(), Error> {
Err(Error::Badf)
}
@@ -291,16 +290,13 @@ impl<W: Write + Any> WasiFile for WritePipe<W> {
fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error> {
Err(Error::Badf)
}
fn stream_position(&self) -> Result<u64, Error> {
Err(Error::Badf)
}
fn peek(&self, buf: &mut [u8]) -> Result<u64, Error> {
Err(Error::Badf)
}
fn set_times(
&self,
atime: Option<fs_set_times::SystemTimeSpec>,
mtime: Option<fs_set_times::SystemTimeSpec>,
atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>,
) -> Result<(), Error> {
Err(Error::Badf)
}

View File

@@ -1,14 +1,17 @@
#![allow(unused_variables)]
use crate::dir::{
DirCaps, DirEntry, DirEntryExt, DirFdStat, ReaddirCursor, ReaddirEntity, TableDirExt,
use crate::{
dir::{DirCaps, DirEntry, DirEntryExt, DirFdStat, ReaddirCursor, ReaddirEntity, TableDirExt},
file::{
FdFlags, FdStat, FileCaps, FileEntry, FileEntryExt, FileType, Filestat, OFlags,
TableFileExt,
},
sched::{
subscription::{RwEventFlags, SubscriptionResult},
Poll,
},
Error, SystemTimeSpec, WasiCtx,
};
use crate::file::{
FdFlags, FdStat, FileCaps, FileEntry, FileEntryExt, FileType, Filestat, OFlags, TableFileExt,
};
use crate::sched::subscription::{RwEventFlags, SubscriptionResult};
use crate::sched::Poll;
use crate::{Error, WasiCtx};
use fs_set_times::SystemTimeSpec;
use cap_std::time::{Duration, SystemClock};
use std::cell::{Ref, RefMut};
use std::convert::{TryFrom, TryInto};
use std::io::{IoSlice, IoSliceMut};
@@ -172,7 +175,6 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
id: types::Clockid,
precision: types::Timestamp,
) -> Result<types::Timestamp, Error> {
use cap_std::time::Duration;
let precision = Duration::from_nanos(precision);
match id {
types::Clockid::Realtime => {
@@ -344,55 +346,31 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
return Err(Error::Inval);
}
let atim = if set_atim {
Some(SystemTimeSpec::Absolute(
SystemClock::UNIX_EPOCH + Duration::from_nanos(atim),
))
} else if set_atim_now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
let mtim = if set_mtim {
Some(SystemTimeSpec::Absolute(
SystemClock::UNIX_EPOCH + Duration::from_nanos(mtim),
))
} else if set_mtim_now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
if table.is::<FileEntry>(fd) {
use std::time::{Duration, UNIX_EPOCH};
let atim = if set_atim {
Some(SystemTimeSpec::Absolute(
UNIX_EPOCH + Duration::from_nanos(atim),
))
} else if set_atim_now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
let mtim = if set_mtim {
Some(SystemTimeSpec::Absolute(
UNIX_EPOCH + Duration::from_nanos(mtim),
))
} else if set_mtim_now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
table
.get_file(fd)
.expect("checked that entry is file")
.get_cap(FileCaps::FILESTAT_SET_TIMES)?
.set_times(atim, mtim)
} else if table.is::<DirEntry>(fd) {
use cap_std::time::{Duration, SystemClock};
use cap_fs_ext::SystemTimeSpec;
let atim = if set_atim {
Some(SystemTimeSpec::Absolute(
SystemClock::UNIX_EPOCH + Duration::from_nanos(atim),
))
} else if set_atim_now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
let mtim = if set_mtim {
Some(SystemTimeSpec::Absolute(
SystemClock::UNIX_EPOCH + Duration::from_nanos(mtim),
))
} else if set_mtim_now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
table
.get_dir(fd)
.expect("checked that entry is dir")
@@ -599,6 +577,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
}
fn fd_tell(&self, fd: types::Fd) -> Result<types::Filesize, Error> {
// XXX should this be stream_position?
let offset = self
.table()
.get_file(u32::from(fd))?
@@ -694,7 +673,6 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
mtim: types::Timestamp,
fst_flags: types::Fstflags,
) -> Result<(), Error> {
// XXX DRY these are in fd_filestat_set_times twice!
let set_atim = fst_flags.contains(types::Fstflags::ATIM);
let set_atim_now = fst_flags.contains(types::Fstflags::ATIM_NOW);
let set_mtim = fst_flags.contains(types::Fstflags::MTIM);
@@ -702,27 +680,19 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
return Err(Error::Inval);
}
use cap_fs_ext::SystemTimeSpec;
use cap_std::time::{Duration, SystemClock};
let atim = if set_atim {
Some(SystemTimeSpec::Absolute(
SystemClock::UNIX_EPOCH + Duration::from_nanos(atim),
))
} else if set_atim_now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
let mtim = if set_mtim {
Some(SystemTimeSpec::Absolute(
SystemClock::UNIX_EPOCH + Duration::from_nanos(mtim),
))
} else if set_mtim_now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
};
fn systimespec(set: bool, ts: types::Timestamp, now: bool) -> Option<SystemTimeSpec> {
if set {
Some(SystemTimeSpec::Absolute(
SystemClock::UNIX_EPOCH + Duration::from_nanos(ts),
))
} else if now {
Some(SystemTimeSpec::SymbolicNow)
} else {
None
}
}
let atim = systimespec(set_atim, atim, set_atim_now);
let mtim = systimespec(set_mtim, mtim, set_mtim_now);
self.table()
.get_dir(u32::from(dirfd))?
.get_cap(DirCaps::PATH_FILESTAT_SET_TIMES)?
@@ -893,7 +863,6 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
return Err(Error::Inval);
}
use cap_std::time::Duration;
let table = self.table();
let mut poll = Poll::new();