From 67e73339553a2c757d30fffa29abe7f7272c4c91 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Tue, 12 Aug 2008 14:46:56 -0700
Subject: [PATCH] 'tahoe debug dump-share': add --offsets, to show section
 offsets

---
 src/allmydata/scripts/debug.py    | 51 +++++++++++++++++++++++++++++--
 src/allmydata/test/test_system.py |  4 +--
 2 files changed, 50 insertions(+), 5 deletions(-)

diff --git a/src/allmydata/scripts/debug.py b/src/allmydata/scripts/debug.py
index a32c67c3..4ca32aaa 100644
--- a/src/allmydata/scripts/debug.py
+++ b/src/allmydata/scripts/debug.py
@@ -8,6 +8,10 @@ class DumpOptions(usage.Options):
     def getSynopsis(self):
         return "Usage: tahoe debug dump-share SHARE_FILENAME"
 
+    optFlags = [
+        ["offsets", None, "Display a table of section offsets"],
+        ]
+
     def getUsage(self, width=None):
         t = usage.Options.getUsage(self, width)
         t += """
@@ -106,7 +110,20 @@ def dump_immutable_share(options):
     for k in sorted(sizes):
         print >>out, "%20s: %s" % (k, sizes[k])
 
+    if options['offsets']:
+        print >>out
+        print >>out, " Section Offsets:"
+        print >>out, "%20s: %s" % ("share data", f._data_offset)
+        for k in ["data", "plaintext_hash_tree", "crypttext_hash_tree",
+                  "block_hashes", "share_hashes", "uri_extension"]:
+            name = {"data": "block data"}.get(k,k)
+            offset = f._data_offset + offsets[k]
+            print >>out, "  %20s: %s   (0x%x)" % (name, offset, offset)
+        print >>out, "%20s: %s" % ("leases", f._lease_offset)
+
+
     # display lease information too
+    print >>out
     leases = list(f.iter_leases())
     if leases:
         for i,lease in enumerate(leases):
@@ -173,16 +190,18 @@ def dump_mutable_share(options):
     print >>out
 
     if share_type == "SDMF":
-        dump_SDMF_share(m.DATA_OFFSET, data_length, options)
+        dump_SDMF_share(m, data_length, options)
 
     return 0
 
-def dump_SDMF_share(offset, length, options):
-    from allmydata.mutable.layout import unpack_share
+def dump_SDMF_share(m, length, options):
+    from allmydata.mutable.layout import unpack_share, unpack_header
     from allmydata.mutable.common import NeedMoreDataError
     from allmydata.util import base32, hashutil
     from allmydata.uri import SSKVerifierURI
 
+    offset = m.DATA_OFFSET
+
     out = options.stdout
 
     f = open(options['filename'], "rb")
@@ -204,6 +223,8 @@ def dump_SDMF_share(offset, length, options):
     (seqnum, root_hash, IV, k, N, segsize, datalen,
      pubkey, signature, share_hash_chain, block_hash_tree,
      share_data, enc_privkey) = pieces
+    (ig_version, ig_seqnum, ig_roothash, ig_IV, ig_k, ig_N, ig_segsize,
+     ig_datalen, offsets) = unpack_header(data)
 
     print >>out, " SDMF contents:"
     print >>out, "  seqnum: %d" % seqnum
@@ -231,6 +252,30 @@ def dump_SDMF_share(offset, length, options):
         verify_cap = u.to_string()
         print >>out, "  verify-cap:", verify_cap
 
+    if options['offsets']:
+        # NOTE: this offset-calculation code is fragile, and needs to be
+        # merged with MutableShareFile's internals.
+        print >>out
+        print >>out, " Section Offsets:"
+        def printoffset(name, value, shift=0):
+            print >>out, "%s%20s: %s   (0x%x)" % (" "*shift, name, value, value)
+        printoffset("first lease", m.HEADER_SIZE)
+        printoffset("share data", m.DATA_OFFSET)
+        o_seqnum = m.DATA_OFFSET + struct.calcsize(">B")
+        printoffset("seqnum", o_seqnum, 2)
+        o_root_hash = m.DATA_OFFSET + struct.calcsize(">BQ")
+        printoffset("root_hash", o_root_hash, 2)
+        for k in ["signature", "share_hash_chain", "block_hash_tree",
+                  "share_data",
+                  "enc_privkey", "EOF"]:
+            name = {"share_data": "block data",
+                    "EOF": "end of share data"}.get(k,k)
+            offset = m.DATA_OFFSET + offsets[k]
+            printoffset(name, offset, 2)
+        f = open(options['filename'], "rb")
+        printoffset("extra leases", m._read_extra_lease_offset(f) + 4)
+        f.close()
+
     print >>out
 
 
diff --git a/src/allmydata/test/test_system.py b/src/allmydata/test/test_system.py
index b91c1dbb..dd920fc0 100644
--- a/src/allmydata/test/test_system.py
+++ b/src/allmydata/test/test_system.py
@@ -438,7 +438,7 @@ class SystemTest(SystemTestMixin, unittest.TestCase):
             log.msg(" for clients[%d]" % client_num)
 
             out,err = StringIO(), StringIO()
-            rc = runner.runner(["debug", "dump-share",
+            rc = runner.runner(["debug", "dump-share", "--offsets",
                                 filename],
                                stdout=out, stderr=err)
             output = out.getvalue()
@@ -1233,7 +1233,7 @@ class SystemTest(SystemTestMixin, unittest.TestCase):
         log.msg("test_system.SystemTest._test_runner using %s" % filename)
 
         out,err = StringIO(), StringIO()
-        rc = runner.runner(["debug", "dump-share",
+        rc = runner.runner(["debug", "dump-share", "--offsets",
                             filename],
                            stdout=out, stderr=err)
         output = out.getvalue()
-- 
2.45.2