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);
37 threadmain(int argc, char *argv[])
39 char *host = nil, *pref;
40 uchar score[VtScoreSize];
42 uchar buf[VtMaxLumpSize];
56 if(p == nil || vtparsescore(p, &pref, fscore) < 0 || !pref || strcmp(pref, "vac") != 0)
64 bout = vtmallocz(sizeof(Biobuf));
65 Binit(bout, 1, OWRITE);
70 fmtinstall('V', vtscorefmt);
71 fmtinstall('H', encodefmt);
75 sysfatal("could not connect to server: %r");
78 sysfatal("vtconnect: %r");
80 readroot(&root, score, argv[0]);
81 bsize = root.blocksize;
83 Bprint(bout, "score: %V\n", score);
84 Bprint(bout, "name: %s\n", root.name);
85 Bprint(bout, "type: %s\n", root.type);
86 Bprint(bout, "bsize: %d\n", bsize);
87 Bprint(bout, "prev: %V\n", root.prev);
90 n = vtread(z, root.score, VtDirType, buf, bsize);
92 sysfatal("could not read root dir");
94 /* fake up top level source */
95 memset(&source, 0, sizeof(source));
96 memmove(source.score, root.score, VtScoreSize);
113 sourceprint(Source *s, int indent, int entry)
119 for(i=0; i<indent; i++)
121 Bprint(bout, "%4d", entry);
123 /* dir size in directory entries */
125 ne = s->dsize/VtEntrySize;
126 size = ne*(s->size/s->dsize) + (s->size%s->dsize)/VtEntrySize;
130 Bprint(bout, ": gen: %lud size: %llud",
133 Bprint(bout, ": %V", s->score);
135 Bprint(bout, ": gen: %lud psize: %d dsize: %d",
136 s->gen, s->psize, s->dsize);
137 Bprint(bout, " depth: %d size: %llud: %V",
138 s->depth, size, s->score);
142 Bprint(bout, ": reserved not emtpy");
148 parse(Source *s, uchar *p)
152 memset(s, 0, sizeof(*s));
153 if(vtentryunpack(&dir, p, 0) < 0)
156 if(!(dir.flags & VtEntryActive))
161 s->psize = dir.psize;
164 memmove(s->score, dir.score, VtScoreSize);
165 if((dir.type&~VtTypeDepthMask) == VtDirType)
167 fprint(2, "sdir %d type %d %.*H\n", s->dir, dir.type, VtEntrySize, p);
168 s->depth = dir.type&VtTypeDepthMask;
173 sourceread(Source *s, ulong block, uchar *p, int n)
176 uchar score[VtScoreSize];
178 int elem[VtPointerDepth];
180 buf = vtmalloc(VtMaxLumpSize);
182 memmove(score, s->score, VtScoreSize);
184 np = s->psize/VtScoreSize;
185 for(i=0; i<s->depth; i++) {
186 elem[i] = block % np;
191 for(i=s->depth-1; i>=0; i--) {
192 nn = vtread(z, score, (s->dir ? VtDirType : VtDataType)+1+i, buf, s->psize);
194 fprint(2, "vtread %V %d: %r\n", score, (s->dir ? VtDirType : VtDataType)+1+i);
199 if((elem[i]+1)*VtScoreSize > nn){
203 memmove(score, buf + elem[i]*VtScoreSize, VtScoreSize);
211 nn = vtread(z, score, type, p, n);
213 fprint(2, "vtread %V %d: %r\n", score, type);
224 dumpfilecontents(Source *s)
227 uchar buf[VtMaxLumpSize];
229 nb = (s->size + s->dsize - 1)/s->dsize;
230 lb = s->size%s->dsize;
231 for(i=0; i<nb; i++) {
232 memset(buf, 0, s->dsize);
233 n = sourceread(s, i, buf, s->dsize);
235 fprint(2, "could not read block: %d: %r\n", i);
239 Bwrite(bout, buf, s->dsize);
241 Bwrite(bout, buf, lb);
246 dumpfile(Source *s, int indent)
250 uchar score[VtScoreSize];
252 buf = vtmalloc(VtMaxLumpSize);
253 nb = (s->size + s->dsize - 1)/s->dsize;
254 for(i=0; i<nb; i++) {
255 memset(buf, 0, s->dsize);
256 n = sourceread(s, i, buf, s->dsize);
258 fprint(2, "could not read block: %d: %r\n", i);
261 for(j=0; j<indent; j++)
263 sha1(buf, n, score, nil);
264 Bprint(bout, "%4d: size: %ud: %V\n", i, n, score);
270 dumpdir(Source *s, int indent)
272 int pb, ne, nb, i, j, n, entry;
276 buf = vtmalloc(VtMaxLumpSize);
277 pb = s->dsize/VtEntrySize;
278 ne = pb*(s->size/s->dsize) + (s->size%s->dsize)/VtEntrySize;
279 nb = (s->size + s->dsize - 1)/s->dsize;
280 for(i=0; i<nb; i++) {
281 memset(buf, 0, s->dsize);
282 n = sourceread(s, i, buf, s->dsize);
284 fprint(2, "could not read block: %d: %r\n", i);
287 for(j=0; j<pb; j++) {
291 parse(&ss, buf + j * VtEntrySize);
294 sourceprint(&ss, indent, entry);
295 else if(memcmp(ss.score, fscore, VtScoreSize) == 0) {
296 dumpfilecontents(&ss);
302 if(dumpdir(&ss, indent+1) < 0){
307 dumpfile(&ss, indent+1);
317 fprint(2, "%s: [file]\n", argv0);
318 threadexits("usage");
322 readroot(VtRoot *root, uchar *score, char *file)
326 char buf[VtRootSize];
332 fd = open(file, OREAD);
334 sysfatal("could not open file: %s: %r\n", file);
336 n = readn(fd, buf, sizeof(buf)-1);
338 sysfatal("read failed: %r\n");
339 if(n==0 || buf[n-1] != '\n')
340 sysfatal("not a root file");
344 if(vtparsescore(buf, &pref, score) < 0){
345 sysfatal("not a root file");
347 nn = vtread(z, score, VtRootType, buf, VtRootSize);
349 sysfatal("cannot read root %V", score);
350 if(vtrootunpack(root, buf) < 0)
351 sysfatal("cannot parse root: %r");