wasi-cap-std-sync: WasiFile and WasiDir converted to async_traits
This commit is contained in:
@@ -17,11 +17,12 @@ impl Dir {
|
||||
}
|
||||
}
|
||||
|
||||
#[wiggle::async_trait]
|
||||
impl WasiDir for Dir {
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
fn open_file(
|
||||
async fn open_file(
|
||||
&self,
|
||||
symlink_follow: bool,
|
||||
path: &str,
|
||||
@@ -84,7 +85,7 @@ impl WasiDir for Dir {
|
||||
Ok(Box::new(File::from_cap_std(f)))
|
||||
}
|
||||
|
||||
fn open_dir(&self, symlink_follow: bool, path: &str) -> Result<Box<dyn WasiDir>, Error> {
|
||||
async fn open_dir(&self, symlink_follow: bool, path: &str) -> Result<Box<dyn WasiDir>, Error> {
|
||||
let d = if symlink_follow {
|
||||
self.0.open_dir(Path::new(path))?
|
||||
} else {
|
||||
@@ -93,11 +94,11 @@ impl WasiDir for Dir {
|
||||
Ok(Box::new(Dir::from_cap_std(d)))
|
||||
}
|
||||
|
||||
fn create_dir(&self, path: &str) -> Result<(), Error> {
|
||||
async fn create_dir(&self, path: &str) -> Result<(), Error> {
|
||||
self.0.create_dir(Path::new(path))?;
|
||||
Ok(())
|
||||
}
|
||||
fn readdir(
|
||||
async fn readdir(
|
||||
&self,
|
||||
cursor: ReaddirCursor,
|
||||
) -> Result<Box<dyn Iterator<Item = Result<ReaddirEntity, Error>>>, Error> {
|
||||
@@ -146,24 +147,24 @@ impl WasiDir for Dir {
|
||||
Ok(Box::new(rd))
|
||||
}
|
||||
|
||||
fn symlink(&self, src_path: &str, dest_path: &str) -> Result<(), Error> {
|
||||
async fn symlink(&self, src_path: &str, dest_path: &str) -> Result<(), Error> {
|
||||
self.0.symlink(src_path, dest_path)?;
|
||||
Ok(())
|
||||
}
|
||||
fn remove_dir(&self, path: &str) -> Result<(), Error> {
|
||||
async fn remove_dir(&self, path: &str) -> Result<(), Error> {
|
||||
self.0.remove_dir(Path::new(path))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn unlink_file(&self, path: &str) -> Result<(), Error> {
|
||||
async fn unlink_file(&self, path: &str) -> Result<(), Error> {
|
||||
self.0.remove_file_or_symlink(Path::new(path))?;
|
||||
Ok(())
|
||||
}
|
||||
fn read_link(&self, path: &str) -> Result<PathBuf, Error> {
|
||||
async fn read_link(&self, path: &str) -> Result<PathBuf, Error> {
|
||||
let link = self.0.read_link(Path::new(path))?;
|
||||
Ok(link)
|
||||
}
|
||||
fn get_filestat(&self) -> Result<Filestat, Error> {
|
||||
async fn get_filestat(&self) -> Result<Filestat, Error> {
|
||||
let meta = self.0.dir_metadata()?;
|
||||
Ok(Filestat {
|
||||
device_id: meta.dev(),
|
||||
@@ -176,7 +177,11 @@ impl WasiDir for Dir {
|
||||
ctim: meta.created().map(|t| Some(t.into_std())).unwrap_or(None),
|
||||
})
|
||||
}
|
||||
fn get_path_filestat(&self, path: &str, follow_symlinks: bool) -> Result<Filestat, Error> {
|
||||
async fn get_path_filestat(
|
||||
&self,
|
||||
path: &str,
|
||||
follow_symlinks: bool,
|
||||
) -> Result<Filestat, Error> {
|
||||
let meta = if follow_symlinks {
|
||||
self.0.metadata(Path::new(path))?
|
||||
} else {
|
||||
@@ -193,7 +198,12 @@ impl WasiDir for Dir {
|
||||
ctim: meta.created().map(|t| Some(t.into_std())).unwrap_or(None),
|
||||
})
|
||||
}
|
||||
fn rename(&self, src_path: &str, dest_dir: &dyn WasiDir, dest_path: &str) -> Result<(), Error> {
|
||||
async fn rename(
|
||||
&self,
|
||||
src_path: &str,
|
||||
dest_dir: &dyn WasiDir,
|
||||
dest_path: &str,
|
||||
) -> Result<(), Error> {
|
||||
let dest_dir = dest_dir
|
||||
.as_any()
|
||||
.downcast_ref::<Self>()
|
||||
@@ -202,7 +212,7 @@ impl WasiDir for Dir {
|
||||
.rename(Path::new(src_path), &dest_dir.0, Path::new(dest_path))?;
|
||||
Ok(())
|
||||
}
|
||||
fn hard_link(
|
||||
async fn hard_link(
|
||||
&self,
|
||||
src_path: &str,
|
||||
target_dir: &dyn WasiDir,
|
||||
@@ -217,7 +227,7 @@ impl WasiDir for Dir {
|
||||
self.0.hard_link(src_path, &target_dir.0, target_path)?;
|
||||
Ok(())
|
||||
}
|
||||
fn set_times(
|
||||
async fn set_times(
|
||||
&self,
|
||||
path: &str,
|
||||
atime: Option<wasi_common::SystemTimeSpec>,
|
||||
@@ -261,7 +271,7 @@ mod test {
|
||||
let preopen_dir = unsafe { cap_std::fs::Dir::open_ambient_dir(tempdir.path()) }
|
||||
.expect("open ambient temporary dir");
|
||||
let preopen_dir = Dir::from_cap_std(preopen_dir);
|
||||
wasi_common::WasiDir::open_dir(&preopen_dir, false, ".")
|
||||
run(wasi_common::WasiDir::open_dir(&preopen_dir, false, "."))
|
||||
.expect("open the same directory via WasiDir abstraction");
|
||||
}
|
||||
|
||||
@@ -275,9 +285,8 @@ mod test {
|
||||
|
||||
fn readdir_into_map(dir: &dyn WasiDir) -> HashMap<String, ReaddirEntity> {
|
||||
let mut out = HashMap::new();
|
||||
for readdir_result in dir
|
||||
.readdir(ReaddirCursor::from(0))
|
||||
.expect("readdir succeeds")
|
||||
for readdir_result in
|
||||
run(dir.readdir(ReaddirCursor::from(0))).expect("readdir succeeds")
|
||||
{
|
||||
let entity = readdir_result.expect("readdir entry is valid");
|
||||
out.insert(entity.name.clone(), entity);
|
||||
@@ -303,16 +312,15 @@ mod test {
|
||||
assert!(entities.get(".").is_some());
|
||||
assert!(entities.get("..").is_some());
|
||||
|
||||
preopen_dir
|
||||
.open_file(
|
||||
false,
|
||||
"file1",
|
||||
OFlags::CREATE,
|
||||
true,
|
||||
false,
|
||||
FdFlags::empty(),
|
||||
)
|
||||
.expect("create file1");
|
||||
run(preopen_dir.open_file(
|
||||
false,
|
||||
"file1",
|
||||
OFlags::CREATE,
|
||||
true,
|
||||
false,
|
||||
FdFlags::empty(),
|
||||
))
|
||||
.expect("create file1");
|
||||
|
||||
let entities = readdir_into_map(&preopen_dir);
|
||||
assert_eq!(entities.len(), 3, "should be ., .., file1 {:?}", entities);
|
||||
@@ -329,4 +337,41 @@ mod test {
|
||||
FileType::RegularFile
|
||||
);
|
||||
}
|
||||
|
||||
fn run<F: std::future::Future>(future: F) -> F::Output {
|
||||
use std::pin::Pin;
|
||||
use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
|
||||
|
||||
let mut f = Pin::from(Box::new(future));
|
||||
let waker = dummy_waker();
|
||||
let mut cx = Context::from_waker(&waker);
|
||||
match f.as_mut().poll(&mut cx) {
|
||||
Poll::Ready(val) => return val,
|
||||
Poll::Pending => {
|
||||
panic!("Cannot wait on pending future: must enable wiggle \"async\" future and execute on an async Store")
|
||||
}
|
||||
}
|
||||
|
||||
fn dummy_waker() -> Waker {
|
||||
return unsafe { Waker::from_raw(clone(5 as *const _)) };
|
||||
|
||||
unsafe fn clone(ptr: *const ()) -> RawWaker {
|
||||
assert_eq!(ptr as usize, 5);
|
||||
const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake_by_ref, drop);
|
||||
RawWaker::new(ptr, &VTABLE)
|
||||
}
|
||||
|
||||
unsafe fn wake(ptr: *const ()) {
|
||||
assert_eq!(ptr as usize, 5);
|
||||
}
|
||||
|
||||
unsafe fn wake_by_ref(ptr: *const ()) {
|
||||
assert_eq!(ptr as usize, 5);
|
||||
}
|
||||
|
||||
unsafe fn drop(ptr: *const ()) {
|
||||
assert_eq!(ptr as usize, 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user