Upgrades to utility code for new formats, and a few more database tweaks.
[cumulus.git] / lbs-util
index 4342601..8789a75 100755 (executable)
--- a/lbs-util
+++ b/lbs-util
@@ -2,10 +2,19 @@
 #
 # Utility for managing LBS archives.
 
-import getpass, os, sys
+import getpass, os, stat, sys, time
 from optparse import OptionParser
 import lbs
 
+# We support up to "LBS Snapshot v0.6" formats, but are also limited by the lbs
+# module.
+FORMAT_VERSION = min(lbs.FORMAT_VERSION, (0, 6))
+
+def check_version(format):
+    ver = lbs.parse_metadata_version(format)
+    if ver > FORMAT_VERSION:
+        raise RuntimeError("Unsupported LBS format: " + format)
+
 parser = OptionParser(usage="%prog [option]... command [arg]...")
 parser.add_option("-v", action="store_true", dest="verbose", default=False,
                   help="increase verbosity")
@@ -66,6 +75,7 @@ def cmd_list_snapshot_sizes():
     previous = set()
     for s in sorted(lowlevel.list_snapshots()):
         d = lbs.parse_full(store.load_snapshot(s))
+        check_version(d['Format'])
         segments = d['Segments'].split()
         (size, added, removed) = (0, 0, 0)
         for seg in segments:
@@ -99,10 +109,23 @@ def cmd_read_snapshots(snapshots):
     store = lbs.ObjectStore(lowlevel)
     for s in snapshots:
         d = lbs.parse_full(store.load_snapshot(s))
+        check_version(d['Format'])
         print d
         print d['Segments'].split()
     store.cleanup()
 
+# Produce a flattened metadata dump from a snapshot
+def cmd_read_metadata(snapshot):
+    get_passphrase()
+    lowlevel = lbs.LowlevelDataStore(options.store)
+    store = lbs.ObjectStore(lowlevel)
+    d = lbs.parse_full(store.load_snapshot(snapshot))
+    check_version(d['Format'])
+    metadata = lbs.read_metadata(store, d['Root'])
+    for l in metadata:
+        sys.stdout.write(l)
+    store.cleanup()
+
 # Verify snapshot integrity
 def cmd_verify_snapshots(snapshots):
     get_passphrase()
@@ -111,10 +134,11 @@ def cmd_verify_snapshots(snapshots):
     for s in snapshots:
         print "#### Snapshot", s
         d = lbs.parse_full(store.load_snapshot(s))
+        check_version(d['Format'])
         print "## Root:", d['Root']
         metadata = lbs.iterate_metadata(store, d['Root'])
         for m in metadata:
-            if m.fields['type'] != '-': continue
+            if m.fields['type'] not in ('-', 'f'): continue
             print "%s [%d bytes]" % (m.fields['name'], int(m.fields['size']))
             verifier = lbs.ChecksumVerifier(m.fields['checksum'])
             size = 0
@@ -128,6 +152,99 @@ def cmd_verify_snapshots(snapshots):
                 raise ValueError("Bad checksum found")
     store.cleanup()
 
+# Restore a snapshot, or some subset of files from it
+def cmd_restore_snapshot(args):
+    get_passphrase()
+    lowlevel = lbs.LowlevelDataStore(options.store)
+    store = lbs.ObjectStore(lowlevel)
+    snapshot = lbs.parse_full(store.load_snapshot(args[0]))
+    check_version(snapshot['Format'])
+    destdir = args[1]
+    paths = args[2:]
+
+    def warn(m, msg):
+        print "Warning: %s: %s" % (m.items.name, msg)
+
+    for m in lbs.iterate_metadata(store, snapshot['Root']):
+        pathname = os.path.normpath(m.items.name)
+        while os.path.isabs(pathname):
+            pathname = pathname[1:]
+        print pathname
+        destpath = os.path.join(destdir, pathname)
+        (path, filename) = os.path.split(destpath)
+
+        # TODO: Check for ../../../paths that might attempt to write outside
+        # the destination directory.  Maybe also check attempts to follow
+        # symlinks pointing outside?
+
+        try:
+            if not os.path.isdir(path):
+                os.makedirs(path)
+
+            if m.items.type in ('-', 'f'):
+                file = open(destpath, 'wb')
+                verifier = lbs.ChecksumVerifier(m.items.checksum)
+                size = 0
+                for block in m.data():
+                    data = store.get(block)
+                    verifier.update(data)
+                    size += len(data)
+                    file.write(data)
+                file.close()
+                if int(m.fields['size']) != size:
+                    raise ValueError("File size does not match!")
+                if not verifier.valid():
+                    raise ValueError("Bad checksum found")
+            elif m.items.type == 'd':
+                if filename != '.':
+                    os.mkdir(destpath)
+            elif m.items.type == 'l':
+                try:
+                    target = m.items.target
+                except:
+                    # Old (v0.2 format) name for 'target'
+                    target = m.items.contents
+                os.symlink(target, destpath)
+            elif m.items.type == 'p':
+                os.mkfifo(destpath)
+            elif m.items.type in ('c', 'b'):
+                if m.items.type == 'c':
+                    mode = 0600 | stat.S_IFCHR
+                else:
+                    mode = 0600 | stat.S_IFBLK
+                os.mknod(destpath, mode, os.makedev(*m.items.device))
+            elif m.items.type == 's':
+                pass        # TODO: Implement
+            else:
+                warn(m, "Unknown type code: " + m.items.type)
+                continue
+
+        except Exception, e:
+            warn(m, "Error restoring: %s" % (e,))
+            continue
+
+        try:
+            uid = m.items.user[0]
+            gid = m.items.group[0]
+            os.lchown(destpath, uid, gid)
+        except Exception, e:
+            warn(m, "Error restoring file ownership: %s" % (e,))
+
+        if m.items.type == 'l':
+            continue
+
+        try:
+            os.chmod(destpath, m.items.mode)
+        except Exception, e:
+            warn(m, "Error restoring file permissions: %s" % (e,))
+
+        try:
+            os.utime(destpath, (time.time(), m.items.mtime))
+        except Exception, e:
+            warn(m, "Error restoring file timestamps: %s" % (e,))
+
+    store.cleanup()
+
 if len(args) == 0:
     parser.print_usage()
     sys.exit(1)
@@ -143,10 +260,14 @@ elif cmd == 'object-sums':
     cmd_object_checksums(args)
 elif cmd == 'read-snapshots':
     cmd_read_snapshots(args)
+elif cmd == 'read-metadata':
+    cmd_read_metadata(args[0])
 elif cmd == 'list-snapshot-sizes':
     cmd_list_snapshot_sizes()
 elif cmd == 'verify-snapshots':
     cmd_verify_snapshots(args)
+elif cmd == 'restore-snapshot':
+    cmd_restore_snapshot(args)
 else:
     print "Unknown command:", cmd
     parser.print_usage()