copy.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. int fast;
  5. VtSession *zsrc, *zdst;
  6. void
  7. usage(void)
  8. {
  9. fprint(2, "usage: copy src-host dst-host score [type]\n");
  10. exits("usage");
  11. }
  12. int
  13. parseScore(uchar *score, char *buf, int n)
  14. {
  15. int i, c;
  16. memset(score, 0, VtScoreSize);
  17. if(n < VtScoreSize*2)
  18. return 0;
  19. for(i=0; i<VtScoreSize*2; i++) {
  20. if(buf[i] >= '0' && buf[i] <= '9')
  21. c = buf[i] - '0';
  22. else if(buf[i] >= 'a' && buf[i] <= 'f')
  23. c = buf[i] - 'a' + 10;
  24. else if(buf[i] >= 'A' && buf[i] <= 'F')
  25. c = buf[i] - 'A' + 10;
  26. else {
  27. return 0;
  28. }
  29. if((i & 1) == 0)
  30. c <<= 4;
  31. score[i>>1] |= c;
  32. }
  33. return 1;
  34. }
  35. void
  36. walk(uchar score[VtScoreSize], uint type, int base)
  37. {
  38. int i, n, sub;
  39. uchar *buf;
  40. VtEntry e;
  41. VtRoot root;
  42. if(memcmp(score, vtZeroScore, VtScoreSize) == 0)
  43. return;
  44. buf = vtMemAllocZ(VtMaxLumpSize);
  45. if(fast && vtRead(zdst, score, type, buf, VtMaxLumpSize) >= 0){
  46. fprint(2, "skip %V\n", score);
  47. free(buf);
  48. return;
  49. }
  50. n = vtRead(zsrc, score, type, buf, VtMaxLumpSize);
  51. if(n < 0){
  52. fprint(2, "warning: could not read block %V %d: %R", score, type);
  53. return;
  54. }
  55. switch(type){
  56. case VtRootType:
  57. if(!vtRootUnpack(&root, buf)){
  58. fprint(2, "warning: could not unpack root in %V %d\n", score, type);
  59. break;
  60. }
  61. walk(root.score, VtDirType, 0);
  62. walk(root.prev, VtRootType, 0);
  63. break;
  64. case VtDirType:
  65. for(i=0; i<n/VtEntrySize; i++){
  66. if(!vtEntryUnpack(&e, buf, i)){
  67. fprint(2, "warning: could not unpack entry #%d in %V %d\n", i, score, type);
  68. continue;
  69. }
  70. if(!(e.flags & VtEntryActive))
  71. continue;
  72. if(e.flags&VtEntryDir)
  73. base = VtDirType;
  74. else
  75. base = VtDataType;
  76. if(e.depth == 0)
  77. sub = base;
  78. else
  79. sub = VtPointerType0+e.depth-1;
  80. walk(e.score, sub, base);
  81. }
  82. break;
  83. case VtDataType:
  84. break;
  85. default: /* pointers */
  86. if(type == VtPointerType0)
  87. sub = base;
  88. else
  89. sub = type-1;
  90. for(i=0; i<n; i+=VtScoreSize)
  91. if(memcmp(buf+i, vtZeroScore, VtScoreSize) != 0)
  92. walk(buf+i, sub, base);
  93. break;
  94. }
  95. if(!vtWrite(zdst, score, type, buf, n))
  96. fprint(2, "warning: could not write block %V %d: %R", score, type);
  97. free(buf);
  98. }
  99. int
  100. main(int argc, char *argv[])
  101. {
  102. int type, n;
  103. uchar score[VtScoreSize];
  104. uchar *buf;
  105. ARGBEGIN{
  106. case 'f':
  107. fast = 1;
  108. break;
  109. default:
  110. usage();
  111. break;
  112. }ARGEND
  113. if(argc != 3 && argc != 4)
  114. usage();
  115. vtAttach();
  116. fmtinstall('V', vtScoreFmt);
  117. fmtinstall('R', vtErrFmt);
  118. if(!parseScore(score, argv[2], strlen(argv[2])))
  119. vtFatal("could not parse score: %s", vtGetError());
  120. buf = vtMemAllocZ(VtMaxLumpSize);
  121. zsrc = vtDial(argv[0], 0);
  122. if(zsrc == nil)
  123. vtFatal("could not dial src server: %R");
  124. if(!vtConnect(zsrc, 0))
  125. sysfatal("vtConnect src: %r");
  126. zdst = vtDial(argv[1], 0);
  127. if(zdst == nil)
  128. vtFatal("could not dial dst server: %R");
  129. if(!vtConnect(zdst, 0))
  130. sysfatal("vtConnect dst: %r");
  131. if(argc == 4){
  132. type = atoi(argv[3]);
  133. n = vtRead(zsrc, score, type, buf, VtMaxLumpSize);
  134. if(n < 0)
  135. vtFatal("could not read block: %R");
  136. }else{
  137. for(type=0; type<VtMaxType; type++){
  138. n = vtRead(zsrc, score, type, buf, VtMaxLumpSize);
  139. if(n >= 0)
  140. break;
  141. }
  142. if(type == VtMaxType)
  143. vtFatal("could not find block %V of any type", score);
  144. }
  145. walk(score, type, VtDirType);
  146. if(!vtSync(zdst))
  147. vtFatal("could not sync dst server: %R");
  148. vtDetach();
  149. exits(0);
  150. return 0; /* shut up compiler */
  151. }