2020-05-30 01:55:39 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2020-06-05 23:12:02 +03:00
|
|
|
|
2020-06-02 01:02:22 +03:00
|
|
|
#![allow(clippy::missing_safety_doc)]
|
2020-05-30 01:55:39 +03:00
|
|
|
|
|
|
|
mod mem;
|
|
|
|
mod result;
|
|
|
|
|
2020-06-05 23:12:02 +03:00
|
|
|
use crate::result::{RESULT_PTR, RESULT_SIZE};
|
2020-05-30 01:55:39 +03:00
|
|
|
|
2020-06-11 01:07:32 +03:00
|
|
|
const RESULT_PATH: &str = "/tmp/ipfs_rpc_file";
|
|
|
|
|
2020-05-30 01:55:39 +03:00
|
|
|
#[no_mangle]
|
2020-06-11 01:07:32 +03:00
|
|
|
pub unsafe fn put(file_path_ptr: *mut u8, file_path_size: usize) {
|
|
|
|
let file_path = String::from_raw_parts(file_path_ptr, file_path_size, file_path_size);
|
2020-06-05 23:12:02 +03:00
|
|
|
|
2020-06-11 03:10:37 +03:00
|
|
|
let msg = format!("ipfs_node.put: file path is {}\n", file_path);
|
2020-06-01 02:45:04 +03:00
|
|
|
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
|
|
|
|
|
2020-06-11 01:07:32 +03:00
|
|
|
let cmd = format!("add -Q {}", file_path);
|
|
|
|
let result = ipfs(cmd.as_ptr() as _, cmd.len() as _);
|
2020-06-05 23:12:02 +03:00
|
|
|
|
2020-06-11 03:10:37 +03:00
|
|
|
let hash = if result == 1 {
|
2020-06-11 01:07:32 +03:00
|
|
|
String::from_raw_parts(
|
|
|
|
*RESULT_PTR.get_mut() as _,
|
|
|
|
*RESULT_SIZE.get_mut(),
|
|
|
|
*RESULT_SIZE.get_mut(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
"host ipfs call failed".to_string()
|
|
|
|
};
|
2020-06-07 12:33:26 +03:00
|
|
|
|
2020-06-11 03:10:37 +03:00
|
|
|
let msg = format!("ipfs_node.put: file add wtih hash is {} \n", hash);
|
2020-06-11 01:07:32 +03:00
|
|
|
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
|
2020-06-05 23:12:02 +03:00
|
|
|
|
2020-06-11 01:07:32 +03:00
|
|
|
*RESULT_PTR.get_mut() = hash.as_ptr() as _;
|
|
|
|
*RESULT_SIZE.get_mut() = hash.len();
|
|
|
|
std::mem::forget(hash);
|
2020-06-05 23:12:02 +03:00
|
|
|
}
|
2020-06-01 02:45:04 +03:00
|
|
|
|
2020-06-05 23:12:02 +03:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe fn get(hash_ptr: *mut u8, hash_size: usize) {
|
|
|
|
let hash = String::from_raw_parts(hash_ptr, hash_size, hash_size);
|
2020-05-30 01:55:39 +03:00
|
|
|
|
2020-06-11 03:10:37 +03:00
|
|
|
let msg = format!("ipfs_node.get: file hash is {}\n", hash);
|
2020-06-01 02:45:04 +03:00
|
|
|
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
|
2020-06-05 23:12:02 +03:00
|
|
|
|
2020-06-11 01:07:32 +03:00
|
|
|
let cmd = format!("get -o {} {}", RESULT_PATH, hash);
|
|
|
|
let _result = ipfs(cmd.as_ptr() as _, cmd.len() as _);
|
2020-06-05 23:12:02 +03:00
|
|
|
|
2020-06-11 01:07:32 +03:00
|
|
|
let _output = String::from_raw_parts(
|
|
|
|
*RESULT_PTR.get_mut() as _,
|
|
|
|
*RESULT_SIZE.get_mut(),
|
|
|
|
*RESULT_SIZE.get_mut(),
|
|
|
|
);
|
|
|
|
|
|
|
|
// TODO: check output
|
|
|
|
|
|
|
|
let file_path = RESULT_PATH.to_string();
|
2020-06-11 03:10:37 +03:00
|
|
|
let msg = format!("ipfs_node.get: file path is {}", file_path);
|
2020-06-11 01:07:32 +03:00
|
|
|
log_utf8_string(msg.as_ptr() as _, msg.len() as _);
|
2020-06-05 23:12:02 +03:00
|
|
|
|
2020-06-11 01:07:32 +03:00
|
|
|
*RESULT_PTR.get_mut() = file_path.as_ptr() as _;
|
|
|
|
*RESULT_SIZE.get_mut() = file_path.len();
|
|
|
|
std::mem::forget(file_path);
|
2020-05-30 01:55:39 +03:00
|
|
|
}
|
|
|
|
|
2020-06-06 21:34:13 +03:00
|
|
|
#[link(wasm_import_module = "host")]
|
2020-05-30 01:55:39 +03:00
|
|
|
extern "C" {
|
|
|
|
/// Writes a byte string of size bytes that starts from ptr to a logger.
|
|
|
|
fn log_utf8_string(ptr: i32, size: i32);
|
|
|
|
|
|
|
|
/// Put a file to ipfs, returns ipfs hash of the file.
|
2020-06-07 12:33:26 +03:00
|
|
|
fn ipfs(ptr: i32, size: i32) -> i32;
|
2020-05-30 01:55:39 +03:00
|
|
|
}
|