Coverage / crates/spectra-core/src/durable.rs 100.0%
26 of 26 lines run, 10 of 10 functions · run #31 on main · run missed
| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | | use std::io::Write; |
| 5 | | use std::path::Path; |
| 6 | |
|
| 7 | 612× | fn write(path: &Path, bytes: &[u8], private: bool) -> std::io::Result<()> { |
| 8 | 612× | let dir = path.parent().filter(|d| !d.as_os_str().is_empty()).unwrap_or(Path::new(".")); |
| 9 | 612× | std::fs::create_dir_all(dir)?; |
| 10 | 612× | let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("file"); |
| 11 | 612× | let tmp = dir.join(format!(".{name}.{}.tmp", std::process::id())); |
| 12 | 612× | let mut opts = std::fs::OpenOptions::new(); |
| 13 | 612× | opts.write(true).create(true).truncate(true); |
| 14 | | #[cfg(unix)] |
| 15 | | { |
| 16 | | use std::os::unix::fs::OpenOptionsExt; |
| 17 | 612× | opts.mode(if private { 0o600 } else { 0o644 }); |
| 18 | | } |
| 19 | | #[cfg(not(unix))] |
| 20 | | let _ = private; |
| 21 | 612× | let mut f = opts.open(&tmp)?; |
| 22 | 612× | f.write_all(bytes)?; |
| 23 | 612× | f.sync_all()?; |
| 24 | 612× | drop(f); |
| 25 | 612× | std::fs::rename(&tmp, path)?; |
| 26 | 612× | if let Ok(d) = std::fs::File::open(dir) { |
| 27 | 612× | let _ = d.sync_all(); |
| 28 | 612× | } |
| 29 | 612× | Ok(()) |
| 30 | 612× | } |
| 31 | |
|
| 32 | 418× | pub fn write_atomic(path: &Path, bytes: &[u8]) -> std::io::Result<()> { |
| 33 | 418× | write(path, bytes, false) |
| 34 | 418× | } |
| 35 | |
|
| 36 | |
|
| 37 | 194× | pub fn write_atomic_private(path: &Path, bytes: &[u8]) -> std::io::Result<()> { |
| 38 | 194× | write(path, bytes, true) |
| 39 | 194× | } |