cache-tree: replace a sscanf() by two strtol() calls
[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 (!it)
114                 return;
115         slash = strchr(path, '/');
116         it->entry_count = -1;
117         if (!slash) {
118                 int pos;
119                 namelen = strlen(path);
120                 pos = subtree_pos(it, path, namelen);
121                 if (0 <= pos) {
122                         cache_tree_free(&it->down[pos]->cache_tree);
123                         free(it->down[pos]);
124                         /* 0 1 2 3 4 5
125                          *       ^     ^subtree_nr = 6
126                          *       pos
127                          * move 4 and 5 up one place (2 entries)
128                          * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
129                          */
130                         memmove(it->down+pos, it->down+pos+1,
131                                 sizeof(struct cache_tree_sub *) *
132                                 (it->subtree_nr - pos - 1));
133                         it->subtree_nr--;
134                 }
135                 return;
136         }
137         namelen = slash - path;
138         down = find_subtree(it, path, namelen, 0);
139         if (down)
140                 cache_tree_invalidate_path(down->cache_tree, slash + 1);
141 }
142
143 static int verify_cache(struct cache_entry **cache,
144                         int entries)
145 {
146         int i, funny;
147
148         /* Verify that the tree is merged */
149         funny = 0;
150         for (i = 0; i < entries; i++) {
151                 struct cache_entry *ce = cache[i];
152                 if (ce_stage(ce)) {
153                         if (10 < ++funny) {
154                                 fprintf(stderr, "...\n");
155                                 break;
156                         }
157                         fprintf(stderr, "%s: unmerged (%s)\n",
158                                 ce->name, sha1_to_hex(ce->sha1));
159                 }
160         }
161         if (funny)
162                 return -1;
163
164         /* Also verify that the cache does not have path and path/file
165          * at the same time.  At this point we know the cache has only
166          * stage 0 entries.
167          */
168         funny = 0;
169         for (i = 0; i < entries - 1; i++) {
170                 /* path/file always comes after path because of the way
171                  * the cache is sorted.  Also path can appear only once,
172                  * which means conflicting one would immediately follow.
173                  */
174                 const char *this_name = cache[i]->name;
175                 const char *next_name = cache[i+1]->name;
176                 int this_len = strlen(this_name);
177                 if (this_len < strlen(next_name) &&
178                     strncmp(this_name, next_name, this_len) == 0 &&
179                     next_name[this_len] == '/') {
180                         if (10 < ++funny) {
181                                 fprintf(stderr, "...\n");
182                                 break;
183                         }
184                         fprintf(stderr, "You have both %s and %s\n",
185                                 this_name, next_name);
186                 }
187         }
188         if (funny)
189                 return -1;
190         return 0;
191 }
192
193 static void discard_unused_subtrees(struct cache_tree *it)
194 {
195         struct cache_tree_sub **down = it->down;
196         int nr = it->subtree_nr;
197         int dst, src;
198         for (dst = src = 0; src < nr; src++) {
199                 struct cache_tree_sub *s = down[src];
200                 if (s->used)
201                         down[dst++] = s;
202                 else {
203                         cache_tree_free(&s->cache_tree);
204                         free(s);
205                         it->subtree_nr--;
206                 }
207         }
208 }
209
210 int cache_tree_fully_valid(struct cache_tree *it)
211 {
212         int i;
213         if (!it)
214                 return 0;
215         if (it->entry_count < 0 || !has_sha1_file(it->sha1))
216                 return 0;
217         for (i = 0; i < it->subtree_nr; i++) {
218                 if (!cache_tree_fully_valid(it->down[i]->cache_tree))
219                         return 0;
220         }
221         return 1;
222 }
223
224 static int update_one(struct cache_tree *it,
225                       struct cache_entry **cache,
226                       int entries,
227                       const char *base,
228                       int baselen,
229                       int missing_ok,
230                       int dryrun)
231 {
232         unsigned long size, offset;
233         char *buffer;
234         int i;
235
236         if (0 <= it->entry_count && has_sha1_file(it->sha1))
237                 return it->entry_count;
238
239         /*
240          * We first scan for subtrees and update them; we start by
241          * marking existing subtrees -- the ones that are unmarked
242          * should not be in the result.
243          */
244         for (i = 0; i < it->subtree_nr; i++)
245                 it->down[i]->used = 0;
246
247         /*
248          * Find the subtrees and update them.
249          */
250         for (i = 0; i < entries; i++) {
251                 struct cache_entry *ce = cache[i];
252                 struct cache_tree_sub *sub;
253                 const char *path, *slash;
254                 int pathlen, sublen, subcnt;
255
256                 path = ce->name;
257                 pathlen = ce_namelen(ce);
258                 if (pathlen <= baselen || memcmp(base, path, baselen))
259                         break; /* at the end of this level */
260
261                 slash = strchr(path + baselen, '/');
262                 if (!slash)
263                         continue;
264                 /*
265                  * a/bbb/c (base = a/, slash = /c)
266                  * ==>
267                  * path+baselen = bbb/c, sublen = 3
268                  */
269                 sublen = slash - (path + baselen);
270                 sub = find_subtree(it, path + baselen, sublen, 1);
271                 if (!sub->cache_tree)
272                         sub->cache_tree = cache_tree();
273                 subcnt = update_one(sub->cache_tree,
274                                     cache + i, entries - i,
275                                     path,
276                                     baselen + sublen + 1,
277                                     missing_ok,
278                                     dryrun);
279                 i += subcnt - 1;
280                 sub->used = 1;
281         }
282
283         discard_unused_subtrees(it);
284
285         /*
286          * Then write out the tree object for this level.
287          */
288         size = 8192;
289         buffer = xmalloc(size);
290         offset = 0;
291
292         for (i = 0; i < entries; i++) {
293                 struct cache_entry *ce = cache[i];
294                 struct cache_tree_sub *sub;
295                 const char *path, *slash;
296                 int pathlen, entlen;
297                 const unsigned char *sha1;
298                 unsigned mode;
299
300                 path = ce->name;
301                 pathlen = ce_namelen(ce);
302                 if (pathlen <= baselen || memcmp(base, path, baselen))
303                         break; /* at the end of this level */
304
305                 slash = strchr(path + baselen, '/');
306                 if (slash) {
307                         entlen = slash - (path + baselen);
308                         sub = find_subtree(it, path + baselen, entlen, 0);
309                         if (!sub)
310                                 die("cache-tree.c: '%.*s' in '%s' not found",
311                                     entlen, path + baselen, path);
312                         i += sub->cache_tree->entry_count - 1;
313                         sha1 = sub->cache_tree->sha1;
314                         mode = S_IFDIR;
315                 }
316                 else {
317                         sha1 = ce->sha1;
318                         mode = ntohl(ce->ce_mode);
319                         entlen = pathlen - baselen;
320                 }
321                 if (!missing_ok && !has_sha1_file(sha1))
322                         return error("invalid object %s", sha1_to_hex(sha1));
323
324                 if (!ce->ce_mode)
325                         continue; /* entry being removed */
326
327                 if (size < offset + entlen + 100) {
328                         size = alloc_nr(offset + entlen + 100);
329                         buffer = xrealloc(buffer, size);
330                 }
331                 offset += sprintf(buffer + offset,
332                                   "%o %.*s", mode, entlen, path + baselen);
333                 buffer[offset++] = 0;
334                 memcpy(buffer + offset, sha1, 20);
335                 offset += 20;
336
337 #if DEBUG
338                 fprintf(stderr, "cache-tree %o %.*s\n",
339                         mode, entlen, path + baselen);
340 #endif
341         }
342
343         if (dryrun) {
344                 unsigned char hdr[200];
345                 int hdrlen;
346                 write_sha1_file_prepare(buffer, offset, tree_type, it->sha1,
347                                         hdr, &hdrlen);
348         }
349         else
350                 write_sha1_file(buffer, offset, tree_type, it->sha1);
351         free(buffer);
352         it->entry_count = i;
353 #if DEBUG
354         fprintf(stderr, "cache-tree (%d ent, %d subtree) %s\n",
355                 it->entry_count, it->subtree_nr,
356                 sha1_to_hex(it->sha1));
357 #endif
358         return i;
359 }
360
361 int cache_tree_update(struct cache_tree *it,
362                       struct cache_entry **cache,
363                       int entries,
364                       int missing_ok,
365                       int dryrun)
366 {
367         int i;
368         i = verify_cache(cache, entries);
369         if (i)
370                 return i;
371         i = update_one(it, cache, entries, "", 0, missing_ok, dryrun);
372         if (i < 0)
373                 return i;
374         return 0;
375 }
376
377 static void *write_one(struct cache_tree *it,
378                        char *path,
379                        int pathlen,
380                        char *buffer,
381                        unsigned long *size,
382                        unsigned long *offset)
383 {
384         int i;
385
386         /* One "cache-tree" entry consists of the following:
387          * path (NUL terminated)
388          * entry_count, subtree_nr ("%d %d\n")
389          * tree-sha1 (missing if invalid)
390          * subtree_nr "cache-tree" entries for subtrees.
391          */
392         if (*size < *offset + pathlen + 100) {
393                 *size = alloc_nr(*offset + pathlen + 100);
394                 buffer = xrealloc(buffer, *size);
395         }
396         *offset += sprintf(buffer + *offset, "%.*s%c%d %d\n",
397                            pathlen, path, 0,
398                            it->entry_count, it->subtree_nr);
399
400 #if DEBUG
401         if (0 <= it->entry_count)
402                 fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
403                         pathlen, path, it->entry_count, it->subtree_nr,
404                         sha1_to_hex(it->sha1));
405         else
406                 fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
407                         pathlen, path, it->subtree_nr);
408 #endif
409
410         if (0 <= it->entry_count) {
411                 memcpy(buffer + *offset, it->sha1, 20);
412                 *offset += 20;
413         }
414         for (i = 0; i < it->subtree_nr; i++) {
415                 struct cache_tree_sub *down = it->down[i];
416                 if (i) {
417                         struct cache_tree_sub *prev = it->down[i-1];
418                         if (subtree_name_cmp(down->name, down->namelen,
419                                              prev->name, prev->namelen) <= 0)
420                                 die("fatal - unsorted cache subtree");
421                 }
422                 buffer = write_one(down->cache_tree, down->name, down->namelen,
423                                    buffer, size, offset);
424         }
425         return buffer;
426 }
427
428 void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
429 {
430         char path[PATH_MAX];
431         unsigned long size = 8192;
432         char *buffer = xmalloc(size);
433
434         *size_p = 0;
435         path[0] = 0;
436         return write_one(root, path, 0, buffer, &size, size_p);
437 }
438
439 static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
440 {
441         const char *buf = *buffer;
442         unsigned long size = *size_p;
443         const char *cp;
444         char *ep;
445         struct cache_tree *it;
446         int i, subtree_nr;
447
448         it = NULL;
449         /* skip name, but make sure name exists */
450         while (size && *buf) {
451                 size--;
452                 buf++;
453         }
454         if (!size)
455                 goto free_return;
456         buf++; size--;
457         it = cache_tree();
458
459         cp = buf;
460         it->entry_count = strtol(cp, &ep, 10);
461         if (cp == ep)
462                 goto free_return;
463         cp = ep;
464         subtree_nr = strtol(cp, &ep, 10);
465         if (cp == ep)
466                 goto free_return;
467         while (size && *buf && *buf != '\n') {
468                 size--;
469                 buf++;
470         }
471         if (!size)
472                 goto free_return;
473         buf++; size--;
474         if (0 <= it->entry_count) {
475                 if (size < 20)
476                         goto free_return;
477                 memcpy(it->sha1, buf, 20);
478                 buf += 20;
479                 size -= 20;
480         }
481
482 #if DEBUG
483         if (0 <= it->entry_count)
484                 fprintf(stderr, "cache-tree <%s> (%d ent, %d subtree) %s\n",
485                         *buffer, it->entry_count, subtree_nr,
486                         sha1_to_hex(it->sha1));
487         else
488                 fprintf(stderr, "cache-tree <%s> (%d subtrees) invalid\n",
489                         *buffer, subtree_nr);
490 #endif
491
492         /*
493          * Just a heuristic -- we do not add directories that often but
494          * we do not want to have to extend it immediately when we do,
495          * hence +2.
496          */
497         it->subtree_alloc = subtree_nr + 2;
498         it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
499         for (i = 0; i < subtree_nr; i++) {
500                 /* read each subtree */
501                 struct cache_tree *sub;
502                 struct cache_tree_sub *subtree;
503                 const char *name = buf;
504
505                 sub = read_one(&buf, &size);
506                 if (!sub)
507                         goto free_return;
508                 subtree = cache_tree_sub(it, name);
509                 subtree->cache_tree = sub;
510         }
511         if (subtree_nr != it->subtree_nr)
512                 die("cache-tree: internal error");
513         *buffer = buf;
514         *size_p = size;
515         return it;
516
517  free_return:
518         cache_tree_free(&it);
519         return NULL;
520 }
521
522 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
523 {
524         if (buffer[0])
525                 return NULL; /* not the whole tree */
526         return read_one(&buffer, &size);
527 }