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 struct cache_tree_sub
*cache_tree_sub(struct cache_tree
*it
, const char *path
)
96 int pathlen
= strlen(path
);
97 return find_subtree(it
, path
, pathlen
, 1);
100 void cache_tree_invalidate_path(struct cache_tree
*it
, const char *path
)
103 * ==> invalidate self
104 * ==> find "a", have it invalidate "b/c"
106 * ==> invalidate self
107 * ==> if "a" exists as a subtree, remove it.
111 struct cache_tree_sub
*down
;
114 fprintf(stderr
, "cache-tree invalidate <%s>\n", path
);
119 slash
= strchr(path
, '/');
120 it
->entry_count
= -1;
123 namelen
= strlen(path
);
124 pos
= subtree_pos(it
, path
, namelen
);
126 cache_tree_free(&it
->down
[pos
]->cache_tree
);
131 * move 4 and 5 up one place (2 entries)
132 * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
134 memmove(it
->down
+pos
, it
->down
+pos
+1,
135 sizeof(struct cache_tree_sub
*) *
136 (it
->subtree_nr
- pos
- 1));
141 namelen
= slash
- path
;
142 down
= find_subtree(it
, path
, namelen
, 0);
144 cache_tree_invalidate_path(down
->cache_tree
, slash
+ 1);
147 static int verify_cache(struct cache_entry
**cache
,
152 /* Verify that the tree is merged */
154 for (i
= 0; i
< entries
; i
++) {
155 struct cache_entry
*ce
= cache
[i
];
158 fprintf(stderr
, "...\n");
161 fprintf(stderr
, "%s: unmerged (%s)\n",
162 ce
->name
, sha1_to_hex(ce
->sha1
));
168 /* Also verify that the cache does not have path and path/file
169 * at the same time. At this point we know the cache has only
173 for (i
= 0; i
< entries
- 1; i
++) {
174 /* path/file always comes after path because of the way
175 * the cache is sorted. Also path can appear only once,
176 * which means conflicting one would immediately follow.
178 const char *this_name
= cache
[i
]->name
;
179 const char *next_name
= cache
[i
+1]->name
;
180 int this_len
= strlen(this_name
);
181 if (this_len
< strlen(next_name
) &&
182 strncmp(this_name
, next_name
, this_len
) == 0 &&
183 next_name
[this_len
] == '/') {
185 fprintf(stderr
, "...\n");
188 fprintf(stderr
, "You have both %s and %s\n",
189 this_name
, next_name
);
197 static void discard_unused_subtrees(struct cache_tree
*it
)
199 struct cache_tree_sub
**down
= it
->down
;
200 int nr
= it
->subtree_nr
;
202 for (dst
= src
= 0; src
< nr
; src
++) {
203 struct cache_tree_sub
*s
= down
[src
];
207 cache_tree_free(&s
->cache_tree
);
214 int cache_tree_fully_valid(struct cache_tree
*it
)
219 if (it
->entry_count
< 0 || !has_sha1_file(it
->sha1
))
221 for (i
= 0; i
< it
->subtree_nr
; i
++) {
222 if (!cache_tree_fully_valid(it
->down
[i
]->cache_tree
))
228 static int update_one(struct cache_tree
*it
,
229 struct cache_entry
**cache
,
236 unsigned long size
, offset
;
240 if (0 <= it
->entry_count
&& has_sha1_file(it
->sha1
))
241 return it
->entry_count
;
244 * We first scan for subtrees and update them; we start by
245 * marking existing subtrees -- the ones that are unmarked
246 * should not be in the result.
248 for (i
= 0; i
< it
->subtree_nr
; i
++)
249 it
->down
[i
]->used
= 0;
252 * Find the subtrees and update them.
254 for (i
= 0; i
< entries
; i
++) {
255 struct cache_entry
*ce
= cache
[i
];
256 struct cache_tree_sub
*sub
;
257 const char *path
, *slash
;
258 int pathlen
, sublen
, subcnt
;
261 pathlen
= ce_namelen(ce
);
262 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
263 break; /* at the end of this level */
265 slash
= strchr(path
+ baselen
, '/');
269 * a/bbb/c (base = a/, slash = /c)
271 * path+baselen = bbb/c, sublen = 3
273 sublen
= slash
- (path
+ baselen
);
274 sub
= find_subtree(it
, path
+ baselen
, sublen
, 1);
275 if (!sub
->cache_tree
)
276 sub
->cache_tree
= cache_tree();
277 subcnt
= update_one(sub
->cache_tree
,
278 cache
+ i
, entries
- i
,
280 baselen
+ sublen
+ 1,
287 discard_unused_subtrees(it
);
290 * Then write out the tree object for this level.
293 buffer
= xmalloc(size
);
296 for (i
= 0; i
< entries
; i
++) {
297 struct cache_entry
*ce
= cache
[i
];
298 struct cache_tree_sub
*sub
;
299 const char *path
, *slash
;
301 const unsigned char *sha1
;
305 pathlen
= ce_namelen(ce
);
306 if (pathlen
<= baselen
|| memcmp(base
, path
, baselen
))
307 break; /* at the end of this level */
309 slash
= strchr(path
+ baselen
, '/');
311 entlen
= slash
- (path
+ baselen
);
312 sub
= find_subtree(it
, path
+ baselen
, entlen
, 0);
314 die("cache-tree.c: '%.*s' in '%s' not found",
315 entlen
, path
+ baselen
, path
);
316 i
+= sub
->cache_tree
->entry_count
- 1;
317 sha1
= sub
->cache_tree
->sha1
;
322 mode
= ntohl(ce
->ce_mode
);
323 entlen
= pathlen
- baselen
;
325 if (!missing_ok
&& !has_sha1_file(sha1
))
326 return error("invalid object %s", sha1_to_hex(sha1
));
329 continue; /* entry being removed */
331 if (size
< offset
+ entlen
+ 100) {
332 size
= alloc_nr(offset
+ entlen
+ 100);
333 buffer
= xrealloc(buffer
, size
);
335 offset
+= sprintf(buffer
+ offset
,
336 "%o %.*s", mode
, entlen
, path
+ baselen
);
337 buffer
[offset
++] = 0;
338 memcpy(buffer
+ offset
, sha1
, 20);
342 fprintf(stderr
, "cache-tree update-one %o %.*s\n",
343 mode
, entlen
, path
+ baselen
);
348 unsigned char hdr
[200];
350 write_sha1_file_prepare(buffer
, offset
, tree_type
, it
->sha1
,
354 write_sha1_file(buffer
, offset
, tree_type
, it
->sha1
);
358 fprintf(stderr
, "cache-tree update-one (%d ent, %d subtree) %s\n",
359 it
->entry_count
, it
->subtree_nr
,
360 sha1_to_hex(it
->sha1
));
365 int cache_tree_update(struct cache_tree
*it
,
366 struct cache_entry
**cache
,
372 i
= verify_cache(cache
, entries
);
375 i
= update_one(it
, cache
, entries
, "", 0, missing_ok
, dryrun
);
381 static void *write_one(struct cache_tree
*it
,
386 unsigned long *offset
)
390 /* One "cache-tree" entry consists of the following:
391 * path (NUL terminated)
392 * entry_count, subtree_nr ("%d %d\n")
393 * tree-sha1 (missing if invalid)
394 * subtree_nr "cache-tree" entries for subtrees.
396 if (*size
< *offset
+ pathlen
+ 100) {
397 *size
= alloc_nr(*offset
+ pathlen
+ 100);
398 buffer
= xrealloc(buffer
, *size
);
400 *offset
+= sprintf(buffer
+ *offset
, "%.*s%c%d %d\n",
402 it
->entry_count
, it
->subtree_nr
);
405 if (0 <= it
->entry_count
)
406 fprintf(stderr
, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
407 pathlen
, path
, it
->entry_count
, it
->subtree_nr
,
408 sha1_to_hex(it
->sha1
));
410 fprintf(stderr
, "cache-tree <%.*s> (%d subtree) invalid\n",
411 pathlen
, path
, it
->subtree_nr
);
414 if (0 <= it
->entry_count
) {
415 memcpy(buffer
+ *offset
, it
->sha1
, 20);
418 for (i
= 0; i
< it
->subtree_nr
; i
++) {
419 struct cache_tree_sub
*down
= it
->down
[i
];
421 struct cache_tree_sub
*prev
= it
->down
[i
-1];
422 if (subtree_name_cmp(down
->name
, down
->namelen
,
423 prev
->name
, prev
->namelen
) <= 0)
424 die("fatal - unsorted cache subtree");
426 buffer
= write_one(down
->cache_tree
, down
->name
, down
->namelen
,
427 buffer
, size
, offset
);
432 void *cache_tree_write(struct cache_tree
*root
, unsigned long *size_p
)
435 unsigned long size
= 8192;
436 char *buffer
= xmalloc(size
);
440 return write_one(root
, path
, 0, buffer
, &size
, size_p
);
443 static struct cache_tree
*read_one(const char **buffer
, unsigned long *size_p
)
445 const char *buf
= *buffer
;
446 unsigned long size
= *size_p
;
449 struct cache_tree
*it
;
453 /* skip name, but make sure name exists */
454 while (size
&& *buf
) {
464 it
->entry_count
= strtol(cp
, &ep
, 10);
468 subtree_nr
= strtol(cp
, &ep
, 10);
471 while (size
&& *buf
&& *buf
!= '\n') {
478 if (0 <= it
->entry_count
) {
481 memcpy(it
->sha1
, buf
, 20);
487 if (0 <= it
->entry_count
)
488 fprintf(stderr
, "cache-tree <%s> (%d ent, %d subtree) %s\n",
489 *buffer
, it
->entry_count
, subtree_nr
,
490 sha1_to_hex(it
->sha1
));
492 fprintf(stderr
, "cache-tree <%s> (%d subtrees) invalid\n",
493 *buffer
, subtree_nr
);
497 * Just a heuristic -- we do not add directories that often but
498 * we do not want to have to extend it immediately when we do,
501 it
->subtree_alloc
= subtree_nr
+ 2;
502 it
->down
= xcalloc(it
->subtree_alloc
, sizeof(struct cache_tree_sub
*));
503 for (i
= 0; i
< subtree_nr
; i
++) {
504 /* read each subtree */
505 struct cache_tree
*sub
;
506 struct cache_tree_sub
*subtree
;
507 const char *name
= buf
;
509 sub
= read_one(&buf
, &size
);
512 subtree
= cache_tree_sub(it
, name
);
513 subtree
->cache_tree
= sub
;
515 if (subtree_nr
!= it
->subtree_nr
)
516 die("cache-tree: internal error");
522 cache_tree_free(&it
);
526 struct cache_tree
*cache_tree_read(const char *buffer
, unsigned long size
)
529 return NULL
; /* not the whole tree */
530 return read_one(&buffer
, &size
);
533 struct cache_tree
*cache_tree_find(struct cache_tree
*it
, const char *path
)
537 struct cache_tree_sub
*sub
;
539 slash
= strchr(path
, '/');
541 slash
= path
+ strlen(path
);
542 /* between path and slash is the name of the
543 * subtree to look for.
545 sub
= find_subtree(it
, path
, slash
- path
, 0);
548 it
= sub
->cache_tree
;
550 while (*slash
&& *slash
== '/')
552 if (!slash
|| !*slash
)
553 return it
; /* prefix ended with slashes */