Commit | Line | Data |
---|---|---|
5fc2fc8f NTND |
1 | #include "cache.h" |
2 | #include "split-index.h" | |
96a1d8d3 | 3 | #include "ewah/ewok.h" |
5fc2fc8f NTND |
4 | |
5 | struct 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 | ||
14 | int 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; | |
76b07b37 NTND |
19 | int ret; |
20 | ||
2182abd9 | 21 | if (sz < the_hash_algo->rawsz) |
5fc2fc8f NTND |
22 | return error("corrupt link extension (too short)"); |
23 | si = init_split_index(istate); | |
2182abd9 | 24 | hashcpy(si->base_oid.hash, data); |
25 | data += the_hash_algo->rawsz; | |
26 | sz -= the_hash_algo->rawsz; | |
76b07b37 NTND |
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) | |
5fc2fc8f NTND |
40 | return error("garbage at the end of link extension"); |
41 | return 0; | |
42 | } | |
43 | ||
44 | int write_link_extension(struct strbuf *sb, | |
45 | struct index_state *istate) | |
46 | { | |
47 | struct split_index *si = istate->split_index; | |
2182abd9 | 48 | strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz); |
96a1d8d3 NTND |
49 | if (!si->delete_bitmap && !si->replace_bitmap) |
50 | return 0; | |
be0d9d53 NTND |
51 | ewah_serialize_strbuf(si->delete_bitmap, sb); |
52 | ewah_serialize_strbuf(si->replace_bitmap, sb); | |
5fc2fc8f NTND |
53 | return 0; |
54 | } | |
55 | ||
56 | static 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 | |
832c0e5e | 63 | * from 1 instead of 0, which is reserved to say "this is a new |
5fc2fc8f NTND |
64 | * entry". |
65 | */ | |
66 | for (i = 0; i < base->cache_nr; i++) | |
67 | base->cache[i]->index = i + 1; | |
68 | } | |
69 | ||
c18b80a0 NTND |
70 | void move_cache_to_base_index(struct index_state *istate) |
71 | { | |
72 | struct split_index *si = istate->split_index; | |
73 | int i; | |
74 | ||
75 | /* | |
8e72d675 JM |
76 | * If there was a previous base index, then transfer ownership of allocated |
77 | * entries to the parent index. | |
c18b80a0 | 78 | */ |
8e72d675 JM |
79 | if (si->base && |
80 | si->base->ce_mem_pool) { | |
81 | ||
82 | if (!istate->ce_mem_pool) | |
83 | mem_pool_init(&istate->ce_mem_pool, 0); | |
84 | ||
85 | mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool); | |
86 | } | |
87 | ||
c18b80a0 NTND |
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; | |
92 | ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc); | |
93 | si->base->cache_nr = istate->cache_nr; | |
8e72d675 JM |
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 | ||
45ccef87 | 101 | COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr); |
c18b80a0 NTND |
102 | mark_base_index_entries(si->base); |
103 | for (i = 0; i < si->base->cache_nr; i++) | |
104 | si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE; | |
105 | } | |
106 | ||
76b07b37 NTND |
107 | static void mark_entry_for_delete(size_t pos, void *data) |
108 | { | |
109 | struct index_state *istate = data; | |
110 | if (pos >= istate->cache_nr) | |
111 | die("position for delete %d exceeds base index size %d", | |
112 | (int)pos, istate->cache_nr); | |
113 | istate->cache[pos]->ce_flags |= CE_REMOVE; | |
2034e848 | 114 | istate->split_index->nr_deletions++; |
76b07b37 NTND |
115 | } |
116 | ||
117 | static void replace_entry(size_t pos, void *data) | |
118 | { | |
119 | struct index_state *istate = data; | |
120 | struct split_index *si = istate->split_index; | |
121 | struct cache_entry *dst, *src; | |
b3c96fb1 | 122 | |
76b07b37 NTND |
123 | if (pos >= istate->cache_nr) |
124 | die("position for replacement %d exceeds base index size %d", | |
125 | (int)pos, istate->cache_nr); | |
126 | if (si->nr_replacements >= si->saved_cache_nr) | |
127 | die("too many replacements (%d vs %d)", | |
128 | si->nr_replacements, si->saved_cache_nr); | |
129 | dst = istate->cache[pos]; | |
130 | if (dst->ce_flags & CE_REMOVE) | |
131 | die("entry %d is marked as both replaced and deleted", | |
132 | (int)pos); | |
133 | src = si->saved_cache[si->nr_replacements]; | |
b3c96fb1 NTND |
134 | if (ce_namelen(src)) |
135 | die("corrupt link extension, entry %d should have " | |
136 | "zero length name", (int)pos); | |
76b07b37 NTND |
137 | src->index = pos + 1; |
138 | src->ce_flags |= CE_UPDATE_IN_BASE; | |
b3c96fb1 NTND |
139 | src->ce_namelen = dst->ce_namelen; |
140 | copy_cache_entry(dst, src); | |
a849735b | 141 | discard_cache_entry(src); |
76b07b37 NTND |
142 | si->nr_replacements++; |
143 | } | |
144 | ||
5fc2fc8f NTND |
145 | void merge_base_index(struct index_state *istate) |
146 | { | |
147 | struct split_index *si = istate->split_index; | |
76b07b37 | 148 | unsigned int i; |
5fc2fc8f NTND |
149 | |
150 | mark_base_index_entries(si->base); | |
76b07b37 NTND |
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; | |
5fc2fc8f | 157 | ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc); |
45ccef87 | 158 | COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr); |
76b07b37 NTND |
159 | |
160 | si->nr_deletions = 0; | |
161 | si->nr_replacements = 0; | |
162 | ewah_each_bit(si->replace_bitmap, replace_entry, istate); | |
163 | ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate); | |
164 | if (si->nr_deletions) | |
165 | remove_marked_cache_entries(istate); | |
166 | ||
167 | for (i = si->nr_replacements; i < si->saved_cache_nr; i++) { | |
b3c96fb1 NTND |
168 | if (!ce_namelen(si->saved_cache[i])) |
169 | die("corrupt link extension, entry %d should " | |
170 | "have non-zero length name", i); | |
76b07b37 NTND |
171 | add_index_entry(istate, si->saved_cache[i], |
172 | ADD_CACHE_OK_TO_ADD | | |
ce7c614b | 173 | ADD_CACHE_KEEP_CACHE_TREE | |
76b07b37 NTND |
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 | ||
183 | ewah_free(si->delete_bitmap); | |
184 | ewah_free(si->replace_bitmap); | |
88ce3ef6 | 185 | FREE_AND_NULL(si->saved_cache); |
76b07b37 NTND |
186 | si->delete_bitmap = NULL; |
187 | si->replace_bitmap = NULL; | |
76b07b37 | 188 | si->saved_cache_nr = 0; |
5fc2fc8f NTND |
189 | } |
190 | ||
e3d83798 SG |
191 | /* |
192 | * Compare most of the fields in two cache entries, i.e. all except the | |
193 | * hashmap_entry and the name. | |
194 | */ | |
195 | static int compare_ce_content(struct cache_entry *a, struct cache_entry *b) | |
196 | { | |
197 | const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID | | |
198 | CE_EXTENDED_FLAGS; | |
199 | unsigned int ce_flags = a->ce_flags; | |
200 | unsigned int base_flags = b->ce_flags; | |
201 | int 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, | |
207 | offsetof(struct cache_entry, name) - | |
208 | offsetof(struct cache_entry, ce_stat_data)); | |
209 | a->ce_flags = ce_flags; | |
210 | b->ce_flags = base_flags; | |
211 | ||
212 | return ret; | |
213 | } | |
214 | ||
5fc2fc8f NTND |
215 | void prepare_to_write_split_index(struct index_state *istate) |
216 | { | |
217 | struct split_index *si = init_split_index(istate); | |
96a1d8d3 NTND |
218 | struct cache_entry **entries = NULL, *ce; |
219 | int i, nr_entries = 0, nr_alloc = 0; | |
220 | ||
221 | si->delete_bitmap = ewah_new(); | |
222 | si->replace_bitmap = ewah_new(); | |
223 | ||
224 | if (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 | |
753c4515 | 228 | * that are not marked with either CE_MATCHED or |
96a1d8d3 NTND |
229 | * CE_UPDATE_IN_BASE. If istate->cache[i] is a |
230 | * duplicate, deduplicate it. | |
231 | */ | |
232 | for (i = 0; i < istate->cache_nr; i++) { | |
233 | struct cache_entry *base; | |
96a1d8d3 | 234 | ce = istate->cache[i]; |
e3d83798 SG |
235 | if (!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 | */ | |
96a1d8d3 | 254 | continue; |
e3d83798 | 255 | } |
96a1d8d3 NTND |
256 | if (ce->index > si->base->cache_nr) { |
257 | ce->index = 0; | |
258 | continue; | |
259 | } | |
260 | ce->ce_flags |= CE_MATCHED; /* or "shared" */ | |
261 | base = si->base->cache[ce->index - 1]; | |
262 | if (ce == base) | |
263 | continue; | |
264 | if (ce->ce_namelen != base->ce_namelen || | |
265 | strcmp(ce->name, base->name)) { | |
266 | ce->index = 0; | |
267 | continue; | |
268 | } | |
e3d83798 SG |
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 | */ | |
274 | if (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 | */ | |
294 | if (compare_ce_content(ce, base)) | |
295 | ce->ce_flags |= CE_UPDATE_IN_BASE; | |
296 | } | |
a849735b | 297 | discard_cache_entry(base); |
96a1d8d3 NTND |
298 | si->base->cache[ce->index - 1] = ce; |
299 | } | |
300 | for (i = 0; i < si->base->cache_nr; i++) { | |
301 | ce = si->base->cache[i]; | |
302 | if ((ce->ce_flags & CE_REMOVE) || | |
303 | !(ce->ce_flags & CE_MATCHED)) | |
304 | ewah_set(si->delete_bitmap, i); | |
305 | else if (ce->ce_flags & CE_UPDATE_IN_BASE) { | |
306 | ewah_set(si->replace_bitmap, i); | |
b3c96fb1 | 307 | ce->ce_flags |= CE_STRIP_NAME; |
96a1d8d3 NTND |
308 | ALLOC_GROW(entries, nr_entries+1, nr_alloc); |
309 | entries[nr_entries++] = ce; | |
310 | } | |
4bddd983 TG |
311 | if (is_null_oid(&ce->oid)) |
312 | istate->drop_cache_tree = 1; | |
96a1d8d3 NTND |
313 | } |
314 | } | |
315 | ||
316 | for (i = 0; i < istate->cache_nr; i++) { | |
317 | ce = istate->cache[i]; | |
318 | if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) { | |
b3c96fb1 | 319 | assert(!(ce->ce_flags & CE_STRIP_NAME)); |
96a1d8d3 NTND |
320 | ALLOC_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; | |
5fc2fc8f | 331 | si->saved_cache_nr = istate->cache_nr; |
96a1d8d3 NTND |
332 | istate->cache = entries; |
333 | istate->cache_nr = nr_entries; | |
5fc2fc8f NTND |
334 | } |
335 | ||
336 | void finish_writing_split_index(struct index_state *istate) | |
337 | { | |
338 | struct split_index *si = init_split_index(istate); | |
96a1d8d3 NTND |
339 | |
340 | ewah_free(si->delete_bitmap); | |
341 | ewah_free(si->replace_bitmap); | |
342 | si->delete_bitmap = NULL; | |
343 | si->replace_bitmap = NULL; | |
344 | free(istate->cache); | |
345 | istate->cache = si->saved_cache; | |
5fc2fc8f NTND |
346 | istate->cache_nr = si->saved_cache_nr; |
347 | } | |
348 | ||
349 | void discard_split_index(struct index_state *istate) | |
350 | { | |
351 | struct split_index *si = istate->split_index; | |
352 | if (!si) | |
353 | return; | |
354 | istate->split_index = NULL; | |
355 | si->refcount--; | |
356 | if (si->refcount) | |
357 | return; | |
358 | if (si->base) { | |
359 | discard_index(si->base); | |
360 | free(si->base); | |
361 | } | |
362 | free(si); | |
363 | } | |
045113a5 NTND |
364 | |
365 | void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce) | |
366 | { | |
367 | if (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; | |
373 | else | |
a849735b | 374 | discard_cache_entry(ce); |
045113a5 | 375 | } |
078a58e8 NTND |
376 | |
377 | void replace_index_entry_in_base(struct index_state *istate, | |
75b7b971 BW |
378 | struct cache_entry *old_entry, |
379 | struct cache_entry *new_entry) | |
078a58e8 | 380 | { |
75b7b971 | 381 | if (old_entry->index && |
078a58e8 NTND |
382 | istate->split_index && |
383 | istate->split_index->base && | |
75b7b971 BW |
384 | old_entry->index <= istate->split_index->base->cache_nr) { |
385 | new_entry->index = old_entry->index; | |
386 | if (old_entry != istate->split_index->base->cache[new_entry->index - 1]) | |
a849735b | 387 | discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]); |
75b7b971 | 388 | istate->split_index->base->cache[new_entry->index - 1] = new_entry; |
078a58e8 NTND |
389 | } |
390 | } | |
cef4fc7e CC |
391 | |
392 | void add_split_index(struct index_state *istate) | |
393 | { | |
394 | if (!istate->split_index) { | |
395 | init_split_index(istate); | |
396 | istate->cache_changed |= SPLIT_INDEX_ORDERED; | |
397 | } | |
398 | } | |
399 | ||
400 | void remove_split_index(struct index_state *istate) | |
401 | { | |
64719b11 JH |
402 | if (istate->split_index) { |
403 | /* | |
8e72d675 JM |
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[]. | |
64719b11 | 409 | */ |
8e72d675 JM |
410 | mem_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 | */ | |
419 | if (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 | */ | |
427 | discard_split_index(istate); | |
428 | ||
64719b11 JH |
429 | istate->cache_changed |= SOMETHING_CHANGED; |
430 | } | |
cef4fc7e | 431 | } |