3 #include "cache-tree.h"
7 struct cache_tree *cache_tree(void)
9 struct cache_tree *it = xcalloc(1, sizeof(struct cache_tree));
14 void cache_tree_free(struct cache_tree **it_p)
17 struct cache_tree *it = *it_p;
21 for (i = 0; i < it->subtree_nr; i++)
23 cache_tree_free(&it->down[i]->cache_tree);
29 static int subtree_name_cmp(const char *one, int onelen,
30 const char *two, int twolen)
36 return memcmp(one, two, onelen);
39 static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
41 struct cache_tree_sub **down = it->down;
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);
60 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
65 struct cache_tree_sub *down;
66 int pos = subtree_pos(it, path, pathlen);
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 *
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;
86 if (pos < it->subtree_nr)
87 memmove(it->down + pos + 1,
89 sizeof(down) * (it->subtree_nr - pos - 1));
94 void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
98 * ==> find "a", have it invalidate "b/c"
100 * ==> invalidate self
101 * ==> if "a" exists as a subtree, remove it.
105 struct cache_tree_sub *down;
109 slash = strchr(path, '/');
110 it->entry_count = -1;
113 namelen = strlen(path);
114 pos = subtree_pos(it, path, namelen);
116 cache_tree_free(&it->down[pos]->cache_tree);
121 * move 4 and 5 up one place (2 entries)
122 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
124 memmove(it->down+pos, it->down+pos+1,
125 sizeof(struct cache_tree_sub *) *
126 (it->subtree_nr - pos - 1));
131 namelen = slash - path;
132 down = find_subtree(it, path, namelen, 0);
134 cache_tree_invalidate_path(down->cache_tree, slash + 1);
137 static int verify_cache(struct cache_entry **cache,
142 /* Verify that the tree is merged */
144 for (i = 0; i < entries; i++) {
145 struct cache_entry *ce = cache[i];
148 fprintf(stderr, "...\n");
151 fprintf(stderr, "%s: unmerged (%s)\n",
152 ce->name, sha1_to_hex(ce->sha1));
158 /* Also verify that the cache does not have path and path/file
159 * at the same time. At this point we know the cache has only
163 for (i = 0; i < entries - 1; i++) {
164 /* path/file always comes after path because of the way
165 * the cache is sorted. Also path can appear only once,
166 * which means conflicting one would immediately follow.
168 const char *this_name = cache[i]->name;
169 const char *next_name = cache[i+1]->name;
170 int this_len = strlen(this_name);
171 if (this_len < strlen(next_name) &&
172 strncmp(this_name, next_name, this_len) == 0 &&
173 next_name[this_len] == '/') {
175 fprintf(stderr, "...\n");
178 fprintf(stderr, "You have both %s and %s\n",
179 this_name, next_name);
187 static void discard_unused_subtrees(struct cache_tree *it)
189 struct cache_tree_sub **down = it->down;
190 int nr = it->subtree_nr;
192 for (dst = src = 0; src < nr; src++) {
193 struct cache_tree_sub *s = down[src];
197 cache_tree_free(&s->cache_tree);
204 int cache_tree_fully_valid(struct cache_tree *it)
209 if (it->entry_count < 0 || !has_sha1_file(it->sha1))
211 for (i = 0; i < it->subtree_nr; i++) {
212 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
218 static int update_one(struct cache_tree *it,
219 struct cache_entry **cache,
225 unsigned long size, offset;
229 if (0 <= it->entry_count && has_sha1_file(it->sha1))
230 return it->entry_count;
233 * We first scan for subtrees and update them; we start by
234 * marking existing subtrees -- the ones that are unmarked
235 * should not be in the result.
237 for (i = 0; i < it->subtree_nr; i++)
238 it->down[i]->used = 0;
241 * Find the subtrees and update them.
243 for (i = 0; i < entries; i++) {
244 struct cache_entry *ce = cache[i];
245 struct cache_tree_sub *sub;
246 const char *path, *slash;
247 int pathlen, sublen, subcnt;
250 pathlen = ce_namelen(ce);
251 if (pathlen <= baselen || memcmp(base, path, baselen))
252 break; /* at the end of this level */
254 slash = strchr(path + baselen, '/');
258 * a/bbb/c (base = a/, slash = /c)
260 * path+baselen = bbb/c, sublen = 3
262 sublen = slash - (path + baselen);
263 sub = find_subtree(it, path + baselen, sublen, 1);
264 if (!sub->cache_tree)
265 sub->cache_tree = cache_tree();
266 subcnt = update_one(sub->cache_tree,
267 cache + i, entries - i,
269 baselen + sublen + 1,
275 discard_unused_subtrees(it);
278 * Then write out the tree object for this level.
281 buffer = xmalloc(size);
284 for (i = 0; i < entries; i++) {
285 struct cache_entry *ce = cache[i];
286 struct cache_tree_sub *sub;
287 const char *path, *slash;
289 const unsigned char *sha1;
293 pathlen = ce_namelen(ce);
294 if (pathlen <= baselen || memcmp(base, path, baselen))
295 break; /* at the end of this level */
297 slash = strchr(path + baselen, '/');
299 entlen = slash - (path + baselen);
300 sub = find_subtree(it, path + baselen, entlen, 0);
302 die("cache-tree.c: '%.*s' in '%s' not found",
303 entlen, path + baselen, path);
304 i += sub->cache_tree->entry_count - 1;
305 sha1 = sub->cache_tree->sha1;
310 mode = ntohl(ce->ce_mode);
311 entlen = pathlen - baselen;
313 if (!missing_ok && !has_sha1_file(sha1))
314 return error("invalid object %s", sha1_to_hex(sha1));
317 continue; /* entry being removed */
319 if (size < offset + entlen + 100) {
320 size = alloc_nr(offset + entlen + 100);
321 buffer = xrealloc(buffer, size);
323 offset += sprintf(buffer + offset,
324 "%o %.*s", mode, entlen, path + baselen);
325 buffer[offset++] = 0;
326 memcpy(buffer + offset, sha1, 20);
330 fprintf(stderr, "cache-tree %o %.*s\n",
331 mode, entlen, path + baselen);
335 write_sha1_file(buffer, offset, tree_type, it->sha1);
339 fprintf(stderr, "cache-tree (%d ent, %d subtree) %s\n",
340 it->entry_count, it->subtree_nr,
341 sha1_to_hex(it->sha1));
346 int cache_tree_update(struct cache_tree *it,
347 struct cache_entry **cache,
352 i = verify_cache(cache, entries);
355 i = update_one(it, cache, entries, "", 0, missing_ok);
361 static void *write_one(struct cache_tree *it,
366 unsigned long *offset)
370 /* One "cache-tree" entry consists of the following:
371 * path (NUL terminated)
372 * entry_count, subtree_nr ("%d %d\n")
373 * tree-sha1 (missing if invalid)
374 * subtree_nr "cache-tree" entries for subtrees.
376 if (*size < *offset + pathlen + 100) {
377 *size = alloc_nr(*offset + pathlen + 100);
378 buffer = xrealloc(buffer, *size);
380 *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n",
382 it->entry_count, it->subtree_nr);
385 if (0 <= it->entry_count)
386 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
387 pathlen, path, it->entry_count, it->subtree_nr,
388 sha1_to_hex(it->sha1));
390 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
391 pathlen, path, it->subtree_nr);
394 if (0 <= it->entry_count) {
395 memcpy(buffer + *offset, it->sha1, 20);
398 for (i = 0; i < it->subtree_nr; i++) {
399 struct cache_tree_sub *down = it->down[i];
401 struct cache_tree_sub *prev = it->down[i-1];
402 if (subtree_name_cmp(down->name, down->namelen,
403 prev->name, prev->namelen) <= 0)
404 die("fatal - unsorted cache subtree");
406 buffer = write_one(down->cache_tree, down->name, down->namelen,
407 buffer, size, offset);
412 void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
415 unsigned long size = 8192;
416 char *buffer = xmalloc(size);
420 return write_one(root, path, 0, buffer, &size, size_p);
423 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
425 const char *buf = *buffer;
426 unsigned long size = *size_p;
427 struct cache_tree *it;
431 /* skip name, but make sure name exists */
432 while (size && *buf) {
440 if (sscanf(buf, "%d %d\n", &it->entry_count, &subtree_nr) != 2)
442 while (size && *buf && *buf != '\n') {
449 if (0 <= it->entry_count) {
452 memcpy(it->sha1, buf, 20);
458 if (0 <= it->entry_count)
459 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
460 *buffer, it->entry_count, subtree_nr,
461 sha1_to_hex(it->sha1));
463 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
464 *buffer, subtree_nr);
468 * Just a heuristic -- we do not add directories that often but
469 * we do not want to have to extend it immediately when we do,
472 it->subtree_alloc = subtree_nr + 2;
473 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
474 for (i = 0; i < subtree_nr; i++) {
475 /* read each subtree */
476 struct cache_tree *sub;
477 struct cache_tree_sub *subtree;
478 const char *name = buf;
480 sub = read_one(&buf, &size);
483 namelen = strlen(name);
484 subtree = find_subtree(it, name, namelen, 1);
485 subtree->cache_tree = sub;
487 if (subtree_nr != it->subtree_nr)
488 die("cache-tree: internal error");
494 cache_tree_free(&it);
498 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
501 return NULL; /* not the whole tree */
502 return read_one(&buffer, &size);