Skip to content

Commit 71e66ec

Browse files
committed
Merge branch 'aj/pack'
* aj/pack: pack-check: Sort entries by pack offset before unpacking them.
2 parents 89dd19e + 3af5192 commit 71e66ec

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

pack-check.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
#include "cache.h"
22
#include "pack.h"
33

4+
struct idx_entry
5+
{
6+
const unsigned char *sha1;
7+
off_t offset;
8+
};
9+
10+
static int compare_entries(const void *e1, const void *e2)
11+
{
12+
const struct idx_entry *entry1 = e1;
13+
const struct idx_entry *entry2 = e2;
14+
if (entry1->offset < entry2->offset)
15+
return -1;
16+
if (entry1->offset > entry2->offset)
17+
return 1;
18+
return 0;
19+
}
20+
421
static int verify_packfile(struct packed_git *p,
522
struct pack_window **w_curs)
623
{
@@ -11,6 +28,7 @@ static int verify_packfile(struct packed_git *p,
1128
off_t offset = 0, pack_sig = p->pack_size - 20;
1229
uint32_t nr_objects, i;
1330
int err;
31+
struct idx_entry *entries;
1432

1533
/* Note that the pack header checks are actually performed by
1634
* use_pack when it first opens the pack file. If anything
@@ -41,33 +59,38 @@ static int verify_packfile(struct packed_git *p,
4159
* we do not do scan-streaming check on the pack file.
4260
*/
4361
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);
66+
if (!entries[i].sha1)
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");
71+
}
72+
qsort(entries, nr_objects, sizeof(*entries), compare_entries);
73+
4474
for (i = 0, err = 0; i < nr_objects; i++) {
45-
const unsigned char *sha1;
4675
void *data;
4776
enum object_type type;
4877
unsigned long size;
49-
off_t offset;
5078

51-
sha1 = nth_packed_object_sha1(p, i);
52-
if (!sha1)
53-
die("internal error pack-check nth-packed-object");
54-
offset = find_pack_entry_one(sha1, p);
55-
if (!offset)
56-
die("internal error pack-check find-pack-entry-one");
57-
data = unpack_entry(p, offset, &type, &size);
79+
data = unpack_entry(p, entries[i].offset, &type, &size);
5880
if (!data) {
5981
err = error("cannot unpack %s from %s",
60-
sha1_to_hex(sha1), p->pack_name);
82+
sha1_to_hex(entries[i].sha1), p->pack_name);
6183
continue;
6284
}
63-
if (check_sha1_signature(sha1, data, size, typename(type))) {
85+
if (check_sha1_signature(entries[i].sha1, data, size, typename(type))) {
6486
err = error("packed %s from %s is corrupt",
65-
sha1_to_hex(sha1), p->pack_name);
87+
sha1_to_hex(entries[i].sha1), p->pack_name);
6688
free(data);
6789
continue;
6890
}
6991
free(data);
7092
}
93+
free(entries);
7194

7295
return err;
7396
}

0 commit comments

Comments
 (0)