4 static int verify_packfile(struct packed_git *p,
5 struct pack_window **w_curs)
7 unsigned long index_size = p->index_size;
8 void *index_base = p->index_base;
10 unsigned char sha1[20];
11 unsigned long pack_size = p->pack_size;
13 struct pack_header *hdr;
14 int nr_objects, err, i;
16 /* Header consistency check */
17 pack_base = use_pack(p, w_curs, 0, NULL);
18 hdr = (struct pack_header*)pack_base;
19 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
20 return error("Packfile %s signature mismatch", p->pack_name);
21 if (!pack_version_ok(hdr->hdr_version))
22 return error("Packfile version %d unsupported",
23 ntohl(hdr->hdr_version));
24 nr_objects = ntohl(hdr->hdr_entries);
25 if (num_packed_objects(p) != nr_objects)
26 return error("Packfile claims to have %d objects, "
27 "while idx size expects %d", nr_objects,
28 num_packed_objects(p));
31 SHA1_Update(&ctx, pack_base, pack_size - 20);
32 SHA1_Final(sha1, &ctx);
33 if (hashcmp(sha1, (unsigned char *)pack_base + pack_size - 20))
34 return error("Packfile %s SHA1 mismatch with itself",
36 if (hashcmp(sha1, (unsigned char *)index_base + index_size - 40))
37 return error("Packfile %s SHA1 mismatch with idx",
40 /* Make sure everything reachable from idx is valid. Since we
41 * have verified that nr_objects matches between idx and pack,
42 * we do not do scan-streaming check on the pack file.
44 for (i = err = 0; i < nr_objects; i++) {
45 unsigned char sha1[20];
48 unsigned long size, offset;
50 if (nth_packed_object_sha1(p, i, sha1))
51 die("internal error pack-check nth-packed-object");
52 offset = find_pack_entry_one(sha1, p);
54 die("internal error pack-check find-pack-entry-one");
55 data = unpack_entry(p, offset, type, &size);
57 err = error("cannot unpack %s from %s",
58 sha1_to_hex(sha1), p->pack_name);
61 if (check_sha1_signature(sha1, data, size, type)) {
62 err = error("packed %s from %s is corrupt",
63 sha1_to_hex(sha1), p->pack_name);
76 static void show_pack_info(struct packed_git *p,
77 struct pack_window **w_curs)
79 struct pack_header *hdr;
81 unsigned int chain_histogram[MAX_CHAIN];
83 hdr = (struct pack_header*)use_pack(p, w_curs, 0, NULL);
84 nr_objects = ntohl(hdr->hdr_entries);
85 memset(chain_histogram, 0, sizeof(chain_histogram));
87 for (i = 0; i < nr_objects; i++) {
88 unsigned char sha1[20], base_sha1[20];
91 unsigned long store_size;
93 unsigned int delta_chain_length;
95 if (nth_packed_object_sha1(p, i, sha1))
96 die("internal error pack-check nth-packed-object");
97 offset = find_pack_entry_one(sha1, p);
99 die("internal error pack-check find-pack-entry-one");
101 packed_object_info_detail(p, offset, type, &size, &store_size,
104 printf("%s ", sha1_to_hex(sha1));
105 if (!delta_chain_length)
106 printf("%-6s %lu %lu\n", type, size, offset);
108 printf("%-6s %lu %lu %u %s\n", type, size, offset,
109 delta_chain_length, sha1_to_hex(base_sha1));
110 if (delta_chain_length < MAX_CHAIN)
111 chain_histogram[delta_chain_length]++;
113 chain_histogram[0]++;
117 for (i = 0; i < MAX_CHAIN; i++) {
118 if (!chain_histogram[i])
120 printf("chain length %s %d: %d object%s\n",
124 1 < chain_histogram[i] ? "s" : "");
128 int verify_pack(struct packed_git *p, int verbose)
130 unsigned long index_size = p->index_size;
131 void *index_base = p->index_base;
133 unsigned char sha1[20];
137 /* Verify SHA1 sum of the index file */
139 SHA1_Update(&ctx, index_base, index_size - 20);
140 SHA1_Final(sha1, &ctx);
141 if (hashcmp(sha1, (unsigned char *)index_base + index_size - 20))
142 ret = error("Packfile index for %s SHA1 mismatch",
146 /* Verify pack file */
147 struct pack_window *w_curs = NULL;
148 ret = verify_packfile(p, &w_curs);
154 printf("%s: bad\n", p->pack_name);
156 struct pack_window *w_curs = NULL;
157 show_pack_info(p, &w_curs);
159 printf("%s: ok\n", p->pack_name);