From: Eduardo Robles Elvira Date: Wed, 10 Jul 2013 16:41:00 +0000 (+0200) Subject: fixing compressed multivol X-Git-Tag: v2.2~167 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=26fa5ad5defc839362816e024bf029fcd9a512c3;p=python-delta-tar fixing compressed multivol --- diff --git a/deltatar/tarfile.py b/deltatar/tarfile.py index dc9554e..f57cdd7 100644 --- a/deltatar/tarfile.py +++ b/deltatar/tarfile.py @@ -2249,8 +2249,17 @@ class TarFile(object): # Create nonexistent files in append mode. self.mode = "w" self._mode = "wb" - fileobj = bltn_open(name, self._mode) self._extfileobj = False + + if isinstance(self.fileobj, _Stream): + fileobj = _Stream(name=name, + mode=self.fileobj.mode, + comptype=self.fileobj.comptype, + fileobj=None, + bufsize=self.fileobj.bufsize, + concat_stream=self.fileobj.concat_stream) + else: + fileobj = bltn_open(name, self._mode) else: if name is None and hasattr(fileobj, "name"): name = fileobj.name diff --git a/testing/__init__.py b/testing/__init__.py index d47b054..673414b 100644 --- a/testing/__init__.py +++ b/testing/__init__.py @@ -1,6 +1,13 @@ import os, unittest, hashlib, string import random +def new_volume_handler(tarobj, base_name, volume_number): + ''' + Handles the new volumes + ''' + volume_path = "%s.%d" % (base_name, volume_number) + tarobj.open_volume(volume_path) + class BaseTest(unittest.TestCase): """ Test concatenated compression in tarfiles @@ -10,7 +17,7 @@ class BaseTest(unittest.TestCase): ''' Remove temporal files created by unit tests ''' - os.system("rm -rf big small small2 sample.*") + os.system("rm -rf big big2 small small2 sample.*") def create_file(self, path, length): ''' @@ -34,4 +41,4 @@ class BaseTest(unittest.TestCase): with open(filename,'rb') as f: for chunk in iter(lambda: f.read(128*md5.block_size), b''): md5.update(chunk) - return md5.hexdigest() \ No newline at end of file + return md5.hexdigest() diff --git a/testing/test_concat_compress.py b/testing/test_concat_compress.py index 2659792..4262e67 100644 --- a/testing/test_concat_compress.py +++ b/testing/test_concat_compress.py @@ -3,7 +3,7 @@ import os, unittest, hashlib, string from deltatar.tarfile import TarFile, GNU_FORMAT import filesplit -from . import BaseTest +from . import BaseTest, new_volume_handler class ConcatCompressTest(BaseTest): """ @@ -133,6 +133,48 @@ class ConcatCompressTest(BaseTest): assert os.path.exists(key) assert value == self.md5sum(key) + def test_multivol_gzip_concat_extract(self): + ''' + Test multivol tarball with concat compression. + ''' + + # create sample data + hash = dict() + hash["big"] = self.create_file("big", 50000) + hash["big2"] = self.create_file("big2", 10200) + hash["small"] = self.create_file("small", 100) + hash["small2"] = self.create_file("small2", 354) + + # create the tar file with volumes + tarobj = TarFile.open("sample.tar.gz", + mode="w#gz", + concat_compression=True, + max_volume_size=20000, + new_volume_handler=new_volume_handler) + tarobj.add("big") + tarobj.add("big2") + tarobj.add("small") + tarobj.add("small2") + tarobj.close() + + assert os.path.exists("sample.tar.gz") + os.unlink("big") + os.unlink("big2") + os.unlink("small") + os.unlink("small2") + + # extract + tarobj = TarFile.open("sample.tar.gz", + mode="r#gz", + new_volume_handler=new_volume_handler) + tarobj.extractall() + tarobj.close() + + # check output + for key, value in hash.iteritems(): + assert os.path.exists(key) + assert value == self.md5sum(key) + def test_multiple_files_rescue_extract(self): ''' Use filesplit utility to split the file in compressed tar blocks that diff --git a/testing/test_multivol.py b/testing/test_multivol.py index cdb2d5b..fce20c8 100644 --- a/testing/test_multivol.py +++ b/testing/test_multivol.py @@ -1,15 +1,7 @@ import os, unittest, hashlib, string from deltatar.tarfile import TarFile, PAX_FORMAT, GNU_FORMAT, BLOCKSIZE -from . import BaseTest - -def new_volume_handler(tarobj, base_name, volume_number): - ''' - Handles the new volumes - ''' - volume_path = "%s.%d" % (base_name, volume_number) - tarobj.open_volume(volume_path) - +from . import BaseTest, new_volume_handler class MultivolGnuFormatTest(BaseTest): """