summaryrefslogtreecommitdiff
path: root/pack-revindex.c
diff options
context:
space:
mode:
Diffstat (limited to 'pack-revindex.c')
-rw-r--r--pack-revindex.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/pack-revindex.c b/pack-revindex.c
index 3c58784a5f..d28a7e43d0 100644
--- a/pack-revindex.c
+++ b/pack-revindex.c
@@ -1,6 +1,7 @@
#include "cache.h"
#include "pack-revindex.h"
#include "object-store.h"
+#include "packfile.h"
/*
* Pack index for existing packs give us easy access to the offsets into
@@ -119,7 +120,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
*/
static void create_pack_revindex(struct packed_git *p)
{
- unsigned num_ent = p->num_objects;
+ const unsigned num_ent = p->num_objects;
unsigned i;
const char *index = p->index_data;
const unsigned hashsz = the_hash_algo->rawsz;
@@ -132,7 +133,7 @@ static void create_pack_revindex(struct packed_git *p)
(uint32_t *)(index + 8 + p->num_objects * (hashsz + 4));
const uint32_t *off_64 = off_32 + p->num_objects;
for (i = 0; i < num_ent; i++) {
- uint32_t off = ntohl(*off_32++);
+ const uint32_t off = ntohl(*off_32++);
if (!(off & 0x80000000)) {
p->revindex[i].offset = off;
} else {
@@ -143,7 +144,7 @@ static void create_pack_revindex(struct packed_git *p)
}
} else {
for (i = 0; i < num_ent; i++) {
- uint32_t hl = *((uint32_t *)(index + (hashsz + 4) * i));
+ const uint32_t hl = *((uint32_t *)(index + (hashsz + 4) * i));
p->revindex[i].offset = ntohl(hl);
p->revindex[i].nr = i;
}
@@ -158,20 +159,24 @@ static void create_pack_revindex(struct packed_git *p)
sort_revindex(p->revindex, num_ent, p->pack_size);
}
-void load_pack_revindex(struct packed_git *p)
+int load_pack_revindex(struct packed_git *p)
{
- if (!p->revindex)
+ if (!p->revindex) {
+ if (open_pack_index(p))
+ return -1;
create_pack_revindex(p);
+ }
+ return 0;
}
int find_revindex_position(struct packed_git *p, off_t ofs)
{
int lo = 0;
int hi = p->num_objects + 1;
- struct revindex_entry *revindex = p->revindex;
+ const struct revindex_entry *revindex = p->revindex;
do {
- unsigned mi = lo + (hi - lo) / 2;
+ const unsigned mi = lo + (hi - lo) / 2;
if (revindex[mi].offset == ofs) {
return mi;
} else if (ofs < revindex[mi].offset)
@@ -188,7 +193,9 @@ struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
{
int pos;
- load_pack_revindex(p);
+ if (load_pack_revindex(p))
+ return NULL;
+
pos = find_revindex_position(p, ofs);
if (pos < 0)