(path: &std::path::Path, bytes: &[u8])
| 113 | } |
| 114 | |
| 115 | fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> std::io::Result<()> { |
| 116 | use std::io::Write; |
| 117 | let mut tmp = path.to_path_buf(); |
| 118 | let ext = path |
| 119 | .extension() |
| 120 | .map(|e| e.to_string_lossy().into_owned()) |
| 121 | .unwrap_or_default(); |
| 122 | tmp.set_extension(format!("{ext}.tmp")); |
| 123 | { |
| 124 | let mut f = std::fs::File::create(&tmp)?; |
| 125 | f.write_all(bytes)?; |
| 126 | f.sync_all()?; |
| 127 | } |
| 128 | std::fs::rename(&tmp, path)?; |
| 129 | if let Some(dir) = path.parent() |
| 130 | && let Ok(d) = std::fs::File::open(dir) |
| 131 | { |
| 132 | let _ = d.sync_all(); |
| 133 | } |
| 134 | Ok(()) |
| 135 | } |
| 136 | |
| 137 | #[cfg(test)] |
| 138 | mod tests { |
no test coverage detected