Skip to content

Commit

Permalink
fix: use atomic operation to write cursor (#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega authored Jul 6, 2024
1 parent ef40ed1 commit d7db1d2
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/utils/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ impl CanStore for FileStorage {
}

fn write_cursor(&self, point: PointArg) -> Result<(), Error> {
std::fs::write(&self.0.path, point.to_string().as_bytes())?;
// we save to a tmp file and then rename to make it an atomic operation. If the
// write were to fail, the only affected file will be the temporal one.
let tmp_file = format!("{}.tmp", self.0.path);
std::fs::write(&tmp_file, point.to_string().as_bytes())?;
std::fs::rename(&tmp_file, &self.0.path)?;

Ok(())
}
Expand Down

0 comments on commit d7db1d2

Please sign in to comment.