4 static int verify_packfile(struct packed_git *p)
6 unsigned long index_size = p->index_size;
7 void *index_base = p->index_base;
9 unsigned char sha1[20];
10 unsigned long pack_size = p->pack_size;
12 struct pack_header *hdr;
13 int nr_objects, err, i;
15 /* Header consistency check */
17 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
18 return error("Packfile signature mismatch", p->pack_name);
19 if (hdr->hdr_version != htonl(PACK_VERSION))
20 return error("Packfile version %d different from ours %d",
21 ntohl(hdr->hdr_version), PACK_VERSION);
22 nr_objects = ntohl(hdr->hdr_entries);
23 if (num_packed_objects(p) != nr_objects)
24 return error("Packfile claims to have %d objects, "
25 "while idx size expects %d", nr_objects,
26 num_packed_objects(p));
29 pack_base = p->pack_base;
30 SHA1_Update(&ctx, pack_base, pack_size - 20);
31 SHA1_Final(sha1, &ctx);
32 if (memcmp(sha1, index_base + index_size - 40, 20))
33 return error("Packfile %s SHA1 mismatch with idx",
35 if (memcmp(sha1, pack_base + pack_size - 20, 20))
36 return error("Packfile %s SHA1 mismatch with itself",
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.
43 for (i = err = 0; i < nr_objects; i++) {
44 unsigned char sha1[20];
50 if (nth_packed_object_sha1(p, i, sha1))
51 die("internal error pack-check nth-packed-object");
52 if (!find_pack_entry_one(sha1, &e, p))
53 die("internal error pack-check find-pack-entry-one");
54 data = unpack_entry_gently(&e, type, &size);
56 err = error("cannot unpack %s from %s",
57 sha1_to_hex(sha1), p->pack_name);
60 if (check_sha1_signature(sha1, data, size, type)) {
61 err = error("cannot packed %s from %s corrupt",
62 sha1_to_hex(sha1), p->pack_name);
73 static void show_pack_info(struct packed_git *p)
78 int verify_pack(struct packed_git *p, int verbose)
80 unsigned long index_size = p->index_size;
81 void *index_base = p->index_base;
83 unsigned char sha1[20];
87 /* Verify SHA1 sum of the index file */
89 SHA1_Update(&ctx, index_base, index_size - 20);
90 SHA1_Final(sha1, &ctx);
91 if (memcmp(sha1, index_base + index_size - 20, 20))
92 ret = error("Packfile index for %s SHA1 mismatch",
96 /* Verify pack file */
98 ret = verify_packfile(p);
104 printf("%s: bad\n", p->pack_name);
107 printf("%s: ok\n", p->pack_name);