diff --git a/examples/ipfs_node/Config.toml b/examples/ipfs_node/Config.toml index bc55ba70..8e3c9711 100644 --- a/examples/ipfs_node/Config.toml +++ b/examples/ipfs_node/Config.toml @@ -8,15 +8,15 @@ ipfs = "/usr/local/bin/ipfs" [core_module.wasi] - envs = ["asdsad=sdaasd"] + envs = [] preopened_files = ["/Users/mike/dev/work/fluence/wasm/tmp/"] - mapped_dirs = { "/tmp" = "/Users/mike/dev/work/fluence/wasm/tmp" } + mapped_dirs = { "tmp" = "/Users/mike/dev/work/fluence/wasm/tmp" } [rpc_module] mem_pages_count = 100 logger_enabled = true [rpc_module.wasi] - envs = ["asdsad=sdaasd"] + envs = [] preopened_files = ["/Users/mike/dev/work/fluence/wasm/tmp"] - mapped_dirs = { "/tmp" = "/Users/mike/dev/work/fluence/wasm/tmp" } + mapped_dirs = { "tmp" = "/Users/mike/dev/work/fluence/wasm/tmp" } diff --git a/examples/ipfs_node/src/main.rs b/examples/ipfs_node/src/main.rs index eaa05173..27eda05a 100644 --- a/examples/ipfs_node/src/main.rs +++ b/examples/ipfs_node/src/main.rs @@ -46,23 +46,13 @@ fn main() { println!("ipfs node interface is\n{}", ipfs_node.get_interface()); let node_addresses = ipfs_node - .core_call( - "ipfs_node.wasm", - "get_addresses", - &[], - ) + .core_call("ipfs_node.wasm", "get_addresses", &[]) .unwrap(); println!("ipfs node addresses are:\n{:?}", node_addresses); let result = ipfs_node - .rpc_call( - &ipfs_rpc, - "get", - &[IValue::String( - "Qmf412jQZiuVUtdgnB36FXFX7xg5V6KEbSJ4dpQuhkLyfD".to_string(), - )], - ) + .rpc_call(&ipfs_rpc, "put", &[IValue::String("asdasdasd".to_string())]) .unwrap(); println!("execution result {:?}", result); diff --git a/examples/ipfs_node/src/node/utils.rs b/examples/ipfs_node/src/node/utils.rs index 2c0f2852..081cbaa5 100644 --- a/examples/ipfs_node/src/node/utils.rs +++ b/examples/ipfs_node/src/node/utils.rs @@ -29,7 +29,6 @@ use wasmer_core::backend::SigRegistry; use wasmer_runtime::types::LocalOrImport; use wasmer_core::module::ExportIndex; -use std::collections::HashMap; use std::path::PathBuf; // based on Wasmer: https://github.com/wasmerio/wasmer/blob/081f6250e69b98b9f95a8f62ad6d8386534f3279/lib/runtime-core/src/instance.rs#L863 @@ -146,13 +145,15 @@ pub(super) fn make_wasm_process_config( .map(|(from, to)| (from, PathBuf::from(to))) .collect::>(); } - }; - let _mapped_dirs = wasm_module_config - .wasi_mapped_dirs - .iter() - .map(|(from, to)| (from.clone(), to.as_path().to_str().unwrap().to_string())) - .collect::>(); + let mapped_dirs = wasm_module_config + .wasi_mapped_dirs + .iter() + .map(|(from, to)| (format!("{}={}", from, to.as_path().to_str().unwrap()).into_bytes())) + .collect::>(); + + wasm_module_config.wasi_envs.extend(mapped_dirs); + }; if let Some(imports) = module_config.imports { for (import_name, host_cmd) in imports { diff --git a/examples/ipfs_node/wasm/ipfs_node/src/main.rs b/examples/ipfs_node/wasm/ipfs_node/src/main.rs index dd32c34f..12909952 100644 --- a/examples/ipfs_node/wasm/ipfs_node/src/main.rs +++ b/examples/ipfs_node/wasm/ipfs_node/src/main.rs @@ -18,13 +18,19 @@ mod mem; mod result; +mod path; use crate::result::{RESULT_PTR, RESULT_SIZE}; +use crate::path::to_full_path; -const RESULT_PATH: &str = "/Users/mike/dev/work/fluence/wasm/tmp/ipfs_rpc_file"; +const RESULT_FILE_PATH: &str = "/tmp/ipfs_rpc_file"; pub fn main() { - println!("ipfs_node.main: WASI initialization finished"); + let env_variable = std::env::var("tmp").unwrap(); + println!( + "ipfs_node.main: WASI initialization finished, env {}", + env_variable + ); } #[no_mangle] @@ -34,6 +40,8 @@ pub unsafe fn put(file_path_ptr: *mut u8, file_path_size: usize) { let msg = format!("ipfs_node.put: file path is {}\n", file_path); log_utf8_string(msg.as_ptr() as _, msg.len() as _); + let file_path = to_full_path(file_path); + let cmd = format!("add -Q {}", file_path); let result = ipfs(cmd.as_ptr() as _, cmd.len() as _); @@ -62,7 +70,9 @@ pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize) { let msg = format!("ipfs_node.get: file hash is {}\n", hash); log_utf8_string(msg.as_ptr() as _, msg.len() as _); - let cmd = format!("get -o {} {}", RESULT_PATH, hash); + let result_file_path = to_full_path(RESULT_FILE_PATH); + + let cmd = format!("get -o {} {}", result_file_path, hash); let _result = ipfs(cmd.as_ptr() as _, cmd.len() as _); let _output = String::from_raw_parts( @@ -73,7 +83,7 @@ pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize) { // TODO: check output - let file_path = RESULT_PATH.to_string(); + let file_path = RESULT_FILE_PATH.to_string(); let msg = format!("ipfs_node.get: file path is {}", file_path); log_utf8_string(msg.as_ptr() as _, msg.len() as _); @@ -100,7 +110,10 @@ pub unsafe fn get_addresses() { "host ipfs call failed".to_string() }; - let msg = format!("ipfs_node.get_addresses: node addresses are {} \n", multiaddrs); + let msg = format!( + "ipfs_node.get_addresses: node addresses are {} \n", + multiaddrs + ); log_utf8_string(msg.as_ptr() as _, msg.len() as _); *RESULT_PTR.get_mut() = multiaddrs.as_ptr() as _; diff --git a/examples/ipfs_node/wasm/ipfs_node/src/path.rs b/examples/ipfs_node/wasm/ipfs_node/src/path.rs new file mode 100644 index 00000000..fcecf042 --- /dev/null +++ b/examples/ipfs_node/wasm/ipfs_node/src/path.rs @@ -0,0 +1,51 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +pub(super) fn to_full_path(cmd: S) -> String +where + S: Into, +{ + use std::path::Path; + use std::path::Component; + + let cmd = cmd.into(); + let path = Path::new(&cmd); + + let mut components = path.components(); + let is_absolute = components.next() == Some(Component::RootDir); + + if !is_absolute { + return cmd; + } + + let parent = match components.next() { + Some(Component::Normal(path)) => path.to_str().unwrap(), + _ => return cmd, + }; + + match std::env::var(parent) { + Ok(to_dir) => { + let mut full_path = std::path::PathBuf::from(to_dir); + + // TODO: optimize this + while let Some(component) = components.next() { + full_path.push(component); + } + full_path.to_string_lossy().into_owned() + } + Err(_) => cmd, + } +} diff --git a/examples/ipfs_node/wasm/ipfs_rpc/src/main.rs b/examples/ipfs_node/wasm/ipfs_rpc/src/main.rs index c708703a..45727fb2 100644 --- a/examples/ipfs_node/wasm/ipfs_rpc/src/main.rs +++ b/examples/ipfs_node/wasm/ipfs_rpc/src/main.rs @@ -22,7 +22,7 @@ use crate::result::{RESULT_PTR, RESULT_SIZE}; use std::fs; use std::path::PathBuf; -const RPC_TMP_FILEPATH: &str = "/Users/mike/dev/work/fluence/wasm/tmp/ipfs_rpc_file"; +const RPC_TMP_FILEPATH: &str = "/tmp/ipfs_rpc_file"; pub fn main() { println!("ipfs_rpc.main: WASI initialization finished");