#include #include #include #include #include #include int changes; int rewrite; int ignoreerrors; int fast; int verbose; int nskip; int nwrite; VtConn *zsrc, *zdst; uchar zeroscore[VtScoreSize]; /* all zeros */ typedef struct ScoreTree ScoreTree; struct ScoreTree { Avl avl; uchar score[VtScoreSize]; int type; }; Avltree *scoretree; Bin *scorebin; static int scoretreecmp(Avl *va, Avl *vb) { ScoreTree *a, *b; int i; a = (ScoreTree*)va; b = (ScoreTree*)vb; i = memcmp(a->score, b->score, VtScoreSize); if(i != 0) return i; return a->type - b->type; } static int havevisited(uchar score[VtScoreSize], int type) { ScoreTree a; if(scoretree == nil) return 0; memmove(a.score, score, VtScoreSize); a.type = type; return lookupavl(scoretree, &a.avl) != nil; } static void markvisited(uchar score[VtScoreSize], int type) { ScoreTree *a; Avl *old; if(scoretree == nil) return; a = binalloc(&scorebin, sizeof *a, 1); memmove(a->score, score, VtScoreSize); a->type = type; insertavl(scoretree, &a->avl, &old); } void usage(void) { fprint(2, "usage: %s [-fimrv] [-t type] srchost dsthost score\n", argv0); exits("usage"); } void walk(uchar score[VtScoreSize], uint type, int base) { int i, n; uchar *buf; uchar nscore[VtScoreSize]; VtEntry e; VtRoot root; if(memcmp(score, vtzeroscore, VtScoreSize) == 0 || memcmp(score, zeroscore, VtScoreSize) == 0) return; if(havevisited(score, type)){ nskip++; return; } buf = vtmallocz(VtMaxLumpSize); if(fast && vtread(zdst, score, type, buf, VtMaxLumpSize) >= 0){ if(verbose) fprint(2, "skip %V\n", score); free(buf); return; } n = vtread(zsrc, score, type, buf, VtMaxLumpSize); if(n < 0){ if(rewrite){ changes++; memmove(score, vtzeroscore, VtScoreSize); }else if(!ignoreerrors) sysfatal("reading block %V (type %d): %r", score, type); return; } switch(type){ case VtRootType: if(vtrootunpack(&root, buf) < 0){ fprint(2, "warning: could not unpack root in %V %d\n", score, type); break; } walk(root.prev, VtRootType, 0); walk(root.score, VtDirType, 0); if(rewrite) vtrootpack(&root, buf); /* walk might have changed score */ break; case VtDirType: for(i=0; i*VtEntrySize= 0) break; } if(type == VtMaxType) sysfatal("could not find block %V of any type", score); } walk(score, type, VtDirType); if(changes) print("%s:%V (%d pointers rewritten)\n", prefix, score, changes); if(verbose) print("%d skipped, %d written\n", nskip, nwrite); if(vtsync(zdst) < 0) sysfatal("could not sync dst server: %r"); exits(0); }