Coverage Report

Created: 2024-09-16 06:10

/src/git/split-index.c
Line
Count
Source (jump to first uncovered line)
1
#define USE_THE_REPOSITORY_VARIABLE
2
3
#include "git-compat-util.h"
4
#include "gettext.h"
5
#include "hash.h"
6
#include "mem-pool.h"
7
#include "read-cache-ll.h"
8
#include "split-index.h"
9
#include "strbuf.h"
10
#include "ewah/ewok.h"
11
12
struct split_index *init_split_index(struct index_state *istate)
13
0
{
14
0
  if (!istate->split_index) {
15
0
    if (istate->sparse_index)
16
0
      die(_("cannot use split index with a sparse index"));
17
18
0
    CALLOC_ARRAY(istate->split_index, 1);
19
0
    istate->split_index->refcount = 1;
20
0
  }
21
0
  return istate->split_index;
22
0
}
23
24
int read_link_extension(struct index_state *istate,
25
       const void *data_, unsigned long sz)
26
0
{
27
0
  const unsigned char *data = data_;
28
0
  struct split_index *si;
29
0
  int ret;
30
31
0
  if (sz < the_hash_algo->rawsz)
32
0
    return error("corrupt link extension (too short)");
33
0
  si = init_split_index(istate);
34
0
  oidread(&si->base_oid, data, the_repository->hash_algo);
35
0
  data += the_hash_algo->rawsz;
36
0
  sz -= the_hash_algo->rawsz;
37
0
  if (!sz)
38
0
    return 0;
39
0
  si->delete_bitmap = ewah_new();
40
0
  ret = ewah_read_mmap(si->delete_bitmap, data, sz);
41
0
  if (ret < 0)
42
0
    return error("corrupt delete bitmap in link extension");
43
0
  data += ret;
44
0
  sz -= ret;
45
0
  si->replace_bitmap = ewah_new();
46
0
  ret = ewah_read_mmap(si->replace_bitmap, data, sz);
47
0
  if (ret < 0)
48
0
    return error("corrupt replace bitmap in link extension");
49
0
  if (ret != sz)
50
0
    return error("garbage at the end of link extension");
51
0
  return 0;
52
0
}
53
54
int write_link_extension(struct strbuf *sb,
55
       struct index_state *istate)
56
0
{
57
0
  struct split_index *si = istate->split_index;
58
0
  strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);
59
0
  if (!si->delete_bitmap && !si->replace_bitmap)
60
0
    return 0;
61
0
  ewah_serialize_strbuf(si->delete_bitmap, sb);
62
0
  ewah_serialize_strbuf(si->replace_bitmap, sb);
63
0
  return 0;
64
0
}
65
66
static void mark_base_index_entries(struct index_state *base)
67
0
{
68
0
  int i;
69
  /*
70
   * To keep track of the shared entries between
71
   * istate->base->cache[] and istate->cache[], base entry
72
   * position is stored in each base entry. All positions start
73
   * from 1 instead of 0, which is reserved to say "this is a new
74
   * entry".
75
   */
76
0
  for (i = 0; i < base->cache_nr; i++)
77
0
    base->cache[i]->index = i + 1;
78
0
}
79
80
void move_cache_to_base_index(struct index_state *istate)
81
0
{
82
0
  struct split_index *si = istate->split_index;
83
0
  int i;
84
85
  /*
86
   * If there was a previous base index, then transfer ownership of allocated
87
   * entries to the parent index.
88
   */
89
0
  if (si->base &&
90
0
    si->base->ce_mem_pool) {
91
92
0
    if (!istate->ce_mem_pool) {
93
0
      istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));
94
0
      mem_pool_init(istate->ce_mem_pool, 0);
95
0
    }
96
97
0
    mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);
98
0
  }
99
100
0
  ALLOC_ARRAY(si->base, 1);
101
0
  index_state_init(si->base, istate->repo);
102
0
  si->base->version = istate->version;
103
  /* zero timestamp disables racy test in ce_write_index() */
104
0
  si->base->timestamp = istate->timestamp;
105
0
  ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
106
0
  si->base->cache_nr = istate->cache_nr;
107
108
  /*
109
   * The mem_pool needs to move with the allocated entries.
110
   */
111
0
  si->base->ce_mem_pool = istate->ce_mem_pool;
112
0
  istate->ce_mem_pool = NULL;
113
114
0
  COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
115
0
  mark_base_index_entries(si->base);
116
0
  for (i = 0; i < si->base->cache_nr; i++)
117
0
    si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
