]>
Commit | Line | Data |
---|---|---|
f9253394 JH |
1 | #include "cache.h" |
2 | #include "pack.h" | |
3 | ||
03e79c88 SP |
4 | static int verify_packfile(struct packed_git *p, |
5 | struct pack_window **w_curs) | |
f9253394 | 6 | { |
c4001d92 | 7 | off_t index_size = p->index_size; |
42873078 | 8 | const unsigned char *index_base = p->index_data; |
f9253394 JH |
9 | SHA_CTX ctx; |
10 | unsigned char sha1[20]; | |
c4001d92 | 11 | off_t offset = 0, pack_sig = p->pack_size - 20; |
326bf396 SP |
12 | uint32_t nr_objects, i; |
13 | int err; | |
f9253394 | 14 | |
079afb18 SP |
15 | /* Note that the pack header checks are actually performed by |
16 | * use_pack when it first opens the pack file. If anything | |
17 | * goes wrong during those checks then the call will die out | |
18 | * immediately. | |
19 | */ | |
f9253394 JH |
20 | |
21 | SHA1_Init(&ctx); | |
079afb18 SP |
22 | while (offset < pack_sig) { |
23 | unsigned int remaining; | |
24 | unsigned char *in = use_pack(p, w_curs, offset, &remaining); | |
25 | offset += remaining; | |
26 | if (offset > pack_sig) | |
c4001d92 | 27 | remaining -= (unsigned int)(offset - pack_sig); |
079afb18 SP |
28 | SHA1_Update(&ctx, in, remaining); |
29 | } | |
f9253394 | 30 | SHA1_Final(sha1, &ctx); |
079afb18 | 31 | if (hashcmp(sha1, use_pack(p, w_curs, pack_sig, NULL))) |
f9253394 JH |
32 | return error("Packfile %s SHA1 mismatch with itself", |
33 | p->pack_name); | |
42873078 | 34 | if (hashcmp(sha1, index_base + index_size - 40)) |
55e1805d JH |
35 | return error("Packfile %s SHA1 mismatch with idx", |
36 | p->pack_name); | |
079afb18 | 37 | unuse_pack(w_curs); |
f3bf9224 JH |
38 | |
39 | /* Make sure everything reachable from idx is valid. Since we | |
40 | * have verified that nr_objects matches between idx and pack, | |
41 | * we do not do scan-streaming check on the pack file. | |
42 | */ | |
079afb18 | 43 | nr_objects = num_packed_objects(p); |
326bf396 | 44 | for (i = 0, err = 0; i < nr_objects; i++) { |
f3bf9224 | 45 | unsigned char sha1[20]; |
f3bf9224 | 46 | void *data; |
21666f1a | 47 | enum object_type type; |
c4001d92 SP |
48 | unsigned long size; |
49 | off_t offset; | |
f3bf9224 JH |
50 | |
51 | if (nth_packed_object_sha1(p, i, sha1)) | |
52 | die("internal error pack-check nth-packed-object"); | |
43057304 NP |
53 | offset = find_pack_entry_one(sha1, p); |
54 | if (!offset) | |
f3bf9224 | 55 | die("internal error pack-check find-pack-entry-one"); |
21666f1a | 56 | data = unpack_entry(p, offset, &type, &size); |
f3bf9224 JH |
57 | if (!data) { |
58 | err = error("cannot unpack %s from %s", | |
59 | sha1_to_hex(sha1), p->pack_name); | |
60 | continue; | |
61 | } | |
21666f1a | 62 | if (check_sha1_signature(sha1, data, size, typename(type))) { |
1038f0c0 | 63 | err = error("packed %s from %s is corrupt", |
f3bf9224 JH |
64 | sha1_to_hex(sha1), p->pack_name); |
65 | free(data); | |
66 | continue; | |
67 | } | |
68 | free(data); | |
69 | } | |
70 | ||
71 | return err; | |
f9253394 JH |
72 | } |
73 | ||
74 | ||
473ab165 JH |
75 | #define MAX_CHAIN 40 |
76 | ||
079afb18 | 77 | static void show_pack_info(struct packed_git *p) |
f3bf9224 | 78 | { |
326bf396 | 79 | uint32_t nr_objects, i, chain_histogram[MAX_CHAIN]; |
ad8c80a5 | 80 | |
079afb18 | 81 | nr_objects = num_packed_objects(p); |
473ab165 | 82 | memset(chain_histogram, 0, sizeof(chain_histogram)); |
ad8c80a5 JH |
83 | |
84 | for (i = 0; i < nr_objects; i++) { | |
85 | unsigned char sha1[20], base_sha1[20]; | |
21666f1a | 86 | const char *type; |
ad8c80a5 JH |
87 | unsigned long size; |
88 | unsigned long store_size; | |
c4001d92 | 89 | off_t offset; |
f8f135c9 | 90 | unsigned int delta_chain_length; |
ad8c80a5 JH |
91 | |
92 | if (nth_packed_object_sha1(p, i, sha1)) | |
93 | die("internal error pack-check nth-packed-object"); | |
43057304 NP |
94 | offset = find_pack_entry_one(sha1, p); |
95 | if (!offset) | |
ad8c80a5 JH |
96 | die("internal error pack-check find-pack-entry-one"); |
97 | ||
21666f1a NP |
98 | type = packed_object_info_detail(p, offset, &size, &store_size, |
99 | &delta_chain_length, | |
100 | base_sha1); | |
ad8c80a5 JH |
101 | printf("%s ", sha1_to_hex(sha1)); |
102 | if (!delta_chain_length) | |
c4001d92 SP |
103 | printf("%-6s %lu %"PRIuMAX"\n", |
104 | type, size, (uintmax_t)offset); | |
473ab165 | 105 | else { |
c4001d92 SP |
106 | printf("%-6s %lu %"PRIuMAX" %u %s\n", |
107 | type, size, (uintmax_t)offset, | |
ad8c80a5 | 108 | delta_chain_length, sha1_to_hex(base_sha1)); |
473ab165 JH |
109 | if (delta_chain_length < MAX_CHAIN) |
110 | chain_histogram[delta_chain_length]++; | |
111 | else | |
112 | chain_histogram[0]++; | |
113 | } | |
ad8c80a5 JH |
114 | } |
115 | ||
473ab165 JH |
116 | for (i = 0; i < MAX_CHAIN; i++) { |
117 | if (!chain_histogram[i]) | |
118 | continue; | |
119 | printf("chain length %s %d: %d object%s\n", | |
120 | i ? "=" : ">=", | |
121 | i ? i : MAX_CHAIN, | |
122 | chain_histogram[i], | |
123 | 1 < chain_histogram[i] ? "s" : ""); | |
124 | } | |
f3bf9224 JH |
125 | } |
126 | ||
127 | int verify_pack(struct packed_git *p, int verbose) | |
f9253394 | 128 | { |
c4001d92 | 129 | off_t index_size = p->index_size; |
42873078 | 130 | const unsigned char *index_base = p->index_data; |
f9253394 JH |
131 | SHA_CTX ctx; |
132 | unsigned char sha1[20]; | |
133 | int ret; | |
134 | ||
f3bf9224 | 135 | ret = 0; |
f9253394 JH |
136 | /* Verify SHA1 sum of the index file */ |
137 | SHA1_Init(&ctx); | |
c4001d92 | 138 | SHA1_Update(&ctx, index_base, (unsigned int)(index_size - 20)); |
f9253394 | 139 | SHA1_Final(sha1, &ctx); |
42873078 | 140 | if (hashcmp(sha1, index_base + index_size - 20)) |
f3bf9224 JH |
141 | ret = error("Packfile index for %s SHA1 mismatch", |
142 | p->pack_name); | |
143 | ||
144 | if (!ret) { | |
145 | /* Verify pack file */ | |
03e79c88 SP |
146 | struct pack_window *w_curs = NULL; |
147 | ret = verify_packfile(p, &w_curs); | |
148 | unuse_pack(&w_curs); | |
f3bf9224 JH |
149 | } |
150 | ||
151 | if (verbose) { | |
152 | if (ret) | |
153 | printf("%s: bad\n", p->pack_name); | |
154 | else { | |
079afb18 | 155 | show_pack_info(p); |
f3bf9224 JH |
156 | printf("%s: ok\n", p->pack_name); |
157 | } | |
158 | } | |
f9253394 | 159 | |
f9253394 JH |
160 | return ret; |
161 | } |