Skip to content

Commit d8d88f1

Browse files
authored
refactor(wasi): use WasiFilesystemCtx (#11394)
Signed-off-by: Roman Volosatovs <[email protected]>
1 parent e83da48 commit d8d88f1

File tree

7 files changed

+117
-100
lines changed

7 files changed

+117
-100
lines changed

crates/wasi/src/ctx.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::cli::{StdinStream, StdoutStream, WasiCliCtx};
22
use crate::clocks::{HostMonotonicClock, HostWallClock, WasiClocksCtx};
3+
use crate::filesystem::WasiFilesystemCtx;
34
use crate::p2::filesystem::Dir;
45
use crate::random::WasiRandomCtx;
56
use crate::sockets::{SocketAddrCheck, SocketAddrUse, WasiSocketsCtx};
@@ -38,10 +39,9 @@ use tokio::io::{stderr, stdin, stdout};
3839
pub struct WasiCtxBuilder {
3940
cli: WasiCliCtx,
4041
clocks: WasiClocksCtx,
42+
filesystem: WasiFilesystemCtx,
4143
random: WasiRandomCtx,
4244
sockets: WasiSocketsCtx,
43-
allow_blocking_current_thread: bool,
44-
preopens: Vec<(Dir, String)>,
4545
built: bool,
4646
}
4747

@@ -164,7 +164,7 @@ impl WasiCtxBuilder {
164164
///
165165
/// [`Config::async_support`]: https://docs.rs/wasmtime/latest/wasmtime/struct.Config.html#method.async_support
166166
pub fn allow_blocking_current_thread(&mut self, enable: bool) -> &mut Self {
167-
self.allow_blocking_current_thread = enable;
167+
self.filesystem.allow_blocking_current_thread = enable;
168168
self
169169
}
170170

@@ -305,13 +305,13 @@ impl WasiCtxBuilder {
305305
if dir_perms.contains(DirPerms::MUTATE) {
306306
open_mode |= OpenMode::WRITE;
307307
}
308-
self.preopens.push((
308+
self.filesystem.preopens.push((
309309
Dir::new(
310310
dir,
311311
dir_perms,
312312
file_perms,
313313
open_mode,
314-
self.allow_blocking_current_thread,
314+
self.filesystem.allow_blocking_current_thread,
315315
),
316316
guest_path.as_ref().to_owned(),
317317
));
@@ -435,21 +435,19 @@ impl WasiCtxBuilder {
435435
let Self {
436436
cli,
437437
clocks,
438+
filesystem,
438439
random,
439440
sockets,
440-
allow_blocking_current_thread,
441-
preopens,
442441
built: _,
443442
} = mem::replace(self, Self::new());
444443
self.built = true;
445444

446445
WasiCtx {
447446
cli,
448447
clocks,
449-
sockets,
448+
filesystem,
450449
random,
451-
allow_blocking_current_thread,
452-
preopens,
450+
sockets,
453451
}
454452
}
455453
/// Builds a WASIp1 context instead of a [`WasiCtx`].
@@ -519,12 +517,11 @@ impl WasiCtxBuilder {
519517
/// ```
520518
#[derive(Default)]
521519
pub struct WasiCtx {
522-
pub(crate) random: WasiRandomCtx,
523-
pub(crate) clocks: WasiClocksCtx,
524520
pub(crate) cli: WasiCliCtx,
521+
pub(crate) clocks: WasiClocksCtx,
522+
pub(crate) filesystem: WasiFilesystemCtx,
523+
pub(crate) random: WasiRandomCtx,
525524
pub(crate) sockets: WasiSocketsCtx,
526-
pub(crate) allow_blocking_current_thread: bool,
527-
pub(crate) preopens: Vec<(Dir, String)>,
528525
}
529526

530527
impl WasiCtx {

crates/wasi/src/filesystem.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
use crate::p2::filesystem::Dir;
2+
use wasmtime::component::{HasData, ResourceTable};
3+
4+
pub(crate) struct WasiFilesystem;
5+
6+
impl HasData for WasiFilesystem {
7+
type Data<'a> = WasiFilesystemCtxView<'a>;
8+
}
9+
10+
#[derive(Clone, Default)]
11+
pub struct WasiFilesystemCtx {
12+
pub allow_blocking_current_thread: bool,
13+
pub preopens: Vec<(Dir, String)>,
14+
}
15+
16+
pub struct WasiFilesystemCtxView<'a> {
17+
pub ctx: &'a mut WasiFilesystemCtx,
18+
pub table: &'a mut ResourceTable,
19+
}
20+
21+
pub trait WasiFilesystemView: Send {
22+
fn filesystem(&mut self) -> WasiFilesystemCtxView<'_>;
23+
}
24+
125
bitflags::bitflags! {
226
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
327
pub struct FilePerms: usize {

0 commit comments

Comments
 (0)