convert TarInfo to index format
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 24 Aug 2017 11:21:30 +0000 (13:21 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:09 +0000 (13:34 +0200)
deltatar/tarfile.py

index 4f9e033..cd77208 100644 (file)
@@ -3333,6 +3333,43 @@ def read_tarobj_at_offset (fileobj, offset, mode, secret=None):
     return tarobj.next ()
 
 
+def idxent_of_tarinfo (tarinfo):
+    """
+    Scrape the information relevant for the index from a *TarInfo* object.
+    Keys like the inode number that lack a corresponding field in a TarInfo
+    will be set to some neutral value.
+    Example output:
+
+        { "inode"  : 0
+        , "uid"    : 0
+        , "path"   : "snapshot://annotations.db"
+        , "offset" : 0
+        , "volume" : 0
+        , "mode"   : 33152
+        , "ctime"  : 1502798115
+        , "mtime"  : 1502196423
+        , "size"   : 144
+        , "type"   : "file"
+        , "gid"    : 0
+        }
+
+    """
+
+    return \
+        { "inode"  : 0            # ignored when reading the index
+        , "uid"    : tarinfo.uid
+        , "gid"    : tarinfo.gid
+        , "path"   : tarinfo.name # keeping URI scheme
+        , "offset" : 0            # to be added by the caller
+        , "volume" : tarinfo.volume_offset
+        , "mode"   : tarinfo.mode
+        , "ctime"  : tarinfo.mtime
+        , "mtime"  : tarinfo.mtime
+        , "size"   : tarinfo.size
+        , "type"   : tarinfo.type
+        }
+
+
 def gen_rescue_index (backup_tar_path, mode, password=None, key=None):
     psidx   = [] # pseudo index, return value
     offsets = None
@@ -3347,8 +3384,13 @@ def gen_rescue_index (backup_tar_path, mode, password=None, key=None):
     if secret is not None:
         offsets = crypto.reconstruct_offsets (backup_tar_path, secret)
         fileobj = bltn_open (backup_tar_path, "rb")
-        psinfos = [ read_tarobj_at_offset (fileobj, off, mode, secret=secret)
+        infos   = [ (off, read_tarobj_at_offset (fileobj, off, mode, secret=secret))
                     for off in offsets ]
+        def aux (o, ti):
+            ie = idxent_of_tarinfo (ti)
+            ie ["offset"] = o
+            return ie
+        psidx   = [ aux (o, ti) for o, ti in infos ]
 
     return psidx