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