This commit merges [CraneStation/wasi-common] repo as a subdir of this repo while preserving **all** of git history. There is an initiative to pull `wasi-common` into [CraneStation/wasmtime], and [CraneStation/wasmtime] becoming a monorepo. This came about for several reasons with a common theme of convenience, namely, having a monorepo: 1. cleans up the problem of dependencies (as we have seen first hand with dependabot enabled, it can cause some grief) 2. completely removes the problem of syncing the closely dependent repos (e.g., updating `wasi-common` with say a bugfix generally implies creating a "sync" commit for pulling in the changes into the "parent" repo, in this case, `wasmtime`) 3. mainly for the two reasons above, makes publishing to crates.io easier 4. hopefully streamlines the process of getting the community involved in contributing to `wasi-common` as now everything is one place [CraneStation/wasi-common]: https://github.com/CraneStation/wasi-common [CraneStation/wasmtime]: https://github.com/CraneStation/wasmtime
31 lines
891 B
Rust
31 lines
891 B
Rust
use std::fs;
|
|
use std::path::Path;
|
|
use tempfile::{Builder, TempDir};
|
|
|
|
pub fn read_wasm(path: &Path) -> Result<Vec<u8>, String> {
|
|
let data = fs::read(path).map_err(|err| err.to_string())?;
|
|
if data.starts_with(&[b'\0', b'a', b's', b'm']) {
|
|
Ok(data)
|
|
} else {
|
|
Err("Invalid Wasm file encountered".to_owned())
|
|
}
|
|
}
|
|
|
|
pub fn prepare_workspace(exe_name: &str) -> Result<TempDir, String> {
|
|
let prefix = format!("wasi_common_{}", exe_name);
|
|
Builder::new()
|
|
.prefix(&prefix)
|
|
.tempdir()
|
|
.map_err(|e| format!("couldn't create workspace in temp files: {}", e))
|
|
}
|
|
|
|
pub fn extract_exec_name_from_path(path: &Path) -> Result<String, String> {
|
|
path.file_stem()
|
|
.and_then(|s| s.to_str())
|
|
.map(String::from)
|
|
.ok_or(format!(
|
|
"couldn't extract the file stem from path {}",
|
|
path.display()
|
|
))
|
|
}
|