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 void readroot(VtRoot*, uchar *score, char *file);
34 int dumpdir(Source*, int indent);
35 int timevtread(VtConn*, uchar*, int, void*, int);
37 int mainstacksize = 512*1024;
40 threadmain(int argc, char *argv[])
42 char *host = nil, *pref;
43 uchar score[VtScoreSize];
47 uchar buf[VtMaxLumpSize];
59 if(p == nil || vtparsescore(p, &pref, fscore) < 0 || !pref || strcmp(pref, "vac") != 0)
67 bout = vtmallocz(sizeof(Biobuf));
68 Binit(bout, 1, OWRITE);
73 fmtinstall('V', vtscorefmt);
74 fmtinstall('H', encodefmt);
78 sysfatal("could not connect to server: %r");
81 sysfatal("vtconnect: %r");
83 readroot(&root, score, argv[0]);
84 bsize = root.blocksize;
86 Bprint(bout, "score: %V\n", score);
87 Bprint(bout, "name: %s\n", root.name);
88 Bprint(bout, "type: %s\n", root.type);
89 Bprint(bout, "bsize: %d\n", bsize);
90 Bprint(bout, "prev: %V\n", root.prev);
93 fprint(2, "read...\n");
94 n = timevtread(z, root.score, VtDirType, buf, bsize);
96 sysfatal("could not read root dir");
99 /* fake up top level source */
100 memset(&source, 0, sizeof(source));
101 memmove(source.score, root.score, VtScoreSize);
102 source.psize = bsize;
103 source.dsize = bsize;
118 sourceprint(Source *s, int indent, int entry)
124 for(i=0; i<indent; i++)
126 Bprint(bout, "%4d", entry);
128 /* dir size in directory entries */
130 ne = s->dsize/VtEntrySize;
131 size = ne*(s->size/s->dsize) + (s->size%s->dsize)/VtEntrySize;
135 Bprint(bout, ": gen: %lud size: %llud",
138 Bprint(bout, ": %V", s->score);
140 Bprint(bout, ": gen: %lud psize: %d dsize: %d",
141 s->gen, s->psize, s->dsize);
142 Bprint(bout, " depth: %d size: %llud: %V",
143 s->depth, size, s->score);
147 Bprint(bout, ": reserved not emtpy");
153 parse(Source *s, uchar *p)
157 memset(s, 0, sizeof(*s));
158 if(vtentryunpack(&dir, p, 0) < 0)
161 if(!(dir.flags & VtEntryActive))
166 s->psize = dir.psize;
167 s->dsize = dir.dsize;
169 memmove(s->score, dir.score, VtScoreSize);
170 if((dir.type&~VtTypeDepthMask) == VtDirType)
172 s->depth = dir.type&VtTypeDepthMask;
177 sourceread(Source *s, ulong block, uchar *p, int n)
180 uchar score[VtScoreSize];
182 int elem[VtPointerDepth];
184 buf = vtmalloc(VtMaxLumpSize);
186 memmove(score, s->score, VtScoreSize);
188 np = s->psize/VtScoreSize;
189 for(i=0; i<s->depth; i++) {
190 elem[i] = block % np;
195 for(i=s->depth-1; i>=0; i--) {
196 nn = timevtread(z, score, (s->dir ? VtDirType : VtDataType)+1+i, buf, s->psize);
198 fprint(2, "vtread %V %d: %r\n", score, (s->dir ? VtDirType : VtDataType)+1+i);
203 if((elem[i]+1)*VtScoreSize > nn){
207 memmove(score, buf + elem[i]*VtScoreSize, VtScoreSize);
215 nn = timevtread(z, score, type, p, n);
217 fprint(2, "vtread %V %d: %r\n", score, type);
228 dumpfilecontents(Source *s)
231 uchar buf[VtMaxLumpSize];
233 nb = (s->size + s->dsize - 1)/s->dsize;
234 lb = s->size%s->dsize;
235 for(i=0; i<nb; i++) {
236 memset(buf, 0, s->dsize);
237 n = sourceread(s, i, buf, s->dsize);
239 fprint(2, "could not read block: %d: %r\n", i);
243 Bwrite(bout, buf, s->dsize);
245 Bwrite(bout, buf, lb);
250 dumpfile(Source *s, int indent)
254 uchar score[VtScoreSize];
256 buf = vtmalloc(VtMaxLumpSize);
257 nb = (s->size + s->dsize - 1)/s->dsize;
258 for(i=0; i<nb; i++) {
259 memset(buf, 0, s->dsize);
260 n = sourceread(s, i, buf, s->dsize);
262 fprint(2, "could not read block: %d: %r\n", i);
265 for(j=0; j<indent; j++)
267 sha1(buf, n, score, nil);
268 Bprint(bout, "%4d: size: %ud: %V\n", i, n, score);
274 dumpdir(Source *s, int indent)
276 int pb, ne, nb, i, j, n, entry;
280 buf = vtmalloc(VtMaxLumpSize);
281 pb = s->dsize/VtEntrySize;
282 ne = pb*(s->size/s->dsize) + (s->size%s->dsize)/VtEntrySize;
283 nb = (s->size + s->dsize - 1)/s->dsize;
284 for(i=0; i<nb; i++) {
285 memset(buf, 0, s->dsize);
286 n = sourceread(s, i, buf, s->dsize);
288 fprint(2, "could not read block: %d: %r\n", i);
291 for(j=0; j<pb; j++) {
295 parse(&ss, buf + j * VtEntrySize);
298 sourceprint(&ss, indent, entry);
299 else if(memcmp(ss.score, fscore, VtScoreSize) == 0) {
300 dumpfilecontents(&ss);
306 if(dumpdir(&ss, indent+1) < 0){
311 dumpfile(&ss, indent+1);
321 fprint(2, "%s: [file]\n", argv0);
322 threadexits("usage");
326 readroot(VtRoot *root, uchar *score, char *file)
330 char buf[VtRootSize];
336 fd = open(file, OREAD);
338 sysfatal("could not open file: %s: %r\n", file);
340 n = readn(fd, buf, sizeof(buf)-1);
342 sysfatal("read failed: %r\n");
343 if(n==0 || buf[n-1] != '\n')
344 sysfatal("not a root file");
348 if(vtparsescore(buf, &pref, score) < 0){
349 sysfatal("not a root file");
351 nn = timevtread(z, score, VtRootType, buf, VtRootSize);
353 sysfatal("cannot read root %V", score);
354 if(vtrootunpack(root, buf) < 0)
355 sysfatal("cannot parse root: %r");
359 timevtread(VtConn *z, uchar *score, int type, void *buf, int nbuf)
366 n = vtread(z, score, type, buf, nbuf);
368 fprint(2, "read %V: %.6f seconds\n", score, (t1-t0)/1.e9);
371 return vtread(z, score, type, buf, nbuf);