Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "pack.h" |
Nicolas Pitre | 70f5d5d | 2008-02-28 00:25:19 -0500 | [diff] [blame] | 3 | #include "pack-revindex.h" |
Nguyễn Thái Ngọc Duy | 1e49f22 | 2011-11-07 09:59:26 +0700 | [diff] [blame] | 4 | #include "progress.h" |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 5 | |
Jonathan Nieder | 9cba13c | 2011-03-16 02:08:34 -0500 | [diff] [blame] | 6 | struct idx_entry { |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 7 | off_t offset; |
Nicolas Pitre | c41a4a9 | 2008-06-24 23:19:02 -0400 | [diff] [blame] | 8 | const unsigned char *sha1; |
| 9 | unsigned int nr; |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 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 | |
Nicolas Pitre | c41a4a9 | 2008-06-24 23:19:02 -0400 | [diff] [blame] | 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; |
Stephen Boyd | 1e4cd68 | 2011-04-03 00:06:54 -0700 | [diff] [blame] | 27 | uint32_t data_crc = crc32(0, NULL, 0); |
Nicolas Pitre | c41a4a9 | 2008-06-24 23:19:02 -0400 | [diff] [blame] | 28 | |
| 29 | do { |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 30 | unsigned long avail; |
Nicolas Pitre | c41a4a9 | 2008-06-24 23:19:02 -0400 | [diff] [blame] | 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 | |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 45 | static int verify_packfile(struct packed_git *p, |
Nguyễn Thái Ngọc Duy | c9486eb | 2011-11-07 09:59:25 +0700 | [diff] [blame] | 46 | struct pack_window **w_curs, |
Nguyễn Thái Ngọc Duy | 1e49f22 | 2011-11-07 09:59:26 +0700 | [diff] [blame] | 47 | verify_fn fn, |
| 48 | struct progress *progress, uint32_t base_count) |
| 49 | |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 50 | { |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 51 | off_t index_size = p->index_size; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 52 | const unsigned char *index_base = p->index_data; |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 53 | git_SHA_CTX ctx; |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 54 | unsigned char sha1[20], *pack_sig; |
Mike Hommey | 4277c67 | 2009-01-18 09:04:26 +0100 | [diff] [blame] | 55 | off_t offset = 0, pack_sig_ofs = 0; |
Shawn O. Pearce | 326bf39 | 2007-03-06 20:44:19 -0500 | [diff] [blame] | 56 | uint32_t nr_objects, i; |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 57 | int err = 0; |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 58 | struct idx_entry *entries; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 59 | |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 60 | /* Note that the pack header checks are actually performed by |
| 61 | * use_pack when it first opens the pack file. If anything |
| 62 | * goes wrong during those checks then the call will die out |
| 63 | * immediately. |
| 64 | */ |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 65 | |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 66 | git_SHA1_Init(&ctx); |
Mike Hommey | 4277c67 | 2009-01-18 09:04:26 +0100 | [diff] [blame] | 67 | do { |
Junio C Hamano | ef49a7a | 2011-06-10 11:52:15 -0700 | [diff] [blame] | 68 | unsigned long remaining; |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 69 | unsigned char *in = use_pack(p, w_curs, offset, &remaining); |
| 70 | offset += remaining; |
Mike Hommey | 4277c67 | 2009-01-18 09:04:26 +0100 | [diff] [blame] | 71 | if (!pack_sig_ofs) |
| 72 | pack_sig_ofs = p->pack_size - 20; |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 73 | if (offset > pack_sig_ofs) |
| 74 | remaining -= (unsigned int)(offset - pack_sig_ofs); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 75 | git_SHA1_Update(&ctx, in, remaining); |
Mike Hommey | 4277c67 | 2009-01-18 09:04:26 +0100 | [diff] [blame] | 76 | } while (offset < pack_sig_ofs); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 77 | git_SHA1_Final(sha1, &ctx); |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 78 | pack_sig = use_pack(p, w_curs, pack_sig_ofs, NULL); |
| 79 | if (hashcmp(sha1, pack_sig)) |
| 80 | err = error("%s SHA1 checksum mismatch", |
| 81 | p->pack_name); |
| 82 | if (hashcmp(index_base + index_size - 40, pack_sig)) |
Ralf Wildenhues | 22e5e58 | 2010-08-22 13:12:12 +0200 | [diff] [blame] | 83 | err = error("%s SHA1 does not match its index", |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 84 | p->pack_name); |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 85 | unuse_pack(w_curs); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 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 | */ |
Nicolas Pitre | 5705909 | 2007-04-09 01:06:28 -0400 | [diff] [blame] | 91 | nr_objects = p->num_objects; |
Nicolas Pitre | c41a4a9 | 2008-06-24 23:19:02 -0400 | [diff] [blame] | 92 | entries = xmalloc((nr_objects + 1) * sizeof(*entries)); |
| 93 | entries[nr_objects].offset = pack_sig_ofs; |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 94 | /* first sort entries by pack offset, since unpacking them is more efficient that way */ |
| 95 | for (i = 0; i < nr_objects; i++) { |
| 96 | entries[i].sha1 = nth_packed_object_sha1(p, i); |
| 97 | if (!entries[i].sha1) |
| 98 | die("internal error pack-check nth-packed-object"); |
Nicolas Pitre | 9909323 | 2008-06-24 23:17:12 -0400 | [diff] [blame] | 99 | entries[i].offset = nth_packed_object_offset(p, i); |
Nicolas Pitre | c41a4a9 | 2008-06-24 23:19:02 -0400 | [diff] [blame] | 100 | entries[i].nr = i; |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 101 | } |
| 102 | qsort(entries, nr_objects, sizeof(*entries), compare_entries); |
| 103 | |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 104 | for (i = 0; i < nr_objects; i++) { |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 105 | void *data; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 106 | enum object_type type; |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 107 | unsigned long size; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 108 | |
Nicolas Pitre | c41a4a9 | 2008-06-24 23:19:02 -0400 | [diff] [blame] | 109 | if (p->index_version > 1) { |
| 110 | off_t offset = entries[i].offset; |
| 111 | off_t len = entries[i+1].offset - offset; |
| 112 | unsigned int nr = entries[i].nr; |
| 113 | if (check_pack_crc(p, w_curs, offset, len, nr)) |
| 114 | err = error("index CRC mismatch for object %s " |
| 115 | "from %s at offset %"PRIuMAX"", |
| 116 | sha1_to_hex(entries[i].sha1), |
| 117 | p->pack_name, (uintmax_t)offset); |
| 118 | } |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 119 | data = unpack_entry(p, entries[i].offset, &type, &size); |
Nguyễn Thái Ngọc Duy | 4739351 | 2011-11-07 09:59:24 +0700 | [diff] [blame] | 120 | if (!data) |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 121 | err = error("cannot unpack %s from %s at offset %"PRIuMAX"", |
| 122 | sha1_to_hex(entries[i].sha1), p->pack_name, |
| 123 | (uintmax_t)entries[i].offset); |
Nguyễn Thái Ngọc Duy | 4739351 | 2011-11-07 09:59:24 +0700 | [diff] [blame] | 124 | else if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) |
Junio C Hamano | 1038f0c | 2005-07-07 15:12:20 -0700 | [diff] [blame] | 125 | err = error("packed %s from %s is corrupt", |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 126 | sha1_to_hex(entries[i].sha1), p->pack_name); |
Nguyễn Thái Ngọc Duy | c9486eb | 2011-11-07 09:59:25 +0700 | [diff] [blame] | 127 | else if (fn) { |
| 128 | int eaten = 0; |
| 129 | fn(entries[i].sha1, type, size, data, &eaten); |
| 130 | if (eaten) |
| 131 | data = NULL; |
| 132 | } |
Nguyễn Thái Ngọc Duy | 1e49f22 | 2011-11-07 09:59:26 +0700 | [diff] [blame] | 133 | if (((base_count + i) & 1023) == 0) |
| 134 | display_progress(progress, base_count + i); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 135 | free(data); |
Nguyễn Thái Ngọc Duy | 1e49f22 | 2011-11-07 09:59:26 +0700 | [diff] [blame] | 136 | |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 137 | } |
Nguyễn Thái Ngọc Duy | 1e49f22 | 2011-11-07 09:59:26 +0700 | [diff] [blame] | 138 | display_progress(progress, base_count + i); |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 139 | free(entries); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 140 | |
| 141 | return err; |
| 142 | } |
| 143 | |
Shawn O. Pearce | 9b0aa72 | 2010-04-19 07:23:07 -0700 | [diff] [blame] | 144 | int verify_pack_index(struct packed_git *p) |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 145 | { |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 146 | off_t index_size; |
| 147 | const unsigned char *index_base; |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 148 | git_SHA_CTX ctx; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 149 | unsigned char sha1[20]; |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 150 | int err = 0; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 151 | |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 152 | if (open_pack_index(p)) |
| 153 | return error("packfile %s index not opened", p->pack_name); |
| 154 | index_size = p->index_size; |
| 155 | index_base = p->index_data; |
| 156 | |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 157 | /* Verify SHA1 sum of the index file */ |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 158 | git_SHA1_Init(&ctx); |
| 159 | git_SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20)); |
| 160 | git_SHA1_Final(sha1, &ctx); |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 161 | if (hashcmp(sha1, index_base + index_size - 20)) |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 162 | err = error("Packfile index for %s SHA1 mismatch", |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 163 | p->pack_name); |
Shawn O. Pearce | 9b0aa72 | 2010-04-19 07:23:07 -0700 | [diff] [blame] | 164 | return err; |
| 165 | } |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 166 | |
Nguyễn Thái Ngọc Duy | 1e49f22 | 2011-11-07 09:59:26 +0700 | [diff] [blame] | 167 | int verify_pack(struct packed_git *p, verify_fn fn, |
| 168 | struct progress *progress, uint32_t base_count) |
Shawn O. Pearce | 9b0aa72 | 2010-04-19 07:23:07 -0700 | [diff] [blame] | 169 | { |
| 170 | int err = 0; |
| 171 | struct pack_window *w_curs = NULL; |
| 172 | |
| 173 | err |= verify_pack_index(p); |
| 174 | if (!p->index_data) |
| 175 | return -1; |
| 176 | |
Nguyễn Thái Ngọc Duy | 1e49f22 | 2011-11-07 09:59:26 +0700 | [diff] [blame] | 177 | err |= verify_packfile(p, &w_curs, fn, progress, base_count); |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 178 | unuse_pack(&w_curs); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 179 | |
Nicolas Pitre | 6241360 | 2008-05-29 17:34:50 -0400 | [diff] [blame] | 180 | return err; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 181 | } |