prefer symbolic constants over literals referring to gzip header
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 6 Apr 2017 13:09:10 +0000 (15:09 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
The mixed use of hex and octal is pretty confusing to say the
least, use named constants instead that are defined only in
tarfile.

deltatar/tarfile.py
rescue_tar.py
testing/test_concat_compress.py

index bf1f03a..4143821 100644 (file)
@@ -124,6 +124,9 @@ GZ_METHOD_DEFLATE    = 0x08 # 0o10
 GZ_FLAG_ORIG_NAME    = 0x08 # 0o10, default in gzip
 GZ_DEFLATE_FLAGS     = 0x00 # 0o00, never read (deflate.c)
 GZ_OS_CODE           = 0x03 # 0o03, default in gzip (tailor.h)
+GZ_MAGIC_BYTES       = struct.pack ("<BB", GZ_MAGIC [0], GZ_MAGIC [1])
+GZ_MAGIC_DEFLATE     = struct.pack ("<BBB", GZ_MAGIC [0], GZ_MAGIC [1],
+                                    GZ_METHOD_DEFLATE)
 
 #---------------------------------------------------------
 # tarfile constants
@@ -378,7 +381,6 @@ class _LowLevelFile:
         self.offset = pos
 
 
-
 def gz_header (name=None):
     timestamp = int(time.time())
     flags     = 0x0
@@ -404,6 +406,7 @@ def gz_header (name=None):
 
     return hdr + name
 
+
 class _Stream:
     """Class that serves as an adapter between TarFile and
        a stream-like object.  The stream-like object only
@@ -733,7 +736,7 @@ class _Stream:
 
         # taken from gzip.GzipFile with some alterations
         read2 = self.__read(2)
-        if read2 != b"\037\213":
+        if read2 != GZ_MAGIC_BYTES:
             raise ReadError("not a gzip file")
 
         read1 = self.__read(1)
@@ -963,7 +966,7 @@ class _StreamProxy(object):
         return self.buf
 
     def getcomptype(self):
-        if self.buf.startswith(b"\x1f\x8b\x08"):
+        if self.buf.startswith(GZ_MAGIC_DEFLATE):
             return "gz"
         elif self.buf[0:3] == b"BZh" and self.buf[4:10] == b"1AY&SY":
             return "bz2"
index 7ff1f55..6ddd83a 100644 (file)
@@ -76,7 +76,7 @@ def rescue(tar_files, rescue_dir=None):
 
         # split in compressed chunks
         for f in tar_files:
-            filesplit.split_file(b'\x1f\x8b',
+            filesplit.split_file(tarfile.GZ_MAGIC_BYTES,
                 os.path.join(rescue_dir, base_name), f, new_gz)
 
     # includes volumes already extracted with new_volume_handler
index ea425af..56f58e0 100644 (file)
@@ -17,7 +17,7 @@
 
 import os, unittest, hashlib, string
 
-from deltatar.tarfile import TarFile, GNU_FORMAT
+from deltatar.tarfile import TarFile, GNU_FORMAT, GZ_MAGIC_BYTES
 
 import filesplit
 from . import BaseTest, new_volume_handler
@@ -337,7 +337,7 @@ class ConcatCompressTest(BaseTest):
         os.unlink("small")
         os.unlink("small2")
 
-        filesplit.split_file(b'\x1f\x8b', "sample.tar.gz.", "sample.tar.gz")
+        filesplit.split_file(GZ_MAGIC_BYTES, "sample.tar.gz.", "sample.tar.gz")
 
         assert os.path.exists("sample.tar.gz.0") # beginning of the tar file
         assert os.path.exists("sample.tar.gz.1") # first file
@@ -439,7 +439,7 @@ class ConcatCompressTest(BaseTest):
         os.unlink("small2")
 
         # equivalent to $ python filesplit.py -s $'\x1f\x8b' -p sample.tar.gz. sample.tar.gz
-        filesplit.split_file(b'\x1f\x8b', "sample.tar.gz.", "sample.tar.gz")
+        filesplit.split_file(GZ_MAGIC_BYTES, "sample.tar.gz.", "sample.tar.gz")
 
         assert os.path.exists("sample.tar.gz.0") # beginning of the tar file
         assert os.path.exists("sample.tar.gz.1") # first file