(&self, hash: &str, serialized_data: &[u8])
| 262 | } |
| 263 | |
| 264 | fn update_data(&self, hash: &str, serialized_data: &[u8]) -> Option<()> { |
| 265 | let mod_cache_path = self.root_path.join(hash); |
| 266 | trace!("update_data() for path: {}", mod_cache_path.display()); |
| 267 | let compressed_data = zstd::encode_all( |
| 268 | &serialized_data[..], |
| 269 | self.cache.baseline_compression_level(), |
| 270 | ) |
| 271 | .map_err(|err| warn!("Failed to compress cached code: {err}")) |
| 272 | .ok()?; |
| 273 | |
| 274 | // Optimize syscalls: first, try writing to disk. It should succeed in most cases. |
| 275 | // Otherwise, try creating the cache directory and retry writing to the file. |
| 276 | if fs_write_atomic(&mod_cache_path, "mod", &compressed_data).is_ok() { |
| 277 | return Some(()); |
| 278 | } |
| 279 | |
| 280 | debug!( |
| 281 | "Attempting to create the cache directory, because \ |
| 282 | failed to write cached code to disk, path: {}", |
| 283 | mod_cache_path.display(), |
| 284 | ); |
| 285 | |
| 286 | let cache_dir = mod_cache_path.parent().unwrap(); |
| 287 | fs::create_dir_all(cache_dir) |
| 288 | .map_err(|err| { |
| 289 | warn!( |
| 290 | "Failed to create cache directory, path: {}, message: {}", |
| 291 | cache_dir.display(), |
| 292 | err |
| 293 | ) |
| 294 | }) |
| 295 | .ok()?; |
| 296 | |
| 297 | match fs_write_atomic(&mod_cache_path, "mod", &compressed_data) { |
| 298 | Ok(_) => Some(()), |
| 299 | Err(err) => { |
| 300 | warn!( |
| 301 | "Failed to write file with rename, target path: {}, err: {}", |
| 302 | mod_cache_path.display(), |
| 303 | err |
| 304 | ); |
| 305 | None |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | impl Hasher for Sha256Hasher { |
no test coverage detected