]>
Commit | Line | Data |
---|---|---|
f9253394 JH |
1 | #include "cache.h" |
2 | #include "pack.h" | |
3 | ||
03e79c88 SP |
4 | static int verify_packfile(struct packed_git *p, |
5 | struct pack_window **w_curs) | |
f9253394 | 6 | { |
c4001d92 | 7 | off_t index_size = p->index_size; |
42873078 | 8 | const unsigned char *index_base = p->index_data; |
f9253394 JH |
9 | SHA_CTX ctx; |
10 | unsigned char sha1[20]; | |
c4001d92 | 11 | off_t offset = 0, pack_sig = p->pack_size - 20; |
326bf396 SP |
12 | uint32_t nr_objects, i; |
13 | int err; | |
f9253394 | 14 | |
079afb18 SP |
15 | /* Note that the pack header checks are actually performed by |
16 | * use_pack when it first opens the pack file. If anything | |
17 | * goes wrong during those checks then the call will die out | |
18 | * immediately. | |
19 | */ | |
f9253394 JH |
20 | |
21 | SHA1_Init(&ctx); | |
079afb18 SP |
22 | while (offset < pack_sig) { |
23 | unsigned int remaining; | |
24 | unsigned char *in = use_pack(p, w_curs, offset, &remaining); | |
25 | offset += remaining; | |
26 | if (offset > pack_sig) | |
c4001d92 | 27 | remaining -= (unsigned int)(offset - pack_sig); |
079afb18 SP |
28 | SHA1_Update(&ctx, in, remaining); |
29 | } | |
f9253394 | 30 | SHA1_Final(sha1, &ctx); |
079afb18 | 31 | if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL))) |
f9253394 JH |
32 | return error("Packfile %s SHA1 mismatch with itself", |
33 | p->pack_name); | |
42873078 | 34 | if (hashcmp(sha1, index_base + index_size - 40)) |
55e1805d JH |
35 | return error("Packfile %s SHA1 mismatch with idx", |
36 | p->pack_name); | |
079afb18 | 37 | unuse_pack(w_curs); |
f3bf9224 JH |
38 | |
39 | /* Make sure everything reachable from idx is valid. Since we | |
40 | * have verified that nr_objects matches between idx and pack, | |
41 | * we do not do scan-streaming check on the pack file. | |
42 | */ | |
57059091 | 43 | nr_objects = p->num_objects; |
326bf396 | 44 | for (i = 0, err = 0; i < nr_objects; i++) { |
d72308e0 | 45 | const unsigned char *sha1; |
f3bf9224 | 46 | void *data; |
21666f1a | 47 | enum object_type type; |
c4001d92 SP |
48 | unsigned long size; |
49 | off_t offset; | |
f3bf9224 | 50 | |
d72308e0 NP |
51 | sha1 = nth_packed_object_sha1(p, i); |
52 | if (!sha1) | |
f3bf9224 | 53 | die("internal error pack-check nth-packed-object"); |
43057304 NP |
54 | offset = find_pack_entry_one(sha1, p); |
55 | if (!offset) | |
f3bf9224 | 56 | die("internal error pack-check find-pack-entry-one"); |
21666f1a | 57 | data = unpack_entry(p, offset, &type, &size); |
f3bf9224 JH |
58 | if (!data) { |
59 | err = error("cannot unpack %s from %s", | |
60 | sha1_to_hex(sha1), p->pack_name); | |
61 | continue; | |
62 | } | |
21666f1a | 63 | if (check_sha1_signature(sha1, data, size, typename(type))) { |
1038f0c0 | 64 | err = error("packed %s from %s is corrupt", |
f3bf9224 JH |
65 | sha1_to_hex(sha1), p->pack_name); |
66 | free(data); | |
67 | continue; | |
68 | } | |
69 | free(data); | |
70 | } | |
71 | ||
72 | return err; | |
f9253394 JH |
73 | } |
74 | ||
75 | ||
ddcf786f | 76 | #define MAX_CHAIN 50 |
473ab165 | 77 | |
079afb18 | 78 | static void show_pack_info(struct packed_git *p) |
f3bf9224 | 79 | { |
ddcf786f | 80 | uint32_t nr_objects, i, chain_histogram[MAX_CHAIN+1]; |
57059091 | 81 | nr_objects = p->num_objects; |
473ab165 | 82 | memset(chain_histogram, 0, sizeof(chain_histogram)); |
ad8c80a5 JH |
83 | |
84 | for (i = 0; i < nr_objects; i++) { | |
d72308e0 NP |
85 | const unsigned char *sha1; |
86 | unsigned char base_sha1[20]; | |
21666f1a | 87 | const char *type; |
ad8c80a5 JH |
88 | unsigned long size; |
89 | unsigned long store_size; | |
c4001d92 | 90 | off_t offset; |
f8f135c9 | 91 | unsigned int delta_chain_length; |
ad8c80a5 | 92 | |
d72308e0 NP |
93 | sha1 = nth_packed_object_sha1(p, i); |
94 | if (!sha1) | |
ad8c80a5 | 95 | die("internal error pack-check nth-packed-object"); |
43057304 NP |
96 | offset = find_pack_entry_one(sha1, p); |
97 | if (!offset) | |
ad8c80a5 JH |
98 | die("internal error pack-check find-pack-entry-one"); |
99 | ||
21666f1a NP |
100 | type = packed_object_info_detail(p, offset, &size, &store_size, |
101 | &delta_chain_length, | |
102 | base_sha1); | |
ad8c80a5 JH |
103 | printf("%s ", sha1_to_hex(sha1)); |
104 | if (!delta_chain_length) | |
c4001d92 SP |
105 | printf("%-6s %lu %"PRIuMAX"\n", |
106 | type, size, (uintmax_t)offset); | |
473ab165 | 107 | else { |
c4001d92 SP |
108 | printf("%-6s %lu %"PRIuMAX" %u %s\n", |
109 | type, size, (uintmax_t)offset, | |
ad8c80a5 | 110 | delta_chain_length, sha1_to_hex(base_sha1)); |
ddcf786f | 111 | if (delta_chain_length <= MAX_CHAIN) |
473ab165 JH |
112 | chain_histogram[delta_chain_length]++; |
113 | else | |
114 | chain_histogram[0]++; | |
115 | } | |
ad8c80a5 JH |
116 | } |
117 | ||
ddcf786f | 118 | for (i = 0; i <= MAX_CHAIN; i++) { |
473ab165 JH |
119 | if (!chain_histogram[i]) |
120 | continue; | |
ddcf786f NP |
121 | printf("chain length = %d: %d object%s\n", i, |
122 | chain_histogram[i], chain_histogram[i] > 1 ? "s" : ""); | |
473ab165 | 123 | } |
ddcf786f NP |
124 | if (chain_histogram[0]) |
125 | printf("chain length > %d: %d object%s\n", MAX_CHAIN, | |
126 | chain_histogram[0], chain_histogram[0] > 1 ? "s" : ""); | |
f3bf9224 JH |
127 | } |
128 | ||
129 | int verify_pack(struct packed_git *p, int verbose) | |
f9253394 | 130 | { |
c4001d92 | 131 | off_t index_size = p->index_size; |
42873078 | 132 | const unsigned char *index_base = p->index_data; |
f9253394 JH |
133 | SHA_CTX ctx; |
134 | unsigned char sha1[20]; | |
135 | int ret; | |
136 | ||
f3bf9224 | 137 | ret = 0; |
f9253394 JH |
138 | /* Verify SHA1 sum of the index file */ |
139 | SHA1_Init(&ctx); | |
c4001d92 | 140 | SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20)); |
f9253394 | 141 | SHA1_Final(sha1, &ctx); |
42873078 | 142 | if (hashcmp(sha1, index_base + index_size - 20)) |
f3bf9224 JH |
143 | ret = error("Packfile index for %s SHA1 mismatch", |
144 | p->pack_name); | |
145 | ||
146 | if (!ret) { | |
147 | /* Verify pack file */ | |
03e79c88 SP |
148 | struct pack_window *w_curs = NULL; |
149 | ret = verify_packfile(p, &w_curs); | |
150 | unuse_pack(&w_curs); | |
f3bf9224 JH |
151 | } |
152 | ||
153 | if (verbose) { | |
154 | if (ret) | |
155 | printf("%s: bad\n", p->pack_name); | |
156 | else { | |
079afb18 | 157 | show_pack_info(p); |
f3bf9224 JH |
158 | printf("%s: ok\n", p->pack_name); |
159 | } | |
160 | } | |
f9253394 | 161 | |
f9253394 JH |
162 | return ret; |
163 | } |