3 #include "pack-revindex.h"
8 union idx_entry_object
{
9 const unsigned char *hash
;
10 struct object_id
*oid
;
15 static int compare_entries(const void *e1
, const void *e2
)
17 const struct idx_entry
*entry1
= e1
;
18 const struct idx_entry
*entry2
= e2
;
19 if (entry1
->offset
< entry2
->offset
)
21 if (entry1
->offset
> entry2
->offset
)
26 int check_pack_crc(struct packed_git
*p
, struct pack_window
**w_curs
,
27 off_t offset
, off_t len
, unsigned int nr
)
29 const uint32_t *index_crc
;
30 uint32_t data_crc
= crc32(0, NULL
, 0);
34 void *data
= use_pack(p
, w_curs
, offset
, &avail
);
37 data_crc
= crc32(data_crc
, data
, avail
);
42 index_crc
= p
->index_data
;
43 index_crc
+= 2 + 256 + p
->num_objects
* (20/4) + nr
;
45 return data_crc
!= ntohl(*index_crc
);
48 static int verify_packfile(struct packed_git
*p
,
49 struct pack_window
**w_curs
,
51 struct progress
*progress
, uint32_t base_count
)
54 off_t index_size
= p
->index_size
;
55 const unsigned char *index_base
= p
->index_data
;
57 unsigned char hash
[GIT_MAX_RAWSZ
], *pack_sig
;
58 off_t offset
= 0, pack_sig_ofs
= 0;
59 uint32_t nr_objects
, i
;
61 struct idx_entry
*entries
;
63 if (!is_pack_valid(p
))
64 return error("packfile %s cannot be accessed", p
->pack_name
);
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(hash
, &ctx
);
78 pack_sig
= use_pack(p
, w_curs
, pack_sig_ofs
, NULL
);
79 if (hashcmp(hash
, 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 ALLOC_ARRAY(entries
, nr_objects
+ 1);
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
].oid
.hash
= nth_packed_object_sha1(p
, i
);
97 if (!entries
[i
].oid
.hash
)
98 die("internal error pack-check nth-packed-object");
99 entries
[i
].offset
= nth_packed_object_offset(p
, i
);
102 QSORT(entries
, nr_objects
, compare_entries
);
104 for (i
= 0; i
< nr_objects
; i
++) {
106 enum object_type type
;
111 if (p
->index_version
> 1) {
112 off_t offset
= entries
[i
].offset
;
113 off_t len
= entries
[i
+1].offset
- offset
;
114 unsigned int nr
= entries
[i
].nr
;
115 if (check_pack_crc(p
, w_curs
, offset
, len
, nr
))
116 err
= error("index CRC mismatch for object %s "
117 "from %s at offset %"PRIuMAX
"",
118 oid_to_hex(entries
[i
].oid
.oid
),
119 p
->pack_name
, (uintmax_t)offset
);
122 curpos
= entries
[i
].offset
;
123 type
= unpack_object_header(p
, w_curs
, &curpos
, &size
);
126 if (type
== OBJ_BLOB
&& big_file_threshold
<= size
) {
128 * Let check_sha1_signature() check it with
129 * the streaming interface; no point slurping
130 * the data in-core only to discard.
135 data
= unpack_entry(p
, entries
[i
].offset
, &type
, &size
);
139 if (data_valid
&& !data
)
140 err
= error("cannot unpack %s from %s at offset %"PRIuMAX
"",
141 oid_to_hex(entries
[i
].oid
.oid
), p
->pack_name
,
142 (uintmax_t)entries
[i
].offset
);
143 else if (check_sha1_signature(entries
[i
].oid
.hash
, data
, size
, typename(type
)))
144 err
= error("packed %s from %s is corrupt",
145 oid_to_hex(entries
[i
].oid
.oid
), p
->pack_name
);
148 err
|= fn(entries
[i
].oid
.oid
, type
, size
, data
, &eaten
);
152 if (((base_count
+ i
) & 1023) == 0)
153 display_progress(progress
, base_count
+ i
);
157 display_progress(progress
, base_count
+ i
);
163 int verify_pack_index(struct packed_git
*p
)
166 const unsigned char *index_base
;
168 unsigned char sha1
[20];
171 if (open_pack_index(p
))
172 return error("packfile %s index not opened", p
->pack_name
);
173 index_size
= p
->index_size
;
174 index_base
= p
->index_data
;
176 /* Verify SHA1 sum of the index file */
178 git_SHA1_Update(&ctx
, index_base
, (unsigned int)(index_size
- 20));
179 git_SHA1_Final(sha1
, &ctx
);
180 if (hashcmp(sha1
, index_base
+ index_size
- 20))
181 err
= error("Packfile index for %s SHA1 mismatch",
186 int verify_pack(struct packed_git
*p
, verify_fn fn
,
187 struct progress
*progress
, uint32_t base_count
)
190 struct pack_window
*w_curs
= NULL
;
192 err
|= verify_pack_index(p
);
196 err
|= verify_packfile(p
, &w_curs
, fn
, progress
, base_count
);