1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crate::Module;
use memmap::Mmap;
use std::{
fs::{create_dir_all, File},
io::{self, Write},
path::PathBuf,
};
use wasmer_runtime_core::cache::Error as CacheError;
pub use wasmer_runtime_core::cache::{Artifact, Cache, WasmHash, WASMER_VERSION_HASH};
pub struct FileSystemCache {
path: PathBuf,
}
impl FileSystemCache {
pub unsafe fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
let path: PathBuf = {
let mut path = path.into();
path.push(WASMER_VERSION_HASH);
path
};
if path.exists() {
let metadata = path.metadata()?;
if metadata.is_dir() {
if !metadata.permissions().readonly() {
Ok(Self { path })
} else {
Err(io::Error::new(
io::ErrorKind::PermissionDenied,
format!("the supplied path is readonly: {}", path.display()),
))
}
} else {
Err(io::Error::new(
io::ErrorKind::PermissionDenied,
format!(
"the supplied path already points to a file: {}",
path.display()
),
))
}
} else {
create_dir_all(&path)?;
Ok(Self { path })
}
}
}
impl Cache for FileSystemCache {
type LoadError = CacheError;
type StoreError = CacheError;
fn load(&self, key: WasmHash) -> Result<Module, CacheError> {
let filename = key.encode();
let mut new_path_buf = self.path.clone();
new_path_buf.push(filename);
let file = File::open(new_path_buf)?;
let mmap = unsafe { Mmap::map(&file)? };
let serialized_cache = Artifact::deserialize(&mmap[..])?;
unsafe {
wasmer_runtime_core::load_cache_with(serialized_cache, &super::default_compiler())
}
}
fn store(&mut self, key: WasmHash, module: Module) -> Result<(), CacheError> {
let filename = key.encode();
let mut new_path_buf = self.path.clone();
new_path_buf.push(filename);
let serialized_cache = module.cache()?;
let buffer = serialized_cache.serialize()?;
let mut file = File::create(new_path_buf)?;
file.write_all(&buffer)?;
Ok(())
}
}