From: Eduardo Robles Elvira Date: Tue, 30 Jul 2013 09:19:26 +0000 (+0200) Subject: tarfile: adding a way to know the starting position of last tar member added X-Git-Tag: v2.2~139 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=ad4402e8e58591978221191238ddc0215ba2901d;p=python-delta-tar tarfile: adding a way to know the starting position of last tar member added --- diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index 879597a..a57a6dc 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -371,23 +371,30 @@ class _LowLevelFile: """ def __init__(self, name, mode): - mode = { + _mode = { "r": os.O_RDONLY, "w": os.O_WRONLY | os.O_CREAT | os.O_TRUNC, }[mode] if hasattr(os, "O_BINARY"): - mode |= os.O_BINARY - self.fd = os.open(name, mode, 0666) + _mode |= os.O_BINARY + self.fd = os.open(name, _mode, 0666) + self.offset = 0 def close(self): os.close(self.fd) def read(self, size): - return os.read(self.fd, size) + ret = os.read(self.fd, size) + self.offset += len(ret) + return ret def write(self, s): + self.offset += len(s) os.write(self.fd, s) + def tell(self): + return self.offset + class _Stream: """Class that serves as an adapter between TarFile and a stream-like object. The stream-like object only @@ -483,18 +490,18 @@ class _Stream: self.name = self.name[:-3] self.__write(self.name + NUL) - def new_compression_block(self): + def new_compression_block(self, set_last_block_offset=False): ''' Used to notify a new tar block is coming to create a new zip block ''' if self.mode != "w": raise CompressionError("new compression blocks can only be added in mode 'w'") if self.comptype == "gz": - self._new_gz_block() + self._new_gz_block(set_last_block_offset) else: raise CompressionError("Concat compression only available for comptype 'gz'") - def _new_gz_block(self): + def _new_gz_block(self, set_last_block_offset=False): ''' Add a new gzip block, closing last one ''' @@ -510,10 +517,15 @@ class _Stream: # if aes, we encrypt after compression if self.enctype == 'aes': self.__write_to_file(self.encryption.close_enc()) + if set_last_block_offset: + self.last_block_offset = self.fileobj.tell() self.encryption = aescrypto.AESCrypt(self.password, key_length=self.key_length) self.encryption.init() self.__write_to_file(self.encryption.salt_str) + elif set_last_block_offset: + self.last_block_offset = self.fileobj.tell() + self.fileobj timestamp = struct.pack("