]> git.ipfire.org Git - thirdparty/git.git/blame - pack-check.c
autoconf: Add tests for memmem, strtoumax and mkdtemp functions
[thirdparty/git.git] / pack-check.c
CommitLineData
f9253394
JH
1#include "cache.h"
2#include "pack.h"
3
3af51928
AJ
4struct 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
03e79c88
SP
21static int verify_packfile(struct packed_git *p,
22 struct pack_window **w_curs)
f9253394 23{
c4001d92 24 off_t index_size = p->index_size;
42873078 25 const unsigned char *index_base = p->index_data;
f9253394
JH
26 SHA_CTX ctx;
27 unsigned char sha1[20];
c4001d92 28 off_t offset = 0, pack_sig = p->pack_size - 20;
326bf396
SP
29 uint32_t nr_objects, i;
30 int err;
3af51928 31 struct idx_entry *entries;
f9253394 32
079afb18
SP
33 /* 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 */
f9253394
JH
38
39 SHA1_Init(&ctx);
079afb18
SP
40 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)
c4001d92 45 remaining -= (unsigned int)(offset - pack_sig);
079afb18
SP
46 SHA1_Update(&ctx, in, remaining);
47 }
f9253394 48 SHA1_Final(sha1, &ctx);
079afb18 49 if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL)))
f9253394
JH
50 return error("Packfile %s SHA1 mismatch with itself",
51 p->pack_name);
42873078 52 if (hashcmp(sha1, index_base + index_size - 40))
55e1805d
JH
53 return error("Packfile %s SHA1 mismatch with idx",
54 p->pack_name);
079afb18 55 unuse_pack(w_curs);
f3bf9224
JH
56
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 */
57059091 61 nr_objects = p->num_objects;
3af51928
AJ
62 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
326bf396 74 for (i = 0, err = 0; i < nr_objects; i++) {
f3bf9224 75 void *data;
21666f1a 76 enum object_type type;
c4001d92 77 unsigned long size;
f3bf9224 78
3af51928 79 data = unpack_entry(p, entries[i].offset, &type, &size);
f3bf9224
JH
80 if (!data) {
81 err = error("cannot unpack %s from %s",
3af51928 82 sha1_to_hex(entries[i].sha1), p->pack_name);
f3bf9224
JH
83 continue;
84 }
3af51928 85 if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
1038f0c0 86 err = error("packed %s from %s is corrupt",
3af51928 87 sha1_to_hex(entries[i].sha1), p->pack_name);
f3bf9224
JH
88 free(data);
89 continue;
90 }
91 free(data);
92 }
3af51928 93 free(entries);
f3bf9224
JH
94
95 return err;
f9253394
JH
96}
97
98
ddcf786f 99#define MAX_CHAIN 50
473ab165 100
079afb18 101static void show_pack_info(struct packed_git *p)
f3bf9224 102{
ddcf786f 103 uint32_t nr_objects, i, chain_histogram[MAX_CHAIN+1];
57059091 104 nr_objects = p->num_objects;
473ab165 105 memset(chain_histogram, 0, sizeof(chain_histogram));
ad8c80a5
JH
106
107 for (i = 0; i < nr_objects; i++) {
d72308e0
NP
108 const unsigned char *sha1;
109 unsigned char base_sha1[20];
21666f1a 110 const char *type;
ad8c80a5
JH
111 unsigned long size;
112 unsigned long store_size;
c4001d92 113 off_t offset;
f8f135c9 114 unsigned int delta_chain_length;
ad8c80a5 115
d72308e0
NP
116 sha1 = nth_packed_object_sha1(p, i);
117 if (!sha1)
ad8c80a5 118 die("internal error pack-check nth-packed-object");
43057304
NP
119 offset = find_pack_entry_one(sha1, p);
120 if (!offset)
ad8c80a5
JH
121 die("internal error pack-check find-pack-entry-one");
122
21666f1a
NP
123 type = packed_object_info_detail(p, offset, &size, &store_size,
124 &delta_chain_length,
125 base_sha1);
ad8c80a5
JH
126 printf("%s ", sha1_to_hex(sha1));
127 if (!delta_chain_length)
c4001d92
SP
128 printf("%-6s %lu %"PRIuMAX"\n",
129 type, size, (uintmax_t)offset);
473ab165 130 else {
c4001d92
SP
131 printf("%-6s %lu %"PRIuMAX" %u %s\n",
132 type, size, (uintmax_t)offset,
ad8c80a5 133 delta_chain_length, sha1_to_hex(base_sha1));
ddcf786f 134 if (delta_chain_length <= MAX_CHAIN)
473ab165
JH
135 chain_histogram[delta_chain_length]++;
136 else
137 chain_histogram[0]++;
138 }
ad8c80a5
JH
139 }
140
ddcf786f 141 for (i = 0; i <= MAX_CHAIN; i++) {
473ab165
JH
142 if (!chain_histogram[i])
143 continue;
ddcf786f
NP
144 printf("chain length = %d: %d object%s\n", i,
145 chain_histogram[i], chain_histogram[i] > 1 ? "s" : "");
473ab165 146 }
ddcf786f
NP
147 if (chain_histogram[0])
148 printf("chain length > %d: %d object%s\n", MAX_CHAIN,
149 chain_histogram[0], chain_histogram[0] > 1 ? "s" : "");
f3bf9224
JH
150}
151
152int verify_pack(struct packed_git *p, int verbose)
f9253394 153{
d079837e
SP
154 off_t index_size;
155 const unsigned char *index_base;
f9253394
JH
156 SHA_CTX ctx;
157 unsigned char sha1[20];
158 int ret;
159
d079837e
SP
160 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
f3bf9224 165 ret = 0;
f9253394
JH
166 /* Verify SHA1 sum of the index file */
167 SHA1_Init(&ctx);
c4001d92 168 SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
f9253394 169 SHA1_Final(sha1, &ctx);
42873078 170 if (hashcmp(sha1, index_base + index_size - 20))
f3bf9224
JH
171 ret = error("Packfile index for %s SHA1 mismatch",
172 p->pack_name);
173
174 if (!ret) {
175 /* Verify pack file */
03e79c88
SP
176 struct pack_window *w_curs = NULL;
177 ret = verify_packfile(p, &w_curs);
178 unuse_pack(&w_curs);
f3bf9224
JH
179 }
180
181 if (verbose) {
182 if (ret)
183 printf("%s: bad\n", p->pack_name);
184 else {
079afb18 185 show_pack_info(p);
f3bf9224
JH
186 printf("%s: ok\n", p->pack_name);
187 }
188 }
f9253394 189
f9253394
JH
190 return ret;
191}