Make Hierarchy.write_metadata() an atomic operation

This commit is contained in:
Eric Torres 2019-04-01 10:41:01 -07:00
parent 4361ad703a
commit e58c6b7c27

View File

@ -100,11 +100,18 @@ class Hierarchy:
return pickle.load(mfile) return pickle.load(mfile)
def write_metadata(self): 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}") 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) pickle.dump(self._data, mfile)
tmpfile.rename(self.metadata_path)
# ========== Functions ========== # ========== Functions ==========
if __name__ == "__main__": if __name__ == "__main__":