spacetimedb_fs_utils/
lib.rs1use std::io::Write;
2use std::path::Path;
3
4pub mod compression;
5pub mod dir_trie;
6pub mod lockfile;
7
8pub fn create_parent_dir(file: &Path) -> Result<(), std::io::Error> {
9 let Some(parent) = file.parent() else {
13 return Ok(());
14 };
15
16 if parent == Path::new("") {
20 return Ok(());
21 }
22
23 std::fs::create_dir_all(parent)
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}