Skip to content

Commit

Permalink
cli log for host file virtualizations
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford committed Jul 22, 2023
1 parent de11b93 commit c4ea1cc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
19 changes: 8 additions & 11 deletions src/bin/wasi-virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,7 @@ use wasi_virt::{create_virt, VirtOpts};
struct Args {
/// Virtualization TOML configuration
///
/// Example configuration:
///
/// [env]
/// host = "All" # or "None"
/// overrides = [["CUSTOM", "VAL"]]
///
/// Alternatively, allow or deny env keys for the host can be configured via:
///
/// [env.host]
/// Allow = ["ENV_KEY"] # Or Deny = ...
///
/// As defined in VirtOpts
#[arg(short, long, verbatim_doc_comment)]
config: String,

Expand All @@ -42,6 +32,13 @@ fn main() -> Result<()> {

let virt_component = create_virt(&virt_cfg)?;

if virt_component.virtual_files.len() > 0 {
println!("Virtualized files from local filesystem:\n");
for (virtual_path, original_path) in virt_component.virtual_files {
println!(" - {virtual_path} : {original_path}");
}
}

fs::write(args.out, virt_component.adapter)?;

Ok(())
Expand Down
12 changes: 7 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ mod virt_env;
mod virt_fs;
mod walrus_ops;

pub type VirtualFiles = virt_fs::VirtualFiles;

#[derive(Deserialize, Debug, Default, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct VirtOpts {
Expand All @@ -35,7 +37,7 @@ pub struct WasiVirt {

pub struct VirtResult {
pub adapter: Vec<u8>,
pub fs: Option<VirtFs>,
pub virtual_files: VirtualFiles,
}

impl WasiVirt {
Expand All @@ -60,11 +62,11 @@ pub fn create_virt<'a>(opts: &VirtOpts) -> Result<VirtResult> {
} else {
strip_env_virt(&mut module)?;
}
let fs = if let Some(fs) = &opts.fs {
Some(create_fs_virt(&mut module, fs)?)
let virtual_files = if let Some(fs) = &opts.fs {
create_fs_virt(&mut module, fs)?
} else {
strip_fs_virt(&mut module)?;
None
Default::default()
};

// decode the component custom section to strip out the unused world exports
Expand Down Expand Up @@ -138,7 +140,7 @@ pub fn create_virt<'a>(opts: &VirtOpts) -> Result<VirtResult> {

Ok(VirtResult {
adapter: encoded,
fs,
virtual_files,
})
}

Expand Down
2 changes: 2 additions & 0 deletions src/virt_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ use walrus::{
#[serde(deny_unknown_fields)]
pub struct VirtEnv {
/// Set specific environment variable overrides
#[serde(default)]
pub overrides: Vec<(String, String)>,
/// Define how to embed into the host environment
/// (Pass-through / encapsulate / allow / deny)
#[serde(default)]
pub host: HostEnv,
}

Expand Down
41 changes: 26 additions & 15 deletions src/virt_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
WasiVirt,
};

impl WasiVirt {}
pub type VirtualFiles = BTreeMap<String, String>;

#[derive(Deserialize, Debug, Default, Clone)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
Expand Down Expand Up @@ -154,21 +154,29 @@ impl fmt::Debug for StaticFileData {
}

impl FsEntry {
fn visit_pre_mut<'a>(&'a mut self, visit: fn(entry: &mut FsEntry) -> Result<()>) -> Result<()> {
visit(self)?;
self.visit_pre_mut_inner(visit)
fn visit_pre_mut<'a, Visitor>(&'a mut self, base_path: &str, visit: &mut Visitor) -> Result<()>
where
Visitor: FnMut(&mut FsEntry, &str, &str) -> Result<()>,
{
visit(self, base_path, "")?;
self.visit_pre_mut_inner(visit, base_path)
}

fn visit_pre_mut_inner<'a>(
fn visit_pre_mut_inner<'a, Visitor>(
&'a mut self,
visit: fn(entry: &mut FsEntry) -> Result<()>,
) -> Result<()> {
visit: &mut Visitor,
base_path: &str,
) -> Result<()>
where
Visitor: FnMut(&mut FsEntry, &str, &str) -> Result<()>,
{
if let FsEntry::Dir(dir) = self {
for sub_entry in dir.values_mut() {
visit(sub_entry)?;
for (name, sub_entry) in dir.iter_mut() {
visit(sub_entry, name, base_path)?;
}
for sub_entry in dir.values_mut() {
sub_entry.visit_pre_mut_inner(visit)?;
for (name, sub_entry) in dir.iter_mut() {
let path = format!("{base_path}{name}");
sub_entry.visit_pre_mut_inner(visit, &path)?;
}
}
Ok(())
Expand Down Expand Up @@ -203,12 +211,14 @@ impl FsEntry {
}
}

pub fn create_fs_virt<'a>(module: &'a mut Module, fs: &VirtFs) -> Result<VirtFs> {
pub fn create_fs_virt<'a>(module: &'a mut Module, fs: &VirtFs) -> Result<VirtualFiles> {
let mut virtual_files = BTreeMap::new();

// First we iterate the options and fill in all HostDir and HostFile entries
// With inline directory and file entries
let mut fs = fs.clone();
for entry in fs.preopens.values_mut() {
entry.visit_pre_mut(|entry| {
for (name, entry) in fs.preopens.iter_mut() {
entry.visit_pre_mut(name, &mut |entry, name, path| {
match entry {
FsEntry::Source(source) => {
*entry = FsEntry::File(source.as_bytes().to_vec())
Expand All @@ -225,6 +235,7 @@ pub fn create_fs_virt<'a>(module: &'a mut Module, fs: &VirtFs) -> Result<VirtFs>
let mut full_path = host_path.clone();
full_path.push('/');
full_path.push_str(file_name_str);
virtual_files.insert(format!("{path}{name}/{file_name_str}"), full_path.to_string());
entries.insert(file_name_str.into(), FsEntry::Virtualize(full_path));
}
*entry = FsEntry::Dir(entries);
Expand Down Expand Up @@ -385,7 +396,7 @@ pub fn create_fs_virt<'a>(module: &'a mut Module, fs: &VirtFs) -> Result<VirtFs>
data_section.finish(module)?;

// return the processed virtualized filesystem
Ok(fs)
Ok(virtual_files)
}

fn stub_fs_virt(module: &mut Module) -> Result<()> {
Expand Down
9 changes: 1 addition & 8 deletions tests/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,7 @@ async fn virt_test() -> Result<()> {
let virt_opts = test.virt_opts.clone().unwrap_or_default();
let virt_component = create_virt(&virt_opts)
.with_context(|| format!("Error creating virtual adapter for {:?}", test_case_path))?;
// if let Some(fs) = virt_component.fs {
// for (name, preopen) in fs.preopens {
// preopen.visit_pre(&name, &mut |entry, name, path, _rem| {
// eprintln!("FILE: {} : {}", name, path);
// Ok(())
// })?;
// }
// }

fs::write(&virt_component_path, virt_component.adapter)?;

// compose the test component with the defined test virtualization
Expand Down

0 comments on commit c4ea1cc

Please sign in to comment.