findscore.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "stdinc.h"
  2. #include "dat.h"
  3. #include "fns.h"
  4. enum
  5. {
  6. ClumpChunks = 32*1024
  7. };
  8. static int verbose;
  9. static int writeClumpHead(Arena *arena, u64int aa, Clump *cl);
  10. static int writeClumpMagic(Arena *arena, u64int aa, u32int magic);
  11. int
  12. clumpInfoEq(ClumpInfo *c, ClumpInfo *d)
  13. {
  14. return c->type == d->type
  15. && c->size == d->size
  16. && c->uncsize == d->uncsize
  17. && scoreEq(c->score, d->score);
  18. }
  19. /*
  20. * synchronize the clump info directory with
  21. * with the clumps actually stored in the arena.
  22. * the directory should be at least as up to date
  23. * as the arena's trailer.
  24. *
  25. * checks/updates at most n clumps.
  26. *
  27. * returns 1 if ok, -1 if an error occured, 0 if blocks were updated
  28. */
  29. int
  30. findscore(Arena *arena, uchar *score)
  31. {
  32. IEntry ie;
  33. ClumpInfo *ci, *cis;
  34. u64int a;
  35. u32int clump;
  36. int i, n, found;
  37. if(arena->clumps && verbose)
  38. fprint(2, "reading directory for arena=%s with %d entries\n", arena->name, arena->clumps);
  39. cis = MKN(ClumpInfo, ClumpChunks);
  40. found = 0;
  41. a = 0;
  42. memset(&ie, 0, sizeof(IEntry));
  43. for(clump = 0; clump < arena->clumps; clump += n){
  44. n = ClumpChunks;
  45. if(n > arena->clumps - clump)
  46. n = arena->clumps - clump;
  47. if(readClumpInfos(arena, clump, cis, n) != n){
  48. setErr(EOk, "arena directory read failed: %R");
  49. break;
  50. }
  51. for(i = 0; i < n; i++){
  52. ci = &cis[i];
  53. if(scoreEq(score, ci->score)){
  54. fprint(2, "found at clump=%d with type=%d size=%d csize=%d position=%lld\n",
  55. clump + i, ci->type, ci->uncsize, ci->size, a);
  56. found++;
  57. }
  58. a += ci->size + ClumpSize;
  59. }
  60. }
  61. free(cis);
  62. return found;
  63. }
  64. void
  65. usage(void)
  66. {
  67. fprint(2, "usage: findscore [-v] arenafile score\n");
  68. exits(0);
  69. }
  70. int
  71. main(int argc, char *argv[])
  72. {
  73. ArenaPart *ap;
  74. Part *part;
  75. char *file;
  76. u8int score[VtScoreSize];
  77. int i, found;
  78. fmtinstall('V', vtScoreFmt);
  79. fmtinstall('R', vtErrFmt);
  80. vtAttach();
  81. statsInit();
  82. ARGBEGIN{
  83. case 'v':
  84. verbose++;
  85. break;
  86. default:
  87. usage();
  88. break;
  89. }ARGEND
  90. readonly = 1;
  91. if(argc != 2)
  92. usage();
  93. file = argv[0];
  94. if(!strScore(argv[1], score))
  95. fatal("bad score %s\n", argv[1]);
  96. part = initPart(file, 0);
  97. if(part == nil)
  98. fatal("can't open partition %s: %r", file);
  99. ap = initArenaPart(part);
  100. if(ap == nil)
  101. fatal("can't initialize arena partition in %s: %R", file);
  102. if(verbose > 1){
  103. printArenaPart(2, ap);
  104. fprint(2, "\n");
  105. }
  106. initDCache(8 * MaxDiskBlock);
  107. found = 0;
  108. for(i = 0; i < ap->narenas; i++)
  109. found += findscore(ap->arenas[i], score);
  110. print("found %d occurences of %V\n", found, score);
  111. if(verbose > 1)
  112. printStats();
  113. exits(0);
  114. return 0; /* shut up stupid compiler */
  115. }