3 #include "pack-revindex.h"
8 const unsigned char *sha1
;
12 static int compare_entries(const void *e1
, const void *e2
)
14 const struct idx_entry
*entry1
= e1
;
15 const struct idx_entry
*entry2
= e2
;
16 if (entry1
->offset
< entry2
->offset
)
18 if (entry1
->offset
> entry2
->offset
)
23 int check_pack_crc(struct packed_git
*p
, struct pack_window
**w_curs
,
24 off_t offset
, off_t len
, unsigned int nr
)
26 const uint32_t *index_crc
;
27 uint32_t data_crc
= crc32(0, NULL
, 0);
31 void *data
= use_pack(p
, w_curs
, offset
, &avail
);
34 data_crc
= crc32(data_crc
, data
, avail
);
39 index_crc
= p
->index_data
;
40 index_crc
+= 2 + 256 + p
->num_objects
* (20/4) + nr
;
42 return data_crc
!= ntohl(*index_crc
);
45 static int verify_packfile(struct packed_git
*p
,
46 struct pack_window
**w_curs
,
48 struct progress
*progress
, uint32_t base_count
)
51 off_t index_size
= p
->index_size
;
52 const unsigned char *index_base
= p
->index_data
;
54 unsigned char sha1
[20], *pack_sig
;
55 off_t offset
= 0, pack_sig_ofs
= 0;
56 uint32_t nr_objects
, i
;
58 struct idx_entry
*entries
;
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
68 unsigned long remaining
;
69 unsigned char *in
= use_pack(p
, w_curs
, offset
, &remaining
);
72 pack_sig_ofs
= p
->pack_size
- 20;
73 if (offset
> pack_sig_ofs
)
74 remaining
-= (unsigned int)(offset
- pack_sig_ofs
);
75 git_SHA1_Update(&ctx
, in
, remaining
);
76 } while (offset
< pack_sig_ofs
);
77 git_SHA1_Final(sha1
, &ctx
);
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",
82 if (hashcmp(index_base
+ index_size
- 40, pack_sig
))
83 err
= error("%s SHA1 does not match its index",
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.
91 nr_objects
= p
->num_objects
;
92 entries
= xmalloc((nr_objects
+ 1) * sizeof(*entries
));
93 entries
[nr_objects
].offset
= pack_sig_ofs
;
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
);
98 die("internal error pack-check nth-packed-object");
99 entries
[i
].offset
= nth_packed_object_offset(p
, i
);
102 qsort(entries
, nr_objects
, sizeof(*entries
), compare_entries
);
104 for (i
= 0; i
< nr_objects
; i
++) {
106 enum object_type type
;
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
);
119 data
= unpack_entry(p
, entries
[i
].offset
, &type
, &size
);
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
);
124 else if (check_sha1_signature(entries
[i
].sha1
, data
, size
, typename(type
)))
125 err
= error("packed %s from %s is corrupt",
126 sha1_to_hex(entries
[i
].sha1
), p
->pack_name
);
129 fn(entries
[i
].sha1
, type
, size
, data
, &eaten
);
133 if (((base_count
+ i
) & 1023) == 0)
134 display_progress(progress
, base_count
+ i
);
138 display_progress(progress
, base_count
+ i
);
144 int verify_pack_index(struct packed_git
*p
)
147 const unsigned char *index_base
;
149 unsigned char sha1
[20];
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
;
157 /* Verify SHA1 sum of the index file */
159 git_SHA1_Update(&ctx
, index_base
, (unsigned int)(index_size
- 20));
160 git_SHA1_Final(sha1
, &ctx
);
161 if (hashcmp(sha1
, index_base
+ index_size
- 20))
162 err
= error("Packfile index for %s SHA1 mismatch",
167 int verify_pack(struct packed_git
*p
, verify_fn fn
,
168 struct progress
*progress
, uint32_t base_count
)
171 struct pack_window
*w_curs
= NULL
;
173 err
|= verify_pack_index(p
);
177 err
|= verify_packfile(p
, &w_curs
, fn
, progress
, base_count
);