118
0
}
119
120
static void mark_entry_for_delete(size_t pos, void *data)
121
0
{
122
0
  struct index_state *istate = data;
123
0
  if (pos >= istate->cache_nr)
124
0
    die("position for delete %d exceeds base index size %d",
125
0
        (int)pos, istate->cache_nr);
126
0
  istate->cache[pos]->ce_flags |= CE_REMOVE;
127
0
  istate->split_index->nr_deletions++;
128
0
}
129
130
static void replace_entry(size_t pos, void *data)
131
0
{
132
0
  struct index_state *istate = data;
133
0
  struct split_index *si = istate->split_index;
134
0
  struct cache_entry *dst, *src;
135
136
0
  if (pos >= istate->cache_nr)
137
0
    die("position for replacement %d exceeds base index size %d",
138
0
        (int)pos, istate->cache_nr);
139
0
  if (si->nr_replacements >= si->saved_cache_nr)
140
0
    die("too many replacements (%d vs %d)",
141
0
        si->nr_replacements, si->saved_cache_nr);
142
0
  dst = istate->cache[pos];
143
0
  if (dst->ce_flags & CE_REMOVE)
144
0
    die("entry %d is marked as both replaced and deleted",
145
0
        (int)pos);
146
0
  src = si->saved_cache[si->nr_replacements];
147
0
  if (ce_namelen(src))
148
0
    die("corrupt link extension, entry %d should have "
149
0
        "zero length name", (int)pos);
150
0
  src->index = pos + 1;
151
0
  src->ce_flags |= CE_UPDATE_IN_BASE;
152
0
  src->ce_namelen = dst->ce_namelen;
153
0
  copy_cache_entry(dst, src);
154
0
  discard_cache_entry(src);
155
0
  si->nr_replacements++;
156
0
}
157
158
void merge_base_index(struct index_state *istate)
159
0
{
160
0
  struct split_index *si = istate->split_index;
161
0
  unsigned int i;
162
163
0
  mark_base_index_entries(si->base);
164
165
0
  si->saved_cache     = istate->cache;
166
0
  si->saved_cache_nr  = istate->cache_nr;
167
0
  istate->cache_nr    = si->base->cache_nr;
168
0
  istate->cache     = NULL;
169
0
  istate->cache_alloc = 0;
170
0
  ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
171
0
  COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
172
173
0
  si->nr_deletions = 0;
174
0
  si->nr_replacements = 0;
175
0
  ewah_each_bit(si->replace_bitmap, replace_entry, istate);
176
0
  ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
177
0
  if (si->nr_deletions)
178
0
    remove_marked_cache_entries(istate, 0);
179
180
0
  for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
181
0
    if (!ce_namelen(si->saved_cache[i]))
182
0
      die("corrupt link extension, entry %d should "
183
0
          "have non-zero length name", i);
184
0
    add_index_entry(istate, si->saved_cache[i],
185
0
        ADD_CACHE_OK_TO_ADD |
186
0
        ADD_CACHE_KEEP_CACHE_TREE |
187
        /*
188
         * we may have to replay what
189
         * merge-recursive.c:update_stages()
190
         * does, which has this flag on
191
         */
192
0
        ADD_CACHE_SKIP_DFCHECK);
193
0
    si->saved_cache[i] = NULL;
194
0
  }
195
196
0
  ewah_free(si->delete_bitmap);
197
0
  ewah_free(si->replace_bitmap);
198
0
  FREE_AND_NULL(si->saved_cache);
199
0
  si->delete_bitmap  = NULL;
200
0
  si->replace_bitmap = NULL;
201
0
  si->saved_cache_nr = 0;
202
0
}
203
204
/*
205
 * Compare most of the fields in two cache entries, i.e. all except the
206
 * hashmap_entry and the name.
207
 */
