Blob


1 #include "stdinc.h"
2 #include "dat.h"
3 #include "fns.h"
5 enum
6 {
7 ClumpChunks = 32*1024
8 };
10 static int verbose;
12 int
13 clumpinfoeq(ClumpInfo *c, ClumpInfo *d)
14 {
15 return c->type == d->type
16 && c->size == d->size
17 && c->uncsize == d->uncsize
18 && scorecmp(c->score, d->score)==0;
19 }
21 /*
22 * synchronize the clump info directory with
23 * with the clumps actually stored in the arena.
24 * the directory should be at least as up to date
25 * as the arena's trailer.
26 *
27 * checks/updates at most n clumps.
28 *
29 * returns 1 if ok, -1 if an error occured, 0 if blocks were updated
30 */
31 int
32 findscore(Arena *arena, uchar *score)
33 {
34 IEntry ie;
35 ClumpInfo *ci, *cis;
36 u64int a;
37 u32int clump;
38 int i, n, found;
40 //ZZZ remove fprint?
41 if(arena->clumps)
42 fprint(2, "reading directory for arena=%s with %d entries\n", arena->name, arena->clumps);
44 cis = MKN(ClumpInfo, ClumpChunks);
45 found = 0;
46 a = 0;
47 memset(&ie, 0, sizeof(IEntry));
48 for(clump = 0; clump < arena->clumps; clump += n){
49 n = ClumpChunks;
50 if(n > arena->clumps - clump)
51 n = arena->clumps - clump;
52 if(readclumpinfos(arena, clump, cis, n) != n){
53 seterr(EOk, "arena directory read failed: %r");
54 break;
55 }
57 for(i = 0; i < n; i++){
58 ci = &cis[i];
59 if(scorecmp(score, ci->score)==0){
60 fprint(2, "found at clump=%d with type=%d size=%d csize=%d position=%lld\n",
61 clump + i, ci->type, ci->uncsize, ci->size, a);
62 found++;
63 }
64 a += ci->size + ClumpSize;
65 }
66 }
67 free(cis);
68 return found;
69 }
71 void
72 usage(void)
73 {
74 fprint(2, "usage: findscore [-v] arenafile score\n");
75 threadexitsall(0);
76 }
78 void
79 threadmain(int argc, char *argv[])
80 {
81 ArenaPart *ap;
82 Part *part;
83 char *file;
84 u8int score[VtScoreSize];
85 int i, found;
87 fmtinstall('V', vtscorefmt);
88 statsinit();
90 ARGBEGIN{
91 case 'v':
92 verbose++;
93 break;
94 default:
95 usage();
96 break;
97 }ARGEND
99 readonly = 1;
101 if(argc != 2)
102 usage();
104 file = argv[0];
105 if(strscore(argv[1], score) < 0)
106 sysfatal("bad score %s\n", argv[1]);
108 part = initpart(file, 0);
109 if(part == nil)
110 sysfatal("can't open partition %s: %r", file);
112 ap = initarenapart(part);
113 if(ap == nil)
114 sysfatal("can't initialize arena partition in %s: %r", file);
116 if(verbose > 1){
117 printarenapart(2, ap);
118 fprint(2, "\n");
121 initdcache(8 * MaxDiskBlock);
123 found = 0;
124 for(i = 0; i < ap->narenas; i++)
125 found += findscore(ap->arenas[i], score);
127 print("found %d occurances of %V\n", found, score);
129 if(verbose > 1)
130 printstats();
131 threadexitsall(0);