fix file offset calculation
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Mon, 3 Apr 2017 13:56:25 +0000 (15:56 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:08 +0000 (13:34 +0200)
Move the seek-back code down into the “low level file” wrapper so
header writes aren’t counted. This way byte counters match again.

deltatar/tarfile.py

index 89d788e..1788865 100644 (file)
@@ -275,7 +275,6 @@ def copyfileobj(src, dst, length=None):
         dst.write(buf)
         if len(buf) < BUFSIZE:
             raise OSError("end of file reached")
-
     if remainder != 0:
         buf = src.read(remainder)
         dst.write(buf)
@@ -360,9 +359,18 @@ class _LowLevelFile:
         self.offset += len(ret)
         return ret
 
-    def write(self, s):
-        self.offset += len(s)
-        os.write(self.fd, s)
+    def write(self, s, pos=None):
+        if pos is not None:
+            p0 = self.offset
+            os.lseek (self.fd, pos, os.SEEK_SET)
+        n = os.write(self.fd, s)
+        if pos is None:
+            self.offset += len(s)
+        else:
+            append = pos + n - p0
+            if append > 0:
+                self.offset += append
+            os.lseek (self.fd, p0, os.SEEK_SET)
 
     def tell(self):
         return self.offset
@@ -644,21 +652,17 @@ class _Stream:
             self.__enc_write(self.buf[:self.bufsize])
             self.buf = self.buf[self.bufsize:]
 
+
     def __write_to_file(self, s, pos=None):
         '''
         Writes directly to the fileobj; updates self.bytes_written. If “pos” is
         given, the streem will seek to that position first and back afterwards,
         and the total of bytes written is not updated.
         '''
-        if pos is not None:
-            self.fileobj
-            p0 = self.fileobj.tell ()
-            self.fileobj.seek_set (pos)
-        self.fileobj.write(s)
+        self.fileobj.write(s, pos)
         if pos is None:
             self.bytes_written += len(s)
-        else:
-            self.fileobj.seek_set (p0)
+
 
     def __enc_write(self, s):
         '''
@@ -870,10 +874,9 @@ class _Stream:
         """
         c = len(self.dbuf)
         t = [self.dbuf]
-        l_buf = self.bufsize # not mutated
 
         while c < size:
-            buf = self.__read(l_buf)
+            buf = self.__read(self.bufsize)
             if not buf:
                 break