Merge branch 'jc/curl'
[git/git.git] / pack-check.c
CommitLineData
f9253394
JH
1#include "cache.h"
2#include "pack.h"
3
4static int verify_packfile(struct packed_git *p)
5{
6 unsigned long index_size = p->index_size;
7 void *index_base = p->index_base;
8 SHA_CTX ctx;
9 unsigned char sha1[20];
10 unsigned long pack_size = p->pack_size;
11 void *pack_base;
12 struct pack_header *hdr;
f3bf9224 13 int nr_objects, err, i;
f9253394 14
f3bf9224 15 /* Header consistency check */
f9253394
JH
16 hdr = p->pack_base;
17 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
4ec99bf0 18 return error("Packfile %s signature mismatch", p->pack_name);
d60fc1c8
NP
19 if (!pack_version_ok(hdr->hdr_version))
20 return error("Packfile version %d unsupported",
21 ntohl(hdr->hdr_version));
f9253394
JH
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));
27
28 SHA1_Init(&ctx);
29 pack_base = p->pack_base;
30 SHA1_Update(&ctx, pack_base, pack_size - 20);
31 SHA1_Final(sha1, &ctx);
a89fccd2 32 if (hashcmp(sha1, (unsigned char *)pack_base + pack_size - 20))
f9253394
JH
33 return error("Packfile %s SHA1 mismatch with itself",
34 p->pack_name);
a89fccd2 35 if (hashcmp(sha1, (unsigned char *)index_base + index_size - 40))
55e1805d
JH
36 return error("Packfile %s SHA1 mismatch with idx",
37 p->pack_name);
f3bf9224
JH
38
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.
42 */
43 for (i = err = 0; i < nr_objects; i++) {
44 unsigned char sha1[20];
f3bf9224
JH
45 void *data;
46 char type[20];
43057304 47 unsigned long size, offset;
f3bf9224
JH
48
49 if (nth_packed_object_sha1(p, i, sha1))
50 die("internal error pack-check nth-packed-object");
43057304
NP
51 offset = find_pack_entry_one(sha1, p);
52 if (!offset)
f3bf9224 53 die("internal error pack-check find-pack-entry-one");
43057304 54 data = unpack_entry_gently(p, offset, type, &size);
f3bf9224
JH
55 if (!data) {
56 err = error("cannot unpack %s from %s",
57 sha1_to_hex(sha1), p->pack_name);
58 continue;
59 }
60 if (check_sha1_signature(sha1, data, size, type)) {
1038f0c0 61 err = error("packed %s from %s is corrupt",
f3bf9224
JH
62 sha1_to_hex(sha1), p->pack_name);
63 free(data);
64 continue;
65 }
66 free(data);
67 }
68
69 return err;
f9253394
JH
70}
71
72
473ab165
JH
73#define MAX_CHAIN 40
74
f3bf9224
JH
75static void show_pack_info(struct packed_git *p)
76{
ad8c80a5
JH
77 struct pack_header *hdr;
78 int nr_objects, i;
473ab165 79 unsigned int chain_histogram[MAX_CHAIN];
ad8c80a5
JH
80
81 hdr = p->pack_base;
82 nr_objects = ntohl(hdr->hdr_entries);
473ab165 83 memset(chain_histogram, 0, sizeof(chain_histogram));
ad8c80a5
JH
84
85 for (i = 0; i < nr_objects; i++) {
86 unsigned char sha1[20], base_sha1[20];
ad8c80a5
JH
87 char type[20];
88 unsigned long size;
89 unsigned long store_size;
43057304 90 unsigned long offset;
f8f135c9 91 unsigned int delta_chain_length;
ad8c80a5
JH
92
93 if (nth_packed_object_sha1(p, i, sha1))
94 die("internal error pack-check nth-packed-object");
43057304
NP
95 offset = find_pack_entry_one(sha1, p);
96 if (!offset)
ad8c80a5
JH
97 die("internal error pack-check find-pack-entry-one");
98
43057304 99 packed_object_info_detail(p, offset, type, &size, &store_size,
ad8c80a5
JH
100 &delta_chain_length,
101 base_sha1);
102 printf("%s ", sha1_to_hex(sha1));
103 if (!delta_chain_length)
43057304 104 printf("%-6s %lu %lu\n", type, size, offset);
473ab165 105 else {
43057304 106 printf("%-6s %lu %lu %u %s\n", type, size, offset,
ad8c80a5 107 delta_chain_length, sha1_to_hex(base_sha1));
473ab165
JH
108 if (delta_chain_length < MAX_CHAIN)
109 chain_histogram[delta_chain_length]++;
110 else
111 chain_histogram[0]++;
112 }
ad8c80a5
JH
113 }
114
473ab165
JH
115 for (i = 0; i < MAX_CHAIN; i++) {
116 if (!chain_histogram[i])
117 continue;
118 printf("chain length %s %d: %d object%s\n",
119 i ? "=" : ">=",
120 i ? i : MAX_CHAIN,
121 chain_histogram[i],
122 1 < chain_histogram[i] ? "s" : "");
123 }
f3bf9224
JH
124}
125
126int verify_pack(struct packed_git *p, int verbose)
f9253394
JH
127{
128 unsigned long index_size = p->index_size;
129 void *index_base = p->index_base;
130 SHA_CTX ctx;
131 unsigned char sha1[20];
132 int ret;
133
f3bf9224 134 ret = 0;
f9253394
JH
135 /* Verify SHA1 sum of the index file */
136 SHA1_Init(&ctx);
137 SHA1_Update(&ctx, index_base, index_size - 20);
138 SHA1_Final(sha1, &ctx);
a89fccd2 139 if (hashcmp(sha1, (unsigned char *)index_base + index_size - 20))
f3bf9224
JH
140 ret = error("Packfile index for %s SHA1 mismatch",
141 p->pack_name);
142
143 if (!ret) {
144 /* Verify pack file */
145 use_packed_git(p);
146 ret = verify_packfile(p);
147 unuse_packed_git(p);
148 }
149
150 if (verbose) {
151 if (ret)
152 printf("%s: bad\n", p->pack_name);
153 else {
ad8c80a5 154 use_packed_git(p);
f3bf9224 155 show_pack_info(p);
ad8c80a5 156 unuse_packed_git(p);
f3bf9224
JH
157 printf("%s: ok\n", p->pack_name);
158 }
159 }
f9253394 160
f9253394
JH
161 return ret;
162}