Skip to content

Commit

Permalink
Allow calling get-directories more than once (#2)
Browse files Browse the repository at this point in the history
For now `Clone` the directories into new descriptor slots as needed.
  • Loading branch information
alexcrichton authored and sunfishcode committed Sep 28, 2023
1 parent ad62c20 commit 3f06964
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
15 changes: 2 additions & 13 deletions crates/wasi/src/preview2/ctx.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use super::clocks::host::{monotonic_clock, wall_clock};
use crate::preview2::{
bindings::cli::{terminal_input, terminal_output},
bindings::filesystem::types::Descriptor,
bindings::io::streams,
clocks::{self, HostMonotonicClock, HostWallClock},
filesystem::{Dir, TableFsExt},
filesystem::Dir,
pipe, random, stdio,
stdio::{HostTerminalInputState, HostTerminalOutputState},
stream::{HostInputStream, HostOutputStream, TableStreamExt},
Expand Down Expand Up @@ -320,16 +319,6 @@ impl WasiCtxBuilder {
let stdout = Some(table.push_output_stream(stdout.0).context("stdout")?);
let stderr = Some(table.push_output_stream(stderr.0).context("stderr")?);

let preopens = preopens
.into_iter()
.map(|(dir, path)| {
let dirfd = table
.push_dir(dir)
.with_context(|| format!("preopen {path:?}"))?;
Ok((dirfd, path))
})
.collect::<anyhow::Result<Vec<_>>>()?;

Ok(WasiCtx {
stdin,
stdin_terminal,
Expand Down Expand Up @@ -365,7 +354,7 @@ pub struct WasiCtx {
pub(crate) monotonic_clock: Box<dyn HostMonotonicClock + Send + Sync>,
pub(crate) env: Vec<(String, String)>,
pub(crate) args: Vec<String>,
pub(crate) preopens: Vec<(Resource<Descriptor>, String)>,
pub(crate) preopens: Vec<(Dir, String)>,
pub(crate) stdin: Option<Resource<streams::InputStream>>,
pub(crate) stdout: Option<Resource<streams::OutputStream>>,
pub(crate) stderr: Option<Resource<streams::OutputStream>>,
Expand Down
1 change: 1 addition & 0 deletions crates/wasi/src/preview2/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ bitflags::bitflags! {
}
}

#[derive(Clone)]
pub(crate) struct Dir {
pub dir: Arc<cap_std::fs::Dir>,
pub perms: DirPerms,
Expand Down
16 changes: 10 additions & 6 deletions crates/wasi/src/preview2/host/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::preview2::bindings::filesystem::{preopens, types};
use crate::preview2::bindings::io::streams;
use crate::preview2::filesystem::{Dir, File, TableFsExt};
use crate::preview2::{DirPerms, FilePerms, Table, TableError, WasiView};
use anyhow::Context;
use wasmtime::component::Resource;

use types::ErrorCode;
Expand All @@ -22,12 +23,15 @@ impl<T: WasiView> preopens::Host for T {
fn get_directories(
&mut self,
) -> Result<Vec<(Resource<types::Descriptor>, String)>, anyhow::Error> {
Ok(self
.ctx_mut()
.preopens
.drain(..)
.map(|(fd, name)| (Resource::new_own(fd.rep()), name.clone()))
.collect())
let mut results = Vec::new();
for (dir, name) in self.ctx().preopens.clone() {
let fd = self
.table_mut()
.push_dir(dir)
.with_context(|| format!("failed to push preopen {name}"))?;
results.push((fd, name));
}
Ok(results)
}
}

Expand Down

0 comments on commit 3f06964

Please sign in to comment.