Skip to content

Commit

Permalink
Updated WasiRunner to allow mounting FileSystem instances as well…
Browse files Browse the repository at this point in the history
… as directories on the host system
  • Loading branch information
Michael-F-Bryan committed Jan 5, 2024
1 parent ad06d7e commit d37aa0d
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 65 deletions.
7 changes: 7 additions & 0 deletions lib/virtual-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,13 @@ impl FileType {
}
}

pub fn new_file() -> Self {
Self {
file: true,
..Default::default()
}
}

pub fn is_dir(&self) -> bool {
self.dir
}
Expand Down
16 changes: 4 additions & 12 deletions lib/wasix/src/runners/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ mod wasi_common;
#[cfg(feature = "webc_runner_rt_wcgi")]
pub mod wcgi;

pub use self::{runner::Runner, wasi_common::MappedCommand};

/// A directory that should be mapped from the host filesystem into a WASI
/// instance (the "guest").
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct MappedDirectory {
/// The absolute path for a directory on the host filesystem.
pub host: std::path::PathBuf,
/// The absolute path specifying where the host directory should be mounted
/// inside the guest.
pub guest: String,
}
pub use self::{
runner::Runner,
wasi_common::{MappedCommand, MappedDirectory, MountedDirectory},
};
23 changes: 17 additions & 6 deletions lib/wasix/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use std::{path::PathBuf, sync::Arc};

use anyhow::{Context, Error};
use tracing::Instrument;
use virtual_fs::{ArcBoxFile, TmpFileSystem, VirtualFile};
use virtual_fs::{ArcBoxFile, FileSystem, TmpFileSystem, VirtualFile};
use wasmer::Module;
use webc::metadata::{annotations::Wasi, Command};

use crate::{
bin_factory::BinaryPackage,
capabilities::Capabilities,
journal::{DynJournal, SnapshotTrigger},
runners::{wasi_common::CommonWasiOptions, MappedDirectory},
runners::{wasi_common::CommonWasiOptions, MappedDirectory, MountedDirectory},
runtime::{module_cache::ModuleHash, task_manager::VirtualTaskManagerExt},
Runtime, WasiEnvBuilder, WasiError, WasiRuntimeError,
};
Expand Down Expand Up @@ -98,14 +98,25 @@ impl WasiRunner {
self.wasi.forward_host_env = forward;
}

pub fn with_mapped_directories<I, D>(mut self, dirs: I) -> Self
pub fn with_mapped_directories<I, D>(self, dirs: I) -> Self
where
I: IntoIterator<Item = D>,
D: Into<MappedDirectory>,
{
self.wasi
.mapped_dirs
.extend(dirs.into_iter().map(|d| d.into()));
self.with_mounted_directories(dirs.into_iter().map(Into::into).map(MountedDirectory::from))
}

pub fn with_mounted_directories<I, D>(mut self, dirs: I) -> Self
where
I: IntoIterator<Item = D>,
D: Into<MountedDirectory>,
{
self.wasi.mounts.extend(dirs.into_iter().map(Into::into));
self
}

pub fn mount(&mut self, dest: String, fs: Arc<dyn FileSystem + Send + Sync>) -> &mut Self {
self.wasi.mounts.push(MountedDirectory { guest: dest, fs });
self
}

Expand Down
Loading

0 comments on commit d37aa0d

Please sign in to comment.