4 static int verify_packfile(struct packed_git *p,
5 struct pack_window **w_curs)
7 off_t index_size = p->index_size;
8 const unsigned char *index_base = p->index_data;
10 unsigned char sha1[20];
11 off_t offset = 0, pack_sig = p->pack_size - 20;
12 uint32_t nr_objects, i;
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
22 while (offset < pack_sig) {
23 unsigned int remaining;
24 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
26 if (offset > pack_sig)
27 remaining -= (unsigned int)(offset - pack_sig);
28 SHA1_Update(&ctx, in, remaining);
30 SHA1_Final(sha1, &ctx);
31 if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL)))
32 return error("Packfile %s SHA1 mismatch with itself",
34 if (hashcmp(sha1, index_base + index_size - 40))
35 return error("Packfile %s SHA1 mismatch with idx",
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 nr_objects = p->num_objects;
44 for (i = 0, err = 0; i < nr_objects; i++) {
45 const unsigned char *sha1;
47 enum object_type type;
51 sha1 = nth_packed_object_sha1(p, i);
53 die("internal error pack-check nth-packed-object");
54 offset = find_pack_entry_one(sha1, p);
56 die("internal error pack-check find-pack-entry-one");
57 data = unpack_entry(p, offset, &type, &size);
59 err = error("cannot unpack %s from %s",
60 sha1_to_hex(sha1), p->pack_name);
63 if (check_sha1_signature(sha1, data, size, typename(type))) {
64 err = error("packed %s from %s is corrupt",
65 sha1_to_hex(sha1), p->pack_name);
78 static void show_pack_info(struct packed_git *p)
80 uint32_t nr_objects, i, chain_histogram[MAX_CHAIN];
82 nr_objects = p->num_objects;
83 memset(chain_histogram, 0, sizeof(chain_histogram));
85 for (i = 0; i < nr_objects; i++) {
86 const unsigned char *sha1;
87 unsigned char base_sha1[20];
90 unsigned long store_size;
92 unsigned int delta_chain_length;
94 sha1 = nth_packed_object_sha1(p, i);
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 type = packed_object_info_detail(p, offset, &size, &store_size,
104 printf("%s ", sha1_to_hex(sha1));
105 if (!delta_chain_length)
106 printf("%-6s %lu %"PRIuMAX"\n",
107 type, size, (uintmax_t)offset);
109 printf("%-6s %lu %"PRIuMAX" %u %s\n",
110 type, size, (uintmax_t)offset,
111 delta_chain_length, sha1_to_hex(base_sha1));
112 if (delta_chain_length < MAX_CHAIN)
113 chain_histogram[delta_chain_length]++;
115 chain_histogram[0]++;
119 for (i = 0; i < MAX_CHAIN; i++) {
120 if (!chain_histogram[i])
122 printf("chain length %s %d: %d object%s\n",
126 1 < chain_histogram[i] ? "s" : "");
130 int verify_pack(struct packed_git *p, int verbose)
132 off_t index_size = p->index_size;
133 const unsigned char *index_base = p->index_data;
135 unsigned char sha1[20];
139 /* Verify SHA1 sum of the index file */
141 SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
142 SHA1_Final(sha1, &ctx);
143 if (hashcmp(sha1, index_base + index_size - 20))
144 ret = error("Packfile index for %s SHA1 mismatch",
148 /* Verify pack file */
149 struct pack_window *w_curs = NULL;
150 ret = verify_packfile(p, &w_curs);
156 printf("%s: bad\n", p->pack_name);
159 printf("%s: ok\n", p->pack_name);