fix(backup): work with temporary file while writing json metadata file
This commit is contained in:
parent
08e77034a7
commit
3ae19e2168
2 changed files with 20 additions and 1 deletions
|
|
@ -122,10 +122,24 @@ where
|
|||
}
|
||||
|
||||
/// Write the in-memory state to disk.
|
||||
///
|
||||
/// The state is first written to a temporary file before being (atomically, depending on the
|
||||
/// file system) renamed to the final path.
|
||||
pub fn save(&self) -> io::Result<()> {
|
||||
let json_file = File::create(self.backup_dir.join(Self::METADATA_FILE))?;
|
||||
let dest_path = self.backup_dir.join(Self::METADATA_FILE);
|
||||
|
||||
let dest_ext = dest_path
|
||||
.extension()
|
||||
.map(|ext| ext.to_string_lossy().to_string())
|
||||
.unwrap_or(String::new());
|
||||
let temp_path = dest_path.with_extension(format!("{dest_ext}.temp"));
|
||||
|
||||
let json_file = File::create(&temp_path)?;
|
||||
serde_json::to_writer(json_file, &self.chains)?;
|
||||
|
||||
// Rename temp file to the destination path after writing was successful
|
||||
std::fs::rename(temp_path, dest_path)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue