Skip to content

Commit

Permalink
mtda: add support for xz compressed images
Browse files Browse the repository at this point in the history
Based on bz2 logic.

Closes: siemens#121

Signed-off-by: Vijai Kumar K <Vijaikumar_Kanagarajan@mentor.com>
  • Loading branch information
vj-kumar committed Jan 12, 2022
1 parent 7500c66 commit 9349ebf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mtda/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def storage_write_image(self, path, callback=None):
compression = CONSTS.IMAGE.GZ.value
elif path.endswith(".zst"):
compression = CONSTS.IMAGE.ZST.value
elif path.endswith(".xz"):
compression = CONSTS.IMAGE.XZ.value
else:
compression = CONSTS.IMAGE.RAW.value
self._impl.storage_compression(compression, self._session)
Expand Down
1 change: 1 addition & 0 deletions mtda/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class IMAGE(Enum):
BZ2 = 1
GZ = 2
ZST = 3
XZ = 4


class MDNS:
Expand Down
29 changes: 29 additions & 0 deletions mtda/storage/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import mtda.constants as CONSTS
import zlib
import zstandard as zstd
import lzma


class AsyncImageWriter(queue.Queue):
Expand Down Expand Up @@ -55,6 +56,8 @@ def compression(self, compression):
self._write = self.write_gz
elif compression == CONSTS.IMAGE.ZST:
self._write = self.write_zst
elif compression == CONSTS.IMAGE.XZ:
self._write = self.write_xz
else:
raise ValueError("unsupported image compression!")
self._compression = compression
Expand Down Expand Up @@ -213,6 +216,32 @@ def write_zst(self, data):
self.mtda.debug(3, "storage.writer.write_zst(): %s" % str(result))
return result

def write_xz(self, data):
self.mtda.debug(3, "storage.writer.write_xz()")

# Create a xz decompressor when called for the first time
if self._zdec is None:
self._zdec = lzma.LZMADecompressor()

try:
cont = True
result = None
while cont is True:
uncompressed = self._zdec.decompress(data, self._blksz)
result = self.storage.write(uncompressed)
self._written += result if result is not None else 0
cont = self._zdec.needs_input is False
data = b''
except EOFError:
result = 0
except OSError as e:
self.mtda.debug(1, "storage.writer.write_xz(): "
"%s" % str(e.args[0]))
self._failed = True

self.mtda.debug(3, "storage.writer.write_xz(): %s" % str(result))
return result

@property
def writing(self):
return self._writing
Expand Down

0 comments on commit 9349ebf

Please sign in to comment.