]> git.ipfire.org Git - thirdparty/git.git/blame - pack-check.c
Merge branch 'jk/bundle-progress'
[thirdparty/git.git] / pack-check.c
CommitLineData
f9253394 1#include "cache.h"
41771fa4 2#include "hex.h"
57a6a500 3#include "repository.h"
f9253394 4#include "pack.h"
70f5d5d3 5#include "pack-revindex.h"
1e49f22f 6#include "progress.h"
0317f455 7#include "packfile.h"
a80d72db 8#include "object-store.h"
f9253394 9
9cba13ca 10struct idx_entry {
3af51928 11 off_t offset;
c41a4a94 12 unsigned int nr;
3af51928
AJ
13};
14
15static int compare_entries(const void *e1, const void *e2)
16{
17 const struct idx_entry *entry1 = e1;
18 const struct idx_entry *entry2 = e2;
19 if (entry1->offset < entry2->offset)
20 return -1;
21 if (entry1->offset > entry2->offset)
22 return 1;
23 return 0;
24}
25
c41a4a94
NP
26int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
27 off_t offset, off_t len, unsigned int nr)
28{
29 const uint32_t *index_crc;
1e4cd68c 30 uint32_t data_crc = crc32(0, NULL, 0);
c41a4a94
NP
31
32 do {
ef49a7a0 33 unsigned long avail;
c41a4a94
NP
34 void *data = use_pack(p, w_curs, offset, &avail);
35 if (avail > len)
36 avail = len;
37 data_crc = crc32(data_crc, data, avail);
38 offset += avail;
39 len -= avail;
40 } while (len);
41
42 index_crc = p->index_data;
f86f7695 43 index_crc += 2 + 256 + (size_t)p->num_objects * (the_hash_algo->rawsz/4) + nr;
c41a4a94
NP
44
45 return data_crc != ntohl(*index_crc);
46}
47
94e10825
NTND
48static int verify_packfile(struct repository *r,
49 struct packed_git *p,
c9486eb0 50 struct pack_window **w_curs,
1e49f22f
NTND
51 verify_fn fn,
52 struct progress *progress, uint32_t base_count)
53
f9253394 54{
c4001d92 55 off_t index_size = p->index_size;
42873078 56 const unsigned char *index_base = p->index_data;
ccc12e06 57 git_hash_ctx ctx;
9fd75046 58 unsigned char hash[GIT_MAX_RAWSZ], *pack_sig;
4277c670 59 off_t offset = 0, pack_sig_ofs = 0;
326bf396 60 uint32_t nr_objects, i;
62413604 61 int err = 0;
3af51928 62 struct idx_entry *entries;
f9253394 63
a9445d85
JK
64 if (!is_pack_valid(p))
65 return error("packfile %s cannot be accessed", p->pack_name);
f9253394 66
5ec9b8ac 67 r->hash_algo->init_fn(&ctx);
4277c670 68 do {
ef49a7a0 69 unsigned long remaining;
079afb18
SP
70 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
71 offset += remaining;
4277c670 72 if (!pack_sig_ofs)
5ec9b8ac 73 pack_sig_ofs = p->pack_size - r->hash_algo->rawsz;
62413604
NP
74 if (offset > pack_sig_ofs)
75 remaining -= (unsigned int)(offset - pack_sig_ofs);
5ec9b8ac 76 r->hash_algo->update_fn(&ctx, in, remaining);
4277c670 77 } while (offset < pack_sig_ofs);
5ec9b8ac 78 r->hash_algo->final_fn(hash, &ctx);
62413604 79 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
67947c34 80 if (!hasheq(hash, pack_sig))
ccc12e06 81 err = error("%s pack checksum mismatch",
62413604 82 p->pack_name);
5ec9b8ac 83 if (!hasheq(index_base + index_size - r->hash_algo->hexsz, pack_sig))
ccc12e06 84 err = error("%s pack checksum does not match its index",
62413604 85 p->pack_name);
079afb18 86 unuse_pack(w_curs);
f3bf9224
JH
87
88 /* Make sure everything reachable from idx is valid. Since we
89 * have verified that nr_objects matches between idx and pack,
90 * we do not do scan-streaming check on the pack file.
91 */
57059091 92 nr_objects = p->num_objects;
b32fa95f 93 ALLOC_ARRAY(entries, nr_objects + 1);
c41a4a94 94 entries[nr_objects].offset = pack_sig_ofs;
3af51928
AJ
95 /* first sort entries by pack offset, since unpacking them is more efficient that way */
96 for (i = 0; i < nr_objects; i++) {
99093238 97 entries[i].offset = nth_packed_object_offset(p, i);
c41a4a94 98 entries[i].nr = i;
3af51928 99 }
9ed0d8d6 100 QSORT(entries, nr_objects, compare_entries);
3af51928 101
62413604 102 for (i = 0; i < nr_objects; i++) {
f3bf9224 103 void *data;
63f4a7fc 104 struct object_id oid;
21666f1a 105 enum object_type type;
c4001d92 106 unsigned long size;
ec9d2249
NTND
107 off_t curpos;
108 int data_valid;
f3bf9224 109
63f4a7fc
JK
110 if (nth_packed_object_id(&oid, p, entries[i].nr) < 0)
111 BUG("unable to get oid of object %lu from %s",
112 (unsigned long)entries[i].nr, p->pack_name);
113
c41a4a94
NP
114 if (p->index_version > 1) {
115 off_t offset = entries[i].offset;
116 off_t len = entries[i+1].offset - offset;
117 unsigned int nr = entries[i].nr;
118 if (check_pack_crc(p, w_curs, offset, len, nr))
119 err = error("index CRC mismatch for object %s "
120 "from %s at offset %"PRIuMAX"",
63f4a7fc 121 oid_to_hex(&oid),
c41a4a94
NP
122 p->pack_name, (uintmax_t)offset);
123 }
ec9d2249
NTND
124
125 curpos = entries[i].offset;
126 type = unpack_object_header(p, w_curs, &curpos, &size);
127 unuse_pack(w_curs);
128
129 if (type == OBJ_BLOB && big_file_threshold <= size) {
130 /*
0f156dbb 131 * Let stream_object_signature() check it with
ec9d2249
NTND
132 * the streaming interface; no point slurping
133 * the data in-core only to discard.
134 */
135 data = NULL;
136 data_valid = 0;
137 } else {
94e10825 138 data = unpack_entry(r, p, entries[i].offset, &type, &size);
ec9d2249
NTND
139 data_valid = 1;
140 }
141
142 if (data_valid && !data)
62413604 143 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
63f4a7fc 144 oid_to_hex(&oid), p->pack_name,
62413604 145 (uintmax_t)entries[i].offset);
0f156dbb 146 else if (data && check_object_signature(r, &oid, data, size,
44439c1c 147 type) < 0)
0f156dbb
ÆAB
148 err = error("packed %s from %s is corrupt",
149 oid_to_hex(&oid), p->pack_name);
150 else if (!data && stream_object_signature(r, &oid) < 0)
1038f0c0 151 err = error("packed %s from %s is corrupt",
63f4a7fc 152 oid_to_hex(&oid), p->pack_name);
c9486eb0
NTND
153 else if (fn) {
154 int eaten = 0;
63f4a7fc 155 err |= fn(&oid, type, size, data, &eaten);
c9486eb0
NTND
156 if (eaten)
157 data = NULL;
158 }
1e49f22f
NTND
159 if (((base_count + i) & 1023) == 0)
160 display_progress(progress, base_count + i);
f3bf9224 161 free(data);
1e49f22f 162
f3bf9224 163 }
1e49f22f 164 display_progress(progress, base_count + i);
3af51928 165 free(entries);
f3bf9224
JH
166
167 return err;
f9253394
JH
168}
169
9b0aa728 170int verify_pack_index(struct packed_git *p)
f9253394 171{
62413604 172 int err = 0;
f9253394 173
d079837e
SP
174 if (open_pack_index(p))
175 return error("packfile %s index not opened", p->pack_name);
d079837e 176
f9253394 177 /* Verify SHA1 sum of the index file */
f9221e2c 178 if (!hashfile_checksum_valid(p->index_data, p->index_size))
ccc12e06 179 err = error("Packfile index for %s hash mismatch",
f3bf9224 180 p->pack_name);
9b0aa728
SP
181 return err;
182}
183
94e10825 184int verify_pack(struct repository *r, struct packed_git *p, verify_fn fn,
1e49f22f 185 struct progress *progress, uint32_t base_count)
9b0aa728
SP
186{
187 int err = 0;
188 struct pack_window *w_curs = NULL;
189
190 err |= verify_pack_index(p);
191 if (!p->index_data)
192 return -1;
f3bf9224 193
94e10825 194 err |= verify_packfile(r, p, &w_curs, fn, progress, base_count);
62413604 195 unuse_pack(&w_curs);
f3bf9224 196
62413604 197 return err;
f9253394 198}