From ce5719a03f37b278a00a9587ecd39b8b02ba5a74 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Tue, 4 Feb 2020 11:16:05 +0100 Subject: [PATCH] objectstore: move tree-moving code into the tree The code to move the a TreeObject more naturally belongs to the TreeObject itself and makes the ObjectStore.commit() method even easier to read. --- osbuild/objectstore.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/osbuild/objectstore.py b/osbuild/objectstore.py index 23809855..9c4dd03a 100644 --- a/osbuild/objectstore.py +++ b/osbuild/objectstore.py @@ -50,6 +50,16 @@ class TreeObject: return treesum_hash + def move(self, destination: str): + """Move the tree to destination + + Does so atomically by using rename(2). If the + target already exist, use that instead + """ + with suppress_oserror(errno.ENOTEMPTY, errno.EEXIST): + os.rename(self.path, destination) + self.path = destination + class ObjectStore: def __init__(self, store): @@ -140,13 +150,9 @@ class ObjectStore: # the tree is stored in the objects directory using its content # hash as its name, ideally a given object_id (i.e., given config) # will always produce the same content hash, but that is not - # guaranteed - output_tree = f"{self.objects}/{treesum_hash}" - - # if a tree with the same treesum already exist, use that - with suppress_oserror(errno.ENOTEMPTY, errno.EEXIST): - os.rename(tree.path, output_tree) - tree.path = output_tree + # guaranteed. If a tree with the same treesum already exist, us + # the existing one instead + tree.move(f"{self.objects}/{treesum_hash}") # symlink the object_id (config hash) in the refs directory to the # treesum (content hash) in the objects directory. If a symlink by