4 typedef struct Source Source;
15 uchar score[VtScoreSize];
26 uchar fscore[VtScoreSize];
29 int vtGetUint16(uchar *p);
30 ulong vtGetUint32(uchar *p);
31 uvlong vtGetUint48(uchar *p);
33 int parseScore(uchar *score, char *buf, int n);
34 void readRoot(VtRoot*, uchar *score, char *file);
35 int dumpDir(Source*, int indent);
38 main(int argc, char *argv[])
41 uchar score[VtScoreSize];
43 uchar buf[VtMaxLumpSize];
57 if(p == nil || !parseScore(fscore, p, strlen(p)))
67 bout = vtMemAllocZ(sizeof(Biobuf));
68 Binit(bout, 1, OWRITE);
75 fmtinstall('V', vtScoreFmt);
76 fmtinstall('R', vtErrFmt);
80 vtFatal("could not connect to server: %s", vtGetError());
83 sysfatal("vtConnect: %r");
85 readRoot(&root, score, argv[0]);
87 bsize = root.blockSize;
89 Bprint(bout, "score: %V\n", score);
90 Bprint(bout, "version: %d\n", ver);
91 Bprint(bout, "name: %s\n", root.name);
92 Bprint(bout, "type: %s\n", root.type);
93 Bprint(bout, "bsize: %d\n", bsize);
94 Bprint(bout, "prev: %V\n", root.prev);
99 sysfatal("unknown version");
104 n = vtRead(z, root.score, VtDirType, buf, bsize);
106 sysfatal("could not read root dir");
108 /* fake up top level source */
109 memset(&source, 0, sizeof(source));
110 memmove(source.score, root.score, VtScoreSize);
111 source.psize = bsize;
112 source.dsize = bsize;
128 sourcePrint(Source *s, int indent, int entry)
134 for(i=0; i<indent; i++)
136 Bprint(bout, "%4d", entry);
138 /* dir size in directory entries */
140 ne = s->dsize/VtEntrySize;
141 size = ne*(s->size/s->dsize) + (s->size%s->dsize)/VtEntrySize;
145 Bprint(bout, ": gen: %lud size: %llud",
148 Bprint(bout, ": %V", s->score);
150 Bprint(bout, ": gen: %lud psize: %d dsize: %d",
151 s->gen, s->psize, s->dsize);
152 Bprint(bout, " depth: %d size: %llud: %V",
153 s->depth, size, s->score);
157 Bprint(bout, ": reserved not emtpy");
163 parse(Source *s, uchar *p)
167 memset(s, 0, sizeof(*s));
168 if(!vtEntryUnpack(&dir, p, 0))
171 if(!(dir.flags & VtEntryActive))
176 s->psize = dir.psize;
179 memmove(s->score, dir.score, VtScoreSize);
180 if(dir.flags & VtEntryDir)
182 s->depth = dir.depth;
188 sourceRead(Source *s, ulong block, uchar *p, int n)
190 uchar buf[VtMaxLumpSize];
191 uchar score[VtScoreSize];
193 int elem[VtPointerDepth];
195 memmove(score, s->score, VtScoreSize);
197 np = s->psize/VtScoreSize;
198 for(i=0; i<s->depth; i++) {
199 elem[i] = block % np;
204 for(i=s->depth-1; i>=0; i--) {
205 nn = vtRead(z, score, VtPointerType0+i, buf, s->psize);
209 if(!vtSha1Check(score, buf, nn)) {
210 vtSetError("vtSha1Check failed on root block");
214 if((elem[i]+1)*VtScoreSize > nn)
216 memmove(score, buf + elem[i]*VtScoreSize, VtScoreSize);
224 nn = vtRead(z, score, type, p, n);
228 if(!vtSha1Check(score, p, nn)) {
229 vtSetError("vtSha1Check failed on root block");
237 dumpFileContents(Source *s)
240 uchar buf[VtMaxLumpSize];
242 nb = (s->size + s->dsize - 1)/s->dsize;
243 lb = s->size%s->dsize;
244 for(i=0; i<nb; i++) {
245 memset(buf, 0, s->dsize);
246 n = sourceRead(s, i, buf, s->dsize);
248 fprint(2, "could not read block: %d: %s\n", i, vtGetError());
252 Bwrite(bout, buf, s->dsize);
254 Bwrite(bout, buf, lb);
259 dumpFile(Source *s, int indent)
262 uchar buf[VtMaxLumpSize];
263 uchar score[VtScoreSize];
265 nb = (s->size + s->dsize - 1)/s->dsize;
266 for(i=0; i<nb; i++) {
267 memset(buf, 0, s->dsize);
268 n = sourceRead(s, i, buf, s->dsize);
270 fprint(2, "could not read block: %d: %s\n", i, vtGetError());
273 for(j=0; j<indent; j++)
275 vtSha1(score, buf, n);
276 Bprint(bout, "%4d: size: %ud: %V\n", i, n, score);
281 dumpDir(Source *s, int indent)
283 int pb, ne, nb, i, j, n, entry;
284 uchar buf[VtMaxLumpSize];
287 pb = s->dsize/VtEntrySize;
288 ne = pb*(s->size/s->dsize) + (s->size%s->dsize)/VtEntrySize;
289 nb = (s->size + s->dsize - 1)/s->dsize;
290 for(i=0; i<nb; i++) {
291 memset(buf, 0, s->dsize);
292 n = sourceRead(s, i, buf, s->dsize);
294 fprint(2, "could not read block: %d: %s\n", i, vtGetError());
297 for(j=0; j<pb; j++) {
301 parse(&ss, buf + j * VtEntrySize);
304 sourcePrint(&ss, indent, entry);
305 else if(memcmp(ss.score, fscore, VtScoreSize) == 0) {
306 dumpFileContents(&ss);
311 if(!dumpDir(&ss, indent+1))
314 dumpFile(&ss, indent+1);
323 fprint(2, "%s: [file]\n", argv0);
328 parseScore(uchar *score, char *buf, int n)
332 memset(score, 0, VtScoreSize);
334 if(n < VtScoreSize*2)
336 for(i=0; i<VtScoreSize*2; i++) {
337 if(buf[i] >= '0' && buf[i] <= '9')
339 else if(buf[i] >= 'a' && buf[i] <= 'f')
340 c = buf[i] - 'a' + 10;
341 else if(buf[i] >= 'A' && buf[i] <= 'F')
342 c = buf[i] - 'A' + 10;
356 readRoot(VtRoot *root, uchar *score, char *file)
359 uchar buf[VtRootSize];
365 fd = open(file, OREAD);
367 sysfatal("could not open file: %s: %r\n", file);
369 n = readn(fd, buf, sizeof(buf)-1);
371 sysfatal("read failed: %r\n");
376 if(!parseScore(score, (char*)(buf+i), n-i))
378 nn = vtRead(z, score, VtRootType, buf, VtRootSize);
381 sysfatal("vtRead on root too short");
382 if(!vtSha1Check(score, buf, VtRootSize))
383 sysfatal("vtSha1Check failed on root block");
384 if(!vtRootUnpack(root, buf))
385 sysfatal("could not parse root: %r");
390 sysfatal("could not find root");