Skip to content

Commit

Permalink
remove need for virt_preopens
Browse files Browse the repository at this point in the history
  • Loading branch information
iximeow committed Dec 19, 2019
1 parent 802d3be commit aa3b8ee
Showing 1 changed file with 18 additions and 38 deletions.
56 changes: 18 additions & 38 deletions crates/wasi-common/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ impl PendingCString {
/// A builder allowing customizable construction of `WasiCtx` instances.
pub struct WasiCtxBuilder {
fds: HashMap<wasi::__wasi_fd_t, PendingFdEntry>,
preopens: Vec<(PathBuf, File)>,
virt_preopens: Vec<(PathBuf, Box<dyn VirtualFile>)>,
preopens: Vec<(PathBuf, fdentry::File)>,
args: Vec<PendingCString>,
env: HashMap<PendingCString, PendingCString>,
}
Expand All @@ -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(),
};
Expand Down Expand Up @@ -186,20 +184,20 @@ impl WasiCtxBuilder {
self
}

/// Add a preopened directory.
/// Add a preopened OS directory.
pub fn preopened_dir<P: AsRef<Path>>(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<P: AsRef<Path>>(
mut self,
dir: Box<dyn VirtualFile>,
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
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit aa3b8ee

Please sign in to comment.