pack-revindex: open index if necessary
authorJeff King <peff@peff.net>
Fri, 5 Apr 2019 18:04:24 +0000 (14:04 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 16 Apr 2019 07:58:21 +0000 (16:58 +0900)
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pack-bitmap.c
pack-revindex.c
pack-revindex.h
packfile.c
index 4695aaf6b4eda41ec2479a326ddbe7a0bed065ab..3960ad94c898b8c96dfd8648fde5386c8555d69c 100644 (file)
@@ -308,7 +308,8 @@ static int load_pack_bitmap(struct bitmap_index *bitmap_git)
 
        bitmap_git->bitmaps = kh_init_sha1();
        bitmap_git->ext_index.positions = kh_init_sha1_pos();
-       load_pack_revindex(bitmap_git->pack);
+       if (load_pack_revindex(bitmap_git->pack))
+               goto failed;
 
        if (!(bitmap_git->commits = read_bitmap_1(bitmap_git)) ||
                !(bitmap_git->trees = read_bitmap_1(bitmap_git)) ||
index 50891f77a26d6ea65b2c038f4f21ada49255a513..d28a7e43d0bd80c3dc968439b3262da7c80117c1 100644 (file)
@@ -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
@@ -158,10 +159,14 @@ 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)
@@ -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)
index e262f3efe84962e774c1784129de4c17de3936c8..848331d5d67bc93c9076e791adecbf2fc582d682 100644 (file)
@@ -8,7 +8,7 @@ struct revindex_entry {
        unsigned int nr;
 };
 
-void load_pack_revindex(struct packed_git *p);
+int load_pack_revindex(struct packed_git *p);
 int find_revindex_position(struct packed_git *p, off_t ofs);
 
 struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs);
index 16bcb75262d918fc7b88cc5141213e951be145cc..6e40bd89c762caf71439ae597d3d9028c669afcb 100644 (file)
@@ -2023,8 +2023,10 @@ int for_each_object_in_pack(struct packed_git *p,
        uint32_t i;
        int r = 0;
 
-       if (flags & FOR_EACH_OBJECT_PACK_ORDER)
-               load_pack_revindex(p);
+       if (flags & FOR_EACH_OBJECT_PACK_ORDER) {
+               if (load_pack_revindex(p))
+                       return -1;
+       }
 
        for (i = 0; i < p->num_objects; i++) {
                uint32_t pos;