Skip to content

Commit

Permalink
add basic VFS bootstraping
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Jul 15, 2024
1 parent 2ffeaef commit a48b9e4
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 12 deletions.
2 changes: 2 additions & 0 deletions hermes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ tokio = "1.36.0"
libsqlite3-sys = "0.28.0"
stringzilla = "3.8.4"
serial_test = { version = "3.1.1", features = ["file_locks"] }
temp-dir = "0.1.13"
hdf5 = { git="https://github.com/aldanor/hdf5-rust.git", rev="694e900972fbf5ffbdd1a2294f57a2cc3a91c994", version="0.8.1", features = [ "static", "blosc" ]}
# needs to enable blosc compression functionality for hdf5 crate
blosc-src = { version = "0.3.0", features = ["lz4", "zlib", "zstd"] }
Expand All @@ -108,3 +109,4 @@ libipld = "0.16.0"
libp2p = "0.53.2"
rust-ipfs = "0.11.19"
rustyline-async = "0.4.2"
dirs = "5.0.1"
3 changes: 2 additions & 1 deletion hermes/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ console = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
hyper = { version = "0.14.10", features = ["full"] }
dirs = { workspace = true }

jsonschema = { workspace = true }
hex = { workspace = true }
Expand All @@ -79,7 +80,7 @@ build-info-build = { workspace = true }
[dev-dependencies]
tracing-subscriber = { workspace = true }
serial_test = { workspace = true }
temp-dir = "0.1.13"
temp-dir = { workspace = true }

[package.metadata.cargo-machete]
ignored = ["blosc-src"]
27 changes: 17 additions & 10 deletions hermes/bin/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
use std::collections::HashMap;

use crate::wasm::module::{Module, ModuleId};
use crate::{
vfs::Vfs,
wasm::module::{Module, ModuleId},
};

/// Hermes App Name type
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand All @@ -24,21 +27,25 @@ pub(crate) struct HermesApp {

/// WASM modules
indexed_modules: HashMap<ModuleId, Module>,

/// App `Vfs` instance
#[allow(dead_code)]
vfs: Vfs,
}

impl HermesApp {
/// Create a new Hermes app
#[allow(dead_code)]
pub(crate) fn new(app_name: HermesAppName, module_bytes: Vec<Vec<u8>>) -> anyhow::Result<Self> {
let mut modules = HashMap::with_capacity(module_bytes.len());
for module_bytes in module_bytes {
let module = Module::from_bytes(&module_bytes)?;
modules.insert(module.id().clone(), module);
}
Ok(Self {
pub(crate) fn new(app_name: HermesAppName, vfs: Vfs, modules: Vec<Module>) -> Self {
let indexed_modules = modules
.into_iter()
.map(|module| (module.id().clone(), module))
.collect();
Self {
app_name,
indexed_modules: modules,
})
indexed_modules,
vfs,
}
}

/// Get app name
Expand Down
7 changes: 7 additions & 0 deletions hermes/bin/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ enum Commands {
}

impl Cli {
/// Hermes home directory
pub(crate) fn hermes_home() -> PathBuf {
dirs::home_dir()
.unwrap_or("/var/lib".into())
.join(".hermes")
}

/// Execute cli commands of the hermes
pub(crate) fn exec(self) {
println!("{}{}", Emoji::new("ℹ️", ""), style(BUILD_INFO).yellow());
Expand Down
15 changes: 14 additions & 1 deletion hermes/bin/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
use std::path::PathBuf;

use console::Emoji;

use crate::{
app::{HermesApp, HermesAppName},
cli::Cli,
packaging::{
app::ApplicationPackage,
sign::certificate::{self, Certificate},
},
reactor::HermesReactor,
vfs::Vfs,
};

/// Run cli command
Expand All @@ -26,7 +31,15 @@ impl Run {
let package = ApplicationPackage::from_file(app_package)?;
package.validate(unstrusted)?;

let mut reactor = HermesReactor::new(vec![])?;
let app_name = package.get_metadata()?.get_name()?;

println!("{} Bootstrapping virtual filesystem", Emoji::new("🗄️", ""));
let vfs = Vfs::bootstrap(Cli::hermes_home(), app_name.as_str())?;

println!("{} Running application {app_name} ", Emoji::new("🚀", ""),);
let app = HermesApp::new(HermesAppName(app_name), vfs, vec![]);

let mut reactor = HermesReactor::new(vec![app])?;
reactor.wait()?;

Ok(())
Expand Down
1 change: 1 addition & 0 deletions hermes/bin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod reactor;
pub mod runtime_context;
pub mod runtime_extensions;
pub mod wasm;
pub mod vfs;

#[cfg(feature = "bench")]
pub use wasm::module::bench::{
Expand Down
1 change: 1 addition & 0 deletions hermes/bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod packaging;
mod reactor;
mod runtime_context;
mod runtime_extensions;
mod vfs;
mod wasm;

#[cfg(feature = "bench")]
Expand Down
39 changes: 39 additions & 0 deletions hermes/bin/src/vfs/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Hermes virtual file system.
use hdf5 as hdf5_lib;

use crate::hdf5 as hermes_hdf5;

/// Hermes virtual file system type.
#[allow(dead_code)]
pub(crate) struct Vfs {
/// HDFR5 root directory of the virtual file system.
root: hermes_hdf5::Dir,
}

impl Vfs {
/// Hermes virtual file system file extension.
const FILE_EXTENSION: &'static str = "hfs";

/// Bootstrap virtual file system and return a `Vfs` instance.
/// `fs_file_path` is the path to the `Vfs` file's directory.
/// `fs_file_name` is the name of the `Vfs` file.
#[allow(dead_code)]
pub(crate) fn bootstrap<P: AsRef<std::path::Path>>(
fs_file_path: P, fs_file_name: &str,
) -> anyhow::Result<Self> {
let fs_file_path = fs_file_path.as_ref().join(fs_file_name);
fs_file_path.with_extension(Self::FILE_EXTENSION);

let hdf5_file = if let Ok(hdf5_file) = hdf5_lib::File::open_rw(&fs_file_path) {
hdf5_file
} else {
hdf5_lib::File::create(fs_file_path).map_err(|_| {
anyhow::anyhow!("Failed to create Hermes virtual file system instance at.")
})?
};
let root = hermes_hdf5::Dir::new(hdf5_file.as_group()?);

Ok(Self { root })
}
}

0 comments on commit a48b9e4

Please sign in to comment.