From c84895a5ebada96af5e430593d306cd2880cd259 Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Wed, 9 Jun 2021 23:02:21 +0200 Subject: [PATCH] dedup get_tar_filter code also: - check for short tarfile extensions - add .lz4 and .zstd - fix docs about extensions and decompression commands --- src/borg/archiver.py | 42 +++++++++++++--------------------------- src/borg/helpers/misc.py | 18 +++++++++++++++++ 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/borg/archiver.py b/src/borg/archiver.py index 8f82da2d8..1dd4f1142 100644 --- a/src/borg/archiver.py +++ b/src/borg/archiver.py @@ -75,6 +75,7 @@ try: from .helpers import msgpack from .helpers import sig_int from .helpers import iter_separated + from .helpers import get_tar_filter from .nanorst import rst_to_terminal from .patterns import ArgparsePatternAction, ArgparseExcludeFileAction, ArgparsePatternFileAction, parse_exclude_pattern from .patterns import PatternMatcher @@ -951,18 +952,7 @@ class Archiver: # that it has to be installed -- hardly a problem, considering that # the decompressor must be installed as well to make use of the exported tarball! - filter = None - if args.tar_filter == 'auto': - # Note that filter remains None if tarfile is '-'. - if args.tarfile.endswith('.tar.gz'): - filter = 'gzip' - elif args.tarfile.endswith('.tar.bz2'): - filter = 'bzip2' - elif args.tarfile.endswith('.tar.xz'): - filter = 'xz' - logger.debug('Automatically determined tar filter: %s', filter) - else: - filter = args.tar_filter + filter = get_tar_filter(args.tarfile) if args.tar_filter == 'auto' else args.tar_filter tarstream = dash_open(args.tarfile, 'wb') tarstream_close = args.tarfile != '-' @@ -1705,17 +1695,7 @@ class Archiver: self.output_filter = args.output_filter self.output_list = args.output_list - filter = None - if args.tar_filter == 'auto' and args.tarfile != '-': - if args.tarfile.endswith('.tar.gz'): - filter = 'gunzip' - elif args.tarfile.endswith('.tar.bz2'): - filter = 'bunzip2' - elif args.tarfile.endswith('.tar.xz'): - filter = 'unxz' - logger.debug('Automatically determined tar filter: %s', filter) - else: - filter = args.tar_filter + filter = get_tar_filter(args.tarfile) if args.tar_filter == 'auto' else args.tar_filter tarstream = dash_open(args.tarfile, 'rb') tarstream_close = args.tarfile != '-' @@ -3861,9 +3841,11 @@ class Archiver: based on its file extension and pipe the tarball through an appropriate filter before writing it to FILE: - - .tar.gz: gzip - - .tar.bz2: bzip2 - - .tar.xz: xz + - .tar.gz or .tgz: gzip + - .tar.bz2 or .tbz: bzip2 + - .tar.xz or .txz: xz + - .tar.zstd: zstd + - .tar.lz4: lz4 Alternatively, a ``--tar-filter`` program may be explicitly specified. It should read the uncompressed tar stream from stdin and write a compressed/filtered @@ -4733,9 +4715,11 @@ class Archiver: By default (--tar-filter=auto) Borg will detect whether the file is compressed based on its file extension and pipe the file through an appropriate filter: - - .tar.gz: gunzip - - .tar.bz2: bunzip2 - - .tar.xz: unxz + - .tar.gz or .tgz: gzip -d + - .tar.bz2 or .tbz: bzip2 -d + - .tar.xz or .txz: xz -d + - .tar.zstd: zstd -d + - .tar.lz4: lz4 -d Alternatively, a --tar-filter program may be explicitly specified. It should read compressed data from stdin and output an uncompressed tar stream on diff --git a/src/borg/helpers/misc.py b/src/borg/helpers/misc.py index ea8655ab5..573ab0132 100644 --- a/src/borg/helpers/misc.py +++ b/src/borg/helpers/misc.py @@ -238,3 +238,21 @@ def iter_separated(fd, sep=None, read_size=4096): # or if there was no data before EOF if len(part) > 0: yield part + + +def get_tar_filter(fname): + # Note that filter is None if fname is '-'. + if fname.endswith(('.tar.gz', '.tgz')): + filter = 'gzip -d' if decompress else 'gzip' + elif fname.endswith(('.tar.bz2', '.tbz')): + filter = 'bzip2 -d' if decompress else 'bzip2' + elif fname.endswith(('.tar.xz', '.txz')): + filter = 'xz -d' if decompress else 'xz' + elif fname.endswith(('.tar.lz4', )): + filter = 'lz4 -d' if decompress else 'lz4' + elif fname.endswith(('.tar.zstd', )): + filter = 'zstd -d' if decompress else 'zstd' + else: + filter = None + logger.debug('Automatically determined tar filter: %s', filter) + return filter