diff options
author | Lars Wirzenius <liw@liw.fi> | 2024-02-20 09:41:28 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2024-02-20 09:41:28 +0200 |
commit | d96861408c0dbf59622228e4b795ffe231a449be (patch) | |
tree | 34db97c72f693b7558533c973df9eea2cb52b2fe | |
parent | 43b434faddfa9aecd0dc18c108b66428c25f132c (diff) | |
download | radicle-ci-broker-d96861408c0dbf59622228e4b795ffe231a449be.tar.gz |
fix: update status page atomically
Signed-off-by: Lars Wirzenius <liw@liw.fi>
-rw-r--r-- | src/status.rs | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/src/status.rs b/src/status.rs index 17445c8..f79c0bb 100644 --- a/src/status.rs +++ b/src/status.rs @@ -33,9 +33,10 @@ impl Default for StatusData { impl StatusData { fn write(&self, filename: &Path) -> Result<(), StatusError> { + let tmp = filename.with_extension("update"); let s = serde_json::to_string_pretty(&self).map_err(StatusError::serialize)?; - std::fs::write(filename, s.as_bytes()) - .map_err(|e| StatusError::status_write(filename, e))?; + std::fs::write(&tmp, s.as_bytes()).map_err(|e| StatusError::status_write(filename, e))?; + std::fs::rename(&tmp, filename).map_err(|e| StatusError::status_rename(filename, e))?; Ok(()) } } @@ -103,6 +104,9 @@ pub enum StatusError { #[error("failed to write status to file {0}")] StatusWrite(PathBuf, #[source] std::io::Error), + + #[error("failed to rename status to file {0}")] + StatusRename(PathBuf, #[source] std::io::Error), } impl StatusError { @@ -117,4 +121,8 @@ impl StatusError { fn status_write(filename: &Path, err: std::io::Error) -> Self { Self::StatusWrite(filename.into(), err) } + + fn status_rename(filename: &Path, err: std::io::Error) -> Self { + Self::StatusRename(filename.into(), err) + } } |