Skip to content

Commit

Permalink
Merge pull request borgbackup#3252 from ThomasWaldmann/fix-broken-pip…
Browse files Browse the repository at this point in the history
…e-handling

borg list: fix broken pipe handling, fixes borgbackup#3245
  • Loading branch information
ThomasWaldmann authored Nov 4, 2017
2 parents 4cbcd98 + 726ef11 commit c90a39e
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/borg/archiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,26 +1148,18 @@ def do_umount(self, args):
@with_repository(compatibility=(Manifest.Operation.READ,))
def do_list(self, args, repository, manifest, key):
"""List archive or repository contents"""
if not hasattr(sys.stdout, 'buffer'):
# This is a shim for supporting unit tests replacing sys.stdout with e.g. StringIO,
# which doesn't have an underlying buffer (= lower file object).
def write(bytestring):
sys.stdout.write(bytestring.decode('utf-8', errors='replace'))
else:
write = sys.stdout.buffer.write

if args.location.archive:
if args.json:
self.print_error('The --json option is only valid for listing archives, not archive contents.')
return self.exit_code
return self._list_archive(args, repository, manifest, key, write)
return self._list_archive(args, repository, manifest, key)
else:
if args.json_lines:
self.print_error('The --json-lines option is only valid for listing archive contents, not archives.')
return self.exit_code
return self._list_repository(args, repository, manifest, key, write)
return self._list_repository(args, repository, manifest, key)

def _list_archive(self, args, repository, manifest, key, write):
def _list_archive(self, args, repository, manifest, key):
matcher = self.build_matcher(args.patterns, args.paths)
if args.format is not None:
format = args.format
Expand All @@ -1182,7 +1174,7 @@ def _list_inner(cache):

formatter = ItemFormatter(archive, format, json_lines=args.json_lines)
for item in archive.iter_items(lambda item: matcher.match(item.path)):
write(safe_encode(formatter.format_item(item)))
sys.stdout.write(formatter.format_item(item))

# Only load the cache if it will be used
if ItemFormatter.format_needs_cache(format):
Expand All @@ -1193,7 +1185,7 @@ def _list_inner(cache):

return self.exit_code

def _list_repository(self, args, repository, manifest, key, write):
def _list_repository(self, args, repository, manifest, key):
if args.format is not None:
format = args.format
elif args.short:
Expand All @@ -1208,7 +1200,7 @@ def _list_repository(self, args, repository, manifest, key, write):
if args.json:
output_data.append(formatter.get_item_data(archive_info))
else:
write(safe_encode(formatter.format_item(archive_info)))
sys.stdout.write(formatter.format_item(archive_info))

if args.json:
json_print(basic_json_data(manifest, extra={
Expand Down

0 comments on commit c90a39e

Please sign in to comment.