MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / write_atomic

Function write_atomic

nodedb/src/engine/array/flush.rs:115–135  ·  view source on GitHub ↗
(path: &std::path::Path, bytes: &[u8])

Source from the content-addressed store, hash-verified

113}
114
115fn 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)]
138mod tests {

Callers 1

Calls 2

createFunction · 0.50
openFunction · 0.50

Tested by

no test coverage detected