1 # Copyright (C) 2013 Intra2net AG
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU Lesser General Public License as published
5 # by the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU Lesser General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see
15 # <http://www.gnu.org/licenses/lgpl-3.0.html>
18 import os, unittest, hashlib, string
20 from deltatar.tarfile import TarFile, PAX_FORMAT, GNU_FORMAT, BLOCKSIZE
21 from . import BaseTest
24 def new_volume_handler(tarobj, base_name, volume_number):
26 Handles the new volumes
28 volume_path = "%s.%d" % (base_name, volume_number)
29 tarobj.open_volume(volume_path)
31 class RescueTarTest(BaseTest):
32 def test_rescue_ok(self):
34 Test rescue_tar when no file is broken, without using multivol tars.
39 hash["big"] = self.create_file("big", 50000)
40 hash["big2"] = self.create_file("big2", 10200)
41 hash["small"] = self.create_file("small", 100)
42 hash["small2"] = self.create_file("small2", 354)
44 # create the tar file with volumes
45 tarobj = TarFile.open("sample.tar.gz",
53 assert os.path.exists("sample.tar.gz")
60 rescue_tar.rescue("sample.tar.gz")
63 for key, value in hash.items():
64 assert os.path.exists(key)
65 assert value == self.md5sum(key)
67 def test_rescue_broken(self):
69 Use rescue_tar utility to split the file in compressed tar blocks that
70 individually decompressed and "untarred", thanks to be using the
71 concat gzip tar format. In this case, we simulate that one of the files
72 is corrupted. The rest will decompress just fine.
77 hash["big"] = self.create_file("big", 50000)
78 hash["big2"] = self.create_file("big2", 10200)
79 hash["small"] = self.create_file("small", 100)
80 hash["small2"] = self.create_file("small2", 354)
82 # create the tar file with volumes
83 tarobj = TarFile.open("sample.tar.gz",
91 assert os.path.exists("sample.tar.gz")
93 # overwrite stuff in the middle of the big file
94 f = open('sample.tar.gz', 'r+b')
96 f.write(bytes("breaking things", "UTF-8"))
105 rescue_tar.rescue("sample.tar.gz")
108 for key, value in hash.items():
111 assert os.path.exists(key)
112 assert value == self.md5sum(key)