read-cache.con commit Squelch `removal' message from 'git reset --hard'. (e8623d4)
   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 "mode changes" */
  39                if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))
  40                        changed |= MODE_CHANGED;
  41                break;
  42        case S_IFLNK:
  43                changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED : 0;
  44                break;
  45        default:
  46                die("internal error: ce_mode is %o", ntohl(ce->ce_mode));
  47        }
  48        if (ce->ce_mtime.sec != htonl(st->st_mtime))
  49                changed |= MTIME_CHANGED;
  50        if (ce->ce_ctime.sec != htonl(st->st_ctime))
  51                changed |= CTIME_CHANGED;
  52
  53#ifdef USE_NSEC
  54        /*
  55         * nsec seems unreliable - not all filesystems support it, so
  56         * as long as it is in the inode cache you get right nsec
  57         * but after it gets flushed, you get zero nsec.
  58         */
  59        if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec))
  60                changed |= MTIME_CHANGED;
  61        if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec))
  62                changed |= CTIME_CHANGED;
  63#endif  
  64
  65        if (ce->ce_uid != htonl(st->st_uid) ||
  66            ce->ce_gid != htonl(st->st_gid))
  67                changed |= OWNER_CHANGED;
  68        if (ce->ce_ino != htonl(st->st_ino))
  69                changed |= INODE_CHANGED;
  70
  71#ifdef USE_STDEV
  72        /*
  73         * st_dev breaks on network filesystems where different
  74         * clients will have different views of what "device"
  75         * the filesystem is on
  76         */
  77        if (ce->ce_dev != htonl(st->st_dev))
  78                changed |= INODE_CHANGED;
  79#endif
  80
  81        if (ce->ce_size != htonl(st->st_size))
  82                changed |= DATA_CHANGED;
  83        return changed;
  84}
  85
  86int base_name_compare(const char *name1, int len1, int mode1,
  87                      const char *name2, int len2, int mode2)
  88{
  89        unsigned char c1, c2;
  90        int len = len1 < len2 ? len1 : len2;
  91        int cmp;
  92
  93        cmp = memcmp(name1, name2, len);
  94        if (cmp)
  95                return cmp;
  96        c1 = name1[len];
  97        c2 = name2[len];
  98        if (!c1 && S_ISDIR(mode1))
  99                c1 = '/';
 100        if (!c2 && S_ISDIR(mode2))
 101                c2 = '/';
 102        return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
 103}
 104
 105int cache_name_compare(const char *name1, int flags1, const char *name2, int flags2)
 106{
 107        int len1 = flags1 & CE_NAMEMASK;
 108        int len2 = flags2 & CE_NAMEMASK;
 109        int len = len1 < len2 ? len1 : len2;
 110        int cmp;
 111
 112        cmp = memcmp(name1, name2, len);
 113        if (cmp)
 114                return cmp;
 115        if (len1 < len2)
 116                return -1;
 117        if (len1 > len2)
 118                return 1;
 119        if (flags1 < flags2)
 120                return -1;
 121        if (flags1 > flags2)
 122                return 1;
 123        return 0;
 124}
 125
 126int cache_name_pos(const char *name, int namelen)
 127{
 128        int first, last;
 129
 130        first = 0;
 131        last = active_nr;
 132        while (last > first) {
 133                int next = (last + first) >> 1;
 134                struct cache_entry *ce = active_cache[next];
 135                int cmp = cache_name_compare(name, namelen, ce->name, ntohs(ce->ce_flags));
 136                if (!cmp)
 137                        return next;
 138                if (cmp < 0) {
 139                        last = next;
 140                        continue;
 141                }
 142                first = next+1;
 143        }
 144        return -first-1;
 145}
 146
 147/* Remove entry, return true if there are more entries to go.. */
 148int remove_cache_entry_at(int pos)
 149{
 150        active_cache_changed = 1;
 151        active_nr--;
 152        if (pos >= active_nr)
 153                return 0;
 154        memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
 155        return 1;
 156}
 157
 158int remove_file_from_cache(char *path)
 159{
 160        int pos = cache_name_pos(path, strlen(path));
 161        if (pos < 0)
 162                pos = -pos-1;
 163        while (pos < active_nr && !strcmp(active_cache[pos]->name, path))
 164                remove_cache_entry_at(pos);
 165        return 0;
 166}
 167
 168int ce_same_name(struct cache_entry *a, struct cache_entry *b)
 169{
 170        int len = ce_namelen(a);
 171        return ce_namelen(b) == len && !memcmp(a->name, b->name, len);
 172}
 173
 174int ce_path_match(const struct cache_entry *ce, const char **pathspec)
 175{
 176        const char *match, *name;
 177        int len;
 178
 179        if (!pathspec)
 180                return 1;
 181
 182        len = ce_namelen(ce);
 183        name = ce->name;
 184        while ((match = *pathspec++) != NULL) {
 185                int matchlen = strlen(match);
 186                if (matchlen > len)
 187                        continue;
 188                if (memcmp(name, match, matchlen))
 189                        continue;
 190                if (matchlen && name[matchlen-1] == '/')
 191                        return 1;
 192                if (name[matchlen] == '/' || !name[matchlen])
 193                        return 1;
 194                if (!matchlen)
 195                        return 1;
 196        }
 197        return 0;
 198}
 199
 200/*
 201 * Do we have another file that has the beginning components being a
 202 * proper superset of the name we're trying to add?
 203 */
 204static int has_file_name(const struct cache_entry *ce, int pos, int ok_to_replace)
 205{
 206        int retval = 0;
 207        int len = ce_namelen(ce);
 208        int stage = ce_stage(ce);
 209        const char *name = ce->name;
 210
 211        while (pos < active_nr) {
 212                struct cache_entry *p = active_cache[pos++];
 213
 214                if (len >= ce_namelen(p))
 215                        break;
 216                if (memcmp(name, p->name, len))
 217                        break;
 218                if (ce_stage(p) != stage)
 219                        continue;
 220                if (p->name[len] != '/')
 221                        continue;
 222                retval = -1;
 223                if (!ok_to_replace)
 224                        break;
 225                remove_cache_entry_at(--pos);
 226        }
 227        return retval;
 228}
 229
 230/*
 231 * Do we have another file with a pathname that is a proper
 232 * subset of the name we're trying to add?
 233 */
 234static int has_dir_name(const struct cache_entry *ce, int pos, int ok_to_replace)
 235{
 236        int retval = 0;
 237        int stage = ce_stage(ce);
 238        const char *name = ce->name;
 239        const char *slash = name + ce_namelen(ce);
 240
 241        for (;;) {
 242                int len;
 243
 244                for (;;) {
 245                        if (*--slash == '/')
 246                                break;
 247                        if (slash <= ce->name)
 248                                return retval;
 249                }
 250                len = slash - name;
 251
 252                pos = cache_name_pos(name, ntohs(create_ce_flags(len, stage)));
 253                if (pos >= 0) {
 254                        retval = -1;
 255                        if (ok_to_replace)
 256                                break;
 257                        remove_cache_entry_at(pos);
 258                        continue;
 259                }
 260
 261                /*
 262                 * Trivial optimization: if we find an entry that
 263                 * already matches the sub-directory, then we know
 264                 * we're ok, and we can exit.
 265                 */
 266                pos = -pos-1;
 267                while (pos < active_nr) {
 268                        struct cache_entry *p = active_cache[pos];
 269                        if ((ce_namelen(p) <= len) ||
 270                            (p->name[len] != '/') ||
 271                            memcmp(p->name, name, len))
 272                                break; /* not our subdirectory */
 273                        if (ce_stage(p) == stage)
 274                                /* p is at the same stage as our entry, and
 275                                 * is a subdirectory of what we are looking
 276                                 * at, so we cannot have conflicts at our
 277                                 * level or anything shorter.
 278                                 */
 279                                return retval;
 280                        pos++;
 281                }
 282        }
 283        return retval;
 284}
 285
 286/* We may be in a situation where we already have path/file and path
 287 * is being added, or we already have path and path/file is being
 288 * added.  Either one would result in a nonsense tree that has path
 289 * twice when git-write-tree tries to write it out.  Prevent it.
 290 * 
 291 * If ok-to-replace is specified, we remove the conflicting entries
 292 * from the cache so the caller should recompute the insert position.
 293 * When this happens, we return non-zero.
 294 */
 295static int check_file_directory_conflict(const struct cache_entry *ce, int pos, int ok_to_replace)
 296{
 297        /*
 298         * We check if the path is a sub-path of a subsequent pathname
 299         * first, since removing those will not change the position
 300         * in the array
 301         */
 302        int retval = has_file_name(ce, pos, ok_to_replace);
 303        /*
 304         * Then check if the path might have a clashing sub-directory
 305         * before it.
 306         */
 307        return retval + has_dir_name(ce, pos, ok_to_replace);
 308}
 309
 310int add_cache_entry(struct cache_entry *ce, int option)
 311{
 312        int pos;
 313        int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
 314        int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
 315        int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
 316        pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
 317
 318        /* existing match? Just replace it */
 319        if (pos >= 0) {
 320                active_cache_changed = 1;
 321                active_cache[pos] = ce;
 322                return 0;
 323        }
 324        pos = -pos-1;
 325
 326        /*
 327         * Inserting a merged entry ("stage 0") into the index
 328         * will always replace all non-merged entries..
 329         */
 330        if (pos < active_nr && ce_stage(ce) == 0) {
 331                while (ce_same_name(active_cache[pos], ce)) {
 332                        ok_to_add = 1;
 333                        if (!remove_cache_entry_at(pos))
 334                                break;
 335                }
 336        }
 337
 338        if (!ok_to_add)
 339                return -1;
 340
 341        if (!skip_df_check && check_file_directory_conflict(ce, pos, ok_to_replace)) {
 342                if (!ok_to_replace)
 343                        return -1;
 344                pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
 345                pos = -pos-1;
 346        }
 347
 348        /* Make sure the array is big enough .. */
 349        if (active_nr == active_alloc) {
 350                active_alloc = alloc_nr(active_alloc);
 351                active_cache = xrealloc(active_cache, active_alloc * sizeof(struct cache_entry *));
 352        }
 353
 354        /* Add it in.. */
 355        active_nr++;
 356        if (active_nr > pos)
 357                memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
 358        active_cache[pos] = ce;
 359        active_cache_changed = 1;
 360        return 0;
 361}
 362
 363static int verify_hdr(struct cache_header *hdr, unsigned long size)
 364{
 365        SHA_CTX c;
 366        unsigned char sha1[20];
 367
 368        if (hdr->hdr_signature != htonl(CACHE_SIGNATURE))
 369                return error("bad signature");
 370        if (hdr->hdr_version != htonl(2))
 371                return error("bad index version");
 372        SHA1_Init(&c);
 373        SHA1_Update(&c, hdr, size - 20);
 374        SHA1_Final(sha1, &c);
 375        if (memcmp(sha1, (void *)hdr + size - 20, 20))
 376                return error("bad index file sha1 signature");
 377        return 0;
 378}
 379
 380int read_cache(void)
 381{
 382        int fd, i;
 383        struct stat st;
 384        unsigned long size, offset;
 385        void *map;
 386        struct cache_header *hdr;
 387
 388        errno = EBUSY;
 389        if (active_cache)
 390                return error("more than one cachefile");
 391        errno = ENOENT;
 392        fd = open(get_index_file(), O_RDONLY);
 393        if (fd < 0)
 394                return (errno == ENOENT) ? 0 : error("open failed");
 395
 396        size = 0; // avoid gcc warning
 397        map = MAP_FAILED;
 398        if (!fstat(fd, &st)) {
 399                size = st.st_size;
 400                errno = EINVAL;
 401                if (size >= sizeof(struct cache_header) + 20)
 402                        map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
 403        }
 404        close(fd);
 405        if (map == MAP_FAILED)
 406                return error("mmap failed");
 407
 408        hdr = map;
 409        if (verify_hdr(hdr, size) < 0)
 410                goto unmap;
 411
 412        active_nr = ntohl(hdr->hdr_entries);
 413        active_alloc = alloc_nr(active_nr);
 414        active_cache = calloc(active_alloc, sizeof(struct cache_entry *));
 415
 416        offset = sizeof(*hdr);
 417        for (i = 0; i < active_nr; i++) {
 418                struct cache_entry *ce = map + offset;
 419                offset = offset + ce_size(ce);
 420                active_cache[i] = ce;
 421        }
 422        return active_nr;
 423
 424unmap:
 425        munmap(map, size);
 426        errno = EINVAL;
 427        return error("verify header failed");
 428}
 429
 430#define WRITE_BUFFER_SIZE 8192
 431static unsigned char write_buffer[WRITE_BUFFER_SIZE];
 432static unsigned long write_buffer_len;
 433
 434static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
 435{
 436        while (len) {
 437                unsigned int buffered = write_buffer_len;
 438                unsigned int partial = WRITE_BUFFER_SIZE - buffered;
 439                if (partial > len)
 440                        partial = len;
 441                memcpy(write_buffer + buffered, data, partial);
 442                buffered += partial;
 443                if (buffered == WRITE_BUFFER_SIZE) {
 444                        SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
 445                        if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
 446                                return -1;
 447                        buffered = 0;
 448                }
 449                write_buffer_len = buffered;
 450                len -= partial;
 451                data += partial;
 452        }
 453        return 0;
 454}
 455
 456static int ce_flush(SHA_CTX *context, int fd)
 457{
 458        unsigned int left = write_buffer_len;
 459
 460        if (left) {
 461                write_buffer_len = 0;
 462                SHA1_Update(context, write_buffer, left);
 463        }
 464
 465        /* Append the SHA1 signature at the end */
 466        SHA1_Final(write_buffer + left, context);
 467        left += 20;
 468        if (write(fd, write_buffer, left) != left)
 469                return -1;
 470        return 0;
 471}
 472
 473int write_cache(int newfd, struct cache_entry **cache, int entries)
 474{
 475        SHA_CTX c;
 476        struct cache_header hdr;
 477        int i, removed;
 478
 479        for (i = removed = 0; i < entries; i++)
 480                if (!cache[i]->ce_mode)
 481                        removed++;
 482
 483        hdr.hdr_signature = htonl(CACHE_SIGNATURE);
 484        hdr.hdr_version = htonl(2);
 485        hdr.hdr_entries = htonl(entries - removed);
 486
 487        SHA1_Init(&c);
 488        if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
 489                return -1;
 490
 491        for (i = 0; i < entries; i++) {
 492                struct cache_entry *ce = cache[i];
 493                if (!ce->ce_mode)
 494                        continue;
 495                if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
 496                        return -1;
 497        }
 498        return ce_flush(&c, newfd);
 499}