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.
This commit is contained in:
Thomas Waldmann 2017-03-28 23:22:25 +02:00
parent ceaf4a8fcf
commit d4e27e2952
1 changed files with 12 additions and 7 deletions

View File

@ -559,11 +559,16 @@ Utilization of max. archive size: {csize_max:.0%}
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.mode
if stat.S_ISREG(mode):
with backup_io('makedirs'):
if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
make_parent(path)
# Hard link?
if 'source' in item:
source = os.path.join(dest, *item.source.split(os.sep)[stripped_components:])
@ -615,13 +620,13 @@ Utilization of max. archive size: {csize_max:.0%}
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.source
if os.path.exists(path):
os.unlink(path)
@ -631,11 +636,11 @@ Utilization of max. archive size: {csize_max:.0%}
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.mode, item.rdev)
self.restore_attrs(path, item)
else: