Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <venti.h>
5 #include <libsec.h>
6 #include <thread.h>
8 VtConn *z;
9 char *host;
11 void
12 usage(void)
13 {
14 fprint(2, "usage: venti/dump [-h host] score\n");
15 threadexitsall("usage");
16 }
18 Biobuf bout;
19 char spaces[256];
21 void
22 dump(int indent, uchar *score, int type)
23 {
24 int i, n;
25 uchar *buf;
26 VtEntry e;
27 VtRoot root;
29 if(spaces[0] == 0)
30 memset(spaces, ' ', sizeof spaces-1);
32 buf = vtmallocz(VtMaxLumpSize);
33 if(memcmp(score, vtzeroscore, VtScoreSize) == 0)
34 n = 0;
35 else
36 n = vtread(z, score, type, buf, VtMaxLumpSize);
37 if(n < 0){
38 Bprint(&bout, "%.*serror reading %V: %r\n", indent*4, spaces, score);
39 goto out;
40 }
41 switch(type){
42 case VtRootType:
43 if(vtrootunpack(&root, buf) < 0){
44 Bprint(&bout, "%.*serror unpacking root %V: %r\n", indent*4, spaces, score);
45 goto out;
46 }
47 Bprint(&bout, "%.*s%V root name=%s type=%s prev=%V bsize=%d\n",
48 indent*4, spaces, score, root.name, root.type, root.prev, root.blocksize);
49 dump(indent+1, root.score, VtDirType);
50 break;
52 case VtDirType:
53 Bprint(&bout, "%.*s%V dir n=%d\n", indent*4, spaces, score, n);
54 for(i=0; i*VtEntrySize<n; i++){
55 if(vtentryunpack(&e, buf, i) < 0){
56 Bprint(&bout, "%.*s%d: cannot unpack\n", indent+1, spaces, i);
57 continue;
58 }
59 Bprint(&bout, "%.*s%d: gen=%#lux psize=%d dsize=%d type=%d flags=%#x size=%llud score=%V\n",
60 (indent+1)*4, spaces, i, e.gen, e.psize, e.dsize, e.type, e.flags, e.size, e.score);
61 dump(indent+2, e.score, e.type);
62 }
63 break;
65 case VtDataType:
66 Bprint(&bout, "%.*s%V data n=%d", indent*4, spaces, score, n);
67 for(i=0; i<n; i++){
68 if(i%16 == 0)
69 Bprint(&bout, "\n%.*s", (indent+1)*4, spaces);
70 Bprint(&bout, " %02x", buf[i]);
71 }
72 Bprint(&bout, "\n");
73 break;
75 default:
76 if(type >= VtDirType)
77 Bprint(&bout, "%.*s%V dir+%d\n", indent*4, spaces, score, type-VtDirType);
78 else
79 Bprint(&bout, "%.*s%V data+%d\n", indent*4, spaces, score, type-VtDirType);
80 for(i=0; i<n; i+=VtScoreSize)
81 dump(indent+1, buf+i, type-1);
82 break;
83 }
84 out:
85 free(buf);
86 }
89 void
90 threadmain(int argc, char *argv[])
91 {
92 int type, n;
93 uchar score[VtScoreSize];
94 uchar *buf;
95 char *prefix;
97 fmtinstall('F', vtfcallfmt);
98 fmtinstall('V', vtscorefmt);
100 type = -1;
101 ARGBEGIN{
102 case 'h':
103 host = EARGF(usage());
104 break;
105 default:
106 usage();
107 }ARGEND
109 if(argc != 1)
110 usage();
112 if(vtparsescore(argv[0], &prefix, score) < 0)
113 sysfatal("could not parse score: %r");
115 buf = vtmallocz(VtMaxLumpSize);
116 z = vtdial(host);
117 if(z == nil)
118 sysfatal("dialing venti: %r");
119 if(vtconnect(z) < 0)
120 sysfatal("vtconnect src: %r");
122 for(type=0; type<VtMaxType; type++){
123 n = vtread(z, score, type, buf, VtMaxLumpSize);
124 if(n >= 0)
125 goto havetype;
127 sysfatal("cannot find block %V", score);
129 havetype:
130 Binit(&bout, 1, OWRITE);
131 dump(0, score, type);
132 Bflush(&bout);
133 threadexitsall(nil);