diff --git a/crates/wasi-common/src/ctx.rs b/crates/wasi-common/src/ctx.rs index a7ff5886de62..b648dc48cdb1 100644 --- a/crates/wasi-common/src/ctx.rs +++ b/crates/wasi-common/src/ctx.rs @@ -63,8 +63,7 @@ impl PendingCString { /// A builder allowing customizable construction of `WasiCtx` instances. pub struct WasiCtxBuilder { fds: HashMap, - preopens: Vec<(PathBuf, File)>, - virt_preopens: Vec<(PathBuf, Box)>, + preopens: Vec<(PathBuf, fdentry::File)>, args: Vec, env: HashMap, } @@ -75,7 +74,6 @@ impl WasiCtxBuilder { let mut builder = Self { fds: HashMap::new(), preopens: Vec::new(), - virt_preopens: Vec::new(), args: vec![], env: HashMap::new(), }; @@ -186,20 +184,20 @@ impl WasiCtxBuilder { self } - /// Add a preopened directory. + /// Add a preopened OS directory. pub fn preopened_dir>(mut self, dir: File, guest_path: P) -> Self { - self.preopens.push((guest_path.as_ref().to_owned(), dir)); + self.preopens.push((guest_path.as_ref().to_owned(), fdentry::File::OsHandle(OsHandle::from(dir)))); self } - /// Add a thunk that will prooduce some FdEntry. + /// Add a preopened virtual directory. pub fn preopened_virt>( mut self, dir: Box, guest_path: P, ) -> Self { - self.virt_preopens - .push((guest_path.as_ref().to_owned(), dir)); + self.preopens + .push((guest_path.as_ref().to_owned(), fdentry::File::VirtualFile(dir))); self } @@ -257,43 +255,25 @@ impl WasiCtxBuilder { // unnecessarily if we have exactly the maximum number of file descriptors. preopen_fd = preopen_fd.checked_add(1).ok_or(Error::ENFILE)?; - if !dir.metadata()?.is_dir() { - return Err(Error::EBADF); - } - - // We don't currently allow setting file descriptors other than 0-2, but this will avoid - // collisions if we restore that functionality in the future. - while fds.contains_key(&preopen_fd) { - preopen_fd = preopen_fd.checked_add(1).ok_or(Error::ENFILE)?; - } - let mut fe = FdEntry::from(fdentry::File::OsHandle(OsHandle::from(dir)))?; - fe.preopen_path = Some(guest_path); - log::debug!("WasiCtx inserting ({:?}, {:?})", preopen_fd, fe); - fds.insert(preopen_fd, fe); - log::debug!("WasiCtx fds = {:?}", fds); - } - - for (guest_path, virt_dir) in self.virt_preopens { - // We do the increment at the beginning of the loop body, so that we don't overflow - // unnecessarily if we have exactly the maximum number of file descriptors. - preopen_fd = preopen_fd.checked_add(1).ok_or(Error::ENFILE)?; - - /* - if !dir.metadata()?.is_dir() { - return Err(Error::EBADF); + match &dir { + fdentry::File::OsHandle(handle) => { + if !handle.metadata()?.is_dir() { + return Err(Error::EBADF); + } + }, + fdentry::File::VirtualFile(virt) => { + if virt.get_file_type() != wasi::__WASI_FILETYPE_DIRECTORY { + return Err(Error::EBADF); + } + } } - */ // We don't currently allow setting file descriptors other than 0-2, but this will avoid // collisions if we restore that functionality in the future. while fds.contains_key(&preopen_fd) { preopen_fd = preopen_fd.checked_add(1).ok_or(Error::ENFILE)?; } - let mut fe = FdEntry::virtual_from(virt_dir)?; - fe.file_type = wasi::__WASI_FILETYPE_DIRECTORY; - fe.rights_base = wasi::RIGHTS_DIRECTORY_BASE; - fe.rights_inheriting = wasi::RIGHTS_DIRECTORY_INHERITING; - fe.file_type = wasi::__WASI_FILETYPE_DIRECTORY; + let mut fe = FdEntry::from(dir)?; fe.preopen_path = Some(guest_path); log::debug!("WasiCtx inserting ({:?}, {:?})", preopen_fd, fe); fds.insert(preopen_fd, fe);