1#include "cache.h"
2#include "tree.h"
3#include "cache-tree.h"
4
5#define DEBUG 0
6
7struct cache_tree *cache_tree(void)
8{
9 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
10 it->entry_count = -1;
11 return it;
12}
13
14void cache_tree_free(struct cache_tree **it_p)
15{
16 int i;
17 struct cache_tree *it = *it_p;
18
19 if (!it)
20 return;
21 for (i = 0; i < it->subtree_nr; i++)
22 if (it->down[i])
23 cache_tree_free(&it->down[i]->cache_tree);
24 free(it->down);
25 free(it);
26 *it_p = NULL;
27}
28
29static int subtree_name_cmp(const char *one, int onelen,
30 const char *two, int twolen)
31{
32 if (onelen < twolen)
33 return -1;
34 if (twolen < onelen)
35 return 1;
36 return memcmp(one, two, onelen);
37}
38
39static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
40{
41 struct cache_tree_sub **down = it->down;
42 int lo, hi;
43 lo = 0;
44 hi = it->subtree_nr;
45 while (lo < hi) {
46 int mi = (lo + hi) / 2;
47 struct cache_tree_sub *mdl = down[mi];
48 int cmp = subtree_name_cmp(path, pathlen,
49 mdl->name, mdl->namelen);
50 if (!cmp)
51 return mi;
52 if (cmp < 0)
53 hi = mi;
54 else
55 lo = mi + 1;
56 }
57 return -lo-1;
58}
59
60static struct cache_tree_sub *find_subtree(struct cache_tree *it,
61 const char *path,
62 int pathlen,
63 int create)
64{
65 struct cache_tree_sub *down;
66 int pos = subtree_pos(it, path, pathlen);
67 if (0 <= pos)
68 return it->down[pos];
69 if (!create)
70 return NULL;
71
72 pos = -pos-1;
73 if (it->subtree_alloc <= it->subtree_nr) {
74 it->subtree_alloc = alloc_nr(it->subtree_alloc);
75 it->down = xrealloc(it->down, it->subtree_alloc *
76 sizeof(*it->down));
77 }
78 it->subtree_nr++;
79
80 down = xmalloc(sizeof(*down) + pathlen + 1);
81 down->cache_tree = NULL;
82 down->namelen = pathlen;
83 memcpy(down->name, path, pathlen);
84 down->name[pathlen] = 0;
85
86 if (pos < it->subtree_nr)
87 memmove(it->down + pos + 1,
88 it->down + pos,
89 sizeof(down) * (it->subtree_nr - pos - 1));
90 it->down[pos] = down;
91 return down;
92}
93
94struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)
95{
96 int pathlen = strlen(path);
97 return find_subtree(it, path, pathlen, 1);
98}
99
100void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
101{
102 /* a/b/c
103 * ==> invalidate self
104 * ==> find "a", have it invalidate "b/c"
105 * a
106 * ==> invalidate self
107 * ==> if "a" exists as a subtree, remove it.
108 */
109 const char *slash;
110 int namelen;
111 struct cache_tree_sub *down;
112
113 if (!it)
114 return;
115 slash = strchr(path, '/');
116 it->entry_count = -1;
117 if (!slash) {
118 int pos;
119 namelen = strlen(path);
120 pos = subtree_pos(it, path, namelen);
121 if (0 <= pos) {
122 cache_tree_free(&it->down[pos]->cache_tree);
123 free(it->down[pos]);
124 /* 0 1 2 3 4 5
125 * ^ ^subtree_nr = 6
126 * pos
127 * move 4 and 5 up one place (2 entries)
128 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
129 */
130 memmove(it->down+pos, it->down+pos+1,
131 sizeof(struct cache_tree_sub *) *
132 (it->subtree_nr - pos - 1));
133 it->subtree_nr--;
134 }
135 return;
136 }
137 namelen = slash - path;
138 down = find_subtree(it, path, namelen, 0);
139 if (down)
140 cache_tree_invalidate_path(down->cache_tree, slash + 1);
141}
142
143static int verify_cache(struct cache_entry **cache,
144 int entries)
145{
146 int i, funny;
147
148 /* Verify that the tree is merged */
149 funny = 0;
150 for (i = 0; i < entries; i++) {
151 struct cache_entry *ce = cache[i];
152 if (ce_stage(ce)) {
153 if (10 < ++funny) {
154 fprintf(stderr, "...\n");
155 break;
156 }
157 fprintf(stderr, "%s: unmerged (%s)\n",
158 ce->name, sha1_to_hex(ce->sha1));
159 }
160 }
161 if (funny)
162 return -1;
163
164 /* Also verify that the cache does not have path and path/file
165 * at the same time. At this point we know the cache has only
166 * stage 0 entries.
167 */
168 funny = 0;
169 for (i = 0; i < entries - 1; i++) {
170 /* path/file always comes after path because of the way
171 * the cache is sorted. Also path can appear only once,
172 * which means conflicting one would immediately follow.
173 */
174 const char *this_name = cache[i]->name;
175 const char *next_name = cache[i+1]->name;
176 int this_len = strlen(this_name);
177 if (this_len < strlen(next_name) &&
178 strncmp(this_name, next_name, this_len) == 0 &&
179 next_name[this_len] == '/') {
180 if (10 < ++funny) {
181 fprintf(stderr, "...\n");
182 break;
183 }
184 fprintf(stderr, "You have both %s and %s\n",
185 this_name, next_name);
186 }
187 }
188 if (funny)
189 return -1;
190 return 0;
191}
192
193static void discard_unused_subtrees(struct cache_tree *it)
194{
195 struct cache_tree_sub **down = it->down;
196 int nr = it->subtree_nr;
197 int dst, src;
198 for (dst = src = 0; src < nr; src++) {
199 struct cache_tree_sub *s = down[src];
200 if (s->used)
201 down[dst++] = s;
202 else {
203 cache_tree_free(&s->cache_tree);
204 free(s);
205 it->subtree_nr--;
206 }
207 }
208}
209
210int cache_tree_fully_valid(struct cache_tree *it)
211{
212 int i;
213 if (!it)
214 return 0;
215 if (it->entry_count < 0 || !has_sha1_file(it->sha1))
216 return 0;
217 for (i = 0; i < it->subtree_nr; i++) {
218 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
219 return 0;
220 }
221 return 1;
222}
223
224static int update_one(struct cache_tree *it,
225 struct cache_entry **cache,
226 int entries,
227 const char *base,
228 int baselen,
229 int missing_ok)
230{
231 unsigned long size, offset;
232 char *buffer;
233 int i;
234
235 if (0 <= it->entry_count && has_sha1_file(it->sha1))
236 return it->entry_count;
237
238 /*
239 * We first scan for subtrees and update them; we start by
240 * marking existing subtrees -- the ones that are unmarked
241 * should not be in the result.
242 */
243 for (i = 0; i < it->subtree_nr; i++)
244 it->down[i]->used = 0;
245
246 /*
247 * Find the subtrees and update them.
248 */
249 for (i = 0; i < entries; i++) {
250 struct cache_entry *ce = cache[i];
251 struct cache_tree_sub *sub;
252 const char *path, *slash;
253 int pathlen, sublen, subcnt;
254
255 path = ce->name;
256 pathlen = ce_namelen(ce);
257 if (pathlen <= baselen || memcmp(base, path, baselen))
258 break; /* at the end of this level */
259
260 slash = strchr(path + baselen, '/');
261 if (!slash)
262 continue;
263 /*
264 * a/bbb/c (base = a/, slash = /c)
265 * ==>
266 * path+baselen = bbb/c, sublen = 3
267 */
268 sublen = slash - (path + baselen);
269 sub = find_subtree(it, path + baselen, sublen, 1);
270 if (!sub->cache_tree)
271 sub->cache_tree = cache_tree();
272 subcnt = update_one(sub->cache_tree,
273 cache + i, entries - i,
274 path,
275 baselen + sublen + 1,
276 missing_ok);
277 i += subcnt - 1;
278 sub->used = 1;
279 }
280
281 discard_unused_subtrees(it);
282
283 /*
284 * Then write out the tree object for this level.
285 */
286 size = 8192;
287 buffer = xmalloc(size);
288 offset = 0;
289
290 for (i = 0; i < entries; i++) {
291 struct cache_entry *ce = cache[i];
292 struct cache_tree_sub *sub;
293 const char *path, *slash;
294 int pathlen, entlen;
295 const unsigned char *sha1;
296 unsigned mode;
297
298 path = ce->name;
299 pathlen = ce_namelen(ce);
300 if (pathlen <= baselen || memcmp(base, path, baselen))
301 break; /* at the end of this level */
302
303 slash = strchr(path + baselen, '/');
304 if (slash) {
305 entlen = slash - (path + baselen);
306 sub = find_subtree(it, path + baselen, entlen, 0);
307 if (!sub)
308 die("cache-tree.c: '%.*s' in '%s' not found",
309 entlen, path + baselen, path);
310 i += sub->cache_tree->entry_count - 1;
311 sha1 = sub->cache_tree->sha1;
312 mode = S_IFDIR;
313 }
314 else {
315 sha1 = ce->sha1;
316 mode = ntohl(ce->ce_mode);
317 entlen = pathlen - baselen;
318 }
319 if (!missing_ok && !has_sha1_file(sha1))
320 return error("invalid object %s", sha1_to_hex(sha1));
321
322 if (!ce->ce_mode)
323 continue; /* entry being removed */
324
325 if (size < offset + entlen + 100) {
326 size = alloc_nr(offset + entlen + 100);
327 buffer = xrealloc(buffer, size);
328 }
329 offset += sprintf(buffer + offset,
330 "%o %.*s", mode, entlen, path + baselen);
331 buffer[offset++] = 0;
332 memcpy(buffer + offset, sha1, 20);
333 offset += 20;
334
335#if DEBUG
336 fprintf(stderr, "cache-tree %o %.*s\n",
337 mode, entlen, path + baselen);
338#endif
339 }
340
341 write_sha1_file(buffer, offset, tree_type, it->sha1);
342 free(buffer);
343 it->entry_count = i;
344#if DEBUG
345 fprintf(stderr, "cache-tree (%d ent, %d subtree) %s\n",
346 it->entry_count, it->subtree_nr,
347 sha1_to_hex(it->sha1));
348#endif
349 return i;
350}
351
352int cache_tree_update(struct cache_tree *it,
353 struct cache_entry **cache,
354 int entries,
355 int missing_ok)
356{
357 int i;
358 i = verify_cache(cache, entries);
359 if (i)
360 return i;
361 i = update_one(it, cache, entries, "", 0, missing_ok);
362 if (i < 0)
363 return i;
364 return 0;
365}
366
367static void *write_one(struct cache_tree *it,
368 char *path,
369 int pathlen,
370 char *buffer,
371 unsigned long *size,
372 unsigned long *offset)
373{
374 int i;
375
376 /* One "cache-tree" entry consists of the following:
377 * path (NUL terminated)
378 * entry_count, subtree_nr ("%d %d\n")
379 * tree-sha1 (missing if invalid)
380 * subtree_nr "cache-tree" entries for subtrees.
381 */
382 if (*size < *offset + pathlen + 100) {
383 *size = alloc_nr(*offset + pathlen + 100);
384 buffer = xrealloc(buffer, *size);
385 }
386 *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n",
387 pathlen, path, 0,
388 it->entry_count, it->subtree_nr);
389
390#if DEBUG
391 if (0 <= it->entry_count)
392 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
393 pathlen, path, it->entry_count, it->subtree_nr,
394 sha1_to_hex(it->sha1));
395 else
396 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
397 pathlen, path, it->subtree_nr);
398#endif
399
400 if (0 <= it->entry_count) {
401 memcpy(buffer + *offset, it->sha1, 20);
402 *offset += 20;
403 }
404 for (i = 0; i < it->subtree_nr; i++) {
405 struct cache_tree_sub *down = it->down[i];
406 if (i) {
407 struct cache_tree_sub *prev = it->down[i-1];
408 if (subtree_name_cmp(down->name, down->namelen,
409 prev->name, prev->namelen) <= 0)
410 die("fatal - unsorted cache subtree");
411 }
412 buffer = write_one(down->cache_tree, down->name, down->namelen,
413 buffer, size, offset);
414 }
415 return buffer;
416}
417
418void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
419{
420 char path[PATH_MAX];
421 unsigned long size = 8192;
422 char *buffer = xmalloc(size);
423
424 *size_p = 0;
425 path[0] = 0;
426 return write_one(root, path, 0, buffer, &size, size_p);
427}
428
429static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
430{
431 const char *buf = *buffer;
432 unsigned long size = *size_p;
433 struct cache_tree *it;
434 int i, subtree_nr;
435
436 it = NULL;
437 /* skip name, but make sure name exists */
438 while (size && *buf) {
439 size--;
440 buf++;
441 }
442 if (!size)
443 goto free_return;
444 buf++; size--;
445 it = cache_tree();
446 if (sscanf(buf, "%d %d\n", &it->entry_count, &subtree_nr) != 2)
447 goto free_return;
448 while (size && *buf && *buf != '\n') {
449 size--;
450 buf++;
451 }
452 if (!size)
453 goto free_return;
454 buf++; size--;
455 if (0 <= it->entry_count) {
456 if (size < 20)
457 goto free_return;
458 memcpy(it->sha1, buf, 20);
459 buf += 20;
460 size -= 20;
461 }
462
463#if DEBUG
464 if (0 <= it->entry_count)
465 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
466 *buffer, it->entry_count, subtree_nr,
467 sha1_to_hex(it->sha1));
468 else
469 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
470 *buffer, subtree_nr);
471#endif
472
473 /*
474 * Just a heuristic -- we do not add directories that often but
475 * we do not want to have to extend it immediately when we do,
476 * hence +2.
477 */
478 it->subtree_alloc = subtree_nr + 2;
479 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
480 for (i = 0; i < subtree_nr; i++) {
481 /* read each subtree */
482 struct cache_tree *sub;
483 struct cache_tree_sub *subtree;
484 const char *name = buf;
485
486 sub = read_one(&buf, &size);
487 if (!sub)
488 goto free_return;
489 subtree = cache_tree_sub(it, name);
490 subtree->cache_tree = sub;
491 }
492 if (subtree_nr != it->subtree_nr)
493 die("cache-tree: internal error");
494 *buffer = buf;
495 *size_p = size;
496 return it;
497
498 free_return:
499 cache_tree_free(&it);
500 return NULL;
501}
502
503struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
504{
505 if (buffer[0])
506 return NULL; /* not the whole tree */
507 return read_one(&buffer, &size);
508}