| 255 | } |
| 256 | |
| 257 | pub fn write_metadata(metadata: &Metadata, log: &Logger, cache_path: Option<PathBuf>) { |
| 258 | if let Some(cache) = cache_path { |
| 259 | let metadata_path = get_metadata_path(cache.clone()); |
| 260 | log.trace(format!("Writing metadata to {}", metadata_path.display())); |
| 261 | // Several Selenium Manager processes may share one cache (e.g. parallel test |
| 262 | // workers), and writing the file in place lets them read it truncated or half |
| 263 | // written. Renaming a complete sibling file into place is atomic, so readers see |
| 264 | // either the previous or the new content. |
| 265 | let tmp_path = metadata_path.with_extension(format!("json.{}.tmp", std::process::id())); |
| 266 | fs::write(&tmp_path, serde_json::to_string_pretty(metadata).unwrap()) |
| 267 | .and_then(|_| fs::rename(&tmp_path, &metadata_path)) |
| 268 | .unwrap_or_else(|err| { |
| 269 | fs::remove_file(&tmp_path).ok(); |
| 270 | log.warn(format!( |
| 271 | "Metadata cannot be written in cache ({}): {}", |
| 272 | cache.display(), |
| 273 | err |
| 274 | )); |
| 275 | }); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | pub fn clear_metadata(log: &Logger, path: &str) { |
| 280 | let cache_path = Path::new(path).to_path_buf(); |