|
| 1 | +use std::fs; |
1 | 2 | use std::io;
|
2 | 3 | use std::path::PathBuf;
|
3 | 4 | use thiserror::Error;
|
@@ -137,6 +138,22 @@ impl FileSystemCache {
|
137 | 138 | Ok(())
|
138 | 139 | }
|
139 | 140 |
|
| 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 | + |
140 | 157 | /// The path to the latest version of the modules.
|
141 | 158 | fn latest_modules_path(&self) -> PathBuf {
|
142 | 159 | let version = format!(
|
@@ -225,4 +242,34 @@ mod tests {
|
225 | 242 | );
|
226 | 243 | let _serialized_module = fs::read(file_path).unwrap();
|
227 | 244 | }
|
| 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 | + } |
228 | 275 | }
|
0 commit comments