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