split-index.con commit submodule: try harder to fetch needed sha1 by direct fetching sha1 (fb43e31)
   1#include "cache.h"
   2#include "split-index.h"
   3#include "ewah/ewok.h"
   4
   5struct split_index *init_split_index(struct index_state *istate)
   6{
   7        if (!istate->split_index) {
   8                istate->split_index = xcalloc(1, sizeof(*istate->split_index));
   9                istate->split_index->refcount = 1;
  10        }
  11        return istate->split_index;
  12}
  13
  14int read_link_extension(struct index_state *istate,
  15                         const void *data_, unsigned long sz)
  16{
  17        const unsigned char *data = data_;
  18        struct split_index *si;
  19        int ret;
  20
  21        if (sz < 20)
  22                return error("corrupt link extension (too short)");
  23        si = init_split_index(istate);
  24        hashcpy(si->base_sha1, data);
  25        data += 20;
  26        sz -= 20;
  27        if (!sz)
  28                return 0;
  29        si->delete_bitmap = ewah_new();
  30        ret = ewah_read_mmap(si->delete_bitmap, data, sz);
  31        if (ret < 0)
  32                return error("corrupt delete bitmap in link extension");
  33        data += ret;
  34        sz -= ret;
  35        si->replace_bitmap = ewah_new();
  36        ret = ewah_read_mmap(si->replace_bitmap, data, sz);
  37        if (ret < 0)
  38                return error("corrupt replace bitmap in link extension");
  39        if (ret != sz)
  40                return error("garbage at the end of link extension");
  41        return 0;
  42}
  43
  44int write_link_extension(struct strbuf *sb,
  45                         struct index_state *istate)
  46{
  47        struct split_index *si = istate->split_index;
  48        strbuf_add(sb, si->base_sha1, 20);
  49        if (!si->delete_bitmap && !si->replace_bitmap)
  50                return 0;
  51        ewah_serialize_strbuf(si->delete_bitmap, sb);
  52        ewah_serialize_strbuf(si->replace_bitmap, sb);
  53        return 0;
  54}
  55
  56static void mark_base_index_entries(struct index_state *base)
  57{
  58        int i;
  59        /*
  60         * To keep track of the shared entries between
  61         * istate->base->cache[] and istate->cache[], base entry
  62         * position is stored in each base entry. All positions start
  63         * from 1 instead of 0, which is resrved to say "this is a new
  64         * entry".
  65         */
  66        for (i = 0; i < base->cache_nr; i++)
  67                base->cache[i]->index = i + 1;
  68}
  69
  70void move_cache_to_base_index(struct index_state *istate)
  71{
  72        struct split_index *si = istate->split_index;
  73        int i;
  74
  75        /*
  76         * do not delete old si->base, its index entries may be shared
  77         * with istate->cache[]. Accept a bit of leaking here because
  78         * this code is only used by short-lived update-index.
  79         */
  80        si->base = xcalloc(1, sizeof(*si->base));
  81        si->base->version = istate->version;
  82        /* zero timestamp disables racy test in ce_write_index() */
  83        si->base->timestamp = istate->timestamp;
  84        ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
  85        si->base->cache_nr = istate->cache_nr;
  86        memcpy(si->base->cache, istate->cache,
  87               sizeof(*istate->cache) * istate->cache_nr);
  88        mark_base_index_entries(si->base);
  89        for (i = 0; i < si->base->cache_nr; i++)
  90                si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
  91}
  92
  93static void mark_entry_for_delete(size_t pos, void *data)
  94{
  95        struct index_state *istate = data;
  96        if (pos >= istate->cache_nr)
  97                die("position for delete %d exceeds base index size %d",
  98                    (int)pos, istate->cache_nr);
  99        istate->cache[pos]->ce_flags |= CE_REMOVE;
 100        istate->split_index->nr_deletions = 1;
 101}
 102
 103static void replace_entry(size_t pos, void *data)
 104{
 105        struct index_state *istate = data;
 106        struct split_index *si = istate->split_index;
 107        struct cache_entry *dst, *src;
 108
 109        if (pos >= istate->cache_nr)
 110                die("position for replacement %d exceeds base index size %d",
 111                    (int)pos, istate->cache_nr);
 112        if (si->nr_replacements >= si->saved_cache_nr)
 113                die("too many replacements (%d vs %d)",
 114                    si->nr_replacements, si->saved_cache_nr);
 115        dst = istate->cache[pos];
 116        if (dst->ce_flags & CE_REMOVE)
 117                die("entry %d is marked as both replaced and deleted",
 118                    (int)pos);
 119        src = si->saved_cache[si->nr_replacements];
 120        if (ce_namelen(src))
 121                die("corrupt link extension, entry %d should have "
 122                    "zero length name", (int)pos);
 123        src->index = pos + 1;
 124        src->ce_flags |= CE_UPDATE_IN_BASE;
 125        src->ce_namelen = dst->ce_namelen;
 126        copy_cache_entry(dst, src);
 127        free(src);
 128        si->nr_replacements++;
 129}
 130
 131void merge_base_index(struct index_state *istate)
 132{
 133        struct split_index *si = istate->split_index;
 134        unsigned int i;
 135
 136        mark_base_index_entries(si->base);
 137
 138        si->saved_cache     = istate->cache;
 139        si->saved_cache_nr  = istate->cache_nr;
 140        istate->cache_nr    = si->base->cache_nr;
 141        istate->cache       = NULL;
 142        istate->cache_alloc = 0;
 143        ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
 144        memcpy(istate->cache, si->base->cache,
 145               sizeof(*istate->cache) * istate->cache_nr);
 146
 147        si->nr_deletions = 0;
 148        si->nr_replacements = 0;
 149        ewah_each_bit(si->replace_bitmap, replace_entry, istate);
 150        ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
 151        if (si->nr_deletions)
 152                remove_marked_cache_entries(istate);
 153
 154        for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
 155                if (!ce_namelen(si->saved_cache[i]))
 156                        die("corrupt link extension, entry %d should "
 157                            "have non-zero length name", i);
 158                add_index_entry(istate, si->saved_cache[i],
 159                                ADD_CACHE_OK_TO_ADD |
 160                                ADD_CACHE_KEEP_CACHE_TREE |
 161                                /*
 162                                 * we may have to replay what
 163                                 * merge-recursive.c:update_stages()
 164                                 * does, which has this flag on
 165                                 */
 166                                ADD_CACHE_SKIP_DFCHECK);
 167                si->saved_cache[i] = NULL;
 168        }
 169
 170        ewah_free(si->delete_bitmap);
 171        ewah_free(si->replace_bitmap);
 172        free(si->saved_cache);
 173        si->delete_bitmap  = NULL;
 174        si->replace_bitmap = NULL;
 175        si->saved_cache    = NULL;
 176        si->saved_cache_nr = 0;
 177}
 178
 179void prepare_to_write_split_index(struct index_state *istate)
 180{
 181        struct split_index *si = init_split_index(istate);
 182        struct cache_entry **entries = NULL, *ce;
 183        int i, nr_entries = 0, nr_alloc = 0;
 184
 185        si->delete_bitmap = ewah_new();
 186        si->replace_bitmap = ewah_new();
 187
 188        if (si->base) {
 189                /* Go through istate->cache[] and mark CE_MATCHED to
 190                 * entry with positive index. We'll go through
 191                 * base->cache[] later to delete all entries in base
 192                 * that are not marked eith either CE_MATCHED or
 193                 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
 194                 * duplicate, deduplicate it.
 195                 */
 196                for (i = 0; i < istate->cache_nr; i++) {
 197                        struct cache_entry *base;
 198                        /* namelen is checked separately */
 199                        const unsigned int ondisk_flags =
 200                                CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
 201                        unsigned int ce_flags, base_flags, ret;
 202                        ce = istate->cache[i];
 203                        if (!ce->index)
 204                                continue;
 205                        if (ce->index > si->base->cache_nr) {
 206                                ce->index = 0;
 207                                continue;
 208                        }
 209                        ce->ce_flags |= CE_MATCHED; /* or "shared" */
 210                        base = si->base->cache[ce->index - 1];
 211                        if (ce == base)
 212                                continue;
 213                        if (ce->ce_namelen != base->ce_namelen ||
 214                            strcmp(ce->name, base->name)) {
 215                                ce->index = 0;
 216                                continue;
 217                        }
 218                        ce_flags = ce->ce_flags;
 219                        base_flags = base->ce_flags;
 220                        /* only on-disk flags matter */
 221                        ce->ce_flags   &= ondisk_flags;
 222                        base->ce_flags &= ondisk_flags;
 223                        ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
 224                                     offsetof(struct cache_entry, name) -
 225                                     offsetof(struct cache_entry, ce_stat_data));
 226                        ce->ce_flags = ce_flags;
 227                        base->ce_flags = base_flags;
 228                        if (ret)
 229                                ce->ce_flags |= CE_UPDATE_IN_BASE;
 230                        free(base);
 231                        si->base->cache[ce->index - 1] = ce;
 232                }
 233                for (i = 0; i < si->base->cache_nr; i++) {
 234                        ce = si->base->cache[i];
 235                        if ((ce->ce_flags & CE_REMOVE) ||
 236                            !(ce->ce_flags & CE_MATCHED))
 237                                ewah_set(si->delete_bitmap, i);
 238                        else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
 239                                ewah_set(si->replace_bitmap, i);
 240                                ce->ce_flags |= CE_STRIP_NAME;
 241                                ALLOC_GROW(entries, nr_entries+1, nr_alloc);
 242                                entries[nr_entries++] = ce;
 243                        }
 244                }
 245        }
 246
 247        for (i = 0; i < istate->cache_nr; i++) {
 248                ce = istate->cache[i];
 249                if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
 250                        assert(!(ce->ce_flags & CE_STRIP_NAME));
 251                        ALLOC_GROW(entries, nr_entries+1, nr_alloc);
 252                        entries[nr_entries++] = ce;
 253                }
 254                ce->ce_flags &= ~CE_MATCHED;
 255        }
 256
 257        /*
 258         * take cache[] out temporarily, put entries[] in its place
 259         * for writing
 260         */
 261        si->saved_cache = istate->cache;
 262        si->saved_cache_nr = istate->cache_nr;
 263        istate->cache = entries;
 264        istate->cache_nr = nr_entries;
 265}
 266
 267void finish_writing_split_index(struct index_state *istate)
 268{
 269        struct split_index *si = init_split_index(istate);
 270
 271        ewah_free(si->delete_bitmap);
 272        ewah_free(si->replace_bitmap);
 273        si->delete_bitmap = NULL;
 274        si->replace_bitmap = NULL;
 275        free(istate->cache);
 276        istate->cache = si->saved_cache;
 277        istate->cache_nr = si->saved_cache_nr;
 278}
 279
 280void discard_split_index(struct index_state *istate)
 281{
 282        struct split_index *si = istate->split_index;
 283        if (!si)
 284                return;
 285        istate->split_index = NULL;
 286        si->refcount--;
 287        if (si->refcount)
 288                return;
 289        if (si->base) {
 290                discard_index(si->base);
 291                free(si->base);
 292        }
 293        free(si);
 294}
 295
 296void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
 297{
 298        if (ce->index &&
 299            istate->split_index &&
 300            istate->split_index->base &&
 301            ce->index <= istate->split_index->base->cache_nr &&
 302            ce == istate->split_index->base->cache[ce->index - 1])
 303                ce->ce_flags |= CE_REMOVE;
 304        else
 305                free(ce);
 306}
 307
 308void replace_index_entry_in_base(struct index_state *istate,
 309                                 struct cache_entry *old,
 310                                 struct cache_entry *new)
 311{
 312        if (old->index &&
 313            istate->split_index &&
 314            istate->split_index->base &&
 315            old->index <= istate->split_index->base->cache_nr) {
 316                new->index = old->index;
 317                if (old != istate->split_index->base->cache[new->index - 1])
 318                        free(istate->split_index->base->cache[new->index - 1]);
 319                istate->split_index->base->cache[new->index - 1] = new;
 320        }
 321}