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)) 150returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 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: 190/* We ignore most of the st_xxx fields for gitlinks */ 191if(!S_ISDIR(st->st_mode)) 192 changed |= TYPE_CHANGED; 193else if(ce_compare_gitlink(ce)) 194 changed |= DATA_CHANGED; 195return changed; 196default: 197die("internal error: ce_mode is%o", ce->ce_mode); 198} 199if(ce->ce_mtime != (unsigned int) st->st_mtime) 200 changed |= MTIME_CHANGED; 201if(trust_ctime && ce->ce_ctime != (unsigned int) st->st_ctime) 202 changed |= CTIME_CHANGED; 203 204if(ce->ce_uid != (unsigned int) st->st_uid || 205 ce->ce_gid != (unsigned int) st->st_gid) 206 changed |= OWNER_CHANGED; 207if(ce->ce_ino != (unsigned int) st->st_ino) 208 changed |= INODE_CHANGED; 209 210#ifdef USE_STDEV 211/* 212 * st_dev breaks on network filesystems where different 213 * clients will have different views of what "device" 214 * the filesystem is on 215 */ 216if(ce->ce_dev != (unsigned int) st->st_dev) 217 changed |= INODE_CHANGED; 218#endif 219 220if(ce->ce_size != (unsigned int) st->st_size) 221 changed |= DATA_CHANGED; 222 223/* Racily smudged entry? */ 224if(!ce->ce_size) { 225if(!is_empty_blob_sha1(ce->sha1)) 226 changed |= DATA_CHANGED; 227} 228 229return changed; 230} 231 232static intis_racy_timestamp(const struct index_state *istate,struct cache_entry *ce) 233{ 234return(!S_ISGITLINK(ce->ce_mode) && 235 istate->timestamp && 236((unsigned int)istate->timestamp) <= ce->ce_mtime); 237} 238 239intie_match_stat(const struct index_state *istate, 240struct cache_entry *ce,struct stat *st, 241unsigned int options) 242{ 243unsigned int changed; 244int ignore_valid = options & CE_MATCH_IGNORE_VALID; 245int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 246 247/* 248 * If it's marked as always valid in the index, it's 249 * valid whatever the checked-out copy says. 250 */ 251if(!ignore_valid && (ce->ce_flags & CE_VALID)) 252return0; 253 254 changed =ce_match_stat_basic(ce, st); 255 256/* 257 * Within 1 second of this sequence: 258 * echo xyzzy >file && git-update-index --add file 259 * running this command: 260 * echo frotz >file 261 * would give a falsely clean cache entry. The mtime and 262 * length match the cache, and other stat fields do not change. 263 * 264 * We could detect this at update-index time (the cache entry 265 * being registered/updated records the same time as "now") 266 * and delay the return from git-update-index, but that would 267 * effectively mean we can make at most one commit per second, 268 * which is not acceptable. Instead, we check cache entries 269 * whose mtime are the same as the index file timestamp more 270 * carefully than others. 271 */ 272if(!changed &&is_racy_timestamp(istate, ce)) { 273if(assume_racy_is_modified) 274 changed |= DATA_CHANGED; 275else 276 changed |=ce_modified_check_fs(ce, st); 277} 278 279return changed; 280} 281 282intie_modified(const struct index_state *istate, 283struct cache_entry *ce,struct stat *st,unsigned int options) 284{ 285int changed, changed_fs; 286 287 changed =ie_match_stat(istate, ce, st, options); 288if(!changed) 289return0; 290/* 291 * If the mode or type has changed, there's no point in trying 292 * to refresh the entry - it's not going to match 293 */ 294if(changed & (MODE_CHANGED | TYPE_CHANGED)) 295return changed; 296 297/* 298 * Immediately after read-tree or update-index --cacheinfo, 299 * the length field is zero, as we have never even read the 300 * lstat(2) information once, and we cannot trust DATA_CHANGED 301 * returned by ie_match_stat() which in turn was returned by 302 * ce_match_stat_basic() to signal that the filesize of the 303 * blob changed. We have to actually go to the filesystem to 304 * see if the contents match, and if so, should answer "unchanged". 305 * 306 * The logic does not apply to gitlinks, as ce_match_stat_basic() 307 * already has checked the actual HEAD from the filesystem in the 308 * subproject. If ie_match_stat() already said it is different, 309 * then we know it is. 310 */ 311if((changed & DATA_CHANGED) && 312(S_ISGITLINK(ce->ce_mode) || ce->ce_size !=0)) 313return changed; 314 315 changed_fs =ce_modified_check_fs(ce, st); 316if(changed_fs) 317return changed | changed_fs; 318return0; 319} 320 321intbase_name_compare(const char*name1,int len1,int mode1, 322const char*name2,int len2,int mode2) 323{ 324unsigned char c1, c2; 325int len = len1 < len2 ? len1 : len2; 326int cmp; 327 328 cmp =memcmp(name1, name2, len); 329if(cmp) 330return cmp; 331 c1 = name1[len]; 332 c2 = name2[len]; 333if(!c1 &&S_ISDIR(mode1)) 334 c1 ='/'; 335if(!c2 &&S_ISDIR(mode2)) 336 c2 ='/'; 337return(c1 < c2) ? -1: (c1 > c2) ?1:0; 338} 339 340/* 341 * df_name_compare() is identical to base_name_compare(), except it 342 * compares conflicting directory/file entries as equal. Note that 343 * while a directory name compares as equal to a regular file, they 344 * then individually compare _differently_ to a filename that has 345 * a dot after the basename (because '\0' < '.' < '/'). 346 * 347 * This is used by routines that want to traverse the git namespace 348 * but then handle conflicting entries together when possible. 349 */ 350intdf_name_compare(const char*name1,int len1,int mode1, 351const char*name2,int len2,int mode2) 352{ 353int len = len1 < len2 ? len1 : len2, cmp; 354unsigned char c1, c2; 355 356 cmp =memcmp(name1, name2, len); 357if(cmp) 358return cmp; 359/* Directories and files compare equal (same length, same name) */ 360if(len1 == len2) 361return0; 362 c1 = name1[len]; 363if(!c1 &&S_ISDIR(mode1)) 364 c1 ='/'; 365 c2 = name2[len]; 366if(!c2 &&S_ISDIR(mode2)) 367 c2 ='/'; 368if(c1 =='/'&& !c2) 369return0; 370if(c2 =='/'&& !c1) 371return0; 372return c1 - c2; 373} 374 375intcache_name_compare(const char*name1,int flags1,const char*name2,int flags2) 376{ 377int len1 = flags1 & CE_NAMEMASK; 378int len2 = flags2 & CE_NAMEMASK; 379int len = len1 < len2 ? len1 : len2; 380int cmp; 381 382 cmp =memcmp(name1, name2, len); 383if(cmp) 384return cmp; 385if(len1 < len2) 386return-1; 387if(len1 > len2) 388return1; 389 390/* Compare stages */ 391 flags1 &= CE_STAGEMASK; 392 flags2 &= CE_STAGEMASK; 393 394if(flags1 < flags2) 395return-1; 396if(flags1 > flags2) 397return1; 398return0; 399} 400 401intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 402{ 403int first, last; 404 405 first =0; 406 last = istate->cache_nr; 407while(last > first) { 408int next = (last + first) >>1; 409struct cache_entry *ce = istate->cache[next]; 410int cmp =cache_name_compare(name, namelen, ce->name, ce->ce_flags); 411if(!cmp) 412return next; 413if(cmp <0) { 414 last = next; 415continue; 416} 417 first = next+1; 418} 419return-first-1; 420} 421 422/* Remove entry, return true if there are more entries to go.. */ 423intremove_index_entry_at(struct index_state *istate,int pos) 424{ 425struct cache_entry *ce = istate->cache[pos]; 426 427remove_name_hash(ce); 428 istate->cache_changed =1; 429 istate->cache_nr--; 430if(pos >= istate->cache_nr) 431return0; 432memmove(istate->cache + pos, 433 istate->cache + pos +1, 434(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 435return1; 436} 437 438intremove_file_from_index(struct index_state *istate,const char*path) 439{ 440int pos =index_name_pos(istate, path,strlen(path)); 441if(pos <0) 442 pos = -pos-1; 443cache_tree_invalidate_path(istate->cache_tree, path); 444while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 445remove_index_entry_at(istate, pos); 446return0; 447} 448 449static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 450{ 451return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 452} 453 454static intindex_name_pos_also_unmerged(struct index_state *istate, 455const char*path,int namelen) 456{ 457int pos =index_name_pos(istate, path, namelen); 458struct cache_entry *ce; 459 460if(pos >=0) 461return pos; 462 463/* maybe unmerged? */ 464 pos = -1- pos; 465if(pos >= istate->cache_nr || 466compare_name((ce = istate->cache[pos]), path, namelen)) 467return-1; 468 469/* order of preference: stage 2, 1, 3 */ 470if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 471ce_stage((ce = istate->cache[pos +1])) ==2&& 472!compare_name(ce, path, namelen)) 473 pos++; 474return pos; 475} 476 477static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 478{ 479int len =ce_namelen(ce); 480returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 481} 482 483/* 484 * If we add a filename that aliases in the cache, we will use the 485 * name that we already have - but we don't want to update the same 486 * alias twice, because that implies that there were actually two 487 * different files with aliasing names! 488 * 489 * So we use the CE_ADDED flag to verify that the alias was an old 490 * one before we accept it as 491 */ 492static struct cache_entry *create_alias_ce(struct cache_entry *ce,struct cache_entry *alias) 493{ 494int len; 495struct cache_entry *new; 496 497if(alias->ce_flags & CE_ADDED) 498die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 499 500/* Ok, create the new entry using the name of the existing alias */ 501 len =ce_namelen(alias); 502new=xcalloc(1,cache_entry_size(len)); 503memcpy(new->name, alias->name, len); 504copy_cache_entry(new, ce); 505free(ce); 506return new; 507} 508 509intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 510{ 511int size, namelen, was_same; 512 mode_t st_mode = st->st_mode; 513struct cache_entry *ce, *alias; 514unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY; 515int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 516int pretend = flags & ADD_CACHE_PRETEND; 517 518if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 519returnerror("%s: can only add regular files, symbolic links or git-directories", path); 520 521 namelen =strlen(path); 522if(S_ISDIR(st_mode)) { 523while(namelen && path[namelen-1] =='/') 524 namelen--; 525} 526 size =cache_entry_size(namelen); 527 ce =xcalloc(1, size); 528memcpy(ce->name, path, namelen); 529 ce->ce_flags = namelen; 530fill_stat_cache_info(ce, st); 531 532if(trust_executable_bit && has_symlinks) 533 ce->ce_mode =create_ce_mode(st_mode); 534else{ 535/* If there is an existing entry, pick the mode bits and type 536 * from it, otherwise assume unexecutable regular file. 537 */ 538struct cache_entry *ent; 539int pos =index_name_pos_also_unmerged(istate, path, namelen); 540 541 ent = (0<= pos) ? istate->cache[pos] : NULL; 542 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 543} 544 545 alias =index_name_exists(istate, ce->name,ce_namelen(ce), ignore_case); 546if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 547/* Nothing changed, really */ 548free(ce); 549ce_mark_uptodate(alias); 550 alias->ce_flags |= CE_ADDED; 551return0; 552} 553if(index_path(ce->sha1, path, st,1)) 554returnerror("unable to index file%s", path); 555if(ignore_case && alias &&different_name(ce, alias)) 556 ce =create_alias_ce(ce, alias); 557 ce->ce_flags |= CE_ADDED; 558 559/* It was suspected to be racily clean, but it turns out to be Ok */ 560 was_same = (alias && 561!ce_stage(alias) && 562!hashcmp(alias->sha1, ce->sha1) && 563 ce->ce_mode == alias->ce_mode); 564 565if(pretend) 566; 567else if(add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE)) 568returnerror("unable to add%sto index",path); 569if(verbose && !was_same) 570printf("add '%s'\n", path); 571return0; 572} 573 574intadd_file_to_index(struct index_state *istate,const char*path,int flags) 575{ 576struct stat st; 577if(lstat(path, &st)) 578die("%s: unable to stat (%s)", path,strerror(errno)); 579returnadd_to_index(istate, path, &st, flags); 580} 581 582struct cache_entry *make_cache_entry(unsigned int mode, 583const unsigned char*sha1,const char*path,int stage, 584int refresh) 585{ 586int size, len; 587struct cache_entry *ce; 588 589if(!verify_path(path)) 590return NULL; 591 592 len =strlen(path); 593 size =cache_entry_size(len); 594 ce =xcalloc(1, size); 595 596hashcpy(ce->sha1, sha1); 597memcpy(ce->name, path, len); 598 ce->ce_flags =create_ce_flags(len, stage); 599 ce->ce_mode =create_ce_mode(mode); 600 601if(refresh) 602returnrefresh_cache_entry(ce,0); 603 604return ce; 605} 606 607intce_same_name(struct cache_entry *a,struct cache_entry *b) 608{ 609int len =ce_namelen(a); 610returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 611} 612 613intce_path_match(const struct cache_entry *ce,const char**pathspec) 614{ 615const char*match, *name; 616int len; 617 618if(!pathspec) 619return1; 620 621 len =ce_namelen(ce); 622 name = ce->name; 623while((match = *pathspec++) != NULL) { 624int matchlen =strlen(match); 625if(matchlen > len) 626continue; 627if(memcmp(name, match, matchlen)) 628continue; 629if(matchlen && name[matchlen-1] =='/') 630return1; 631if(name[matchlen] =='/'|| !name[matchlen]) 632return1; 633if(!matchlen) 634return1; 635} 636return0; 637} 638 639/* 640 * We fundamentally don't like some paths: we don't want 641 * dot or dot-dot anywhere, and for obvious reasons don't 642 * want to recurse into ".git" either. 643 * 644 * Also, we don't want double slashes or slashes at the 645 * end that can make pathnames ambiguous. 646 */ 647static intverify_dotfile(const char*rest) 648{ 649/* 650 * The first character was '.', but that 651 * has already been discarded, we now test 652 * the rest. 653 */ 654switch(*rest) { 655/* "." is not allowed */ 656case'\0':case'/': 657return0; 658 659/* 660 * ".git" followed by NUL or slash is bad. This 661 * shares the path end test with the ".." case. 662 */ 663case'g': 664if(rest[1] !='i') 665break; 666if(rest[2] !='t') 667break; 668 rest +=2; 669/* fallthrough */ 670case'.': 671if(rest[1] =='\0'|| rest[1] =='/') 672return0; 673} 674return1; 675} 676 677intverify_path(const char*path) 678{ 679char c; 680 681goto inside; 682for(;;) { 683if(!c) 684return1; 685if(c =='/') { 686inside: 687 c = *path++; 688switch(c) { 689default: 690continue; 691case'/':case'\0': 692break; 693case'.': 694if(verify_dotfile(path)) 695continue; 696} 697return0; 698} 699 c = *path++; 700} 701} 702 703/* 704 * Do we have another file that has the beginning components being a 705 * proper superset of the name we're trying to add? 706 */ 707static inthas_file_name(struct index_state *istate, 708const struct cache_entry *ce,int pos,int ok_to_replace) 709{ 710int retval =0; 711int len =ce_namelen(ce); 712int stage =ce_stage(ce); 713const char*name = ce->name; 714 715while(pos < istate->cache_nr) { 716struct cache_entry *p = istate->cache[pos++]; 717 718if(len >=ce_namelen(p)) 719break; 720if(memcmp(name, p->name, len)) 721break; 722if(ce_stage(p) != stage) 723continue; 724if(p->name[len] !='/') 725continue; 726if(p->ce_flags & CE_REMOVE) 727continue; 728 retval = -1; 729if(!ok_to_replace) 730break; 731remove_index_entry_at(istate, --pos); 732} 733return retval; 734} 735 736/* 737 * Do we have another file with a pathname that is a proper 738 * subset of the name we're trying to add? 739 */ 740static inthas_dir_name(struct index_state *istate, 741const struct cache_entry *ce,int pos,int ok_to_replace) 742{ 743int retval =0; 744int stage =ce_stage(ce); 745const char*name = ce->name; 746const char*slash = name +ce_namelen(ce); 747 748for(;;) { 749int len; 750 751for(;;) { 752if(*--slash =='/') 753break; 754if(slash <= ce->name) 755return retval; 756} 757 len = slash - name; 758 759 pos =index_name_pos(istate, name,create_ce_flags(len, stage)); 760if(pos >=0) { 761/* 762 * Found one, but not so fast. This could 763 * be a marker that says "I was here, but 764 * I am being removed". Such an entry is 765 * not a part of the resulting tree, and 766 * it is Ok to have a directory at the same 767 * path. 768 */ 769if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 770 retval = -1; 771if(!ok_to_replace) 772break; 773remove_index_entry_at(istate, pos); 774continue; 775} 776} 777else 778 pos = -pos-1; 779 780/* 781 * Trivial optimization: if we find an entry that 782 * already matches the sub-directory, then we know 783 * we're ok, and we can exit. 784 */ 785while(pos < istate->cache_nr) { 786struct cache_entry *p = istate->cache[pos]; 787if((ce_namelen(p) <= len) || 788(p->name[len] !='/') || 789memcmp(p->name, name, len)) 790break;/* not our subdirectory */ 791if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 792/* 793 * p is at the same stage as our entry, and 794 * is a subdirectory of what we are looking 795 * at, so we cannot have conflicts at our 796 * level or anything shorter. 797 */ 798return retval; 799 pos++; 800} 801} 802return retval; 803} 804 805/* We may be in a situation where we already have path/file and path 806 * is being added, or we already have path and path/file is being 807 * added. Either one would result in a nonsense tree that has path 808 * twice when git-write-tree tries to write it out. Prevent it. 809 * 810 * If ok-to-replace is specified, we remove the conflicting entries 811 * from the cache so the caller should recompute the insert position. 812 * When this happens, we return non-zero. 813 */ 814static intcheck_file_directory_conflict(struct index_state *istate, 815const struct cache_entry *ce, 816int pos,int ok_to_replace) 817{ 818int retval; 819 820/* 821 * When ce is an "I am going away" entry, we allow it to be added 822 */ 823if(ce->ce_flags & CE_REMOVE) 824return0; 825 826/* 827 * We check if the path is a sub-path of a subsequent pathname 828 * first, since removing those will not change the position 829 * in the array. 830 */ 831 retval =has_file_name(istate, ce, pos, ok_to_replace); 832 833/* 834 * Then check if the path might have a clashing sub-directory 835 * before it. 836 */ 837return retval +has_dir_name(istate, ce, pos, ok_to_replace); 838} 839 840static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 841{ 842int pos; 843int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 844int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 845int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 846 847cache_tree_invalidate_path(istate->cache_tree, ce->name); 848 pos =index_name_pos(istate, ce->name, ce->ce_flags); 849 850/* existing match? Just replace it. */ 851if(pos >=0) { 852replace_index_entry(istate, pos, ce); 853return0; 854} 855 pos = -pos-1; 856 857/* 858 * Inserting a merged entry ("stage 0") into the index 859 * will always replace all non-merged entries.. 860 */ 861if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 862while(ce_same_name(istate->cache[pos], ce)) { 863 ok_to_add =1; 864if(!remove_index_entry_at(istate, pos)) 865break; 866} 867} 868 869if(!ok_to_add) 870return-1; 871if(!verify_path(ce->name)) 872return-1; 873 874if(!skip_df_check && 875check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 876if(!ok_to_replace) 877returnerror("'%s' appears as both a file and as a directory", 878 ce->name); 879 pos =index_name_pos(istate, ce->name, ce->ce_flags); 880 pos = -pos-1; 881} 882return pos +1; 883} 884 885intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 886{ 887int pos; 888 889if(option & ADD_CACHE_JUST_APPEND) 890 pos = istate->cache_nr; 891else{ 892int ret; 893 ret =add_index_entry_with_check(istate, ce, option); 894if(ret <=0) 895return ret; 896 pos = ret -1; 897} 898 899/* Make sure the array is big enough .. */ 900if(istate->cache_nr == istate->cache_alloc) { 901 istate->cache_alloc =alloc_nr(istate->cache_alloc); 902 istate->cache =xrealloc(istate->cache, 903 istate->cache_alloc *sizeof(struct cache_entry *)); 904} 905 906/* Add it in.. */ 907 istate->cache_nr++; 908if(istate->cache_nr > pos +1) 909memmove(istate->cache + pos +1, 910 istate->cache + pos, 911(istate->cache_nr - pos -1) *sizeof(ce)); 912set_index_entry(istate, pos, ce); 913 istate->cache_changed =1; 914return0; 915} 916 917/* 918 * "refresh" does not calculate a new sha1 file or bring the 919 * cache up-to-date for mode/content changes. But what it 920 * _does_ do is to "re-match" the stat information of a file 921 * with the cache, so that you can refresh the cache for a 922 * file that hasn't been changed but where the stat entry is 923 * out of date. 924 * 925 * For example, you'd want to do this after doing a "git-read-tree", 926 * to link up the stat cache details with the proper files. 927 */ 928static struct cache_entry *refresh_cache_ent(struct index_state *istate, 929struct cache_entry *ce, 930unsigned int options,int*err) 931{ 932struct stat st; 933struct cache_entry *updated; 934int changed, size; 935int ignore_valid = options & CE_MATCH_IGNORE_VALID; 936 937if(ce_uptodate(ce)) 938return ce; 939 940/* 941 * CE_VALID means the user promised us that the change to 942 * the work tree does not matter and told us not to worry. 943 */ 944if(!ignore_valid && (ce->ce_flags & CE_VALID)) { 945ce_mark_uptodate(ce); 946return ce; 947} 948 949if(lstat(ce->name, &st) <0) { 950if(err) 951*err = errno; 952return NULL; 953} 954 955 changed =ie_match_stat(istate, ce, &st, options); 956if(!changed) { 957/* 958 * The path is unchanged. If we were told to ignore 959 * valid bit, then we did the actual stat check and 960 * found that the entry is unmodified. If the entry 961 * is not marked VALID, this is the place to mark it 962 * valid again, under "assume unchanged" mode. 963 */ 964if(ignore_valid && assume_unchanged && 965!(ce->ce_flags & CE_VALID)) 966;/* mark this one VALID again */ 967else{ 968/* 969 * We do not mark the index itself "modified" 970 * because CE_UPTODATE flag is in-core only; 971 * we are not going to write this change out. 972 */ 973ce_mark_uptodate(ce); 974return ce; 975} 976} 977 978if(ie_modified(istate, ce, &st, options)) { 979if(err) 980*err = EINVAL; 981return NULL; 982} 983 984 size =ce_size(ce); 985 updated =xmalloc(size); 986memcpy(updated, ce, size); 987fill_stat_cache_info(updated, &st); 988/* 989 * If ignore_valid is not set, we should leave CE_VALID bit 990 * alone. Otherwise, paths marked with --no-assume-unchanged 991 * (i.e. things to be edited) will reacquire CE_VALID bit 992 * automatically, which is not really what we want. 993 */ 994if(!ignore_valid && assume_unchanged && 995!(ce->ce_flags & CE_VALID)) 996 updated->ce_flags &= ~CE_VALID; 997 998return updated; 999}10001001intrefresh_index(struct index_state *istate,unsigned int flags,const char**pathspec,char*seen)1002{1003int i;1004int has_errors =0;1005int really = (flags & REFRESH_REALLY) !=0;1006int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1007int quiet = (flags & REFRESH_QUIET) !=0;1008int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1009int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1010unsigned int options = really ? CE_MATCH_IGNORE_VALID :0;1011const char*needs_update_message;10121013 needs_update_message = ((flags & REFRESH_SAY_CHANGED)1014?"locally modified":"needs update");1015for(i =0; i < istate->cache_nr; i++) {1016struct cache_entry *ce, *new;1017int cache_errno =0;10181019 ce = istate->cache[i];1020if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1021continue;10221023if(ce_stage(ce)) {1024while((i < istate->cache_nr) &&1025!strcmp(istate->cache[i]->name, ce->name))1026 i++;1027 i--;1028if(allow_unmerged)1029continue;1030printf("%s: needs merge\n", ce->name);1031 has_errors =1;1032continue;1033}10341035if(pathspec && !match_pathspec(pathspec, ce->name,strlen(ce->name),0, seen))1036continue;10371038new=refresh_cache_ent(istate, ce, options, &cache_errno);1039if(new== ce)1040continue;1041if(!new) {1042if(not_new && cache_errno == ENOENT)1043continue;1044if(really && cache_errno == EINVAL) {1045/* If we are doing --really-refresh that1046 * means the index is not valid anymore.1047 */1048 ce->ce_flags &= ~CE_VALID;1049 istate->cache_changed =1;1050}1051if(quiet)1052continue;1053printf("%s:%s\n", ce->name, needs_update_message);1054 has_errors =1;1055continue;1056}10571058replace_index_entry(istate, i,new);1059}1060return has_errors;1061}10621063struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really)1064{1065returnrefresh_cache_ent(&the_index, ce, really, NULL);1066}10671068static intverify_hdr(struct cache_header *hdr,unsigned long size)1069{1070 SHA_CTX c;1071unsigned char sha1[20];10721073if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1074returnerror("bad signature");1075if(hdr->hdr_version !=htonl(2))1076returnerror("bad index version");1077SHA1_Init(&c);1078SHA1_Update(&c, hdr, size -20);1079SHA1_Final(sha1, &c);1080if(hashcmp(sha1, (unsigned char*)hdr + size -20))1081returnerror("bad index file sha1 signature");1082return0;1083}10841085static intread_index_extension(struct index_state *istate,1086const char*ext,void*data,unsigned long sz)1087{1088switch(CACHE_EXT(ext)) {1089case CACHE_EXT_TREE:1090 istate->cache_tree =cache_tree_read(data, sz);1091break;1092default:1093if(*ext <'A'||'Z'< *ext)1094returnerror("index uses %.4s extension, which we do not understand",1095 ext);1096fprintf(stderr,"ignoring %.4s extension\n", ext);1097break;1098}1099return0;1100}11011102intread_index(struct index_state *istate)1103{1104returnread_index_from(istate,get_index_file());1105}11061107static voidconvert_from_disk(struct ondisk_cache_entry *ondisk,struct cache_entry *ce)1108{1109size_t len;11101111 ce->ce_ctime =ntohl(ondisk->ctime.sec);1112 ce->ce_mtime =ntohl(ondisk->mtime.sec);1113 ce->ce_dev =ntohl(ondisk->dev);1114 ce->ce_ino =ntohl(ondisk->ino);1115 ce->ce_mode =ntohl(ondisk->mode);1116 ce->ce_uid =ntohl(ondisk->uid);1117 ce->ce_gid =ntohl(ondisk->gid);1118 ce->ce_size =ntohl(ondisk->size);1119/* On-disk flags are just 16 bits */1120 ce->ce_flags =ntohs(ondisk->flags);1121hashcpy(ce->sha1, ondisk->sha1);11221123 len = ce->ce_flags & CE_NAMEMASK;1124if(len == CE_NAMEMASK)1125 len =strlen(ondisk->name);1126/*1127 * NEEDSWORK: If the original index is crafted, this copy could1128 * go unchecked.1129 */1130memcpy(ce->name, ondisk->name, len +1);1131}11321133staticinlinesize_testimate_cache_size(size_t ondisk_size,unsigned int entries)1134{1135long per_entry;11361137 per_entry =sizeof(struct cache_entry) -sizeof(struct ondisk_cache_entry);11381139/*1140 * Alignment can cause differences. This should be "alignof", but1141 * since that's a gcc'ism, just use the size of a pointer.1142 */1143 per_entry +=sizeof(void*);1144return ondisk_size + entries*per_entry;1145}11461147/* remember to discard_cache() before reading a different cache! */1148intread_index_from(struct index_state *istate,const char*path)1149{1150int fd, i;1151struct stat st;1152unsigned long src_offset, dst_offset;1153struct cache_header *hdr;1154void*mmap;1155size_t mmap_size;11561157 errno = EBUSY;1158if(istate->initialized)1159return istate->cache_nr;11601161 errno = ENOENT;1162 istate->timestamp =0;1163 fd =open(path, O_RDONLY);1164if(fd <0) {1165if(errno == ENOENT)1166return0;1167die("index file open failed (%s)",strerror(errno));1168}11691170if(fstat(fd, &st))1171die("cannot stat the open index (%s)",strerror(errno));11721173 errno = EINVAL;1174 mmap_size =xsize_t(st.st_size);1175if(mmap_size <sizeof(struct cache_header) +20)1176die("index file smaller than expected");11771178 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1179close(fd);1180if(mmap == MAP_FAILED)1181die("unable to map index file");11821183 hdr = mmap;1184if(verify_hdr(hdr, mmap_size) <0)1185goto unmap;11861187 istate->cache_nr =ntohl(hdr->hdr_entries);1188 istate->cache_alloc =alloc_nr(istate->cache_nr);1189 istate->cache =xcalloc(istate->cache_alloc,sizeof(struct cache_entry *));11901191/*1192 * The disk format is actually larger than the in-memory format,1193 * due to space for nsec etc, so even though the in-memory one1194 * has room for a few more flags, we can allocate using the same1195 * index size1196 */1197 istate->alloc =xmalloc(estimate_cache_size(mmap_size, istate->cache_nr));1198 istate->initialized =1;11991200 src_offset =sizeof(*hdr);1201 dst_offset =0;1202for(i =0; i < istate->cache_nr; i++) {1203struct ondisk_cache_entry *disk_ce;1204struct cache_entry *ce;12051206 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1207 ce = (struct cache_entry *)((char*)istate->alloc + dst_offset);1208convert_from_disk(disk_ce, ce);1209set_index_entry(istate, i, ce);12101211 src_offset +=ondisk_ce_size(ce);1212 dst_offset +=ce_size(ce);1213}1214 istate->timestamp = st.st_mtime;1215while(src_offset <= mmap_size -20-8) {1216/* After an array of active_nr index entries,1217 * there can be arbitrary number of extended1218 * sections, each of which is prefixed with1219 * extension name (4-byte) and section length1220 * in 4-byte network byte order.1221 */1222unsigned long extsize;1223memcpy(&extsize, (char*)mmap + src_offset +4,4);1224 extsize =ntohl(extsize);1225if(read_index_extension(istate,1226(const char*) mmap + src_offset,1227(char*) mmap + src_offset +8,1228 extsize) <0)1229goto unmap;1230 src_offset +=8;1231 src_offset += extsize;1232}1233munmap(mmap, mmap_size);1234return istate->cache_nr;12351236unmap:1237munmap(mmap, mmap_size);1238 errno = EINVAL;1239die("index file corrupt");1240}12411242intdiscard_index(struct index_state *istate)1243{1244 istate->cache_nr =0;1245 istate->cache_changed =0;1246 istate->timestamp =0;1247 istate->name_hash_initialized =0;1248free_hash(&istate->name_hash);1249cache_tree_free(&(istate->cache_tree));1250free(istate->alloc);1251 istate->alloc = NULL;1252 istate->initialized =0;12531254/* no need to throw away allocated active_cache */1255return0;1256}12571258intunmerged_index(const struct index_state *istate)1259{1260int i;1261for(i =0; i < istate->cache_nr; i++) {1262if(ce_stage(istate->cache[i]))1263return1;1264}1265return0;1266}12671268#define WRITE_BUFFER_SIZE 81921269static unsigned char write_buffer[WRITE_BUFFER_SIZE];1270static unsigned long write_buffer_len;12711272static intce_write_flush(SHA_CTX *context,int fd)1273{1274unsigned int buffered = write_buffer_len;1275if(buffered) {1276SHA1_Update(context, write_buffer, buffered);1277if(write_in_full(fd, write_buffer, buffered) != buffered)1278return-1;1279 write_buffer_len =0;1280}1281return0;1282}12831284static intce_write(SHA_CTX *context,int fd,void*data,unsigned int len)1285{1286while(len) {1287unsigned int buffered = write_buffer_len;1288unsigned int partial = WRITE_BUFFER_SIZE - buffered;1289if(partial > len)1290 partial = len;1291memcpy(write_buffer + buffered, data, partial);1292 buffered += partial;1293if(buffered == WRITE_BUFFER_SIZE) {1294 write_buffer_len = buffered;1295if(ce_write_flush(context, fd))1296return-1;1297 buffered =0;1298}1299 write_buffer_len = buffered;1300 len -= partial;1301 data = (char*) data + partial;1302}1303return0;1304}13051306static intwrite_index_ext_header(SHA_CTX *context,int fd,1307unsigned int ext,unsigned int sz)1308{1309 ext =htonl(ext);1310 sz =htonl(sz);1311return((ce_write(context, fd, &ext,4) <0) ||1312(ce_write(context, fd, &sz,4) <0)) ? -1:0;1313}13141315static intce_flush(SHA_CTX *context,int fd)1316{1317unsigned int left = write_buffer_len;13181319if(left) {1320 write_buffer_len =0;1321SHA1_Update(context, write_buffer, left);1322}13231324/* Flush first if not enough space for SHA1 signature */1325if(left +20> WRITE_BUFFER_SIZE) {1326if(write_in_full(fd, write_buffer, left) != left)1327return-1;1328 left =0;1329}13301331/* Append the SHA1 signature at the end */1332SHA1_Final(write_buffer + left, context);1333 left +=20;1334return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1335}13361337static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1338{1339/*1340 * The only thing we care about in this function is to smudge the1341 * falsely clean entry due to touch-update-touch race, so we leave1342 * everything else as they are. We are called for entries whose1343 * ce_mtime match the index file mtime.1344 *1345 * Note that this actually does not do much for gitlinks, for1346 * which ce_match_stat_basic() always goes to the actual1347 * contents. The caller checks with is_racy_timestamp() which1348 * always says "no" for gitlinks, so we are not called for them ;-)1349 */1350struct stat st;13511352if(lstat(ce->name, &st) <0)1353return;1354if(ce_match_stat_basic(ce, &st))1355return;1356if(ce_modified_check_fs(ce, &st)) {1357/* This is "racily clean"; smudge it. Note that this1358 * is a tricky code. At first glance, it may appear1359 * that it can break with this sequence:1360 *1361 * $ echo xyzzy >frotz1362 * $ git-update-index --add frotz1363 * $ : >frotz1364 * $ sleep 31365 * $ echo filfre >nitfol1366 * $ git-update-index --add nitfol1367 *1368 * but it does not. When the second update-index runs,1369 * it notices that the entry "frotz" has the same timestamp1370 * as index, and if we were to smudge it by resetting its1371 * size to zero here, then the object name recorded1372 * in index is the 6-byte file but the cached stat information1373 * becomes zero --- which would then match what we would1374 * obtain from the filesystem next time we stat("frotz").1375 *1376 * However, the second update-index, before calling1377 * this function, notices that the cached size is 61378 * bytes and what is on the filesystem is an empty1379 * file, and never calls us, so the cached size information1380 * for "frotz" stays 6 which does not match the filesystem.1381 */1382 ce->ce_size =0;1383}1384}13851386static intce_write_entry(SHA_CTX *c,int fd,struct cache_entry *ce)1387{1388int size =ondisk_ce_size(ce);1389struct ondisk_cache_entry *ondisk =xcalloc(1, size);13901391 ondisk->ctime.sec =htonl(ce->ce_ctime);1392 ondisk->ctime.nsec =0;1393 ondisk->mtime.sec =htonl(ce->ce_mtime);1394 ondisk->mtime.nsec =0;1395 ondisk->dev =htonl(ce->ce_dev);1396 ondisk->ino =htonl(ce->ce_ino);1397 ondisk->mode =htonl(ce->ce_mode);1398 ondisk->uid =htonl(ce->ce_uid);1399 ondisk->gid =htonl(ce->ce_gid);1400 ondisk->size =htonl(ce->ce_size);1401hashcpy(ondisk->sha1, ce->sha1);1402 ondisk->flags =htons(ce->ce_flags);1403memcpy(ondisk->name, ce->name,ce_namelen(ce));14041405returnce_write(c, fd, ondisk, size);1406}14071408intwrite_index(const struct index_state *istate,int newfd)1409{1410 SHA_CTX c;1411struct cache_header hdr;1412int i, err, removed;1413struct cache_entry **cache = istate->cache;1414int entries = istate->cache_nr;14151416for(i = removed =0; i < entries; i++)1417if(cache[i]->ce_flags & CE_REMOVE)1418 removed++;14191420 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1421 hdr.hdr_version =htonl(2);1422 hdr.hdr_entries =htonl(entries - removed);14231424SHA1_Init(&c);1425if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1426return-1;14271428for(i =0; i < entries; i++) {1429struct cache_entry *ce = cache[i];1430if(ce->ce_flags & CE_REMOVE)1431continue;1432if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1433ce_smudge_racily_clean_entry(ce);1434if(ce_write_entry(&c, newfd, ce) <0)1435return-1;1436}14371438/* Write extension data here */1439if(istate->cache_tree) {1440struct strbuf sb;14411442strbuf_init(&sb,0);1443cache_tree_write(&sb, istate->cache_tree);1444 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01445||ce_write(&c, newfd, sb.buf, sb.len) <0;1446strbuf_release(&sb);1447if(err)1448return-1;1449}1450returnce_flush(&c, newfd);1451}14521453/*1454 * Read the index file that is potentially unmerged into given1455 * index_state, dropping any unmerged entries. Returns true is1456 * the index is unmerged. Callers who want to refuse to work1457 * from an unmerged state can call this and check its return value,1458 * instead of calling read_cache().1459 */1460intread_index_unmerged(struct index_state *istate)1461{1462int i;1463struct cache_entry **dst;1464struct cache_entry *last = NULL;14651466read_index(istate);1467 dst = istate->cache;1468for(i =0; i < istate->cache_nr; i++) {1469struct cache_entry *ce = istate->cache[i];1470if(ce_stage(ce)) {1471remove_name_hash(ce);1472if(last && !strcmp(ce->name, last->name))1473continue;1474cache_tree_invalidate_path(istate->cache_tree, ce->name);1475 last = ce;1476continue;1477}1478*dst++ = ce;1479}1480 istate->cache_nr = dst - istate->cache;1481return!!last;1482}