blob: 0c19b6e5a5677bd14989175abddc119381fac4ef [file] [log] [blame]
Junio C Hamanof3bf9222005-06-30 17:15:39 -07001#include "cache.h"
2#include "pack.h"
Nicolas Pitre70f5d5d2008-02-28 00:25:19 -05003#include "pack-revindex.h"
Junio C Hamanof3bf9222005-06-30 17:15:39 -07004
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05005struct idx_entry {
Alexandre Julliard3af51922007-06-03 20:21:41 +02006 off_t offset;
Nicolas Pitrec41a4a92008-06-24 23:19:02 -04007 const unsigned char *sha1;
8 unsigned int nr;
Alexandre Julliard3af51922007-06-03 20:21:41 +02009};
10
11static int compare_entries(const void *e1, const void *e2)
12{
13 const struct idx_entry *entry1 = e1;
14 const struct idx_entry *entry2 = e2;
15 if (entry1->offset < entry2->offset)
16 return -1;
17 if (entry1->offset > entry2->offset)
18 return 1;
19 return 0;
20}
21
Nicolas Pitrec41a4a92008-06-24 23:19:02 -040022int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
23 off_t offset, off_t len, unsigned int nr)
24{
25 const uint32_t *index_crc;
Stephen Boyd1e4cd682011-04-03 00:06:54 -070026 uint32_t data_crc = crc32(0, NULL, 0);
Nicolas Pitrec41a4a92008-06-24 23:19:02 -040027
28 do {
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070029 unsigned long avail;
Nicolas Pitrec41a4a92008-06-24 23:19:02 -040030 void *data = use_pack(p, w_curs, offset, &avail);
31 if (avail > len)
32 avail = len;
33 data_crc = crc32(data_crc, data, avail);
34 offset += avail;
35 len -= avail;
36 } while (len);
37
38 index_crc = p->index_data;
39 index_crc += 2 + 256 + p->num_objects * (20/4) + nr;
40
41 return data_crc != ntohl(*index_crc);
42}
43
Shawn O. Pearce03e79c82006-12-23 02:34:08 -050044static int verify_packfile(struct packed_git *p,
45 struct pack_window **w_curs)
Junio C Hamanof3bf9222005-06-30 17:15:39 -070046{
Shawn O. Pearcec4001d92007-03-06 20:44:30 -050047 off_t index_size = p->index_size;
Nicolas Pitre42873072007-03-16 16:42:50 -040048 const unsigned char *index_base = p->index_data;
Nicolas Pitre9126f002008-10-01 14:05:20 -040049 git_SHA_CTX ctx;
Nicolas Pitre62413602008-05-29 17:34:50 -040050 unsigned char sha1[20], *pack_sig;
Mike Hommey4277c672009-01-18 09:04:26 +010051 off_t offset = 0, pack_sig_ofs = 0;
Shawn O. Pearce326bf392007-03-06 20:44:19 -050052 uint32_t nr_objects, i;
Nicolas Pitre62413602008-05-29 17:34:50 -040053 int err = 0;
Alexandre Julliard3af51922007-06-03 20:21:41 +020054 struct idx_entry *entries;
Junio C Hamanof3bf9222005-06-30 17:15:39 -070055
Shawn O. Pearce079afb12006-12-23 02:34:13 -050056 /* Note that the pack header checks are actually performed by
57 * use_pack when it first opens the pack file. If anything
58 * goes wrong during those checks then the call will die out
59 * immediately.
60 */
Junio C Hamanof3bf9222005-06-30 17:15:39 -070061
Nicolas Pitre9126f002008-10-01 14:05:20 -040062 git_SHA1_Init(&ctx);
Mike Hommey4277c672009-01-18 09:04:26 +010063 do {
Junio C Hamanoef49a7a2011-06-10 11:52:15 -070064 unsigned long remaining;
Shawn O. Pearce079afb12006-12-23 02:34:13 -050065 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
66 offset += remaining;
Mike Hommey4277c672009-01-18 09:04:26 +010067 if (!pack_sig_ofs)
68 pack_sig_ofs = p->pack_size - 20;
Nicolas Pitre62413602008-05-29 17:34:50 -040069 if (offset > pack_sig_ofs)
70 remaining -= (unsigned int)(offset - pack_sig_ofs);
Nicolas Pitre9126f002008-10-01 14:05:20 -040071 git_SHA1_Update(&ctx, in, remaining);
Mike Hommey4277c672009-01-18 09:04:26 +010072 } while (offset < pack_sig_ofs);
Nicolas Pitre9126f002008-10-01 14:05:20 -040073 git_SHA1_Final(sha1, &ctx);
Nicolas Pitre62413602008-05-29 17:34:50 -040074 pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL);
75 if (hashcmp(sha1, pack_sig))
76 err = error("%s SHA1 checksum mismatch",
77 p->pack_name);
78 if (hashcmp(index_base + index_size - 40, pack_sig))
Ralf Wildenhues22e5e582010-08-22 13:12:12 +020079 err = error("%s SHA1 does not match its index",
Nicolas Pitre62413602008-05-29 17:34:50 -040080 p->pack_name);
Shawn O. Pearce079afb12006-12-23 02:34:13 -050081 unuse_pack(w_curs);
Junio C Hamanof3bf9222005-06-30 17:15:39 -070082
83 /* Make sure everything reachable from idx is valid. Since we
84 * have verified that nr_objects matches between idx and pack,
85 * we do not do scan-streaming check on the pack file.
86 */
Nicolas Pitre57059092007-04-09 01:06:28 -040087 nr_objects = p->num_objects;
Nicolas Pitrec41a4a92008-06-24 23:19:02 -040088 entries = xmalloc((nr_objects + 1) * sizeof(*entries));
89 entries[nr_objects].offset = pack_sig_ofs;
Alexandre Julliard3af51922007-06-03 20:21:41 +020090 /* first sort entries by pack offset, since unpacking them is more efficient that way */
91 for (i = 0; i < nr_objects; i++) {
92 entries[i].sha1 = nth_packed_object_sha1(p, i);
93 if (!entries[i].sha1)
94 die("internal error pack-check nth-packed-object");
Nicolas Pitre99093232008-06-24 23:17:12 -040095 entries[i].offset = nth_packed_object_offset(p, i);
Nicolas Pitrec41a4a92008-06-24 23:19:02 -040096 entries[i].nr = i;
Alexandre Julliard3af51922007-06-03 20:21:41 +020097 }
98 qsort(entries, nr_objects, sizeof(*entries), compare_entries);
99
Nicolas Pitre62413602008-05-29 17:34:50 -0400100 for (i = 0; i < nr_objects; i++) {
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700101 void *data;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500102 enum object_type type;
Shawn O. Pearcec4001d92007-03-06 20:44:30 -0500103 unsigned long size;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700104
Nicolas Pitrec41a4a92008-06-24 23:19:02 -0400105 if (p->index_version > 1) {
106 off_t offset = entries[i].offset;
107 off_t len = entries[i+1].offset - offset;
108 unsigned int nr = entries[i].nr;
109 if (check_pack_crc(p, w_curs, offset, len, nr))
110 err = error("index CRC mismatch for object %s "
111 "from %s at offset %"PRIuMAX"",
112 sha1_to_hex(entries[i].sha1),
113 p->pack_name, (uintmax_t)offset);
114 }
Alexandre Julliard3af51922007-06-03 20:21:41 +0200115 data = unpack_entry(p, entries[i].offset, &type, &size);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700116 if (!data) {
Nicolas Pitre62413602008-05-29 17:34:50 -0400117 err = error("cannot unpack %s from %s at offset %"PRIuMAX"",
118 sha1_to_hex(entries[i].sha1), p->pack_name,
119 (uintmax_t)entries[i].offset);
120 break;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700121 }
Alexandre Julliard3af51922007-06-03 20:21:41 +0200122 if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
Junio C Hamano1038f0c2005-07-07 15:12:20 -0700123 err = error("packed %s from %s is corrupt",
Alexandre Julliard3af51922007-06-03 20:21:41 +0200124 sha1_to_hex(entries[i].sha1), p->pack_name);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700125 free(data);
Nicolas Pitre62413602008-05-29 17:34:50 -0400126 break;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700127 }
128 free(data);
129 }
Alexandre Julliard3af51922007-06-03 20:21:41 +0200130 free(entries);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700131
132 return err;
133}
134
Shawn O. Pearce9b0aa722010-04-19 07:23:07 -0700135int verify_pack_index(struct packed_git *p)
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700136{
Shawn O. Pearced0798372007-05-26 01:24:19 -0400137 off_t index_size;
138 const unsigned char *index_base;
Nicolas Pitre9126f002008-10-01 14:05:20 -0400139 git_SHA_CTX ctx;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700140 unsigned char sha1[20];
Nicolas Pitre62413602008-05-29 17:34:50 -0400141 int err = 0;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700142
Shawn O. Pearced0798372007-05-26 01:24:19 -0400143 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
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700148 /* Verify SHA1 sum of the index file */
Nicolas Pitre9126f002008-10-01 14:05:20 -0400149 git_SHA1_Init(&ctx);
150 git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20));
151 git_SHA1_Final(sha1, &ctx);
Nicolas Pitre42873072007-03-16 16:42:50 -0400152 if (hashcmp(sha1, index_base + index_size - 20))
Nicolas Pitre62413602008-05-29 17:34:50 -0400153 err = error("Packfile index for %s SHA1 mismatch",
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700154 p->pack_name);
Shawn O. Pearce9b0aa722010-04-19 07:23:07 -0700155 return err;
156}
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700157
Shawn O. Pearce9b0aa722010-04-19 07:23:07 -0700158int verify_pack(struct packed_git *p)
159{
160 int err = 0;
161 struct pack_window *w_curs = NULL;
162
163 err |= verify_pack_index(p);
164 if (!p->index_data)
165 return -1;
166
Nicolas Pitre62413602008-05-29 17:34:50 -0400167 err |= verify_packfile(p, &w_curs);
168 unuse_pack(&w_curs);
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700169
Nicolas Pitre62413602008-05-29 17:34:50 -0400170 return err;
Junio C Hamanof3bf9222005-06-30 17:15:39 -0700171}