Skip to content

Commit

Permalink
Use external knowledge when preopening dirs instead of traversing the fs
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Mar 16, 2023
1 parent 41503d0 commit 84c24bf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
14 changes: 11 additions & 3 deletions lib/wasi/src/runners/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,18 @@ impl WapmContainer {

/// Get the entire container as a single filesystem and a list of suggested
/// directories to preopen.
pub(crate) fn container_fs(&self) -> Box<dyn FileSystem + Send + Sync> {
pub(crate) fn container_fs(&self) -> (Box<dyn FileSystem + Send + Sync>, Vec<String>) {
match &self.repr {
Repr::V1Mmap(mapped) => Box::new(WebcFileSystem::init_all(Arc::clone(mapped))),
Repr::V1Owned(owned) => Box::new(WebcFileSystem::init_all(Arc::clone(owned))),
Repr::V1Mmap(mapped) => {
let fs = WebcFileSystem::init_all(Arc::clone(mapped));
let top_level_dirs = fs.top_level_dirs().clone();
(Box::new(fs), top_level_dirs)
}
Repr::V1Owned(owned) => {
let fs = WebcFileSystem::init_all(Arc::clone(owned));
let top_level_dirs = fs.top_level_dirs().clone();
(Box::new(fs), top_level_dirs)
}
}
}
}
Expand Down
38 changes: 17 additions & 21 deletions lib/wasi/src/runners/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,13 @@ impl WasiRunner {
container: &WapmContainer,
command: &str,
) -> Result<WasiEnvBuilder, anyhow::Error> {
let fs = unioned_filesystem(&self.mapped_dirs, container)?;
let fs = container.container_fs();
let (fs, preopen_dirs) = unioned_filesystem(&self.mapped_dirs, container)?;

let mut builder = WasiEnv::builder(command)
.args(&self.args)
.preopen_dir("/")?;
let mut builder = WasiEnv::builder(command).args(&self.args);

preopen(&fs, "/".as_ref(), &mut builder)?;
for dir in preopen_dirs {
builder.add_preopen_dir(dir)?;
}

builder.set_fs(Box::new(fs));

Expand Down Expand Up @@ -199,10 +198,12 @@ impl crate::runners::Runner for WasiRunner {
pub(crate) fn unioned_filesystem(
mapped_dirs: &[MappedDirectory],
container: &WapmContainer,
) -> Result<impl FileSystem, Error> {
) -> Result<(impl FileSystem, Vec<PathBuf>), Error> {
// We start with the root filesystem so we get things like "/dev/"
let primary = RootFileSystemBuilder::new().build();

let mut preopen_dirs = Vec::new();

// Now, let's merge in the host volumes.
if !mapped_dirs.is_empty() {
let host_fs: Arc<dyn FileSystem + Send + Sync> =
Expand Down Expand Up @@ -232,25 +233,20 @@ pub(crate) fn unioned_filesystem(
guest.display()
)
})?;

preopen_dirs.push(guest);
}
}

// Once we've set up the primary filesystem, make sure it is overlayed with
// the WEBC container's files
Ok(OverlayFileSystem::new(primary, [container.container_fs()]))
}
let (container_fs, top_level_dirs) = container.container_fs();

fn preopen(fs: &dyn FileSystem, path: &Path, builder: &mut WasiEnvBuilder) -> Result<(), Error> {
for result in fs.read_dir(path)? {
let entry = result?;
preopen_dirs.extend(top_level_dirs.into_iter().map(|p| Path::new("/").join(p)));

if entry.file_type()?.is_dir() {
builder.add_preopen_dir(&entry.path)?;
preopen(fs, &entry.path, builder)?;
}
}
// Once we've set up the primary filesystem, make sure it is overlayed with
// the WEBC container's files
let fs = OverlayFileSystem::new(primary, [container_fs]);

Ok(())
Ok((fs, preopen_dirs))
}

fn create_dir_all(fs: &(impl FileSystem + ?Sized), path: &Path) -> Result<(), Error> {
Expand Down Expand Up @@ -307,7 +303,7 @@ mod tests {
host: temp.path().to_path_buf(),
}];

let fs = unioned_filesystem(&mapped_dirs, &container).unwrap();
let (fs, _) = unioned_filesystem(&mapped_dirs, &container).unwrap();

// Files that were mounted on the host
let path_contents = read_dir(&fs, "/path/to/");
Expand Down
14 changes: 8 additions & 6 deletions lib/wasi/src/runners/wcgi/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
task::Poll,
};

use anyhow::{Context, Error};
use anyhow::Error;
use futures::{Future, FutureExt, StreamExt, TryFutureExt};
use http::{Request, Response};
use hyper::{service::Service, Body};
Expand All @@ -17,7 +17,7 @@ use tokio::{
};
use tracing::Instrument;
use wasmer::Module;
use wasmer_vfs::{FileSystem, PassthruFileSystem, RootFileSystemBuilder, TmpFileSystem};
use wasmer_vfs::FileSystem;
use wcgi_host::CgiDialect;

use crate::{
Expand Down Expand Up @@ -54,6 +54,8 @@ impl Handler {
self.dialect
.prepare_environment_variables(parts, &mut request_specific_env);

let (fs, preopen_dirs) = self.fs()?;

let rt = PluggableRuntimeImplementation::new(Arc::clone(&self.task_manager));
let mut builder = builder
.envs(self.env.iter())
Expand All @@ -68,11 +70,11 @@ impl Handler {
threading: Default::default(),
})
.runtime(Arc::new(rt))
.fs(Box::new(self.fs()?))
.fs(Box::new(fs))
.preopen_dir(Path::new("/"))?;

for dir in &self.mapped_dirs {
builder.add_preopen_dir(&dir.guest)?;
for dir in preopen_dirs {
builder.add_preopen_dir(dir)?;
}

let module = self.module.clone();
Expand Down Expand Up @@ -134,7 +136,7 @@ impl Handler {
Ok(response)
}

fn fs(&self) -> Result<impl FileSystem, Error> {
fn fs(&self) -> Result<(impl FileSystem, Vec<PathBuf>), Error> {
crate::runners::wasi::unioned_filesystem(&self.mapped_dirs, &self.container)
}
}
Expand Down
5 changes: 0 additions & 5 deletions lib/wasi/src/runners/wcgi/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use tower::{make::Shared, ServiceBuilder};
use tower_http::{catch_panic::CatchPanicLayer, cors::CorsLayer, trace::TraceLayer};
use tracing::Span;
use wasmer::{Engine, Module, Store};
use wasmer_vfs::FileSystem;
use wcgi_host::CgiDialect;
use webc::metadata::{
annotations::{Wasi, Wcgi},
Expand Down Expand Up @@ -226,10 +225,6 @@ impl RunnerContext<'_> {
&self.store
}

fn container_fs(&self) -> Box<dyn FileSystem + Send + Sync> {
self.container.container_fs()
}

fn get_atom(&self, name: &str) -> Option<&[u8]> {
self.container.get_atom(name)
}
Expand Down

0 comments on commit 84c24bf

Please sign in to comment.