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++)
22 cache_tree_free(&it->down[i]->cache_tree);
28 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
34 struct cache_tree_sub *down;
35 for (i = 0; i < it->subtree_nr; i++) {
37 if (down->namelen == pathlen &&
38 !memcmp(down->name, path, pathlen))
43 if (it->subtree_alloc <= it->subtree_nr) {
44 it->subtree_alloc = alloc_nr(it->subtree_alloc);
45 it->down = xrealloc(it->down, it->subtree_alloc *
48 down = xmalloc(sizeof(*down) + pathlen + 1);
49 down->cache_tree = NULL; /* cache_tree(); */
50 down->namelen = pathlen;
51 memcpy(down->name, path, pathlen);
52 down->name[pathlen] = 0; /* not strictly needed */
53 it->down[it->subtree_nr++] = down;
57 void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
61 * ==> find "a", have it invalidate "b/c"
64 * ==> if "a" exists as a subtree, remove it.
68 struct cache_tree_sub *down;
72 slash = strchr(path, '/');
76 namelen = strlen(path);
77 for (i = 0; i < it->subtree_nr; i++) {
78 if (it->down[i]->namelen == namelen &&
79 !memcmp(it->down[i]->name, path, namelen))
82 if (i < it->subtree_nr) {
83 cache_tree_free(&it->down[i]->cache_tree);
88 * move 4 and 5 up one place (2 entries)
89 * 2 = 6 - 3 - 1 = subtree_nr - i - 1
91 memmove(it->down+i, it->down+i+1,
92 sizeof(struct cache_tree_sub *) *
93 (it->subtree_nr - i - 1));
98 namelen = slash - path;
99 down = find_subtree(it, path, namelen, 0);
101 cache_tree_invalidate_path(down->cache_tree, slash + 1);
104 static int verify_cache(struct cache_entry **cache,
109 /* Verify that the tree is merged */
111 for (i = 0; i < entries; i++) {
112 struct cache_entry *ce = cache[i];
115 fprintf(stderr, "...\n");
118 fprintf(stderr, "%s: unmerged (%s)\n",
119 ce->name, sha1_to_hex(ce->sha1));
125 /* Also verify that the cache does not have path and path/file
126 * at the same time. At this point we know the cache has only
130 for (i = 0; i < entries - 1; i++) {
131 /* path/file always comes after path because of the way
132 * the cache is sorted. Also path can appear only once,
133 * which means conflicting one would immediately follow.
135 const char *this_name = cache[i]->name;
136 const char *next_name = cache[i+1]->name;
137 int this_len = strlen(this_name);
138 if (this_len < strlen(next_name) &&
139 strncmp(this_name, next_name, this_len) == 0 &&
140 next_name[this_len] == '/') {
142 fprintf(stderr, "...\n");
145 fprintf(stderr, "You have both %s and %s\n",
146 this_name, next_name);
154 static void discard_unused_subtrees(struct cache_tree *it)
156 struct cache_tree_sub **down = it->down;
157 int nr = it->subtree_nr;
159 for (dst = src = 0; src < nr; src++) {
160 struct cache_tree_sub *s = down[src];
164 cache_tree_free(&s->cache_tree);
171 int cache_tree_fully_valid(struct cache_tree *it)
176 if (it->entry_count < 0 || !has_sha1_file(it->sha1))
178 for (i = 0; i < it->subtree_nr; i++) {
179 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
185 static int update_one(struct cache_tree *it,
186 struct cache_entry **cache,
192 unsigned long size, offset;
196 if (0 <= it->entry_count && has_sha1_file(it->sha1))
197 return it->entry_count;
200 * We first scan for subtrees and update them; we start by
201 * marking existing subtrees -- the ones that are unmarked
202 * should not be in the result.
204 for (i = 0; i < it->subtree_nr; i++)
205 it->down[i]->used = 0;
208 * Find the subtrees and update them.
210 for (i = 0; i < entries; i++) {
211 struct cache_entry *ce = cache[i];
212 struct cache_tree_sub *sub;
213 const char *path, *slash;
214 int pathlen, sublen, subcnt;
217 pathlen = ce_namelen(ce);
218 if (pathlen <= baselen || memcmp(base, path, baselen))
219 break; /* at the end of this level */
221 slash = strchr(path + baselen, '/');
225 * a/bbb/c (base = a/, slash = /c)
227 * path+baselen = bbb/c, sublen = 3
229 sublen = slash - (path + baselen);
230 sub = find_subtree(it, path + baselen, sublen, 1);
231 if (!sub->cache_tree)
232 sub->cache_tree = cache_tree();
233 subcnt = update_one(sub->cache_tree,
234 cache + i, entries - i,
236 baselen + sublen + 1,
242 discard_unused_subtrees(it);
245 * Then write out the tree object for this level.
248 buffer = xmalloc(size);
251 for (i = 0; i < entries; i++) {
252 struct cache_entry *ce = cache[i];
253 struct cache_tree_sub *sub;
254 const char *path, *slash;
256 const unsigned char *sha1;
260 pathlen = ce_namelen(ce);
261 if (pathlen <= baselen || memcmp(base, path, baselen))
262 break; /* at the end of this level */
264 slash = strchr(path + baselen, '/');
266 entlen = slash - (path + baselen);
267 sub = find_subtree(it, path + baselen, entlen, 0);
269 die("cache-tree.c: '%.*s' in '%s' not found",
270 entlen, path + baselen, path);
271 i += sub->cache_tree->entry_count - 1;
272 sha1 = sub->cache_tree->sha1;
277 mode = ntohl(ce->ce_mode);
278 entlen = pathlen - baselen;
280 if (!missing_ok && !has_sha1_file(sha1))
281 return error("invalid object %s", sha1_to_hex(sha1));
284 continue; /* entry being removed */
286 if (size < offset + entlen + 100) {
287 size = alloc_nr(offset + entlen + 100);
288 buffer = xrealloc(buffer, size);
290 offset += sprintf(buffer + offset,
291 "%o %.*s", mode, entlen, path + baselen);
292 buffer[offset++] = 0;
293 memcpy(buffer + offset, sha1, 20);
297 fprintf(stderr, "cache-tree %o %.*s\n",
298 mode, entlen, path + baselen);
302 write_sha1_file(buffer, offset, tree_type, it->sha1);
306 fprintf(stderr, "cache-tree (%d ent, %d subtree) %s\n",
307 it->entry_count, it->subtree_nr,
308 sha1_to_hex(it->sha1));
313 int cache_tree_update(struct cache_tree *it,
314 struct cache_entry **cache,
319 i = verify_cache(cache, entries);
322 i = update_one(it, cache, entries, "", 0, missing_ok);
328 static void *write_one(struct cache_tree *it,
333 unsigned long *offset)
337 /* One "cache-tree" entry consists of the following:
338 * path (NUL terminated)
339 * entry_count, subtree_nr ("%d %d\n")
340 * tree-sha1 (missing if invalid)
341 * subtree_nr "cache-tree" entries for subtrees.
343 if (*size < *offset + pathlen + 100) {
344 *size = alloc_nr(*offset + pathlen + 100);
345 buffer = xrealloc(buffer, *size);
347 *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n",
349 it->entry_count, it->subtree_nr);
352 if (0 <= it->entry_count)
353 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
354 pathlen, path, it->entry_count, it->subtree_nr,
355 sha1_to_hex(it->sha1));
357 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
358 pathlen, path, it->subtree_nr);
361 if (0 <= it->entry_count) {
362 memcpy(buffer + *offset, it->sha1, 20);
365 for (i = 0; i < it->subtree_nr; i++) {
366 struct cache_tree_sub *down = it->down[i];
367 buffer = write_one(down->cache_tree, down->name, down->namelen,
368 buffer, size, offset);
373 void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
376 unsigned long size = 8192;
377 char *buffer = xmalloc(size);
381 return write_one(root, path, 0, buffer, &size, size_p);
384 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
386 const char *buf = *buffer;
387 unsigned long size = *size_p;
388 struct cache_tree *it;
392 /* skip name, but make sure name exists */
393 while (size && *buf) {
401 if (sscanf(buf, "%d %d\n", &it->entry_count, &subtree_nr) != 2)
403 while (size && *buf && *buf != '\n') {
410 if (0 <= it->entry_count) {
413 memcpy(it->sha1, buf, 20);
419 if (0 <= it->entry_count)
420 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
421 *buffer, it->entry_count, subtree_nr,
422 sha1_to_hex(it->sha1));
424 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
425 *buffer, subtree_nr);
429 * Just a heuristic -- we do not add directories that often but
430 * we do not want to have to extend it immediately when we do,
433 it->subtree_alloc = subtree_nr + 2;
434 it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
435 for (i = 0; i < subtree_nr; i++) {
436 /* read each subtree */
437 struct cache_tree *sub;
438 const char *name = buf;
440 sub = read_one(&buf, &size);
443 namelen = strlen(name);
444 it->down[i] = find_subtree(it, name, namelen, 1);
445 it->down[i]->cache_tree = sub;
447 if (subtree_nr != it->subtree_nr)
448 die("cache-tree: internal error");
454 cache_tree_free(&it);
458 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
461 return NULL; /* not the whole tree */
462 return read_one(&buffer, &size);