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 953cache_tree_invalidate_path(istate, ce->name); 954 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 955 956/* existing match? Just replace it. */ 957if(pos >=0) { 958if(!new_only) 959replace_index_entry(istate, pos, ce); 960return0; 961} 962 pos = -pos-1; 963 964/* 965 * Inserting a merged entry ("stage 0") into the index 966 * will always replace all non-merged entries.. 967 */ 968if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 969while(ce_same_name(istate->cache[pos], ce)) { 970 ok_to_add =1; 971if(!remove_index_entry_at(istate, pos)) 972break; 973} 974} 975 976if(!ok_to_add) 977return-1; 978if(!verify_path(ce->name)) 979returnerror("Invalid path '%s'", ce->name); 980 981if(!skip_df_check && 982check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 983if(!ok_to_replace) 984returnerror("'%s' appears as both a file and as a directory", 985 ce->name); 986 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 987 pos = -pos-1; 988} 989return pos +1; 990} 991 992intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 993{ 994int pos; 995 996if(option & ADD_CACHE_JUST_APPEND) 997 pos = istate->cache_nr; 998else{ 999int ret;1000 ret =add_index_entry_with_check(istate, ce, option);1001if(ret <=0)1002return ret;1003 pos = ret -1;1004}10051006/* Make sure the array is big enough .. */1007ALLOC_GROW(istate->cache, istate->cache_nr +1, istate->cache_alloc);10081009/* Add it in.. */1010 istate->cache_nr++;1011if(istate->cache_nr > pos +1)1012memmove(istate->cache + pos +1,1013 istate->cache + pos,1014(istate->cache_nr - pos -1) *sizeof(ce));1015set_index_entry(istate, pos, ce);1016 istate->cache_changed |= CE_ENTRY_ADDED;1017return0;1018}10191020/*1021 * "refresh" does not calculate a new sha1 file or bring the1022 * cache up-to-date for mode/content changes. But what it1023 * _does_ do is to "re-match" the stat information of a file1024 * with the cache, so that you can refresh the cache for a1025 * file that hasn't been changed but where the stat entry is1026 * out of date.1027 *1028 * For example, you'd want to do this after doing a "git-read-tree",1029 * to link up the stat cache details with the proper files.1030 */1031static struct cache_entry *refresh_cache_ent(struct index_state *istate,1032struct cache_entry *ce,1033unsigned int options,int*err,1034int*changed_ret)1035{1036struct stat st;1037struct cache_entry *updated;1038int changed, size;1039int refresh = options & CE_MATCH_REFRESH;1040int ignore_valid = options & CE_MATCH_IGNORE_VALID;1041int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;1042int ignore_missing = options & CE_MATCH_IGNORE_MISSING;10431044if(!refresh ||ce_uptodate(ce))1045return ce;10461047/*1048 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1049 * that the change to the work tree does not matter and told1050 * us not to worry.1051 */1052if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1053ce_mark_uptodate(ce);1054return ce;1055}1056if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1057ce_mark_uptodate(ce);1058return ce;1059}10601061if(lstat(ce->name, &st) <0) {1062if(ignore_missing && errno == ENOENT)1063return ce;1064if(err)1065*err = errno;1066return NULL;1067}10681069 changed =ie_match_stat(istate, ce, &st, options);1070if(changed_ret)1071*changed_ret = changed;1072if(!changed) {1073/*1074 * The path is unchanged. If we were told to ignore1075 * valid bit, then we did the actual stat check and1076 * found that the entry is unmodified. If the entry1077 * is not marked VALID, this is the place to mark it1078 * valid again, under "assume unchanged" mode.1079 */1080if(ignore_valid && assume_unchanged &&1081!(ce->ce_flags & CE_VALID))1082;/* mark this one VALID again */1083else{1084/*1085 * We do not mark the index itself "modified"1086 * because CE_UPTODATE flag is in-core only;1087 * we are not going to write this change out.1088 */1089if(!S_ISGITLINK(ce->ce_mode))1090ce_mark_uptodate(ce);1091return ce;1092}1093}10941095if(ie_modified(istate, ce, &st, options)) {1096if(err)1097*err = EINVAL;1098return NULL;1099}11001101 size =ce_size(ce);1102 updated =xmalloc(size);1103memcpy(updated, ce, size);1104fill_stat_cache_info(updated, &st);1105/*1106 * If ignore_valid is not set, we should leave CE_VALID bit1107 * alone. Otherwise, paths marked with --no-assume-unchanged1108 * (i.e. things to be edited) will reacquire CE_VALID bit1109 * automatically, which is not really what we want.1110 */1111if(!ignore_valid && assume_unchanged &&1112!(ce->ce_flags & CE_VALID))1113 updated->ce_flags &= ~CE_VALID;11141115/* istate->cache_changed is updated in the caller */1116return updated;1117}11181119static voidshow_file(const char* fmt,const char* name,int in_porcelain,1120int* first,const char*header_msg)1121{1122if(in_porcelain && *first && header_msg) {1123printf("%s\n", header_msg);1124*first =0;1125}1126printf(fmt, name);1127}11281129intrefresh_index(struct index_state *istate,unsigned int flags,1130const struct pathspec *pathspec,1131char*seen,const char*header_msg)1132{1133int i;1134int has_errors =0;1135int really = (flags & REFRESH_REALLY) !=0;1136int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1137int quiet = (flags & REFRESH_QUIET) !=0;1138int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1139int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1140int first =1;1141int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1142unsigned int options = (CE_MATCH_REFRESH |1143(really ? CE_MATCH_IGNORE_VALID :0) |1144(not_new ? CE_MATCH_IGNORE_MISSING :0));1145const char*modified_fmt;1146const char*deleted_fmt;1147const char*typechange_fmt;1148const char*added_fmt;1149const char*unmerged_fmt;11501151 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1152 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1153 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1154 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1155 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1156for(i =0; i < istate->cache_nr; i++) {1157struct cache_entry *ce, *new;1158int cache_errno =0;1159int changed =0;1160int filtered =0;11611162 ce = istate->cache[i];1163if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1164continue;11651166if(pathspec && !ce_path_match(ce, pathspec, seen))1167 filtered =1;11681169if(ce_stage(ce)) {1170while((i < istate->cache_nr) &&1171!strcmp(istate->cache[i]->name, ce->name))1172 i++;1173 i--;1174if(allow_unmerged)1175continue;1176if(!filtered)1177show_file(unmerged_fmt, ce->name, in_porcelain,1178&first, header_msg);1179 has_errors =1;1180continue;1181}11821183if(filtered)1184continue;11851186new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1187if(new== ce)1188continue;1189if(!new) {1190const char*fmt;11911192if(really && cache_errno == EINVAL) {1193/* If we are doing --really-refresh that1194 * means the index is not valid anymore.1195 */1196 ce->ce_flags &= ~CE_VALID;1197 ce->ce_flags |= CE_UPDATE_IN_BASE;1198 istate->cache_changed |= CE_ENTRY_CHANGED;1199}1200if(quiet)1201continue;12021203if(cache_errno == ENOENT)1204 fmt = deleted_fmt;1205else if(ce->ce_flags & CE_INTENT_TO_ADD)1206 fmt = added_fmt;/* must be before other checks */1207else if(changed & TYPE_CHANGED)1208 fmt = typechange_fmt;1209else1210 fmt = modified_fmt;1211show_file(fmt,1212 ce->name, in_porcelain, &first, header_msg);1213 has_errors =1;1214continue;1215}12161217replace_index_entry(istate, i,new);1218}1219return has_errors;1220}12211222static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,1223unsigned int options)1224{1225returnrefresh_cache_ent(&the_index, ce, options, NULL, NULL);1226}122712281229/*****************************************************************1230 * Index File I/O1231 *****************************************************************/12321233#define INDEX_FORMAT_DEFAULT 312341235static intindex_format_config(const char*var,const char*value,void*cb)1236{1237unsigned int*version = cb;1238if(!strcmp(var,"index.version")) {1239*version =git_config_int(var, value);1240return0;1241}1242return1;1243}12441245static unsigned intget_index_format_default(void)1246{1247char*envversion =getenv("GIT_INDEX_VERSION");1248char*endp;1249unsigned int version = INDEX_FORMAT_DEFAULT;12501251if(!envversion) {1252git_config(index_format_config, &version);1253if(version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1254warning(_("index.version set, but the value is invalid.\n"1255"Using version%i"), INDEX_FORMAT_DEFAULT);1256return INDEX_FORMAT_DEFAULT;1257}1258return version;1259}12601261 version =strtoul(envversion, &endp,10);1262if(*endp ||1263 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1264warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"1265"Using version%i"), INDEX_FORMAT_DEFAULT);1266 version = INDEX_FORMAT_DEFAULT;1267}1268return version;1269}12701271/*1272 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1273 * Again - this is just a (very strong in practice) heuristic that1274 * the inode hasn't changed.1275 *1276 * We save the fields in big-endian order to allow using the1277 * index file over NFS transparently.1278 */1279struct ondisk_cache_entry {1280struct cache_time ctime;1281struct cache_time mtime;1282uint32_t dev;1283uint32_t ino;1284uint32_t mode;1285uint32_t uid;1286uint32_t gid;1287uint32_t size;1288unsigned char sha1[20];1289uint16_t flags;1290char name[FLEX_ARRAY];/* more */1291};12921293/*1294 * This struct is used when CE_EXTENDED bit is 11295 * The struct must match ondisk_cache_entry exactly from1296 * ctime till flags1297 */1298struct ondisk_cache_entry_extended {1299struct cache_time ctime;1300struct cache_time mtime;1301uint32_t dev;1302uint32_t ino;1303uint32_t mode;1304uint32_t uid;1305uint32_t gid;1306uint32_t size;1307unsigned char sha1[20];1308uint16_t flags;1309uint16_t flags2;1310char name[FLEX_ARRAY];/* more */1311};13121313/* These are only used for v3 or lower */1314#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1315#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1316#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1317#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1318 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1319 ondisk_cache_entry_size(ce_namelen(ce)))13201321static intverify_hdr(struct cache_header *hdr,unsigned long size)1322{1323 git_SHA_CTX c;1324unsigned char sha1[20];1325int hdr_version;13261327if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1328returnerror("bad signature");1329 hdr_version =ntohl(hdr->hdr_version);1330if(hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)1331returnerror("bad index version%d", hdr_version);1332git_SHA1_Init(&c);1333git_SHA1_Update(&c, hdr, size -20);1334git_SHA1_Final(sha1, &c);1335if(hashcmp(sha1, (unsigned char*)hdr + size -20))1336returnerror("bad index file sha1 signature");1337return0;1338}13391340static intread_index_extension(struct index_state *istate,1341const char*ext,void*data,unsigned long sz)1342{1343switch(CACHE_EXT(ext)) {1344case CACHE_EXT_TREE:1345 istate->cache_tree =cache_tree_read(data, sz);1346break;1347case CACHE_EXT_RESOLVE_UNDO:1348 istate->resolve_undo =resolve_undo_read(data, sz);1349break;1350case CACHE_EXT_LINK:1351if(read_link_extension(istate, data, sz))1352return-1;1353break;1354default:1355if(*ext <'A'||'Z'< *ext)1356returnerror("index uses %.4s extension, which we do not understand",1357 ext);1358fprintf(stderr,"ignoring %.4s extension\n", ext);1359break;1360}1361return0;1362}13631364intread_index(struct index_state *istate)1365{1366returnread_index_from(istate,get_index_file());1367}13681369static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1370unsigned int flags,1371const char*name,1372size_t len)1373{1374struct cache_entry *ce =xmalloc(cache_entry_size(len));13751376 ce->ce_stat_data.sd_ctime.sec =get_be32(&ondisk->ctime.sec);1377 ce->ce_stat_data.sd_mtime.sec =get_be32(&ondisk->mtime.sec);1378 ce->ce_stat_data.sd_ctime.nsec =get_be32(&ondisk->ctime.nsec);1379 ce->ce_stat_data.sd_mtime.nsec =get_be32(&ondisk->mtime.nsec);1380 ce->ce_stat_data.sd_dev =get_be32(&ondisk->dev);1381 ce->ce_stat_data.sd_ino =get_be32(&ondisk->ino);1382 ce->ce_mode =get_be32(&ondisk->mode);1383 ce->ce_stat_data.sd_uid =get_be32(&ondisk->uid);1384 ce->ce_stat_data.sd_gid =get_be32(&ondisk->gid);1385 ce->ce_stat_data.sd_size =get_be32(&ondisk->size);1386 ce->ce_flags = flags & ~CE_NAMEMASK;1387 ce->ce_namelen = len;1388 ce->index =0;1389hashcpy(ce->sha1, ondisk->sha1);1390memcpy(ce->name, name, len);1391 ce->name[len] ='\0';1392return ce;1393}13941395/*1396 * Adjacent cache entries tend to share the leading paths, so it makes1397 * sense to only store the differences in later entries. In the v41398 * on-disk format of the index, each on-disk cache entry stores the1399 * number of bytes to be stripped from the end of the previous name,1400 * and the bytes to append to the result, to come up with its name.1401 */1402static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1403{1404const unsigned char*ep, *cp = (const unsigned char*)cp_;1405size_t len =decode_varint(&cp);14061407if(name->len < len)1408die("malformed name field in the index");1409strbuf_remove(name, name->len - len, len);1410for(ep = cp; *ep; ep++)1411;/* find the end */1412strbuf_add(name, cp, ep - cp);1413return(const char*)ep +1- cp_;1414}14151416static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1417unsigned long*ent_size,1418struct strbuf *previous_name)1419{1420struct cache_entry *ce;1421size_t len;1422const char*name;1423unsigned int flags;14241425/* On-disk flags are just 16 bits */1426 flags =get_be16(&ondisk->flags);1427 len = flags & CE_NAMEMASK;14281429if(flags & CE_EXTENDED) {1430struct ondisk_cache_entry_extended *ondisk2;1431int extended_flags;1432 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1433 extended_flags =get_be16(&ondisk2->flags2) <<16;1434/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1435if(extended_flags & ~CE_EXTENDED_FLAGS)1436die("Unknown index entry format%08x", extended_flags);1437 flags |= extended_flags;1438 name = ondisk2->name;1439}1440else1441 name = ondisk->name;14421443if(!previous_name) {1444/* v3 and earlier */1445if(len == CE_NAMEMASK)1446 len =strlen(name);1447 ce =cache_entry_from_ondisk(ondisk, flags, name, len);14481449*ent_size =ondisk_ce_size(ce);1450}else{1451unsigned long consumed;1452 consumed =expand_name_field(previous_name, name);1453 ce =cache_entry_from_ondisk(ondisk, flags,1454 previous_name->buf,1455 previous_name->len);14561457*ent_size = (name - ((char*)ondisk)) + consumed;1458}1459return ce;1460}14611462/* remember to discard_cache() before reading a different cache! */1463static intdo_read_index(struct index_state *istate,const char*path,1464int must_exist)1465{1466int fd, i;1467struct stat st;1468unsigned long src_offset;1469struct cache_header *hdr;1470void*mmap;1471size_t mmap_size;1472struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;14731474if(istate->initialized)1475return istate->cache_nr;14761477 istate->timestamp.sec =0;1478 istate->timestamp.nsec =0;1479 fd =open(path, O_RDONLY);1480if(fd <0) {1481if(!must_exist && errno == ENOENT)1482return0;1483die_errno("%s: index file open failed", path);1484}14851486if(fstat(fd, &st))1487die_errno("cannot stat the open index");14881489 mmap_size =xsize_t(st.st_size);1490if(mmap_size <sizeof(struct cache_header) +20)1491die("index file smaller than expected");14921493 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1494if(mmap == MAP_FAILED)1495die_errno("unable to map index file");1496close(fd);14971498 hdr = mmap;1499if(verify_hdr(hdr, mmap_size) <0)1500goto unmap;15011502hashcpy(istate->sha1, (const unsigned char*)hdr + mmap_size -20);1503 istate->version =ntohl(hdr->hdr_version);1504 istate->cache_nr =ntohl(hdr->hdr_entries);1505 istate->cache_alloc =alloc_nr(istate->cache_nr);1506 istate->cache =xcalloc(istate->cache_alloc,sizeof(*istate->cache));1507 istate->initialized =1;15081509if(istate->version ==4)1510 previous_name = &previous_name_buf;1511else1512 previous_name = NULL;15131514 src_offset =sizeof(*hdr);1515for(i =0; i < istate->cache_nr; i++) {1516struct ondisk_cache_entry *disk_ce;1517struct cache_entry *ce;1518unsigned long consumed;15191520 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1521 ce =create_from_disk(disk_ce, &consumed, previous_name);1522set_index_entry(istate, i, ce);15231524 src_offset += consumed;1525}1526strbuf_release(&previous_name_buf);1527 istate->timestamp.sec = st.st_mtime;1528 istate->timestamp.nsec =ST_MTIME_NSEC(st);15291530while(src_offset <= mmap_size -20-8) {1531/* After an array of active_nr index entries,1532 * there can be arbitrary number of extended1533 * sections, each of which is prefixed with1534 * extension name (4-byte) and section length1535 * in 4-byte network byte order.1536 */1537uint32_t extsize;1538memcpy(&extsize, (char*)mmap + src_offset +4,4);1539 extsize =ntohl(extsize);1540if(read_index_extension(istate,1541(const char*) mmap + src_offset,1542(char*) mmap + src_offset +8,1543 extsize) <0)1544goto unmap;1545 src_offset +=8;1546 src_offset += extsize;1547}1548munmap(mmap, mmap_size);1549return istate->cache_nr;15501551unmap:1552munmap(mmap, mmap_size);1553die("index file corrupt");1554}15551556intread_index_from(struct index_state *istate,const char*path)1557{1558struct split_index *split_index;1559int ret;15601561/* istate->initialized covers both .git/index and .git/sharedindex.xxx */1562if(istate->initialized)1563return istate->cache_nr;15641565 ret =do_read_index(istate, path,0);1566 split_index = istate->split_index;1567if(!split_index)1568return ret;15691570if(is_null_sha1(split_index->base_sha1))1571return ret;15721573if(split_index->base)1574discard_index(split_index->base);1575else1576 split_index->base =xcalloc(1,sizeof(*split_index->base));1577 ret =do_read_index(split_index->base,1578git_path("sharedindex.%s",1579sha1_to_hex(split_index->base_sha1)),1);1580if(hashcmp(split_index->base_sha1, split_index->base->sha1))1581die("broken index, expect%sin%s, got%s",1582sha1_to_hex(split_index->base_sha1),1583git_path("sharedindex.%s",1584sha1_to_hex(split_index->base_sha1)),1585sha1_to_hex(split_index->base->sha1));1586merge_base_index(istate);1587return ret;1588}15891590intis_index_unborn(struct index_state *istate)1591{1592return(!istate->cache_nr && !istate->timestamp.sec);1593}15941595intdiscard_index(struct index_state *istate)1596{1597int i;15981599for(i =0; i < istate->cache_nr; i++) {1600if(istate->cache[i]->index &&1601 istate->split_index &&1602 istate->split_index->base &&1603 istate->cache[i]->index <= istate->split_index->base->cache_nr &&1604 istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index -1])1605continue;1606free(istate->cache[i]);1607}1608resolve_undo_clear_index(istate);1609 istate->cache_nr =0;1610 istate->cache_changed =0;1611 istate->timestamp.sec =0;1612 istate->timestamp.nsec =0;1613free_name_hash(istate);1614cache_tree_free(&(istate->cache_tree));1615 istate->initialized =0;1616free(istate->cache);1617 istate->cache = NULL;1618 istate->cache_alloc =0;1619discard_split_index(istate);1620return0;1621}16221623intunmerged_index(const struct index_state *istate)1624{1625int i;1626for(i =0; i < istate->cache_nr; i++) {1627if(ce_stage(istate->cache[i]))1628return1;1629}1630return0;1631}16321633#define WRITE_BUFFER_SIZE 81921634static unsigned char write_buffer[WRITE_BUFFER_SIZE];1635static unsigned long write_buffer_len;16361637static intce_write_flush(git_SHA_CTX *context,int fd)1638{1639unsigned int buffered = write_buffer_len;1640if(buffered) {1641git_SHA1_Update(context, write_buffer, buffered);1642if(write_in_full(fd, write_buffer, buffered) != buffered)1643return-1;1644 write_buffer_len =0;1645}1646return0;1647}16481649static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1650{1651while(len) {1652unsigned int buffered = write_buffer_len;1653unsigned int partial = WRITE_BUFFER_SIZE - buffered;1654if(partial > len)1655 partial = len;1656memcpy(write_buffer + buffered, data, partial);1657 buffered += partial;1658if(buffered == WRITE_BUFFER_SIZE) {1659 write_buffer_len = buffered;1660if(ce_write_flush(context, fd))1661return-1;1662 buffered =0;1663}1664 write_buffer_len = buffered;1665 len -= partial;1666 data = (char*) data + partial;1667}1668return0;1669}16701671static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1672unsigned int ext,unsigned int sz)1673{1674 ext =htonl(ext);1675 sz =htonl(sz);1676return((ce_write(context, fd, &ext,4) <0) ||1677(ce_write(context, fd, &sz,4) <0)) ? -1:0;1678}16791680static intce_flush(git_SHA_CTX *context,int fd,unsigned char*sha1)1681{1682unsigned int left = write_buffer_len;16831684if(left) {1685 write_buffer_len =0;1686git_SHA1_Update(context, write_buffer, left);1687}16881689/* Flush first if not enough space for SHA1 signature */1690if(left +20> WRITE_BUFFER_SIZE) {1691if(write_in_full(fd, write_buffer, left) != left)1692return-1;1693 left =0;1694}16951696/* Append the SHA1 signature at the end */1697git_SHA1_Final(write_buffer + left, context);1698hashcpy(sha1, write_buffer + left);1699 left +=20;1700return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1701}17021703static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1704{1705/*1706 * The only thing we care about in this function is to smudge the1707 * falsely clean entry due to touch-update-touch race, so we leave1708 * everything else as they are. We are called for entries whose1709 * ce_stat_data.sd_mtime match the index file mtime.1710 *1711 * Note that this actually does not do much for gitlinks, for1712 * which ce_match_stat_basic() always goes to the actual1713 * contents. The caller checks with is_racy_timestamp() which1714 * always says "no" for gitlinks, so we are not called for them ;-)1715 */1716struct stat st;17171718if(lstat(ce->name, &st) <0)1719return;1720if(ce_match_stat_basic(ce, &st))1721return;1722if(ce_modified_check_fs(ce, &st)) {1723/* This is "racily clean"; smudge it. Note that this1724 * is a tricky code. At first glance, it may appear1725 * that it can break with this sequence:1726 *1727 * $ echo xyzzy >frotz1728 * $ git-update-index --add frotz1729 * $ : >frotz1730 * $ sleep 31731 * $ echo filfre >nitfol1732 * $ git-update-index --add nitfol1733 *1734 * but it does not. When the second update-index runs,1735 * it notices that the entry "frotz" has the same timestamp1736 * as index, and if we were to smudge it by resetting its1737 * size to zero here, then the object name recorded1738 * in index is the 6-byte file but the cached stat information1739 * becomes zero --- which would then match what we would1740 * obtain from the filesystem next time we stat("frotz").1741 *1742 * However, the second update-index, before calling1743 * this function, notices that the cached size is 61744 * bytes and what is on the filesystem is an empty1745 * file, and never calls us, so the cached size information1746 * for "frotz" stays 6 which does not match the filesystem.1747 */1748 ce->ce_stat_data.sd_size =0;1749}1750}17511752/* Copy miscellaneous fields but not the name */1753static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1754struct cache_entry *ce)1755{1756short flags;17571758 ondisk->ctime.sec =htonl(ce->ce_stat_data.sd_ctime.sec);1759 ondisk->mtime.sec =htonl(ce->ce_stat_data.sd_mtime.sec);1760 ondisk->ctime.nsec =htonl(ce->ce_stat_data.sd_ctime.nsec);1761 ondisk->mtime.nsec =htonl(ce->ce_stat_data.sd_mtime.nsec);1762 ondisk->dev =htonl(ce->ce_stat_data.sd_dev);1763 ondisk->ino =htonl(ce->ce_stat_data.sd_ino);1764 ondisk->mode =htonl(ce->ce_mode);1765 ondisk->uid =htonl(ce->ce_stat_data.sd_uid);1766 ondisk->gid =htonl(ce->ce_stat_data.sd_gid);1767 ondisk->size =htonl(ce->ce_stat_data.sd_size);1768hashcpy(ondisk->sha1, ce->sha1);17691770 flags = ce->ce_flags & ~CE_NAMEMASK;1771 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK :ce_namelen(ce));1772 ondisk->flags =htons(flags);1773if(ce->ce_flags & CE_EXTENDED) {1774struct ondisk_cache_entry_extended *ondisk2;1775 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1776 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1777return ondisk2->name;1778}1779else{1780return ondisk->name;1781}1782}17831784static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce,1785struct strbuf *previous_name)1786{1787int size;1788struct ondisk_cache_entry *ondisk;1789char*name;1790int result;17911792if(!previous_name) {1793 size =ondisk_ce_size(ce);1794 ondisk =xcalloc(1, size);1795 name =copy_cache_entry_to_ondisk(ondisk, ce);1796memcpy(name, ce->name,ce_namelen(ce));1797}else{1798int common, to_remove, prefix_size;1799unsigned char to_remove_vi[16];1800for(common =0;1801(ce->name[common] &&1802 common < previous_name->len &&1803 ce->name[common] == previous_name->buf[common]);1804 common++)1805;/* still matching */1806 to_remove = previous_name->len - common;1807 prefix_size =encode_varint(to_remove, to_remove_vi);18081809if(ce->ce_flags & CE_EXTENDED)1810 size =offsetof(struct ondisk_cache_entry_extended, name);1811else1812 size =offsetof(struct ondisk_cache_entry, name);1813 size += prefix_size + (ce_namelen(ce) - common +1);18141815 ondisk =xcalloc(1, size);1816 name =copy_cache_entry_to_ondisk(ondisk, ce);1817memcpy(name, to_remove_vi, prefix_size);1818memcpy(name + prefix_size, ce->name + common,ce_namelen(ce) - common);18191820strbuf_splice(previous_name, common, to_remove,1821 ce->name + common,ce_namelen(ce) - common);1822}18231824 result =ce_write(c, fd, ondisk, size);1825free(ondisk);1826return result;1827}18281829static inthas_racy_timestamp(struct index_state *istate)1830{1831int entries = istate->cache_nr;1832int i;18331834for(i =0; i < entries; i++) {1835struct cache_entry *ce = istate->cache[i];1836if(is_racy_timestamp(istate, ce))1837return1;1838}1839return0;1840}18411842/*1843 * Opportunistically update the index but do not complain if we can't1844 */1845voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1846{1847if((istate->cache_changed ||has_racy_timestamp(istate)) &&1848write_locked_index(istate, lockfile, COMMIT_LOCK))1849rollback_lock_file(lockfile);1850}18511852static intdo_write_index(struct index_state *istate,int newfd)1853{1854 git_SHA_CTX c;1855struct cache_header hdr;1856int i, err, removed, extended, hdr_version;1857struct cache_entry **cache = istate->cache;1858int entries = istate->cache_nr;1859struct stat st;1860struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;18611862for(i = removed = extended =0; i < entries; i++) {1863if(cache[i]->ce_flags & CE_REMOVE)1864 removed++;18651866/* reduce extended entries if possible */1867 cache[i]->ce_flags &= ~CE_EXTENDED;1868if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1869 extended++;1870 cache[i]->ce_flags |= CE_EXTENDED;1871}1872}18731874if(!istate->version)1875 istate->version =get_index_format_default();18761877/* demote version 3 to version 2 when the latter suffices */1878if(istate->version ==3|| istate->version ==2)1879 istate->version = extended ?3:2;18801881 hdr_version = istate->version;18821883 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1884 hdr.hdr_version =htonl(hdr_version);1885 hdr.hdr_entries =htonl(entries - removed);18861887git_SHA1_Init(&c);1888if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1889return-1;18901891 previous_name = (hdr_version ==4) ? &previous_name_buf : NULL;1892for(i =0; i < entries; i++) {1893struct cache_entry *ce = cache[i];1894if(ce->ce_flags & CE_REMOVE)1895continue;1896if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1897ce_smudge_racily_clean_entry(ce);1898if(is_null_sha1(ce->sha1)) {1899static const char msg[] ="cache entry has null sha1:%s";1900static int allow = -1;19011902if(allow <0)1903 allow =git_env_bool("GIT_ALLOW_NULL_SHA1",0);1904if(allow)1905warning(msg, ce->name);1906else1907returnerror(msg, ce->name);1908}1909if(ce_write_entry(&c, newfd, ce, previous_name) <0)1910return-1;1911}1912strbuf_release(&previous_name_buf);19131914/* Write extension data here */1915if(istate->split_index) {1916struct strbuf sb = STRBUF_INIT;19171918 err =write_link_extension(&sb, istate) <0||1919write_index_ext_header(&c, newfd, CACHE_EXT_LINK,1920 sb.len) <0||1921ce_write(&c, newfd, sb.buf, sb.len) <0;1922strbuf_release(&sb);1923if(err)1924return-1;1925}1926if(istate->cache_tree) {1927struct strbuf sb = STRBUF_INIT;19281929cache_tree_write(&sb, istate->cache_tree);1930 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01931||ce_write(&c, newfd, sb.buf, sb.len) <0;1932strbuf_release(&sb);1933if(err)1934return-1;1935}1936if(istate->resolve_undo) {1937struct strbuf sb = STRBUF_INIT;19381939resolve_undo_write(&sb, istate->resolve_undo);1940 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,1941 sb.len) <01942||ce_write(&c, newfd, sb.buf, sb.len) <0;1943strbuf_release(&sb);1944if(err)1945return-1;1946}19471948if(ce_flush(&c, newfd, istate->sha1) ||fstat(newfd, &st))1949return-1;1950 istate->timestamp.sec = (unsigned int)st.st_mtime;1951 istate->timestamp.nsec =ST_MTIME_NSEC(st);1952return0;1953}19541955voidset_alternate_index_output(const char*name)1956{1957 alternate_index_output = name;1958}19591960static intcommit_locked_index(struct lock_file *lk)1961{1962if(alternate_index_output) {1963if(lk->fd >=0&&close_lock_file(lk))1964return-1;1965if(rename(lk->filename, alternate_index_output))1966return-1;1967 lk->filename[0] =0;1968return0;1969}else{1970returncommit_lock_file(lk);1971}1972}19731974static intdo_write_locked_index(struct index_state *istate,struct lock_file *lock,1975unsigned flags)1976{1977int ret =do_write_index(istate, lock->fd);1978if(ret)1979return ret;1980assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=1981(COMMIT_LOCK | CLOSE_LOCK));1982if(flags & COMMIT_LOCK)1983returncommit_locked_index(lock);1984else if(flags & CLOSE_LOCK)1985returnclose_lock_file(lock);1986else1987return ret;1988}19891990static intwrite_split_index(struct index_state *istate,1991struct lock_file *lock,1992unsigned flags)1993{1994int ret;1995prepare_to_write_split_index(istate);1996 ret =do_write_locked_index(istate, lock, flags);1997finish_writing_split_index(istate);1998return ret;1999}20002001intwrite_locked_index(struct index_state *istate,struct lock_file *lock,2002unsigned flags)2003{2004struct split_index *si = istate->split_index;20052006if(!si || (istate->cache_changed & ~EXTMASK)) {2007if(si)2008hashclr(si->base_sha1);2009returndo_write_locked_index(istate, lock, flags);2010}20112012returnwrite_split_index(istate, lock, flags);2013}20142015/*2016 * Read the index file that is potentially unmerged into given2017 * index_state, dropping any unmerged entries. Returns true if2018 * the index is unmerged. Callers who want to refuse to work2019 * from an unmerged state can call this and check its return value,2020 * instead of calling read_cache().2021 */2022intread_index_unmerged(struct index_state *istate)2023{2024int i;2025int unmerged =0;20262027read_index(istate);2028for(i =0; i < istate->cache_nr; i++) {2029struct cache_entry *ce = istate->cache[i];2030struct cache_entry *new_ce;2031int size, len;20322033if(!ce_stage(ce))2034continue;2035 unmerged =1;2036 len =ce_namelen(ce);2037 size =cache_entry_size(len);2038 new_ce =xcalloc(1, size);2039memcpy(new_ce->name, ce->name, len);2040 new_ce->ce_flags =create_ce_flags(0) | CE_CONFLICTED;2041 new_ce->ce_namelen = len;2042 new_ce->ce_mode = ce->ce_mode;2043if(add_index_entry(istate, new_ce,0))2044returnerror("%s: cannot drop to stage #0",2045 new_ce->name);2046 i =index_name_pos(istate, new_ce->name, len);2047}2048return unmerged;2049}20502051/*2052 * Returns 1 if the path is an "other" path with respect to2053 * the index; that is, the path is not mentioned in the index at all,2054 * either as a file, a directory with some files in the index,2055 * or as an unmerged entry.2056 *2057 * We helpfully remove a trailing "/" from directories so that2058 * the output of read_directory can be used as-is.2059 */2060intindex_name_is_other(const struct index_state *istate,const char*name,2061int namelen)2062{2063int pos;2064if(namelen && name[namelen -1] =='/')2065 namelen--;2066 pos =index_name_pos(istate, name, namelen);2067if(0<= pos)2068return0;/* exact match */2069 pos = -pos -1;2070if(pos < istate->cache_nr) {2071struct cache_entry *ce = istate->cache[pos];2072if(ce_namelen(ce) == namelen &&2073!memcmp(ce->name, name, namelen))2074return0;/* Yup, this one exists unmerged */2075}2076return1;2077}20782079void*read_blob_data_from_index(struct index_state *istate,const char*path,unsigned long*size)2080{2081int pos, len;2082unsigned long sz;2083enum object_type type;2084void*data;20852086 len =strlen(path);2087 pos =index_name_pos(istate, path, len);2088if(pos <0) {2089/*2090 * We might be in the middle of a merge, in which2091 * case we would read stage #2 (ours).2092 */2093int i;2094for(i = -pos -1;2095(pos <0&& i < istate->cache_nr &&2096!strcmp(istate->cache[i]->name, path));2097 i++)2098if(ce_stage(istate->cache[i]) ==2)2099 pos = i;2100}2101if(pos <0)2102return NULL;2103 data =read_sha1_file(istate->cache[pos]->sha1, &type, &sz);2104if(!data || type != OBJ_BLOB) {2105free(data);2106return NULL;2107}2108if(size)2109*size = sz;2110return data;2111}21122113voidstat_validity_clear(struct stat_validity *sv)2114{2115free(sv->sd);2116 sv->sd = NULL;2117}21182119intstat_validity_check(struct stat_validity *sv,const char*path)2120{2121struct stat st;21222123if(stat(path, &st) <0)2124return sv->sd == NULL;2125if(!sv->sd)2126return0;2127returnS_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);2128}21292130voidstat_validity_update(struct stat_validity *sv,int fd)2131{2132struct stat st;21332134if(fstat(fd, &st) <0|| !S_ISREG(st.st_mode))2135stat_validity_clear(sv);2136else{2137if(!sv->sd)2138 sv->sd =xcalloc(1,sizeof(struct stat_data));2139fill_stat_data(sv->sd, &st);2140}2141}