Merge branch 'master' into jc/cache-tree
[git] / cache-tree.c
1 #include "cache.h"
2 #include "tree.h"
3 #include "cache-tree.h"
4
5 #define DEBUG 0
6
7 struct 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
14 void 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
29 static 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
39 static 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
60 static 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
94 void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
95 {
96         /* a/b/c
97          * ==> invalidate self
98          * ==> find "a", have it invalidate "b/c"
99          * a
100          * ==> invalidate self
101          * ==> if "a" exists as a subtree, remove it.
102          */
103         const char *slash;
104         int namelen;
105         struct cache_tree_sub *down;
106
107         if (!it)
108                 return;
109         slash = strchr(path, '/');
110         it->entry_count = -1;
111         if (!slash) {
112                 int pos;
113                 namelen = strlen(path);
114                 pos = subtree_pos(it, path, namelen);
115                 if (0 <= pos) {
116                         cache_tree_free(&it->down[pos]->cache_tree);
117                         free(it->down[pos]);
118                         /* 0 1 2 3 4 5
119                          *       ^     ^subtree_nr = 6
120                          *       pos
121                          * move 4 and 5 up one place (2 entries)
122                          * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
123                          */
124                         memmove(it->down+pos, it->down+pos+1,
125                                 sizeof(struct cache_tree_sub *) *
126                                 (it->subtree_nr - pos - 1));
127                         it->subtree_nr--;
128                 }
129                 return;
130         }
131         namelen = slash - path;
132         down = find_subtree(it, path, namelen, 0);
133         if (down)
134                 cache_tree_invalidate_path(down->cache_tree, slash + 1);
135 }
136
137 static int verify_cache(struct cache_entry **cache,
138                         int entries)
139 {
140         int i, funny;
141
142         /* Verify that the tree is merged */
143         funny = 0;
144         for (i = 0; i < entries; i++) {
145                 struct cache_entry *ce = cache[i];
146                 if (ce_stage(ce)) {
147                         if (10 < ++funny) {
148                                 fprintf(stderr, "...\n");
149                                 break;
150                         }
151                         fprintf(stderr, "%s: unmerged (%s)\n",
152                                 ce->name, sha1_to_hex(ce->sha1));
153                 }
154         }
155         if (funny)
156                 return -1;
157
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
160          * stage 0 entries.
161          */
162         funny = 0;
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.
167                  */
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] == '/') {
174                         if (10 < ++funny) {
175                                 fprintf(stderr, "...\n");
176                                 break;
177                         }
178                         fprintf(stderr, "You have both %s and %s\n",
179                                 this_name, next_name);
180                 }
181         }
182         if (funny)
183                 return -1;
184         return 0;
185 }
186
187 static void discard_unused_subtrees(struct cache_tree *it)
188 {
189         struct cache_tree_sub **down = it->down;
190         int nr = it->subtree_nr;
191         int dst, src;
192         for (dst = src = 0; src < nr; src++) {
193                 struct cache_tree_sub *s = down[src];
194                 if (s->used)
195                         down[dst++] = s;
196                 else {
197                         cache_tree_free(&s->cache_tree);
198                         free(s);
199                         it->subtree_nr--;
200                 }
201         }
202 }
203
204 int cache_tree_fully_valid(struct cache_tree *it)
205 {
206         int i;
207         if (!it)
208                 return 0;
209         if (it->entry_count < 0 || !has_sha1_file(it->sha1))
210                 return 0;
211         for (i = 0; i < it->subtree_nr; i++) {
212                 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
213                         return 0;
214         }
215         return 1;
216 }
217
218 static int update_one(struct cache_tree *it,
219                       struct cache_entry **cache,
220                       int entries,
221                       const char *base,
222                       int baselen,
223                       int missing_ok)
224 {
225         unsigned long size, offset;
226         char *buffer;
227         int i;
228
229         if (0 <= it->entry_count && has_sha1_file(it->sha1))
230                 return it->entry_count;
231
232         /*
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.
236          */
237         for (i = 0; i < it->subtree_nr; i++)
238                 it->down[i]->used = 0;
239
240         /*
241          * Find the subtrees and update them.
242          */
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;
248
249                 path = ce->name;
250                 pathlen = ce_namelen(ce);
251                 if (pathlen <= baselen || memcmp(base, path, baselen))
252                         break; /* at the end of this level */
253
254                 slash = strchr(path + baselen, '/');
255                 if (!slash)
256                         continue;
257                 /*
258                  * a/bbb/c (base = a/, slash = /c)
259                  * ==>
260                  * path+baselen = bbb/c, sublen = 3
261                  */
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,
268                                     path,
269                                     baselen + sublen + 1,
270                                     missing_ok);
271                 i += subcnt - 1;
272                 sub->used = 1;
273         }
274
275         discard_unused_subtrees(it);
276
277         /*
278          * Then write out the tree object for this level.
279          */
280         size = 8192;
281         buffer = xmalloc(size);
282         offset = 0;
283
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;
288                 int pathlen, entlen;
289                 const unsigned char *sha1;
290                 unsigned mode;
291
292                 path = ce->name;
293                 pathlen = ce_namelen(ce);
294                 if (pathlen <= baselen || memcmp(base, path, baselen))
295                         break; /* at the end of this level */
296
297                 slash = strchr(path + baselen, '/');
298                 if (slash) {
299                         entlen = slash - (path + baselen);
300                         sub = find_subtree(it, path + baselen, entlen, 0);
301                         if (!sub)
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;
306                         mode = S_IFDIR;
307                 }
308                 else {
309                         sha1 = ce->sha1;
310                         mode = ntohl(ce->ce_mode);
311                         entlen = pathlen - baselen;
312                 }
313                 if (!missing_ok && !has_sha1_file(sha1))
314                         return error("invalid object %s", sha1_to_hex(sha1));
315
316                 if (!ce->ce_mode)
317                         continue; /* entry being removed */
318
319                 if (size < offset + entlen + 100) {
320                         size = alloc_nr(offset + entlen + 100);
321                         buffer = xrealloc(buffer, size);
322                 }
323                 offset += sprintf(buffer + offset,
324                                   "%o %.*s", mode, entlen, path + baselen);
325                 buffer[offset++] = 0;
326                 memcpy(buffer + offset, sha1, 20);
327                 offset += 20;
328
329 #if DEBUG
330                 fprintf(stderr, "cache-tree %o %.*s\n",
331                         mode, entlen, path + baselen);
332 #endif
333         }
334
335         write_sha1_file(buffer, offset, tree_type, it->sha1);
336         free(buffer);
337         it->entry_count = i;
338 #if DEBUG
339         fprintf(stderr, "cache-tree (%d ent, %d subtree) %s\n",
340                 it->entry_count, it->subtree_nr,
341                 sha1_to_hex(it->sha1));
342 #endif
343         return i;
344 }
345
346 int cache_tree_update(struct cache_tree *it,
347                       struct cache_entry **cache,
348                       int entries,
349                       int missing_ok)
350 {
351         int i;
352         i = verify_cache(cache, entries);
353         if (i)
354                 return i;
355         i = update_one(it, cache, entries, "", 0, missing_ok);
356         if (i < 0)
357                 return i;
358         return 0;
359 }
360
361 static void *write_one(struct cache_tree *it,
362                        char *path,
363                        int pathlen,
364                        char *buffer,
365                        unsigned long *size,
366                        unsigned long *offset)
367 {
368         int i;
369
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.
375          */
376         if (*size < *offset + pathlen + 100) {
377                 *size = alloc_nr(*offset + pathlen + 100);
378                 buffer = xrealloc(buffer, *size);
379         }
380         *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n",
381                            pathlen, path, 0,
382                            it->entry_count, it->subtree_nr);
383
384 #if DEBUG
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));
389         else
390                 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
391                         pathlen, path, it->subtree_nr);
392 #endif
393
394         if (0 <= it->entry_count) {
395                 memcpy(buffer + *offset, it->sha1, 20);
396                 *offset += 20;
397         }
398         for (i = 0; i < it->subtree_nr; i++) {
399                 struct cache_tree_sub *down = it->down[i];
400                 if (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");
405                 }
406                 buffer = write_one(down->cache_tree, down->name, down->namelen,
407                                    buffer, size, offset);
408         }
409         return buffer;
410 }
411
412 void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
413 {
414         char path[PATH_MAX];
415         unsigned long size = 8192;
416         char *buffer = xmalloc(size);
417
418         *size_p = 0;
419         path[0] = 0;
420         return write_one(root, path, 0, buffer, &size, size_p);
421 }
422
423 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
424 {
425         const char *buf = *buffer;
426         unsigned long size = *size_p;
427         struct cache_tree *it;
428         int i, subtree_nr;
429
430         it = NULL;
431         /* skip name, but make sure name exists */
432         while (size && *buf) {
433                 size--;
434                 buf++;
435         }
436         if (!size)
437                 goto free_return;
438         buf++; size--;
439         it = cache_tree();
440         if (sscanf(buf, "%d %d\n", &it->entry_count, &subtree_nr) != 2)
441                 goto free_return;
442         while (size && *buf && *buf != '\n') {
443                 size--;
444                 buf++;
445         }
446         if (!size)
447                 goto free_return;
448         buf++; size--;
449         if (0 <= it->entry_count) {
450                 if (size < 20)
451                         goto free_return;
452                 memcpy(it->sha1, buf, 20);
453                 buf += 20;
454                 size -= 20;
455         }
456
457 #if DEBUG
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));
462         else
463                 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
464                         *buffer, subtree_nr);
465 #endif
466
467         /*
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,
470          * hence +2.
471          */
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;
479                 int namelen;
480                 sub = read_one(&buf, &size);
481                 if (!sub)
482                         goto free_return;
483                 namelen = strlen(name);
484                 subtree = find_subtree(it, name, namelen, 1);
485                 subtree->cache_tree = sub;
486         }
487         if (subtree_nr != it->subtree_nr)
488                 die("cache-tree: internal error");
489         *buffer = buf;
490         *size_p = size;
491         return it;
492
493  free_return:
494         cache_tree_free(&it);
495         return NULL;
496 }
497
498 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
499 {
500         if (buffer[0])
501                 return NULL; /* not the whole tree */
502         return read_one(&buffer, &size);
503 }