blob: d7dd62bb8346c4cac8dbd7334e999a450c21c5ab [file] [log] [blame]
Junio C Hamanof3bf9222005-06-30 17:15:39 -07001#include "cache.h"
2#include "pack.h"
3
Alexandre Julliard3af51922007-06-03 20:21:41 +02004struct idx_entry
5{
6 const unsigned char *sha1;
7 off_t offset;
8};
9
10static int compare_entries(const void *e1, const void *e2)
11{
12 const struct idx_entry *entry1 = e1;
13 const struct idx_entry *entry2 = e2;
14 if (entry1->offset < entry2->offset)
15 return -1;
16 if (entry1->offset > entry2->offset)
17 return 1;
18 return 0;
19}
20
Shawn O. Pearce03e79c82006-12-23 02:34:08 -050021static int verify_packfile(struct packed_git *p,
22 struct pack_window **w_curs)
Junio C Hamanof3bf9222005-06-30 17:15:39 -070023{
Shawn O. Pearcec4001d92007-03-06 20:44:30 -050024 off_t index_size = p->index_size;
Nicolas Pitre42873072007-03-16 16:42:50 -040025 const unsigned char *index_base = p->index_data;
Junio C Hamanof3bf9222005-06-30 17:15:39 -070026 SHA_CTX ctx;
27 unsigned char sha1[20];
Shawn O. Pearcec4001d92007-03-06 20:44:30 -050028 off_t offset = 0, pack_sig = p->pack_size - 20;
Shawn O. Pearce326bf392007-03-06 20:44:19 -050029 uint32_t nr_objects, i;
30 int err;
Alexandre Julliard3af51922007-06-03 20:21:41 +020031 struct idx_entry *entries;
Junio C Hamanof3bf9222005-06-30 17:15:39 -070032
Shawn O. Pearce079afb12006-12-23 02:34:13 -050033 /* Note that the pack header checks are actually performed by
34 * use_pack when it first opens the pack file. If anything
35 * goes wrong during those checks then the call will die out
36 * immediately.
37 */
Junio C Hamanof3bf9222005-06-30 17:15:39 -070038
39 SHA1_Init(&ctx);
Shawn O. Pearce079afb12006-12-23 02:34:13 -050040 while (offset < pack_sig) {
41 unsigned int remaining;
42 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
43 offset += remaining;
44 if (offset > pack_sig)
Shawn O. Pearcec4001d92007-03-06 20:44:30 -050045 remaining -= (unsigned int)(offset - pack_sig);
Shawn O. Pearce079afb12006-12-23 02:34:13 -050046 SHA1_Update(&ctx, in, remaining);
47 }
Junio C Hamanof3bf9222005-06-30 17:15:39 -070048 SHA1_Final(sha1, &ctx);
Shawn O. Pearce079afb12006-12-23 02:34:13 -050049 if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL)))
Junio C Hamanof3bf9222005-06-30 17:15:39 -070050 return error("Packfile %s SHA1 mismatch with itself",
51 p->pack_name);
Nicolas Pitre42873072007-03-16 16:42:50 -040052 if (hashcmp(sha1, index_base + index_size - 40))
Junio C Hamano55e18052006-04-27 15:42:01 -070053 return error("Packfile %s SHA1 mismatch with idx",
54 p->pack_name);
Shawn O. Pearce079afb12006-12-23 02:34:13 -050055 unuse_pack(w_curs);
Junio C Hamanof3bf9222005-06-30 17:15:39 -070056
57 /* Make sure everything reachable from idx is valid. Since we
58 * have verified that nr_objects matches between idx and pack,
59 * we do not do scan-streaming check on the pack file.
60 */
Nicolas Pitre57059092007-04-09 01:06:28 -040061 nr_objects = p->num_objects;
Alexandre Julliard3af51922007-06-03 20:21:41 +020062 entries = xmalloc(nr_objects * sizeof(*entries));
63 /* first sort entries by pack offset, since unpacking them is more efficient that way */
64 for (i = 0; i < nr_objects; i++) {
65 entries[i].sha1 = nth_packed_object_sha1(p, i);
66 if (!entries[i].sha1)
67 die("internal error pack-check nth-packed-object");
68 entries[i].offset = find_pack_entry_one(entries[i].sha1, p);
69 if (!entries[i].offset)
70 die("internal error pack-check find-pack-entry-one");
71 }
72 qsort(entries, nr_objects, sizeof(*entries), compare_entries);
73
Shawn O. Pearce326bf392007-03-06 20:44:19 -050074 for (i = 0, err = 0; i < nr_objects; i++) {
Junio C Hamanof3bf9222005-06-30 17:15:39 -070075 void *data;
Nicolas Pitre21666f12007-02-26 14:55:59 -050076 enum object_type type;
Shawn O. Pearcec4001d92007-03-06 20:44:30 -050077 unsigned long size;
Junio C Hamanof3bf9222005-06-30 17:15:39 -070078
Alexandre Julliard3af51922007-06-03 20:21:41 +020079 data = unpack_entry(p, entries[i].offset, &type, &size);
Junio C Hamanof3bf9222005-06-30 17:15:39 -070080 if (!data) {
81 err = error("cannot unpack %s from %s",
Alexandre Julliard3af51922007-06-03 20:21:41 +020082 sha1_to_hex(entries[i].sha1), p->pack_name);
Junio C Hamanof3bf9222005-06-30 17:15:39 -070083 continue;
84 }
Alexandre Julliard3af51922007-06-03 20:21:41 +020085 if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
Junio C Hamano1038f0c2005-07-07 15:12:20 -070086 err = error("packed %s from %s is corrupt",
Alexandre Julliard3af51922007-06-03 20:21:41 +020087 sha1_to_hex(entries[i].sha1), p->pack_name);
Junio C Hamanof3bf9222005-06-30 17:15:39 -070088 free(data);
89 continue;
90 }
91 free(data);
92 }
Alexandre Julliard3af51922007-06-03 20:21:41 +020093 free(entries);
Junio C Hamanof3bf9222005-06-30 17:15:39 -070094
95 return err;
96}
97
98
Nicolas Pitreddcf7862007-05-25 23:37:40 -040099#define MAX_CHAIN 50
Junio C Hamano473ab162006-03-05 11:20:12 -0800100
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500101static void show_pack_info(struct packed_git *p)
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700102{
Nicolas Pitreddcf7862007-05-25 23:37:40 -0400103 uint32_t nr_objects, i, chain_histogram[MAX_CHAIN+1];
Nicolas Pitre57059092007-04-09 01:06:28 -0400104 nr_objects = p->num_objects;
Junio C Hamano473ab162006-03-05 11:20:12 -0800105 memset(chain_histogram, 0, sizeof(chain_histogram));
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700106
107 for (i = 0; i < nr_objects; i++) {
Nicolas Pitred72308e2007-04-04 16:49:04 -0400108 const unsigned char *sha1;
109 unsigned char base_sha1[20];
Nicolas Pitre21666f12007-02-26 14:55:59 -0500110 const char *type;
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700111 unsigned long size;
112 unsigned long store_size;
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500113 off_t offset;
Junio C Hamanof8f135c2006-02-15 12:47:43 -0800114 unsigned int delta_chain_length;
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700115
Nicolas Pitred72308e2007-04-04 16:49:04 -0400116 sha1 = nth_packed_object_sha1(p, i);
117 if (!sha1)
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700118 die("internal error pack-check nth-packed-object");
Nicolas Pitre43057302006-09-21 00:05:37 -0400119 offset = find_pack_entry_one(sha1, p);
120 if (!offset)
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700121 die("internal error pack-check find-pack-entry-one");
122
Nicolas Pitre21666f12007-02-26 14:55:59 -0500123 type = packed_object_info_detail(p, offset, &size, &store_size,
124 &delta_chain_length,
125 base_sha1);
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700126 printf("%s ", sha1_to_hex(sha1));
127 if (!delta_chain_length)
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500128 printf("%-6s %lu %"PRIuMAX"\n",
129 type, size, (uintmax_t)offset);
Junio C Hamano473ab162006-03-05 11:20:12 -0800130 else {
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500131 printf("%-6s %lu %"PRIuMAX" %u %s\n",
132 type, size, (uintmax_t)offset,
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700133 delta_chain_length, sha1_to_hex(base_sha1));
Nicolas Pitreddcf7862007-05-25 23:37:40 -0400134 if (delta_chain_length <= MAX_CHAIN)
Junio C Hamano473ab162006-03-05 11:20:12 -0800135 chain_histogram[delta_chain_length]++;
136 else
137 chain_histogram[0]++;
138 }
Junio C Hamanoad8c80a2005-06-30 17:17:20 -0700139 }
140
Nicolas Pitreddcf7862007-05-25 23:37:40 -0400141 for (i = 0; i <= MAX_CHAIN; i++) {
Junio C Hamano473ab162006-03-05 11:20:12 -0800142 if (!chain_histogram[i])
143 continue;
Nicolas Pitreddcf7862007-05-25 23:37:40 -0400144 printf("chain length = %d: %d object%s\n", i,
145 chain_histogram[i], chain_histogram[i] > 1 ? "s" : "");
Junio C Hamano473ab162006-03-05 11:20:12 -0800146 }
Nicolas Pitreddcf7862007-05-25 23:37:40 -0400147 if (chain_histogram[0])
148 printf("chain length > %d: %d object%s\n", MAX_CHAIN,
149 chain_histogram[0], chain_histogram[0] > 1 ? "s" : "");
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700150}
151
152int verify_pack(struct packed_git *p, int verbose)
153{
Shawn O. Pearced0798372007-05-26 01:24:19 -0400154 off_t index_size;
155 const unsigned char *index_base;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700156 SHA_CTX ctx;
157 unsigned char sha1[20];
158 int ret;
159
Shawn O. Pearced0798372007-05-26 01:24:19 -0400160 if (open_pack_index(p))
161 return error("packfile %s index not opened", p->pack_name);
162 index_size = p->index_size;
163 index_base = p->index_data;
164
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700165 ret = 0;
166 /* Verify SHA1 sum of the index file */
167 SHA1_Init(&ctx);
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500168 SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700169 SHA1_Final(sha1, &ctx);
Nicolas Pitre42873072007-03-16 16:42:50 -0400170 if (hashcmp(sha1, index_base + index_size - 20))
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700171 ret = error("Packfile index for %s SHA1 mismatch",
172 p->pack_name);
173
174 if (!ret) {
175 /* Verify pack file */
Shawn O. Pearce03e79c82006-12-23 02:34:08 -0500176 struct pack_window *w_curs = NULL;
177 ret = verify_packfile(p, &w_curs);
178 unuse_pack(&w_curs);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700179 }
180
181 if (verbose) {
182 if (ret)
183 printf("%s: bad\n", p->pack_name);
184 else {
Shawn O. Pearce079afb12006-12-23 02:34:13 -0500185 show_pack_info(p);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700186 printf("%s: ok\n", p->pack_name);
187 }
188 }
189
190 return ret;
191}