Path: blob/main/crates/wasi-common/src/dir.rs
1691 views
use crate::file::{FdFlags, FileType, Filestat, OFlags, WasiFile};1use crate::{Error, ErrorExt, SystemTimeSpec};2use std::any::Any;3use std::path::PathBuf;4use std::sync::Arc;56pub enum OpenResult {7File(Box<dyn WasiFile>),8Dir(Box<dyn WasiDir>),9}1011#[wiggle::async_trait]12pub trait WasiDir: Send + Sync {13fn as_any(&self) -> &dyn Any;1415async fn open_file(16&self,17_symlink_follow: bool,18_path: &str,19_oflags: OFlags,20_read: bool,21_write: bool,22_fdflags: FdFlags,23) -> Result<OpenResult, Error> {24Err(Error::not_supported())25}2627async fn create_dir(&self, _path: &str) -> Result<(), Error> {28Err(Error::not_supported())29}3031// XXX the iterator here needs to be asyncified as well!32async fn readdir(33&self,34_cursor: ReaddirCursor,35) -> Result<Box<dyn Iterator<Item = Result<ReaddirEntity, Error>> + Send>, Error> {36Err(Error::not_supported())37}3839async fn symlink(&self, _old_path: &str, _new_path: &str) -> Result<(), Error> {40Err(Error::not_supported())41}4243async fn remove_dir(&self, _path: &str) -> Result<(), Error> {44Err(Error::not_supported())45}4647async fn unlink_file(&self, _path: &str) -> Result<(), Error> {48Err(Error::not_supported())49}5051async fn read_link(&self, _path: &str) -> Result<PathBuf, Error> {52Err(Error::not_supported())53}5455async fn get_filestat(&self) -> Result<Filestat, Error> {56Err(Error::not_supported())57}5859async fn get_path_filestat(60&self,61_path: &str,62_follow_symlinks: bool,63) -> Result<Filestat, Error> {64Err(Error::not_supported())65}6667async fn rename(68&self,69_path: &str,70_dest_dir: &dyn WasiDir,71_dest_path: &str,72) -> Result<(), Error> {73Err(Error::not_supported())74}7576async fn hard_link(77&self,78_path: &str,79_target_dir: &dyn WasiDir,80_target_path: &str,81) -> Result<(), Error> {82Err(Error::not_supported())83}8485async fn set_times(86&self,87_path: &str,88_atime: Option<SystemTimeSpec>,89_mtime: Option<SystemTimeSpec>,90_follow_symlinks: bool,91) -> Result<(), Error> {92Err(Error::not_supported())93}94}9596pub(crate) struct DirEntry {97preopen_path: Option<PathBuf>, // precondition: PathBuf is valid unicode98pub dir: Box<dyn WasiDir>,99}100101impl DirEntry {102pub fn new(preopen_path: Option<PathBuf>, dir: Box<dyn WasiDir>) -> Self {103DirEntry { preopen_path, dir }104}105pub fn preopen_path(&self) -> &Option<PathBuf> {106&self.preopen_path107}108}109110pub(crate) trait TableDirExt {111fn get_dir(&self, fd: u32) -> Result<Arc<DirEntry>, Error>;112}113114impl TableDirExt for crate::table::Table {115fn get_dir(&self, fd: u32) -> Result<Arc<DirEntry>, Error> {116self.get(fd)117}118}119120#[derive(Debug, Clone)]121pub struct ReaddirEntity {122pub next: ReaddirCursor,123pub inode: u64,124pub name: String,125pub filetype: FileType,126}127128#[derive(Debug, Copy, Clone)]129pub struct ReaddirCursor(u64);130impl From<u64> for ReaddirCursor {131fn from(c: u64) -> ReaddirCursor {132ReaddirCursor(c)133}134}135impl From<ReaddirCursor> for u64 {136fn from(c: ReaddirCursor) -> u64 {137c.0138}139}140141142