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