1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-15 16:40:23 +00:00

Implement fail-safe error handling for borg-extract

Note that this isn't nearly as critical as the other error handling bug,
since nothing is written. So it's "merely" misleading error reporting.
This commit is contained in:
Marian Beermann 2016-07-01 00:14:10 +02:00
parent 5e260fdfda
commit b241f95a4b
2 changed files with 54 additions and 40 deletions

View file

@ -356,54 +356,68 @@ Number of files: {0.stats.nfiles}'''.format(
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)): if not os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path)) with backup_io():
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'])
if os.path.exists(path): with backup_io():
os.unlink(path) if os.path.exists(path):
os.link(source, path) os.unlink(path)
os.link(source, path)
else: else:
with open(path, 'wb') as fd: with backup_io():
fd = open(path, 'wb')
with fd:
ids = [c[0] for c in item[b'chunks']] ids = [c[0] for c in item[b'chunks']]
for data in self.pipeline.fetch_many(ids, is_preloaded=True): for data in self.pipeline.fetch_many(ids, is_preloaded=True):
if sparse and self.zeros.startswith(data): with backup_io():
# all-zero chunk: create a hole in a sparse file if sparse and self.zeros.startswith(data):
fd.seek(len(data), 1) # all-zero chunk: create a hole in a sparse file
else: fd.seek(len(data), 1)
fd.write(data) else:
pos = fd.tell() fd.write(data)
fd.truncate(pos) with backup_io():
fd.flush() pos = fd.tell()
self.restore_attrs(path, item, fd=fd.fileno()) fd.truncate(pos)
elif stat.S_ISDIR(mode): fd.flush()
if not os.path.exists(path): self.restore_attrs(path, item, fd=fd.fileno())
os.makedirs(path) return
if restore_attrs: with backup_io():
# No repository access beyond this point.
if stat.S_ISDIR(mode):
if not os.path.exists(path):
os.makedirs(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))
source = item[b'source']
if os.path.exists(path):
os.unlink(path)
try:
os.symlink(source, path)
except UnicodeEncodeError:
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))
os.mkfifo(path)
self.restore_attrs(path, item) self.restore_attrs(path, item)
elif stat.S_ISLNK(mode): elif stat.S_ISCHR(mode) or stat.S_ISBLK(mode):
if not os.path.exists(os.path.dirname(path)): os.mknod(path, item[b'mode'], item[b'rdev'])
os.makedirs(os.path.dirname(path)) self.restore_attrs(path, item)
source = item[b'source'] else:
if os.path.exists(path): raise Exception('Unknown archive item type %r' % item[b'mode'])
os.unlink(path)
try:
os.symlink(source, path)
except UnicodeEncodeError:
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))
os.mkfifo(path)
self.restore_attrs(path, item)
elif stat.S_ISCHR(mode) or stat.S_ISBLK(mode):
os.mknod(path, item[b'mode'], item[b'rdev'])
self.restore_attrs(path, item)
else:
raise Exception('Unknown archive item type %r' % item[b'mode'])
def restore_attrs(self, path, item, symlink=False, fd=None): def restore_attrs(self, path, item, symlink=False, fd=None):
"""
Restore filesystem attributes on *path* from *item* (*fd*).
Does not access the repository.
"""
uid = gid = None uid = gid = None
if not self.numeric_owner: if not self.numeric_owner:
uid = user2uid(item[b'user']) uid = user2uid(item[b'user'])

View file

@ -384,7 +384,7 @@ class Archiver:
archive.extract_item(item, restore_attrs=False) archive.extract_item(item, restore_attrs=False)
else: else:
archive.extract_item(item, stdout=stdout, sparse=sparse) archive.extract_item(item, stdout=stdout, sparse=sparse)
except OSError as e: except BackupOSError as e:
self.print_warning('%s: %s', remove_surrogates(orig_path), e) self.print_warning('%s: %s', remove_surrogates(orig_path), e)
if not args.dry_run: if not args.dry_run: