Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "pack.h" |
| 3 | |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 4 | struct idx_entry |
| 5 | { |
| 6 | const unsigned char *sha1; |
| 7 | off_t offset; |
| 8 | }; |
| 9 | |
| 10 | static int compare_entries(const void *e1, const void *e2) |
| 11 | { |
| 12 | const struct idx_entry *entry1 = e1; |
| 13 | const struct idx_entry *entry2 = e2; |
| 14 | if (entry1->offset < entry2->offset) |
| 15 | return -1; |
| 16 | if (entry1->offset > entry2->offset) |
| 17 | return 1; |
| 18 | return 0; |
| 19 | } |
| 20 | |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 21 | static int verify_packfile(struct packed_git *p, |
| 22 | struct pack_window **w_curs) |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 23 | { |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 24 | off_t index_size = p->index_size; |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 25 | const unsigned char *index_base = p->index_data; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 26 | SHA_CTX ctx; |
| 27 | unsigned char sha1[20]; |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 28 | off_t offset = 0, pack_sig = p->pack_size - 20; |
Shawn O. Pearce | 326bf39 | 2007-03-06 20:44:19 -0500 | [diff] [blame] | 29 | uint32_t nr_objects, i; |
| 30 | int err; |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 31 | struct idx_entry *entries; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 32 | |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 33 | /* Note that the pack header checks are actually performed by |
| 34 | * use_pack when it first opens the pack file. If anything |
| 35 | * goes wrong during those checks then the call will die out |
| 36 | * immediately. |
| 37 | */ |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 38 | |
| 39 | SHA1_Init(&ctx); |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 40 | while (offset < pack_sig) { |
| 41 | unsigned int remaining; |
| 42 | unsigned char *in = use_pack(p, w_curs, offset, &remaining); |
| 43 | offset += remaining; |
| 44 | if (offset > pack_sig) |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 45 | remaining -= (unsigned int)(offset - pack_sig); |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 46 | SHA1_Update(&ctx, in, remaining); |
| 47 | } |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 48 | SHA1_Final(sha1, &ctx); |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 49 | if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL))) |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 50 | return error("Packfile %s SHA1 mismatch with itself", |
| 51 | p->pack_name); |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 52 | if (hashcmp(sha1, index_base + index_size - 40)) |
Junio C Hamano | 55e1805 | 2006-04-27 15:42:01 -0700 | [diff] [blame] | 53 | return error("Packfile %s SHA1 mismatch with idx", |
| 54 | p->pack_name); |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 55 | unuse_pack(w_curs); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 56 | |
| 57 | /* Make sure everything reachable from idx is valid. Since we |
| 58 | * have verified that nr_objects matches between idx and pack, |
| 59 | * we do not do scan-streaming check on the pack file. |
| 60 | */ |
Nicolas Pitre | 5705909 | 2007-04-09 01:06:28 -0400 | [diff] [blame] | 61 | nr_objects = p->num_objects; |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 62 | entries = xmalloc(nr_objects * sizeof(*entries)); |
| 63 | /* first sort entries by pack offset, since unpacking them is more efficient that way */ |
| 64 | for (i = 0; i < nr_objects; i++) { |
| 65 | entries[i].sha1 = nth_packed_object_sha1(p, i); |
| 66 | if (!entries[i].sha1) |
| 67 | die("internal error pack-check nth-packed-object"); |
| 68 | entries[i].offset = find_pack_entry_one(entries[i].sha1, p); |
| 69 | if (!entries[i].offset) |
| 70 | die("internal error pack-check find-pack-entry-one"); |
| 71 | } |
| 72 | qsort(entries, nr_objects, sizeof(*entries), compare_entries); |
| 73 | |
Shawn O. Pearce | 326bf39 | 2007-03-06 20:44:19 -0500 | [diff] [blame] | 74 | for (i = 0, err = 0; i < nr_objects; i++) { |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 75 | void *data; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 76 | enum object_type type; |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 77 | unsigned long size; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 78 | |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 79 | data = unpack_entry(p, entries[i].offset, &type, &size); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 80 | if (!data) { |
| 81 | err = error("cannot unpack %s from %s", |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 82 | sha1_to_hex(entries[i].sha1), p->pack_name); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 83 | continue; |
| 84 | } |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 85 | if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) { |
Junio C Hamano | 1038f0c | 2005-07-07 15:12:20 -0700 | [diff] [blame] | 86 | err = error("packed %s from %s is corrupt", |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 87 | sha1_to_hex(entries[i].sha1), p->pack_name); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 88 | free(data); |
| 89 | continue; |
| 90 | } |
| 91 | free(data); |
| 92 | } |
Alexandre Julliard | 3af5192 | 2007-06-03 20:21:41 +0200 | [diff] [blame] | 93 | free(entries); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 94 | |
| 95 | return err; |
| 96 | } |
| 97 | |
| 98 | |
Nicolas Pitre | ddcf786 | 2007-05-25 23:37:40 -0400 | [diff] [blame] | 99 | #define MAX_CHAIN 50 |
Junio C Hamano | 473ab16 | 2006-03-05 11:20:12 -0800 | [diff] [blame] | 100 | |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 101 | static void show_pack_info(struct packed_git *p) |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 102 | { |
Nicolas Pitre | ddcf786 | 2007-05-25 23:37:40 -0400 | [diff] [blame] | 103 | uint32_t nr_objects, i, chain_histogram[MAX_CHAIN+1]; |
Nicolas Pitre | 5705909 | 2007-04-09 01:06:28 -0400 | [diff] [blame] | 104 | nr_objects = p->num_objects; |
Junio C Hamano | 473ab16 | 2006-03-05 11:20:12 -0800 | [diff] [blame] | 105 | memset(chain_histogram, 0, sizeof(chain_histogram)); |
Junio C Hamano | ad8c80a | 2005-06-30 17:17:20 -0700 | [diff] [blame] | 106 | |
| 107 | for (i = 0; i < nr_objects; i++) { |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 108 | const unsigned char *sha1; |
| 109 | unsigned char base_sha1[20]; |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 110 | const char *type; |
Junio C Hamano | ad8c80a | 2005-06-30 17:17:20 -0700 | [diff] [blame] | 111 | unsigned long size; |
| 112 | unsigned long store_size; |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 113 | off_t offset; |
Junio C Hamano | f8f135c | 2006-02-15 12:47:43 -0800 | [diff] [blame] | 114 | unsigned int delta_chain_length; |
Junio C Hamano | ad8c80a | 2005-06-30 17:17:20 -0700 | [diff] [blame] | 115 | |
Nicolas Pitre | d72308e | 2007-04-04 16:49:04 -0400 | [diff] [blame] | 116 | sha1 = nth_packed_object_sha1(p, i); |
| 117 | if (!sha1) |
Junio C Hamano | ad8c80a | 2005-06-30 17:17:20 -0700 | [diff] [blame] | 118 | die("internal error pack-check nth-packed-object"); |
Nicolas Pitre | 4305730 | 2006-09-21 00:05:37 -0400 | [diff] [blame] | 119 | offset = find_pack_entry_one(sha1, p); |
| 120 | if (!offset) |
Junio C Hamano | ad8c80a | 2005-06-30 17:17:20 -0700 | [diff] [blame] | 121 | die("internal error pack-check find-pack-entry-one"); |
| 122 | |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 123 | type = packed_object_info_detail(p, offset, &size, &store_size, |
| 124 | &delta_chain_length, |
| 125 | base_sha1); |
Junio C Hamano | ad8c80a | 2005-06-30 17:17:20 -0700 | [diff] [blame] | 126 | printf("%s ", sha1_to_hex(sha1)); |
| 127 | if (!delta_chain_length) |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 128 | printf("%-6s %lu %"PRIuMAX"\n", |
| 129 | type, size, (uintmax_t)offset); |
Junio C Hamano | 473ab16 | 2006-03-05 11:20:12 -0800 | [diff] [blame] | 130 | else { |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 131 | printf("%-6s %lu %"PRIuMAX" %u %s\n", |
| 132 | type, size, (uintmax_t)offset, |
Junio C Hamano | ad8c80a | 2005-06-30 17:17:20 -0700 | [diff] [blame] | 133 | delta_chain_length, sha1_to_hex(base_sha1)); |
Nicolas Pitre | ddcf786 | 2007-05-25 23:37:40 -0400 | [diff] [blame] | 134 | if (delta_chain_length <= MAX_CHAIN) |
Junio C Hamano | 473ab16 | 2006-03-05 11:20:12 -0800 | [diff] [blame] | 135 | chain_histogram[delta_chain_length]++; |
| 136 | else |
| 137 | chain_histogram[0]++; |
| 138 | } |
Junio C Hamano | ad8c80a | 2005-06-30 17:17:20 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Nicolas Pitre | ddcf786 | 2007-05-25 23:37:40 -0400 | [diff] [blame] | 141 | for (i = 0; i <= MAX_CHAIN; i++) { |
Junio C Hamano | 473ab16 | 2006-03-05 11:20:12 -0800 | [diff] [blame] | 142 | if (!chain_histogram[i]) |
| 143 | continue; |
Nicolas Pitre | ddcf786 | 2007-05-25 23:37:40 -0400 | [diff] [blame] | 144 | printf("chain length = %d: %d object%s\n", i, |
| 145 | chain_histogram[i], chain_histogram[i] > 1 ? "s" : ""); |
Junio C Hamano | 473ab16 | 2006-03-05 11:20:12 -0800 | [diff] [blame] | 146 | } |
Nicolas Pitre | ddcf786 | 2007-05-25 23:37:40 -0400 | [diff] [blame] | 147 | if (chain_histogram[0]) |
| 148 | printf("chain length > %d: %d object%s\n", MAX_CHAIN, |
| 149 | chain_histogram[0], chain_histogram[0] > 1 ? "s" : ""); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int verify_pack(struct packed_git *p, int verbose) |
| 153 | { |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 154 | off_t index_size; |
| 155 | const unsigned char *index_base; |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 156 | SHA_CTX ctx; |
| 157 | unsigned char sha1[20]; |
| 158 | int ret; |
| 159 | |
Shawn O. Pearce | d079837 | 2007-05-26 01:24:19 -0400 | [diff] [blame] | 160 | if (open_pack_index(p)) |
| 161 | return error("packfile %s index not opened", p->pack_name); |
| 162 | index_size = p->index_size; |
| 163 | index_base = p->index_data; |
| 164 | |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 165 | ret = 0; |
| 166 | /* Verify SHA1 sum of the index file */ |
| 167 | SHA1_Init(&ctx); |
Shawn O. Pearce | c4001d9 | 2007-03-06 20:44:30 -0500 | [diff] [blame] | 168 | SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20)); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 169 | SHA1_Final(sha1, &ctx); |
Nicolas Pitre | 4287307 | 2007-03-16 16:42:50 -0400 | [diff] [blame] | 170 | if (hashcmp(sha1, index_base + index_size - 20)) |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 171 | ret = error("Packfile index for %s SHA1 mismatch", |
| 172 | p->pack_name); |
| 173 | |
| 174 | if (!ret) { |
| 175 | /* Verify pack file */ |
Shawn O. Pearce | 03e79c8 | 2006-12-23 02:34:08 -0500 | [diff] [blame] | 176 | struct pack_window *w_curs = NULL; |
| 177 | ret = verify_packfile(p, &w_curs); |
| 178 | unuse_pack(&w_curs); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | if (verbose) { |
| 182 | if (ret) |
| 183 | printf("%s: bad\n", p->pack_name); |
| 184 | else { |
Shawn O. Pearce | 079afb1 | 2006-12-23 02:34:13 -0500 | [diff] [blame] | 185 | show_pack_info(p); |
Junio C Hamano | f3bf922 | 2005-06-30 17:15:39 -0700 | [diff] [blame] | 186 | printf("%s: ok\n", p->pack_name); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return ret; |
| 191 | } |