feat: provide default methods for WasiDir (#5019)

When implementing custom WasiDir instances, there is a lot of
boilerplate.  These default methods should reduce code for implementors
who want to provide only a subset of functionality.

Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>

Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
This commit is contained in:
Nathaniel McCallum
2022-10-05 11:58:24 -04:00
committed by GitHub
parent a9be4a9b56
commit bbdafaf5ce

View File

@@ -7,48 +7,94 @@ use std::path::PathBuf;
#[wiggle::async_trait] #[wiggle::async_trait]
pub trait WasiDir: Send + Sync { pub trait WasiDir: Send + Sync {
fn as_any(&self) -> &dyn Any; fn as_any(&self) -> &dyn Any;
async fn open_file( async fn open_file(
&self, &self,
symlink_follow: bool, _symlink_follow: bool,
path: &str, _path: &str,
oflags: OFlags, _oflags: OFlags,
read: bool, _read: bool,
write: bool, _write: bool,
fdflags: FdFlags, _fdflags: FdFlags,
) -> Result<Box<dyn WasiFile>, Error>; ) -> Result<Box<dyn WasiFile>, Error> {
async fn open_dir(&self, symlink_follow: bool, path: &str) -> Result<Box<dyn WasiDir>, Error>; Err(Error::not_supported())
async fn create_dir(&self, path: &str) -> Result<(), Error>; }
async fn open_dir(
&self,
_symlink_follow: bool,
_path: &str,
) -> Result<Box<dyn WasiDir>, Error> {
Err(Error::not_supported())
}
async fn create_dir(&self, _path: &str) -> Result<(), Error> {
Err(Error::not_supported())
}
// XXX the iterator here needs to be asyncified as well! // XXX the iterator here needs to be asyncified as well!
async fn readdir( async fn readdir(
&self, &self,
cursor: ReaddirCursor, _cursor: ReaddirCursor,
) -> Result<Box<dyn Iterator<Item = Result<ReaddirEntity, Error>> + Send>, Error>; ) -> Result<Box<dyn Iterator<Item = Result<ReaddirEntity, Error>> + Send>, Error> {
async fn symlink(&self, old_path: &str, new_path: &str) -> Result<(), Error>; Err(Error::not_supported())
async fn remove_dir(&self, path: &str) -> Result<(), Error>; }
async fn unlink_file(&self, path: &str) -> Result<(), Error>;
async fn read_link(&self, path: &str) -> Result<PathBuf, Error>; async fn symlink(&self, _old_path: &str, _new_path: &str) -> Result<(), Error> {
async fn get_filestat(&self) -> Result<Filestat, Error>; Err(Error::not_supported())
async fn get_path_filestat(&self, path: &str, follow_symlinks: bool) }
-> Result<Filestat, Error>;
async fn remove_dir(&self, _path: &str) -> Result<(), Error> {
Err(Error::not_supported())
}
async fn unlink_file(&self, _path: &str) -> Result<(), Error> {
Err(Error::not_supported())
}
async fn read_link(&self, _path: &str) -> Result<PathBuf, Error> {
Err(Error::not_supported())
}
async fn get_filestat(&self) -> Result<Filestat, Error> {
Err(Error::not_supported())
}
async fn get_path_filestat(
&self,
_path: &str,
_follow_symlinks: bool,
) -> Result<Filestat, Error> {
Err(Error::not_supported())
}
async fn rename( async fn rename(
&self, &self,
path: &str, _path: &str,
dest_dir: &dyn WasiDir, _dest_dir: &dyn WasiDir,
dest_path: &str, _dest_path: &str,
) -> Result<(), Error>; ) -> Result<(), Error> {
Err(Error::not_supported())
}
async fn hard_link( async fn hard_link(
&self, &self,
path: &str, _path: &str,
target_dir: &dyn WasiDir, _target_dir: &dyn WasiDir,
target_path: &str, _target_path: &str,
) -> Result<(), Error>; ) -> Result<(), Error> {
Err(Error::not_supported())
}
async fn set_times( async fn set_times(
&self, &self,
path: &str, _path: &str,
atime: Option<SystemTimeSpec>, _atime: Option<SystemTimeSpec>,
mtime: Option<SystemTimeSpec>, _mtime: Option<SystemTimeSpec>,
follow_symlinks: bool, _follow_symlinks: bool,
) -> Result<(), Error>; ) -> Result<(), Error> {
Err(Error::not_supported())
}
} }
pub(crate) struct DirEntry { pub(crate) struct DirEntry {