force tarfile reopen after bad read in deltatar
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 8 Aug 2017 08:48:31 +0000 (10:48 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:09 +0000 (13:34 +0200)
Closing the tarfile after an unreadable object was encountered
causes the stream to be reopened for the next read. Otherwise,
the corrupt object is already buffered and tarfile would continue
to seek inside the bad data.

deltatar/deltatar.py

index 2d48c39..b2d483f 100644 (file)
@@ -1806,7 +1806,14 @@ class RestoreHelper(object):
             # seek tarfile if needed
             offset = file_data.get('offset', -1)
             if index_data['tarobj']:
-                member = index_data['tarobj'].__iter__().__next__()
+                try:
+                    member = index_data['tarobj'].__iter__().__next__()
+                except tarfile.ReadError as exn:
+                    # Possibly corrupted archive; may still be recoverable
+                    # if offsets did not change.
+                    index_data['tarobj'].close()
+                    index_data['tarobj'] = None
+                    raise
                 if not member or member.path != file_data['path']:
                     # force a seek and reopen
                     index_data['tarobj'].close()