Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
bmaptool: Add zstd compression
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
  • Loading branch information
akiernan committed Jul 3, 2019
1 parent f1f5c31 commit 1b8437d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
37 changes: 31 additions & 6 deletions bmaptools/TransRead.py
Expand Up @@ -18,10 +18,11 @@
This module allows opening and reading local and remote files and decompress
them on-the-fly if needed. Remote files are read using urllib (except of
"ssh://" URLs, which are handled differently). Supported file extentions are:
'bz2', 'gz', 'xz', 'lzo' and a "tar" version of them: 'tar.bz2', 'tbz2', 'tbz',
'tb2', 'tar.gz', 'tgz', 'tar.xz', 'txz', 'tar.lzo', 'tzo', 'tar.lz4', 'tlz4'.
'bz2', 'gz', 'xz', 'lzo', 'zst' and a "tar" version of them: 'tar.bz2', 'tbz2',
'tbz', 'tb2', 'tar.gz', 'tgz', 'tar.xz', 'txz', 'tar.lzo', 'tzo', 'tar.lz4',
'tlz4', '.tar.zst', 'tzst'.
This module uses the following system programs for decompressing: pbzip2, bzip2,
gzip, pigz, xz, lzop, lz4, tar and unzip.
gzip, pigz, xz, lzop, lz4, zstd, tar and unzip.
"""

import os
Expand Down Expand Up @@ -51,9 +52,9 @@
# pylint: disable=R0915

# A list of supported compression types
SUPPORTED_COMPRESSION_TYPES = ('bz2', 'gz', 'xz', 'lzo', 'lz4', 'tar.gz',
'tar.bz2', 'tar.xz', 'tar.lzo', 'tar.lz4',
'zip')
SUPPORTED_COMPRESSION_TYPES = ('bz2', 'gz', 'xz', 'lzo', 'lz4', 'zst',
'tar.gz', 'tar.bz2', 'tar.xz', 'tar.lzo',
'tar.lz4', 'tar.zst', 'zip')


def _fake_seek_forward(file_obj, cur_pos, offset, whence=os.SEEK_SET):
Expand Down Expand Up @@ -254,6 +255,12 @@ def is_lz4(name):
return True
return False

def is_zst(name):
"""Returns 'True' if file 'name' is compressed with 'zstd'."""
if name.endswith('.zst') and not name.endswith('.tar.zst'):
return True
return False

def is_tar_gz(name):
"""
Returns 'True' if file 'name' is a tar archive compressed with
Expand Down Expand Up @@ -304,6 +311,16 @@ def is_tar_lz4(name):
return True
return False

def is_tar_zst(name):
"""
Returns 'True' if file 'name' is a tar archive compressed with
'zstd'.
"""

if name.endswith('.tar.zst') or name.endswith('.tzst'):
return True
return False

archiver = None
if is_tar_gz(self.name) or is_gzip(self.name):
self.compression_type = 'gzip'
Expand Down Expand Up @@ -357,6 +374,14 @@ def is_tar_lz4(name):
else:
archiver = "tar"
args = "-x -Ilz4 -O"
elif is_tar_zst(self.name) or is_zst(self.name):
self.compression_type = 'zst'
decompressor = "zstd"
if is_zst(self.name):
args = "-d"
else:
archiver = "tar"
args = "-x -Izstd -O"
else:
if not self.is_url:
self.size = os.fstat(self._f_objs[-1].fileno()).st_size
Expand Down
3 changes: 2 additions & 1 deletion debian/control
Expand Up @@ -22,7 +22,8 @@ Depends: python (>=2.7),
liblz4-tool,
xz-utils,
tar,
unzip
unzip,
zstd
Description: Tools to generate block map (AKA bmap) and flash images using
bmap. Bmaptool is a generic tool for creating the block map (bmap) for a file,
and copying files using the block map. The idea is that large file containing
Expand Down
5 changes: 4 additions & 1 deletion docs/man1/bmaptool.1
Expand Up @@ -112,7 +112,10 @@ extensions are supported:
4. ".lzo", "tar.lzo", ".tzo" for files and tar archives compressed with "\fIlzo\fR" program
.RE
.RS 4
4. ".lz4", "tar.lz4", ".tlz4" for files and tar archives compressed with "\fIlz4\fR" program
5. ".lz4", "tar.lz4", ".tlz4" for files and tar archives compressed with "\fIlz4\fR" program
.RE
.RS 4
6. ".zst", "tar.zst", ".tzst" for files and tar archives compressed with "\fIzstd\fR" program
.RE

.PP
Expand Down
2 changes: 2 additions & 0 deletions tests/test_api_base.py
Expand Up @@ -91,13 +91,15 @@ def _generate_compressed_files(file_path, delete=True):
("xz", None, ".xz", "-c -k"),
("lzop", None, ".lzo", "-c -k"),
("lz4", None, ".lz4", "-c -k"),
("zstd", None, ".zst", ""),
# The "-P -C /" trick is used to avoid silly warnings:
# "tar: Removing leading `/' from member names"
("bzip2", "tar", ".tar.bz2", "-c -j -O -P -C /"),
("gzip", "tar", ".tar.gz", "-c -z -O -P -C /"),
("xz", "tar", ".tar.xz", "-c -J -O -P -C /"),
("lzop", "tar", ".tar.lzo", "-c --lzo -O -P -C /"),
("lz4", "tar", ".tar.lz4", "-c -Ilz4 -O -P -C /"),
("zstd", "tar", ".tar.zst", "-c -Izstd -O -P -C /"),
("zip", None, ".zip", "-q -j -")]

for decompressor, archiver, suffix, options in compressors:
Expand Down

0 comments on commit 1b8437d

Please sign in to comment.