From 7c968ae064b921d908d78e5d0f407b8d54097d66 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Mon, 27 Mar 2017 17:22:13 +0200 Subject: [PATCH] properly enter/leave encryption sections --- deltatar/tarfile.py | 28 ++++++++++------------------ 1 files changed, 10 insertions(+), 18 deletions(-) diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index 3576879..1d84365 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -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) -- 1.7.1