findscore.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. memset(&ie, 0, sizeof(IEntry));
  42. for(clump = 0; clump < arena->clumps; clump += n){
  43. n = ClumpChunks;
  44. if(n > arena->clumps - clump)
  45. n = arena->clumps - clump;
  46. if(readClumpInfos(arena, clump, cis, n) != n){
  47. setErr(EOk, "arena directory read failed: %R");
  48. break;
  49. }
  50. for(i = 0; i < n; i++){
  51. ci = &cis[i];
  52. if(scoreEq(score, ci->score)){
  53. fprint(2, "found at clump=%d with type=%d size=%d csize=%d position=%lld\n",
  54. clump + i, ci->type, ci->uncsize, ci->size, a);
  55. found++;
  56. }
  57. a += ci->size + ClumpSize;
  58. }
  59. }
  60. free(cis);
  61. return found;
  62. }
  63. void
  64. usage(void)
  65. {
  66. fprint(2, "usage: findscore [-v] arenafile score\n");
  67. exits(0);
  68. }
  69. int
  70. main(int argc, char *argv[])
  71. {
  72. ArenaPart *ap;
  73. Part *part;
  74. char *file;
  75. u8int score[VtScoreSize];
  76. int i, found;
  77. fmtinstall('V', vtScoreFmt);
  78. fmtinstall('R', vtErrFmt);
  79. vtAttach();
  80. statsInit();
  81. ARGBEGIN{
  82. case 'v':
  83. verbose++;
  84. break;
  85. default:
  86. usage();
  87. break;
  88. }ARGEND
  89. readonly = 1;
  90. if(argc != 2)
  91. usage();
  92. file = argv[0];
  93. if(!strScore(argv[1], score))
  94. fatal("bad score %s\n", argv[1]);
  95. part = initPart(file, 0);
  96. if(part == nil)
  97. fatal("can't open partition %s: %r", file);
  98. ap = initArenaPart(part);
  99. if(ap == nil)
  100. fatal("can't initialize arena partition in %s: %R", file);
  101. if(verbose > 1){
  102. printArenaPart(2, ap);
  103. fprint(2, "\n");
  104. }
  105. initDCache(8 * MaxDiskBlock);
  106. found = 0;
  107. for(i = 0; i < ap->narenas; i++)
  108. found += findscore(ap->arenas[i], score);
  109. print("found %d occurences of %V\n", found, score);
  110. if(verbose > 1)
  111. printStats();
  112. exits(0);
  113. return 0; /* shut up stupid compiler */
  114. }