From aee25c35641797d51a8fdc45b4a566bf45ef638a Mon Sep 17 00:00:00 2001 From: Ivan Shapovalov Date: Tue, 13 Sep 2022 17:32:25 +0300 Subject: [PATCH] extract_item: do not delete an existing directory if possible A pre-existing directory might be a Btrfs subvolume that was created by the user ahead of time when restoring several nested subvolumes from a single archive. --- src/borg/archive.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/borg/archive.py b/src/borg/archive.py index b048d5c160..f314221960 100644 --- a/src/borg/archive.py +++ b/src/borg/archive.py @@ -858,10 +858,13 @@ def same_item(item, st): st = os.stat(path, follow_symlinks=False) if continue_extraction and same_item(item, st): return # done! we already have fully extracted this file in a previous run. - elif stat.S_ISDIR(st.st_mode): - os.rmdir(path) - else: + # remove anything that is not a directory + if not stat.S_ISDIR(st.st_mode): os.unlink(path) + # only remove a directory if it is conflicting + # preserve existing directories because they might be subvolumes + elif not stat.S_ISDIR(item.mode): + os.rmdir(path) except UnicodeEncodeError: raise self.IncompatibleFilesystemEncodingError(path, sys.getfilesystemencoding()) from None except OSError: