add restore helper handling for reconstructed indices
[python-delta-tar] / deltatar / tarfile.py
index c052fa5..5477c04 100644 (file)
@@ -381,6 +381,7 @@ class EncryptionError(TarError):
     pass
 class EndOfFile(Exception):
     """Signal end of file condition when they’re not an error."""
+    pass
 
 #---------------------------
 # internal stream interface
@@ -3315,6 +3316,14 @@ class TarIter:
 # support functionality for rescue mode
 #---------------------------------------------------------
 
+def locate_tar_hdr_candidates (fd):
+    raise NotImplementedError ("too soon")
+
+
+def readable_tar_objects_offsets (ifd, cands):
+    raise NotImplementedError ("too soon")
+
+
 def locate_gz_hdr_candidates (fd):
     """
     Walk over instances of the GZ magic in the payload, collecting their
@@ -3523,6 +3532,21 @@ def reconstruct_offsets_gz (fname):
         os.close (ifd)
 
 
+def reconstruct_offsets_tar (fname):
+    """
+    From the given file, retrieve all tar header-like offsets (“candidates”).
+    Then check each of those locations whether they can be processed as tar
+    data.
+    """
+    ifd = os.open (fname, os.O_RDONLY)
+
+    try:
+        cands = locate_tar_hdr_candidates (ifd)
+        return readable_tar_objects_offsets (ifd, cands)
+    finally:
+        os.close (ifd)
+
+
 def read_tarobj_at_offset (fileobj, offset, mode, secret=None):
     decr = None
 
@@ -3596,6 +3620,10 @@ def gen_rescue_index (backup_tar_path, mode, password=None, key=None):
         offsets = crypto.reconstruct_offsets (backup_tar_path, secret)
     elif mode == "#gz":
         offsets = reconstruct_offsets_gz (backup_tar_path)
+    elif mode == "#":
+        offsets = reconstruct_offsets_tar (backup_tar_path)
+    else:
+        raise TarError ("no rescue handling for mode “%s”" % mode)
 
     fileobj = bltn_open (backup_tar_path, "rb")
     infos   = [ (off, read_tarobj_at_offset (fileobj, off, mode, secret=secret))