3 #include "pack-revindex.h"
7 const unsigned char *sha1
;
11 static int compare_entries(const void *e1
, const void *e2
)
13 const struct idx_entry
*entry1
= e1
;
14 const struct idx_entry
*entry2
= e2
;
15 if (entry1
->offset
< entry2
->offset
)
17 if (entry1
->offset
> entry2
->offset
)
22 static int verify_packfile(struct packed_git
*p
,
23 struct pack_window
**w_curs
)
25 off_t index_size
= p
->index_size
;
26 const unsigned char *index_base
= p
->index_data
;
28 unsigned char sha1
[20], *pack_sig
;
29 off_t offset
= 0, pack_sig_ofs
= p
->pack_size
- 20;
30 uint32_t nr_objects
, i
;
32 struct idx_entry
*entries
;
34 /* Note that the pack header checks are actually performed by
35 * use_pack when it first opens the pack file. If anything
36 * goes wrong during those checks then the call will die out
41 while (offset
< pack_sig_ofs
) {
42 unsigned int remaining
;
43 unsigned char *in
= use_pack(p
, w_curs
, offset
, &remaining
);
45 if (offset
> pack_sig_ofs
)
46 remaining
-= (unsigned int)(offset
- pack_sig_ofs
);
47 SHA1_Update(&ctx
, in
, remaining
);
49 SHA1_Final(sha1
, &ctx
);
50 pack_sig
= use_pack(p
, w_curs
, pack_sig_ofs
, NULL
);
51 if (hashcmp(sha1
, pack_sig
))
52 err
= error("%s SHA1 checksum mismatch",
54 if (hashcmp(index_base
+ index_size
- 40, pack_sig
))
55 err
= error("%s SHA1 does not match its inddex",
59 /* Make sure everything reachable from idx is valid. Since we
60 * have verified that nr_objects matches between idx and pack,
61 * we do not do scan-streaming check on the pack file.
63 nr_objects
= p
->num_objects
;
64 entries
= xmalloc(nr_objects
* sizeof(*entries
));
65 /* first sort entries by pack offset, since unpacking them is more efficient that way */
66 for (i
= 0; i
< nr_objects
; i
++) {
67 entries
[i
].sha1
= nth_packed_object_sha1(p
, i
);
69 die("internal error pack-check nth-packed-object");
70 entries
[i
].offset
= nth_packed_object_offset(p
, i
);
72 qsort(entries
, nr_objects
, sizeof(*entries
), compare_entries
);
74 for (i
= 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 at offset %"PRIuMAX
"",
82 sha1_to_hex(entries
[i
].sha1
), p
->pack_name
,
83 (uintmax_t)entries
[i
].offset
);
86 if (check_sha1_signature(entries
[i
].sha1
, data
, size
, typename(type
))) {
87 err
= error("packed %s from %s is corrupt",
88 sha1_to_hex(entries
[i
].sha1
), p
->pack_name
);
99 int verify_pack(struct packed_git
*p
)
102 const unsigned char *index_base
;
104 unsigned char sha1
[20];
106 struct pack_window
*w_curs
= NULL
;
108 if (open_pack_index(p
))
109 return error("packfile %s index not opened", p
->pack_name
);
110 index_size
= p
->index_size
;
111 index_base
= p
->index_data
;
113 /* Verify SHA1 sum of the index file */
115 SHA1_Update(&ctx
, index_base
, (unsigned int)(index_size
- 20));
116 SHA1_Final(sha1
, &ctx
);
117 if (hashcmp(sha1
, index_base
+ index_size
- 20))
118 err
= error("Packfile index for %s SHA1 mismatch",
121 /* Verify pack file */
122 err
|= verify_packfile(p
, &w_curs
);