| 128 | } |
| 129 | |
| 130 | pub fn cleanup_cache_files(prefix: &str, cache_location: &Path, max_cache_files: u32) { |
| 131 | let mut paths: Vec<PathBuf> = std::fs::read_dir(cache_location) |
| 132 | .unwrap() |
| 133 | .filter_map(Result::ok) |
| 134 | .filter(|entry| entry.file_name().to_str().unwrap_or("").starts_with(prefix)) |
| 135 | .map(|entry| entry.path()) |
| 136 | .collect(); |
| 137 | |
| 138 | paths.sort_by_key(|path| { |
| 139 | std::fs::metadata(path) |
| 140 | .and_then(|m| m.modified()) |
| 141 | .unwrap_or_else(|_| std::time::SystemTime::now()) |
| 142 | }); |
| 143 | |
| 144 | paths.reverse(); |
| 145 | while paths.len() > max_cache_files as usize { |
| 146 | let oldest = paths.pop().unwrap(); |
| 147 | std::fs::remove_file(&oldest) |
| 148 | .inspect_err(|e| eprintln!("Failed to delete the old cache file: {}", e)) |
| 149 | .inspect(|_| println!("Old cache file deleted: {:?}", oldest)) |
| 150 | .ok(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | pub const DEFAULT_COMPRESSION_LEVEL: i32 = 5; |
| 155 | |