fixing compressed multivol
authorEduardo Robles Elvira <edulix@wadobo.com>
Wed, 10 Jul 2013 16:41:00 +0000 (18:41 +0200)
committerEduardo Robles Elvira <edulix@wadobo.com>
Wed, 10 Jul 2013 16:41:00 +0000 (18:41 +0200)
deltatar/tarfile.py
testing/__init__.py
testing/test_concat_compress.py
testing/test_multivol.py

index dc9554e..f57cdd7 100644 (file)
@@ -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
index d47b054..673414b 100644 (file)
@@ -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()
index 2659792..4262e67 100644 (file)
@@ -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
index cdb2d5b..fce20c8 100644 (file)
@@ -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):
     """