Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Write project config to temporary file then atomically rename (#1252)

seer-by-sentry[bot]7b4c0489a730
diff --git a/Cargo.lock b/Cargo.lock
index 6f215d73a..1637e0f73 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1450,7 +1450,9 @@ dependencies = [
"serde",
"serde_json",
"specta",
+ "tempfile",
"tracing",
+ "uuid",
]
[[package]]
@@ -9730,9 +9732,9 @@ dependencies = [
[[package]]
name = "tempfile"
-version = "3.22.0"
+version = "3.23.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "84fa4d11fadde498443cca10fd3ac23c951f0dc59e080e9f4b93d4df4e4eea53"
+checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16"
dependencies = [
"fastrand",
"getrandom 0.3.3",
diff --git a/crates/project/Cargo.toml b/crates/project/Cargo.toml
index 1bca4e93a..b5196be10 100644
--- a/crates/project/Cargo.toml
+++ b/crates/project/Cargo.toml
@@ -17,3 +17,5 @@ either = "1.13.0"
relative-path = { version = "1.9.3", features = ["serde"] }
log = "0.4"
tracing = "0.1.41"
+tempfile = "3.23.0"
+uuid = { version = "1.18.1", features = ["v4"]}
diff --git a/crates/project/src/configuration.rs b/crates/project/src/configuration.rs
index 1600653d2..c29aa2ea7 100644
--- a/crates/project/src/configuration.rs
+++ b/crates/project/src/configuration.rs
@@ -1,4 +1,5 @@
use std::{
+ env::temp_dir,
ops::{Add, Div, Mul, Sub, SubAssign},
path::Path,
};
@@ -639,10 +640,17 @@ impl ProjectConfiguration {
}
pub fn write(&self, project_path: impl AsRef<Path>) -> Result<(), std::io::Error> {
- std::fs::write(
- project_path.as_ref().join("project-config.json"),
- serde_json::to_string_pretty(self)?,
- )
+ let temp_path = temp_dir().join(uuid::Uuid::new_v4().to_string());
+
+ // Write to temporary file first to ensure readers don't see partial files
+ std::fs::write(&temp_path, serde_json::to_string_pretty(self)?)?;
+
+ std::fs::rename(
+ &temp_path,
+ &project_path.as_ref().join("project-config.json"),
+ )?;
+
+ Ok(())
}
pub fn get_segment_time(&self, frame_time: f64) -> Option<(f64, u32)> {