From 601362a1095d23aab2d271f4a7252d8377b44e82 Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Sun, 20 Oct 2024 12:36:14 +0200 Subject: [PATCH] added fetch_module --- Cargo.toml | 2 +- src/core/instance/mod.rs | 6 +++++- src/lib.rs | 15 +++++++++++++ src/platform/os/mod.rs | 46 ++++++++++++++++++---------------------- 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7586f4a..4bfb168 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ serde_json = { version = "1.0.117", optional = false } crossbeam = "0.8.4" toml = { version = "0.8.13", features = ["parse"], optional = false } dyn-clone = "1.0.17" +sha2 = "0.10.8" # buildin modules #mize_module_blob = { path = "./modules/modules/Blob" } @@ -72,7 +73,6 @@ http_req = { git = "https://github.com/c2vi/http_req", optional = true, default- axum = { version = "0.6.1", features = ["ws", "headers"], optional = true } axum-extra = { version = "0.4.2", features = ["spa"], optional = true } axum-macros = { version = "0.3.0", optional = true } -sha2 = "0.10.8" #tokio-core = "0.1.18" #tokio-io = "0.1.13" diff --git a/src/core/instance/mod.rs b/src/core/instance/mod.rs index 86f75e1..c28af95 100644 --- a/src/core/instance/mod.rs +++ b/src/core/instance/mod.rs @@ -298,10 +298,14 @@ impl Instance { } pub fn load_module(&mut self, name: &str) -> MizeResult<()> { - // platform specific init code crate::platform::any::load_module(self, name, None) } + pub fn fetch_module(&mut self, name: &str) -> MizeResult { + // platform specific way to fetch a module + crate::platform::any::fetch_module(self, name) + } + pub fn load_module_at(&mut self, name: &str, path: PathBuf) -> MizeResult<()> { // platform specific init code crate::platform::any::load_module(self, name, Some(path)) diff --git a/src/lib.rs b/src/lib.rs index 6013a3e..7f3e2bc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,6 +57,7 @@ pub mod platform { pub mod os; pub mod any { + //////////// instance_init #[cfg(feature = "wasm-target")] pub use super::wasm::wasm_instance_init as instance_init; @@ -67,6 +68,7 @@ pub mod platform { pub use super::super::instance_init; + //////////// load_module #[cfg(feature = "os-target")] pub use super::os::load_module; @@ -76,6 +78,17 @@ pub mod platform { #[cfg(not(any(feature = "os-target", feature = "wasm-target")))] pub use super::super::load_module; + + //////////// fetch_module + #[cfg(feature = "os-target")] + pub use super::os::fetch_module; + + #[cfg(feature = "wasm-target")] + pub use super::super::fetch_module; + + #[cfg(not(any(feature = "os-target", feature = "wasm-target")))] + pub use super::super::fetch_module; + } } @@ -85,4 +98,6 @@ pub fn instance_init(instance: &mut core::instance::Instance) {} pub fn load_module(instance: &mut core::instance::Instance, name: &str, path: Option) -> MizeResult<()> { Ok(()) } +pub fn fetch_module(instance: &mut core::instance::Instance, name: &str, path: Option) -> MizeResult { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) } + diff --git a/src/platform/os/mod.rs b/src/platform/os/mod.rs index bd29e29..90fdeaa 100644 --- a/src/platform/os/mod.rs +++ b/src/platform/os/mod.rs @@ -175,7 +175,7 @@ pub fn get_module_hash(instance: &mut Instance, name: &str, mut options: ItemDat } -fn fetch_module(instance: &mut Instance, store_path: &str, module_url: &str, module_hash: &str, module_name: &str, module_selector: &str) -> MizeResult<()> { +fn download_module(instance: &mut Instance, store_path: &str, module_url: &str, module_hash: &str, module_name: &str, module_selector: &str) -> MizeResult<()> { let tmp_file_path_gz = format!("{}/{}.tar.gz", store_path, module_hash); @@ -184,7 +184,7 @@ fn fetch_module(instance: &mut Instance, store_path: &str, module_url: &str, mod let mut tmp_gz_file = File::create(&tmp_file_path_gz)?; - debug!("fetching module '{}' with hash: {}", module_name, module_hash); + debug!("downloading module '{}' with hash: {}", module_name, module_hash); let response = http_req::request::get(format!("http://{}/mize/dist/{}-{}.tar.gz", module_url, module_hash, module_name), &mut tmp_gz_file)?; @@ -214,34 +214,30 @@ fn fetch_module(instance: &mut Instance, store_path: &str, module_url: &str, mod Ok(()) } -pub fn load_module(instance: &mut Instance, module_name: &str, path: Option) -> MizeResult<()> { +pub fn fetch_module(instance: &mut Instance, module_name: &str) -> MizeResult { - let module_dir_str = instance.get(format!("0/config/module_dir/{}", module_name))?.value_string(); + if let Ok(module_path) = instance.get(format!("0/config/module_dir/{}", module_name))?.value_string() { + return Ok(module_path); + } else { + // if null, load it from the url + let module_url = instance.get("0/config/module_url")?.value_string()?; - let module_path = match module_dir_str { - Err(_) => { - // if null, load it from the url - let module_url = instance.get("0/config/module_url")?.value_string()?; + let store_path = instance.get("0/config/store_path")?.value_string()?; - let store_path = instance.get("0/config/store_path")?.value_string()?; + let (module_hash, module_selector) = get_module_hash(instance, module_name, ItemData::new())?; - let (module_hash, module_selector) = get_module_hash(instance, module_name, ItemData::new())?; - - if !PathBuf::from(format!("{}/modules/{}", store_path, module_hash.as_str())).exists() - || !PathBuf::from(format!("{}/modules/{}/lib/libmize_module_{}.so", store_path.as_str(), module_hash.as_str(), module_name)).exists() - { - // fetch module if it does not exist - fetch_module(instance, store_path.as_str(), module_url.as_str(), module_hash.as_str(), module_name, module_selector.as_str())?; - } - - // the module_path - format!("{}/modules/{}/lib/libmize_module_{}.so", store_path, module_hash, module_name) - }, - Ok(module_dir_str) => { - // the module_path with the module_dir from 0/config - format!("{}/lib/libmize_module_{}.so", module_dir_str.as_str(), module_name) + if !PathBuf::from(format!("{}/modules/{}", store_path, module_hash.as_str())).exists() { + // download module if it does not exist + download_module(instance, store_path.as_str(), module_url.as_str(), module_hash.as_str(), module_name, module_selector.as_str())?; } - }; + + return Ok(format!("{}/modules/{}", store_path, module_hash.as_str())); + } +} + +pub fn load_module(instance: &mut Instance, module_name: &str, path: Option) -> MizeResult<()> { + + let module_path = format!("{}/lib/libmmize_module_{}.so", fetch_module(instance, module_name)?, module_name); let lib = unsafe { libloading::Library::new(module_path)? };