mirror of
https://seed.radicle.dev/rad:zwTxygwuz5LDGBq255RA2CbNGrz8.git
synced 2026-11-16 00:30:35 +08:00
fix: update status page atomically
Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
+10
-2
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user