]> git.ipfire.org Git - thirdparty/git.git/blame - pack-check.c
is_ntfs_dotgit(): only verify the leading segment
[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;
9fd75046 8 union idx_entry_object {
9 const unsigned char *hash;
10 struct object_id *oid;
11 } oid;
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;
43 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
44
45 return data_crc != ntohl(*index_crc);
46}
47
03e79c88 48static int verify_packfile(struct packed_git *p,
c9486eb0 49 struct pack_window **w_curs,
1e49f22f
NTND
50 verify_fn fn,
51 struct progress *progress, uint32_t base_count)
52
f9253394 53{
c4001d92 54 off_t index_size = p->index_size;
42873078 55 const unsigned char *index_base = p->index_data;
9126f009 56 git_SHA_CTX ctx;
9fd75046 57 unsigned char hash[GIT_MAX_RAWSZ], *pack_sig;
4277c670 58 off_t offset = 0, pack_sig_ofs = 0;
326bf396 59 uint32_t nr_objects, i;
62413604 60 int err = 0;
3af51928 61 struct idx_entry *entries;
f9253394 62
a9445d85
JK
63 if (!is_pack_valid(p))
64 return error("packfile %s cannot be accessed", p->pack_name);
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);
9fd75046 77 git_SHA1_Final(hash, &ctx);
62413604 78 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
9fd75046 79 if (hashcmp(hash, pack_sig))
62413604
NP
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;
b32fa95f 92 ALLOC_ARRAY(entries, nr_objects + 1);
c41a4a94 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++) {
9fd75046 96 entries[i].oid.hash = nth_packed_object_sha1(p, i);
97 if (!entries[i].oid.hash)
3af51928 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 101 }
9ed0d8d6 102 QSORT(entries, nr_objects, compare_entries);
3af51928 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;
ec9d2249
NTND
108 off_t curpos;
109 int data_valid;
f3bf9224 110
c41a4a94
NP
111 if (p->index_version > 1) {
112 off_t offset = entries[i].offset;
113 off_t len = entries[i+1].offset - offset;
114 unsigned int nr = entries[i].nr;
115 if (check_pack_crc(p, w_curs, offset, len, nr))
116 err = error("index CRC mismatch for object %s "
117 "from %s at offset %"PRIuMAX"",
9fd75046 118 oid_to_hex(entries[i].oid.oid),
c41a4a94
NP
119 p->pack_name, (uintmax_t)offset);
120 }
ec9d2249
NTND
121
122 curpos = entries[i].offset;
123 type = unpack_object_header(p, w_curs, &curpos, &size);
124 unuse_pack(w_curs);
125
126 if (type == OBJ_BLOB && big_file_threshold <= size) {
127 /*
128 * Let check_sha1_signature() check it with
129 * the streaming interface; no point slurping
130 * the data in-core only to discard.
131 */
132 data = NULL;
133 data_valid = 0;
134 } else {
135 data = unpack_entry(p, entries[i].offset, &type, &size);
136 data_valid = 1;
137 }
138
139 if (data_valid && !data)
62413604 140 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
9fd75046 141 oid_to_hex(entries[i].oid.oid), p->pack_name,
62413604 142 (uintmax_t)entries[i].offset);
9fd75046 143 else if (check_sha1_signature(entries[i].oid.hash, data, size, typename(type)))
1038f0c0 144 err = error("packed %s from %s is corrupt",
9fd75046 145 oid_to_hex(entries[i].oid.oid), p->pack_name);
c9486eb0
NTND
146 else if (fn) {
147 int eaten = 0;
9fd75046 148 err |= fn(entries[i].oid.oid, type, size, data, &eaten);
c9486eb0
NTND
149 if (eaten)
150 data = NULL;
151 }
1e49f22f
NTND
152 if (((base_count + i) & 1023) == 0)
153 display_progress(progress, base_count + i);
f3bf9224 154 free(data);
1e49f22f 155
f3bf9224 156 }
1e49f22f 157 display_progress(progress, base_count + i);
3af51928 158 free(entries);
f3bf9224
JH
159
160 return err;
f9253394
JH
161}
162
9b0aa728 163int verify_pack_index(struct packed_git *p)
f9253394 164{
d079837e
SP
165 off_t index_size;
166 const unsigned char *index_base;
9126f009 167 git_SHA_CTX ctx;
f9253394 168 unsigned char sha1[20];
62413604 169 int err = 0;
f9253394 170
d079837e
SP
171 if (open_pack_index(p))
172 return error("packfile %s index not opened", p->pack_name);
173 index_size = p->index_size;
174 index_base = p->index_data;
175
f9253394 176 /* Verify SHA1 sum of the index file */
9126f009
NP
177 git_SHA1_Init(&ctx);
178 git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
179 git_SHA1_Final(sha1, &ctx);
42873078 180 if (hashcmp(sha1, index_base + index_size - 20))
62413604 181 err = error("Packfile index for %s SHA1 mismatch",
f3bf9224 182 p->pack_name);
9b0aa728
SP
183 return err;
184}
185
1e49f22f
NTND
186int verify_pack(struct packed_git *p, verify_fn fn,
187 struct progress *progress, uint32_t base_count)
9b0aa728
SP
188{
189 int err = 0;
190 struct pack_window *w_curs = NULL;
191
192 err |= verify_pack_index(p);
193 if (!p->index_data)
194 return -1;
f3bf9224 195
1e49f22f 196 err |= verify_packfile(p, &w_curs, fn, progress, base_count);
62413604 197 unuse_pack(&w_curs);
f3bf9224 198
62413604 199 return err;
f9253394 200}