10 fprint(2, "usage: verifyarena [-v]\n");
15 readblock(uchar *buf, int n)
19 for(nr = 0; nr < n; nr += m){
21 m = read(0, &buf[nr], m);
23 sysfatal("can't read arena from standard input: %r");
36 u8int score[VtScoreSize];
38 fprint(2, "verify arena from standard input\n");
40 memset(&arena, 0, sizeof arena);
41 memset(&s, 0, sizeof s);
44 * read the little bit, which will included the header
47 b = alloczblock(bs, 0, 0);
48 readblock(b->data, HeadSize);
49 sha1(b->data, HeadSize, nil, &s);
50 if(unpackarenahead(&head, b->data) < 0)
51 sysfatal("corrupted arena header: %r");
52 if(head.version != ArenaVersion4 && head.version != ArenaVersion5)
53 fprint(2, "warning: unknown arena version %d\n", head.version);
56 * now we know how much to read
57 * read everything but the last block, which is special
59 e = head.size - head.blocksize;
60 for(n = HeadSize; n < e; n += bs){
63 readblock(b->data, bs);
64 sha1(b->data, bs, nil, &s);
68 * read the last block update the sum.
69 * the sum is calculated assuming the slot for the sum is zero.
72 readblock(b->data, bs);
73 sha1(b->data, bs-VtScoreSize, nil, &s);
74 sha1(zeroscore, VtScoreSize, nil, &s);
75 sha1(nil, 0, score, &s);
78 * validity check on the trailer
80 arena.blocksize = head.blocksize;
81 if(unpackarena(&arena, b->data) < 0)
82 sysfatal("corrupted arena trailer: %r");
83 scorecp(arena.score, &b->data[arena.blocksize - VtScoreSize]);
85 if(namecmp(arena.name, head.name) != 0)
86 sysfatal("arena header and trailer names clash: %s vs. %s\n", head.name, arena.name);
87 if(arena.version != head.version)
88 sysfatal("arena header and trailer versions clash: %d vs. %d\n", head.version, arena.version);
89 arena.size = head.size - 2 * head.blocksize;
92 * check for no checksum or the same
94 if(scorecmp(score, arena.score) != 0){
95 if(scorecmp(zeroscore, arena.score) != 0)
96 fprint(2, "warning: mismatched checksums for arena=%s, found=%V calculated=%V",
97 arena.name, arena.score, score);
98 scorecp(arena.score, score);
100 fprint(2, "matched score\n");
102 printarena(2, &arena);
106 threadmain(int argc, char *argv[])