objectstore: clarify ENOTEMPTY handling

This commit is contained in:
Lars Karlitski 2019-09-21 14:49:02 +02:00
parent 56a25adf7f
commit f1151a1719

View file

@ -13,6 +13,19 @@ __all__ = [
]
@contextlib.contextmanager
def suppress_oserror(*errnos):
"""A context manager that suppresses any OSError with an errno in `errnos`.
Like contextlib.suppress, but can differentiate between OSErrors.
"""
try:
yield
except OSError as e:
if e.errno not in errnos:
raise e
class ObjectStore:
def __init__(self, store):
self.store = store
@ -72,13 +85,11 @@ class ObjectStore:
# will always produce the same content hash, but that is not
# guaranteed
output_tree = f"{self.objects}/{treesum_hash}"
try:
# if a tree with the same treesum already exist, use that
with suppress_oserror(errno.ENOTEMPTY):
os.rename(tree, output_tree)
except OSError as e:
if e.errno == errno.ENOTEMPTY:
pass # tree with the same content hash already exist, use that
else:
raise
# symlink the tree_id (config hash) in the refs directory to the treesum
# (content hash) in the objects directory. If a symlink by that name
# alreday exists, atomically replace it, but leave the backing object