added fetch_module

This commit is contained in:
Sebastian Moser
2024-10-20 12:36:14 +02:00
parent 03312ee42a
commit 601362a109
4 changed files with 42 additions and 27 deletions

View File

@@ -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<String> {
// 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))

View File

@@ -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<PathBuf>) -> MizeResult<()> { Ok(()) }
pub fn fetch_module(instance: &mut core::instance::Instance, name: &str, path: Option<PathBuf>) -> MizeResult<String> { Ok("oh noooooooooooooo, something went really really wrong, if this ends up in the executable....".to_owned()) }

View File

@@ -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<PathBuf>) -> MizeResult<()> {
pub fn fetch_module(instance: &mut Instance, module_name: &str) -> MizeResult<String> {
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<PathBuf>) -> MizeResult<()> {
let module_path = format!("{}/lib/libmmize_module_{}.so", fetch_module(instance, module_name)?, module_name);
let lib = unsafe { libloading::Library::new(module_path)? };