merge-ort/merge-recursive: do report errors in `merge_submodule()`
[git/gitster.git] / pack-check.c
blob25104d5b14c1e8f9a9988f106a1ad1a6823ca31c
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "hex.h"
4 #include "repository.h"
5 #include "pack.h"
6 #include "progress.h"
7 #include "packfile.h"
8 #include "object-file.h"
9 #include "object-store-ll.h"
11 struct idx_entry {
12 off_t offset;
13 unsigned int nr;
16 static int compare_entries(const void *e1, const void *e2)
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;
27 int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
28 off_t offset, off_t len, unsigned int nr)
30 const uint32_t *index_crc;
31 uint32_t data_crc = crc32(0, NULL, 0);
33 do {
34 unsigned long avail;
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);
43 index_crc = p->index_data;
44 index_crc += 2 + 256 + (size_t)p->num_objects * (the_hash_algo->rawsz/4) + nr;
46 return data_crc != ntohl(*index_crc);
49 static int verify_packfile(struct repository *r,
50 struct packed_git *p,
51 struct pack_window **w_curs,
52 verify_fn fn,
53 struct progress *progress, uint32_t base_count)
56 off_t index_size = p->index_size;
57 const unsigned char *index_base = p->index_data;
58 git_hash_ctx ctx;
59 unsigned char hash[GIT_MAX_RAWSZ], *pack_sig;
60 off_t offset = 0, pack_sig_ofs = 0;
61 uint32_t nr_objects, i;
62 int err = 0;
63 struct idx_entry *entries;
65 if (!is_pack_valid(p))
66 return error("packfile %s cannot be accessed", p->pack_name);
68 r->hash_algo->init_fn(&ctx);
69 do {
70 unsigned long remaining;
71 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
72 offset += remaining;
73 if (!pack_sig_ofs)
74 pack_sig_ofs = p->pack_size - r->hash_algo->rawsz;
75 if (offset > pack_sig_ofs)
76 remaining -= (unsigned int)(offset - pack_sig_ofs);
77 r->hash_algo->update_fn(&ctx, in, remaining);
78 } while (offset < pack_sig_ofs);
79 r->hash_algo->final_fn(hash, &ctx);
80 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
81 if (!hasheq(hash, pack_sig))
82 err = error("%s pack checksum mismatch",
83 p->pack_name);
84 if (!hasheq(index_base + index_size - r->hash_algo->hexsz, pack_sig))
85 err = error("%s pack checksum does not match its index",
86 p->pack_name);
87 unuse_pack(w_curs);
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.
93 nr_objects = p->num_objects;
94 ALLOC_ARRAY(entries, nr_objects + 1);
95 entries[nr_objects].offset = pack_sig_ofs;
96 /* first sort entries by pack offset, since unpacking them is more efficient that way */
97 for (i = 0; i < nr_objects; i++) {
98 entries[i].offset = nth_packed_object_offset(p, i);
99 entries[i].nr = i;
101 QSORT(entries, nr_objects, compare_entries);
103 for (i = 0; i < nr_objects; i++) {
104 void *data;
105 struct object_id oid;
106 enum object_type type;
107 unsigned long size;
108 off_t curpos;
109 int data_valid;
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);
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"",
122 oid_to_hex(&oid),
123 p->pack_name, (uintmax_t)offset);
126 curpos = entries[i].offset;
127 type = unpack_object_header(p, w_curs, &curpos, &size);
128 unuse_pack(w_curs);
130 if (type == OBJ_BLOB && big_file_threshold <= size) {
132 * Let stream_object_signature() check it with
133 * the streaming interface; no point slurping
134 * the data in-core only to discard.
136 data = NULL;
137 data_valid = 0;
138 } else {
139 data = unpack_entry(r, p, entries[i].offset, &type, &size);
140 data_valid = 1;
143 if (data_valid && !data)
144 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
145 oid_to_hex(&oid), p->pack_name,
146 (uintmax_t)entries[i].offset);
147 else if (data && check_object_signature(r, &oid, data, size,
148 type) < 0)
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)
152 err = error("packed %s from %s is corrupt",
153 oid_to_hex(&oid), p->pack_name);
154 else if (fn) {
155 int eaten = 0;
156 err |= fn(&oid, type, size, data, &eaten);
157 if (eaten)
158 data = NULL;
160 if (((base_count + i) & 1023) == 0)
161 display_progress(progress, base_count + i);
162 free(data);
165 display_progress(progress, base_count + i);
166 free(entries);
168 return err;
171 int verify_pack_index(struct packed_git *p)
173 int err = 0;
175 if (open_pack_index(p))
176 return error("packfile %s index not opened", p->pack_name);
178 /* Verify SHA1 sum of the index file */
179 if (!hashfile_checksum_valid(p->index_data, p->index_size))
180 err = error("Packfile index for %s hash mismatch",
181 p->pack_name);
182 return err;
185 int verify_pack(struct repository *r, struct packed_git *p, verify_fn fn,
186 struct progress *progress, uint32_t base_count)
188 int err = 0;
189 struct pack_window *w_curs = NULL;
191 err |= verify_pack_index(p);
192 if (!p->index_data)
193 return -1;
195 err |= verify_packfile(r, p, &w_curs, fn, progress, base_count);
196 unuse_pack(&w_curs);
198 return err;