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