Skip to content

Commit 4828ce9

Browse files
peffgitster
authored andcommitted
pack-revindex: open index if necessary
We can't create a pack revindex if we haven't actually looked at the index. Normally we would never get as far as creating a revindex without having already been looking in the pack, so this code never bothered to double-check that pack->index_data had been loaded. But with the new multi-pack-index feature, many code paths might not load the individual pack .idx at all (they'd find objects via the midx and then open the .pack, but not its index). This can't yet be triggered in practice, because a bug in the midx code means we accidentally open up the individual .idx files anyway. But in preparation for fixing that, let's have the revindex code check that everything it needs has been loaded. In most cases this will just be a quick noop. But note that this does introduce a possibility of error (if we have to open the index and it's corrupt), so load_pack_revindex() now returns a result code, and callers need to handle the error. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 336226c commit 4828ce9

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

pack-bitmap.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ static int load_pack_bitmap(struct bitmap_index *bitmap_git)
308308

309309
bitmap_git->bitmaps = kh_init_sha1();
310310
bitmap_git->ext_index.positions = kh_init_sha1_pos();
311-
load_pack_revindex(bitmap_git->pack);
311+
if (load_pack_revindex(bitmap_git->pack))
312+
goto failed;
312313

313314
if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
314315
!(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||

pack-revindex.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cache.h"
22
#include "pack-revindex.h"
33
#include "object-store.h"
4+
#include "packfile.h"
45

56
/*
67
* Pack index for existing packs give us easy access to the offsets into
@@ -158,10 +159,14 @@ static void create_pack_revindex(struct packed_git *p)
158159
sort_revindex(p->revindex, num_ent, p->pack_size);
159160
}
160161

161-
void load_pack_revindex(struct packed_git *p)
162+
int load_pack_revindex(struct packed_git *p)
162163
{
163-
if (!p->revindex)
164+
if (!p->revindex) {
165+
if (open_pack_index(p))
166+
return -1;
164167
create_pack_revindex(p);
168+
}
169+
return 0;
165170
}
166171

167172
int find_revindex_position(struct packed_git *p, off_t ofs)
@@ -188,7 +193,9 @@ struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)
188193
{
189194
int pos;
190195

191-
load_pack_revindex(p);
196+
if (load_pack_revindex(p))
197+
return NULL;
198+
192199
pos = find_revindex_position(p, ofs);
193200

194201
if (pos < 0)

pack-revindex.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct revindex_entry {
88
unsigned int nr;
99
};
1010

11-
void load_pack_revindex(struct packed_git *p);
11+
int load_pack_revindex(struct packed_git *p);
1212
int find_revindex_position(struct packed_git *p, off_t ofs);
1313

1414
struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs);

packfile.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -2023,8 +2023,10 @@ int for_each_object_in_pack(struct packed_git *p,
20232023
uint32_t i;
20242024
int r = 0;
20252025

2026-
if (flags & FOR_EACH_OBJECT_PACK_ORDER)
2027-
load_pack_revindex(p);
2026+
if (flags & FOR_EACH_OBJECT_PACK_ORDER) {
2027+
if (load_pack_revindex(p))
2028+
return -1;
2029+
}
20282030

20292031
for (i = 0; i < p->num_objects; i++) {
20302032
uint32_t pos;

0 commit comments

Comments
 (0)