MCPcopy Create free account
hub / github.com/clockworklabs/SpacetimeDB / atomic_write

Function atomic_write

crates/fs-utils/src/lib.rs:29–46  ·  view source on GitHub ↗
(file_path: &Path, data: String)

Source from the content-addressed store, hash-verified

27}
28
29pub fn atomic_write(file_path: &Path, data: String) -> anyhow::Result<()> {
30 let mut temp_path = file_path.to_path_buf();
31 let mut temp_file: std::fs::File;
32 loop {
33 temp_path.set_extension(format!(".tmp{}", rand::random::<u32>()));
34 let opened = std::fs::OpenOptions::new()
35 .write(true)
36 .create_new(true)
37 .open(&temp_path);
38 if let Ok(file) = opened {
39 temp_file = file;
40 break;
41 }
42 }
43 temp_file.write_all(data.as_bytes())?;
44 std::fs::rename(&temp_path, file_path)?;
45 Ok(())
46}

Callers 1

saveMethod · 0.85

Calls 6

renameFunction · 0.85
openMethod · 0.65
newFunction · 0.50
OkFunction · 0.50
writeMethod · 0.45
as_bytesMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…