From: Philipp Gesang Date: Mon, 3 Apr 2017 13:56:25 +0000 (+0200) Subject: fix file offset calculation X-Git-Tag: v2.2~7^2~195 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=867f75f7811625f83ed203e91434d60cd895f825;p=python-delta-tar fix file offset calculation Move the seek-back code down into the “low level file” wrapper so header writes aren’t counted. This way byte counters match again. --- diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index 89d788e..1788865 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -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