208
static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)
209
0
{
210
0
  const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID |
211
0
            CE_EXTENDED_FLAGS;
212
0
  unsigned int ce_flags = a->ce_flags;
213
0
  unsigned int base_flags = b->ce_flags;
214
0
  int ret;
215
216
  /* only on-disk flags matter */
217
0
  a->ce_flags &= ondisk_flags;
218
0
  b->ce_flags &= ondisk_flags;
219
0
  ret = memcmp(&a->ce_stat_data, &b->ce_stat_data,
220
0
         offsetof(struct cache_entry, name) -
221
0
         offsetof(struct cache_entry, oid)) ||
222
0
    !oideq(&a->oid, &b->oid);
223
0
  a->ce_flags = ce_flags;
224
0
  b->ce_flags = base_flags;
225
226
0
  return ret;
227
0
}
228
229
void prepare_to_write_split_index(struct index_state *istate)
230
0
{
231
0
  struct split_index *si = init_split_index(istate);
232
0
  struct cache_entry **entries = NULL, *ce;
233
0
  int i, nr_entries = 0, nr_alloc = 0;
234
235
0
  si->delete_bitmap = ewah_new();
236
0
  si->replace_bitmap = ewah_new();
237
238
0
  if (si->base) {
239
    /* Go through istate->cache[] and mark CE_MATCHED to
240
     * entry with positive index. We'll go through
241
     * base->cache[] later to delete all entries in base
242
     * that are not marked with either CE_MATCHED or
243
     * CE_UPDATE_IN_BASE. If istate->cache[i] is a
244
     * duplicate, deduplicate it.
245
     */
246
0
    for (i = 0; i < istate->cache_nr; i++) {
247
0
      struct cache_entry *base;
248
0
      ce = istate->cache[i];
249
0
      if (!ce->index) {
250
        /*
251
         * During simple update index operations this
252
         * is a cache entry that is not present in
253
         * the shared index.  It will be added to the
254
         * split index.
255
         *
256
         * However, it might also represent a file
257
         * that already has a cache entry in the
258
         * shared index, but a new index has just
259
         * been constructed by unpack_trees(), and
260
         * this entry now refers to different content
261
         * than what was recorded in the original
262
         * index, e.g. during 'read-tree -m HEAD^' or
263
         * 'checkout HEAD^'.  In this case the
264
         * original entry in the shared index will be
265
         * marked as deleted, and this entry will be
266
         * added to the split index.
267
         */
268
0
        continue;
269
0
      }
270
0
      if (ce->index > si->base->cache_nr) {
271
0
        BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",
272
0
            ce->index, si->base->cache_nr);
273
0
      }
274
0
      ce->ce_flags |= CE_MATCHED; /* or "shared" */
275
0
      base = si->base->cache[ce->index - 1];
276
0
      if (ce == base) {
277
        /* The entry is present in the shared index. */
278
0
        if (ce->ce_flags & CE_UPDATE_IN_BASE) {
279
          /*
280
           * Already marked for inclusion in
281
           * the split index, either because
282
           * the corresponding file was
283
           * modified and the cached stat data
284
           * was refreshed, or because there
285
           * is already a replacement entry in
286
           * the split index.
287
           * Nothing more to do here.
288
           */
289
0
        } else if (!ce_uptodate(ce) &&
290
0
             is_racy_timestamp(istate, ce)) {
291
          /*
292
           * A racily clean cache entry stored
293
           * only in the shared index: it must
294
           * be added to the split index, so
295
           * the subsequent do_write_index()
296
           * can smudge its stat data.
297
           */
298
0
          ce->ce_flags |= CE_UPDATE_IN_BASE;
299
0
        } else {
300
          /*
301
           * The entry is only present in the
302
           * shared index and it was not
303
           * refreshed.
304
           * Just leave it there.
305
           */
306
0
        }
307
0
        continue;
308
0
      }
309
0
      if (ce->ce_namelen != base->ce_namelen ||
310
0
          strcmp(ce->name, base->name)) {
311
0
        ce->index = 0;
312
0
        continue;
313
0
      }
314
      /*
315
       * This is the copy of a cache entry that is present
316
       * in the shared index, created by unpack_trees()
317
       * while it constructed a new index.
318
       */
319
0
      if (ce->ce_flags & CE_UPDATE_IN_BASE) {
320
        /*
321
         * Already marked for inclusion in the split
322
         * index, either because the corresponding
323
         * file was modified and the cached stat data
324
         * was refreshed, or because the original
325
         * entry already had a replacement entry in
326
         * the split index.
327
         * Nothing to do.
328
         */
329
0
      } else if (!ce_uptodate(ce) &&
330
0
           is_racy_timestamp(istate, ce)) {
331
        /*
332
         * A copy of a racily clean cache entry from
333
         * the shared index.  It must be added to
334
         * the split index, so the subsequent
335
         * do_write_index() can smudge its stat data.
336
         */
337
0
        ce->ce_flags |= CE_UPDATE_IN_BASE;
338
0
      } else {
339
        /*
340
         * Thoroughly compare the cached data to see
341
         * whether it should be marked for inclusion
342
         * in the split index.
343
         *
344
         * This comparison might be unnecessary, as
345
         * code paths modifying the cached data do
346
         * set CE_UPDATE_IN_BASE as well.
347
         */
348
0
        if (compare_ce_content(ce, base))
349
0
          ce->ce_flags |= CE_UPDATE_IN_BASE;
350
0
      }
351
0
      discard_cache_entry(base);
352
0
      si->base->cache[ce->index - 1] = ce;
353
0
    }
354
0
    for (i = 0; i < si->base->cache_nr; i++) {
355
0
      ce = si->base->cache[i];
356
0
      if ((ce->ce_flags & CE_REMOVE) ||
357
0
          !(ce->ce_flags & CE_MATCHED))
358
0
        ewah_set(si->delete_bitmap, i);
359
0
      else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
360
0
        ewah_set(si->replace_bitmap, i);
361
0
        ce->ce_flags |= CE_STRIP_NAME;
362
0
        ALLOC_GROW(entries, nr_entries+1, nr_alloc);
363
0
        entries[nr_entries++] = ce;
364
0
      }
365
0
      if (is_null_oid(&ce->oid))
366
0
        istate->drop_cache_tree = 1;
367
0
    }
