Skip to content

Commit c3cde9b

Browse files
committed
Add FileSystemCache::remove
1 parent f8c4f77 commit c3cde9b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

packages/vm/src/modules/file_system_cache.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fs;
12
use std::io;
23
use std::path::PathBuf;
34
use thiserror::Error;
@@ -137,6 +138,22 @@ impl FileSystemCache {
137138
Ok(())
138139
}
139140

141+
/// Removes a serialized module from the file system.
142+
///
143+
/// Returns true if the file existed and false if the file did not exist.
144+
pub fn remove(&mut self, checksum: &Checksum) -> VmResult<bool> {
145+
let filename = checksum.to_hex();
146+
let file_path = self.latest_modules_path().join(filename);
147+
148+
if file_path.exists() {
149+
fs::remove_file(file_path)
150+
.map_err(|_e| VmError::cache_err("Error deleting module from disk"))?;
151+
Ok(true)
152+
} else {
153+
Ok(false)
154+
}
155+
}
156+
140157
/// The path to the latest version of the modules.
141158
fn latest_modules_path(&self) -> PathBuf {
142159
let version = format!(
@@ -225,4 +242,34 @@ mod tests {
225242
);
226243
let _serialized_module = fs::read(file_path).unwrap();
227244
}
245+
246+
#[test]
247+
fn file_system_cache_remove_works() {
248+
let tmp_dir = TempDir::new().unwrap();
249+
let mut cache = unsafe { FileSystemCache::new(tmp_dir.path()).unwrap() };
250+
251+
// Create module
252+
let wasm = wat::parse_str(SOME_WAT).unwrap();
253+
let checksum = Checksum::generate(&wasm);
254+
255+
// Store module
256+
let module = compile(&wasm, None, &[]).unwrap();
257+
cache.store(&checksum, &module).unwrap();
258+
259+
// It's there
260+
let store = make_runtime_store(TESTING_MEMORY_LIMIT);
261+
assert!(cache.load(&checksum, &store).unwrap().is_some());
262+
263+
// Remove module
264+
let existed = cache.remove(&checksum).unwrap();
265+
assert!(existed);
266+
267+
// it's gone now
268+
let store = make_runtime_store(TESTING_MEMORY_LIMIT);
269+
assert!(cache.load(&checksum, &store).unwrap().is_none());
270+
271+
// Remove again
272+
let existed = cache.remove(&checksum).unwrap();
273+
assert!(!existed);
274+
}
228275
}

0 commit comments

Comments
 (0)