1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#define NO_THE_INDEX_COMPATIBILITY_MACROS 7#include"cache.h" 8#include"cache-tree.h" 9#include"refs.h" 10#include"dir.h" 11#include"tree.h" 12#include"commit.h" 13#include"blob.h" 14#include"resolve-undo.h" 15#include"strbuf.h" 16#include"varint.h" 17 18static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really); 19 20/* Index extensions. 21 * 22 * The first letter should be 'A'..'Z' for extensions that are not 23 * necessary for a correct operation (i.e. optimization data). 24 * When new extensions are added that _needs_ to be understood in 25 * order to correctly interpret the index file, pick character that 26 * is outside the range, to cause the reader to abort. 27 */ 28 29#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 30#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 31#define CACHE_EXT_RESOLVE_UNDO 0x52455543/* "REUC" */ 32 33struct index_state the_index; 34 35static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 36{ 37 istate->cache[nr] = ce; 38add_name_hash(istate, ce); 39} 40 41static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 42{ 43struct cache_entry *old = istate->cache[nr]; 44 45remove_name_hash(old); 46set_index_entry(istate, nr, ce); 47 istate->cache_changed =1; 48} 49 50voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 51{ 52struct cache_entry *old = istate->cache[nr], *new; 53int namelen =strlen(new_name); 54 55new=xmalloc(cache_entry_size(namelen)); 56copy_cache_entry(new, old); 57new->ce_flags &= ~(CE_STATE_MASK | CE_NAMEMASK); 58new->ce_flags |= (namelen >= CE_NAMEMASK ? CE_NAMEMASK : namelen); 59memcpy(new->name, new_name, namelen +1); 60 61cache_tree_invalidate_path(istate->cache_tree, old->name); 62remove_index_entry_at(istate, nr); 63add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 64} 65 66/* 67 * This only updates the "non-critical" parts of the directory 68 * cache, ie the parts that aren't tracked by GIT, and only used 69 * to validate the cache. 70 */ 71voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 72{ 73 ce->ce_ctime.sec = (unsigned int)st->st_ctime; 74 ce->ce_mtime.sec = (unsigned int)st->st_mtime; 75 ce->ce_ctime.nsec =ST_CTIME_NSEC(*st); 76 ce->ce_mtime.nsec =ST_MTIME_NSEC(*st); 77 ce->ce_dev = st->st_dev; 78 ce->ce_ino = st->st_ino; 79 ce->ce_uid = st->st_uid; 80 ce->ce_gid = st->st_gid; 81 ce->ce_size = st->st_size; 82 83if(assume_unchanged) 84 ce->ce_flags |= CE_VALID; 85 86if(S_ISREG(st->st_mode)) 87ce_mark_uptodate(ce); 88} 89 90static intce_compare_data(struct cache_entry *ce,struct stat *st) 91{ 92int match = -1; 93int fd =open(ce->name, O_RDONLY); 94 95if(fd >=0) { 96unsigned char sha1[20]; 97if(!index_fd(sha1, fd, st, OBJ_BLOB, ce->name,0)) 98 match =hashcmp(sha1, ce->sha1); 99/* index_fd() closed the file descriptor already */ 100} 101return match; 102} 103 104static intce_compare_link(struct cache_entry *ce,size_t expected_size) 105{ 106int match = -1; 107void*buffer; 108unsigned long size; 109enum object_type type; 110struct strbuf sb = STRBUF_INIT; 111 112if(strbuf_readlink(&sb, ce->name, expected_size)) 113return-1; 114 115 buffer =read_sha1_file(ce->sha1, &type, &size); 116if(buffer) { 117if(size == sb.len) 118 match =memcmp(buffer, sb.buf, size); 119free(buffer); 120} 121strbuf_release(&sb); 122return match; 123} 124 125static intce_compare_gitlink(struct cache_entry *ce) 126{ 127unsigned char sha1[20]; 128 129/* 130 * We don't actually require that the .git directory 131 * under GITLINK directory be a valid git directory. It 132 * might even be missing (in case nobody populated that 133 * sub-project). 134 * 135 * If so, we consider it always to match. 136 */ 137if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 138return0; 139returnhashcmp(sha1, ce->sha1); 140} 141 142static intce_modified_check_fs(struct cache_entry *ce,struct stat *st) 143{ 144switch(st->st_mode & S_IFMT) { 145case S_IFREG: 146if(ce_compare_data(ce, st)) 147return DATA_CHANGED; 148break; 149case S_IFLNK: 150if(ce_compare_link(ce,xsize_t(st->st_size))) 151return DATA_CHANGED; 152break; 153case S_IFDIR: 154if(S_ISGITLINK(ce->ce_mode)) 155returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 156default: 157return TYPE_CHANGED; 158} 159return0; 160} 161 162static intce_match_stat_basic(struct cache_entry *ce,struct stat *st) 163{ 164unsigned int changed =0; 165 166if(ce->ce_flags & CE_REMOVE) 167return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 168 169switch(ce->ce_mode & S_IFMT) { 170case S_IFREG: 171 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 172/* We consider only the owner x bit to be relevant for 173 * "mode changes" 174 */ 175if(trust_executable_bit && 176(0100& (ce->ce_mode ^ st->st_mode))) 177 changed |= MODE_CHANGED; 178break; 179case S_IFLNK: 180if(!S_ISLNK(st->st_mode) && 181(has_symlinks || !S_ISREG(st->st_mode))) 182 changed |= TYPE_CHANGED; 183break; 184case S_IFGITLINK: 185/* We ignore most of the st_xxx fields for gitlinks */ 186if(!S_ISDIR(st->st_mode)) 187 changed |= TYPE_CHANGED; 188else if(ce_compare_gitlink(ce)) 189 changed |= DATA_CHANGED; 190return changed; 191default: 192die("internal error: ce_mode is%o", ce->ce_mode); 193} 194if(ce->ce_mtime.sec != (unsigned int)st->st_mtime) 195 changed |= MTIME_CHANGED; 196if(trust_ctime && ce->ce_ctime.sec != (unsigned int)st->st_ctime) 197 changed |= CTIME_CHANGED; 198 199#ifdef USE_NSEC 200if(ce->ce_mtime.nsec !=ST_MTIME_NSEC(*st)) 201 changed |= MTIME_CHANGED; 202if(trust_ctime && ce->ce_ctime.nsec !=ST_CTIME_NSEC(*st)) 203 changed |= CTIME_CHANGED; 204#endif 205 206if(ce->ce_uid != (unsigned int) st->st_uid || 207 ce->ce_gid != (unsigned int) st->st_gid) 208 changed |= OWNER_CHANGED; 209if(ce->ce_ino != (unsigned int) st->st_ino) 210 changed |= INODE_CHANGED; 211 212#ifdef USE_STDEV 213/* 214 * st_dev breaks on network filesystems where different 215 * clients will have different views of what "device" 216 * the filesystem is on 217 */ 218if(ce->ce_dev != (unsigned int) st->st_dev) 219 changed |= INODE_CHANGED; 220#endif 221 222if(ce->ce_size != (unsigned int) st->st_size) 223 changed |= DATA_CHANGED; 224 225/* Racily smudged entry? */ 226if(!ce->ce_size) { 227if(!is_empty_blob_sha1(ce->sha1)) 228 changed |= DATA_CHANGED; 229} 230 231return changed; 232} 233 234static intis_racy_timestamp(const struct index_state *istate,struct cache_entry *ce) 235{ 236return(!S_ISGITLINK(ce->ce_mode) && 237 istate->timestamp.sec && 238#ifdef USE_NSEC 239/* nanosecond timestamped files can also be racy! */ 240(istate->timestamp.sec < ce->ce_mtime.sec || 241(istate->timestamp.sec == ce->ce_mtime.sec && 242 istate->timestamp.nsec <= ce->ce_mtime.nsec)) 243#else 244 istate->timestamp.sec <= ce->ce_mtime.sec 245#endif 246); 247} 248 249intie_match_stat(const struct index_state *istate, 250struct cache_entry *ce,struct stat *st, 251unsigned int options) 252{ 253unsigned int changed; 254int ignore_valid = options & CE_MATCH_IGNORE_VALID; 255int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; 256int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 257 258/* 259 * If it's marked as always valid in the index, it's 260 * valid whatever the checked-out copy says. 261 * 262 * skip-worktree has the same effect with higher precedence 263 */ 264if(!ignore_skip_worktree &&ce_skip_worktree(ce)) 265return0; 266if(!ignore_valid && (ce->ce_flags & CE_VALID)) 267return0; 268 269/* 270 * Intent-to-add entries have not been added, so the index entry 271 * by definition never matches what is in the work tree until it 272 * actually gets added. 273 */ 274if(ce->ce_flags & CE_INTENT_TO_ADD) 275return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; 276 277 changed =ce_match_stat_basic(ce, st); 278 279/* 280 * Within 1 second of this sequence: 281 * echo xyzzy >file && git-update-index --add file 282 * running this command: 283 * echo frotz >file 284 * would give a falsely clean cache entry. The mtime and 285 * length match the cache, and other stat fields do not change. 286 * 287 * We could detect this at update-index time (the cache entry 288 * being registered/updated records the same time as "now") 289 * and delay the return from git-update-index, but that would 290 * effectively mean we can make at most one commit per second, 291 * which is not acceptable. Instead, we check cache entries 292 * whose mtime are the same as the index file timestamp more 293 * carefully than others. 294 */ 295if(!changed &&is_racy_timestamp(istate, ce)) { 296if(assume_racy_is_modified) 297 changed |= DATA_CHANGED; 298else 299 changed |=ce_modified_check_fs(ce, st); 300} 301 302return changed; 303} 304 305intie_modified(const struct index_state *istate, 306struct cache_entry *ce,struct stat *st,unsigned int options) 307{ 308int changed, changed_fs; 309 310 changed =ie_match_stat(istate, ce, st, options); 311if(!changed) 312return0; 313/* 314 * If the mode or type has changed, there's no point in trying 315 * to refresh the entry - it's not going to match 316 */ 317if(changed & (MODE_CHANGED | TYPE_CHANGED)) 318return changed; 319 320/* 321 * Immediately after read-tree or update-index --cacheinfo, 322 * the length field is zero, as we have never even read the 323 * lstat(2) information once, and we cannot trust DATA_CHANGED 324 * returned by ie_match_stat() which in turn was returned by 325 * ce_match_stat_basic() to signal that the filesize of the 326 * blob changed. We have to actually go to the filesystem to 327 * see if the contents match, and if so, should answer "unchanged". 328 * 329 * The logic does not apply to gitlinks, as ce_match_stat_basic() 330 * already has checked the actual HEAD from the filesystem in the 331 * subproject. If ie_match_stat() already said it is different, 332 * then we know it is. 333 */ 334if((changed & DATA_CHANGED) && 335(S_ISGITLINK(ce->ce_mode) || ce->ce_size !=0)) 336return changed; 337 338 changed_fs =ce_modified_check_fs(ce, st); 339if(changed_fs) 340return changed | changed_fs; 341return0; 342} 343 344intbase_name_compare(const char*name1,int len1,int mode1, 345const char*name2,int len2,int mode2) 346{ 347unsigned char c1, c2; 348int len = len1 < len2 ? len1 : len2; 349int cmp; 350 351 cmp =memcmp(name1, name2, len); 352if(cmp) 353return cmp; 354 c1 = name1[len]; 355 c2 = name2[len]; 356if(!c1 &&S_ISDIR(mode1)) 357 c1 ='/'; 358if(!c2 &&S_ISDIR(mode2)) 359 c2 ='/'; 360return(c1 < c2) ? -1: (c1 > c2) ?1:0; 361} 362 363/* 364 * df_name_compare() is identical to base_name_compare(), except it 365 * compares conflicting directory/file entries as equal. Note that 366 * while a directory name compares as equal to a regular file, they 367 * then individually compare _differently_ to a filename that has 368 * a dot after the basename (because '\0' < '.' < '/'). 369 * 370 * This is used by routines that want to traverse the git namespace 371 * but then handle conflicting entries together when possible. 372 */ 373intdf_name_compare(const char*name1,int len1,int mode1, 374const char*name2,int len2,int mode2) 375{ 376int len = len1 < len2 ? len1 : len2, cmp; 377unsigned char c1, c2; 378 379 cmp =memcmp(name1, name2, len); 380if(cmp) 381return cmp; 382/* Directories and files compare equal (same length, same name) */ 383if(len1 == len2) 384return0; 385 c1 = name1[len]; 386if(!c1 &&S_ISDIR(mode1)) 387 c1 ='/'; 388 c2 = name2[len]; 389if(!c2 &&S_ISDIR(mode2)) 390 c2 ='/'; 391if(c1 =='/'&& !c2) 392return0; 393if(c2 =='/'&& !c1) 394return0; 395return c1 - c2; 396} 397 398intcache_name_compare(const char*name1,int flags1,const char*name2,int flags2) 399{ 400int len1, len2, len, cmp; 401 402 len1 = flags1 & CE_NAMEMASK; 403if(CE_NAMEMASK <= len1) 404 len1 =strlen(name1 + CE_NAMEMASK) + CE_NAMEMASK; 405 len2 = flags2 & CE_NAMEMASK; 406if(CE_NAMEMASK <= len2) 407 len2 =strlen(name2 + CE_NAMEMASK) + CE_NAMEMASK; 408 len = len1 < len2 ? len1 : len2; 409 410 cmp =memcmp(name1, name2, len); 411if(cmp) 412return cmp; 413if(len1 < len2) 414return-1; 415if(len1 > len2) 416return1; 417 418/* Compare stages */ 419 flags1 &= CE_STAGEMASK; 420 flags2 &= CE_STAGEMASK; 421 422if(flags1 < flags2) 423return-1; 424if(flags1 > flags2) 425return1; 426return0; 427} 428 429intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 430{ 431int first, last; 432 433 first =0; 434 last = istate->cache_nr; 435while(last > first) { 436int next = (last + first) >>1; 437struct cache_entry *ce = istate->cache[next]; 438int cmp =cache_name_compare(name, namelen, ce->name, ce->ce_flags); 439if(!cmp) 440return next; 441if(cmp <0) { 442 last = next; 443continue; 444} 445 first = next+1; 446} 447return-first-1; 448} 449 450/* Remove entry, return true if there are more entries to go.. */ 451intremove_index_entry_at(struct index_state *istate,int pos) 452{ 453struct cache_entry *ce = istate->cache[pos]; 454 455record_resolve_undo(istate, ce); 456remove_name_hash(ce); 457 istate->cache_changed =1; 458 istate->cache_nr--; 459if(pos >= istate->cache_nr) 460return0; 461memmove(istate->cache + pos, 462 istate->cache + pos +1, 463(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 464return1; 465} 466 467/* 468 * Remove all cache ententries marked for removal, that is where 469 * CE_REMOVE is set in ce_flags. This is much more effective than 470 * calling remove_index_entry_at() for each entry to be removed. 471 */ 472voidremove_marked_cache_entries(struct index_state *istate) 473{ 474struct cache_entry **ce_array = istate->cache; 475unsigned int i, j; 476 477for(i = j =0; i < istate->cache_nr; i++) { 478if(ce_array[i]->ce_flags & CE_REMOVE) 479remove_name_hash(ce_array[i]); 480else 481 ce_array[j++] = ce_array[i]; 482} 483 istate->cache_changed =1; 484 istate->cache_nr = j; 485} 486 487intremove_file_from_index(struct index_state *istate,const char*path) 488{ 489int pos =index_name_pos(istate, path,strlen(path)); 490if(pos <0) 491 pos = -pos-1; 492cache_tree_invalidate_path(istate->cache_tree, path); 493while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 494remove_index_entry_at(istate, pos); 495return0; 496} 497 498static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 499{ 500return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 501} 502 503static intindex_name_pos_also_unmerged(struct index_state *istate, 504const char*path,int namelen) 505{ 506int pos =index_name_pos(istate, path, namelen); 507struct cache_entry *ce; 508 509if(pos >=0) 510return pos; 511 512/* maybe unmerged? */ 513 pos = -1- pos; 514if(pos >= istate->cache_nr || 515compare_name((ce = istate->cache[pos]), path, namelen)) 516return-1; 517 518/* order of preference: stage 2, 1, 3 */ 519if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 520ce_stage((ce = istate->cache[pos +1])) ==2&& 521!compare_name(ce, path, namelen)) 522 pos++; 523return pos; 524} 525 526static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 527{ 528int len =ce_namelen(ce); 529returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 530} 531 532/* 533 * If we add a filename that aliases in the cache, we will use the 534 * name that we already have - but we don't want to update the same 535 * alias twice, because that implies that there were actually two 536 * different files with aliasing names! 537 * 538 * So we use the CE_ADDED flag to verify that the alias was an old 539 * one before we accept it as 540 */ 541static struct cache_entry *create_alias_ce(struct cache_entry *ce,struct cache_entry *alias) 542{ 543int len; 544struct cache_entry *new; 545 546if(alias->ce_flags & CE_ADDED) 547die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 548 549/* Ok, create the new entry using the name of the existing alias */ 550 len =ce_namelen(alias); 551new=xcalloc(1,cache_entry_size(len)); 552memcpy(new->name, alias->name, len); 553copy_cache_entry(new, ce); 554free(ce); 555return new; 556} 557 558static voidrecord_intent_to_add(struct cache_entry *ce) 559{ 560unsigned char sha1[20]; 561if(write_sha1_file("",0, blob_type, sha1)) 562die("cannot create an empty blob in the object database"); 563hashcpy(ce->sha1, sha1); 564} 565 566intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 567{ 568int size, namelen, was_same; 569 mode_t st_mode = st->st_mode; 570struct cache_entry *ce, *alias; 571unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; 572int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 573int pretend = flags & ADD_CACHE_PRETEND; 574int intent_only = flags & ADD_CACHE_INTENT; 575int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 576(intent_only ? ADD_CACHE_NEW_ONLY :0)); 577 578if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 579returnerror("%s: can only add regular files, symbolic links or git-directories", path); 580 581 namelen =strlen(path); 582if(S_ISDIR(st_mode)) { 583while(namelen && path[namelen-1] =='/') 584 namelen--; 585} 586 size =cache_entry_size(namelen); 587 ce =xcalloc(1, size); 588memcpy(ce->name, path, namelen); 589 ce->ce_flags = namelen; 590if(!intent_only) 591fill_stat_cache_info(ce, st); 592else 593 ce->ce_flags |= CE_INTENT_TO_ADD; 594 595if(trust_executable_bit && has_symlinks) 596 ce->ce_mode =create_ce_mode(st_mode); 597else{ 598/* If there is an existing entry, pick the mode bits and type 599 * from it, otherwise assume unexecutable regular file. 600 */ 601struct cache_entry *ent; 602int pos =index_name_pos_also_unmerged(istate, path, namelen); 603 604 ent = (0<= pos) ? istate->cache[pos] : NULL; 605 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 606} 607 608/* When core.ignorecase=true, determine if a directory of the same name but differing 609 * case already exists within the Git repository. If it does, ensure the directory 610 * case of the file being added to the repository matches (is folded into) the existing 611 * entry's directory case. 612 */ 613if(ignore_case) { 614const char*startPtr = ce->name; 615const char*ptr = startPtr; 616while(*ptr) { 617while(*ptr && *ptr !='/') 618++ptr; 619if(*ptr =='/') { 620struct cache_entry *foundce; 621++ptr; 622 foundce =index_name_exists(&the_index, ce->name, ptr - ce->name, ignore_case); 623if(foundce) { 624memcpy((void*)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); 625 startPtr = ptr; 626} 627} 628} 629} 630 631 alias =index_name_exists(istate, ce->name,ce_namelen(ce), ignore_case); 632if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 633/* Nothing changed, really */ 634free(ce); 635if(!S_ISGITLINK(alias->ce_mode)) 636ce_mark_uptodate(alias); 637 alias->ce_flags |= CE_ADDED; 638return0; 639} 640if(!intent_only) { 641if(index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) 642returnerror("unable to index file%s", path); 643}else 644record_intent_to_add(ce); 645 646if(ignore_case && alias &&different_name(ce, alias)) 647 ce =create_alias_ce(ce, alias); 648 ce->ce_flags |= CE_ADDED; 649 650/* It was suspected to be racily clean, but it turns out to be Ok */ 651 was_same = (alias && 652!ce_stage(alias) && 653!hashcmp(alias->sha1, ce->sha1) && 654 ce->ce_mode == alias->ce_mode); 655 656if(pretend) 657; 658else if(add_index_entry(istate, ce, add_option)) 659returnerror("unable to add%sto index",path); 660if(verbose && !was_same) 661printf("add '%s'\n", path); 662return0; 663} 664 665intadd_file_to_index(struct index_state *istate,const char*path,int flags) 666{ 667struct stat st; 668if(lstat(path, &st)) 669die_errno("unable to stat '%s'", path); 670returnadd_to_index(istate, path, &st, flags); 671} 672 673struct cache_entry *make_cache_entry(unsigned int mode, 674const unsigned char*sha1,const char*path,int stage, 675int refresh) 676{ 677int size, len; 678struct cache_entry *ce; 679 680if(!verify_path(path)) { 681error("Invalid path '%s'", path); 682return NULL; 683} 684 685 len =strlen(path); 686 size =cache_entry_size(len); 687 ce =xcalloc(1, size); 688 689hashcpy(ce->sha1, sha1); 690memcpy(ce->name, path, len); 691 ce->ce_flags =create_ce_flags(len, stage); 692 ce->ce_mode =create_ce_mode(mode); 693 694if(refresh) 695returnrefresh_cache_entry(ce,0); 696 697return ce; 698} 699 700intce_same_name(struct cache_entry *a,struct cache_entry *b) 701{ 702int len =ce_namelen(a); 703returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 704} 705 706intce_path_match(const struct cache_entry *ce,const struct pathspec *pathspec) 707{ 708returnmatch_pathspec_depth(pathspec, ce->name,ce_namelen(ce),0, NULL); 709} 710 711/* 712 * We fundamentally don't like some paths: we don't want 713 * dot or dot-dot anywhere, and for obvious reasons don't 714 * want to recurse into ".git" either. 715 * 716 * Also, we don't want double slashes or slashes at the 717 * end that can make pathnames ambiguous. 718 */ 719static intverify_dotfile(const char*rest) 720{ 721/* 722 * The first character was '.', but that 723 * has already been discarded, we now test 724 * the rest. 725 */ 726 727/* "." is not allowed */ 728if(*rest =='\0'||is_dir_sep(*rest)) 729return0; 730 731switch(*rest) { 732/* 733 * ".git" followed by NUL or slash is bad. This 734 * shares the path end test with the ".." case. 735 */ 736case'g': 737if(rest[1] !='i') 738break; 739if(rest[2] !='t') 740break; 741 rest +=2; 742/* fallthrough */ 743case'.': 744if(rest[1] =='\0'||is_dir_sep(rest[1])) 745return0; 746} 747return1; 748} 749 750intverify_path(const char*path) 751{ 752char c; 753 754if(has_dos_drive_prefix(path)) 755return0; 756 757goto inside; 758for(;;) { 759if(!c) 760return1; 761if(is_dir_sep(c)) { 762inside: 763 c = *path++; 764if((c =='.'&& !verify_dotfile(path)) || 765is_dir_sep(c) || c =='\0') 766return0; 767} 768 c = *path++; 769} 770} 771 772/* 773 * Do we have another file that has the beginning components being a 774 * proper superset of the name we're trying to add? 775 */ 776static inthas_file_name(struct index_state *istate, 777const struct cache_entry *ce,int pos,int ok_to_replace) 778{ 779int retval =0; 780int len =ce_namelen(ce); 781int stage =ce_stage(ce); 782const char*name = ce->name; 783 784while(pos < istate->cache_nr) { 785struct cache_entry *p = istate->cache[pos++]; 786 787if(len >=ce_namelen(p)) 788break; 789if(memcmp(name, p->name, len)) 790break; 791if(ce_stage(p) != stage) 792continue; 793if(p->name[len] !='/') 794continue; 795if(p->ce_flags & CE_REMOVE) 796continue; 797 retval = -1; 798if(!ok_to_replace) 799break; 800remove_index_entry_at(istate, --pos); 801} 802return retval; 803} 804 805/* 806 * Do we have another file with a pathname that is a proper 807 * subset of the name we're trying to add? 808 */ 809static inthas_dir_name(struct index_state *istate, 810const struct cache_entry *ce,int pos,int ok_to_replace) 811{ 812int retval =0; 813int stage =ce_stage(ce); 814const char*name = ce->name; 815const char*slash = name +ce_namelen(ce); 816 817for(;;) { 818int len; 819 820for(;;) { 821if(*--slash =='/') 822break; 823if(slash <= ce->name) 824return retval; 825} 826 len = slash - name; 827 828 pos =index_name_pos(istate, name,create_ce_flags(len, stage)); 829if(pos >=0) { 830/* 831 * Found one, but not so fast. This could 832 * be a marker that says "I was here, but 833 * I am being removed". Such an entry is 834 * not a part of the resulting tree, and 835 * it is Ok to have a directory at the same 836 * path. 837 */ 838if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 839 retval = -1; 840if(!ok_to_replace) 841break; 842remove_index_entry_at(istate, pos); 843continue; 844} 845} 846else 847 pos = -pos-1; 848 849/* 850 * Trivial optimization: if we find an entry that 851 * already matches the sub-directory, then we know 852 * we're ok, and we can exit. 853 */ 854while(pos < istate->cache_nr) { 855struct cache_entry *p = istate->cache[pos]; 856if((ce_namelen(p) <= len) || 857(p->name[len] !='/') || 858memcmp(p->name, name, len)) 859break;/* not our subdirectory */ 860if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 861/* 862 * p is at the same stage as our entry, and 863 * is a subdirectory of what we are looking 864 * at, so we cannot have conflicts at our 865 * level or anything shorter. 866 */ 867return retval; 868 pos++; 869} 870} 871return retval; 872} 873 874/* We may be in a situation where we already have path/file and path 875 * is being added, or we already have path and path/file is being 876 * added. Either one would result in a nonsense tree that has path 877 * twice when git-write-tree tries to write it out. Prevent it. 878 * 879 * If ok-to-replace is specified, we remove the conflicting entries 880 * from the cache so the caller should recompute the insert position. 881 * When this happens, we return non-zero. 882 */ 883static intcheck_file_directory_conflict(struct index_state *istate, 884const struct cache_entry *ce, 885int pos,int ok_to_replace) 886{ 887int retval; 888 889/* 890 * When ce is an "I am going away" entry, we allow it to be added 891 */ 892if(ce->ce_flags & CE_REMOVE) 893return0; 894 895/* 896 * We check if the path is a sub-path of a subsequent pathname 897 * first, since removing those will not change the position 898 * in the array. 899 */ 900 retval =has_file_name(istate, ce, pos, ok_to_replace); 901 902/* 903 * Then check if the path might have a clashing sub-directory 904 * before it. 905 */ 906return retval +has_dir_name(istate, ce, pos, ok_to_replace); 907} 908 909static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 910{ 911int pos; 912int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 913int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 914int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 915int new_only = option & ADD_CACHE_NEW_ONLY; 916 917cache_tree_invalidate_path(istate->cache_tree, ce->name); 918 pos =index_name_pos(istate, ce->name, ce->ce_flags); 919 920/* existing match? Just replace it. */ 921if(pos >=0) { 922if(!new_only) 923replace_index_entry(istate, pos, ce); 924return0; 925} 926 pos = -pos-1; 927 928/* 929 * Inserting a merged entry ("stage 0") into the index 930 * will always replace all non-merged entries.. 931 */ 932if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 933while(ce_same_name(istate->cache[pos], ce)) { 934 ok_to_add =1; 935if(!remove_index_entry_at(istate, pos)) 936break; 937} 938} 939 940if(!ok_to_add) 941return-1; 942if(!verify_path(ce->name)) 943returnerror("Invalid path '%s'", ce->name); 944 945if(!skip_df_check && 946check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 947if(!ok_to_replace) 948returnerror("'%s' appears as both a file and as a directory", 949 ce->name); 950 pos =index_name_pos(istate, ce->name, ce->ce_flags); 951 pos = -pos-1; 952} 953return pos +1; 954} 955 956intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 957{ 958int pos; 959 960if(option & ADD_CACHE_JUST_APPEND) 961 pos = istate->cache_nr; 962else{ 963int ret; 964 ret =add_index_entry_with_check(istate, ce, option); 965if(ret <=0) 966return ret; 967 pos = ret -1; 968} 969 970/* Make sure the array is big enough .. */ 971if(istate->cache_nr == istate->cache_alloc) { 972 istate->cache_alloc =alloc_nr(istate->cache_alloc); 973 istate->cache =xrealloc(istate->cache, 974 istate->cache_alloc *sizeof(struct cache_entry *)); 975} 976 977/* Add it in.. */ 978 istate->cache_nr++; 979if(istate->cache_nr > pos +1) 980memmove(istate->cache + pos +1, 981 istate->cache + pos, 982(istate->cache_nr - pos -1) *sizeof(ce)); 983set_index_entry(istate, pos, ce); 984 istate->cache_changed =1; 985return0; 986} 987 988/* 989 * "refresh" does not calculate a new sha1 file or bring the 990 * cache up-to-date for mode/content changes. But what it 991 * _does_ do is to "re-match" the stat information of a file 992 * with the cache, so that you can refresh the cache for a 993 * file that hasn't been changed but where the stat entry is 994 * out of date. 995 * 996 * For example, you'd want to do this after doing a "git-read-tree", 997 * to link up the stat cache details with the proper files. 998 */ 999static struct cache_entry *refresh_cache_ent(struct index_state *istate,1000struct cache_entry *ce,1001unsigned int options,int*err,1002int*changed_ret)1003{1004struct stat st;1005struct cache_entry *updated;1006int changed, size;1007int ignore_valid = options & CE_MATCH_IGNORE_VALID;1008int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;10091010if(ce_uptodate(ce))1011return ce;10121013/*1014 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1015 * that the change to the work tree does not matter and told1016 * us not to worry.1017 */1018if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1019ce_mark_uptodate(ce);1020return ce;1021}1022if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1023ce_mark_uptodate(ce);1024return ce;1025}10261027if(lstat(ce->name, &st) <0) {1028if(err)1029*err = errno;1030return NULL;1031}10321033 changed =ie_match_stat(istate, ce, &st, options);1034if(changed_ret)1035*changed_ret = changed;1036if(!changed) {1037/*1038 * The path is unchanged. If we were told to ignore1039 * valid bit, then we did the actual stat check and1040 * found that the entry is unmodified. If the entry1041 * is not marked VALID, this is the place to mark it1042 * valid again, under "assume unchanged" mode.1043 */1044if(ignore_valid && assume_unchanged &&1045!(ce->ce_flags & CE_VALID))1046;/* mark this one VALID again */1047else{1048/*1049 * We do not mark the index itself "modified"1050 * because CE_UPTODATE flag is in-core only;1051 * we are not going to write this change out.1052 */1053if(!S_ISGITLINK(ce->ce_mode))1054ce_mark_uptodate(ce);1055return ce;1056}1057}10581059if(ie_modified(istate, ce, &st, options)) {1060if(err)1061*err = EINVAL;1062return NULL;1063}10641065 size =ce_size(ce);1066 updated =xmalloc(size);1067memcpy(updated, ce, size);1068fill_stat_cache_info(updated, &st);1069/*1070 * If ignore_valid is not set, we should leave CE_VALID bit1071 * alone. Otherwise, paths marked with --no-assume-unchanged1072 * (i.e. things to be edited) will reacquire CE_VALID bit1073 * automatically, which is not really what we want.1074 */1075if(!ignore_valid && assume_unchanged &&1076!(ce->ce_flags & CE_VALID))1077 updated->ce_flags &= ~CE_VALID;10781079return updated;1080}10811082static voidshow_file(const char* fmt,const char* name,int in_porcelain,1083int* first,const char*header_msg)1084{1085if(in_porcelain && *first && header_msg) {1086printf("%s\n", header_msg);1087*first =0;1088}1089printf(fmt, name);1090}10911092intrefresh_index(struct index_state *istate,unsigned int flags,const char**pathspec,1093char*seen,const char*header_msg)1094{1095int i;1096int has_errors =0;1097int really = (flags & REFRESH_REALLY) !=0;1098int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1099int quiet = (flags & REFRESH_QUIET) !=0;1100int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1101int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1102int first =1;1103int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1104unsigned int options = really ? CE_MATCH_IGNORE_VALID :0;1105const char*modified_fmt;1106const char*deleted_fmt;1107const char*typechange_fmt;1108const char*added_fmt;1109const char*unmerged_fmt;11101111 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1112 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1113 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1114 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1115 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1116for(i =0; i < istate->cache_nr; i++) {1117struct cache_entry *ce, *new;1118int cache_errno =0;1119int changed =0;1120int filtered =0;11211122 ce = istate->cache[i];1123if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1124continue;11251126if(pathspec &&1127!match_pathspec(pathspec, ce->name,strlen(ce->name),0, seen))1128 filtered =1;11291130if(ce_stage(ce)) {1131while((i < istate->cache_nr) &&1132!strcmp(istate->cache[i]->name, ce->name))1133 i++;1134 i--;1135if(allow_unmerged)1136continue;1137if(!filtered)1138show_file(unmerged_fmt, ce->name, in_porcelain,1139&first, header_msg);1140 has_errors =1;1141continue;1142}11431144if(filtered)1145continue;11461147new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1148if(new== ce)1149continue;1150if(!new) {1151const char*fmt;11521153if(not_new && cache_errno == ENOENT)1154continue;1155if(really && cache_errno == EINVAL) {1156/* If we are doing --really-refresh that1157 * means the index is not valid anymore.1158 */1159 ce->ce_flags &= ~CE_VALID;1160 istate->cache_changed =1;1161}1162if(quiet)1163continue;11641165if(cache_errno == ENOENT)1166 fmt = deleted_fmt;1167else if(ce->ce_flags & CE_INTENT_TO_ADD)1168 fmt = added_fmt;/* must be before other checks */1169else if(changed & TYPE_CHANGED)1170 fmt = typechange_fmt;1171else1172 fmt = modified_fmt;1173show_file(fmt,1174 ce->name, in_porcelain, &first, header_msg);1175 has_errors =1;1176continue;1177}11781179replace_index_entry(istate, i,new);1180}1181return has_errors;1182}11831184static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really)1185{1186returnrefresh_cache_ent(&the_index, ce, really, NULL, NULL);1187}118811891190/*****************************************************************1191 * Index File I/O1192 *****************************************************************/11931194#define INDEX_FORMAT_DEFAULT 311951196/*1197 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1198 * Again - this is just a (very strong in practice) heuristic that1199 * the inode hasn't changed.1200 *1201 * We save the fields in big-endian order to allow using the1202 * index file over NFS transparently.1203 */1204struct ondisk_cache_entry {1205struct cache_time ctime;1206struct cache_time mtime;1207unsigned int dev;1208unsigned int ino;1209unsigned int mode;1210unsigned int uid;1211unsigned int gid;1212unsigned int size;1213unsigned char sha1[20];1214unsigned short flags;1215char name[FLEX_ARRAY];/* more */1216};12171218/*1219 * This struct is used when CE_EXTENDED bit is 11220 * The struct must match ondisk_cache_entry exactly from1221 * ctime till flags1222 */1223struct ondisk_cache_entry_extended {1224struct cache_time ctime;1225struct cache_time mtime;1226unsigned int dev;1227unsigned int ino;1228unsigned int mode;1229unsigned int uid;1230unsigned int gid;1231unsigned int size;1232unsigned char sha1[20];1233unsigned short flags;1234unsigned short flags2;1235char name[FLEX_ARRAY];/* more */1236};12371238/* These are only used for v3 or lower */1239#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1240#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1241#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1242#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1243 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1244 ondisk_cache_entry_size(ce_namelen(ce)))12451246static intverify_hdr(struct cache_header *hdr,unsigned long size)1247{1248 git_SHA_CTX c;1249unsigned char sha1[20];1250int hdr_version;12511252if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1253returnerror("bad signature");1254 hdr_version =ntohl(hdr->hdr_version);1255if(hdr_version <2||4< hdr_version)1256returnerror("bad index version%d", hdr_version);1257git_SHA1_Init(&c);1258git_SHA1_Update(&c, hdr, size -20);1259git_SHA1_Final(sha1, &c);1260if(hashcmp(sha1, (unsigned char*)hdr + size -20))1261returnerror("bad index file sha1 signature");1262return0;1263}12641265static intread_index_extension(struct index_state *istate,1266const char*ext,void*data,unsigned long sz)1267{1268switch(CACHE_EXT(ext)) {1269case CACHE_EXT_TREE:1270 istate->cache_tree =cache_tree_read(data, sz);1271break;1272case CACHE_EXT_RESOLVE_UNDO:1273 istate->resolve_undo =resolve_undo_read(data, sz);1274break;1275default:1276if(*ext <'A'||'Z'< *ext)1277returnerror("index uses %.4s extension, which we do not understand",1278 ext);1279fprintf(stderr,"ignoring %.4s extension\n", ext);1280break;1281}1282return0;1283}12841285intread_index(struct index_state *istate)1286{1287returnread_index_from(istate,get_index_file());1288}12891290#ifndef NEEDS_ALIGNED_ACCESS1291#define ntoh_s(var) ntohs(var)1292#define ntoh_l(var) ntohl(var)1293#else1294staticinlineuint16_tntoh_s_force_align(void*p)1295{1296uint16_t x;1297memcpy(&x, p,sizeof(x));1298returnntohs(x);1299}1300staticinlineuint32_tntoh_l_force_align(void*p)1301{1302uint32_t x;1303memcpy(&x, p,sizeof(x));1304returnntohl(x);1305}1306#define ntoh_s(var) ntoh_s_force_align(&(var))1307#define ntoh_l(var) ntoh_l_force_align(&(var))1308#endif13091310static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1311unsigned int flags,1312const char*name,1313size_t len)1314{1315struct cache_entry *ce =xmalloc(cache_entry_size(len));13161317 ce->ce_ctime.sec =ntoh_l(ondisk->ctime.sec);1318 ce->ce_mtime.sec =ntoh_l(ondisk->mtime.sec);1319 ce->ce_ctime.nsec =ntoh_l(ondisk->ctime.nsec);1320 ce->ce_mtime.nsec =ntoh_l(ondisk->mtime.nsec);1321 ce->ce_dev =ntoh_l(ondisk->dev);1322 ce->ce_ino =ntoh_l(ondisk->ino);1323 ce->ce_mode =ntoh_l(ondisk->mode);1324 ce->ce_uid =ntoh_l(ondisk->uid);1325 ce->ce_gid =ntoh_l(ondisk->gid);1326 ce->ce_size =ntoh_l(ondisk->size);1327 ce->ce_flags = flags;1328hashcpy(ce->sha1, ondisk->sha1);1329memcpy(ce->name, name, len);1330 ce->name[len] ='\0';1331return ce;1332}13331334/*1335 * Adjacent cache entries tend to share the leading paths, so it makes1336 * sense to only store the differences in later entries. In the v41337 * on-disk format of the index, each on-disk cache entry stores the1338 * number of bytes to be stripped from the end of the previous name,1339 * and the bytes to append to the result, to come up with its name.1340 */1341static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1342{1343const unsigned char*ep, *cp = (const unsigned char*)cp_;1344size_t len =decode_varint(&cp);13451346if(name->len < len)1347die("malformed name field in the index");1348strbuf_remove(name, name->len - len, len);1349for(ep = cp; *ep; ep++)1350;/* find the end */1351strbuf_add(name, cp, ep - cp);1352return(const char*)ep +1- cp_;1353}13541355static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1356unsigned long*ent_size,1357struct strbuf *previous_name)1358{1359struct cache_entry *ce;1360size_t len;1361const char*name;1362unsigned int flags;13631364/* On-disk flags are just 16 bits */1365 flags =ntoh_s(ondisk->flags);1366 len = flags & CE_NAMEMASK;13671368if(flags & CE_EXTENDED) {1369struct ondisk_cache_entry_extended *ondisk2;1370int extended_flags;1371 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1372 extended_flags =ntoh_s(ondisk2->flags2) <<16;1373/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1374if(extended_flags & ~CE_EXTENDED_FLAGS)1375die("Unknown index entry format%08x", extended_flags);1376 flags |= extended_flags;1377 name = ondisk2->name;1378}1379else1380 name = ondisk->name;13811382if(!previous_name) {1383/* v3 and earlier */1384if(len == CE_NAMEMASK)1385 len =strlen(name);1386 ce =cache_entry_from_ondisk(ondisk, flags, name, len);13871388*ent_size =ondisk_ce_size(ce);1389}else{1390unsigned long consumed;1391 consumed =expand_name_field(previous_name, name);1392 ce =cache_entry_from_ondisk(ondisk, flags,1393 previous_name->buf,1394 previous_name->len);13951396*ent_size = (name - ((char*)ondisk)) + consumed;1397}1398return ce;1399}14001401/* remember to discard_cache() before reading a different cache! */1402intread_index_from(struct index_state *istate,const char*path)1403{1404int fd, i;1405struct stat st;1406unsigned long src_offset;1407struct cache_header *hdr;1408void*mmap;1409size_t mmap_size;1410struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;14111412 errno = EBUSY;1413if(istate->initialized)1414return istate->cache_nr;14151416 errno = ENOENT;1417 istate->timestamp.sec =0;1418 istate->timestamp.nsec =0;1419 fd =open(path, O_RDONLY);1420if(fd <0) {1421if(errno == ENOENT)1422return0;1423die_errno("index file open failed");1424}14251426if(fstat(fd, &st))1427die_errno("cannot stat the open index");14281429 errno = EINVAL;1430 mmap_size =xsize_t(st.st_size);1431if(mmap_size <sizeof(struct cache_header) +20)1432die("index file smaller than expected");14331434 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1435close(fd);1436if(mmap == MAP_FAILED)1437die_errno("unable to map index file");14381439 hdr = mmap;1440if(verify_hdr(hdr, mmap_size) <0)1441goto unmap;14421443 istate->version =ntohl(hdr->hdr_version);1444 istate->cache_nr =ntohl(hdr->hdr_entries);1445 istate->cache_alloc =alloc_nr(istate->cache_nr);1446 istate->cache =xcalloc(istate->cache_alloc,sizeof(struct cache_entry *));1447 istate->initialized =1;14481449if(istate->version ==4)1450 previous_name = &previous_name_buf;1451else1452 previous_name = NULL;14531454 src_offset =sizeof(*hdr);1455for(i =0; i < istate->cache_nr; i++) {1456struct ondisk_cache_entry *disk_ce;1457struct cache_entry *ce;1458unsigned long consumed;14591460 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1461 ce =create_from_disk(disk_ce, &consumed, previous_name);1462set_index_entry(istate, i, ce);14631464 src_offset += consumed;1465}1466strbuf_release(&previous_name_buf);1467 istate->timestamp.sec = st.st_mtime;1468 istate->timestamp.nsec =ST_MTIME_NSEC(st);14691470while(src_offset <= mmap_size -20-8) {1471/* After an array of active_nr index entries,1472 * there can be arbitrary number of extended1473 * sections, each of which is prefixed with1474 * extension name (4-byte) and section length1475 * in 4-byte network byte order.1476 */1477uint32_t extsize;1478memcpy(&extsize, (char*)mmap + src_offset +4,4);1479 extsize =ntohl(extsize);1480if(read_index_extension(istate,1481(const char*) mmap + src_offset,1482(char*) mmap + src_offset +8,1483 extsize) <0)1484goto unmap;1485 src_offset +=8;1486 src_offset += extsize;1487}1488munmap(mmap, mmap_size);1489return istate->cache_nr;14901491unmap:1492munmap(mmap, mmap_size);1493 errno = EINVAL;1494die("index file corrupt");1495}14961497intis_index_unborn(struct index_state *istate)1498{1499return(!istate->cache_nr && !istate->timestamp.sec);1500}15011502intdiscard_index(struct index_state *istate)1503{1504int i;15051506for(i =0; i < istate->cache_nr; i++)1507free(istate->cache[i]);1508resolve_undo_clear_index(istate);1509 istate->cache_nr =0;1510 istate->cache_changed =0;1511 istate->timestamp.sec =0;1512 istate->timestamp.nsec =0;1513 istate->name_hash_initialized =0;1514free_hash(&istate->name_hash);1515cache_tree_free(&(istate->cache_tree));1516 istate->initialized =0;15171518/* no need to throw away allocated active_cache */1519return0;1520}15211522intunmerged_index(const struct index_state *istate)1523{1524int i;1525for(i =0; i < istate->cache_nr; i++) {1526if(ce_stage(istate->cache[i]))1527return1;1528}1529return0;1530}15311532#define WRITE_BUFFER_SIZE 81921533static unsigned char write_buffer[WRITE_BUFFER_SIZE];1534static unsigned long write_buffer_len;15351536static intce_write_flush(git_SHA_CTX *context,int fd)1537{1538unsigned int buffered = write_buffer_len;1539if(buffered) {1540git_SHA1_Update(context, write_buffer, buffered);1541if(write_in_full(fd, write_buffer, buffered) != buffered)1542return-1;1543 write_buffer_len =0;1544}1545return0;1546}15471548static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1549{1550while(len) {1551unsigned int buffered = write_buffer_len;1552unsigned int partial = WRITE_BUFFER_SIZE - buffered;1553if(partial > len)1554 partial = len;1555memcpy(write_buffer + buffered, data, partial);1556 buffered += partial;1557if(buffered == WRITE_BUFFER_SIZE) {1558 write_buffer_len = buffered;1559if(ce_write_flush(context, fd))1560return-1;1561 buffered =0;1562}1563 write_buffer_len = buffered;1564 len -= partial;1565 data = (char*) data + partial;1566}1567return0;1568}15691570static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1571unsigned int ext,unsigned int sz)1572{1573 ext =htonl(ext);1574 sz =htonl(sz);1575return((ce_write(context, fd, &ext,4) <0) ||1576(ce_write(context, fd, &sz,4) <0)) ? -1:0;1577}15781579static intce_flush(git_SHA_CTX *context,int fd)1580{1581unsigned int left = write_buffer_len;15821583if(left) {1584 write_buffer_len =0;1585git_SHA1_Update(context, write_buffer, left);1586}15871588/* Flush first if not enough space for SHA1 signature */1589if(left +20> WRITE_BUFFER_SIZE) {1590if(write_in_full(fd, write_buffer, left) != left)1591return-1;1592 left =0;1593}15941595/* Append the SHA1 signature at the end */1596git_SHA1_Final(write_buffer + left, context);1597 left +=20;1598return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1599}16001601static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1602{1603/*1604 * The only thing we care about in this function is to smudge the1605 * falsely clean entry due to touch-update-touch race, so we leave1606 * everything else as they are. We are called for entries whose1607 * ce_mtime match the index file mtime.1608 *1609 * Note that this actually does not do much for gitlinks, for1610 * which ce_match_stat_basic() always goes to the actual1611 * contents. The caller checks with is_racy_timestamp() which1612 * always says "no" for gitlinks, so we are not called for them ;-)1613 */1614struct stat st;16151616if(lstat(ce->name, &st) <0)1617return;1618if(ce_match_stat_basic(ce, &st))1619return;1620if(ce_modified_check_fs(ce, &st)) {1621/* This is "racily clean"; smudge it. Note that this1622 * is a tricky code. At first glance, it may appear1623 * that it can break with this sequence:1624 *1625 * $ echo xyzzy >frotz1626 * $ git-update-index --add frotz1627 * $ : >frotz1628 * $ sleep 31629 * $ echo filfre >nitfol1630 * $ git-update-index --add nitfol1631 *1632 * but it does not. When the second update-index runs,1633 * it notices that the entry "frotz" has the same timestamp1634 * as index, and if we were to smudge it by resetting its1635 * size to zero here, then the object name recorded1636 * in index is the 6-byte file but the cached stat information1637 * becomes zero --- which would then match what we would1638 * obtain from the filesystem next time we stat("frotz").1639 *1640 * However, the second update-index, before calling1641 * this function, notices that the cached size is 61642 * bytes and what is on the filesystem is an empty1643 * file, and never calls us, so the cached size information1644 * for "frotz" stays 6 which does not match the filesystem.1645 */1646 ce->ce_size =0;1647}1648}16491650/* Copy miscellaneous fields but not the name */1651static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1652struct cache_entry *ce)1653{1654 ondisk->ctime.sec =htonl(ce->ce_ctime.sec);1655 ondisk->mtime.sec =htonl(ce->ce_mtime.sec);1656 ondisk->ctime.nsec =htonl(ce->ce_ctime.nsec);1657 ondisk->mtime.nsec =htonl(ce->ce_mtime.nsec);1658 ondisk->dev =htonl(ce->ce_dev);1659 ondisk->ino =htonl(ce->ce_ino);1660 ondisk->mode =htonl(ce->ce_mode);1661 ondisk->uid =htonl(ce->ce_uid);1662 ondisk->gid =htonl(ce->ce_gid);1663 ondisk->size =htonl(ce->ce_size);1664hashcpy(ondisk->sha1, ce->sha1);1665 ondisk->flags =htons(ce->ce_flags);1666if(ce->ce_flags & CE_EXTENDED) {1667struct ondisk_cache_entry_extended *ondisk2;1668 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1669 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1670return ondisk2->name;1671}1672else{1673return ondisk->name;1674}1675}16761677static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce,1678struct strbuf *previous_name)1679{1680int size;1681struct ondisk_cache_entry *ondisk;1682char*name;1683int result;16841685if(!previous_name) {1686 size =ondisk_ce_size(ce);1687 ondisk =xcalloc(1, size);1688 name =copy_cache_entry_to_ondisk(ondisk, ce);1689memcpy(name, ce->name,ce_namelen(ce));1690}else{1691int common, to_remove, prefix_size;1692unsigned char to_remove_vi[16];1693for(common =0;1694(ce->name[common] &&1695 common < previous_name->len &&1696 ce->name[common] == previous_name->buf[common]);1697 common++)1698;/* still matching */1699 to_remove = previous_name->len - common;1700 prefix_size =encode_varint(to_remove, to_remove_vi);17011702if(ce->ce_flags & CE_EXTENDED)1703 size =offsetof(struct ondisk_cache_entry_extended, name);1704else1705 size =offsetof(struct ondisk_cache_entry, name);1706 size += prefix_size + (ce_namelen(ce) - common +1);17071708 ondisk =xcalloc(1, size);1709 name =copy_cache_entry_to_ondisk(ondisk, ce);1710memcpy(name, to_remove_vi, prefix_size);1711memcpy(name + prefix_size, ce->name + common,ce_namelen(ce) - common);17121713strbuf_splice(previous_name, common, to_remove,1714 ce->name + common,ce_namelen(ce) - common);1715}17161717 result =ce_write(c, fd, ondisk, size);1718free(ondisk);1719return result;1720}17211722static inthas_racy_timestamp(struct index_state *istate)1723{1724int entries = istate->cache_nr;1725int i;17261727for(i =0; i < entries; i++) {1728struct cache_entry *ce = istate->cache[i];1729if(is_racy_timestamp(istate, ce))1730return1;1731}1732return0;1733}17341735/*1736 * Opportunisticly update the index but do not complain if we can't1737 */1738voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1739{1740if((istate->cache_changed ||has_racy_timestamp(istate)) &&1741!write_index(istate, lockfile->fd))1742commit_locked_index(lockfile);1743else1744rollback_lock_file(lockfile);1745}17461747intwrite_index(struct index_state *istate,int newfd)1748{1749 git_SHA_CTX c;1750struct cache_header hdr;1751int i, err, removed, extended, hdr_version;1752struct cache_entry **cache = istate->cache;1753int entries = istate->cache_nr;1754struct stat st;1755struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;17561757for(i = removed = extended =0; i < entries; i++) {1758if(cache[i]->ce_flags & CE_REMOVE)1759 removed++;17601761/* reduce extended entries if possible */1762 cache[i]->ce_flags &= ~CE_EXTENDED;1763if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1764 extended++;1765 cache[i]->ce_flags |= CE_EXTENDED;1766}1767}17681769if(!istate->version)1770 istate->version = INDEX_FORMAT_DEFAULT;17711772/* demote version 3 to version 2 when the latter suffices */1773if(istate->version ==3|| istate->version ==2)1774 istate->version = extended ?3:2;17751776 hdr_version = istate->version;17771778 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1779 hdr.hdr_version =htonl(hdr_version);1780 hdr.hdr_entries =htonl(entries - removed);17811782git_SHA1_Init(&c);1783if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1784return-1;17851786 previous_name = (hdr_version ==4) ? &previous_name_buf : NULL;1787for(i =0; i < entries; i++) {1788struct cache_entry *ce = cache[i];1789if(ce->ce_flags & CE_REMOVE)1790continue;1791if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1792ce_smudge_racily_clean_entry(ce);1793if(ce_write_entry(&c, newfd, ce, previous_name) <0)1794return-1;1795}1796strbuf_release(&previous_name_buf);17971798/* Write extension data here */1799if(istate->cache_tree) {1800struct strbuf sb = STRBUF_INIT;18011802cache_tree_write(&sb, istate->cache_tree);1803 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01804||ce_write(&c, newfd, sb.buf, sb.len) <0;1805strbuf_release(&sb);1806if(err)1807return-1;1808}1809if(istate->resolve_undo) {1810struct strbuf sb = STRBUF_INIT;18111812resolve_undo_write(&sb, istate->resolve_undo);1813 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,1814 sb.len) <01815||ce_write(&c, newfd, sb.buf, sb.len) <0;1816strbuf_release(&sb);1817if(err)1818return-1;1819}18201821if(ce_flush(&c, newfd) ||fstat(newfd, &st))1822return-1;1823 istate->timestamp.sec = (unsigned int)st.st_mtime;1824 istate->timestamp.nsec =ST_MTIME_NSEC(st);1825return0;1826}18271828/*1829 * Read the index file that is potentially unmerged into given1830 * index_state, dropping any unmerged entries. Returns true if1831 * the index is unmerged. Callers who want to refuse to work1832 * from an unmerged state can call this and check its return value,1833 * instead of calling read_cache().1834 */1835intread_index_unmerged(struct index_state *istate)1836{1837int i;1838int unmerged =0;18391840read_index(istate);1841for(i =0; i < istate->cache_nr; i++) {1842struct cache_entry *ce = istate->cache[i];1843struct cache_entry *new_ce;1844int size, len;18451846if(!ce_stage(ce))1847continue;1848 unmerged =1;1849 len =strlen(ce->name);1850 size =cache_entry_size(len);1851 new_ce =xcalloc(1, size);1852memcpy(new_ce->name, ce->name, len);1853 new_ce->ce_flags =create_ce_flags(len,0) | CE_CONFLICTED;1854 new_ce->ce_mode = ce->ce_mode;1855if(add_index_entry(istate, new_ce,0))1856returnerror("%s: cannot drop to stage #0",1857 ce->name);1858 i =index_name_pos(istate, new_ce->name, len);1859}1860return unmerged;1861}18621863/*1864 * Returns 1 if the path is an "other" path with respect to1865 * the index; that is, the path is not mentioned in the index at all,1866 * either as a file, a directory with some files in the index,1867 * or as an unmerged entry.1868 *1869 * We helpfully remove a trailing "/" from directories so that1870 * the output of read_directory can be used as-is.1871 */1872intindex_name_is_other(const struct index_state *istate,const char*name,1873int namelen)1874{1875int pos;1876if(namelen && name[namelen -1] =='/')1877 namelen--;1878 pos =index_name_pos(istate, name, namelen);1879if(0<= pos)1880return0;/* exact match */1881 pos = -pos -1;1882if(pos < istate->cache_nr) {1883struct cache_entry *ce = istate->cache[pos];1884if(ce_namelen(ce) == namelen &&1885!memcmp(ce->name, name, namelen))1886return0;/* Yup, this one exists unmerged */1887}1888return1;1889}