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 enum
9 {
10 // XXX What to do here?
11 VtMaxLumpSize = 65536,
12 };
14 VtConn *z;
15 char *host;
17 void
18 usage(void)
19 {
20 fprint(2, "usage: venti/dump [-h host] score\n");
21 threadexitsall("usage");
22 }
24 Biobuf bout;
25 char spaces[256];
27 void
28 dump(int indent, uchar *score, int type)
29 {
30 int i, n;
31 uchar *buf;
32 VtEntry e;
33 VtRoot root;
35 if(spaces[0] == 0)
36 memset(spaces, ' ', sizeof spaces-1);
38 buf = vtmallocz(VtMaxLumpSize);
39 if(memcmp(score, vtzeroscore, VtScoreSize) == 0)
40 n = 0;
41 else
42 n = vtread(z, score, type, buf, VtMaxLumpSize);
43 if(n < 0){
44 Bprint(&bout, "%.*serror reading %V: %r\n", indent*4, spaces, score);
45 goto out;
46 }
47 switch(type){
48 case VtRootType:
49 if(vtrootunpack(&root, buf) < 0){
50 Bprint(&bout, "%.*serror unpacking root %V: %r\n", indent*4, spaces, score);
51 goto out;
52 }
53 Bprint(&bout, "%.*s%V root name=%s type=%s prev=%V bsize=%d\n",
54 indent*4, spaces, score, root.name, root.type, root.prev, root.blocksize);
55 dump(indent+1, root.score, VtDirType);
56 break;
58 case VtDirType:
59 Bprint(&bout, "%.*s%V dir n=%d\n", indent*4, spaces, score, n);
60 for(i=0; i*VtEntrySize<n; i++){
61 if(vtentryunpack(&e, buf, i) < 0){
62 Bprint(&bout, "%.*s%d: cannot unpack\n", indent+1, spaces, i);
63 continue;
64 }
65 Bprint(&bout, "%.*s%d: gen=%#lux psize=%d dsize=%d type=%d flags=%#x size=%llud score=%V\n",
66 (indent+1)*4, spaces, i, e.gen, e.psize, e.dsize, e.type, e.flags, e.size, e.score);
67 dump(indent+2, e.score, e.type);
68 }
69 break;
71 case VtDataType:
72 Bprint(&bout, "%.*s%V data n=%d", indent*4, spaces, score, n);
73 for(i=0; i<n; i++){
74 if(i%16 == 0)
75 Bprint(&bout, "\n%.*s", (indent+1)*4, spaces);
76 Bprint(&bout, " %02x", buf[i]);
77 }
78 Bprint(&bout, "\n");
79 break;
81 default:
82 if(type >= VtDirType)
83 Bprint(&bout, "%.*s%V dir+%d\n", indent*4, spaces, score, type-VtDirType);
84 else
85 Bprint(&bout, "%.*s%V data+%d\n", indent*4, spaces, score, type-VtDirType);
86 for(i=0; i<n; i+=VtScoreSize)
87 dump(indent+1, buf+i, type-1);
88 break;
89 }
90 out:
91 free(buf);
92 }
95 void
96 threadmain(int argc, char *argv[])
97 {
98 int type, n;
99 uchar score[VtScoreSize];
100 uchar *buf;
101 char *prefix;
103 fmtinstall('F', vtfcallfmt);
104 fmtinstall('V', vtscorefmt);
106 type = -1;
107 ARGBEGIN{
108 case 'h':
109 host = EARGF(usage());
110 break;
111 default:
112 usage();
113 }ARGEND
115 if(argc != 1)
116 usage();
118 if(vtparsescore(argv[0], &prefix, score) < 0)
119 sysfatal("could not parse score: %r");
121 buf = vtmallocz(VtMaxLumpSize);
122 z = vtdial(host);
123 if(z == nil)
124 sysfatal("dialing venti: %r");
125 if(vtconnect(z) < 0)
126 sysfatal("vtconnect src: %r");
128 for(type=0; type<VtMaxType; type++){
129 n = vtread(z, score, type, buf, VtMaxLumpSize);
130 if(n >= 0)
131 goto havetype;
133 sysfatal("cannot find block %V", score);
135 havetype:
136 Binit(&bout, 1, OWRITE);
137 dump(0, score, type);
138 Bflush(&bout);
139 threadexitsall(nil);