Fix encryption end writing for external fileobjs.
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Nov 2020 09:32:06 +0000 (10:32 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 12 Nov 2020 14:04:34 +0000 (15:04 +0100)
The fix in commit 867f75f78 for fixing offset calculations did not
take into account that file objects could be given from external code.

Correct that case.

deltatar/tarfile.py

index d548f92..4a3dba3 100644 (file)
@@ -745,9 +745,17 @@ class _Stream:
         given, the stream will seek to that position first and back afterwards,
         and the total of bytes written is not updated.
         '''
-        self.fileobj.write(s, pos)
         if pos is None:
+            self.fileobj.write(s)
             self.bytes_written += len(s)
+        elif isinstance(self.fileobj, _LowLevelFile):
+            self.fileobj.write(s, pos)  # change offset update in _LowLevelFile
+        else:   # implement the seek here, e.g. for extfileobj
+            p0 = self.fileobj.tell()
+            os.lseek (self.fd, pos, os.SEEK_SET)
+            self.fileobj.write(s)
+            os.lseek (self.fd, p0, os.SEEK_SET)
+            # do not update bytes_written
 
 
     def __enc_write(self, s):