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#include"split-index.h" 18 19static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, 20unsigned int options); 21 22/* Mask for the name length in ce_flags in the on-disk index */ 23 24#define CE_NAMEMASK (0x0fff) 25 26/* Index extensions. 27 * 28 * The first letter should be 'A'..'Z' for extensions that are not 29 * necessary for a correct operation (i.e. optimization data). 30 * When new extensions are added that _needs_ to be understood in 31 * order to correctly interpret the index file, pick character that 32 * is outside the range, to cause the reader to abort. 33 */ 34 35#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 36#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 37#define CACHE_EXT_RESOLVE_UNDO 0x52455543/* "REUC" */ 38#define CACHE_EXT_LINK 0x6c696e6b/* "link" */ 39 40/* changes that can be kept in $GIT_DIR/index (basically all extensions) */ 41#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ 42 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED) 43 44struct index_state the_index; 45static const char*alternate_index_output; 46 47static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 48{ 49 istate->cache[nr] = ce; 50add_name_hash(istate, ce); 51} 52 53static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 54{ 55struct cache_entry *old = istate->cache[nr]; 56 57replace_index_entry_in_base(istate, old, ce); 58remove_name_hash(istate, old); 59free(old); 60set_index_entry(istate, nr, ce); 61 ce->ce_flags |= CE_UPDATE_IN_BASE; 62 istate->cache_changed |= CE_ENTRY_CHANGED; 63} 64 65voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 66{ 67struct cache_entry *old = istate->cache[nr], *new; 68int namelen =strlen(new_name); 69 70new=xmalloc(cache_entry_size(namelen)); 71copy_cache_entry(new, old); 72new->ce_flags &= ~CE_HASHED; 73new->ce_namelen = namelen; 74new->index =0; 75memcpy(new->name, new_name, namelen +1); 76 77cache_tree_invalidate_path(istate, old->name); 78remove_index_entry_at(istate, nr); 79add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 80} 81 82voidfill_stat_data(struct stat_data *sd,struct stat *st) 83{ 84 sd->sd_ctime.sec = (unsigned int)st->st_ctime; 85 sd->sd_mtime.sec = (unsigned int)st->st_mtime; 86 sd->sd_ctime.nsec =ST_CTIME_NSEC(*st); 87 sd->sd_mtime.nsec =ST_MTIME_NSEC(*st); 88 sd->sd_dev = st->st_dev; 89 sd->sd_ino = st->st_ino; 90 sd->sd_uid = st->st_uid; 91 sd->sd_gid = st->st_gid; 92 sd->sd_size = st->st_size; 93} 94 95intmatch_stat_data(const struct stat_data *sd,struct stat *st) 96{ 97int changed =0; 98 99if(sd->sd_mtime.sec != (unsigned int)st->st_mtime) 100 changed |= MTIME_CHANGED; 101if(trust_ctime && check_stat && 102 sd->sd_ctime.sec != (unsigned int)st->st_ctime) 103 changed |= CTIME_CHANGED; 104 105#ifdef USE_NSEC 106if(check_stat && sd->sd_mtime.nsec !=ST_MTIME_NSEC(*st)) 107 changed |= MTIME_CHANGED; 108if(trust_ctime && check_stat && 109 sd->sd_ctime.nsec !=ST_CTIME_NSEC(*st)) 110 changed |= CTIME_CHANGED; 111#endif 112 113if(check_stat) { 114if(sd->sd_uid != (unsigned int) st->st_uid || 115 sd->sd_gid != (unsigned int) st->st_gid) 116 changed |= OWNER_CHANGED; 117if(sd->sd_ino != (unsigned int) st->st_ino) 118 changed |= INODE_CHANGED; 119} 120 121#ifdef USE_STDEV 122/* 123 * st_dev breaks on network filesystems where different 124 * clients will have different views of what "device" 125 * the filesystem is on 126 */ 127if(check_stat && sd->sd_dev != (unsigned int) st->st_dev) 128 changed |= INODE_CHANGED; 129#endif 130 131if(sd->sd_size != (unsigned int) st->st_size) 132 changed |= DATA_CHANGED; 133 134return changed; 135} 136 137/* 138 * This only updates the "non-critical" parts of the directory 139 * cache, ie the parts that aren't tracked by GIT, and only used 140 * to validate the cache. 141 */ 142voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 143{ 144fill_stat_data(&ce->ce_stat_data, st); 145 146if(assume_unchanged) 147 ce->ce_flags |= CE_VALID; 148 149if(S_ISREG(st->st_mode)) 150ce_mark_uptodate(ce); 151} 152 153static intce_compare_data(const struct cache_entry *ce,struct stat *st) 154{ 155int match = -1; 156int fd =open(ce->name, O_RDONLY); 157 158if(fd >=0) { 159unsigned char sha1[20]; 160if(!index_fd(sha1, fd, st, OBJ_BLOB, ce->name,0)) 161 match =hashcmp(sha1, ce->sha1); 162/* index_fd() closed the file descriptor already */ 163} 164return match; 165} 166 167static intce_compare_link(const struct cache_entry *ce,size_t expected_size) 168{ 169int match = -1; 170void*buffer; 171unsigned long size; 172enum object_type type; 173struct strbuf sb = STRBUF_INIT; 174 175if(strbuf_readlink(&sb, ce->name, expected_size)) 176return-1; 177 178 buffer =read_sha1_file(ce->sha1, &type, &size); 179if(buffer) { 180if(size == sb.len) 181 match =memcmp(buffer, sb.buf, size); 182free(buffer); 183} 184strbuf_release(&sb); 185return match; 186} 187 188static intce_compare_gitlink(const struct cache_entry *ce) 189{ 190unsigned char sha1[20]; 191 192/* 193 * We don't actually require that the .git directory 194 * under GITLINK directory be a valid git directory. It 195 * might even be missing (in case nobody populated that 196 * sub-project). 197 * 198 * If so, we consider it always to match. 199 */ 200if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 201return0; 202returnhashcmp(sha1, ce->sha1); 203} 204 205static intce_modified_check_fs(const struct cache_entry *ce,struct stat *st) 206{ 207switch(st->st_mode & S_IFMT) { 208case S_IFREG: 209if(ce_compare_data(ce, st)) 210return DATA_CHANGED; 211break; 212case S_IFLNK: 213if(ce_compare_link(ce,xsize_t(st->st_size))) 214return DATA_CHANGED; 215break; 216case S_IFDIR: 217if(S_ISGITLINK(ce->ce_mode)) 218returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 219default: 220return TYPE_CHANGED; 221} 222return0; 223} 224 225static intce_match_stat_basic(const struct cache_entry *ce,struct stat *st) 226{ 227unsigned int changed =0; 228 229if(ce->ce_flags & CE_REMOVE) 230return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 231 232switch(ce->ce_mode & S_IFMT) { 233case S_IFREG: 234 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 235/* We consider only the owner x bit to be relevant for 236 * "mode changes" 237 */ 238if(trust_executable_bit && 239(0100& (ce->ce_mode ^ st->st_mode))) 240 changed |= MODE_CHANGED; 241break; 242case S_IFLNK: 243if(!S_ISLNK(st->st_mode) && 244(has_symlinks || !S_ISREG(st->st_mode))) 245 changed |= TYPE_CHANGED; 246break; 247case S_IFGITLINK: 248/* We ignore most of the st_xxx fields for gitlinks */ 249if(!S_ISDIR(st->st_mode)) 250 changed |= TYPE_CHANGED; 251else if(ce_compare_gitlink(ce)) 252 changed |= DATA_CHANGED; 253return changed; 254default: 255die("internal error: ce_mode is%o", ce->ce_mode); 256} 257 258 changed |=match_stat_data(&ce->ce_stat_data, st); 259 260/* Racily smudged entry? */ 261if(!ce->ce_stat_data.sd_size) { 262if(!is_empty_blob_sha1(ce->sha1)) 263 changed |= DATA_CHANGED; 264} 265 266return changed; 267} 268 269static intis_racy_timestamp(const struct index_state *istate, 270const struct cache_entry *ce) 271{ 272return(!S_ISGITLINK(ce->ce_mode) && 273 istate->timestamp.sec && 274#ifdef USE_NSEC 275/* nanosecond timestamped files can also be racy! */ 276(istate->timestamp.sec < ce->ce_stat_data.sd_mtime.sec || 277(istate->timestamp.sec == ce->ce_stat_data.sd_mtime.sec && 278 istate->timestamp.nsec <= ce->ce_stat_data.sd_mtime.nsec)) 279#else 280 istate->timestamp.sec <= ce->ce_stat_data.sd_mtime.sec 281#endif 282); 283} 284 285intie_match_stat(const struct index_state *istate, 286const struct cache_entry *ce,struct stat *st, 287unsigned int options) 288{ 289unsigned int changed; 290int ignore_valid = options & CE_MATCH_IGNORE_VALID; 291int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; 292int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 293 294/* 295 * If it's marked as always valid in the index, it's 296 * valid whatever the checked-out copy says. 297 * 298 * skip-worktree has the same effect with higher precedence 299 */ 300if(!ignore_skip_worktree &&ce_skip_worktree(ce)) 301return0; 302if(!ignore_valid && (ce->ce_flags & CE_VALID)) 303return0; 304 305/* 306 * Intent-to-add entries have not been added, so the index entry 307 * by definition never matches what is in the work tree until it 308 * actually gets added. 309 */ 310if(ce->ce_flags & CE_INTENT_TO_ADD) 311return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; 312 313 changed =ce_match_stat_basic(ce, st); 314 315/* 316 * Within 1 second of this sequence: 317 * echo xyzzy >file && git-update-index --add file 318 * running this command: 319 * echo frotz >file 320 * would give a falsely clean cache entry. The mtime and 321 * length match the cache, and other stat fields do not change. 322 * 323 * We could detect this at update-index time (the cache entry 324 * being registered/updated records the same time as "now") 325 * and delay the return from git-update-index, but that would 326 * effectively mean we can make at most one commit per second, 327 * which is not acceptable. Instead, we check cache entries 328 * whose mtime are the same as the index file timestamp more 329 * carefully than others. 330 */ 331if(!changed &&is_racy_timestamp(istate, ce)) { 332if(assume_racy_is_modified) 333 changed |= DATA_CHANGED; 334else 335 changed |=ce_modified_check_fs(ce, st); 336} 337 338return changed; 339} 340 341intie_modified(const struct index_state *istate, 342const struct cache_entry *ce, 343struct stat *st,unsigned int options) 344{ 345int changed, changed_fs; 346 347 changed =ie_match_stat(istate, ce, st, options); 348if(!changed) 349return0; 350/* 351 * If the mode or type has changed, there's no point in trying 352 * to refresh the entry - it's not going to match 353 */ 354if(changed & (MODE_CHANGED | TYPE_CHANGED)) 355return changed; 356 357/* 358 * Immediately after read-tree or update-index --cacheinfo, 359 * the length field is zero, as we have never even read the 360 * lstat(2) information once, and we cannot trust DATA_CHANGED 361 * returned by ie_match_stat() which in turn was returned by 362 * ce_match_stat_basic() to signal that the filesize of the 363 * blob changed. We have to actually go to the filesystem to 364 * see if the contents match, and if so, should answer "unchanged". 365 * 366 * The logic does not apply to gitlinks, as ce_match_stat_basic() 367 * already has checked the actual HEAD from the filesystem in the 368 * subproject. If ie_match_stat() already said it is different, 369 * then we know it is. 370 */ 371if((changed & DATA_CHANGED) && 372(S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size !=0)) 373return changed; 374 375 changed_fs =ce_modified_check_fs(ce, st); 376if(changed_fs) 377return changed | changed_fs; 378return0; 379} 380 381intbase_name_compare(const char*name1,int len1,int mode1, 382const char*name2,int len2,int mode2) 383{ 384unsigned char c1, c2; 385int len = len1 < len2 ? len1 : len2; 386int cmp; 387 388 cmp =memcmp(name1, name2, len); 389if(cmp) 390return cmp; 391 c1 = name1[len]; 392 c2 = name2[len]; 393if(!c1 &&S_ISDIR(mode1)) 394 c1 ='/'; 395if(!c2 &&S_ISDIR(mode2)) 396 c2 ='/'; 397return(c1 < c2) ? -1: (c1 > c2) ?1:0; 398} 399 400/* 401 * df_name_compare() is identical to base_name_compare(), except it 402 * compares conflicting directory/file entries as equal. Note that 403 * while a directory name compares as equal to a regular file, they 404 * then individually compare _differently_ to a filename that has 405 * a dot after the basename (because '\0' < '.' < '/'). 406 * 407 * This is used by routines that want to traverse the git namespace 408 * but then handle conflicting entries together when possible. 409 */ 410intdf_name_compare(const char*name1,int len1,int mode1, 411const char*name2,int len2,int mode2) 412{ 413int len = len1 < len2 ? len1 : len2, cmp; 414unsigned char c1, c2; 415 416 cmp =memcmp(name1, name2, len); 417if(cmp) 418return cmp; 419/* Directories and files compare equal (same length, same name) */ 420if(len1 == len2) 421return0; 422 c1 = name1[len]; 423if(!c1 &&S_ISDIR(mode1)) 424 c1 ='/'; 425 c2 = name2[len]; 426if(!c2 &&S_ISDIR(mode2)) 427 c2 ='/'; 428if(c1 =='/'&& !c2) 429return0; 430if(c2 =='/'&& !c1) 431return0; 432return c1 - c2; 433} 434 435intcache_name_stage_compare(const char*name1,int len1,int stage1,const char*name2,int len2,int stage2) 436{ 437int len = len1 < len2 ? len1 : len2; 438int cmp; 439 440 cmp =memcmp(name1, name2, len); 441if(cmp) 442return cmp; 443if(len1 < len2) 444return-1; 445if(len1 > len2) 446return1; 447 448if(stage1 < stage2) 449return-1; 450if(stage1 > stage2) 451return1; 452return0; 453} 454 455intcache_name_compare(const char*name1,int len1,const char*name2,int len2) 456{ 457returncache_name_stage_compare(name1, len1,0, name2, len2,0); 458} 459 460static intindex_name_stage_pos(const struct index_state *istate,const char*name,int namelen,int stage) 461{ 462int first, last; 463 464 first =0; 465 last = istate->cache_nr; 466while(last > first) { 467int next = (last + first) >>1; 468struct cache_entry *ce = istate->cache[next]; 469int cmp =cache_name_stage_compare(name, namelen, stage, ce->name,ce_namelen(ce),ce_stage(ce)); 470if(!cmp) 471return next; 472if(cmp <0) { 473 last = next; 474continue; 475} 476 first = next+1; 477} 478return-first-1; 479} 480 481intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 482{ 483returnindex_name_stage_pos(istate, name, namelen,0); 484} 485 486/* Remove entry, return true if there are more entries to go.. */ 487intremove_index_entry_at(struct index_state *istate,int pos) 488{ 489struct cache_entry *ce = istate->cache[pos]; 490 491record_resolve_undo(istate, ce); 492remove_name_hash(istate, ce); 493save_or_free_index_entry(istate, ce); 494 istate->cache_changed |= CE_ENTRY_REMOVED; 495 istate->cache_nr--; 496if(pos >= istate->cache_nr) 497return0; 498memmove(istate->cache + pos, 499 istate->cache + pos +1, 500(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 501return1; 502} 503 504/* 505 * Remove all cache entries marked for removal, that is where 506 * CE_REMOVE is set in ce_flags. This is much more effective than 507 * calling remove_index_entry_at() for each entry to be removed. 508 */ 509voidremove_marked_cache_entries(struct index_state *istate) 510{ 511struct cache_entry **ce_array = istate->cache; 512unsigned int i, j; 513 514for(i = j =0; i < istate->cache_nr; i++) { 515if(ce_array[i]->ce_flags & CE_REMOVE) { 516remove_name_hash(istate, ce_array[i]); 517save_or_free_index_entry(istate, ce_array[i]); 518} 519else 520 ce_array[j++] = ce_array[i]; 521} 522if(j == istate->cache_nr) 523return; 524 istate->cache_changed |= CE_ENTRY_REMOVED; 525 istate->cache_nr = j; 526} 527 528intremove_file_from_index(struct index_state *istate,const char*path) 529{ 530int pos =index_name_pos(istate, path,strlen(path)); 531if(pos <0) 532 pos = -pos-1; 533cache_tree_invalidate_path(istate, path); 534while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 535remove_index_entry_at(istate, pos); 536return0; 537} 538 539static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 540{ 541return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 542} 543 544static intindex_name_pos_also_unmerged(struct index_state *istate, 545const char*path,int namelen) 546{ 547int pos =index_name_pos(istate, path, namelen); 548struct cache_entry *ce; 549 550if(pos >=0) 551return pos; 552 553/* maybe unmerged? */ 554 pos = -1- pos; 555if(pos >= istate->cache_nr || 556compare_name((ce = istate->cache[pos]), path, namelen)) 557return-1; 558 559/* order of preference: stage 2, 1, 3 */ 560if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 561ce_stage((ce = istate->cache[pos +1])) ==2&& 562!compare_name(ce, path, namelen)) 563 pos++; 564return pos; 565} 566 567static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 568{ 569int len =ce_namelen(ce); 570returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 571} 572 573/* 574 * If we add a filename that aliases in the cache, we will use the 575 * name that we already have - but we don't want to update the same 576 * alias twice, because that implies that there were actually two 577 * different files with aliasing names! 578 * 579 * So we use the CE_ADDED flag to verify that the alias was an old 580 * one before we accept it as 581 */ 582static struct cache_entry *create_alias_ce(struct index_state *istate, 583struct cache_entry *ce, 584struct cache_entry *alias) 585{ 586int len; 587struct cache_entry *new; 588 589if(alias->ce_flags & CE_ADDED) 590die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 591 592/* Ok, create the new entry using the name of the existing alias */ 593 len =ce_namelen(alias); 594new=xcalloc(1,cache_entry_size(len)); 595memcpy(new->name, alias->name, len); 596copy_cache_entry(new, ce); 597save_or_free_index_entry(istate, ce); 598return new; 599} 600 601voidset_object_name_for_intent_to_add_entry(struct cache_entry *ce) 602{ 603unsigned char sha1[20]; 604if(write_sha1_file("",0, blob_type, sha1)) 605die("cannot create an empty blob in the object database"); 606hashcpy(ce->sha1, sha1); 607} 608 609intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 610{ 611int size, namelen, was_same; 612 mode_t st_mode = st->st_mode; 613struct cache_entry *ce, *alias; 614unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; 615int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 616int pretend = flags & ADD_CACHE_PRETEND; 617int intent_only = flags & ADD_CACHE_INTENT; 618int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 619(intent_only ? ADD_CACHE_NEW_ONLY :0)); 620 621if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 622returnerror("%s: can only add regular files, symbolic links or git-directories", path); 623 624 namelen =strlen(path); 625if(S_ISDIR(st_mode)) { 626while(namelen && path[namelen-1] =='/') 627 namelen--; 628} 629 size =cache_entry_size(namelen); 630 ce =xcalloc(1, size); 631memcpy(ce->name, path, namelen); 632 ce->ce_namelen = namelen; 633if(!intent_only) 634fill_stat_cache_info(ce, st); 635else 636 ce->ce_flags |= CE_INTENT_TO_ADD; 637 638if(trust_executable_bit && has_symlinks) 639 ce->ce_mode =create_ce_mode(st_mode); 640else{ 641/* If there is an existing entry, pick the mode bits and type 642 * from it, otherwise assume unexecutable regular file. 643 */ 644struct cache_entry *ent; 645int pos =index_name_pos_also_unmerged(istate, path, namelen); 646 647 ent = (0<= pos) ? istate->cache[pos] : NULL; 648 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 649} 650 651/* When core.ignorecase=true, determine if a directory of the same name but differing 652 * case already exists within the Git repository. If it does, ensure the directory 653 * case of the file being added to the repository matches (is folded into) the existing 654 * entry's directory case. 655 */ 656if(ignore_case) { 657const char*startPtr = ce->name; 658const char*ptr = startPtr; 659while(*ptr) { 660while(*ptr && *ptr !='/') 661++ptr; 662if(*ptr =='/') { 663struct cache_entry *foundce; 664++ptr; 665 foundce =index_dir_exists(istate, ce->name, ptr - ce->name -1); 666if(foundce) { 667memcpy((void*)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); 668 startPtr = ptr; 669} 670} 671} 672} 673 674 alias =index_file_exists(istate, ce->name,ce_namelen(ce), ignore_case); 675if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 676/* Nothing changed, really */ 677free(ce); 678if(!S_ISGITLINK(alias->ce_mode)) 679ce_mark_uptodate(alias); 680 alias->ce_flags |= CE_ADDED; 681return0; 682} 683if(!intent_only) { 684if(index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) 685returnerror("unable to index file%s", path); 686}else 687set_object_name_for_intent_to_add_entry(ce); 688 689if(ignore_case && alias &&different_name(ce, alias)) 690 ce =create_alias_ce(istate, ce, alias); 691 ce->ce_flags |= CE_ADDED; 692 693/* It was suspected to be racily clean, but it turns out to be Ok */ 694 was_same = (alias && 695!ce_stage(alias) && 696!hashcmp(alias->sha1, ce->sha1) && 697 ce->ce_mode == alias->ce_mode); 698 699if(pretend) 700; 701else if(add_index_entry(istate, ce, add_option)) 702returnerror("unable to add%sto index",path); 703if(verbose && !was_same) 704printf("add '%s'\n", path); 705return0; 706} 707 708intadd_file_to_index(struct index_state *istate,const char*path,int flags) 709{ 710struct stat st; 711if(lstat(path, &st)) 712die_errno("unable to stat '%s'", path); 713returnadd_to_index(istate, path, &st, flags); 714} 715 716struct cache_entry *make_cache_entry(unsigned int mode, 717const unsigned char*sha1,const char*path,int stage, 718unsigned int refresh_options) 719{ 720int size, len; 721struct cache_entry *ce; 722 723if(!verify_path(path)) { 724error("Invalid path '%s'", path); 725return NULL; 726} 727 728 len =strlen(path); 729 size =cache_entry_size(len); 730 ce =xcalloc(1, size); 731 732hashcpy(ce->sha1, sha1); 733memcpy(ce->name, path, len); 734 ce->ce_flags =create_ce_flags(stage); 735 ce->ce_namelen = len; 736 ce->ce_mode =create_ce_mode(mode); 737 738returnrefresh_cache_entry(ce, refresh_options); 739} 740 741intce_same_name(const struct cache_entry *a,const struct cache_entry *b) 742{ 743int len =ce_namelen(a); 744returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 745} 746 747/* 748 * We fundamentally don't like some paths: we don't want 749 * dot or dot-dot anywhere, and for obvious reasons don't 750 * want to recurse into ".git" either. 751 * 752 * Also, we don't want double slashes or slashes at the 753 * end that can make pathnames ambiguous. 754 */ 755static intverify_dotfile(const char*rest) 756{ 757/* 758 * The first character was '.', but that 759 * has already been discarded, we now test 760 * the rest. 761 */ 762 763/* "." is not allowed */ 764if(*rest =='\0'||is_dir_sep(*rest)) 765return0; 766 767switch(*rest) { 768/* 769 * ".git" followed by NUL or slash is bad. This 770 * shares the path end test with the ".." case. 771 */ 772case'g': 773if(rest[1] !='i') 774break; 775if(rest[2] !='t') 776break; 777 rest +=2; 778/* fallthrough */ 779case'.': 780if(rest[1] =='\0'||is_dir_sep(rest[1])) 781return0; 782} 783return1; 784} 785 786intverify_path(const char*path) 787{ 788char c; 789 790if(has_dos_drive_prefix(path)) 791return0; 792 793goto inside; 794for(;;) { 795if(!c) 796return1; 797if(is_dir_sep(c)) { 798inside: 799 c = *path++; 800if((c =='.'&& !verify_dotfile(path)) || 801is_dir_sep(c) || c =='\0') 802return0; 803} 804 c = *path++; 805} 806} 807 808/* 809 * Do we have another file that has the beginning components being a 810 * proper superset of the name we're trying to add? 811 */ 812static inthas_file_name(struct index_state *istate, 813const struct cache_entry *ce,int pos,int ok_to_replace) 814{ 815int retval =0; 816int len =ce_namelen(ce); 817int stage =ce_stage(ce); 818const char*name = ce->name; 819 820while(pos < istate->cache_nr) { 821struct cache_entry *p = istate->cache[pos++]; 822 823if(len >=ce_namelen(p)) 824break; 825if(memcmp(name, p->name, len)) 826break; 827if(ce_stage(p) != stage) 828continue; 829if(p->name[len] !='/') 830continue; 831if(p->ce_flags & CE_REMOVE) 832continue; 833 retval = -1; 834if(!ok_to_replace) 835break; 836remove_index_entry_at(istate, --pos); 837} 838return retval; 839} 840 841/* 842 * Do we have another file with a pathname that is a proper 843 * subset of the name we're trying to add? 844 */ 845static inthas_dir_name(struct index_state *istate, 846const struct cache_entry *ce,int pos,int ok_to_replace) 847{ 848int retval =0; 849int stage =ce_stage(ce); 850const char*name = ce->name; 851const char*slash = name +ce_namelen(ce); 852 853for(;;) { 854int len; 855 856for(;;) { 857if(*--slash =='/') 858break; 859if(slash <= ce->name) 860return retval; 861} 862 len = slash - name; 863 864 pos =index_name_stage_pos(istate, name, len, stage); 865if(pos >=0) { 866/* 867 * Found one, but not so fast. This could 868 * be a marker that says "I was here, but 869 * I am being removed". Such an entry is 870 * not a part of the resulting tree, and 871 * it is Ok to have a directory at the same 872 * path. 873 */ 874if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 875 retval = -1; 876if(!ok_to_replace) 877break; 878remove_index_entry_at(istate, pos); 879continue; 880} 881} 882else 883 pos = -pos-1; 884 885/* 886 * Trivial optimization: if we find an entry that 887 * already matches the sub-directory, then we know 888 * we're ok, and we can exit. 889 */ 890while(pos < istate->cache_nr) { 891struct cache_entry *p = istate->cache[pos]; 892if((ce_namelen(p) <= len) || 893(p->name[len] !='/') || 894memcmp(p->name, name, len)) 895break;/* not our subdirectory */ 896if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 897/* 898 * p is at the same stage as our entry, and 899 * is a subdirectory of what we are looking 900 * at, so we cannot have conflicts at our 901 * level or anything shorter. 902 */ 903return retval; 904 pos++; 905} 906} 907return retval; 908} 909 910/* We may be in a situation where we already have path/file and path 911 * is being added, or we already have path and path/file is being 912 * added. Either one would result in a nonsense tree that has path 913 * twice when git-write-tree tries to write it out. Prevent it. 914 * 915 * If ok-to-replace is specified, we remove the conflicting entries 916 * from the cache so the caller should recompute the insert position. 917 * When this happens, we return non-zero. 918 */ 919static intcheck_file_directory_conflict(struct index_state *istate, 920const struct cache_entry *ce, 921int pos,int ok_to_replace) 922{ 923int retval; 924 925/* 926 * When ce is an "I am going away" entry, we allow it to be added 927 */ 928if(ce->ce_flags & CE_REMOVE) 929return0; 930 931/* 932 * We check if the path is a sub-path of a subsequent pathname 933 * first, since removing those will not change the position 934 * in the array. 935 */ 936 retval =has_file_name(istate, ce, pos, ok_to_replace); 937 938/* 939 * Then check if the path might have a clashing sub-directory 940 * before it. 941 */ 942return retval +has_dir_name(istate, ce, pos, ok_to_replace); 943} 944 945static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 946{ 947int pos; 948int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 949int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 950int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 951int new_only = option & ADD_CACHE_NEW_ONLY; 952 953if(!(option & ADD_CACHE_KEEP_CACHE_TREE)) 954cache_tree_invalidate_path(istate, ce->name); 955 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 956 957/* existing match? Just replace it. */ 958if(pos >=0) { 959if(!new_only) 960replace_index_entry(istate, pos, ce); 961return0; 962} 963 pos = -pos-1; 964 965/* 966 * Inserting a merged entry ("stage 0") into the index 967 * will always replace all non-merged entries.. 968 */ 969if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 970while(ce_same_name(istate->cache[pos], ce)) { 971 ok_to_add =1; 972if(!remove_index_entry_at(istate, pos)) 973break; 974} 975} 976 977if(!ok_to_add) 978return-1; 979if(!verify_path(ce->name)) 980returnerror("Invalid path '%s'", ce->name); 981 982if(!skip_df_check && 983check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 984if(!ok_to_replace) 985returnerror("'%s' appears as both a file and as a directory", 986 ce->name); 987 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 988 pos = -pos-1; 989} 990return pos +1; 991} 992 993intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 994{ 995int pos; 996 997if(option & ADD_CACHE_JUST_APPEND) 998 pos = istate->cache_nr; 999else{1000int ret;1001 ret =add_index_entry_with_check(istate, ce, option);1002if(ret <=0)1003return ret;1004 pos = ret -1;1005}10061007/* Make sure the array is big enough .. */1008ALLOC_GROW(istate->cache, istate->cache_nr +1, istate->cache_alloc);10091010/* Add it in.. */1011 istate->cache_nr++;1012if(istate->cache_nr > pos +1)1013memmove(istate->cache + pos +1,1014 istate->cache + pos,1015(istate->cache_nr - pos -1) *sizeof(ce));1016set_index_entry(istate, pos, ce);1017 istate->cache_changed |= CE_ENTRY_ADDED;1018return0;1019}10201021/*1022 * "refresh" does not calculate a new sha1 file or bring the1023 * cache up-to-date for mode/content changes. But what it1024 * _does_ do is to "re-match" the stat information of a file1025 * with the cache, so that you can refresh the cache for a1026 * file that hasn't been changed but where the stat entry is1027 * out of date.1028 *1029 * For example, you'd want to do this after doing a "git-read-tree",1030 * to link up the stat cache details with the proper files.1031 */1032static struct cache_entry *refresh_cache_ent(struct index_state *istate,1033struct cache_entry *ce,1034unsigned int options,int*err,1035int*changed_ret)1036{1037struct stat st;1038struct cache_entry *updated;1039int changed, size;1040int refresh = options & CE_MATCH_REFRESH;1041int ignore_valid = options & CE_MATCH_IGNORE_VALID;1042int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;1043int ignore_missing = options & CE_MATCH_IGNORE_MISSING;10441045if(!refresh ||ce_uptodate(ce))1046return ce;10471048/*1049 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1050 * that the change to the work tree does not matter and told1051 * us not to worry.1052 */1053if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1054ce_mark_uptodate(ce);1055return ce;1056}1057if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1058ce_mark_uptodate(ce);1059return ce;1060}10611062if(lstat(ce->name, &st) <0) {1063if(ignore_missing && errno == ENOENT)1064return ce;1065if(err)1066*err = errno;1067return NULL;1068}10691070 changed =ie_match_stat(istate, ce, &st, options);1071if(changed_ret)1072*changed_ret = changed;1073if(!changed) {1074/*1075 * The path is unchanged. If we were told to ignore1076 * valid bit, then we did the actual stat check and1077 * found that the entry is unmodified. If the entry1078 * is not marked VALID, this is the place to mark it1079 * valid again, under "assume unchanged" mode.1080 */1081if(ignore_valid && assume_unchanged &&1082!(ce->ce_flags & CE_VALID))1083;/* mark this one VALID again */1084else{1085/*1086 * We do not mark the index itself "modified"1087 * because CE_UPTODATE flag is in-core only;1088 * we are not going to write this change out.1089 */1090if(!S_ISGITLINK(ce->ce_mode))1091ce_mark_uptodate(ce);1092return ce;1093}1094}10951096if(ie_modified(istate, ce, &st, options)) {1097if(err)1098*err = EINVAL;1099return NULL;1100}11011102 size =ce_size(ce);1103 updated =xmalloc(size);1104memcpy(updated, ce, size);1105fill_stat_cache_info(updated, &st);1106/*1107 * If ignore_valid is not set, we should leave CE_VALID bit1108 * alone. Otherwise, paths marked with --no-assume-unchanged1109 * (i.e. things to be edited) will reacquire CE_VALID bit1110 * automatically, which is not really what we want.1111 */1112if(!ignore_valid && assume_unchanged &&1113!(ce->ce_flags & CE_VALID))1114 updated->ce_flags &= ~CE_VALID;11151116/* istate->cache_changed is updated in the caller */1117return updated;1118}11191120static voidshow_file(const char* fmt,const char* name,int in_porcelain,1121int* first,const char*header_msg)1122{1123if(in_porcelain && *first && header_msg) {1124printf("%s\n", header_msg);1125*first =0;1126}1127printf(fmt, name);1128}11291130intrefresh_index(struct index_state *istate,unsigned int flags,1131const struct pathspec *pathspec,1132char*seen,const char*header_msg)1133{1134int i;1135int has_errors =0;1136int really = (flags & REFRESH_REALLY) !=0;1137int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1138int quiet = (flags & REFRESH_QUIET) !=0;1139int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1140int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1141int first =1;1142int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1143unsigned int options = (CE_MATCH_REFRESH |1144(really ? CE_MATCH_IGNORE_VALID :0) |1145(not_new ? CE_MATCH_IGNORE_MISSING :0));1146const char*modified_fmt;1147const char*deleted_fmt;1148const char*typechange_fmt;1149const char*added_fmt;1150const char*unmerged_fmt;11511152 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1153 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1154 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1155 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1156 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1157for(i =0; i < istate->cache_nr; i++) {1158struct cache_entry *ce, *new;1159int cache_errno =0;1160int changed =0;1161int filtered =0;11621163 ce = istate->cache[i];1164if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1165continue;11661167if(pathspec && !ce_path_match(ce, pathspec, seen))1168 filtered =1;11691170if(ce_stage(ce)) {1171while((i < istate->cache_nr) &&1172!strcmp(istate->cache[i]->name, ce->name))1173 i++;1174 i--;1175if(allow_unmerged)1176continue;1177if(!filtered)1178show_file(unmerged_fmt, ce->name, in_porcelain,1179&first, header_msg);1180 has_errors =1;1181continue;1182}11831184if(filtered)1185continue;11861187new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1188if(new== ce)1189continue;1190if(!new) {1191const char*fmt;11921193if(really && cache_errno == EINVAL) {1194/* If we are doing --really-refresh that1195 * means the index is not valid anymore.1196 */1197 ce->ce_flags &= ~CE_VALID;1198 ce->ce_flags |= CE_UPDATE_IN_BASE;1199 istate->cache_changed |= CE_ENTRY_CHANGED;1200}1201if(quiet)1202continue;12031204if(cache_errno == ENOENT)1205 fmt = deleted_fmt;1206else if(ce->ce_flags & CE_INTENT_TO_ADD)1207 fmt = added_fmt;/* must be before other checks */1208else if(changed & TYPE_CHANGED)1209 fmt = typechange_fmt;1210else1211 fmt = modified_fmt;1212show_file(fmt,1213 ce->name, in_porcelain, &first, header_msg);1214 has_errors =1;1215continue;1216}12171218replace_index_entry(istate, i,new);1219}1220return has_errors;1221}12221223static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,1224unsigned int options)1225{1226returnrefresh_cache_ent(&the_index, ce, options, NULL, NULL);1227}122812291230/*****************************************************************1231 * Index File I/O1232 *****************************************************************/12331234#define INDEX_FORMAT_DEFAULT 312351236static intindex_format_config(const char*var,const char*value,void*cb)1237{1238unsigned int*version = cb;1239if(!strcmp(var,"index.version")) {1240*version =git_config_int(var, value);1241return0;1242}1243return1;1244}12451246static unsigned intget_index_format_default(void)1247{1248char*envversion =getenv("GIT_INDEX_VERSION");1249char*endp;1250unsigned int version = INDEX_FORMAT_DEFAULT;12511252if(!envversion) {1253git_config(index_format_config, &version);1254if(version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1255warning(_("index.version set, but the value is invalid.\n"1256"Using version%i"), INDEX_FORMAT_DEFAULT);1257return INDEX_FORMAT_DEFAULT;1258}1259return version;1260}12611262 version =strtoul(envversion, &endp,10);1263if(*endp ||1264 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1265warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"1266"Using version%i"), INDEX_FORMAT_DEFAULT);1267 version = INDEX_FORMAT_DEFAULT;1268}1269return version;1270}12711272/*1273 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1274 * Again - this is just a (very strong in practice) heuristic that1275 * the inode hasn't changed.1276 *1277 * We save the fields in big-endian order to allow using the1278 * index file over NFS transparently.1279 */1280struct ondisk_cache_entry {1281struct cache_time ctime;1282struct cache_time mtime;1283uint32_t dev;1284uint32_t ino;1285uint32_t mode;1286uint32_t uid;1287uint32_t gid;1288uint32_t size;1289unsigned char sha1[20];1290uint16_t flags;1291char name[FLEX_ARRAY];/* more */1292};12931294/*1295 * This struct is used when CE_EXTENDED bit is 11296 * The struct must match ondisk_cache_entry exactly from1297 * ctime till flags1298 */1299struct ondisk_cache_entry_extended {1300struct cache_time ctime;1301struct cache_time mtime;1302uint32_t dev;1303uint32_t ino;1304uint32_t mode;1305uint32_t uid;1306uint32_t gid;1307uint32_t size;1308unsigned char sha1[20];1309uint16_t flags;1310uint16_t flags2;1311char name[FLEX_ARRAY];/* more */1312};13131314/* These are only used for v3 or lower */1315#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1316#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1317#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1318#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1319 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1320 ondisk_cache_entry_size(ce_namelen(ce)))13211322static intverify_hdr(struct cache_header *hdr,unsigned long size)1323{1324 git_SHA_CTX c;1325unsigned char sha1[20];1326int hdr_version;13271328if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1329returnerror("bad signature");1330 hdr_version =ntohl(hdr->hdr_version);1331if(hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)1332returnerror("bad index version%d", hdr_version);1333git_SHA1_Init(&c);1334git_SHA1_Update(&c, hdr, size -20);1335git_SHA1_Final(sha1, &c);1336if(hashcmp(sha1, (unsigned char*)hdr + size -20))1337returnerror("bad index file sha1 signature");1338return0;1339}13401341static intread_index_extension(struct index_state *istate,1342const char*ext,void*data,unsigned long sz)1343{1344switch(CACHE_EXT(ext)) {1345case CACHE_EXT_TREE:1346 istate->cache_tree =cache_tree_read(data, sz);1347break;1348case CACHE_EXT_RESOLVE_UNDO:1349 istate->resolve_undo =resolve_undo_read(data, sz);1350break;1351case CACHE_EXT_LINK:1352if(read_link_extension(istate, data, sz))1353return-1;1354break;1355default:1356if(*ext <'A'||'Z'< *ext)1357returnerror("index uses %.4s extension, which we do not understand",1358 ext);1359fprintf(stderr,"ignoring %.4s extension\n", ext);1360break;1361}1362return0;1363}13641365intread_index(struct index_state *istate)1366{1367returnread_index_from(istate,get_index_file());1368}13691370static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1371unsigned int flags,1372const char*name,1373size_t len)1374{1375struct cache_entry *ce =xmalloc(cache_entry_size(len));13761377 ce->ce_stat_data.sd_ctime.sec =get_be32(&ondisk->ctime.sec);1378 ce->ce_stat_data.sd_mtime.sec =get_be32(&ondisk->mtime.sec);1379 ce->ce_stat_data.sd_ctime.nsec =get_be32(&ondisk->ctime.nsec);1380 ce->ce_stat_data.sd_mtime.nsec =get_be32(&ondisk->mtime.nsec);1381 ce->ce_stat_data.sd_dev =get_be32(&ondisk->dev);1382 ce->ce_stat_data.sd_ino =get_be32(&ondisk->ino);1383 ce->ce_mode =get_be32(&ondisk->mode);1384 ce->ce_stat_data.sd_uid =get_be32(&ondisk->uid);1385 ce->ce_stat_data.sd_gid =get_be32(&ondisk->gid);1386 ce->ce_stat_data.sd_size =get_be32(&ondisk->size);1387 ce->ce_flags = flags & ~CE_NAMEMASK;1388 ce->ce_namelen = len;1389 ce->index =0;1390hashcpy(ce->sha1, ondisk->sha1);1391memcpy(ce->name, name, len);1392 ce->name[len] ='\0';1393return ce;1394}13951396/*1397 * Adjacent cache entries tend to share the leading paths, so it makes1398 * sense to only store the differences in later entries. In the v41399 * on-disk format of the index, each on-disk cache entry stores the1400 * number of bytes to be stripped from the end of the previous name,1401 * and the bytes to append to the result, to come up with its name.1402 */1403static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1404{1405const unsigned char*ep, *cp = (const unsigned char*)cp_;1406size_t len =decode_varint(&cp);14071408if(name->len < len)1409die("malformed name field in the index");1410strbuf_remove(name, name->len - len, len);1411for(ep = cp; *ep; ep++)1412;/* find the end */1413strbuf_add(name, cp, ep - cp);1414return(const char*)ep +1- cp_;1415}14161417static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1418unsigned long*ent_size,1419struct strbuf *previous_name)1420{1421struct cache_entry *ce;1422size_t len;1423const char*name;1424unsigned int flags;14251426/* On-disk flags are just 16 bits */1427 flags =get_be16(&ondisk->flags);1428 len = flags & CE_NAMEMASK;14291430if(flags & CE_EXTENDED) {1431struct ondisk_cache_entry_extended *ondisk2;1432int extended_flags;1433 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1434 extended_flags =get_be16(&ondisk2->flags2) <<16;1435/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1436if(extended_flags & ~CE_EXTENDED_FLAGS)1437die("Unknown index entry format%08x", extended_flags);1438 flags |= extended_flags;1439 name = ondisk2->name;1440}1441else1442 name = ondisk->name;14431444if(!previous_name) {1445/* v3 and earlier */1446if(len == CE_NAMEMASK)1447 len =strlen(name);1448 ce =cache_entry_from_ondisk(ondisk, flags, name, len);14491450*ent_size =ondisk_ce_size(ce);1451}else{1452unsigned long consumed;1453 consumed =expand_name_field(previous_name, name);1454 ce =cache_entry_from_ondisk(ondisk, flags,1455 previous_name->buf,1456 previous_name->len);14571458*ent_size = (name - ((char*)ondisk)) + consumed;1459}1460return ce;1461}14621463/* remember to discard_cache() before reading a different cache! */1464static intdo_read_index(struct index_state *istate,const char*path,1465int must_exist)1466{1467int fd, i;1468struct stat st;1469unsigned long src_offset;1470struct cache_header *hdr;1471void*mmap;1472size_t mmap_size;1473struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;14741475if(istate->initialized)1476return istate->cache_nr;14771478 istate->timestamp.sec =0;1479 istate->timestamp.nsec =0;1480 fd =open(path, O_RDONLY);1481if(fd <0) {1482if(!must_exist && errno == ENOENT)1483return0;1484die_errno("%s: index file open failed", path);1485}14861487if(fstat(fd, &st))1488die_errno("cannot stat the open index");14891490 mmap_size =xsize_t(st.st_size);1491if(mmap_size <sizeof(struct cache_header) +20)1492die("index file smaller than expected");14931494 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1495if(mmap == MAP_FAILED)1496die_errno("unable to map index file");1497close(fd);14981499 hdr = mmap;1500if(verify_hdr(hdr, mmap_size) <0)1501goto unmap;15021503hashcpy(istate->sha1, (const unsigned char*)hdr + mmap_size -20);1504 istate->version =ntohl(hdr->hdr_version);1505 istate->cache_nr =ntohl(hdr->hdr_entries);1506 istate->cache_alloc =alloc_nr(istate->cache_nr);1507 istate->cache =xcalloc(istate->cache_alloc,sizeof(*istate->cache));1508 istate->initialized =1;15091510if(istate->version ==4)1511 previous_name = &previous_name_buf;1512else1513 previous_name = NULL;15141515 src_offset =sizeof(*hdr);1516for(i =0; i < istate->cache_nr; i++) {1517struct ondisk_cache_entry *disk_ce;1518struct cache_entry *ce;1519unsigned long consumed;15201521 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1522 ce =create_from_disk(disk_ce, &consumed, previous_name);1523set_index_entry(istate, i, ce);15241525 src_offset += consumed;1526}1527strbuf_release(&previous_name_buf);1528 istate->timestamp.sec = st.st_mtime;1529 istate->timestamp.nsec =ST_MTIME_NSEC(st);15301531while(src_offset <= mmap_size -20-8) {1532/* After an array of active_nr index entries,1533 * there can be arbitrary number of extended1534 * sections, each of which is prefixed with1535 * extension name (4-byte) and section length1536 * in 4-byte network byte order.1537 */1538uint32_t extsize;1539memcpy(&extsize, (char*)mmap + src_offset +4,4);1540 extsize =ntohl(extsize);1541if(read_index_extension(istate,1542(const char*) mmap + src_offset,1543(char*) mmap + src_offset +8,1544 extsize) <0)1545goto unmap;1546 src_offset +=8;1547 src_offset += extsize;1548}1549munmap(mmap, mmap_size);1550return istate->cache_nr;15511552unmap:1553munmap(mmap, mmap_size);1554die("index file corrupt");1555}15561557intread_index_from(struct index_state *istate,const char*path)1558{1559struct split_index *split_index;1560int ret;15611562/* istate->initialized covers both .git/index and .git/sharedindex.xxx */1563if(istate->initialized)1564return istate->cache_nr;15651566 ret =do_read_index(istate, path,0);1567 split_index = istate->split_index;1568if(!split_index)1569return ret;15701571if(is_null_sha1(split_index->base_sha1))1572return ret;15731574if(split_index->base)1575discard_index(split_index->base);1576else1577 split_index->base =xcalloc(1,sizeof(*split_index->base));1578 ret =do_read_index(split_index->base,1579git_path("sharedindex.%s",1580sha1_to_hex(split_index->base_sha1)),1);1581if(hashcmp(split_index->base_sha1, split_index->base->sha1))1582die("broken index, expect%sin%s, got%s",1583sha1_to_hex(split_index->base_sha1),1584git_path("sharedindex.%s",1585sha1_to_hex(split_index->base_sha1)),1586sha1_to_hex(split_index->base->sha1));1587merge_base_index(istate);1588return ret;1589}15901591intis_index_unborn(struct index_state *istate)1592{1593return(!istate->cache_nr && !istate->timestamp.sec);1594}15951596intdiscard_index(struct index_state *istate)1597{1598int i;15991600for(i =0; i < istate->cache_nr; i++) {1601if(istate->cache[i]->index &&1602 istate->split_index &&1603 istate->split_index->base &&1604 istate->cache[i]->index <= istate->split_index->base->cache_nr &&1605 istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index -1])1606continue;1607free(istate->cache[i]);1608}1609resolve_undo_clear_index(istate);1610 istate->cache_nr =0;1611 istate->cache_changed =0;1612 istate->timestamp.sec =0;1613 istate->timestamp.nsec =0;1614free_name_hash(istate);1615cache_tree_free(&(istate->cache_tree));1616 istate->initialized =0;1617free(istate->cache);1618 istate->cache = NULL;1619 istate->cache_alloc =0;1620discard_split_index(istate);1621return0;1622}16231624intunmerged_index(const struct index_state *istate)1625{1626int i;1627for(i =0; i < istate->cache_nr; i++) {1628if(ce_stage(istate->cache[i]))1629return1;1630}1631return0;1632}16331634#define WRITE_BUFFER_SIZE 81921635static unsigned char write_buffer[WRITE_BUFFER_SIZE];1636static unsigned long write_buffer_len;16371638static intce_write_flush(git_SHA_CTX *context,int fd)1639{1640unsigned int buffered = write_buffer_len;1641if(buffered) {1642git_SHA1_Update(context, write_buffer, buffered);1643if(write_in_full(fd, write_buffer, buffered) != buffered)1644return-1;1645 write_buffer_len =0;1646}1647return0;1648}16491650static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1651{1652while(len) {1653unsigned int buffered = write_buffer_len;1654unsigned int partial = WRITE_BUFFER_SIZE - buffered;1655if(partial > len)1656 partial = len;1657memcpy(write_buffer + buffered, data, partial);1658 buffered += partial;1659if(buffered == WRITE_BUFFER_SIZE) {1660 write_buffer_len = buffered;1661if(ce_write_flush(context, fd))1662return-1;1663 buffered =0;1664}1665 write_buffer_len = buffered;1666 len -= partial;1667 data = (char*) data + partial;1668}1669return0;1670}16711672static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1673unsigned int ext,unsigned int sz)1674{1675 ext =htonl(ext);1676 sz =htonl(sz);1677return((ce_write(context, fd, &ext,4) <0) ||1678(ce_write(context, fd, &sz,4) <0)) ? -1:0;1679}16801681static intce_flush(git_SHA_CTX *context,int fd,unsigned char*sha1)1682{1683unsigned int left = write_buffer_len;16841685if(left) {1686 write_buffer_len =0;1687git_SHA1_Update(context, write_buffer, left);1688}16891690/* Flush first if not enough space for SHA1 signature */1691if(left +20> WRITE_BUFFER_SIZE) {1692if(write_in_full(fd, write_buffer, left) != left)1693return-1;1694 left =0;1695}16961697/* Append the SHA1 signature at the end */1698git_SHA1_Final(write_buffer + left, context);1699hashcpy(sha1, write_buffer + left);1700 left +=20;1701return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1702}17031704static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1705{1706/*1707 * The only thing we care about in this function is to smudge the1708 * falsely clean entry due to touch-update-touch race, so we leave1709 * everything else as they are. We are called for entries whose1710 * ce_stat_data.sd_mtime match the index file mtime.1711 *1712 * Note that this actually does not do much for gitlinks, for1713 * which ce_match_stat_basic() always goes to the actual1714 * contents. The caller checks with is_racy_timestamp() which1715 * always says "no" for gitlinks, so we are not called for them ;-)1716 */1717struct stat st;17181719if(lstat(ce->name, &st) <0)1720return;1721if(ce_match_stat_basic(ce, &st))1722return;1723if(ce_modified_check_fs(ce, &st)) {1724/* This is "racily clean"; smudge it. Note that this1725 * is a tricky code. At first glance, it may appear1726 * that it can break with this sequence:1727 *1728 * $ echo xyzzy >frotz1729 * $ git-update-index --add frotz1730 * $ : >frotz1731 * $ sleep 31732 * $ echo filfre >nitfol1733 * $ git-update-index --add nitfol1734 *1735 * but it does not. When the second update-index runs,1736 * it notices that the entry "frotz" has the same timestamp1737 * as index, and if we were to smudge it by resetting its1738 * size to zero here, then the object name recorded1739 * in index is the 6-byte file but the cached stat information1740 * becomes zero --- which would then match what we would1741 * obtain from the filesystem next time we stat("frotz").1742 *1743 * However, the second update-index, before calling1744 * this function, notices that the cached size is 61745 * bytes and what is on the filesystem is an empty1746 * file, and never calls us, so the cached size information1747 * for "frotz" stays 6 which does not match the filesystem.1748 */1749 ce->ce_stat_data.sd_size =0;1750}1751}17521753/* Copy miscellaneous fields but not the name */1754static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1755struct cache_entry *ce)1756{1757short flags;17581759 ondisk->ctime.sec =htonl(ce->ce_stat_data.sd_ctime.sec);1760 ondisk->mtime.sec =htonl(ce->ce_stat_data.sd_mtime.sec);1761 ondisk->ctime.nsec =htonl(ce->ce_stat_data.sd_ctime.nsec);1762 ondisk->mtime.nsec =htonl(ce->ce_stat_data.sd_mtime.nsec);1763 ondisk->dev =htonl(ce->ce_stat_data.sd_dev);1764 ondisk->ino =htonl(ce->ce_stat_data.sd_ino);1765 ondisk->mode =htonl(ce->ce_mode);1766 ondisk->uid =htonl(ce->ce_stat_data.sd_uid);1767 ondisk->gid =htonl(ce->ce_stat_data.sd_gid);1768 ondisk->size =htonl(ce->ce_stat_data.sd_size);1769hashcpy(ondisk->sha1, ce->sha1);17701771 flags = ce->ce_flags & ~CE_NAMEMASK;1772 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK :ce_namelen(ce));1773 ondisk->flags =htons(flags);1774if(ce->ce_flags & CE_EXTENDED) {1775struct ondisk_cache_entry_extended *ondisk2;1776 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1777 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1778return ondisk2->name;1779}1780else{1781return ondisk->name;1782}1783}17841785static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce,1786struct strbuf *previous_name)1787{1788int size;1789struct ondisk_cache_entry *ondisk;1790int saved_namelen = saved_namelen;/* compiler workaround */1791char*name;1792int result;17931794if(ce->ce_flags & CE_STRIP_NAME) {1795 saved_namelen =ce_namelen(ce);1796 ce->ce_namelen =0;1797}17981799if(!previous_name) {1800 size =ondisk_ce_size(ce);1801 ondisk =xcalloc(1, size);1802 name =copy_cache_entry_to_ondisk(ondisk, ce);1803memcpy(name, ce->name,ce_namelen(ce));1804}else{1805int common, to_remove, prefix_size;1806unsigned char to_remove_vi[16];1807for(common =0;1808(ce->name[common] &&1809 common < previous_name->len &&1810 ce->name[common] == previous_name->buf[common]);1811 common++)1812;/* still matching */1813 to_remove = previous_name->len - common;1814 prefix_size =encode_varint(to_remove, to_remove_vi);18151816if(ce->ce_flags & CE_EXTENDED)1817 size =offsetof(struct ondisk_cache_entry_extended, name);1818else1819 size =offsetof(struct ondisk_cache_entry, name);1820 size += prefix_size + (ce_namelen(ce) - common +1);18211822 ondisk =xcalloc(1, size);1823 name =copy_cache_entry_to_ondisk(ondisk, ce);1824memcpy(name, to_remove_vi, prefix_size);1825memcpy(name + prefix_size, ce->name + common,ce_namelen(ce) - common);18261827strbuf_splice(previous_name, common, to_remove,1828 ce->name + common,ce_namelen(ce) - common);1829}1830if(ce->ce_flags & CE_STRIP_NAME) {1831 ce->ce_namelen = saved_namelen;1832 ce->ce_flags &= ~CE_STRIP_NAME;1833}18341835 result =ce_write(c, fd, ondisk, size);1836free(ondisk);1837return result;1838}18391840static inthas_racy_timestamp(struct index_state *istate)1841{1842int entries = istate->cache_nr;1843int i;18441845for(i =0; i < entries; i++) {1846struct cache_entry *ce = istate->cache[i];1847if(is_racy_timestamp(istate, ce))1848return1;1849}1850return0;1851}18521853/*1854 * Opportunistically update the index but do not complain if we can't1855 */1856voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1857{1858if((istate->cache_changed ||has_racy_timestamp(istate)) &&1859write_locked_index(istate, lockfile, COMMIT_LOCK))1860rollback_lock_file(lockfile);1861}18621863static intdo_write_index(struct index_state *istate,int newfd)1864{1865 git_SHA_CTX c;1866struct cache_header hdr;1867int i, err, removed, extended, hdr_version;1868struct cache_entry **cache = istate->cache;1869int entries = istate->cache_nr;1870struct stat st;1871struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;18721873for(i = removed = extended =0; i < entries; i++) {1874if(cache[i]->ce_flags & CE_REMOVE)1875 removed++;18761877/* reduce extended entries if possible */1878 cache[i]->ce_flags &= ~CE_EXTENDED;1879if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1880 extended++;1881 cache[i]->ce_flags |= CE_EXTENDED;1882}1883}18841885if(!istate->version)1886 istate->version =get_index_format_default();18871888/* demote version 3 to version 2 when the latter suffices */1889if(istate->version ==3|| istate->version ==2)1890 istate->version = extended ?3:2;18911892 hdr_version = istate->version;18931894 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1895 hdr.hdr_version =htonl(hdr_version);1896 hdr.hdr_entries =htonl(entries - removed);18971898git_SHA1_Init(&c);1899if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1900return-1;19011902 previous_name = (hdr_version ==4) ? &previous_name_buf : NULL;1903for(i =0; i < entries; i++) {1904struct cache_entry *ce = cache[i];1905if(ce->ce_flags & CE_REMOVE)1906continue;1907if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1908ce_smudge_racily_clean_entry(ce);1909if(is_null_sha1(ce->sha1)) {1910static const char msg[] ="cache entry has null sha1:%s";1911static int allow = -1;19121913if(allow <0)1914 allow =git_env_bool("GIT_ALLOW_NULL_SHA1",0);1915if(allow)1916warning(msg, ce->name);1917else1918returnerror(msg, ce->name);1919}1920if(ce_write_entry(&c, newfd, ce, previous_name) <0)1921return-1;1922}1923strbuf_release(&previous_name_buf);19241925/* Write extension data here */1926if(istate->split_index) {1927struct strbuf sb = STRBUF_INIT;19281929 err =write_link_extension(&sb, istate) <0||1930write_index_ext_header(&c, newfd, CACHE_EXT_LINK,1931 sb.len) <0||1932ce_write(&c, newfd, sb.buf, sb.len) <0;1933strbuf_release(&sb);1934if(err)1935return-1;1936}1937if(istate->cache_tree) {1938struct strbuf sb = STRBUF_INIT;19391940cache_tree_write(&sb, istate->cache_tree);1941 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01942||ce_write(&c, newfd, sb.buf, sb.len) <0;1943strbuf_release(&sb);1944if(err)1945return-1;1946}1947if(istate->resolve_undo) {1948struct strbuf sb = STRBUF_INIT;19491950resolve_undo_write(&sb, istate->resolve_undo);1951 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,1952 sb.len) <01953||ce_write(&c, newfd, sb.buf, sb.len) <0;1954strbuf_release(&sb);1955if(err)1956return-1;1957}19581959if(ce_flush(&c, newfd, istate->sha1) ||fstat(newfd, &st))1960return-1;1961 istate->timestamp.sec = (unsigned int)st.st_mtime;1962 istate->timestamp.nsec =ST_MTIME_NSEC(st);1963return0;1964}19651966voidset_alternate_index_output(const char*name)1967{1968 alternate_index_output = name;1969}19701971static intcommit_locked_index(struct lock_file *lk)1972{1973if(alternate_index_output) {1974if(lk->fd >=0&&close_lock_file(lk))1975return-1;1976if(rename(lk->filename, alternate_index_output))1977return-1;1978 lk->filename[0] =0;1979return0;1980}else{1981returncommit_lock_file(lk);1982}1983}19841985static intdo_write_locked_index(struct index_state *istate,struct lock_file *lock,1986unsigned flags)1987{1988int ret =do_write_index(istate, lock->fd);1989if(ret)1990return ret;1991assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=1992(COMMIT_LOCK | CLOSE_LOCK));1993if(flags & COMMIT_LOCK)1994returncommit_locked_index(lock);1995else if(flags & CLOSE_LOCK)1996returnclose_lock_file(lock);1997else1998return ret;1999}20002001static intwrite_split_index(struct index_state *istate,2002struct lock_file *lock,2003unsigned flags)2004{2005int ret;2006prepare_to_write_split_index(istate);2007 ret =do_write_locked_index(istate, lock, flags);2008finish_writing_split_index(istate);2009return ret;2010}20112012intwrite_locked_index(struct index_state *istate,struct lock_file *lock,2013unsigned flags)2014{2015struct split_index *si = istate->split_index;20162017if(!si || (istate->cache_changed & ~EXTMASK)) {2018if(si)2019hashclr(si->base_sha1);2020returndo_write_locked_index(istate, lock, flags);2021}20222023returnwrite_split_index(istate, lock, flags);2024}20252026/*2027 * Read the index file that is potentially unmerged into given2028 * index_state, dropping any unmerged entries. Returns true if2029 * the index is unmerged. Callers who want to refuse to work2030 * from an unmerged state can call this and check its return value,2031 * instead of calling read_cache().2032 */2033intread_index_unmerged(struct index_state *istate)2034{2035int i;2036int unmerged =0;20372038read_index(istate);2039for(i =0; i < istate->cache_nr; i++) {2040struct cache_entry *ce = istate->cache[i];2041struct cache_entry *new_ce;2042int size, len;20432044if(!ce_stage(ce))2045continue;2046 unmerged =1;2047 len =ce_namelen(ce);2048 size =cache_entry_size(len);2049 new_ce =xcalloc(1, size);2050memcpy(new_ce->name, ce->name, len);2051 new_ce->ce_flags =create_ce_flags(0) | CE_CONFLICTED;2052 new_ce->ce_namelen = len;2053 new_ce->ce_mode = ce->ce_mode;2054if(add_index_entry(istate, new_ce,0))2055returnerror("%s: cannot drop to stage #0",2056 new_ce->name);2057 i =index_name_pos(istate, new_ce->name, len);2058}2059return unmerged;2060}20612062/*2063 * Returns 1 if the path is an "other" path with respect to2064 * the index; that is, the path is not mentioned in the index at all,2065 * either as a file, a directory with some files in the index,2066 * or as an unmerged entry.2067 *2068 * We helpfully remove a trailing "/" from directories so that2069 * the output of read_directory can be used as-is.2070 */2071intindex_name_is_other(const struct index_state *istate,const char*name,2072int namelen)2073{2074int pos;2075if(namelen && name[namelen -1] =='/')2076 namelen--;2077 pos =index_name_pos(istate, name, namelen);2078if(0<= pos)2079return0;/* exact match */2080 pos = -pos -1;2081if(pos < istate->cache_nr) {2082struct cache_entry *ce = istate->cache[pos];2083if(ce_namelen(ce) == namelen &&2084!memcmp(ce->name, name, namelen))2085return0;/* Yup, this one exists unmerged */2086}2087return1;2088}20892090void*read_blob_data_from_index(struct index_state *istate,const char*path,unsigned long*size)2091{2092int pos, len;2093unsigned long sz;2094enum object_type type;2095void*data;20962097 len =strlen(path);2098 pos =index_name_pos(istate, path, len);2099if(pos <0) {2100/*2101 * We might be in the middle of a merge, in which2102 * case we would read stage #2 (ours).2103 */2104int i;2105for(i = -pos -1;2106(pos <0&& i < istate->cache_nr &&2107!strcmp(istate->cache[i]->name, path));2108 i++)2109if(ce_stage(istate->cache[i]) ==2)2110 pos = i;2111}2112if(pos <0)2113return NULL;2114 data =read_sha1_file(istate->cache[pos]->sha1, &type, &sz);2115if(!data || type != OBJ_BLOB) {2116free(data);2117return NULL;2118}2119if(size)2120*size = sz;2121return data;2122}21232124voidstat_validity_clear(struct stat_validity *sv)2125{2126free(sv->sd);2127 sv->sd = NULL;2128}21292130intstat_validity_check(struct stat_validity *sv,const char*path)2131{2132struct stat st;21332134if(stat(path, &st) <0)2135return sv->sd == NULL;2136if(!sv->sd)2137return0;2138returnS_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);2139}21402141voidstat_validity_update(struct stat_validity *sv,int fd)2142{2143struct stat st;21442145if(fstat(fd, &st) <0|| !S_ISREG(st.st_mode))2146stat_validity_clear(sv);2147else{2148if(!sv->sd)2149 sv->sd =xcalloc(1,sizeof(struct stat_data));2150fill_stat_data(sv->sd, &st);2151}2152}