properly enter/leave encryption sections
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 27 Mar 2017 15:22:13 +0000 (17:22 +0200)
committerPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 27 Mar 2017 15:42:21 +0000 (17:42 +0200)
deltatar/tarfile.py

index 3576879..1d84365 100644 (file)
@@ -765,8 +765,7 @@ class _Stream:
         if self.mode == "w":
             self._finalize_write_gz ()
             self.__enc_write(self.buf)
-            if self.encryption is not None:
-                self._finalize_write_encrypt ()
+            self._finalize_write_encrypt ()
 
         if close_fileobj and not self._extfileobj:
             self.fileobj.close()
@@ -2540,29 +2539,23 @@ class TarFile(object):
            You can create TarInfo objects using gettarinfo().
            On Windows platforms, `fileobj' should always be opened with mode
            'rb' to avoid irritation about the file size.
+
+           Encryption restarts with each volume. Compression restarts with each
+           file and may span volumes.
         """
         self._check("aw")
 
         tarinfo = copy.copy(tarinfo)
+
+        self.fileobj._finalize_write_encrypt ()
+        self.fileobj._init_write_encrypt (tarinfo.name)
+
         if self.concat_compression:
             self.fileobj.new_compression_block()
             self.last_block_offset = self.fileobj.last_block_offset
         else:
             self.last_block_offset = self.fileobj.tell()
 
-        # below attributes aren’t present with other compression methods
-        init_e     = getattr (self.fileobj, "_init_write_encrypt",     None)
-        finalize_e = getattr (self.fileobj, "_finalize_write_encrypt", None)
-
-        def new_item_hook (): # crypto is outer, compress is inner
-            if init_e is not None: init_e (tarinfo.name)
-
-        def end_item_hook (): # crypto is outer, compress is inner
-            if finalize_e is not None: finalize_e ()
-
-        end_item_hook () # finalize current object
-        new_item_hook ()
-
         buf = tarinfo.tobuf(self.format, self.encoding, self.errors)
         self.fileobj.write(buf)
         self.offset += len(buf)
@@ -2577,7 +2570,6 @@ class TarFile(object):
 
         # If there's no data to follow, finish
         if not fileobj:
-            new_item_hook ()
             if self.save_to_members:
                 self.members.append(tarinfo)
             return
@@ -2613,7 +2605,7 @@ class TarFile(object):
                 # Only finalize the crypto entry here if we’re continuing with
                 # another one; otherwise, the encryption must include the block
                 # padding below.
-                end_item_hook ()
+                self.fileobj._finalize_write_encrypt ()
 
                 tarinfo.type = GNUTYPE_MULTIVOL
 
@@ -2637,7 +2629,7 @@ class TarFile(object):
 
                 self.volume_tarinfo = None
 
-                new_item_hook ()
+                self.fileobj._init_write_encrypt (tarinfo.name)
 
                 # write new volume header
                 buf = tarinfo.tobuf(self.format, self.encoding, self.errors)