1#include "cache.h"
2#include "tree.h"
3#include "tree-walk.h"
4#include "cache-tree.h"
5
6#ifndef DEBUG
7#define DEBUG 0
8#endif
9
10struct cache_tree *cache_tree(void)
11{
12 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
13 it->entry_count = -1;
14 return it;
15}
16
17void cache_tree_free(struct cache_tree **it_p)
18{
19 int i;
20 struct cache_tree *it = *it_p;
21
22 if (!it)
23 return;
24 for (i = 0; i < it->subtree_nr; i++)
25 if (it->down[i]) {
26 cache_tree_free(&it->down[i]->cache_tree);
27 free(it->down[i]);
28 }
29 free(it->down);
30 free(it);
31 *it_p = NULL;
32}
33
34static int subtree_name_cmp(const char *one, int onelen,
35 const char *two, int twolen)
36{
37 if (onelen < twolen)
38 return -1;
39 if (twolen < onelen)
40 return 1;
41 return memcmp(one, two, onelen);
42}
43
44static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
45{
46 struct cache_tree_sub **down = it->down;
47 int lo, hi;
48 lo = 0;
49 hi = it->subtree_nr;
50 while (lo < hi) {
51 int mi = (lo + hi) / 2;
52 struct cache_tree_sub *mdl = down[mi];
53 int cmp = subtree_name_cmp(path, pathlen,
54 mdl->name, mdl->namelen);
55 if (!cmp)
56 return mi;
57 if (cmp < 0)
58 hi = mi;
59 else
60 lo = mi + 1;
61 }
62 return -lo-1;
63}
64
65static struct cache_tree_sub *find_subtree(struct cache_tree *it,
66 const char *path,
67 int pathlen,
68 int create)
69{
70 struct cache_tree_sub *down;
71 int pos = subtree_pos(it, path, pathlen);
72 if (0 <= pos)
73 return it->down[pos];
74 if (!create)
75 return NULL;
76
77 pos = -pos-1;
78 if (it->subtree_alloc <= it->subtree_nr) {
79 it->subtree_alloc = alloc_nr(it->subtree_alloc);
80 it->down = xrealloc(it->down, it->subtree_alloc *
81 sizeof(*it->down));
82 }
83 it->subtree_nr++;
84
85 down = xmalloc(sizeof(*down) + pathlen + 1);
86 down->cache_tree = NULL;
87 down->namelen = pathlen;
88 memcpy(down->name, path, pathlen);
89 down->name[pathlen] = 0;
90
91 if (pos < it->subtree_nr)
92 memmove(it->down + pos + 1,
93 it->down + pos,
94 sizeof(down) * (it->subtree_nr - pos - 1));
95 it->down[pos] = down;
96 return down;
97}
98
99struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
100{
101 int pathlen = strlen(path);
102 return find_subtree(it, path, pathlen, 1);
103}
104
105void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
106{
107 /* a/b/c
108 * ==> invalidate self
109 * ==> find "a", have it invalidate "b/c"
110 * a
111 * ==> invalidate self
112 * ==> if "a" exists as a subtree, remove it.
113 */
114 const char *slash;
115 int namelen;
116 struct cache_tree_sub *down;
117
118#if DEBUG
119 fprintf(stderr, "cache-tree invalidate <%s>\n", path);
120#endif
121
122 if (!it)
123 return;
124 slash = strchrnul(path, '/');
125 namelen = slash - path;
126 it->entry_count = -1;
127 if (!*slash) {
128 int pos;
129 pos = subtree_pos(it, path, namelen);
130 if (0 <= pos) {
131 cache_tree_free(&it->down[pos]->cache_tree);
132 free(it->down[pos]);
133 /* 0 1 2 3 4 5
134 * ^ ^subtree_nr = 6
135 * pos
136 * move 4 and 5 up one place (2 entries)
137 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
138 */
139 memmove(it->down+pos, it->down+pos+1,
140 sizeof(struct cache_tree_sub *) *
141 (it->subtree_nr - pos - 1));
142 it->subtree_nr--;
143 }
144 return;
145 }
146 down = find_subtree(it, path, namelen, 0);
147 if (down)
148 cache_tree_invalidate_path(down->cache_tree, slash + 1);
149}
150
151static int verify_cache(const struct cache_entry * const *cache,
152 int entries, int flags)
153{
154 int i, funny;
155 int silent = flags & WRITE_TREE_SILENT;
156
157 /* Verify that the tree is merged */
158 funny = 0;
159 for (i = 0; i < entries; i++) {
160 const struct cache_entry *ce = cache[i];
161 if (ce_stage(ce)) {
162 if (silent)
163 return -1;
164 if (10 < ++funny) {
165 fprintf(stderr, "...\n");
166 break;
167 }
168 fprintf(stderr, "%s: unmerged (%s)\n",
169 ce->name, sha1_to_hex(ce->sha1));
170 }
171 }
172 if (funny)
173 return -1;
174
175 /* Also verify that the cache does not have path and path/file
176 * at the same time. At this point we know the cache has only
177 * stage 0 entries.
178 */
179 funny = 0;
180 for (i = 0; i < entries - 1; i++) {
181 /* path/file always comes after path because of the way
182 * the cache is sorted. Also path can appear only once,
183 * which means conflicting one would immediately follow.
184 */
185 const char *this_name = cache[i]->name;
186 const char *next_name = cache[i+1]->name;
187 int this_len = strlen(this_name);
188 if (this_len < strlen(next_name) &&
189 strncmp(this_name, next_name, this_len) == 0 &&
190 next_name[this_len] == '/') {
191 if (10 < ++funny) {
192 fprintf(stderr, "...\n");
193 break;
194 }
195 fprintf(stderr, "You have both %s and %s\n",
196 this_name, next_name);
197 }
198 }
199 if (funny)
200 return -1;
201 return 0;
202}
203
204static void discard_unused_subtrees(struct cache_tree *it)
205{
206 struct cache_tree_sub **down = it->down;
207 int nr = it->subtree_nr;
208 int dst, src;
209 for (dst = src = 0; src < nr; src++) {
210 struct cache_tree_sub *s = down[src];
211 if (s->used)
212 down[dst++] = s;
213 else {
214 cache_tree_free(&s->cache_tree);
215 free(s);
216 it->subtree_nr--;
217 }
218 }
219}
220
221int cache_tree_fully_valid(struct cache_tree *it)
222{
223 int i;
224 if (!it)
225 return 0;
226 if (it->entry_count < 0 || !has_sha1_file(it->sha1))
227 return 0;
228 for (i = 0; i < it->subtree_nr; i++) {
229 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
230 return 0;
231 }
232 return 1;
233}
234
235static int update_one(struct cache_tree *it,
236 const struct cache_entry * const *cache,
237 int entries,
238 const char *base,
239 int baselen,
240 int *skip_count,
241 int flags)
242{
243 struct strbuf buffer;
244 int missing_ok = flags & WRITE_TREE_MISSING_OK;
245 int dryrun = flags & WRITE_TREE_DRY_RUN;
246 int to_invalidate = 0;
247 int i;
248
249 *skip_count = 0;
250
251 if (0 <= it->entry_count && has_sha1_file(it->sha1))
252 return it->entry_count;
253
254 /*
255 * We first scan for subtrees and update them; we start by
256 * marking existing subtrees -- the ones that are unmarked
257 * should not be in the result.
258 */
259 for (i = 0; i < it->subtree_nr; i++)
260 it->down[i]->used = 0;
261
262 /*
263 * Find the subtrees and update them.
264 */
265 i = 0;
266 while (i < entries) {
267 const struct cache_entry *ce = cache[i];
268 struct cache_tree_sub *sub;
269 const char *path, *slash;
270 int pathlen, sublen, subcnt, subskip;
271
272 path = ce->name;
273 pathlen = ce_namelen(ce);
274 if (pathlen <= baselen || memcmp(base, path, baselen))
275 break; /* at the end of this level */
276
277 slash = strchr(path + baselen, '/');
278 if (!slash) {
279 i++;
280 continue;
281 }
282 /*
283 * a/bbb/c (base = a/, slash = /c)
284 * ==>
285 * path+baselen = bbb/c, sublen = 3
286 */
287 sublen = slash - (path + baselen);
288 sub = find_subtree(it, path + baselen, sublen, 1);
289 if (!sub->cache_tree)
290 sub->cache_tree = cache_tree();
291 subcnt = update_one(sub->cache_tree,
292 cache + i, entries - i,
293 path,
294 baselen + sublen + 1,
295 &subskip,
296 flags);
297 if (subcnt < 0)
298 return subcnt;
299 i += subcnt;
300 sub->count = subcnt; /* to be used in the next loop */
301 *skip_count += subskip;
302 sub->used = 1;
303 }
304
305 discard_unused_subtrees(it);
306
307 /*
308 * Then write out the tree object for this level.
309 */
310 strbuf_init(&buffer, 8192);
311
312 i = 0;
313 while (i < entries) {
314 const struct cache_entry *ce = cache[i];
315 struct cache_tree_sub *sub;
316 const char *path, *slash;
317 int pathlen, entlen;
318 const unsigned char *sha1;
319 unsigned mode;
320
321 path = ce->name;
322 pathlen = ce_namelen(ce);
323 if (pathlen <= baselen || memcmp(base, path, baselen))
324 break; /* at the end of this level */
325
326 slash = strchr(path + baselen, '/');
327 if (slash) {
328 entlen = slash - (path + baselen);
329 sub = find_subtree(it, path + baselen, entlen, 0);
330 if (!sub)
331 die("cache-tree.c: '%.*s' in '%s' not found",
332 entlen, path + baselen, path);
333 i += sub->count;
334 sha1 = sub->cache_tree->sha1;
335 mode = S_IFDIR;
336 if (sub->cache_tree->entry_count < 0)
337 to_invalidate = 1;
338 }
339 else {
340 sha1 = ce->sha1;
341 mode = ce->ce_mode;
342 entlen = pathlen - baselen;
343 i++;
344 }
345 if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
346 strbuf_release(&buffer);
347 return error("invalid object %06o %s for '%.*s'",
348 mode, sha1_to_hex(sha1), entlen+baselen, path);
349 }
350
351 /*
352 * CE_REMOVE entries are removed before the index is
353 * written to disk. Skip them to remain consistent
354 * with the future on-disk index.
355 */
356 if (ce->ce_flags & CE_REMOVE) {
357 *skip_count = *skip_count + 1;
358 continue;
359 }
360
361 /*
362 * CE_INTENT_TO_ADD entries exist on on-disk index but
363 * they are not part of generated trees. Invalidate up
364 * to root to force cache-tree users to read elsewhere.
365 */
366 if (ce->ce_flags & CE_INTENT_TO_ADD) {
367 to_invalidate = 1;
368 continue;
369 }
370
371 strbuf_grow(&buffer, entlen + 100);
372 strbuf_addf(&buffer, "%o %.*s%c", mode, entlen, path + baselen, '\0');
373 strbuf_add(&buffer, sha1, 20);
374
375#if DEBUG
376 fprintf(stderr, "cache-tree update-one %o %.*s\n",
377 mode, entlen, path + baselen);
378#endif
379 }
380
381 if (dryrun)
382 hash_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1);
383 else if (write_sha1_file(buffer.buf, buffer.len, tree_type, it->sha1)) {
384 strbuf_release(&buffer);
385 return -1;
386 }
387
388 strbuf_release(&buffer);
389 it->entry_count = to_invalidate ? -1 : i - *skip_count;
390#if DEBUG
391 fprintf(stderr, "cache-tree update-one (%d ent, %d subtree) %s\n",
392 it->entry_count, it->subtree_nr,
393 sha1_to_hex(it->sha1));
394#endif
395 return i;
396}
397
398int cache_tree_update(struct cache_tree *it,
399 const struct cache_entry * const *cache,
400 int entries,
401 int flags)
402{
403 int i, skip;
404 i = verify_cache(cache, entries, flags);
405 if (i)
406 return i;
407 i = update_one(it, cache, entries, "", 0, &skip, flags);
408 if (i < 0)
409 return i;
410 return 0;
411}
412
413static void write_one(struct strbuf *buffer, struct cache_tree *it,
414 const char *path, int pathlen)
415{
416 int i;
417
418 /* One "cache-tree" entry consists of the following:
419 * path (NUL terminated)
420 * entry_count, subtree_nr ("%d %d\n")
421 * tree-sha1 (missing if invalid)
422 * subtree_nr "cache-tree" entries for subtrees.
423 */
424 strbuf_grow(buffer, pathlen + 100);
425 strbuf_add(buffer, path, pathlen);
426 strbuf_addf(buffer, "%c%d %d\n", 0, it->entry_count, it->subtree_nr);
427
428#if DEBUG
429 if (0 <= it->entry_count)
430 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
431 pathlen, path, it->entry_count, it->subtree_nr,
432 sha1_to_hex(it->sha1));
433 else
434 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
435 pathlen, path, it->subtree_nr);
436#endif
437
438 if (0 <= it->entry_count) {
439 strbuf_add(buffer, it->sha1, 20);
440 }
441 for (i = 0; i < it->subtree_nr; i++) {
442 struct cache_tree_sub *down = it->down[i];
443 if (i) {
444 struct cache_tree_sub *prev = it->down[i-1];
445 if (subtree_name_cmp(down->name, down->namelen,
446 prev->name, prev->namelen) <= 0)
447 die("fatal - unsorted cache subtree");
448 }
449 write_one(buffer, down->cache_tree, down->name, down->namelen);
450 }
451}
452
453void cache_tree_write(struct strbuf *sb, struct cache_tree *root)
454{
455 write_one(sb, root, "", 0);
456}
457
458static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
459{
460 const char *buf = *buffer;
461 unsigned long size = *size_p;
462 const char *cp;
463 char *ep;
464 struct cache_tree *it;
465 int i, subtree_nr;
466
467 it = NULL;
468 /* skip name, but make sure name exists */
469 while (size && *buf) {
470 size--;
471 buf++;
472 }
473 if (!size)
474 goto free_return;
475 buf++; size--;
476 it = cache_tree();
477
478 cp = buf;
479 it->entry_count = strtol(cp, &ep, 10);
480 if (cp == ep)
481 goto free_return;
482 cp = ep;
483 subtree_nr = strtol(cp, &ep, 10);
484 if (cp == ep)
485 goto free_return;
486 while (size && *buf && *buf != '\n') {
487 size--;
488 buf++;
489 }
490 if (!size)
491 goto free_return;
492 buf++; size--;
493 if (0 <= it->entry_count) {
494 if (size < 20)
495 goto free_return;
496 hashcpy(it->sha1, (const unsigned char*)buf);
497 buf += 20;
498 size -= 20;
499 }
500
501#if DEBUG
502 if (0 <= it->entry_count)
503 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
504 *buffer, it->entry_count, subtree_nr,
505 sha1_to_hex(it->sha1));
506 else
507 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
508 *buffer, subtree_nr);
509#endif
510
511 /*
512 * Just a heuristic -- we do not add directories that often but
513 * we do not want to have to extend it immediately when we do,
514 * hence +2.
515 */
516 it->subtree_alloc = subtree_nr + 2;
517 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
518 for (i = 0; i < subtree_nr; i++) {
519 /* read each subtree */
520 struct cache_tree *sub;
521 struct cache_tree_sub *subtree;
522 const char *name = buf;
523
524 sub = read_one(&buf, &size);
525 if (!sub)
526 goto free_return;
527 subtree = cache_tree_sub(it, name);
528 subtree->cache_tree = sub;
529 }
530 if (subtree_nr != it->subtree_nr)
531 die("cache-tree: internal error");
532 *buffer = buf;
533 *size_p = size;
534 return it;
535
536 free_return:
537 cache_tree_free(&it);
538 return NULL;
539}
540
541struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
542{
543 if (buffer[0])
544 return NULL; /* not the whole tree */
545 return read_one(&buffer, &size);
546}
547
548static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
549{
550 if (!it)
551 return NULL;
552 while (*path) {
553 const char *slash;
554 struct cache_tree_sub *sub;
555
556 slash = strchrnul(path, '/');
557 /* between path and slash is the name of the
558 * subtree to look for.
559 */
560 sub = find_subtree(it, path, slash - path, 0);
561 if (!sub)
562 return NULL;
563 it = sub->cache_tree;
564 if (*slash)
565 while (*slash && *slash == '/')
566 slash++;
567 if (!*slash)
568 return it; /* prefix ended with slashes */
569 path = slash;
570 }
571 return it;
572}
573
574int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix)
575{
576 int entries, was_valid, newfd;
577 struct lock_file *lock_file;
578
579 /*
580 * We can't free this memory, it becomes part of a linked list
581 * parsed atexit()
582 */
583 lock_file = xcalloc(1, sizeof(struct lock_file));
584
585 newfd = hold_locked_index(lock_file, 1);
586
587 entries = read_cache();
588 if (entries < 0)
589 return WRITE_TREE_UNREADABLE_INDEX;
590 if (flags & WRITE_TREE_IGNORE_CACHE_TREE)
591 cache_tree_free(&(active_cache_tree));
592
593 if (!active_cache_tree)
594 active_cache_tree = cache_tree();
595
596 was_valid = cache_tree_fully_valid(active_cache_tree);
597 if (!was_valid) {
598 if (cache_tree_update(active_cache_tree,
599 (const struct cache_entry * const *)active_cache,
600 active_nr, flags) < 0)
601 return WRITE_TREE_UNMERGED_INDEX;
602 if (0 <= newfd) {
603 if (!write_cache(newfd, active_cache, active_nr) &&
604 !commit_lock_file(lock_file))
605 newfd = -1;
606 }
607 /* Not being able to write is fine -- we are only interested
608 * in updating the cache-tree part, and if the next caller
609 * ends up using the old index with unupdated cache-tree part
610 * it misses the work we did here, but that is just a
611 * performance penalty and not a big deal.
612 */
613 }
614
615 if (prefix) {
616 struct cache_tree *subtree =
617 cache_tree_find(active_cache_tree, prefix);
618 if (!subtree)
619 return WRITE_TREE_PREFIX_ERROR;
620 hashcpy(sha1, subtree->sha1);
621 }
622 else
623 hashcpy(sha1, active_cache_tree->sha1);
624
625 if (0 <= newfd)
626 rollback_lock_file(lock_file);
627
628 return 0;
629}
630
631static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
632{
633 struct tree_desc desc;
634 struct name_entry entry;
635 int cnt;
636
637 hashcpy(it->sha1, tree->object.sha1);
638 init_tree_desc(&desc, tree->buffer, tree->size);
639 cnt = 0;
640 while (tree_entry(&desc, &entry)) {
641 if (!S_ISDIR(entry.mode))
642 cnt++;
643 else {
644 struct cache_tree_sub *sub;
645 struct tree *subtree = lookup_tree(entry.sha1);
646 if (!subtree->object.parsed)
647 parse_tree(subtree);
648 sub = cache_tree_sub(it, entry.path);
649 sub->cache_tree = cache_tree();
650 prime_cache_tree_rec(sub->cache_tree, subtree);
651 cnt += sub->cache_tree->entry_count;
652 }
653 }
654 it->entry_count = cnt;
655}
656
657void prime_cache_tree(struct cache_tree **it, struct tree *tree)
658{
659 cache_tree_free(it);
660 *it = cache_tree();
661 prime_cache_tree_rec(*it, tree);
662}
663
664/*
665 * find the cache_tree that corresponds to the current level without
666 * exploding the full path into textual form. The root of the
667 * cache tree is given as "root", and our current level is "info".
668 * (1) When at root level, info->prev is NULL, so it is "root" itself.
669 * (2) Otherwise, find the cache_tree that corresponds to one level
670 * above us, and find ourselves in there.
671 */
672static struct cache_tree *find_cache_tree_from_traversal(struct cache_tree *root,
673 struct traverse_info *info)
674{
675 struct cache_tree *our_parent;
676
677 if (!info->prev)
678 return root;
679 our_parent = find_cache_tree_from_traversal(root, info->prev);
680 return cache_tree_find(our_parent, info->name.path);
681}
682
683int cache_tree_matches_traversal(struct cache_tree *root,
684 struct name_entry *ent,
685 struct traverse_info *info)
686{
687 struct cache_tree *it;
688
689 it = find_cache_tree_from_traversal(root, info);
690 it = cache_tree_find(it, ent->path);
691 if (it && it->entry_count > 0 && !hashcmp(ent->sha1, it->sha1))
692 return it->entry_count;
693 return 0;
694}
695
696int update_main_cache_tree(int flags)
697{
698 if (!the_index.cache_tree)
699 the_index.cache_tree = cache_tree();
700 return cache_tree_update(the_index.cache_tree,
701 (const struct cache_entry * const *)the_index.cache,
702 the_index.cache_nr, flags);
703}