From 5deb1be514609196cd8aa9dfa1039759a8cb64bd Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Mon, 17 Feb 2020 16:57:44 +0100 Subject: [PATCH] objectstore: change Object.move to .store_tree Now that Object manages its work directory itself, re-create the latter when the its tree is moved, i.e. when the object is being committed to the store. This means that after the object has been written to the store it is in the same state is if it was new and can be used in the very same way. If the move itself fails (the rename(2) fails), the tree and its contents is cleaned up with the reset of the work directory. Rename the `move` method to `store_tree` to better reflect how the method should be used, i.e. to store the tree corresponding to the Object instance. --- osbuild/objectstore.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/osbuild/objectstore.py b/osbuild/objectstore.py index 360578d3..5b7b55ef 100644 --- a/osbuild/objectstore.py +++ b/osbuild/objectstore.py @@ -62,15 +62,16 @@ class Object: finally: os.close(fd) - def move(self, destination: str): - """Move the object to destination + def store_tree(self, destination: str): + """Store the tree at destination and reset itself - Does so atomically by using rename(2). If the - target already exist, use that instead + Moves the tree atomically by using rename(2). If the + target already exist, does nothing. Afterwards it + resets itself and can be used as if it was new. """ with suppress_oserror(errno.ENOTEMPTY, errno.EEXIST): os.rename(self.path, destination) - self._tree = destination + self.reset() def reset(self): self.cleanup() @@ -194,7 +195,7 @@ class ObjectStore: # will always produce the same content hash, but that is not # guaranteed. If an object with the same treesum already exist, us # the existing one instead - obj.move(f"{self.objects}/{treesum_hash}") + obj.store_tree(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