17 uchar zeroscore[VtScoreSize]; /* all zeros */
19 typedef struct ScoreTree ScoreTree;
23 uchar score[VtScoreSize];
31 scoretreecmp(Avl *va, Avl *vb)
39 i = memcmp(a->score, b->score, VtScoreSize);
42 return a->type - b->type;
46 havevisited(uchar score[VtScoreSize], int type)
52 memmove(a.score, score, VtScoreSize);
54 return avllookup(scoretree, &a) != nil;
58 markvisited(uchar score[VtScoreSize], int type)
64 a = binalloc(&scorebin, sizeof *a, 1);
65 memmove(a->score, score, VtScoreSize);
67 avlinsert(scoretree, a);
73 fprint(2, "usage: %s [-fimrv] [-t type] srchost dsthost score\n", argv0);
78 walk(uchar score[VtScoreSize], uint type, int base)
82 uchar nscore[VtScoreSize];
86 if(memcmp(score, vtzeroscore, VtScoreSize) == 0 || memcmp(score, zeroscore, VtScoreSize) == 0)
89 if(havevisited(score, type)){
94 buf = vtmallocz(VtMaxLumpSize);
95 if(fast && vtread(zdst, score, type, buf, VtMaxLumpSize) >= 0){
97 fprint(2, "skip %V\n", score);
102 n = vtread(zsrc, score, type, buf, VtMaxLumpSize);
106 memmove(score, vtzeroscore, VtScoreSize);
107 }else if(!ignoreerrors)
108 sysfatal("reading block %V (type %d): %r", score, type);
114 if(vtrootunpack(&root, buf) < 0){
115 fprint(2, "warning: could not unpack root in %V %d\n", score, type);
118 walk(root.prev, VtRootType, 0);
119 walk(root.score, VtDirType, 0);
121 vtrootpack(&root, buf); /* walk might have changed score */
125 for(i=0; i<n/VtEntrySize; i++){
126 if(vtentryunpack(&e, buf, i) < 0){
127 fprint(2, "warning: could not unpack entry #%d in %V %d\n", i, score, type);
130 if(!(e.flags & VtEntryActive))
132 walk(e.score, e.type, e.type&VtTypeBaseMask);
134 * Don't repack unless we're rewriting -- some old
135 * vac files have psize==0 and dsize==0, and these
136 * get rewritten by vtentryunpack to have less strange
137 * block sizes. So vtentryunpack; vtentrypack does not
138 * guarantee to preserve the exact bytes in buf.
141 vtentrypack(&e, buf, i);
148 default: /* pointers */
149 for(i=0; i<n; i+=VtScoreSize)
150 if(memcmp(buf+i, vtzeroscore, VtScoreSize) != 0)
151 walk(buf+i, type-1, base);
156 if(vtwrite(zdst, nscore, type, buf, n) < 0){
157 /* figure out score for better error message */
158 /* can't use input argument - might have changed contents */
159 n = vtzerotruncate(type, buf, n);
160 sha1(buf, n, score, nil);
161 sysfatal("writing block %V (type %d): %r", score, type);
163 if(!rewrite && memcmp(score, nscore, VtScoreSize) != 0){
164 fprint(2, "not rewriting: wrote %V got %V\n", score, nscore);
166 sysfatal("not rewriting: wrote %V got %V", score, nscore);
169 markvisited(score, type);
174 main(int argc, char *argv[])
177 uchar score[VtScoreSize];
181 fmtinstall('F', vtfcallfmt);
182 fmtinstall('V', vtscorefmt);
198 scoretree = avlcreate(scoretreecmp);
206 type = atoi(EARGF(usage()));
219 if(vtparsescore(argv[2], &prefix, score) < 0)
220 sysfatal("could not parse score: %r");
222 buf = vtmallocz(VtMaxLumpSize);
224 zsrc = vtdial(argv[0]);
226 sysfatal("could not dial src server: %r");
227 if(vtconnect(zsrc) < 0)
228 sysfatal("vtconnect src: %r");
230 zdst = vtdial(argv[1]);
232 sysfatal("could not dial dst server: %r");
233 if(vtconnect(zdst) < 0)
234 sysfatal("vtconnect dst: %r");
237 n = vtread(zsrc, score, type, buf, VtMaxLumpSize);
239 sysfatal("could not read block: %r");
241 for(type=0; type<VtMaxType; type++){
242 n = vtread(zsrc, score, type, buf, VtMaxLumpSize);
246 if(type == VtMaxType)
247 sysfatal("could not find block %V of any type", score);
250 walk(score, type, VtDirType);
252 print("%s:%V (%d pointers rewritten)\n", prefix, score, changes);
255 print("%d skipped, %d written\n", nskip, nwrite);
258 sysfatal("could not sync dst server: %r");