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 12/* Index extensions. 13 * 14 * The first letter should be 'A'..'Z' for extensions that are not 15 * necessary for a correct operation (i.e. optimization data). 16 * When new extensions are added that _needs_ to be understood in 17 * order to correctly interpret the index file, pick character that 18 * is outside the range, to cause the reader to abort. 19 */ 20 21#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 22#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 23 24struct index_state the_index; 25 26static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 27{ 28 istate->cache[nr] = ce; 29add_name_hash(istate, ce); 30} 31 32static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 33{ 34struct cache_entry *old = istate->cache[nr]; 35 36remove_name_hash(old); 37set_index_entry(istate, nr, ce); 38 istate->cache_changed =1; 39} 40 41voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 42{ 43struct cache_entry *old = istate->cache[nr], *new; 44int namelen =strlen(new_name); 45 46new=xmalloc(cache_entry_size(namelen)); 47copy_cache_entry(new, old); 48new->ce_flags &= ~(CE_STATE_MASK | CE_NAMEMASK); 49new->ce_flags |= (namelen >= CE_NAMEMASK ? CE_NAMEMASK : namelen); 50memcpy(new->name, new_name, namelen +1); 51 52cache_tree_invalidate_path(istate->cache_tree, old->name); 53remove_index_entry_at(istate, nr); 54add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 55} 56 57/* 58 * This only updates the "non-critical" parts of the directory 59 * cache, ie the parts that aren't tracked by GIT, and only used 60 * to validate the cache. 61 */ 62voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 63{ 64 ce->ce_ctime = st->st_ctime; 65 ce->ce_mtime = st->st_mtime; 66 ce->ce_dev = st->st_dev; 67 ce->ce_ino = st->st_ino; 68 ce->ce_uid = st->st_uid; 69 ce->ce_gid = st->st_gid; 70 ce->ce_size = st->st_size; 71 72if(assume_unchanged) 73 ce->ce_flags |= CE_VALID; 74 75if(S_ISREG(st->st_mode)) 76ce_mark_uptodate(ce); 77} 78 79static intce_compare_data(struct cache_entry *ce,struct stat *st) 80{ 81int match = -1; 82int fd =open(ce->name, O_RDONLY); 83 84if(fd >=0) { 85unsigned char sha1[20]; 86if(!index_fd(sha1, fd, st,0, OBJ_BLOB, ce->name)) 87 match =hashcmp(sha1, ce->sha1); 88/* index_fd() closed the file descriptor already */ 89} 90return match; 91} 92 93static intce_compare_link(struct cache_entry *ce,size_t expected_size) 94{ 95int match = -1; 96char*target; 97void*buffer; 98unsigned long size; 99enum object_type type; 100int len; 101 102 target =xmalloc(expected_size); 103 len =readlink(ce->name, target, expected_size); 104if(len != expected_size) { 105free(target); 106return-1; 107} 108 buffer =read_sha1_file(ce->sha1, &type, &size); 109if(!buffer) { 110free(target); 111return-1; 112} 113if(size == expected_size) 114 match =memcmp(buffer, target, size); 115free(buffer); 116free(target); 117return match; 118} 119 120static intce_compare_gitlink(struct cache_entry *ce) 121{ 122unsigned char sha1[20]; 123 124/* 125 * We don't actually require that the .git directory 126 * under GITLINK directory be a valid git directory. It 127 * might even be missing (in case nobody populated that 128 * sub-project). 129 * 130 * If so, we consider it always to match. 131 */ 132if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 133return0; 134returnhashcmp(sha1, ce->sha1); 135} 136 137static intce_modified_check_fs(struct cache_entry *ce,struct stat *st) 138{ 139switch(st->st_mode & S_IFMT) { 140case S_IFREG: 141if(ce_compare_data(ce, st)) 142return DATA_CHANGED; 143break; 144case S_IFLNK: 145if(ce_compare_link(ce,xsize_t(st->st_size))) 146return DATA_CHANGED; 147break; 148case S_IFDIR: 149if(S_ISGITLINK(ce->ce_mode)) 150return0; 151default: 152return TYPE_CHANGED; 153} 154return0; 155} 156 157static intis_empty_blob_sha1(const unsigned char*sha1) 158{ 159static const unsigned char empty_blob_sha1[20] = { 1600xe6,0x9d,0xe2,0x9b,0xb2,0xd1,0xd6,0x43,0x4b,0x8b, 1610x29,0xae,0x77,0x5a,0xd8,0xc2,0xe4,0x8c,0x53,0x91 162}; 163 164return!hashcmp(sha1, empty_blob_sha1); 165} 166 167static intce_match_stat_basic(struct cache_entry *ce,struct stat *st) 168{ 169unsigned int changed =0; 170 171if(ce->ce_flags & CE_REMOVE) 172return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 173 174switch(ce->ce_mode & S_IFMT) { 175case S_IFREG: 176 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 177/* We consider only the owner x bit to be relevant for 178 * "mode changes" 179 */ 180if(trust_executable_bit && 181(0100& (ce->ce_mode ^ st->st_mode))) 182 changed |= MODE_CHANGED; 183break; 184case S_IFLNK: 185if(!S_ISLNK(st->st_mode) && 186(has_symlinks || !S_ISREG(st->st_mode))) 187 changed |= TYPE_CHANGED; 188break; 189case S_IFGITLINK: 190if(!S_ISDIR(st->st_mode)) 191 changed |= TYPE_CHANGED; 192else if(ce_compare_gitlink(ce)) 193 changed |= DATA_CHANGED; 194return changed; 195default: 196die("internal error: ce_mode is%o", ce->ce_mode); 197} 198if(ce->ce_mtime != (unsigned int) st->st_mtime) 199 changed |= MTIME_CHANGED; 200if(trust_ctime && ce->ce_ctime != (unsigned int) st->st_ctime) 201 changed |= CTIME_CHANGED; 202 203if(ce->ce_uid != (unsigned int) st->st_uid || 204 ce->ce_gid != (unsigned int) st->st_gid) 205 changed |= OWNER_CHANGED; 206if(ce->ce_ino != (unsigned int) st->st_ino) 207 changed |= INODE_CHANGED; 208 209#ifdef USE_STDEV 210/* 211 * st_dev breaks on network filesystems where different 212 * clients will have different views of what "device" 213 * the filesystem is on 214 */ 215if(ce->ce_dev != (unsigned int) st->st_dev) 216 changed |= INODE_CHANGED; 217#endif 218 219if(ce->ce_size != (unsigned int) st->st_size) 220 changed |= DATA_CHANGED; 221 222/* Racily smudged entry? */ 223if(!ce->ce_size) { 224if(!is_empty_blob_sha1(ce->sha1)) 225 changed |= DATA_CHANGED; 226} 227 228return changed; 229} 230 231static intis_racy_timestamp(const struct index_state *istate,struct cache_entry *ce) 232{ 233return(!S_ISGITLINK(ce->ce_mode) && 234 istate->timestamp && 235((unsigned int)istate->timestamp) <= ce->ce_mtime); 236} 237 238intie_match_stat(const struct index_state *istate, 239struct cache_entry *ce,struct stat *st, 240unsigned int options) 241{ 242unsigned int changed; 243int ignore_valid = options & CE_MATCH_IGNORE_VALID; 244int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 245 246/* 247 * If it's marked as always valid in the index, it's 248 * valid whatever the checked-out copy says. 249 */ 250if(!ignore_valid && (ce->ce_flags & CE_VALID)) 251return0; 252 253 changed =ce_match_stat_basic(ce, st); 254 255/* 256 * Within 1 second of this sequence: 257 * echo xyzzy >file && git-update-index --add file 258 * running this command: 259 * echo frotz >file 260 * would give a falsely clean cache entry. The mtime and 261 * length match the cache, and other stat fields do not change. 262 * 263 * We could detect this at update-index time (the cache entry 264 * being registered/updated records the same time as "now") 265 * and delay the return from git-update-index, but that would 266 * effectively mean we can make at most one commit per second, 267 * which is not acceptable. Instead, we check cache entries 268 * whose mtime are the same as the index file timestamp more 269 * carefully than others. 270 */ 271if(!changed &&is_racy_timestamp(istate, ce)) { 272if(assume_racy_is_modified) 273 changed |= DATA_CHANGED; 274else 275 changed |=ce_modified_check_fs(ce, st); 276} 277 278return changed; 279} 280 281intie_modified(const struct index_state *istate, 282struct cache_entry *ce,struct stat *st,unsigned int options) 283{ 284int changed, changed_fs; 285 286 changed =ie_match_stat(istate, ce, st, options); 287if(!changed) 288return0; 289/* 290 * If the mode or type has changed, there's no point in trying 291 * to refresh the entry - it's not going to match 292 */ 293if(changed & (MODE_CHANGED | TYPE_CHANGED)) 294return changed; 295 296/* Immediately after read-tree or update-index --cacheinfo, 297 * the length field is zero. For other cases the ce_size 298 * should match the SHA1 recorded in the index entry. 299 */ 300if((changed & DATA_CHANGED) && ce->ce_size !=0) 301return changed; 302 303 changed_fs =ce_modified_check_fs(ce, st); 304if(changed_fs) 305return changed | changed_fs; 306return0; 307} 308 309intbase_name_compare(const char*name1,int len1,int mode1, 310const char*name2,int len2,int mode2) 311{ 312unsigned char c1, c2; 313int len = len1 < len2 ? len1 : len2; 314int cmp; 315 316 cmp =memcmp(name1, name2, len); 317if(cmp) 318return cmp; 319 c1 = name1[len]; 320 c2 = name2[len]; 321if(!c1 &&S_ISDIR(mode1)) 322 c1 ='/'; 323if(!c2 &&S_ISDIR(mode2)) 324 c2 ='/'; 325return(c1 < c2) ? -1: (c1 > c2) ?1:0; 326} 327 328/* 329 * df_name_compare() is identical to base_name_compare(), except it 330 * compares conflicting directory/file entries as equal. Note that 331 * while a directory name compares as equal to a regular file, they 332 * then individually compare _differently_ to a filename that has 333 * a dot after the basename (because '\0' < '.' < '/'). 334 * 335 * This is used by routines that want to traverse the git namespace 336 * but then handle conflicting entries together when possible. 337 */ 338intdf_name_compare(const char*name1,int len1,int mode1, 339const char*name2,int len2,int mode2) 340{ 341int len = len1 < len2 ? len1 : len2, cmp; 342unsigned char c1, c2; 343 344 cmp =memcmp(name1, name2, len); 345if(cmp) 346return cmp; 347/* Directories and files compare equal (same length, same name) */ 348if(len1 == len2) 349return0; 350 c1 = name1[len]; 351if(!c1 &&S_ISDIR(mode1)) 352 c1 ='/'; 353 c2 = name2[len]; 354if(!c2 &&S_ISDIR(mode2)) 355 c2 ='/'; 356if(c1 =='/'&& !c2) 357return0; 358if(c2 =='/'&& !c1) 359return0; 360return c1 - c2; 361} 362 363intcache_name_compare(const char*name1,int flags1,const char*name2,int flags2) 364{ 365int len1 = flags1 & CE_NAMEMASK; 366int len2 = flags2 & CE_NAMEMASK; 367int len = len1 < len2 ? len1 : len2; 368int cmp; 369 370 cmp =memcmp(name1, name2, len); 371if(cmp) 372return cmp; 373if(len1 < len2) 374return-1; 375if(len1 > len2) 376return1; 377 378/* Compare stages */ 379 flags1 &= CE_STAGEMASK; 380 flags2 &= CE_STAGEMASK; 381 382if(flags1 < flags2) 383return-1; 384if(flags1 > flags2) 385return1; 386return0; 387} 388 389intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 390{ 391int first, last; 392 393 first =0; 394 last = istate->cache_nr; 395while(last > first) { 396int next = (last + first) >>1; 397struct cache_entry *ce = istate->cache[next]; 398int cmp =cache_name_compare(name, namelen, ce->name, ce->ce_flags); 399if(!cmp) 400return next; 401if(cmp <0) { 402 last = next; 403continue; 404} 405 first = next+1; 406} 407return-first-1; 408} 409 410/* Remove entry, return true if there are more entries to go.. */ 411intremove_index_entry_at(struct index_state *istate,int pos) 412{ 413struct cache_entry *ce = istate->cache[pos]; 414 415remove_name_hash(ce); 416 istate->cache_changed =1; 417 istate->cache_nr--; 418if(pos >= istate->cache_nr) 419return0; 420memmove(istate->cache + pos, 421 istate->cache + pos +1, 422(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 423return1; 424} 425 426intremove_file_from_index(struct index_state *istate,const char*path) 427{ 428int pos =index_name_pos(istate, path,strlen(path)); 429if(pos <0) 430 pos = -pos-1; 431cache_tree_invalidate_path(istate->cache_tree, path); 432while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 433remove_index_entry_at(istate, pos); 434return0; 435} 436 437static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 438{ 439return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 440} 441 442static intindex_name_pos_also_unmerged(struct index_state *istate, 443const char*path,int namelen) 444{ 445int pos =index_name_pos(istate, path, namelen); 446struct cache_entry *ce; 447 448if(pos >=0) 449return pos; 450 451/* maybe unmerged? */ 452 pos = -1- pos; 453if(pos >= istate->cache_nr || 454compare_name((ce = istate->cache[pos]), path, namelen)) 455return-1; 456 457/* order of preference: stage 2, 1, 3 */ 458if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 459ce_stage((ce = istate->cache[pos +1])) ==2&& 460!compare_name(ce, path, namelen)) 461 pos++; 462return pos; 463} 464 465static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 466{ 467int len =ce_namelen(ce); 468returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 469} 470 471/* 472 * If we add a filename that aliases in the cache, we will use the 473 * name that we already have - but we don't want to update the same 474 * alias twice, because that implies that there were actually two 475 * different files with aliasing names! 476 * 477 * So we use the CE_ADDED flag to verify that the alias was an old 478 * one before we accept it as 479 */ 480static struct cache_entry *create_alias_ce(struct cache_entry *ce,struct cache_entry *alias) 481{ 482int len; 483struct cache_entry *new; 484 485if(alias->ce_flags & CE_ADDED) 486die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 487 488/* Ok, create the new entry using the name of the existing alias */ 489 len =ce_namelen(alias); 490new=xcalloc(1,cache_entry_size(len)); 491memcpy(new->name, alias->name, len); 492copy_cache_entry(new, ce); 493free(ce); 494return new; 495} 496 497intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 498{ 499int size, namelen, was_same; 500 mode_t st_mode = st->st_mode; 501struct cache_entry *ce, *alias; 502unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY; 503int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 504int pretend = flags & ADD_CACHE_PRETEND; 505 506if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 507returnerror("%s: can only add regular files, symbolic links or git-directories", path); 508 509 namelen =strlen(path); 510if(S_ISDIR(st_mode)) { 511while(namelen && path[namelen-1] =='/') 512 namelen--; 513} 514 size =cache_entry_size(namelen); 515 ce =xcalloc(1, size); 516memcpy(ce->name, path, namelen); 517 ce->ce_flags = namelen; 518fill_stat_cache_info(ce, st); 519 520if(trust_executable_bit && has_symlinks) 521 ce->ce_mode =create_ce_mode(st_mode); 522else{ 523/* If there is an existing entry, pick the mode bits and type 524 * from it, otherwise assume unexecutable regular file. 525 */ 526struct cache_entry *ent; 527int pos =index_name_pos_also_unmerged(istate, path, namelen); 528 529 ent = (0<= pos) ? istate->cache[pos] : NULL; 530 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 531} 532 533 alias =index_name_exists(istate, ce->name,ce_namelen(ce), ignore_case); 534if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 535/* Nothing changed, really */ 536free(ce); 537ce_mark_uptodate(alias); 538 alias->ce_flags |= CE_ADDED; 539return0; 540} 541if(index_path(ce->sha1, path, st,1)) 542returnerror("unable to index file%s", path); 543if(ignore_case && alias &&different_name(ce, alias)) 544 ce =create_alias_ce(ce, alias); 545 ce->ce_flags |= CE_ADDED; 546 547/* It was suspected to be racily clean, but it turns out to be Ok */ 548 was_same = (alias && 549!ce_stage(alias) && 550!hashcmp(alias->sha1, ce->sha1) && 551 ce->ce_mode == alias->ce_mode); 552 553if(pretend) 554; 555else if(add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE)) 556returnerror("unable to add%sto index",path); 557if(verbose && !was_same) 558printf("add '%s'\n", path); 559return0; 560} 561 562intadd_file_to_index(struct index_state *istate,const char*path,int flags) 563{ 564struct stat st; 565if(lstat(path, &st)) 566die("%s: unable to stat (%s)", path,strerror(errno)); 567returnadd_to_index(istate, path, &st, flags); 568} 569 570struct cache_entry *make_cache_entry(unsigned int mode, 571const unsigned char*sha1,const char*path,int stage, 572int refresh) 573{ 574int size, len; 575struct cache_entry *ce; 576 577if(!verify_path(path)) 578return NULL; 579 580 len =strlen(path); 581 size =cache_entry_size(len); 582 ce =xcalloc(1, size); 583 584hashcpy(ce->sha1, sha1); 585memcpy(ce->name, path, len); 586 ce->ce_flags =create_ce_flags(len, stage); 587 ce->ce_mode =create_ce_mode(mode); 588 589if(refresh) 590returnrefresh_cache_entry(ce,0); 591 592return ce; 593} 594 595intce_same_name(struct cache_entry *a,struct cache_entry *b) 596{ 597int len =ce_namelen(a); 598returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 599} 600 601intce_path_match(const struct cache_entry *ce,const char**pathspec) 602{ 603const char*match, *name; 604int len; 605 606if(!pathspec) 607return1; 608 609 len =ce_namelen(ce); 610 name = ce->name; 611while((match = *pathspec++) != NULL) { 612int matchlen =strlen(match); 613if(matchlen > len) 614continue; 615if(memcmp(name, match, matchlen)) 616continue; 617if(matchlen && name[matchlen-1] =='/') 618return1; 619if(name[matchlen] =='/'|| !name[matchlen]) 620return1; 621if(!matchlen) 622return1; 623} 624return0; 625} 626 627/* 628 * We fundamentally don't like some paths: we don't want 629 * dot or dot-dot anywhere, and for obvious reasons don't 630 * want to recurse into ".git" either. 631 * 632 * Also, we don't want double slashes or slashes at the 633 * end that can make pathnames ambiguous. 634 */ 635static intverify_dotfile(const char*rest) 636{ 637/* 638 * The first character was '.', but that 639 * has already been discarded, we now test 640 * the rest. 641 */ 642switch(*rest) { 643/* "." is not allowed */ 644case'\0':case'/': 645return0; 646 647/* 648 * ".git" followed by NUL or slash is bad. This 649 * shares the path end test with the ".." case. 650 */ 651case'g': 652if(rest[1] !='i') 653break; 654if(rest[2] !='t') 655break; 656 rest +=2; 657/* fallthrough */ 658case'.': 659if(rest[1] =='\0'|| rest[1] =='/') 660return0; 661} 662return1; 663} 664 665intverify_path(const char*path) 666{ 667char c; 668 669goto inside; 670for(;;) { 671if(!c) 672return1; 673if(c =='/') { 674inside: 675 c = *path++; 676switch(c) { 677default: 678continue; 679case'/':case'\0': 680break; 681case'.': 682if(verify_dotfile(path)) 683continue; 684} 685return0; 686} 687 c = *path++; 688} 689} 690 691/* 692 * Do we have another file that has the beginning components being a 693 * proper superset of the name we're trying to add? 694 */ 695static inthas_file_name(struct index_state *istate, 696const struct cache_entry *ce,int pos,int ok_to_replace) 697{ 698int retval =0; 699int len =ce_namelen(ce); 700int stage =ce_stage(ce); 701const char*name = ce->name; 702 703while(pos < istate->cache_nr) { 704struct cache_entry *p = istate->cache[pos++]; 705 706if(len >=ce_namelen(p)) 707break; 708if(memcmp(name, p->name, len)) 709break; 710if(ce_stage(p) != stage) 711continue; 712if(p->name[len] !='/') 713continue; 714if(p->ce_flags & CE_REMOVE) 715continue; 716 retval = -1; 717if(!ok_to_replace) 718break; 719remove_index_entry_at(istate, --pos); 720} 721return retval; 722} 723 724/* 725 * Do we have another file with a pathname that is a proper 726 * subset of the name we're trying to add? 727 */ 728static inthas_dir_name(struct index_state *istate, 729const struct cache_entry *ce,int pos,int ok_to_replace) 730{ 731int retval =0; 732int stage =ce_stage(ce); 733const char*name = ce->name; 734const char*slash = name +ce_namelen(ce); 735 736for(;;) { 737int len; 738 739for(;;) { 740if(*--slash =='/') 741break; 742if(slash <= ce->name) 743return retval; 744} 745 len = slash - name; 746 747 pos =index_name_pos(istate, name,create_ce_flags(len, stage)); 748if(pos >=0) { 749/* 750 * Found one, but not so fast. This could 751 * be a marker that says "I was here, but 752 * I am being removed". Such an entry is 753 * not a part of the resulting tree, and 754 * it is Ok to have a directory at the same 755 * path. 756 */ 757if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 758 retval = -1; 759if(!ok_to_replace) 760break; 761remove_index_entry_at(istate, pos); 762continue; 763} 764} 765else 766 pos = -pos-1; 767 768/* 769 * Trivial optimization: if we find an entry that 770 * already matches the sub-directory, then we know 771 * we're ok, and we can exit. 772 */ 773while(pos < istate->cache_nr) { 774struct cache_entry *p = istate->cache[pos]; 775if((ce_namelen(p) <= len) || 776(p->name[len] !='/') || 777memcmp(p->name, name, len)) 778break;/* not our subdirectory */ 779if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 780/* 781 * p is at the same stage as our entry, and 782 * is a subdirectory of what we are looking 783 * at, so we cannot have conflicts at our 784 * level or anything shorter. 785 */ 786return retval; 787 pos++; 788} 789} 790return retval; 791} 792 793/* We may be in a situation where we already have path/file and path 794 * is being added, or we already have path and path/file is being 795 * added. Either one would result in a nonsense tree that has path 796 * twice when git-write-tree tries to write it out. Prevent it. 797 * 798 * If ok-to-replace is specified, we remove the conflicting entries 799 * from the cache so the caller should recompute the insert position. 800 * When this happens, we return non-zero. 801 */ 802static intcheck_file_directory_conflict(struct index_state *istate, 803const struct cache_entry *ce, 804int pos,int ok_to_replace) 805{ 806int retval; 807 808/* 809 * When ce is an "I am going away" entry, we allow it to be added 810 */ 811if(ce->ce_flags & CE_REMOVE) 812return0; 813 814/* 815 * We check if the path is a sub-path of a subsequent pathname 816 * first, since removing those will not change the position 817 * in the array. 818 */ 819 retval =has_file_name(istate, ce, pos, ok_to_replace); 820 821/* 822 * Then check if the path might have a clashing sub-directory 823 * before it. 824 */ 825return retval +has_dir_name(istate, ce, pos, ok_to_replace); 826} 827 828static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 829{ 830int pos; 831int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 832int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 833int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 834 835cache_tree_invalidate_path(istate->cache_tree, ce->name); 836 pos =index_name_pos(istate, ce->name, ce->ce_flags); 837 838/* existing match? Just replace it. */ 839if(pos >=0) { 840replace_index_entry(istate, pos, ce); 841return0; 842} 843 pos = -pos-1; 844 845/* 846 * Inserting a merged entry ("stage 0") into the index 847 * will always replace all non-merged entries.. 848 */ 849if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 850while(ce_same_name(istate->cache[pos], ce)) { 851 ok_to_add =1; 852if(!remove_index_entry_at(istate, pos)) 853break; 854} 855} 856 857if(!ok_to_add) 858return-1; 859if(!verify_path(ce->name)) 860return-1; 861 862if(!skip_df_check && 863check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 864if(!ok_to_replace) 865returnerror("'%s' appears as both a file and as a directory", 866 ce->name); 867 pos =index_name_pos(istate, ce->name, ce->ce_flags); 868 pos = -pos-1; 869} 870return pos +1; 871} 872 873intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 874{ 875int pos; 876 877if(option & ADD_CACHE_JUST_APPEND) 878 pos = istate->cache_nr; 879else{ 880int ret; 881 ret =add_index_entry_with_check(istate, ce, option); 882if(ret <=0) 883return ret; 884 pos = ret -1; 885} 886 887/* Make sure the array is big enough .. */ 888if(istate->cache_nr == istate->cache_alloc) { 889 istate->cache_alloc =alloc_nr(istate->cache_alloc); 890 istate->cache =xrealloc(istate->cache, 891 istate->cache_alloc *sizeof(struct cache_entry *)); 892} 893 894/* Add it in.. */ 895 istate->cache_nr++; 896if(istate->cache_nr > pos +1) 897memmove(istate->cache + pos +1, 898 istate->cache + pos, 899(istate->cache_nr - pos -1) *sizeof(ce)); 900set_index_entry(istate, pos, ce); 901 istate->cache_changed =1; 902return0; 903} 904 905/* 906 * "refresh" does not calculate a new sha1 file or bring the 907 * cache up-to-date for mode/content changes. But what it 908 * _does_ do is to "re-match" the stat information of a file 909 * with the cache, so that you can refresh the cache for a 910 * file that hasn't been changed but where the stat entry is 911 * out of date. 912 * 913 * For example, you'd want to do this after doing a "git-read-tree", 914 * to link up the stat cache details with the proper files. 915 */ 916static struct cache_entry *refresh_cache_ent(struct index_state *istate, 917struct cache_entry *ce, 918unsigned int options,int*err) 919{ 920struct stat st; 921struct cache_entry *updated; 922int changed, size; 923int ignore_valid = options & CE_MATCH_IGNORE_VALID; 924 925if(ce_uptodate(ce)) 926return ce; 927 928/* 929 * CE_VALID means the user promised us that the change to 930 * the work tree does not matter and told us not to worry. 931 */ 932if(!ignore_valid && (ce->ce_flags & CE_VALID)) { 933ce_mark_uptodate(ce); 934return ce; 935} 936 937if(lstat(ce->name, &st) <0) { 938if(err) 939*err = errno; 940return NULL; 941} 942 943 changed =ie_match_stat(istate, ce, &st, options); 944if(!changed) { 945/* 946 * The path is unchanged. If we were told to ignore 947 * valid bit, then we did the actual stat check and 948 * found that the entry is unmodified. If the entry 949 * is not marked VALID, this is the place to mark it 950 * valid again, under "assume unchanged" mode. 951 */ 952if(ignore_valid && assume_unchanged && 953!(ce->ce_flags & CE_VALID)) 954;/* mark this one VALID again */ 955else{ 956/* 957 * We do not mark the index itself "modified" 958 * because CE_UPTODATE flag is in-core only; 959 * we are not going to write this change out. 960 */ 961ce_mark_uptodate(ce); 962return ce; 963} 964} 965 966if(ie_modified(istate, ce, &st, options)) { 967if(err) 968*err = EINVAL; 969return NULL; 970} 971 972 size =ce_size(ce); 973 updated =xmalloc(size); 974memcpy(updated, ce, size); 975fill_stat_cache_info(updated, &st); 976/* 977 * If ignore_valid is not set, we should leave CE_VALID bit 978 * alone. Otherwise, paths marked with --no-assume-unchanged 979 * (i.e. things to be edited) will reacquire CE_VALID bit 980 * automatically, which is not really what we want. 981 */ 982if(!ignore_valid && assume_unchanged && 983!(ce->ce_flags & CE_VALID)) 984 updated->ce_flags &= ~CE_VALID; 985 986return updated; 987} 988 989intrefresh_index(struct index_state *istate,unsigned int flags,const char**pathspec,char*seen) 990{ 991int i; 992int has_errors =0; 993int really = (flags & REFRESH_REALLY) !=0; 994int allow_unmerged = (flags & REFRESH_UNMERGED) !=0; 995int quiet = (flags & REFRESH_QUIET) !=0; 996int not_new = (flags & REFRESH_IGNORE_MISSING) !=0; 997int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0; 998unsigned int options = really ? CE_MATCH_IGNORE_VALID :0; 999const char*needs_update_message;10001001 needs_update_message = ((flags & REFRESH_SAY_CHANGED)1002?"locally modified":"needs update");1003for(i =0; i < istate->cache_nr; i++) {1004struct cache_entry *ce, *new;1005int cache_errno =0;10061007 ce = istate->cache[i];1008if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1009continue;10101011if(ce_stage(ce)) {1012while((i < istate->cache_nr) &&1013!strcmp(istate->cache[i]->name, ce->name))1014 i++;1015 i--;1016if(allow_unmerged)1017continue;1018printf("%s: needs merge\n", ce->name);1019 has_errors =1;1020continue;1021}10221023if(pathspec && !match_pathspec(pathspec, ce->name,strlen(ce->name),0, seen))1024continue;10251026new=refresh_cache_ent(istate, ce, options, &cache_errno);1027if(new== ce)1028continue;1029if(!new) {1030if(not_new && cache_errno == ENOENT)1031continue;1032if(really && cache_errno == EINVAL) {1033/* If we are doing --really-refresh that1034 * means the index is not valid anymore.1035 */1036 ce->ce_flags &= ~CE_VALID;1037 istate->cache_changed =1;1038}1039if(quiet)1040continue;1041printf("%s:%s\n", ce->name, needs_update_message);1042 has_errors =1;1043continue;1044}10451046replace_index_entry(istate, i,new);1047}1048return has_errors;1049}10501051struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really)1052{1053returnrefresh_cache_ent(&the_index, ce, really, NULL);1054}10551056static intverify_hdr(struct cache_header *hdr,unsigned long size)1057{1058 SHA_CTX c;1059unsigned char sha1[20];10601061if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1062returnerror("bad signature");1063if(hdr->hdr_version !=htonl(2))1064returnerror("bad index version");1065SHA1_Init(&c);1066SHA1_Update(&c, hdr, size -20);1067SHA1_Final(sha1, &c);1068if(hashcmp(sha1, (unsigned char*)hdr + size -20))1069returnerror("bad index file sha1 signature");1070return0;1071}10721073static intread_index_extension(struct index_state *istate,1074const char*ext,void*data,unsigned long sz)1075{1076switch(CACHE_EXT(ext)) {1077case CACHE_EXT_TREE:1078 istate->cache_tree =cache_tree_read(data, sz);1079break;1080default:1081if(*ext <'A'||'Z'< *ext)1082returnerror("index uses %.4s extension, which we do not understand",1083 ext);1084fprintf(stderr,"ignoring %.4s extension\n", ext);1085break;1086}1087return0;1088}10891090intread_index(struct index_state *istate)1091{1092returnread_index_from(istate,get_index_file());1093}10941095static voidconvert_from_disk(struct ondisk_cache_entry *ondisk,struct cache_entry *ce)1096{1097size_t len;10981099 ce->ce_ctime =ntohl(ondisk->ctime.sec);1100 ce->ce_mtime =ntohl(ondisk->mtime.sec);1101 ce->ce_dev =ntohl(ondisk->dev);1102 ce->ce_ino =ntohl(ondisk->ino);1103 ce->ce_mode =ntohl(ondisk->mode);1104 ce->ce_uid =ntohl(ondisk->uid);1105 ce->ce_gid =ntohl(ondisk->gid);1106 ce->ce_size =ntohl(ondisk->size);1107/* On-disk flags are just 16 bits */1108 ce->ce_flags =ntohs(ondisk->flags);1109hashcpy(ce->sha1, ondisk->sha1);11101111 len = ce->ce_flags & CE_NAMEMASK;1112if(len == CE_NAMEMASK)1113 len =strlen(ondisk->name);1114/*1115 * NEEDSWORK: If the original index is crafted, this copy could1116 * go unchecked.1117 */1118memcpy(ce->name, ondisk->name, len +1);1119}11201121staticinlinesize_testimate_cache_size(size_t ondisk_size,unsigned int entries)1122{1123long per_entry;11241125 per_entry =sizeof(struct cache_entry) -sizeof(struct ondisk_cache_entry);11261127/*1128 * Alignment can cause differences. This should be "alignof", but1129 * since that's a gcc'ism, just use the size of a pointer.1130 */1131 per_entry +=sizeof(void*);1132return ondisk_size + entries*per_entry;1133}11341135/* remember to discard_cache() before reading a different cache! */1136intread_index_from(struct index_state *istate,const char*path)1137{1138int fd, i;1139struct stat st;1140unsigned long src_offset, dst_offset;1141struct cache_header *hdr;1142void*mmap;1143size_t mmap_size;11441145 errno = EBUSY;1146if(istate->alloc)1147return istate->cache_nr;11481149 errno = ENOENT;1150 istate->timestamp =0;1151 fd =open(path, O_RDONLY);1152if(fd <0) {1153if(errno == ENOENT)1154return0;1155die("index file open failed (%s)",strerror(errno));1156}11571158if(fstat(fd, &st))1159die("cannot stat the open index (%s)",strerror(errno));11601161 errno = EINVAL;1162 mmap_size =xsize_t(st.st_size);1163if(mmap_size <sizeof(struct cache_header) +20)1164die("index file smaller than expected");11651166 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1167close(fd);1168if(mmap == MAP_FAILED)1169die("unable to map index file");11701171 hdr = mmap;1172if(verify_hdr(hdr, mmap_size) <0)1173goto unmap;11741175 istate->cache_nr =ntohl(hdr->hdr_entries);1176 istate->cache_alloc =alloc_nr(istate->cache_nr);1177 istate->cache =xcalloc(istate->cache_alloc,sizeof(struct cache_entry *));11781179/*1180 * The disk format is actually larger than the in-memory format,1181 * due to space for nsec etc, so even though the in-memory one1182 * has room for a few more flags, we can allocate using the same1183 * index size1184 */1185 istate->alloc =xmalloc(estimate_cache_size(mmap_size, istate->cache_nr));11861187 src_offset =sizeof(*hdr);1188 dst_offset =0;1189for(i =0; i < istate->cache_nr; i++) {1190struct ondisk_cache_entry *disk_ce;1191struct cache_entry *ce;11921193 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1194 ce = (struct cache_entry *)((char*)istate->alloc + dst_offset);1195convert_from_disk(disk_ce, ce);1196set_index_entry(istate, i, ce);11971198 src_offset +=ondisk_ce_size(ce);1199 dst_offset +=ce_size(ce);1200}1201 istate->timestamp = st.st_mtime;1202while(src_offset <= mmap_size -20-8) {1203/* After an array of active_nr index entries,1204 * there can be arbitrary number of extended1205 * sections, each of which is prefixed with1206 * extension name (4-byte) and section length1207 * in 4-byte network byte order.1208 */1209unsigned long extsize;1210memcpy(&extsize, (char*)mmap + src_offset +4,4);1211 extsize =ntohl(extsize);1212if(read_index_extension(istate,1213(const char*) mmap + src_offset,1214(char*) mmap + src_offset +8,1215 extsize) <0)1216goto unmap;1217 src_offset +=8;1218 src_offset += extsize;1219}1220munmap(mmap, mmap_size);1221return istate->cache_nr;12221223unmap:1224munmap(mmap, mmap_size);1225 errno = EINVAL;1226die("index file corrupt");1227}12281229intdiscard_index(struct index_state *istate)1230{1231 istate->cache_nr =0;1232 istate->cache_changed =0;1233 istate->timestamp =0;1234free_hash(&istate->name_hash);1235cache_tree_free(&(istate->cache_tree));1236free(istate->alloc);1237 istate->alloc = NULL;12381239/* no need to throw away allocated active_cache */1240return0;1241}12421243intunmerged_index(const struct index_state *istate)1244{1245int i;1246for(i =0; i < istate->cache_nr; i++) {1247if(ce_stage(istate->cache[i]))1248return1;1249}1250return0;1251}12521253#define WRITE_BUFFER_SIZE 81921254static unsigned char write_buffer[WRITE_BUFFER_SIZE];1255static unsigned long write_buffer_len;12561257static intce_write_flush(SHA_CTX *context,int fd)1258{1259unsigned int buffered = write_buffer_len;1260if(buffered) {1261SHA1_Update(context, write_buffer, buffered);1262if(write_in_full(fd, write_buffer, buffered) != buffered)1263return-1;1264 write_buffer_len =0;1265}1266return0;1267}12681269static intce_write(SHA_CTX *context,int fd,void*data,unsigned int len)1270{1271while(len) {1272unsigned int buffered = write_buffer_len;1273unsigned int partial = WRITE_BUFFER_SIZE - buffered;1274if(partial > len)1275 partial = len;1276memcpy(write_buffer + buffered, data, partial);1277 buffered += partial;1278if(buffered == WRITE_BUFFER_SIZE) {1279 write_buffer_len = buffered;1280if(ce_write_flush(context, fd))1281return-1;1282 buffered =0;1283}1284 write_buffer_len = buffered;1285 len -= partial;1286 data = (char*) data + partial;1287}1288return0;1289}12901291static intwrite_index_ext_header(SHA_CTX *context,int fd,1292unsigned int ext,unsigned int sz)1293{1294 ext =htonl(ext);1295 sz =htonl(sz);1296return((ce_write(context, fd, &ext,4) <0) ||1297(ce_write(context, fd, &sz,4) <0)) ? -1:0;1298}12991300static intce_flush(SHA_CTX *context,int fd)1301{1302unsigned int left = write_buffer_len;13031304if(left) {1305 write_buffer_len =0;1306SHA1_Update(context, write_buffer, left);1307}13081309/* Flush first if not enough space for SHA1 signature */1310if(left +20> WRITE_BUFFER_SIZE) {1311if(write_in_full(fd, write_buffer, left) != left)1312return-1;1313 left =0;1314}13151316/* Append the SHA1 signature at the end */1317SHA1_Final(write_buffer + left, context);1318 left +=20;1319return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1320}13211322static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1323{1324/*1325 * The only thing we care about in this function is to smudge the1326 * falsely clean entry due to touch-update-touch race, so we leave1327 * everything else as they are. We are called for entries whose1328 * ce_mtime match the index file mtime.1329 */1330struct stat st;13311332if(lstat(ce->name, &st) <0)1333return;1334if(ce_match_stat_basic(ce, &st))1335return;1336if(ce_modified_check_fs(ce, &st)) {1337/* This is "racily clean"; smudge it. Note that this1338 * is a tricky code. At first glance, it may appear1339 * that it can break with this sequence:1340 *1341 * $ echo xyzzy >frotz1342 * $ git-update-index --add frotz1343 * $ : >frotz1344 * $ sleep 31345 * $ echo filfre >nitfol1346 * $ git-update-index --add nitfol1347 *1348 * but it does not. When the second update-index runs,1349 * it notices that the entry "frotz" has the same timestamp1350 * as index, and if we were to smudge it by resetting its1351 * size to zero here, then the object name recorded1352 * in index is the 6-byte file but the cached stat information1353 * becomes zero --- which would then match what we would1354 * obtain from the filesystem next time we stat("frotz").1355 *1356 * However, the second update-index, before calling1357 * this function, notices that the cached size is 61358 * bytes and what is on the filesystem is an empty1359 * file, and never calls us, so the cached size information1360 * for "frotz" stays 6 which does not match the filesystem.1361 */1362 ce->ce_size =0;1363}1364}13651366static intce_write_entry(SHA_CTX *c,int fd,struct cache_entry *ce)1367{1368int size =ondisk_ce_size(ce);1369struct ondisk_cache_entry *ondisk =xcalloc(1, size);13701371 ondisk->ctime.sec =htonl(ce->ce_ctime);1372 ondisk->ctime.nsec =0;1373 ondisk->mtime.sec =htonl(ce->ce_mtime);1374 ondisk->mtime.nsec =0;1375 ondisk->dev =htonl(ce->ce_dev);1376 ondisk->ino =htonl(ce->ce_ino);1377 ondisk->mode =htonl(ce->ce_mode);1378 ondisk->uid =htonl(ce->ce_uid);1379 ondisk->gid =htonl(ce->ce_gid);1380 ondisk->size =htonl(ce->ce_size);1381hashcpy(ondisk->sha1, ce->sha1);1382 ondisk->flags =htons(ce->ce_flags);1383memcpy(ondisk->name, ce->name,ce_namelen(ce));13841385returnce_write(c, fd, ondisk, size);1386}13871388intwrite_index(const struct index_state *istate,int newfd)1389{1390 SHA_CTX c;1391struct cache_header hdr;1392int i, err, removed;1393struct cache_entry **cache = istate->cache;1394int entries = istate->cache_nr;13951396for(i = removed =0; i < entries; i++)1397if(cache[i]->ce_flags & CE_REMOVE)1398 removed++;13991400 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1401 hdr.hdr_version =htonl(2);1402 hdr.hdr_entries =htonl(entries - removed);14031404SHA1_Init(&c);1405if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1406return-1;14071408for(i =0; i < entries; i++) {1409struct cache_entry *ce = cache[i];1410if(ce->ce_flags & CE_REMOVE)1411continue;1412if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1413ce_smudge_racily_clean_entry(ce);1414if(ce_write_entry(&c, newfd, ce) <0)1415return-1;1416}14171418/* Write extension data here */1419if(istate->cache_tree) {1420struct strbuf sb;14211422strbuf_init(&sb,0);1423cache_tree_write(&sb, istate->cache_tree);1424 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01425||ce_write(&c, newfd, sb.buf, sb.len) <0;1426strbuf_release(&sb);1427if(err)1428return-1;1429}1430returnce_flush(&c, newfd);1431}14321433/*1434 * Read the index file that is potentially unmerged into given1435 * index_state, dropping any unmerged entries. Returns true is1436 * the index is unmerged. Callers who want to refuse to work1437 * from an unmerged state can call this and check its return value,1438 * instead of calling read_cache().1439 */1440intread_index_unmerged(struct index_state *istate)1441{1442int i;1443struct cache_entry **dst;1444struct cache_entry *last = NULL;14451446read_index(istate);1447 dst = istate->cache;1448for(i =0; i < istate->cache_nr; i++) {1449struct cache_entry *ce = istate->cache[i];1450if(ce_stage(ce)) {1451remove_name_hash(ce);1452if(last && !strcmp(ce->name, last->name))1453continue;1454cache_tree_invalidate_path(istate->cache_tree, ce->name);1455 last = ce;1456continue;1457}1458*dst++ = ce;1459}1460 istate->cache_nr = dst - istate->cache;1461return!!last;1462}