Skip to content

Commit 3905a60

Browse files
authored
Allow calling get-directories more than once (#2)
For now `Clone` the directories into new descriptor slots as needed.
1 parent e171625 commit 3905a60

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

crates/wasi/src/preview2/ctx.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use super::clocks::host::{monotonic_clock, wall_clock};
22
use crate::preview2::{
33
bindings::cli::{terminal_input, terminal_output},
4-
bindings::filesystem::types::Descriptor,
54
bindings::io::streams,
65
clocks::{self, HostMonotonicClock, HostWallClock},
7-
filesystem::{Dir, TableFsExt},
6+
filesystem::Dir,
87
pipe, random, stdio,
98
stdio::{HostTerminalInputState, HostTerminalOutputState},
109
stream::{HostInputStream, HostOutputStream, TableStreamExt},
@@ -317,16 +316,6 @@ impl WasiCtxBuilder {
317316
let stdout = Some(table.push_output_stream(stdout.0).context("stdout")?);
318317
let stderr = Some(table.push_output_stream(stderr.0).context("stderr")?);
319318

320-
let preopens = preopens
321-
.into_iter()
322-
.map(|(dir, path)| {
323-
let dirfd = table
324-
.push_dir(dir)
325-
.with_context(|| format!("preopen {path:?}"))?;
326-
Ok((dirfd, path))
327-
})
328-
.collect::<anyhow::Result<Vec<_>>>()?;
329-
330319
Ok(WasiCtx {
331320
stdin,
332321
stdin_terminal,
@@ -362,7 +351,7 @@ pub struct WasiCtx {
362351
pub(crate) monotonic_clock: Box<dyn HostMonotonicClock + Send + Sync>,
363352
pub(crate) env: Vec<(String, String)>,
364353
pub(crate) args: Vec<String>,
365-
pub(crate) preopens: Vec<(Resource<Descriptor>, String)>,
354+
pub(crate) preopens: Vec<(Dir, String)>,
366355
pub(crate) stdin: Option<Resource<streams::InputStream>>,
367356
pub(crate) stdout: Option<Resource<streams::OutputStream>>,
368357
pub(crate) stderr: Option<Resource<streams::OutputStream>>,

crates/wasi/src/preview2/filesystem.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ bitflags::bitflags! {
9393
}
9494
}
9595

96+
#[derive(Clone)]
9697
pub(crate) struct Dir {
9798
pub dir: Arc<cap_std::fs::Dir>,
9899
pub perms: DirPerms,

crates/wasi/src/preview2/host/filesystem.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::preview2::bindings::filesystem::{preopens, types};
66
use crate::preview2::bindings::io::streams;
77
use crate::preview2::filesystem::{Dir, File, TableFsExt};
88
use crate::preview2::{DirPerms, FilePerms, Table, TableError, WasiView};
9+
use anyhow::Context;
910
use wasmtime::component::Resource;
1011

1112
use types::ErrorCode;
@@ -22,12 +23,15 @@ impl<T: WasiView> preopens::Host for T {
2223
fn get_directories(
2324
&mut self,
2425
) -> Result<Vec<(Resource<types::Descriptor>, String)>, anyhow::Error> {
25-
Ok(self
26-
.ctx_mut()
27-
.preopens
28-
.drain(..)
29-
.map(|(fd, name)| (Resource::new_own(fd.rep()), name.clone()))
30-
.collect())
26+
let mut results = Vec::new();
27+
for (dir, name) in self.ctx().preopens.clone() {
28+
let fd = self
29+
.table_mut()
30+
.push_dir(dir)
31+
.with_context(|| format!("failed to push preopen {name}"))?;
32+
results.push((fd, name));
33+
}
34+
Ok(results)
3135
}
3236
}
3337

0 commit comments

Comments
 (0)