Skip to content

Commit 7b4c048

Browse files
feat: Write project config to temporary file then atomically rename (#1252)
* feat: Write project config to temporary file then atomically rename * format * use unique temp file names * fix --------- Co-authored-by: seer-by-sentry[bot] <157164994+seer-by-sentry[bot]@users.noreply.github.com> Co-authored-by: Brendan Allan <brendonovich@outlook.com>
1 parent 94f8dff commit 7b4c048

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

Cargo.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/project/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ either = "1.13.0"
1717
relative-path = { version = "1.9.3", features = ["serde"] }
1818
log = "0.4"
1919
tracing = "0.1.41"
20+
tempfile = "3.23.0"
21+
uuid = { version = "1.18.1", features = ["v4"]}

crates/project/src/configuration.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
env::temp_dir,
23
ops::{Add, Div, Mul, Sub, SubAssign},
34
path::Path,
45
};
@@ -639,10 +640,17 @@ impl ProjectConfiguration {
639640
}
640641

641642
pub fn write(&self, project_path: impl AsRef<Path>) -> Result<(), std::io::Error> {
642-
std::fs::write(
643-
project_path.as_ref().join("project-config.json"),
644-
serde_json::to_string_pretty(self)?,
645-
)
643+
let temp_path = temp_dir().join(uuid::Uuid::new_v4().to_string());
644+
645+
// Write to temporary file first to ensure readers don't see partial files
646+
std::fs::write(&temp_path, serde_json::to_string_pretty(self)?)?;
647+
648+
std::fs::rename(
649+
&temp_path,
650+
&project_path.as_ref().join("project-config.json"),
651+
)?;
652+
653+
Ok(())
646654
}
647655

648656
pub fn get_segment_time(&self, frame_time: f64) -> Option<(f64, u32)> {

0 commit comments

Comments
 (0)