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