From ff4f04e5f1b761c090943303a53fb51522a305c8 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 28 Mar 2017 23:22:25 +0200 Subject: [PATCH 1/2] extract: small bugfix and refactoring for parent dir creation make_parent(path) helper to reduce code duplication. also use it for directories although makedirs can also do it. bugfix: also create parent dir for device files, if needed. (cherry picked from commit d4e27e2952888d547c54d5b0f4be22892aa9e2f1) --- borg/archive.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/borg/archive.py b/borg/archive.py index 91a15019d..d37a1ce01 100644 --- a/borg/archive.py +++ b/borg/archive.py @@ -389,11 +389,16 @@ Number of files: {0.stats.nfiles}'''.format( raise self.IncompatibleFilesystemEncodingError(path, sys.getfilesystemencoding()) from None except OSError: pass + + def make_parent(path): + parent_dir = os.path.dirname(path) + if not os.path.exists(parent_dir): + os.makedirs(parent_dir) + mode = item[b'mode'] if stat.S_ISREG(mode): - if not os.path.exists(os.path.dirname(path)): - with backup_io(): - os.makedirs(os.path.dirname(path)) + with backup_io(): + make_parent(path) # Hard link? if b'source' in item: source = os.path.join(dest, item[b'source']) @@ -425,13 +430,13 @@ Number of files: {0.stats.nfiles}'''.format( with backup_io(): # No repository access beyond this point. if stat.S_ISDIR(mode): + make_parent(path) if not os.path.exists(path): - os.makedirs(path) + os.mkdir(path) if restore_attrs: self.restore_attrs(path, item) elif stat.S_ISLNK(mode): - if not os.path.exists(os.path.dirname(path)): - os.makedirs(os.path.dirname(path)) + make_parent(path) source = item[b'source'] if os.path.exists(path): os.unlink(path) @@ -441,11 +446,11 @@ Number of files: {0.stats.nfiles}'''.format( raise self.IncompatibleFilesystemEncodingError(source, sys.getfilesystemencoding()) from None self.restore_attrs(path, item, symlink=True) elif stat.S_ISFIFO(mode): - if not os.path.exists(os.path.dirname(path)): - os.makedirs(os.path.dirname(path)) + make_parent(path) os.mkfifo(path) self.restore_attrs(path, item) elif stat.S_ISCHR(mode) or stat.S_ISBLK(mode): + make_parent(path) os.mknod(path, item[b'mode'], item[b'rdev']) self.restore_attrs(path, item) else: From f1bc2076a758ef1c1ce86a8cfa302a2b42f39dac Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Sat, 1 Apr 2017 17:05:38 +0200 Subject: [PATCH 2/2] extract: remove duplicate code anything at gets nuked already a few lines above, if possible. --- borg/archive.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/borg/archive.py b/borg/archive.py index d37a1ce01..178ad94b8 100644 --- a/borg/archive.py +++ b/borg/archive.py @@ -403,8 +403,6 @@ Number of files: {0.stats.nfiles}'''.format( if b'source' in item: source = os.path.join(dest, item[b'source']) with backup_io(): - if os.path.exists(path): - os.unlink(path) os.link(source, path) else: with backup_io(): @@ -438,8 +436,6 @@ Number of files: {0.stats.nfiles}'''.format( elif stat.S_ISLNK(mode): make_parent(path) source = item[b'source'] - if os.path.exists(path): - os.unlink(path) try: os.symlink(source, path) except UnicodeEncodeError: