]> git.ipfire.org Git - thirdparty/git.git/blame - pack-check.c
strbuf_getwholeline: use getdelim if it is available
[thirdparty/git.git] / pack-check.c
CommitLineData
f9253394
JH
1#include "cache.h"
2#include "pack.h"
70f5d5d3 3#include "pack-revindex.h"
1e49f22f 4#include "progress.h"
f9253394 5
9cba13ca 6struct idx_entry {
3af51928 7 off_t offset;
c41a4a94
NP
8 const unsigned char *sha1;
9 unsigned int nr;
3af51928
AJ
10};
11
12static int compare_entries(const void *e1, const void *e2)
13{
14 const struct idx_entry *entry1 = e1;
15 const struct idx_entry *entry2 = e2;
16 if (entry1->offset < entry2->offset)
17 return -1;
18 if (entry1->offset > entry2->offset)
19 return 1;
20 return 0;
21}
22
c41a4a94
NP
23int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
24 off_t offset, off_t len, unsigned int nr)
25{
26 const uint32_t *index_crc;
1e4cd68c 27 uint32_t data_crc = crc32(0, NULL, 0);
c41a4a94
NP
28
29 do {
ef49a7a0 30 unsigned long avail;
c41a4a94
NP
31 void *data = use_pack(p, w_curs, offset, &avail);
32 if (avail > len)
33 avail = len;
34 data_crc = crc32(data_crc, data, avail);
35 offset += avail;
36 len -= avail;
37 } while (len);
38
39 index_crc = p->index_data;
40 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
41
42 return data_crc != ntohl(*index_crc);
43}
44
03e79c88 45static int verify_packfile(struct packed_git *p,
c9486eb0 46 struct pack_window **w_curs,
1e49f22f
NTND
47 verify_fn fn,
48 struct progress *progress, uint32_t base_count)
49
f9253394 50{
c4001d92 51 off_t index_size = p->index_size;
42873078 52 const unsigned char *index_base = p->index_data;
9126f009 53 git_SHA_CTX ctx;
62413604 54 unsigned char sha1[20], *pack_sig;
4277c670 55 off_t offset = 0, pack_sig_ofs = 0;
326bf396 56 uint32_t nr_objects, i;
62413604 57 int err = 0;
3af51928 58 struct idx_entry *entries;
f9253394 59
079afb18
SP
60 /* Note that the pack header checks are actually performed by
61 * use_pack when it first opens the pack file. If anything
62 * goes wrong during those checks then the call will die out
63 * immediately.
64 */
f9253394 65
9126f009 66 git_SHA1_Init(&ctx);
4277c670 67 do {
ef49a7a0 68 unsigned long remaining;
079afb18
SP
69 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
70 offset += remaining;
4277c670
MH
71 if (!pack_sig_ofs)
72 pack_sig_ofs = p->pack_size - 20;
62413604
NP
73 if (offset > pack_sig_ofs)
74 remaining -= (unsigned int)(offset - pack_sig_ofs);
9126f009 75 git_SHA1_Update(&ctx, in, remaining);
4277c670 76 } while (offset < pack_sig_ofs);
9126f009 77 git_SHA1_Final(sha1, &ctx);
62413604
NP
78 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
79 if (hashcmp(sha1, pack_sig))
80 err = error("%s SHA1 checksum mismatch",
81 p->pack_name);
82 if (hashcmp(index_base + index_size - 40, pack_sig))
22e5e58a 83 err = error("%s SHA1 does not match its index",
62413604 84 p->pack_name);
079afb18 85 unuse_pack(w_curs);
f3bf9224
JH
86
87 /* Make sure everything reachable from idx is valid. Since we
88 * have verified that nr_objects matches between idx and pack,
89 * we do not do scan-streaming check on the pack file.
90 */
57059091 91 nr_objects = p->num_objects;
c41a4a94
NP
92 entries = xmalloc((nr_objects + 1) * sizeof(*entries));
93 entries[nr_objects].offset = pack_sig_ofs;
3af51928
AJ
94 /* first sort entries by pack offset, since unpacking them is more efficient that way */
95 for (i = 0; i < nr_objects; i++) {
96 entries[i].sha1 = nth_packed_object_sha1(p, i);
97 if (!entries[i].sha1)
98 die("internal error pack-check nth-packed-object");
99093238 99 entries[i].offset = nth_packed_object_offset(p, i);
c41a4a94 100 entries[i].nr = i;
3af51928
AJ
101 }
102 qsort(entries, nr_objects, sizeof(*entries), compare_entries);
103
62413604 104 for (i = 0; i < nr_objects; i++) {
f3bf9224 105 void *data;
21666f1a 106 enum object_type type;
c4001d92 107 unsigned long size;
f3bf9224 108
c41a4a94
NP
109 if (p->index_version > 1) {
110 off_t offset = entries[i].offset;
111 off_t len = entries[i+1].offset - offset;
112 unsigned int nr = entries[i].nr;
113 if (check_pack_crc(p, w_curs, offset, len, nr))
114 err = error("index CRC mismatch for object %s "
115 "from %s at offset %"PRIuMAX"",
116 sha1_to_hex(entries[i].sha1),
117 p->pack_name, (uintmax_t)offset);
118 }
3af51928 119 data = unpack_entry(p, entries[i].offset, &type, &size);
47393518 120 if (!data)
62413604
NP
121 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
122 sha1_to_hex(entries[i].sha1), p->pack_name,
123 (uintmax_t)entries[i].offset);
47393518 124 else if (check_sha1_signature(entries[i].sha1, data, size, typename(type)))
1038f0c0 125 err = error("packed %s from %s is corrupt",
3af51928 126 sha1_to_hex(entries[i].sha1), p->pack_name);
c9486eb0
NTND
127 else if (fn) {
128 int eaten = 0;
129 fn(entries[i].sha1, type, size, data, &eaten);
130 if (eaten)
131 data = NULL;
132 }
1e49f22f
NTND
133 if (((base_count + i) & 1023) == 0)
134 display_progress(progress, base_count + i);
f3bf9224 135 free(data);
1e49f22f 136
f3bf9224 137 }
1e49f22f 138 display_progress(progress, base_count + i);
3af51928 139 free(entries);
f3bf9224
JH
140
141 return err;
f9253394
JH
142}
143
9b0aa728 144int verify_pack_index(struct packed_git *p)
f9253394 145{
d079837e
SP
146 off_t index_size;
147 const unsigned char *index_base;
9126f009 148 git_SHA_CTX ctx;
f9253394 149 unsigned char sha1[20];
62413604 150 int err = 0;
f9253394 151
d079837e
SP
152 if (open_pack_index(p))
153 return error("packfile %s index not opened", p->pack_name);
154 index_size = p->index_size;
155 index_base = p->index_data;
156
f9253394 157 /* Verify SHA1 sum of the index file */
9126f009
NP
158 git_SHA1_Init(&ctx);
159 git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
160 git_SHA1_Final(sha1, &ctx);
42873078 161 if (hashcmp(sha1, index_base + index_size - 20))
62413604 162 err = error("Packfile index for %s SHA1 mismatch",
f3bf9224 163 p->pack_name);
9b0aa728
SP
164 return err;
165}
166
1e49f22f
NTND
167int verify_pack(struct packed_git *p, verify_fn fn,
168 struct progress *progress, uint32_t base_count)
9b0aa728
SP
169{
170 int err = 0;
171 struct pack_window *w_curs = NULL;
172
173 err |= verify_pack_index(p);
174 if (!p->index_data)
175 return -1;
f3bf9224 176
1e49f22f 177 err |= verify_packfile(p, &w_curs, fn, progress, base_count);
62413604 178 unuse_pack(&w_curs);
f3bf9224 179
62413604 180 return err;
f9253394 181}