Blob


1 #include "stdinc.h"
2 #include "dat.h"
3 #include "fns.h"
5 int fast;
7 VtConn *zsrc, *zdst;
9 void
10 usage(void)
11 {
12 fprint(2, "usage: copy src-host dst-host score [type]\n");
13 threadexitsall("usage");
14 }
16 int
17 parsescore(uchar *score, char *buf, int n)
18 {
19 int i, c;
21 memset(score, 0, VtScoreSize);
23 if(n < VtScoreSize*2)
24 return -1;
25 for(i=0; i<VtScoreSize*2; i++) {
26 if(buf[i] >= '0' && buf[i] <= '9')
27 c = buf[i] - '0';
28 else if(buf[i] >= 'a' && buf[i] <= 'f')
29 c = buf[i] - 'a' + 10;
30 else if(buf[i] >= 'A' && buf[i] <= 'F')
31 c = buf[i] - 'A' + 10;
32 else {
33 return -1;
34 }
36 if((i & 1) == 0)
37 c <<= 4;
39 score[i>>1] |= c;
40 }
41 return 0;
42 }
44 void
45 walk(uchar score[VtScoreSize], uint type, int base)
46 {
47 int i, n, sub;
48 uchar *buf;
49 VtEntry e;
50 VtRoot root;
52 if(memcmp(score, vtzeroscore, VtScoreSize) == 0)
53 return;
55 buf = vtmallocz(VtMaxLumpSize);
56 if(fast && vtread(zdst, score, type, buf, VtMaxLumpSize) >= 0){
57 fprint(2, "skip %V\n", score);
58 free(buf);
59 return;
60 }
62 n = vtread(zsrc, score, type, buf, VtMaxLumpSize);
63 if(n < 0){
64 fprint(2, "warning: could not read block %V %d: %r", score, type);
65 return;
66 }
68 switch(type){
69 case VtRootType:
70 if(vtrootunpack(&root, buf) < 0){
71 fprint(2, "warning: could not unpack root in %V %d\n", score, type);
72 break;
73 }
74 walk(root.score, VtDirType, 0);
75 walk(root.prev, VtRootType, 0);
76 break;
78 case VtDirType:
79 for(i=0; i<n/VtEntrySize; i++){
80 if(vtentryunpack(&e, buf, i) < 0){
81 fprint(2, "warning: could not unpack entry #%d in %V %d\n", i, score, type);
82 continue;
83 }
84 if(!(e.flags & VtEntryActive))
85 continue;
86 if(e.flags&VtEntryDir)
87 base = VtDirType;
88 else
89 base = VtDataType;
90 sub = base | ((e.flags&VtEntryDepthMask)>>VtEntryDepthShift);
91 walk(e.score, sub, base);
92 }
93 break;
95 case VtDataType:
96 break;
98 default: /* pointers */
99 for(i=0; i<n; i+=VtScoreSize)
100 if(memcmp(buf+i, vtzeroscore, VtScoreSize) != 0)
101 walk(buf+i, type-1, base);
102 break;
105 if(vtwrite(zdst, score, type, buf, n) < 0)
106 fprint(2, "warning: could not write block %V %d: %r", score, type);
107 free(buf);
110 void
111 threadmain(int argc, char *argv[])
113 int type, n;
114 uchar score[VtScoreSize];
115 uchar *buf;
117 ARGBEGIN{
118 case 'f':
119 fast = 1;
120 break;
121 default:
122 usage();
123 break;
124 }ARGEND
126 if(argc != 3 && argc != 4)
127 usage();
129 fmtinstall('V', vtscorefmt);
131 if(parsescore(score, argv[2], strlen(argv[2]) < 0))
132 sysfatal("could not parse score: %r");
134 buf = vtmallocz(VtMaxLumpSize);
136 zsrc = vtdial(argv[0]);
137 if(zsrc == nil)
138 sysfatal("could not dial src server: %r");
139 if(vtconnect(zsrc) < 0)
140 sysfatal("vtconnect src: %r");
142 zdst = vtdial(argv[1]);
143 if(zdst == nil)
144 sysfatal("could not dial dst server: %r");
145 if(vtconnect(zdst) < 0)
146 sysfatal("vtconnect dst: %r");
148 if(argc == 4){
149 type = atoi(argv[3]);
150 n = vtread(zsrc, score, type, buf, VtMaxLumpSize);
151 if(n < 0)
152 sysfatal("could not read block: %r");
153 }else{
154 for(type=0; type<VtMaxType; type++){
155 n = vtread(zsrc, score, type, buf, VtMaxLumpSize);
156 if(n >= 0)
157 break;
159 if(type == VtMaxType)
160 sysfatal("could not find block %V of any type", score);
163 walk(score, type, VtDirType);
165 if(vtsync(zdst) < 0)
166 sysfatal("could not sync dst server: %r");
168 threadexitsall(0);