Merge pull request #2358 from ThomasWaldmann/refactor-extract-1.0

extract: small bugfix and refactoring for parent dir creation
This commit is contained in:
enkore 2017-04-01 20:49:36 +02:00 committed by GitHub
commit 50b4c9f2f9
1 changed files with 13 additions and 12 deletions

View File

@ -389,17 +389,20 @@ Number of files: {0.stats.nfiles}'''.format(
raise self.IncompatibleFilesystemEncodingError(path, sys.getfilesystemencoding()) from None raise self.IncompatibleFilesystemEncodingError(path, sys.getfilesystemencoding()) from None
except OSError: except OSError:
pass 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'] mode = item[b'mode']
if stat.S_ISREG(mode): if stat.S_ISREG(mode):
if not os.path.exists(os.path.dirname(path)): with backup_io():
with backup_io(): make_parent(path)
os.makedirs(os.path.dirname(path))
# Hard link? # Hard link?
if b'source' in item: if b'source' in item:
source = os.path.join(dest, item[b'source']) source = os.path.join(dest, item[b'source'])
with backup_io(): with backup_io():
if os.path.exists(path):
os.unlink(path)
os.link(source, path) os.link(source, path)
else: else:
with backup_io(): with backup_io():
@ -425,27 +428,25 @@ Number of files: {0.stats.nfiles}'''.format(
with backup_io(): with backup_io():
# No repository access beyond this point. # No repository access beyond this point.
if stat.S_ISDIR(mode): if stat.S_ISDIR(mode):
make_parent(path)
if not os.path.exists(path): if not os.path.exists(path):
os.makedirs(path) os.mkdir(path)
if restore_attrs: if restore_attrs:
self.restore_attrs(path, item) self.restore_attrs(path, item)
elif stat.S_ISLNK(mode): elif stat.S_ISLNK(mode):
if not os.path.exists(os.path.dirname(path)): make_parent(path)
os.makedirs(os.path.dirname(path))
source = item[b'source'] source = item[b'source']
if os.path.exists(path):
os.unlink(path)
try: try:
os.symlink(source, path) os.symlink(source, path)
except UnicodeEncodeError: except UnicodeEncodeError:
raise self.IncompatibleFilesystemEncodingError(source, sys.getfilesystemencoding()) from None raise self.IncompatibleFilesystemEncodingError(source, sys.getfilesystemencoding()) from None
self.restore_attrs(path, item, symlink=True) self.restore_attrs(path, item, symlink=True)
elif stat.S_ISFIFO(mode): elif stat.S_ISFIFO(mode):
if not os.path.exists(os.path.dirname(path)): make_parent(path)
os.makedirs(os.path.dirname(path))
os.mkfifo(path) os.mkfifo(path)
self.restore_attrs(path, item) self.restore_attrs(path, item)
elif stat.S_ISCHR(mode) or stat.S_ISBLK(mode): elif stat.S_ISCHR(mode) or stat.S_ISBLK(mode):
make_parent(path)
os.mknod(path, item[b'mode'], item[b'rdev']) os.mknod(path, item[b'mode'], item[b'rdev'])
self.restore_attrs(path, item) self.restore_attrs(path, item)
else: else: