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) 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 57remove_name_hash(istate, old); 58free(old); 59set_index_entry(istate, nr, ce); 60 istate->cache_changed |= CE_ENTRY_CHANGED; 61} 62 63voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 64{ 65struct cache_entry *old = istate->cache[nr], *new; 66int namelen =strlen(new_name); 67 68new=xmalloc(cache_entry_size(namelen)); 69copy_cache_entry(new, old); 70new->ce_flags &= ~CE_HASHED; 71new->ce_namelen = namelen; 72new->index =0; 73memcpy(new->name, new_name, namelen +1); 74 75cache_tree_invalidate_path(istate, old->name); 76remove_index_entry_at(istate, nr); 77add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 78} 79 80voidfill_stat_data(struct stat_data *sd,struct stat *st) 81{ 82 sd->sd_ctime.sec = (unsigned int)st->st_ctime; 83 sd->sd_mtime.sec = (unsigned int)st->st_mtime; 84 sd->sd_ctime.nsec =ST_CTIME_NSEC(*st); 85 sd->sd_mtime.nsec =ST_MTIME_NSEC(*st); 86 sd->sd_dev = st->st_dev; 87 sd->sd_ino = st->st_ino; 88 sd->sd_uid = st->st_uid; 89 sd->sd_gid = st->st_gid; 90 sd->sd_size = st->st_size; 91} 92 93intmatch_stat_data(const struct stat_data *sd,struct stat *st) 94{ 95int changed =0; 96 97if(sd->sd_mtime.sec != (unsigned int)st->st_mtime) 98 changed |= MTIME_CHANGED; 99if(trust_ctime && check_stat && 100 sd->sd_ctime.sec != (unsigned int)st->st_ctime) 101 changed |= CTIME_CHANGED; 102 103#ifdef USE_NSEC 104if(check_stat && sd->sd_mtime.nsec !=ST_MTIME_NSEC(*st)) 105 changed |= MTIME_CHANGED; 106if(trust_ctime && check_stat && 107 sd->sd_ctime.nsec !=ST_CTIME_NSEC(*st)) 108 changed |= CTIME_CHANGED; 109#endif 110 111if(check_stat) { 112if(sd->sd_uid != (unsigned int) st->st_uid || 113 sd->sd_gid != (unsigned int) st->st_gid) 114 changed |= OWNER_CHANGED; 115if(sd->sd_ino != (unsigned int) st->st_ino) 116 changed |= INODE_CHANGED; 117} 118 119#ifdef USE_STDEV 120/* 121 * st_dev breaks on network filesystems where different 122 * clients will have different views of what "device" 123 * the filesystem is on 124 */ 125if(check_stat && sd->sd_dev != (unsigned int) st->st_dev) 126 changed |= INODE_CHANGED; 127#endif 128 129if(sd->sd_size != (unsigned int) st->st_size) 130 changed |= DATA_CHANGED; 131 132return changed; 133} 134 135/* 136 * This only updates the "non-critical" parts of the directory 137 * cache, ie the parts that aren't tracked by GIT, and only used 138 * to validate the cache. 139 */ 140voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 141{ 142fill_stat_data(&ce->ce_stat_data, st); 143 144if(assume_unchanged) 145 ce->ce_flags |= CE_VALID; 146 147if(S_ISREG(st->st_mode)) 148ce_mark_uptodate(ce); 149} 150 151static intce_compare_data(const struct cache_entry *ce,struct stat *st) 152{ 153int match = -1; 154int fd =open(ce->name, O_RDONLY); 155 156if(fd >=0) { 157unsigned char sha1[20]; 158if(!index_fd(sha1, fd, st, OBJ_BLOB, ce->name,0)) 159 match =hashcmp(sha1, ce->sha1); 160/* index_fd() closed the file descriptor already */ 161} 162return match; 163} 164 165static intce_compare_link(const struct cache_entry *ce,size_t expected_size) 166{ 167int match = -1; 168void*buffer; 169unsigned long size; 170enum object_type type; 171struct strbuf sb = STRBUF_INIT; 172 173if(strbuf_readlink(&sb, ce->name, expected_size)) 174return-1; 175 176 buffer =read_sha1_file(ce->sha1, &type, &size); 177if(buffer) { 178if(size == sb.len) 179 match =memcmp(buffer, sb.buf, size); 180free(buffer); 181} 182strbuf_release(&sb); 183return match; 184} 185 186static intce_compare_gitlink(const struct cache_entry *ce) 187{ 188unsigned char sha1[20]; 189 190/* 191 * We don't actually require that the .git directory 192 * under GITLINK directory be a valid git directory. It 193 * might even be missing (in case nobody populated that 194 * sub-project). 195 * 196 * If so, we consider it always to match. 197 */ 198if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 199return0; 200returnhashcmp(sha1, ce->sha1); 201} 202 203static intce_modified_check_fs(const struct cache_entry *ce,struct stat *st) 204{ 205switch(st->st_mode & S_IFMT) { 206case S_IFREG: 207if(ce_compare_data(ce, st)) 208return DATA_CHANGED; 209break; 210case S_IFLNK: 211if(ce_compare_link(ce,xsize_t(st->st_size))) 212return DATA_CHANGED; 213break; 214case S_IFDIR: 215if(S_ISGITLINK(ce->ce_mode)) 216returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 217default: 218return TYPE_CHANGED; 219} 220return0; 221} 222 223static intce_match_stat_basic(const struct cache_entry *ce,struct stat *st) 224{ 225unsigned int changed =0; 226 227if(ce->ce_flags & CE_REMOVE) 228return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 229 230switch(ce->ce_mode & S_IFMT) { 231case S_IFREG: 232 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 233/* We consider only the owner x bit to be relevant for 234 * "mode changes" 235 */ 236if(trust_executable_bit && 237(0100& (ce->ce_mode ^ st->st_mode))) 238 changed |= MODE_CHANGED; 239break; 240case S_IFLNK: 241if(!S_ISLNK(st->st_mode) && 242(has_symlinks || !S_ISREG(st->st_mode))) 243 changed |= TYPE_CHANGED; 244break; 245case S_IFGITLINK: 246/* We ignore most of the st_xxx fields for gitlinks */ 247if(!S_ISDIR(st->st_mode)) 248 changed |= TYPE_CHANGED; 249else if(ce_compare_gitlink(ce)) 250 changed |= DATA_CHANGED; 251return changed; 252default: 253die("internal error: ce_mode is%o", ce->ce_mode); 254} 255 256 changed |=match_stat_data(&ce->ce_stat_data, st); 257 258/* Racily smudged entry? */ 259if(!ce->ce_stat_data.sd_size) { 260if(!is_empty_blob_sha1(ce->sha1)) 261 changed |= DATA_CHANGED; 262} 263 264return changed; 265} 266 267static intis_racy_timestamp(const struct index_state *istate, 268const struct cache_entry *ce) 269{ 270return(!S_ISGITLINK(ce->ce_mode) && 271 istate->timestamp.sec && 272#ifdef USE_NSEC 273/* nanosecond timestamped files can also be racy! */ 274(istate->timestamp.sec < ce->ce_stat_data.sd_mtime.sec || 275(istate->timestamp.sec == ce->ce_stat_data.sd_mtime.sec && 276 istate->timestamp.nsec <= ce->ce_stat_data.sd_mtime.nsec)) 277#else 278 istate->timestamp.sec <= ce->ce_stat_data.sd_mtime.sec 279#endif 280); 281} 282 283intie_match_stat(const struct index_state *istate, 284const struct cache_entry *ce,struct stat *st, 285unsigned int options) 286{ 287unsigned int changed; 288int ignore_valid = options & CE_MATCH_IGNORE_VALID; 289int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; 290int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 291 292/* 293 * If it's marked as always valid in the index, it's 294 * valid whatever the checked-out copy says. 295 * 296 * skip-worktree has the same effect with higher precedence 297 */ 298if(!ignore_skip_worktree &&ce_skip_worktree(ce)) 299return0; 300if(!ignore_valid && (ce->ce_flags & CE_VALID)) 301return0; 302 303/* 304 * Intent-to-add entries have not been added, so the index entry 305 * by definition never matches what is in the work tree until it 306 * actually gets added. 307 */ 308if(ce->ce_flags & CE_INTENT_TO_ADD) 309return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; 310 311 changed =ce_match_stat_basic(ce, st); 312 313/* 314 * Within 1 second of this sequence: 315 * echo xyzzy >file && git-update-index --add file 316 * running this command: 317 * echo frotz >file 318 * would give a falsely clean cache entry. The mtime and 319 * length match the cache, and other stat fields do not change. 320 * 321 * We could detect this at update-index time (the cache entry 322 * being registered/updated records the same time as "now") 323 * and delay the return from git-update-index, but that would 324 * effectively mean we can make at most one commit per second, 325 * which is not acceptable. Instead, we check cache entries 326 * whose mtime are the same as the index file timestamp more 327 * carefully than others. 328 */ 329if(!changed &&is_racy_timestamp(istate, ce)) { 330if(assume_racy_is_modified) 331 changed |= DATA_CHANGED; 332else 333 changed |=ce_modified_check_fs(ce, st); 334} 335 336return changed; 337} 338 339intie_modified(const struct index_state *istate, 340const struct cache_entry *ce, 341struct stat *st,unsigned int options) 342{ 343int changed, changed_fs; 344 345 changed =ie_match_stat(istate, ce, st, options); 346if(!changed) 347return0; 348/* 349 * If the mode or type has changed, there's no point in trying 350 * to refresh the entry - it's not going to match 351 */ 352if(changed & (MODE_CHANGED | TYPE_CHANGED)) 353return changed; 354 355/* 356 * Immediately after read-tree or update-index --cacheinfo, 357 * the length field is zero, as we have never even read the 358 * lstat(2) information once, and we cannot trust DATA_CHANGED 359 * returned by ie_match_stat() which in turn was returned by 360 * ce_match_stat_basic() to signal that the filesize of the 361 * blob changed. We have to actually go to the filesystem to 362 * see if the contents match, and if so, should answer "unchanged". 363 * 364 * The logic does not apply to gitlinks, as ce_match_stat_basic() 365 * already has checked the actual HEAD from the filesystem in the 366 * subproject. If ie_match_stat() already said it is different, 367 * then we know it is. 368 */ 369if((changed & DATA_CHANGED) && 370(S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size !=0)) 371return changed; 372 373 changed_fs =ce_modified_check_fs(ce, st); 374if(changed_fs) 375return changed | changed_fs; 376return0; 377} 378 379intbase_name_compare(const char*name1,int len1,int mode1, 380const char*name2,int len2,int mode2) 381{ 382unsigned char c1, c2; 383int len = len1 < len2 ? len1 : len2; 384int cmp; 385 386 cmp =memcmp(name1, name2, len); 387if(cmp) 388return cmp; 389 c1 = name1[len]; 390 c2 = name2[len]; 391if(!c1 &&S_ISDIR(mode1)) 392 c1 ='/'; 393if(!c2 &&S_ISDIR(mode2)) 394 c2 ='/'; 395return(c1 < c2) ? -1: (c1 > c2) ?1:0; 396} 397 398/* 399 * df_name_compare() is identical to base_name_compare(), except it 400 * compares conflicting directory/file entries as equal. Note that 401 * while a directory name compares as equal to a regular file, they 402 * then individually compare _differently_ to a filename that has 403 * a dot after the basename (because '\0' < '.' < '/'). 404 * 405 * This is used by routines that want to traverse the git namespace 406 * but then handle conflicting entries together when possible. 407 */ 408intdf_name_compare(const char*name1,int len1,int mode1, 409const char*name2,int len2,int mode2) 410{ 411int len = len1 < len2 ? len1 : len2, cmp; 412unsigned char c1, c2; 413 414 cmp =memcmp(name1, name2, len); 415if(cmp) 416return cmp; 417/* Directories and files compare equal (same length, same name) */ 418if(len1 == len2) 419return0; 420 c1 = name1[len]; 421if(!c1 &&S_ISDIR(mode1)) 422 c1 ='/'; 423 c2 = name2[len]; 424if(!c2 &&S_ISDIR(mode2)) 425 c2 ='/'; 426if(c1 =='/'&& !c2) 427return0; 428if(c2 =='/'&& !c1) 429return0; 430return c1 - c2; 431} 432 433intcache_name_stage_compare(const char*name1,int len1,int stage1,const char*name2,int len2,int stage2) 434{ 435int len = len1 < len2 ? len1 : len2; 436int cmp; 437 438 cmp =memcmp(name1, name2, len); 439if(cmp) 440return cmp; 441if(len1 < len2) 442return-1; 443if(len1 > len2) 444return1; 445 446if(stage1 < stage2) 447return-1; 448if(stage1 > stage2) 449return1; 450return0; 451} 452 453intcache_name_compare(const char*name1,int len1,const char*name2,int len2) 454{ 455returncache_name_stage_compare(name1, len1,0, name2, len2,0); 456} 457 458static intindex_name_stage_pos(const struct index_state *istate,const char*name,int namelen,int stage) 459{ 460int first, last; 461 462 first =0; 463 last = istate->cache_nr; 464while(last > first) { 465int next = (last + first) >>1; 466struct cache_entry *ce = istate->cache[next]; 467int cmp =cache_name_stage_compare(name, namelen, stage, ce->name,ce_namelen(ce),ce_stage(ce)); 468if(!cmp) 469return next; 470if(cmp <0) { 471 last = next; 472continue; 473} 474 first = next+1; 475} 476return-first-1; 477} 478 479intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 480{ 481returnindex_name_stage_pos(istate, name, namelen,0); 482} 483 484/* Remove entry, return true if there are more entries to go.. */ 485intremove_index_entry_at(struct index_state *istate,int pos) 486{ 487struct cache_entry *ce = istate->cache[pos]; 488 489record_resolve_undo(istate, ce); 490remove_name_hash(istate, ce); 491free(ce); 492 istate->cache_changed |= CE_ENTRY_REMOVED; 493 istate->cache_nr--; 494if(pos >= istate->cache_nr) 495return0; 496memmove(istate->cache + pos, 497 istate->cache + pos +1, 498(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 499return1; 500} 501 502/* 503 * Remove all cache entries marked for removal, that is where 504 * CE_REMOVE is set in ce_flags. This is much more effective than 505 * calling remove_index_entry_at() for each entry to be removed. 506 */ 507voidremove_marked_cache_entries(struct index_state *istate) 508{ 509struct cache_entry **ce_array = istate->cache; 510unsigned int i, j; 511 512for(i = j =0; i < istate->cache_nr; i++) { 513if(ce_array[i]->ce_flags & CE_REMOVE) { 514remove_name_hash(istate, ce_array[i]); 515free(ce_array[i]); 516} 517else 518 ce_array[j++] = ce_array[i]; 519} 520if(j == istate->cache_nr) 521return; 522 istate->cache_changed |= CE_ENTRY_REMOVED; 523 istate->cache_nr = j; 524} 525 526intremove_file_from_index(struct index_state *istate,const char*path) 527{ 528int pos =index_name_pos(istate, path,strlen(path)); 529if(pos <0) 530 pos = -pos-1; 531cache_tree_invalidate_path(istate, path); 532while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 533remove_index_entry_at(istate, pos); 534return0; 535} 536 537static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 538{ 539return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 540} 541 542static intindex_name_pos_also_unmerged(struct index_state *istate, 543const char*path,int namelen) 544{ 545int pos =index_name_pos(istate, path, namelen); 546struct cache_entry *ce; 547 548if(pos >=0) 549return pos; 550 551/* maybe unmerged? */ 552 pos = -1- pos; 553if(pos >= istate->cache_nr || 554compare_name((ce = istate->cache[pos]), path, namelen)) 555return-1; 556 557/* order of preference: stage 2, 1, 3 */ 558if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 559ce_stage((ce = istate->cache[pos +1])) ==2&& 560!compare_name(ce, path, namelen)) 561 pos++; 562return pos; 563} 564 565static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 566{ 567int len =ce_namelen(ce); 568returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 569} 570 571/* 572 * If we add a filename that aliases in the cache, we will use the 573 * name that we already have - but we don't want to update the same 574 * alias twice, because that implies that there were actually two 575 * different files with aliasing names! 576 * 577 * So we use the CE_ADDED flag to verify that the alias was an old 578 * one before we accept it as 579 */ 580static struct cache_entry *create_alias_ce(struct cache_entry *ce,struct cache_entry *alias) 581{ 582int len; 583struct cache_entry *new; 584 585if(alias->ce_flags & CE_ADDED) 586die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 587 588/* Ok, create the new entry using the name of the existing alias */ 589 len =ce_namelen(alias); 590new=xcalloc(1,cache_entry_size(len)); 591memcpy(new->name, alias->name, len); 592copy_cache_entry(new, ce); 593free(ce); 594return new; 595} 596 597voidset_object_name_for_intent_to_add_entry(struct cache_entry *ce) 598{ 599unsigned char sha1[20]; 600if(write_sha1_file("",0, blob_type, sha1)) 601die("cannot create an empty blob in the object database"); 602hashcpy(ce->sha1, sha1); 603} 604 605intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 606{ 607int size, namelen, was_same; 608 mode_t st_mode = st->st_mode; 609struct cache_entry *ce, *alias; 610unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; 611int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 612int pretend = flags & ADD_CACHE_PRETEND; 613int intent_only = flags & ADD_CACHE_INTENT; 614int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 615(intent_only ? ADD_CACHE_NEW_ONLY :0)); 616 617if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 618returnerror("%s: can only add regular files, symbolic links or git-directories", path); 619 620 namelen =strlen(path); 621if(S_ISDIR(st_mode)) { 622while(namelen && path[namelen-1] =='/') 623 namelen--; 624} 625 size =cache_entry_size(namelen); 626 ce =xcalloc(1, size); 627memcpy(ce->name, path, namelen); 628 ce->ce_namelen = namelen; 629if(!intent_only) 630fill_stat_cache_info(ce, st); 631else 632 ce->ce_flags |= CE_INTENT_TO_ADD; 633 634if(trust_executable_bit && has_symlinks) 635 ce->ce_mode =create_ce_mode(st_mode); 636else{ 637/* If there is an existing entry, pick the mode bits and type 638 * from it, otherwise assume unexecutable regular file. 639 */ 640struct cache_entry *ent; 641int pos =index_name_pos_also_unmerged(istate, path, namelen); 642 643 ent = (0<= pos) ? istate->cache[pos] : NULL; 644 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 645} 646 647/* When core.ignorecase=true, determine if a directory of the same name but differing 648 * case already exists within the Git repository. If it does, ensure the directory 649 * case of the file being added to the repository matches (is folded into) the existing 650 * entry's directory case. 651 */ 652if(ignore_case) { 653const char*startPtr = ce->name; 654const char*ptr = startPtr; 655while(*ptr) { 656while(*ptr && *ptr !='/') 657++ptr; 658if(*ptr =='/') { 659struct cache_entry *foundce; 660++ptr; 661 foundce =index_dir_exists(istate, ce->name, ptr - ce->name -1); 662if(foundce) { 663memcpy((void*)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); 664 startPtr = ptr; 665} 666} 667} 668} 669 670 alias =index_file_exists(istate, ce->name,ce_namelen(ce), ignore_case); 671if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 672/* Nothing changed, really */ 673free(ce); 674if(!S_ISGITLINK(alias->ce_mode)) 675ce_mark_uptodate(alias); 676 alias->ce_flags |= CE_ADDED; 677return0; 678} 679if(!intent_only) { 680if(index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) 681returnerror("unable to index file%s", path); 682}else 683set_object_name_for_intent_to_add_entry(ce); 684 685if(ignore_case && alias &&different_name(ce, alias)) 686 ce =create_alias_ce(ce, alias); 687 ce->ce_flags |= CE_ADDED; 688 689/* It was suspected to be racily clean, but it turns out to be Ok */ 690 was_same = (alias && 691!ce_stage(alias) && 692!hashcmp(alias->sha1, ce->sha1) && 693 ce->ce_mode == alias->ce_mode); 694 695if(pretend) 696; 697else if(add_index_entry(istate, ce, add_option)) 698returnerror("unable to add%sto index",path); 699if(verbose && !was_same) 700printf("add '%s'\n", path); 701return0; 702} 703 704intadd_file_to_index(struct index_state *istate,const char*path,int flags) 705{ 706struct stat st; 707if(lstat(path, &st)) 708die_errno("unable to stat '%s'", path); 709returnadd_to_index(istate, path, &st, flags); 710} 711 712struct cache_entry *make_cache_entry(unsigned int mode, 713const unsigned char*sha1,const char*path,int stage, 714unsigned int refresh_options) 715{ 716int size, len; 717struct cache_entry *ce; 718 719if(!verify_path(path)) { 720error("Invalid path '%s'", path); 721return NULL; 722} 723 724 len =strlen(path); 725 size =cache_entry_size(len); 726 ce =xcalloc(1, size); 727 728hashcpy(ce->sha1, sha1); 729memcpy(ce->name, path, len); 730 ce->ce_flags =create_ce_flags(stage); 731 ce->ce_namelen = len; 732 ce->ce_mode =create_ce_mode(mode); 733 734returnrefresh_cache_entry(ce, refresh_options); 735} 736 737intce_same_name(const struct cache_entry *a,const struct cache_entry *b) 738{ 739int len =ce_namelen(a); 740returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 741} 742 743/* 744 * We fundamentally don't like some paths: we don't want 745 * dot or dot-dot anywhere, and for obvious reasons don't 746 * want to recurse into ".git" either. 747 * 748 * Also, we don't want double slashes or slashes at the 749 * end that can make pathnames ambiguous. 750 */ 751static intverify_dotfile(const char*rest) 752{ 753/* 754 * The first character was '.', but that 755 * has already been discarded, we now test 756 * the rest. 757 */ 758 759/* "." is not allowed */ 760if(*rest =='\0'||is_dir_sep(*rest)) 761return0; 762 763switch(*rest) { 764/* 765 * ".git" followed by NUL or slash is bad. This 766 * shares the path end test with the ".." case. 767 */ 768case'g': 769if(rest[1] !='i') 770break; 771if(rest[2] !='t') 772break; 773 rest +=2; 774/* fallthrough */ 775case'.': 776if(rest[1] =='\0'||is_dir_sep(rest[1])) 777return0; 778} 779return1; 780} 781 782intverify_path(const char*path) 783{ 784char c; 785 786if(has_dos_drive_prefix(path)) 787return0; 788 789goto inside; 790for(;;) { 791if(!c) 792return1; 793if(is_dir_sep(c)) { 794inside: 795 c = *path++; 796if((c =='.'&& !verify_dotfile(path)) || 797is_dir_sep(c) || c =='\0') 798return0; 799} 800 c = *path++; 801} 802} 803 804/* 805 * Do we have another file that has the beginning components being a 806 * proper superset of the name we're trying to add? 807 */ 808static inthas_file_name(struct index_state *istate, 809const struct cache_entry *ce,int pos,int ok_to_replace) 810{ 811int retval =0; 812int len =ce_namelen(ce); 813int stage =ce_stage(ce); 814const char*name = ce->name; 815 816while(pos < istate->cache_nr) { 817struct cache_entry *p = istate->cache[pos++]; 818 819if(len >=ce_namelen(p)) 820break; 821if(memcmp(name, p->name, len)) 822break; 823if(ce_stage(p) != stage) 824continue; 825if(p->name[len] !='/') 826continue; 827if(p->ce_flags & CE_REMOVE) 828continue; 829 retval = -1; 830if(!ok_to_replace) 831break; 832remove_index_entry_at(istate, --pos); 833} 834return retval; 835} 836 837/* 838 * Do we have another file with a pathname that is a proper 839 * subset of the name we're trying to add? 840 */ 841static inthas_dir_name(struct index_state *istate, 842const struct cache_entry *ce,int pos,int ok_to_replace) 843{ 844int retval =0; 845int stage =ce_stage(ce); 846const char*name = ce->name; 847const char*slash = name +ce_namelen(ce); 848 849for(;;) { 850int len; 851 852for(;;) { 853if(*--slash =='/') 854break; 855if(slash <= ce->name) 856return retval; 857} 858 len = slash - name; 859 860 pos =index_name_stage_pos(istate, name, len, stage); 861if(pos >=0) { 862/* 863 * Found one, but not so fast. This could 864 * be a marker that says "I was here, but 865 * I am being removed". Such an entry is 866 * not a part of the resulting tree, and 867 * it is Ok to have a directory at the same 868 * path. 869 */ 870if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 871 retval = -1; 872if(!ok_to_replace) 873break; 874remove_index_entry_at(istate, pos); 875continue; 876} 877} 878else 879 pos = -pos-1; 880 881/* 882 * Trivial optimization: if we find an entry that 883 * already matches the sub-directory, then we know 884 * we're ok, and we can exit. 885 */ 886while(pos < istate->cache_nr) { 887struct cache_entry *p = istate->cache[pos]; 888if((ce_namelen(p) <= len) || 889(p->name[len] !='/') || 890memcmp(p->name, name, len)) 891break;/* not our subdirectory */ 892if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 893/* 894 * p is at the same stage as our entry, and 895 * is a subdirectory of what we are looking 896 * at, so we cannot have conflicts at our 897 * level or anything shorter. 898 */ 899return retval; 900 pos++; 901} 902} 903return retval; 904} 905 906/* We may be in a situation where we already have path/file and path 907 * is being added, or we already have path and path/file is being 908 * added. Either one would result in a nonsense tree that has path 909 * twice when git-write-tree tries to write it out. Prevent it. 910 * 911 * If ok-to-replace is specified, we remove the conflicting entries 912 * from the cache so the caller should recompute the insert position. 913 * When this happens, we return non-zero. 914 */ 915static intcheck_file_directory_conflict(struct index_state *istate, 916const struct cache_entry *ce, 917int pos,int ok_to_replace) 918{ 919int retval; 920 921/* 922 * When ce is an "I am going away" entry, we allow it to be added 923 */ 924if(ce->ce_flags & CE_REMOVE) 925return0; 926 927/* 928 * We check if the path is a sub-path of a subsequent pathname 929 * first, since removing those will not change the position 930 * in the array. 931 */ 932 retval =has_file_name(istate, ce, pos, ok_to_replace); 933 934/* 935 * Then check if the path might have a clashing sub-directory 936 * before it. 937 */ 938return retval +has_dir_name(istate, ce, pos, ok_to_replace); 939} 940 941static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 942{ 943int pos; 944int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 945int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 946int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 947int new_only = option & ADD_CACHE_NEW_ONLY; 948 949cache_tree_invalidate_path(istate, ce->name); 950 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 951 952/* existing match? Just replace it. */ 953if(pos >=0) { 954if(!new_only) 955replace_index_entry(istate, pos, ce); 956return0; 957} 958 pos = -pos-1; 959 960/* 961 * Inserting a merged entry ("stage 0") into the index 962 * will always replace all non-merged entries.. 963 */ 964if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 965while(ce_same_name(istate->cache[pos], ce)) { 966 ok_to_add =1; 967if(!remove_index_entry_at(istate, pos)) 968break; 969} 970} 971 972if(!ok_to_add) 973return-1; 974if(!verify_path(ce->name)) 975returnerror("Invalid path '%s'", ce->name); 976 977if(!skip_df_check && 978check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 979if(!ok_to_replace) 980returnerror("'%s' appears as both a file and as a directory", 981 ce->name); 982 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 983 pos = -pos-1; 984} 985return pos +1; 986} 987 988intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 989{ 990int pos; 991 992if(option & ADD_CACHE_JUST_APPEND) 993 pos = istate->cache_nr; 994else{ 995int ret; 996 ret =add_index_entry_with_check(istate, ce, option); 997if(ret <=0) 998return ret; 999 pos = ret -1;1000}10011002/* Make sure the array is big enough .. */1003ALLOC_GROW(istate->cache, istate->cache_nr +1, istate->cache_alloc);10041005/* Add it in.. */1006 istate->cache_nr++;1007if(istate->cache_nr > pos +1)1008memmove(istate->cache + pos +1,1009 istate->cache + pos,1010(istate->cache_nr - pos -1) *sizeof(ce));1011set_index_entry(istate, pos, ce);1012 istate->cache_changed |= CE_ENTRY_ADDED;1013return0;1014}10151016/*1017 * "refresh" does not calculate a new sha1 file or bring the1018 * cache up-to-date for mode/content changes. But what it1019 * _does_ do is to "re-match" the stat information of a file1020 * with the cache, so that you can refresh the cache for a1021 * file that hasn't been changed but where the stat entry is1022 * out of date.1023 *1024 * For example, you'd want to do this after doing a "git-read-tree",1025 * to link up the stat cache details with the proper files.1026 */1027static struct cache_entry *refresh_cache_ent(struct index_state *istate,1028struct cache_entry *ce,1029unsigned int options,int*err,1030int*changed_ret)1031{1032struct stat st;1033struct cache_entry *updated;1034int changed, size;1035int refresh = options & CE_MATCH_REFRESH;1036int ignore_valid = options & CE_MATCH_IGNORE_VALID;1037int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;1038int ignore_missing = options & CE_MATCH_IGNORE_MISSING;10391040if(!refresh ||ce_uptodate(ce))1041return ce;10421043/*1044 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1045 * that the change to the work tree does not matter and told1046 * us not to worry.1047 */1048if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1049ce_mark_uptodate(ce);1050return ce;1051}1052if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1053ce_mark_uptodate(ce);1054return ce;1055}10561057if(lstat(ce->name, &st) <0) {1058if(ignore_missing && errno == ENOENT)1059return ce;1060if(err)1061*err = errno;1062return NULL;1063}10641065 changed =ie_match_stat(istate, ce, &st, options);1066if(changed_ret)1067*changed_ret = changed;1068if(!changed) {1069/*1070 * The path is unchanged. If we were told to ignore1071 * valid bit, then we did the actual stat check and1072 * found that the entry is unmodified. If the entry1073 * is not marked VALID, this is the place to mark it1074 * valid again, under "assume unchanged" mode.1075 */1076if(ignore_valid && assume_unchanged &&1077!(ce->ce_flags & CE_VALID))1078;/* mark this one VALID again */1079else{1080/*1081 * We do not mark the index itself "modified"1082 * because CE_UPTODATE flag is in-core only;1083 * we are not going to write this change out.1084 */1085if(!S_ISGITLINK(ce->ce_mode))1086ce_mark_uptodate(ce);1087return ce;1088}1089}10901091if(ie_modified(istate, ce, &st, options)) {1092if(err)1093*err = EINVAL;1094return NULL;1095}10961097 size =ce_size(ce);1098 updated =xmalloc(size);1099memcpy(updated, ce, size);1100fill_stat_cache_info(updated, &st);1101/*1102 * If ignore_valid is not set, we should leave CE_VALID bit1103 * alone. Otherwise, paths marked with --no-assume-unchanged1104 * (i.e. things to be edited) will reacquire CE_VALID bit1105 * automatically, which is not really what we want.1106 */1107if(!ignore_valid && assume_unchanged &&1108!(ce->ce_flags & CE_VALID))1109 updated->ce_flags &= ~CE_VALID;11101111/* istate->cache_changed is updated in the caller */1112return updated;1113}11141115static voidshow_file(const char* fmt,const char* name,int in_porcelain,1116int* first,const char*header_msg)1117{1118if(in_porcelain && *first && header_msg) {1119printf("%s\n", header_msg);1120*first =0;1121}1122printf(fmt, name);1123}11241125intrefresh_index(struct index_state *istate,unsigned int flags,1126const struct pathspec *pathspec,1127char*seen,const char*header_msg)1128{1129int i;1130int has_errors =0;1131int really = (flags & REFRESH_REALLY) !=0;1132int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1133int quiet = (flags & REFRESH_QUIET) !=0;1134int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1135int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1136int first =1;1137int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1138unsigned int options = (CE_MATCH_REFRESH |1139(really ? CE_MATCH_IGNORE_VALID :0) |1140(not_new ? CE_MATCH_IGNORE_MISSING :0));1141const char*modified_fmt;1142const char*deleted_fmt;1143const char*typechange_fmt;1144const char*added_fmt;1145const char*unmerged_fmt;11461147 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1148 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1149 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1150 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1151 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1152for(i =0; i < istate->cache_nr; i++) {1153struct cache_entry *ce, *new;1154int cache_errno =0;1155int changed =0;1156int filtered =0;11571158 ce = istate->cache[i];1159if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1160continue;11611162if(pathspec && !ce_path_match(ce, pathspec, seen))1163 filtered =1;11641165if(ce_stage(ce)) {1166while((i < istate->cache_nr) &&1167!strcmp(istate->cache[i]->name, ce->name))1168 i++;1169 i--;1170if(allow_unmerged)1171continue;1172if(!filtered)1173show_file(unmerged_fmt, ce->name, in_porcelain,1174&first, header_msg);1175 has_errors =1;1176continue;1177}11781179if(filtered)1180continue;11811182new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1183if(new== ce)1184continue;1185if(!new) {1186const char*fmt;11871188if(really && cache_errno == EINVAL) {1189/* If we are doing --really-refresh that1190 * means the index is not valid anymore.1191 */1192 ce->ce_flags &= ~CE_VALID;1193 istate->cache_changed |= CE_ENTRY_CHANGED;1194}1195if(quiet)1196continue;11971198if(cache_errno == ENOENT)1199 fmt = deleted_fmt;1200else if(ce->ce_flags & CE_INTENT_TO_ADD)1201 fmt = added_fmt;/* must be before other checks */1202else if(changed & TYPE_CHANGED)1203 fmt = typechange_fmt;1204else1205 fmt = modified_fmt;1206show_file(fmt,1207 ce->name, in_porcelain, &first, header_msg);1208 has_errors =1;1209continue;1210}12111212replace_index_entry(istate, i,new);1213}1214return has_errors;1215}12161217static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,1218unsigned int options)1219{1220returnrefresh_cache_ent(&the_index, ce, options, NULL, NULL);1221}122212231224/*****************************************************************1225 * Index File I/O1226 *****************************************************************/12271228#define INDEX_FORMAT_DEFAULT 312291230static intindex_format_config(const char*var,const char*value,void*cb)1231{1232unsigned int*version = cb;1233if(!strcmp(var,"index.version")) {1234*version =git_config_int(var, value);1235return0;1236}1237return1;1238}12391240static unsigned intget_index_format_default(void)1241{1242char*envversion =getenv("GIT_INDEX_VERSION");1243char*endp;1244unsigned int version = INDEX_FORMAT_DEFAULT;12451246if(!envversion) {1247git_config(index_format_config, &version);1248if(version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1249warning(_("index.version set, but the value is invalid.\n"1250"Using version%i"), INDEX_FORMAT_DEFAULT);1251return INDEX_FORMAT_DEFAULT;1252}1253return version;1254}12551256 version =strtoul(envversion, &endp,10);1257if(*endp ||1258 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1259warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"1260"Using version%i"), INDEX_FORMAT_DEFAULT);1261 version = INDEX_FORMAT_DEFAULT;1262}1263return version;1264}12651266/*1267 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1268 * Again - this is just a (very strong in practice) heuristic that1269 * the inode hasn't changed.1270 *1271 * We save the fields in big-endian order to allow using the1272 * index file over NFS transparently.1273 */1274struct ondisk_cache_entry {1275struct cache_time ctime;1276struct cache_time mtime;1277uint32_t dev;1278uint32_t ino;1279uint32_t mode;1280uint32_t uid;1281uint32_t gid;1282uint32_t size;1283unsigned char sha1[20];1284uint16_t flags;1285char name[FLEX_ARRAY];/* more */1286};12871288/*1289 * This struct is used when CE_EXTENDED bit is 11290 * The struct must match ondisk_cache_entry exactly from1291 * ctime till flags1292 */1293struct ondisk_cache_entry_extended {1294struct cache_time ctime;1295struct cache_time mtime;1296uint32_t dev;1297uint32_t ino;1298uint32_t mode;1299uint32_t uid;1300uint32_t gid;1301uint32_t size;1302unsigned char sha1[20];1303uint16_t flags;1304uint16_t flags2;1305char name[FLEX_ARRAY];/* more */1306};13071308/* These are only used for v3 or lower */1309#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1310#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1311#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1312#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1313 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1314 ondisk_cache_entry_size(ce_namelen(ce)))13151316static intverify_hdr(struct cache_header *hdr,unsigned long size)1317{1318 git_SHA_CTX c;1319unsigned char sha1[20];1320int hdr_version;13211322if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1323returnerror("bad signature");1324 hdr_version =ntohl(hdr->hdr_version);1325if(hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)1326returnerror("bad index version%d", hdr_version);1327git_SHA1_Init(&c);1328git_SHA1_Update(&c, hdr, size -20);1329git_SHA1_Final(sha1, &c);1330if(hashcmp(sha1, (unsigned char*)hdr + size -20))1331returnerror("bad index file sha1 signature");1332return0;1333}13341335static intread_index_extension(struct index_state *istate,1336const char*ext,void*data,unsigned long sz)1337{1338switch(CACHE_EXT(ext)) {1339case CACHE_EXT_TREE:1340 istate->cache_tree =cache_tree_read(data, sz);1341break;1342case CACHE_EXT_RESOLVE_UNDO:1343 istate->resolve_undo =resolve_undo_read(data, sz);1344break;1345case CACHE_EXT_LINK:1346if(read_link_extension(istate, data, sz))1347return-1;1348break;1349default:1350if(*ext <'A'||'Z'< *ext)1351returnerror("index uses %.4s extension, which we do not understand",1352 ext);1353fprintf(stderr,"ignoring %.4s extension\n", ext);1354break;1355}1356return0;1357}13581359intread_index(struct index_state *istate)1360{1361returnread_index_from(istate,get_index_file());1362}13631364static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1365unsigned int flags,1366const char*name,1367size_t len)1368{1369struct cache_entry *ce =xmalloc(cache_entry_size(len));13701371 ce->ce_stat_data.sd_ctime.sec =get_be32(&ondisk->ctime.sec);1372 ce->ce_stat_data.sd_mtime.sec =get_be32(&ondisk->mtime.sec);1373 ce->ce_stat_data.sd_ctime.nsec =get_be32(&ondisk->ctime.nsec);1374 ce->ce_stat_data.sd_mtime.nsec =get_be32(&ondisk->mtime.nsec);1375 ce->ce_stat_data.sd_dev =get_be32(&ondisk->dev);1376 ce->ce_stat_data.sd_ino =get_be32(&ondisk->ino);1377 ce->ce_mode =get_be32(&ondisk->mode);1378 ce->ce_stat_data.sd_uid =get_be32(&ondisk->uid);1379 ce->ce_stat_data.sd_gid =get_be32(&ondisk->gid);1380 ce->ce_stat_data.sd_size =get_be32(&ondisk->size);1381 ce->ce_flags = flags & ~CE_NAMEMASK;1382 ce->ce_namelen = len;1383 ce->index =0;1384hashcpy(ce->sha1, ondisk->sha1);1385memcpy(ce->name, name, len);1386 ce->name[len] ='\0';1387return ce;1388}13891390/*1391 * Adjacent cache entries tend to share the leading paths, so it makes1392 * sense to only store the differences in later entries. In the v41393 * on-disk format of the index, each on-disk cache entry stores the1394 * number of bytes to be stripped from the end of the previous name,1395 * and the bytes to append to the result, to come up with its name.1396 */1397static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1398{1399const unsigned char*ep, *cp = (const unsigned char*)cp_;1400size_t len =decode_varint(&cp);14011402if(name->len < len)1403die("malformed name field in the index");1404strbuf_remove(name, name->len - len, len);1405for(ep = cp; *ep; ep++)1406;/* find the end */1407strbuf_add(name, cp, ep - cp);1408return(const char*)ep +1- cp_;1409}14101411static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1412unsigned long*ent_size,1413struct strbuf *previous_name)1414{1415struct cache_entry *ce;1416size_t len;1417const char*name;1418unsigned int flags;14191420/* On-disk flags are just 16 bits */1421 flags =get_be16(&ondisk->flags);1422 len = flags & CE_NAMEMASK;14231424if(flags & CE_EXTENDED) {1425struct ondisk_cache_entry_extended *ondisk2;1426int extended_flags;1427 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1428 extended_flags =get_be16(&ondisk2->flags2) <<16;1429/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1430if(extended_flags & ~CE_EXTENDED_FLAGS)1431die("Unknown index entry format%08x", extended_flags);1432 flags |= extended_flags;1433 name = ondisk2->name;1434}1435else1436 name = ondisk->name;14371438if(!previous_name) {1439/* v3 and earlier */1440if(len == CE_NAMEMASK)1441 len =strlen(name);1442 ce =cache_entry_from_ondisk(ondisk, flags, name, len);14431444*ent_size =ondisk_ce_size(ce);1445}else{1446unsigned long consumed;1447 consumed =expand_name_field(previous_name, name);1448 ce =cache_entry_from_ondisk(ondisk, flags,1449 previous_name->buf,1450 previous_name->len);14511452*ent_size = (name - ((char*)ondisk)) + consumed;1453}1454return ce;1455}14561457/* remember to discard_cache() before reading a different cache! */1458static intdo_read_index(struct index_state *istate,const char*path,1459int must_exist)1460{1461int fd, i;1462struct stat st;1463unsigned long src_offset;1464struct cache_header *hdr;1465void*mmap;1466size_t mmap_size;1467struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;14681469if(istate->initialized)1470return istate->cache_nr;14711472 istate->timestamp.sec =0;1473 istate->timestamp.nsec =0;1474 fd =open(path, O_RDONLY);1475if(fd <0) {1476if(!must_exist && errno == ENOENT)1477return0;1478die_errno("%s: index file open failed", path);1479}14801481if(fstat(fd, &st))1482die_errno("cannot stat the open index");14831484 mmap_size =xsize_t(st.st_size);1485if(mmap_size <sizeof(struct cache_header) +20)1486die("index file smaller than expected");14871488 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1489if(mmap == MAP_FAILED)1490die_errno("unable to map index file");1491close(fd);14921493 hdr = mmap;1494if(verify_hdr(hdr, mmap_size) <0)1495goto unmap;14961497hashcpy(istate->sha1, (const unsigned char*)hdr + mmap_size -20);1498 istate->version =ntohl(hdr->hdr_version);1499 istate->cache_nr =ntohl(hdr->hdr_entries);1500 istate->cache_alloc =alloc_nr(istate->cache_nr);1501 istate->cache =xcalloc(istate->cache_alloc,sizeof(*istate->cache));1502 istate->initialized =1;15031504if(istate->version ==4)1505 previous_name = &previous_name_buf;1506else1507 previous_name = NULL;15081509 src_offset =sizeof(*hdr);1510for(i =0; i < istate->cache_nr; i++) {1511struct ondisk_cache_entry *disk_ce;1512struct cache_entry *ce;1513unsigned long consumed;15141515 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1516 ce =create_from_disk(disk_ce, &consumed, previous_name);1517set_index_entry(istate, i, ce);15181519 src_offset += consumed;1520}1521strbuf_release(&previous_name_buf);1522 istate->timestamp.sec = st.st_mtime;1523 istate->timestamp.nsec =ST_MTIME_NSEC(st);15241525while(src_offset <= mmap_size -20-8) {1526/* After an array of active_nr index entries,1527 * there can be arbitrary number of extended1528 * sections, each of which is prefixed with1529 * extension name (4-byte) and section length1530 * in 4-byte network byte order.1531 */1532uint32_t extsize;1533memcpy(&extsize, (char*)mmap + src_offset +4,4);1534 extsize =ntohl(extsize);1535if(read_index_extension(istate,1536(const char*) mmap + src_offset,1537(char*) mmap + src_offset +8,1538 extsize) <0)1539goto unmap;1540 src_offset +=8;1541 src_offset += extsize;1542}1543munmap(mmap, mmap_size);1544return istate->cache_nr;15451546unmap:1547munmap(mmap, mmap_size);1548die("index file corrupt");1549}15501551intread_index_from(struct index_state *istate,const char*path)1552{1553struct split_index *split_index;1554int ret;15551556/* istate->initialized covers both .git/index and .git/sharedindex.xxx */1557if(istate->initialized)1558return istate->cache_nr;15591560 ret =do_read_index(istate, path,0);1561 split_index = istate->split_index;1562if(!split_index)1563return ret;15641565if(is_null_sha1(split_index->base_sha1))1566return ret;1567if(istate->cache_nr)1568die("index in split-index mode must contain no entries");15691570if(split_index->base)1571discard_index(split_index->base);1572else1573 split_index->base =xcalloc(1,sizeof(*split_index->base));1574 ret =do_read_index(split_index->base,1575git_path("sharedindex.%s",1576sha1_to_hex(split_index->base_sha1)),1);1577if(hashcmp(split_index->base_sha1, split_index->base->sha1))1578die("broken index, expect%sin%s, got%s",1579sha1_to_hex(split_index->base_sha1),1580git_path("sharedindex.%s",1581sha1_to_hex(split_index->base_sha1)),1582sha1_to_hex(split_index->base->sha1));1583merge_base_index(istate);1584return ret;1585}15861587intis_index_unborn(struct index_state *istate)1588{1589return(!istate->cache_nr && !istate->timestamp.sec);1590}15911592intdiscard_index(struct index_state *istate)1593{1594int i;15951596for(i =0; i < istate->cache_nr; i++) {1597if(istate->cache[i]->index &&1598 istate->split_index &&1599 istate->split_index->base &&1600 istate->cache[i]->index <= istate->split_index->base->cache_nr &&1601 istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index -1])1602continue;1603free(istate->cache[i]);1604}1605resolve_undo_clear_index(istate);1606 istate->cache_nr =0;1607 istate->cache_changed =0;1608 istate->timestamp.sec =0;1609 istate->timestamp.nsec =0;1610free_name_hash(istate);1611cache_tree_free(&(istate->cache_tree));1612 istate->initialized =0;1613free(istate->cache);1614 istate->cache = NULL;1615 istate->cache_alloc =0;1616discard_split_index(istate);1617return0;1618}16191620intunmerged_index(const struct index_state *istate)1621{1622int i;1623for(i =0; i < istate->cache_nr; i++) {1624if(ce_stage(istate->cache[i]))1625return1;1626}1627return0;1628}16291630#define WRITE_BUFFER_SIZE 81921631static unsigned char write_buffer[WRITE_BUFFER_SIZE];1632static unsigned long write_buffer_len;16331634static intce_write_flush(git_SHA_CTX *context,int fd)1635{1636unsigned int buffered = write_buffer_len;1637if(buffered) {1638git_SHA1_Update(context, write_buffer, buffered);1639if(write_in_full(fd, write_buffer, buffered) != buffered)1640return-1;1641 write_buffer_len =0;1642}1643return0;1644}16451646static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1647{1648while(len) {1649unsigned int buffered = write_buffer_len;1650unsigned int partial = WRITE_BUFFER_SIZE - buffered;1651if(partial > len)1652 partial = len;1653memcpy(write_buffer + buffered, data, partial);1654 buffered += partial;1655if(buffered == WRITE_BUFFER_SIZE) {1656 write_buffer_len = buffered;1657if(ce_write_flush(context, fd))1658return-1;1659 buffered =0;1660}1661 write_buffer_len = buffered;1662 len -= partial;1663 data = (char*) data + partial;1664}1665return0;1666}16671668static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1669unsigned int ext,unsigned int sz)1670{1671 ext =htonl(ext);1672 sz =htonl(sz);1673return((ce_write(context, fd, &ext,4) <0) ||1674(ce_write(context, fd, &sz,4) <0)) ? -1:0;1675}16761677static intce_flush(git_SHA_CTX *context,int fd,unsigned char*sha1)1678{1679unsigned int left = write_buffer_len;16801681if(left) {1682 write_buffer_len =0;1683git_SHA1_Update(context, write_buffer, left);1684}16851686/* Flush first if not enough space for SHA1 signature */1687if(left +20> WRITE_BUFFER_SIZE) {1688if(write_in_full(fd, write_buffer, left) != left)1689return-1;1690 left =0;1691}16921693/* Append the SHA1 signature at the end */1694git_SHA1_Final(write_buffer + left, context);1695hashcpy(sha1, write_buffer + left);1696 left +=20;1697return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1698}16991700static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1701{1702/*1703 * The only thing we care about in this function is to smudge the1704 * falsely clean entry due to touch-update-touch race, so we leave1705 * everything else as they are. We are called for entries whose1706 * ce_stat_data.sd_mtime match the index file mtime.1707 *1708 * Note that this actually does not do much for gitlinks, for1709 * which ce_match_stat_basic() always goes to the actual1710 * contents. The caller checks with is_racy_timestamp() which1711 * always says "no" for gitlinks, so we are not called for them ;-)1712 */1713struct stat st;17141715if(lstat(ce->name, &st) <0)1716return;1717if(ce_match_stat_basic(ce, &st))1718return;1719if(ce_modified_check_fs(ce, &st)) {1720/* This is "racily clean"; smudge it. Note that this1721 * is a tricky code. At first glance, it may appear1722 * that it can break with this sequence:1723 *1724 * $ echo xyzzy >frotz1725 * $ git-update-index --add frotz1726 * $ : >frotz1727 * $ sleep 31728 * $ echo filfre >nitfol1729 * $ git-update-index --add nitfol1730 *1731 * but it does not. When the second update-index runs,1732 * it notices that the entry "frotz" has the same timestamp1733 * as index, and if we were to smudge it by resetting its1734 * size to zero here, then the object name recorded1735 * in index is the 6-byte file but the cached stat information1736 * becomes zero --- which would then match what we would1737 * obtain from the filesystem next time we stat("frotz").1738 *1739 * However, the second update-index, before calling1740 * this function, notices that the cached size is 61741 * bytes and what is on the filesystem is an empty1742 * file, and never calls us, so the cached size information1743 * for "frotz" stays 6 which does not match the filesystem.1744 */1745 ce->ce_stat_data.sd_size =0;1746}1747}17481749/* Copy miscellaneous fields but not the name */1750static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1751struct cache_entry *ce)1752{1753short flags;17541755 ondisk->ctime.sec =htonl(ce->ce_stat_data.sd_ctime.sec);1756 ondisk->mtime.sec =htonl(ce->ce_stat_data.sd_mtime.sec);1757 ondisk->ctime.nsec =htonl(ce->ce_stat_data.sd_ctime.nsec);1758 ondisk->mtime.nsec =htonl(ce->ce_stat_data.sd_mtime.nsec);1759 ondisk->dev =htonl(ce->ce_stat_data.sd_dev);1760 ondisk->ino =htonl(ce->ce_stat_data.sd_ino);1761 ondisk->mode =htonl(ce->ce_mode);1762 ondisk->uid =htonl(ce->ce_stat_data.sd_uid);1763 ondisk->gid =htonl(ce->ce_stat_data.sd_gid);1764 ondisk->size =htonl(ce->ce_stat_data.sd_size);1765hashcpy(ondisk->sha1, ce->sha1);17661767 flags = ce->ce_flags & ~CE_NAMEMASK;1768 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK :ce_namelen(ce));1769 ondisk->flags =htons(flags);1770if(ce->ce_flags & CE_EXTENDED) {1771struct ondisk_cache_entry_extended *ondisk2;1772 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1773 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1774return ondisk2->name;1775}1776else{1777return ondisk->name;1778}1779}17801781static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce,1782struct strbuf *previous_name)1783{1784int size;1785struct ondisk_cache_entry *ondisk;1786char*name;1787int result;17881789if(!previous_name) {1790 size =ondisk_ce_size(ce);1791 ondisk =xcalloc(1, size);1792 name =copy_cache_entry_to_ondisk(ondisk, ce);1793memcpy(name, ce->name,ce_namelen(ce));1794}else{1795int common, to_remove, prefix_size;1796unsigned char to_remove_vi[16];1797for(common =0;1798(ce->name[common] &&1799 common < previous_name->len &&1800 ce->name[common] == previous_name->buf[common]);1801 common++)1802;/* still matching */1803 to_remove = previous_name->len - common;1804 prefix_size =encode_varint(to_remove, to_remove_vi);18051806if(ce->ce_flags & CE_EXTENDED)1807 size =offsetof(struct ondisk_cache_entry_extended, name);1808else1809 size =offsetof(struct ondisk_cache_entry, name);1810 size += prefix_size + (ce_namelen(ce) - common +1);18111812 ondisk =xcalloc(1, size);1813 name =copy_cache_entry_to_ondisk(ondisk, ce);1814memcpy(name, to_remove_vi, prefix_size);1815memcpy(name + prefix_size, ce->name + common,ce_namelen(ce) - common);18161817strbuf_splice(previous_name, common, to_remove,1818 ce->name + common,ce_namelen(ce) - common);1819}18201821 result =ce_write(c, fd, ondisk, size);1822free(ondisk);1823return result;1824}18251826static inthas_racy_timestamp(struct index_state *istate)1827{1828int entries = istate->cache_nr;1829int i;18301831for(i =0; i < entries; i++) {1832struct cache_entry *ce = istate->cache[i];1833if(is_racy_timestamp(istate, ce))1834return1;1835}1836return0;1837}18381839/*1840 * Opportunistically update the index but do not complain if we can't1841 */1842voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1843{1844if((istate->cache_changed ||has_racy_timestamp(istate)) &&1845write_locked_index(istate, lockfile, COMMIT_LOCK))1846rollback_lock_file(lockfile);1847}18481849static intdo_write_index(struct index_state *istate,int newfd)1850{1851 git_SHA_CTX c;1852struct cache_header hdr;1853int i, err, removed, extended, hdr_version;1854struct cache_entry **cache = istate->cache;1855int entries = istate->cache_nr;1856struct stat st;1857struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;18581859for(i = removed = extended =0; i < entries; i++) {1860if(cache[i]->ce_flags & CE_REMOVE)1861 removed++;18621863/* reduce extended entries if possible */1864 cache[i]->ce_flags &= ~CE_EXTENDED;1865if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1866 extended++;1867 cache[i]->ce_flags |= CE_EXTENDED;1868}1869}18701871if(!istate->version)1872 istate->version =get_index_format_default();18731874/* demote version 3 to version 2 when the latter suffices */1875if(istate->version ==3|| istate->version ==2)1876 istate->version = extended ?3:2;18771878 hdr_version = istate->version;18791880 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1881 hdr.hdr_version =htonl(hdr_version);1882 hdr.hdr_entries =htonl(entries - removed);18831884git_SHA1_Init(&c);1885if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1886return-1;18871888 previous_name = (hdr_version ==4) ? &previous_name_buf : NULL;1889for(i =0; i < entries; i++) {1890struct cache_entry *ce = cache[i];1891if(ce->ce_flags & CE_REMOVE)1892continue;1893if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1894ce_smudge_racily_clean_entry(ce);1895if(is_null_sha1(ce->sha1)) {1896static const char msg[] ="cache entry has null sha1:%s";1897static int allow = -1;18981899if(allow <0)1900 allow =git_env_bool("GIT_ALLOW_NULL_SHA1",0);1901if(allow)1902warning(msg, ce->name);1903else1904returnerror(msg, ce->name);1905}1906if(ce_write_entry(&c, newfd, ce, previous_name) <0)1907return-1;1908}1909strbuf_release(&previous_name_buf);19101911/* Write extension data here */1912if(istate->split_index) {1913struct strbuf sb = STRBUF_INIT;19141915 err =write_link_extension(&sb, istate) <0||1916write_index_ext_header(&c, newfd, CACHE_EXT_LINK,1917 sb.len) <0||1918ce_write(&c, newfd, sb.buf, sb.len) <0;1919strbuf_release(&sb);1920if(err)1921return-1;1922}1923if(istate->cache_tree) {1924struct strbuf sb = STRBUF_INIT;19251926cache_tree_write(&sb, istate->cache_tree);1927 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01928||ce_write(&c, newfd, sb.buf, sb.len) <0;1929strbuf_release(&sb);1930if(err)1931return-1;1932}1933if(istate->resolve_undo) {1934struct strbuf sb = STRBUF_INIT;19351936resolve_undo_write(&sb, istate->resolve_undo);1937 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,1938 sb.len) <01939||ce_write(&c, newfd, sb.buf, sb.len) <0;1940strbuf_release(&sb);1941if(err)1942return-1;1943}19441945if(ce_flush(&c, newfd, istate->sha1) ||fstat(newfd, &st))1946return-1;1947 istate->timestamp.sec = (unsigned int)st.st_mtime;1948 istate->timestamp.nsec =ST_MTIME_NSEC(st);1949return0;1950}19511952voidset_alternate_index_output(const char*name)1953{1954 alternate_index_output = name;1955}19561957static intcommit_locked_index(struct lock_file *lk)1958{1959if(alternate_index_output) {1960if(lk->fd >=0&&close_lock_file(lk))1961return-1;1962if(rename(lk->filename, alternate_index_output))1963return-1;1964 lk->filename[0] =0;1965return0;1966}else{1967returncommit_lock_file(lk);1968}1969}19701971static intdo_write_locked_index(struct index_state *istate,struct lock_file *lock,1972unsigned flags)1973{1974int ret =do_write_index(istate, lock->fd);1975if(ret)1976return ret;1977assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=1978(COMMIT_LOCK | CLOSE_LOCK));1979if(flags & COMMIT_LOCK)1980returncommit_locked_index(lock);1981else if(flags & CLOSE_LOCK)1982returnclose_lock_file(lock);1983else1984return ret;1985}19861987static intwrite_split_index(struct index_state *istate,1988struct lock_file *lock,1989unsigned flags)1990{1991int ret;1992prepare_to_write_split_index(istate);1993 ret =do_write_locked_index(istate, lock, flags);1994finish_writing_split_index(istate);1995return ret;1996}19971998intwrite_locked_index(struct index_state *istate,struct lock_file *lock,1999unsigned flags)2000{2001struct split_index *si = istate->split_index;20022003if(!si || (istate->cache_changed & ~EXTMASK)) {2004if(si)2005hashclr(si->base_sha1);2006returndo_write_locked_index(istate, lock, flags);2007}20082009returnwrite_split_index(istate, lock, flags);2010}20112012/*2013 * Read the index file that is potentially unmerged into given2014 * index_state, dropping any unmerged entries. Returns true if2015 * the index is unmerged. Callers who want to refuse to work2016 * from an unmerged state can call this and check its return value,2017 * instead of calling read_cache().2018 */2019intread_index_unmerged(struct index_state *istate)2020{2021int i;2022int unmerged =0;20232024read_index(istate);2025for(i =0; i < istate->cache_nr; i++) {2026struct cache_entry *ce = istate->cache[i];2027struct cache_entry *new_ce;2028int size, len;20292030if(!ce_stage(ce))2031continue;2032 unmerged =1;2033 len =ce_namelen(ce);2034 size =cache_entry_size(len);2035 new_ce =xcalloc(1, size);2036memcpy(new_ce->name, ce->name, len);2037 new_ce->ce_flags =create_ce_flags(0) | CE_CONFLICTED;2038 new_ce->ce_namelen = len;2039 new_ce->ce_mode = ce->ce_mode;2040if(add_index_entry(istate, new_ce,0))2041returnerror("%s: cannot drop to stage #0",2042 new_ce->name);2043 i =index_name_pos(istate, new_ce->name, len);2044}2045return unmerged;2046}20472048/*2049 * Returns 1 if the path is an "other" path with respect to2050 * the index; that is, the path is not mentioned in the index at all,2051 * either as a file, a directory with some files in the index,2052 * or as an unmerged entry.2053 *2054 * We helpfully remove a trailing "/" from directories so that2055 * the output of read_directory can be used as-is.2056 */2057intindex_name_is_other(const struct index_state *istate,const char*name,2058int namelen)2059{2060int pos;2061if(namelen && name[namelen -1] =='/')2062 namelen--;2063 pos =index_name_pos(istate, name, namelen);2064if(0<= pos)2065return0;/* exact match */2066 pos = -pos -1;2067if(pos < istate->cache_nr) {2068struct cache_entry *ce = istate->cache[pos];2069if(ce_namelen(ce) == namelen &&2070!memcmp(ce->name, name, namelen))2071return0;/* Yup, this one exists unmerged */2072}2073return1;2074}20752076void*read_blob_data_from_index(struct index_state *istate,const char*path,unsigned long*size)2077{2078int pos, len;2079unsigned long sz;2080enum object_type type;2081void*data;20822083 len =strlen(path);2084 pos =index_name_pos(istate, path, len);2085if(pos <0) {2086/*2087 * We might be in the middle of a merge, in which2088 * case we would read stage #2 (ours).2089 */2090int i;2091for(i = -pos -1;2092(pos <0&& i < istate->cache_nr &&2093!strcmp(istate->cache[i]->name, path));2094 i++)2095if(ce_stage(istate->cache[i]) ==2)2096 pos = i;2097}2098if(pos <0)2099return NULL;2100 data =read_sha1_file(istate->cache[pos]->sha1, &type, &sz);2101if(!data || type != OBJ_BLOB) {2102free(data);2103return NULL;2104}2105if(size)2106*size = sz;2107return data;2108}21092110voidstat_validity_clear(struct stat_validity *sv)2111{2112free(sv->sd);2113 sv->sd = NULL;2114}21152116intstat_validity_check(struct stat_validity *sv,const char*path)2117{2118struct stat st;21192120if(stat(path, &st) <0)2121return sv->sd == NULL;2122if(!sv->sd)2123return0;2124returnS_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);2125}21262127voidstat_validity_update(struct stat_validity *sv,int fd)2128{2129struct stat st;21302131if(fstat(fd, &st) <0|| !S_ISREG(st.st_mode))2132stat_validity_clear(sv);2133else{2134if(!sv->sd)2135 sv->sd =xcalloc(1,sizeof(struct stat_data));2136fill_stat_data(sv->sd, &st);2137}2138}