git-instaweb: respect bindir from Makefile
[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 struct 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
100 void 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 DEBUG
114         fprintf(stderr, "cache-tree invalidate <%s>\n", path);
115 #endif
116
117         if (!it)
118                 return;
119         slash = strchr(path, '/');
120         it->entry_count = -1;
121         if (!slash) {
122                 int pos;
123                 namelen = strlen(path);
124                 pos = subtree_pos(it, path, namelen);
125                 if (0 <= pos) {
126                         cache_tree_free(&it->down[pos]->cache_tree);
127                         free(it->down[pos]);
128                         /* 0 1 2 3 4 5
129                          *       ^     ^subtree_nr = 6
130                          *       pos
131                          * move 4 and 5 up one place (2 entries)
132                          * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
133                          */
134                         memmove(it->down+pos, it->down+pos+1,
135                                 sizeof(struct cache_tree_sub *) *
136                                 (it->subtree_nr - pos - 1));
137                         it->subtree_nr--;
138                 }
139                 return;
140         }
141         namelen = slash - path;
142         down = find_subtree(it, path, namelen, 0);
143         if (down)
144                 cache_tree_invalidate_path(down->cache_tree, slash + 1);
145 }
146
147 static int verify_cache(struct cache_entry **cache,
148                         int entries)
149 {
150         int i, funny;
151
152         /* Verify that the tree is merged */
153         funny = 0;
154         for (i = 0; i < entries; i++) {
155                 struct cache_entry *ce = cache[i];
156                 if (ce_stage(ce)) {
157                         if (10 < ++funny) {
158                                 fprintf(stderr, "...\n");
159                                 break;
160                         }
161                         fprintf(stderr, "%s: unmerged (%s)\n",
162                                 ce->name, sha1_to_hex(ce->sha1));
163                 }
164         }
165         if (funny)
166                 return -1;
167
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
170          * stage 0 entries.
171          */
172         funny = 0;
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.
177                  */
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] == '/') {
184                         if (10 < ++funny) {
185                                 fprintf(stderr, "...\n");
186                                 break;
187                         }
188                         fprintf(stderr, "You have both %s and %s\n",
189                                 this_name, next_name);
190                 }
191         }
192         if (funny)
193                 return -1;
194         return 0;
195 }
196
197 static void discard_unused_subtrees(struct cache_tree *it)
198 {
199         struct cache_tree_sub **down = it->down;
200         int nr = it->subtree_nr;
201         int dst, src;
202         for (dst = src = 0; src < nr; src++) {
203                 struct cache_tree_sub *s = down[src];
204                 if (s->used)
205                         down[dst++] = s;
206                 else {
207                         cache_tree_free(&s->cache_tree);
208                         free(s);
209                         it->subtree_nr--;
210                 }
211         }
212 }
213
214 int cache_tree_fully_valid(struct cache_tree *it)
215 {
216         int i;
217         if (!it)
218                 return 0;
219         if (it->entry_count < 0 || !has_sha1_file(it->sha1))
220                 return 0;
221         for (i = 0; i < it->subtree_nr; i++) {
222                 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
223                         return 0;
224         }
225         return 1;
226 }
227
228 static int update_one(struct cache_tree *it,
229                       struct cache_entry **cache,
230                       int entries,
231                       const char *base,
232                       int baselen,
233                       int missing_ok,
234                       int dryrun)
235 {
236         unsigned long size, offset;
237         char *buffer;
238         int i;
239
240         if (0 <= it->entry_count && has_sha1_file(it->sha1))
241                 return it->entry_count;
242
243         /*
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.
247          */
248         for (i = 0; i < it->subtree_nr; i++)
249                 it->down[i]->used = 0;
250
251         /*
252          * Find the subtrees and update them.
253          */
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;
259
260                 path = ce->name;
261                 pathlen = ce_namelen(ce);
262                 if (pathlen <= baselen || memcmp(base, path, baselen))
263                         break; /* at the end of this level */
264
265                 slash = strchr(path + baselen, '/');
266                 if (!slash)
267                         continue;
268                 /*
269                  * a/bbb/c (base = a/, slash = /c)
270                  * ==>
271                  * path+baselen = bbb/c, sublen = 3
272                  */
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,
279                                     path,
280                                     baselen + sublen + 1,
281                                     missing_ok,
282                                     dryrun);
283                 i += subcnt - 1;
284                 sub->used = 1;
285         }
286
287         discard_unused_subtrees(it);
288
289         /*
290          * Then write out the tree object for this level.
291          */
292         size = 8192;
293         buffer = xmalloc(size);
294         offset = 0;
295
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;
300                 int pathlen, entlen;
301                 const unsigned char *sha1;
302                 unsigned mode;
303
304                 path = ce->name;
305                 pathlen = ce_namelen(ce);
306                 if (pathlen <= baselen || memcmp(base, path, baselen))
307                         break; /* at the end of this level */
308
309                 slash = strchr(path + baselen, '/');
310                 if (slash) {
311                         entlen = slash - (path + baselen);
312                         sub = find_subtree(it, path + baselen, entlen, 0);
313                         if (!sub)
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;
318                         mode = S_IFDIR;
319                 }
320                 else {
321                         sha1 = ce->sha1;
322                         mode = ntohl(ce->ce_mode);
323                         entlen = pathlen - baselen;
324                 }
325                 if (!missing_ok && !has_sha1_file(sha1))
326                         return error("invalid object %s", sha1_to_hex(sha1));
327
328                 if (!ce->ce_mode)
329                         continue; /* entry being removed */
330
331                 if (size < offset + entlen + 100) {
332                         size = alloc_nr(offset + entlen + 100);
333                         buffer = xrealloc(buffer, size);
334                 }
335                 offset += sprintf(buffer + offset,
336                                   "%o %.*s", mode, entlen, path + baselen);
337                 buffer[offset++] = 0;
338                 memcpy(buffer + offset, sha1, 20);
339                 offset += 20;
340
341 #if DEBUG
342                 fprintf(stderr, "cache-tree update-one %o %.*s\n",
343                         mode, entlen, path + baselen);
344 #endif
345         }
346
347         if (dryrun) {
348                 unsigned char hdr[200];
349                 int hdrlen;
350                 write_sha1_file_prepare(buffer, offset, tree_type, it->sha1,
351                                         hdr, &hdrlen);
352         }
353         else
354                 write_sha1_file(buffer, offset, tree_type, it->sha1);
355         free(buffer);
356         it->entry_count = i;
357 #if DEBUG
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));
361 #endif
362         return i;
363 }
364
365 int cache_tree_update(struct cache_tree *it,
366                       struct cache_entry **cache,
367                       int entries,
368                       int missing_ok,
369                       int dryrun)
370 {
371         int i;
372         i = verify_cache(cache, entries);
373         if (i)
374                 return i;
375         i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
376         if (i < 0)
377                 return i;
378         return 0;
379 }
380
381 static void *write_one(struct cache_tree *it,
382                        char *path,
383                        int pathlen,
384                        char *buffer,
385                        unsigned long *size,
386                        unsigned long *offset)
387 {
388         int i;
389
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.
395          */
396         if (*size < *offset + pathlen + 100) {
397                 *size = alloc_nr(*offset + pathlen + 100);
398                 buffer = xrealloc(buffer, *size);
399         }
400         *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n",
401                            pathlen, path, 0,
402                            it->entry_count, it->subtree_nr);
403
404 #if DEBUG
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));
409         else
410                 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
411                         pathlen, path, it->subtree_nr);
412 #endif
413
414         if (0 <= it->entry_count) {
415                 memcpy(buffer + *offset, it->sha1, 20);
416                 *offset += 20;
417         }
418         for (i = 0; i < it->subtree_nr; i++) {
419                 struct cache_tree_sub *down = it->down[i];
420                 if (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");
425                 }
426                 buffer = write_one(down->cache_tree, down->name, down->namelen,
427                                    buffer, size, offset);
428         }
429         return buffer;
430 }
431
432 void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
433 {
434         char path[PATH_MAX];
435         unsigned long size = 8192;
436         char *buffer = xmalloc(size);
437
438         *size_p = 0;
439         path[0] = 0;
440         return write_one(root, path, 0, buffer, &size, size_p);
441 }
442
443 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
444 {
445         const char *buf = *buffer;
446         unsigned long size = *size_p;
447         const char *cp;
448         char *ep;
449         struct cache_tree *it;
450         int i, subtree_nr;
451
452         it = NULL;
453         /* skip name, but make sure name exists */
454         while (size && *buf) {
455                 size--;
456                 buf++;
457         }
458         if (!size)
459                 goto free_return;
460         buf++; size--;
461         it = cache_tree();
462
463         cp = buf;
464         it->entry_count = strtol(cp, &ep, 10);
465         if (cp == ep)
466                 goto free_return;
467         cp = ep;
468         subtree_nr = strtol(cp, &ep, 10);
469         if (cp == ep)
470                 goto free_return;
471         while (size && *buf && *buf != '\n') {
472                 size--;
473                 buf++;
474         }
475         if (!size)
476                 goto free_return;
477         buf++; size--;
478         if (0 <= it->entry_count) {
479                 if (size < 20)
480                         goto free_return;
481                 memcpy(it->sha1, buf, 20);
482                 buf += 20;
483                 size -= 20;
484         }
485
486 #if DEBUG
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));
491         else
492                 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
493                         *buffer, subtree_nr);
494 #endif
495
496         /*
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,
499          * hence +2.
500          */
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;
508
509                 sub = read_one(&buf, &size);
510                 if (!sub)
511                         goto free_return;
512                 subtree = cache_tree_sub(it, name);
513                 subtree->cache_tree = sub;
514         }
515         if (subtree_nr != it->subtree_nr)
516                 die("cache-tree: internal error");
517         *buffer = buf;
518         *size_p = size;
519         return it;
520
521  free_return:
522         cache_tree_free(&it);
523         return NULL;
524 }
525
526 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
527 {
528         if (buffer[0])
529                 return NULL; /* not the whole tree */
530         return read_one(&buffer, &size);
531 }
532
533 struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
534 {
535         while (*path) {
536                 const char *slash;
537                 struct cache_tree_sub *sub;
538
539                 slash = strchr(path, '/');
540                 if (!slash)
541                         slash = path + strlen(path);
542                 /* between path and slash is the name of the
543                  * subtree to look for.
544                  */
545                 sub = find_subtree(it, path, slash - path, 0);
546                 if (!sub)
547                         return NULL;
548                 it = sub->cache_tree;
549                 if (slash)
550                         while (*slash && *slash == '/')
551                                 slash++;
552                 if (!slash || !*slash)
553                         return it; /* prefix ended with slashes */
554                 path = slash;
555         }
556         return it;
557 }