From e58c6b7c2705d7a4d3bb70bfdb90399617c9fb1c Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Mon, 1 Apr 2019 10:41:01 -0700 Subject: [PATCH] Make Hierarchy.write_metadata() an atomic operation --- rbackup/hierarchy/hierarchy.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/rbackup/hierarchy/hierarchy.py b/rbackup/hierarchy/hierarchy.py index 9e84fe6..fab943d 100644 --- a/rbackup/hierarchy/hierarchy.py +++ b/rbackup/hierarchy/hierarchy.py @@ -100,11 +100,18 @@ class Hierarchy: return pickle.load(mfile) def write_metadata(self): - """Write this repository's metadata to its file.""" + """Write this repository's metadata to its file. + Note that this write operation is atomic to the caller. + """ syslog.debug(f"Writing metadata to {self.metadata_path}") - with self.metadata_path.open(mode=METADATA_WRITE) as mfile: + + tmpfile = self.metadata_path.with_suffix('.tmp') + + with tmpfile.open(mode=METADATA_WRITE) as mfile: pickle.dump(self._data, mfile) + tmpfile.rename(self.metadata_path) + # ========== Functions ========== if __name__ == "__main__":