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