368
0
  }
369
370
0
  for (i = 0; i < istate->cache_nr; i++) {
371
0
    ce = istate->cache[i];
372
0
    if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
373
0
      assert(!(ce->ce_flags & CE_STRIP_NAME));
374
0
      ALLOC_GROW(entries, nr_entries+1, nr_alloc);
375
0
      entries[nr_entries++] = ce;
376
0
    }
377
0
    ce->ce_flags &= ~CE_MATCHED;
378
0
  }
379
380
  /*
381
   * take cache[] out temporarily, put entries[] in its place
382
   * for writing
383
   */
384
0
  si->saved_cache = istate->cache;
385
0
  si->saved_cache_nr = istate->cache_nr;
386
0
  istate->cache = entries;
387
0
  istate->cache_nr = nr_entries;
388
0
}
389
390
void finish_writing_split_index(struct index_state *istate)
391
0
{
392
0
  struct split_index *si = init_split_index(istate);
393
394
0
  ewah_free(si->delete_bitmap);
395
0
  ewah_free(si->replace_bitmap);
396
0
  si->delete_bitmap = NULL;
397
0
  si->replace_bitmap = NULL;
398
0
  free(istate->cache);
399
0
  istate->cache = si->saved_cache;
400
0
  istate->cache_nr = si->saved_cache_nr;
401
0
}
402
403
void discard_split_index(struct index_state *istate)
404
0
{
405
0
  struct split_index *si = istate->split_index;
406
0
  if (!si)
407
0
    return;
408
0
  istate->split_index = NULL;
409
0
  si->refcount--;
410
0
  if (si->refcount)
411
0
    return;
412
0
  if (si->base) {
413
0
    discard_index(si->base);
414
0
    free(si->base);
415
0
  }
416
0
  free(si);
417
0
}
418
419
void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
420
0
{
421
0
  if (ce->index &&
422
0
      istate->split_index &&
423
0
      istate->split_index->base &&
424
0
      ce->index <= istate->split_index->base->cache_nr &&
425
0
      ce == istate->split_index->base->cache[ce->index - 1])
426
0
    ce->ce_flags |= CE_REMOVE;
427
0
  else
428
0
    discard_cache_entry(ce);
429
0
}
430
431
void replace_index_entry_in_base(struct index_state *istate,
432
         struct cache_entry *old_entry,
433
         struct cache_entry *new_entry)
434
0
{
435
0
  if (old_entry->index &&
436
0
      istate->split_index &&
437
0
      istate->split_index->base &&
438
0
      old_entry->index <= istate->split_index->base->cache_nr) {
439
0
    new_entry->index = old_entry->index;
440
0
    if (old_entry != istate->split_index->base->cache[new_entry->index - 1])
441
0
      discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);
442
0
    istate->split_index->base->cache[new_entry->index - 1] = new_entry;
443
0
  }
444
0
}
445
446
void add_split_index(struct index_state *istate)
447
0
{
448
0
  if (!istate->split_index) {
449
0
    init_split_index(istate);
450
0
    istate->cache_changed |= SPLIT_INDEX_ORDERED;
451
0
  }
452
0
}
453
454
void remove_split_index(struct index_state *istate)
455
0
{
456
0
  if (istate->split_index) {
457
0
    if (istate->split_index->base) {
458
      /*
459
       * When removing the split index, we need to move
460
       * ownership of the mem_pool associated with the
461
       * base index to the main index. There may be cache entries
462
       * allocated from the base's memory pool that are shared with
463
       * the_index.cache[].
464
       */
465
0
      mem_pool_combine(istate->ce_mem_pool,
466
0
           istate->split_index->base->ce_mem_pool);
467
468
      /*
469
       * The split index no longer owns the mem_pool backing
470
       * its cache array. As we are discarding this index,
471
       * mark the index as having no cache entries, so it
472
       * will not attempt to clean up the cache entries or
473
       * validate them.
474
       */
475
0
      istate->split_index->base->cache_nr = 0;
476
0
    }
477
478
    /*
479
     * We can discard the split index because its
480
     * memory pool has been incorporated into the
481
     * memory pool associated with the the_index.
482
     */
483
0
    discard_split_index(istate);
484
485
0
    istate->cache_changed |= SOMETHING_CHANGED;
486
0
  }
487
0
}