Skip to content

Commit

Permalink
Merge #278
Browse files Browse the repository at this point in the history
278: Add versioning to cache r=lachlansneff a=MarkMcCaskey

resolves #272 

Co-authored-by: Mark <mark@marks-macbook-pro.local>
Co-authored-by: Mark McCaskey <mark@wasmer.io>
Co-authored-by: Lachlan Sneff <lachlan.sneff@gmail.com>
  • Loading branch information
4 people committed Mar 19, 2019
2 parents c101498 + 5657be1 commit 4a74382
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/runtime-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ field-offset = "0.1.1"
[features]
debug = []

[build-dependencies]
blake2b_simd = "0.4.1"
26 changes: 26 additions & 0 deletions lib/runtime-core/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use blake2b_simd::blake2bp;
use std::{env, fs, io::Write, path::PathBuf};

const WASMER_VERSION: &'static str = env!("CARGO_PKG_VERSION");

fn main() {
let mut state = blake2bp::State::new();
state.update(WASMER_VERSION.as_bytes());

let hasher = state.finalize();
let hash_string = hasher.to_hex().as_str().to_owned();

let crate_dir = env::var("OUT_DIR").unwrap();
let wasmer_version_hash_file = {
let mut path = PathBuf::from(&crate_dir);
path.push("wasmer_version_hash.txt");
path
};

let mut f_out = fs::File::create(wasmer_version_hash_file)
.expect("Could not create file for wasmer hash value");

f_out
.write_all(hash_string.as_bytes())
.expect("Could not write to file for wasmer hash value");
}
4 changes: 4 additions & 0 deletions lib/runtime-core/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,7 @@ pub trait Cache {
fn load(&self, key: WasmHash) -> Result<Module, Self::LoadError>;
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), Self::StoreError>;
}

/// A unique ID generated from the version of Wasmer for use with cache versioning
pub const WASMER_VERSION_HASH: &'static str =
include_str!(concat!(env!("OUT_DIR"), "/wasmer_version_hash.txt"));
9 changes: 7 additions & 2 deletions lib/runtime/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
};

use wasmer_runtime_core::cache::Error as CacheError;
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash};
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash, WASMER_VERSION_HASH};

/// Representation of a directory that contains compiled wasm artifacts.
///
Expand Down Expand Up @@ -40,12 +40,17 @@ pub struct FileSystemCache {

impl FileSystemCache {
/// Construct a new `FileSystemCache` around the specified directory.
/// The contents of the cache are stored in sub-versioned directories.
///
/// # Note:
/// This method is unsafe because there's no way to ensure the artifacts
/// stored in this cache haven't been corrupted or tampered with.
pub unsafe fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
let path: PathBuf = path.into();
let path: PathBuf = {
let mut path = path.into();
path.push(WASMER_VERSION_HASH);
path
};

if path.exists() {
let metadata = path.metadata()?;
Expand Down
11 changes: 8 additions & 3 deletions src/bin/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use structopt::StructOpt;
use wasmer::webassembly::InstanceABI;
use wasmer::*;
use wasmer_emscripten;
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash};
use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH};

#[derive(Debug, StructOpt)]
#[structopt(name = "wasmer", about = "Wasm execution runtime.")]
Expand Down Expand Up @@ -48,9 +48,11 @@ struct Run {

#[derive(Debug, StructOpt)]
enum Cache {
/// Clear the cache
#[structopt(name = "clean")]
Clean,

/// Display the location of the cache
#[structopt(name = "dir")]
Dir,
}
Expand All @@ -72,6 +74,7 @@ fn get_cache_dir() -> PathBuf {
// We use a temporal directory for saving cache files
let mut temp_dir = env::temp_dir();
temp_dir.push("wasmer");
temp_dir.push(WASMER_VERSION_HASH);
temp_dir
}
}
Expand Down Expand Up @@ -194,8 +197,10 @@ fn main() {
Cache::Clean => {
use std::fs;
let cache_dir = get_cache_dir();
fs::remove_dir_all(cache_dir.clone()).expect("Can't remove cache dir");
fs::create_dir(cache_dir.clone()).expect("Can't create cache dir");
if cache_dir.exists() {
fs::remove_dir_all(cache_dir.clone()).expect("Can't remove cache dir");
}
fs::create_dir_all(cache_dir.clone()).expect("Can't create cache dir");
}
Cache::Dir => {
println!("{}", get_cache_dir().to_string_lossy());
Expand Down

0 comments on commit 4a74382

Please sign in to comment.