fix multivol compression handling
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 27 Apr 2017 14:03:46 +0000 (16:03 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
deltatar/tarfile.py

index 7e8ef9c..cf1e24c 100644 (file)
@@ -465,7 +465,7 @@ class _Stream:
                     self.exception = zlib.error
                     self._init_read_gz()
                 elif mode == "w":
-                    if self.encryption is None and concat_stream is False:
+                    if concat_stream is False and self.encryption is None:
                         self._init_write_gz ()
                 self.crc = zlib.crc32(b"") & 0xFFFFffff
 
@@ -699,6 +699,7 @@ class _Stream:
                     raise CompressionError("bad gzip crc")
         self.closed = True
 
+
     def _init_read_gz(self):
         """Initialize for reading a gzip compressed fileobj.
         """
@@ -732,7 +733,6 @@ class _Stream:
         if flag & 2:
             self.__read(2)
 
-
     def _init_read_encrypt (self):
         """Initialize encryption for next entry in archive. Read a header and
         notify the crypto context."""
@@ -847,8 +847,8 @@ class _Stream:
             if self.cmp is not None:
                 try:
                     buf = self.cmp.decompress(buf)
-                except self.exception:
-                    raise ReadError("invalid compressed data")
+                except self.exception as exn:
+                    raise ReadError("invalid compressed data (%r)" % exn)
                 except Exception as e:
                     # happens at the end of the file
                     # _init_read_gz failed in the previous iteration so
@@ -2428,7 +2428,8 @@ class TarFile(object):
 
         tarinfo = copy.copy(tarinfo)
 
-        if self.concat_compression is True:
+        if self.concat_compression is True and \
+                getattr (self.fileobj, "cmp", None) is not None:
             self.fileobj._finalize_write_gz ()
 
         if getattr (self.fileobj, "encryption", None) is not None:
@@ -2497,6 +2498,7 @@ class TarFile(object):
                     raise Exception("We need to create a new volume and you "
                                     "didn't supply a new_volume_handler")
 
+
                 # the new volume handler should do everything needed to
                 # start working in a new volume. usually, the handler calls
                 # to self.open_volume
@@ -2511,7 +2513,8 @@ class TarFile(object):
                 # the “new_volume_handler” is supposed to call .close() on the
                 # “fileobj” _Stream
                 self.new_volume_handler(self, self.base_name, self.volume_number)
-                if getattr (self.fileobj, "cmp", None) is not None:
+                if self.concat_compression and \
+                        getattr (self.fileobj, "cmp", None) is not None:
                     # e. g. compressed PAX header written
                     self.fileobj._finalize_write_gz ()
 
@@ -2519,8 +2522,7 @@ class TarFile(object):
 
                 if getattr (self.fileobj, "encryption", None) is not None:
                     self.fileobj._init_write_encrypt (tarinfo.name)
-                if self.concat_compression or \
-                        getattr (self.fileobj, "cmp", None) is not None:
+                if self.concat_compression is True:
                     self.fileobj._init_write_gz ()
 
                 # write new volume header
@@ -2624,7 +2626,7 @@ class TarFile(object):
                     buf = self.tarinfo.create_pax_global_header(volume_info.copy())
                     self.fileobj.write(buf)
                     self.offset += len(buf)
-        except:
+        except Exception as exn:
             if not self._extfileobj:
                 self.fileobj.close()
             self.closed = True