6 const unsigned char *sha1
;
10 static int compare_entries(const void *e1
, const void *e2
)
12 const struct idx_entry
*entry1
= e1
;
13 const struct idx_entry
*entry2
= e2
;
14 if (entry1
->offset
< entry2
->offset
)
16 if (entry1
->offset
> entry2
->offset
)
21 static int verify_packfile(struct packed_git
*p
,
22 struct pack_window
**w_curs
)
24 off_t index_size
= p
->index_size
;
25 const unsigned char *index_base
= p
->index_data
;
27 unsigned char sha1
[20];
28 off_t offset
= 0, pack_sig
= p
->pack_size
- 20;
29 uint32_t nr_objects
, i
;
31 struct idx_entry
*entries
;
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
40 while (offset
< pack_sig
) {
41 unsigned int remaining
;
42 unsigned char *in
= use_pack(p
, w_curs
, offset
, &remaining
);
44 if (offset
> pack_sig
)
45 remaining
-= (unsigned int)(offset
- pack_sig
);
46 SHA1_Update(&ctx
, in
, remaining
);
48 SHA1_Final(sha1
, &ctx
);
49 if (hashcmp(sha1
, use_pack(p
, w_curs
, pack_sig
, NULL
)))
50 return error("Packfile %s SHA1 mismatch with itself",
52 if (hashcmp(sha1
, index_base
+ index_size
- 40))
53 return error("Packfile %s SHA1 mismatch with idx",
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.
61 nr_objects
= p
->num_objects
;
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
);
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");
72 qsort(entries
, nr_objects
, sizeof(*entries
), compare_entries
);
74 for (i
= 0, err
= 0; i
< nr_objects
; i
++) {
76 enum object_type type
;
79 data
= unpack_entry(p
, entries
[i
].offset
, &type
, &size
);
81 err
= error("cannot unpack %s from %s",
82 sha1_to_hex(entries
[i
].sha1
), p
->pack_name
);
85 if (check_sha1_signature(entries
[i
].sha1
, data
, size
, typename(type
))) {
86 err
= error("packed %s from %s is corrupt",
87 sha1_to_hex(entries
[i
].sha1
), p
->pack_name
);
101 static void show_pack_info(struct packed_git
*p
)
103 uint32_t nr_objects
, i
, chain_histogram
[MAX_CHAIN
+1];
104 nr_objects
= p
->num_objects
;
105 memset(chain_histogram
, 0, sizeof(chain_histogram
));
107 for (i
= 0; i
< nr_objects
; i
++) {
108 const unsigned char *sha1
;
109 unsigned char base_sha1
[20];
112 unsigned long store_size
;
114 unsigned int delta_chain_length
;
116 sha1
= nth_packed_object_sha1(p
, i
);
118 die("internal error pack-check nth-packed-object");
119 offset
= find_pack_entry_one(sha1
, p
);
121 die("internal error pack-check find-pack-entry-one");
123 type
= packed_object_info_detail(p
, offset
, &size
, &store_size
,
126 printf("%s ", sha1_to_hex(sha1
));
127 if (!delta_chain_length
)
128 printf("%-6s %lu %"PRIuMAX
"\n",
129 type
, size
, (uintmax_t)offset
);
131 printf("%-6s %lu %"PRIuMAX
" %u %s\n",
132 type
, size
, (uintmax_t)offset
,
133 delta_chain_length
, sha1_to_hex(base_sha1
));
134 if (delta_chain_length
<= MAX_CHAIN
)
135 chain_histogram
[delta_chain_length
]++;
137 chain_histogram
[0]++;
141 for (i
= 0; i
<= MAX_CHAIN
; i
++) {
142 if (!chain_histogram
[i
])
144 printf("chain length = %d: %d object%s\n", i
,
145 chain_histogram
[i
], chain_histogram
[i
] > 1 ? "s" : "");
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" : "");
152 int verify_pack(struct packed_git
*p
, int verbose
)
155 const unsigned char *index_base
;
157 unsigned char sha1
[20];
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
;
166 /* Verify SHA1 sum of the index file */
168 SHA1_Update(&ctx
, index_base
, (unsigned int)(index_size
- 20));
169 SHA1_Final(sha1
, &ctx
);
170 if (hashcmp(sha1
, index_base
+ index_size
- 20))
171 ret
= error("Packfile index for %s SHA1 mismatch",
175 /* Verify pack file */
176 struct pack_window
*w_curs
= NULL
;
177 ret
= verify_packfile(p
, &w_curs
);
183 printf("%s: bad\n", p
->pack_name
);
186 printf("%s: ok\n", p
->pack_name
);