]>
Commit | Line | Data |
---|---|---|
f9253394 JH |
1 | #include "cache.h" |
2 | #include "pack.h" | |
70f5d5d3 | 3 | #include "pack-revindex.h" |
f9253394 | 4 | |
3af51928 AJ |
5 | struct idx_entry |
6 | { | |
3af51928 | 7 | off_t offset; |
c41a4a94 NP |
8 | const unsigned char *sha1; |
9 | unsigned int nr; | |
3af51928 AJ |
10 | }; |
11 | ||
12 | static 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 |
23 | int 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; | |
27 | uint32_t data_crc = crc32(0, Z_NULL, 0); | |
28 | ||
29 | do { | |
30 | unsigned int avail; | |
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 SP |
45 | static int verify_packfile(struct packed_git *p, |
46 | struct pack_window **w_curs) | |
f9253394 | 47 | { |
c4001d92 | 48 | off_t index_size = p->index_size; |
42873078 | 49 | const unsigned char *index_base = p->index_data; |
f9253394 | 50 | SHA_CTX ctx; |
62413604 NP |
51 | unsigned char sha1[20], *pack_sig; |
52 | off_t offset = 0, pack_sig_ofs = p->pack_size - 20; | |
326bf396 | 53 | uint32_t nr_objects, i; |
62413604 | 54 | int err = 0; |
3af51928 | 55 | struct idx_entry *entries; |
f9253394 | 56 | |
079afb18 SP |
57 | /* Note that the pack header checks are actually performed by |
58 | * use_pack when it first opens the pack file. If anything | |
59 | * goes wrong during those checks then the call will die out | |
60 | * immediately. | |
61 | */ | |
f9253394 JH |
62 | |
63 | SHA1_Init(&ctx); | |
62413604 | 64 | while (offset < pack_sig_ofs) { |
079afb18 SP |
65 | unsigned int remaining; |
66 | unsigned char *in = use_pack(p, w_curs, offset, &remaining); | |
67 | offset += remaining; | |
62413604 NP |
68 | if (offset > pack_sig_ofs) |
69 | remaining -= (unsigned int)(offset - pack_sig_ofs); | |
079afb18 SP |
70 | SHA1_Update(&ctx, in, remaining); |
71 | } | |
f9253394 | 72 | SHA1_Final(sha1, &ctx); |
62413604 NP |
73 | pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL); |
74 | if (hashcmp(sha1, pack_sig)) | |
75 | err = error("%s SHA1 checksum mismatch", | |
76 | p->pack_name); | |
77 | if (hashcmp(index_base + index_size - 40, pack_sig)) | |
78 | err = error("%s SHA1 does not match its inddex", | |
79 | p->pack_name); | |
079afb18 | 80 | unuse_pack(w_curs); |
f3bf9224 JH |
81 | |
82 | /* Make sure everything reachable from idx is valid. Since we | |
83 | * have verified that nr_objects matches between idx and pack, | |
84 | * we do not do scan-streaming check on the pack file. | |
85 | */ | |
57059091 | 86 | nr_objects = p->num_objects; |
c41a4a94 NP |
87 | entries = xmalloc((nr_objects + 1) * sizeof(*entries)); |
88 | entries[nr_objects].offset = pack_sig_ofs; | |
3af51928 AJ |
89 | /* first sort entries by pack offset, since unpacking them is more efficient that way */ |
90 | for (i = 0; i < nr_objects; i++) { | |
91 | entries[i].sha1 = nth_packed_object_sha1(p, i); | |
92 | if (!entries[i].sha1) | |
93 | die("internal error pack-check nth-packed-object"); | |
99093238 | 94 | entries[i].offset = nth_packed_object_offset(p, i); |
c41a4a94 | 95 | entries[i].nr = i; |
3af51928 AJ |
96 | } |
97 | qsort(entries, nr_objects, sizeof(*entries), compare_entries); | |
98 | ||
62413604 | 99 | for (i = 0; i < nr_objects; i++) { |
f3bf9224 | 100 | void *data; |
21666f1a | 101 | enum object_type type; |
c4001d92 | 102 | unsigned long size; |
f3bf9224 | 103 | |
c41a4a94 NP |
104 | if (p->index_version > 1) { |
105 | off_t offset = entries[i].offset; | |
106 | off_t len = entries[i+1].offset - offset; | |
107 | unsigned int nr = entries[i].nr; | |
108 | if (check_pack_crc(p, w_curs, offset, len, nr)) | |
109 | err = error("index CRC mismatch for object %s " | |
110 | "from %s at offset %"PRIuMAX"", | |
111 | sha1_to_hex(entries[i].sha1), | |
112 | p->pack_name, (uintmax_t)offset); | |
113 | } | |
3af51928 | 114 | data = unpack_entry(p, entries[i].offset, &type, &size); |
f3bf9224 | 115 | if (!data) { |
62413604 NP |
116 | err = error("cannot unpack %s from %s at offset %"PRIuMAX"", |
117 | sha1_to_hex(entries[i].sha1), p->pack_name, | |
118 | (uintmax_t)entries[i].offset); | |
119 | break; | |
f3bf9224 | 120 | } |
3af51928 | 121 | if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) { |
1038f0c0 | 122 | err = error("packed %s from %s is corrupt", |
3af51928 | 123 | sha1_to_hex(entries[i].sha1), p->pack_name); |
f3bf9224 | 124 | free(data); |
62413604 | 125 | break; |
f3bf9224 JH |
126 | } |
127 | free(data); | |
128 | } | |
3af51928 | 129 | free(entries); |
f3bf9224 JH |
130 | |
131 | return err; | |
f9253394 JH |
132 | } |
133 | ||
77d3ecee | 134 | int verify_pack(struct packed_git *p) |
f9253394 | 135 | { |
d079837e SP |
136 | off_t index_size; |
137 | const unsigned char *index_base; | |
f9253394 JH |
138 | SHA_CTX ctx; |
139 | unsigned char sha1[20]; | |
62413604 NP |
140 | int err = 0; |
141 | struct pack_window *w_curs = NULL; | |
f9253394 | 142 | |
d079837e SP |
143 | if (open_pack_index(p)) |
144 | return error("packfile %s index not opened", p->pack_name); | |
145 | index_size = p->index_size; | |
146 | index_base = p->index_data; | |
147 | ||
f9253394 JH |
148 | /* Verify SHA1 sum of the index file */ |
149 | SHA1_Init(&ctx); | |
c4001d92 | 150 | SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20)); |
f9253394 | 151 | SHA1_Final(sha1, &ctx); |
42873078 | 152 | if (hashcmp(sha1, index_base + index_size - 20)) |
62413604 | 153 | err = error("Packfile index for %s SHA1 mismatch", |
f3bf9224 JH |
154 | p->pack_name); |
155 | ||
62413604 NP |
156 | /* Verify pack file */ |
157 | err |= verify_packfile(p, &w_curs); | |
158 | unuse_pack(&w_curs); | |
f3bf9224 | 159 | |
62413604 | 160 | return err; |
f9253394 | 161 | } |