read-cache.con commit Merge http://www.kernel.org/pub/scm/gitk/gitk (5e80092)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7
   8struct cache_entry **active_cache = NULL;
   9unsigned int active_nr = 0, active_alloc = 0, active_cache_changed = 0;
  10
  11/*
  12 * This only updates the "non-critical" parts of the directory
  13 * cache, ie the parts that aren't tracked by GIT, and only used
  14 * to validate the cache.
  15 */
  16void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
  17{
  18        ce->ce_ctime.sec = htonl(st->st_ctime);
  19        ce->ce_mtime.sec = htonl(st->st_mtime);
  20#ifdef USE_NSEC
  21        ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec);
  22        ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec);
  23#endif
  24        ce->ce_dev = htonl(st->st_dev);
  25        ce->ce_ino = htonl(st->st_ino);
  26        ce->ce_uid = htonl(st->st_uid);
  27        ce->ce_gid = htonl(st->st_gid);
  28        ce->ce_size = htonl(st->st_size);
  29}
  30
  31int ce_match_stat(struct cache_entry *ce, struct stat *st)
  32{
  33        unsigned int changed = 0;
  34
  35        switch (ntohl(ce->ce_mode) & S_IFMT) {
  36        case S_IFREG:
  37                changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
  38                /* We consider only the owner x bit to be relevant for
  39                 * "mode changes"
  40                 */
  41                if (trust_executable_bit &&
  42                    (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
  43                        changed |= MODE_CHANGED;
  44                break;
  45        case S_IFLNK:
  46                changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
  47                break;
  48        default:
  49                die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
  50        }
  51        if (ce->ce_mtime.sec != htonl(st->st_mtime))
  52                changed |= MTIME_CHANGED;
  53        if (ce->ce_ctime.sec != htonl(st->st_ctime))
  54                changed |= CTIME_CHANGED;
  55
  56#ifdef USE_NSEC
  57        /*
  58         * nsec seems unreliable - not all filesystems support it, so
  59         * as long as it is in the inode cache you get right nsec
  60         * but after it gets flushed, you get zero nsec.
  61         */
  62        if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
  63                changed |= MTIME_CHANGED;
  64        if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
  65                changed |= CTIME_CHANGED;
  66#endif  
  67
  68        if (ce->ce_uid != htonl(st->st_uid) ||
  69            ce->ce_gid != htonl(st->st_gid))
  70                changed |= OWNER_CHANGED;
  71        if (ce->ce_ino != htonl(st->st_ino))
  72                changed |= INODE_CHANGED;
  73
  74#ifdef USE_STDEV
  75        /*
  76         * st_dev breaks on network filesystems where different
  77         * clients will have different views of what "device"
  78         * the filesystem is on
  79         */
  80        if (ce->ce_dev != htonl(st->st_dev))
  81                changed |= INODE_CHANGED;
  82#endif
  83
  84        if (ce->ce_size != htonl(st->st_size))
  85                changed |= DATA_CHANGED;
  86        return changed;
  87}
  88
  89static int ce_compare_data(struct cache_entry *ce, struct stat *st)
  90{
  91        int match = -1;
  92        int fd = open(ce->name, O_RDONLY);
  93
  94        if (fd >= 0) {
  95                unsigned char sha1[20];
  96                if (!index_fd(sha1, fd, st, 0, NULL))
  97                        match = memcmp(sha1, ce->sha1, 20);
  98                close(fd);
  99        }
 100        return match;
 101}
 102
 103static int ce_compare_link(struct cache_entry *ce, unsigned long expected_size)
 104{
 105        int match = -1;
 106        char *target;
 107        void *buffer;
 108        unsigned long size;
 109        char type[10];
 110        int len;
 111
 112        target = xmalloc(expected_size);
 113        len = readlink(ce->name, target, expected_size);
 114        if (len != expected_size) {
 115                free(target);
 116                return -1;
 117        }
 118        buffer = read_sha1_file(ce->sha1, type, &size);
 119        if (!buffer) {
 120                free(target);
 121                return -1;
 122        }
 123        if (size == expected_size)
 124                match = memcmp(buffer, target, size);
 125        free(buffer);
 126        free(target);
 127        return match;
 128}
 129
 130int ce_modified(struct cache_entry *ce, struct stat *st)
 131{
 132        int changed;
 133        changed = ce_match_stat(ce, st);
 134        if (!changed)
 135                return 0;
 136
 137        /*
 138         * If the mode or type has changed, there's no point in trying
 139         * to refresh the entry - it's not going to match
 140         */
 141        if (changed & (MODE_CHANGED | TYPE_CHANGED))
 142                return changed;
 143
 144        /* Immediately after read-tree or update-index --cacheinfo,
 145         * the length field is zero.  For other cases the ce_size
 146         * should match the SHA1 recorded in the index entry.
 147         */
 148        if ((changed & DATA_CHANGED) && ce->ce_size != htonl(0))
 149                return changed;
 150
 151        switch (st->st_mode & S_IFMT) {
 152        case S_IFREG:
 153                if (ce_compare_data(ce, st))
 154                        return changed | DATA_CHANGED;
 155                break;
 156        case S_IFLNK:
 157                if (ce_compare_link(ce, st->st_size))
 158                        return changed | DATA_CHANGED;
 159                break;
 160        default:
 161                return changed | TYPE_CHANGED;
 162        }
 163        return 0;
 164}
 165
 166int base_name_compare(const char *name1, int len1, int mode1,
 167                      const char *name2, int len2, int mode2)
 168{
 169        unsigned char c1, c2;
 170        int len = len1 < len2 ? len1 : len2;
 171        int cmp;
 172
 173        cmp = memcmp(name1, name2, len);
 174        if (cmp)
 175                return cmp;
 176        c1 = name1[len];
 177        c2 = name2[len];
 178        if (!c1 && S_ISDIR(mode1))
 179                c1 = '/';
 180        if (!c2 && S_ISDIR(mode2))
 181                c2 = '/';
 182        return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
 183}
 184
 185int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
 186{
 187        int len1 = flags1 & CE_NAMEMASK;
 188        int len2 = flags2 & CE_NAMEMASK;
 189        int len = len1 < len2 ? len1 : len2;
 190        int cmp;
 191
 192        cmp = memcmp(name1, name2, len);
 193        if (cmp)
 194                return cmp;
 195        if (len1 < len2)
 196                return -1;
 197        if (len1 > len2)
 198                return 1;
 199        if (flags1 < flags2)
 200                return -1;
 201        if (flags1 > flags2)
 202                return 1;
 203        return 0;
 204}
 205
 206int cache_name_pos(const char *name, int namelen)
 207{
 208        int first, last;
 209
 210        first = 0;
 211        last = active_nr;
 212        while (last > first) {
 213                int next = (last + first) >> 1;
 214                struct cache_entry *ce = active_cache[next];
 215                int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
 216                if (!cmp)
 217                        return next;
 218                if (cmp < 0) {
 219                        last = next;
 220                        continue;
 221                }
 222                first = next+1;
 223        }
 224        return -first-1;
 225}
 226
 227/* Remove entry, return true if there are more entries to go.. */
 228int remove_cache_entry_at(int pos)
 229{
 230        active_cache_changed = 1;
 231        active_nr--;
 232        if (pos >= active_nr)
 233                return 0;
 234        memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
 235        return 1;
 236}
 237
 238int remove_file_from_cache(const char *path)
 239{
 240        int pos = cache_name_pos(path, strlen(path));
 241        if (pos < 0)
 242                pos = -pos-1;
 243        while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
 244                remove_cache_entry_at(pos);
 245        return 0;
 246}
 247
 248int ce_same_name(struct cache_entry *a, struct cache_entry *b)
 249{
 250        int len = ce_namelen(a);
 251        return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
 252}
 253
 254int ce_path_match(const struct cache_entry *ce, const char **pathspec)
 255{
 256        const char *match, *name;
 257        int len;
 258
 259        if (!pathspec)
 260                return 1;
 261
 262        len = ce_namelen(ce);
 263        name = ce->name;
 264        while ((match = *pathspec++) != NULL) {
 265                int matchlen = strlen(match);
 266                if (matchlen > len)
 267                        continue;
 268                if (memcmp(name, match, matchlen))
 269                        continue;
 270                if (matchlen && name[matchlen-1] == '/')
 271                        return 1;
 272                if (name[matchlen] == '/' || !name[matchlen])
 273                        return 1;
 274                if (!matchlen)
 275                        return 1;
 276        }
 277        return 0;
 278}
 279
 280/*
 281 * Do we have another file that has the beginning components being a
 282 * proper superset of the name we're trying to add?
 283 */
 284static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
 285{
 286        int retval = 0;
 287        int len = ce_namelen(ce);
 288        int stage = ce_stage(ce);
 289        const char *name = ce->name;
 290
 291        while (pos < active_nr) {
 292                struct cache_entry *p = active_cache[pos++];
 293
 294                if (len >= ce_namelen(p))
 295                        break;
 296                if (memcmp(name, p->name, len))
 297                        break;
 298                if (ce_stage(p) != stage)
 299                        continue;
 300                if (p->name[len] != '/')
 301                        continue;
 302                retval = -1;
 303                if (!ok_to_replace)
 304                        break;
 305                remove_cache_entry_at(--pos);
 306        }
 307        return retval;
 308}
 309
 310/*
 311 * Do we have another file with a pathname that is a proper
 312 * subset of the name we're trying to add?
 313 */
 314static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
 315{
 316        int retval = 0;
 317        int stage = ce_stage(ce);
 318        const char *name = ce->name;
 319        const char *slash = name + ce_namelen(ce);
 320
 321        for (;;) {
 322                int len;
 323
 324                for (;;) {
 325                        if (*--slash == '/')
 326                                break;
 327                        if (slash <= ce->name)
 328                                return retval;
 329                }
 330                len = slash - name;
 331
 332                pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
 333                if (pos >= 0) {
 334                        retval = -1;
 335                        if (ok_to_replace)
 336                                break;
 337                        remove_cache_entry_at(pos);
 338                        continue;
 339                }
 340
 341                /*
 342                 * Trivial optimization: if we find an entry that
 343                 * already matches the sub-directory, then we know
 344                 * we're ok, and we can exit.
 345                 */
 346                pos = -pos-1;
 347                while (pos < active_nr) {
 348                        struct cache_entry *p = active_cache[pos];
 349                        if ((ce_namelen(p) <= len) ||
 350                            (p->name[len] != '/') ||
 351                            memcmp(p->name, name, len))
 352                                break; /* not our subdirectory */
 353                        if (ce_stage(p) == stage)
 354                                /* p is at the same stage as our entry, and
 355                                 * is a subdirectory of what we are looking
 356                                 * at, so we cannot have conflicts at our
 357                                 * level or anything shorter.
 358                                 */
 359                                return retval;
 360                        pos++;
 361                }
 362        }
 363        return retval;
 364}
 365
 366/* We may be in a situation where we already have path/file and path
 367 * is being added, or we already have path and path/file is being
 368 * added.  Either one would result in a nonsense tree that has path
 369 * twice when git-write-tree tries to write it out.  Prevent it.
 370 * 
 371 * If ok-to-replace is specified, we remove the conflicting entries
 372 * from the cache so the caller should recompute the insert position.
 373 * When this happens, we return non-zero.
 374 */
 375static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
 376{
 377        /*
 378         * We check if the path is a sub-path of a subsequent pathname
 379         * first, since removing those will not change the position
 380         * in the array
 381         */
 382        int retval = has_file_name(ce, pos, ok_to_replace);
 383        /*
 384         * Then check if the path might have a clashing sub-directory
 385         * before it.
 386         */
 387        return retval + has_dir_name(ce, pos, ok_to_replace);
 388}
 389
 390int add_cache_entry(struct cache_entry *ce, int option)
 391{
 392        int pos;
 393        int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
 394        int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
 395        int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
 396        pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
 397
 398        /* existing match? Just replace it. */
 399        if (pos >= 0) {
 400                active_cache_changed = 1;
 401                active_cache[pos] = ce;
 402                return 0;
 403        }
 404        pos = -pos-1;
 405
 406        /*
 407         * Inserting a merged entry ("stage 0") into the index
 408         * will always replace all non-merged entries..
 409         */
 410        if (pos < active_nr && ce_stage(ce) == 0) {
 411                while (ce_same_name(active_cache[pos], ce)) {
 412                        ok_to_add = 1;
 413                        if (!remove_cache_entry_at(pos))
 414                                break;
 415                }
 416        }
 417
 418        if (!ok_to_add)
 419                return -1;
 420
 421        if (!skip_df_check &&
 422            check_file_directory_conflict(ce, pos, ok_to_replace)) {
 423                if (!ok_to_replace)
 424                        return -1;
 425                pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
 426                pos = -pos-1;
 427        }
 428
 429        /* Make sure the array is big enough .. */
 430        if (active_nr == active_alloc) {
 431                active_alloc = alloc_nr(active_alloc);
 432                active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
 433        }
 434
 435        /* Add it in.. */
 436        active_nr++;
 437        if (active_nr > pos)
 438                memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
 439        active_cache[pos] = ce;
 440        active_cache_changed = 1;
 441        return 0;
 442}
 443
 444static int verify_hdr(struct cache_header *hdr, unsigned long size)
 445{
 446        SHA_CTX c;
 447        unsigned char sha1[20];
 448
 449        if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
 450                return error("bad signature");
 451        if (hdr->hdr_version != htonl(2))
 452                return error("bad index version");
 453        SHA1_Init(&c);
 454        SHA1_Update(&c, hdr, size - 20);
 455        SHA1_Final(sha1, &c);
 456        if (memcmp(sha1, (void *)hdr + size - 20, 20))
 457                return error("bad index file sha1 signature");
 458        return 0;
 459}
 460
 461int read_cache(void)
 462{
 463        int fd, i;
 464        struct stat st;
 465        unsigned long size, offset;
 466        void *map;
 467        struct cache_header *hdr;
 468
 469        errno = EBUSY;
 470        if (active_cache)
 471                return active_nr;
 472
 473        errno = ENOENT;
 474        fd = open(get_index_file(), O_RDONLY);
 475        if (fd < 0) {
 476                if (errno == ENOENT)
 477                        return 0;
 478                die("index file open failed (%s)", strerror(errno));
 479        }
 480
 481        size = 0; // avoid gcc warning
 482        map = MAP_FAILED;
 483        if (!fstat(fd, &st)) {
 484                size = st.st_size;
 485                errno = EINVAL;
 486                if (size >= sizeof(struct cache_header) + 20)
 487                        map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
 488        }
 489        close(fd);
 490        if (map == MAP_FAILED)
 491                die("index file mmap failed (%s)", strerror(errno));
 492
 493        hdr = map;
 494        if (verify_hdr(hdr, size) < 0)
 495                goto unmap;
 496
 497        active_nr = ntohl(hdr->hdr_entries);
 498        active_alloc = alloc_nr(active_nr);
 499        active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
 500
 501        offset = sizeof(*hdr);
 502        for (i = 0; i < active_nr; i++) {
 503                struct cache_entry *ce = map + offset;
 504                offset = offset + ce_size(ce);
 505                active_cache[i] = ce;
 506        }
 507        return active_nr;
 508
 509unmap:
 510        munmap(map, size);
 511        errno = EINVAL;
 512        die("index file corrupt");
 513}
 514
 515#define WRITE_BUFFER_SIZE 8192
 516static unsigned char write_buffer[WRITE_BUFFER_SIZE];
 517static unsigned long write_buffer_len;
 518
 519static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
 520{
 521        while (len) {
 522                unsigned int buffered = write_buffer_len;
 523                unsigned int partial = WRITE_BUFFER_SIZE - buffered;
 524                if (partial > len)
 525                        partial = len;
 526                memcpy(write_buffer + buffered, data, partial);
 527                buffered += partial;
 528                if (buffered == WRITE_BUFFER_SIZE) {
 529                        SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
 530                        if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
 531                                return -1;
 532                        buffered = 0;
 533                }
 534                write_buffer_len = buffered;
 535                len -= partial;
 536                data += partial;
 537        }
 538        return 0;
 539}
 540
 541static int ce_flush(SHA_CTX *context, int fd)
 542{
 543        unsigned int left = write_buffer_len;
 544
 545        if (left) {
 546                write_buffer_len = 0;
 547                SHA1_Update(context, write_buffer, left);
 548        }
 549
 550        /* Flush first if not enough space for SHA1 signature */
 551        if (left + 20 > WRITE_BUFFER_SIZE) {
 552                if (write(fd, write_buffer, left) != left)
 553                        return -1;
 554                left = 0;
 555        }
 556
 557        /* Append the SHA1 signature at the end */
 558        SHA1_Final(write_buffer + left, context);
 559        left += 20;
 560        if (write(fd, write_buffer, left) != left)
 561                return -1;
 562        return 0;
 563}
 564
 565int write_cache(int newfd, struct cache_entry **cache, int entries)
 566{
 567        SHA_CTX c;
 568        struct cache_header hdr;
 569        int i, removed;
 570
 571        for (i = removed = 0; i < entries; i++)
 572                if (!cache[i]->ce_mode)
 573                        removed++;
 574
 575        hdr.hdr_signature = htonl(CACHE_SIGNATURE);
 576        hdr.hdr_version = htonl(2);
 577        hdr.hdr_entries = htonl(entries - removed);
 578
 579        SHA1_Init(&c);
 580        if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
 581                return -1;
 582
 583        for (i = 0; i < entries; i++) {
 584                struct cache_entry *ce = cache[i];
 585                if (!ce->ce_mode)
 586                        continue;
 587                if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
 588                        return -1;
 589        }
 590        return ce_flush(&c, newfd);
 591}