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) 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); 491save_or_free_index_entry(istate, 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]); 515save_or_free_index_entry(istate, 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 index_state *istate, 581struct cache_entry *ce, 582struct cache_entry *alias) 583{ 584int len; 585struct cache_entry *new; 586 587if(alias->ce_flags & CE_ADDED) 588die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 589 590/* Ok, create the new entry using the name of the existing alias */ 591 len =ce_namelen(alias); 592new=xcalloc(1,cache_entry_size(len)); 593memcpy(new->name, alias->name, len); 594copy_cache_entry(new, ce); 595save_or_free_index_entry(istate, ce); 596return new; 597} 598 599voidset_object_name_for_intent_to_add_entry(struct cache_entry *ce) 600{ 601unsigned char sha1[20]; 602if(write_sha1_file("",0, blob_type, sha1)) 603die("cannot create an empty blob in the object database"); 604hashcpy(ce->sha1, sha1); 605} 606 607intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 608{ 609int size, namelen, was_same; 610 mode_t st_mode = st->st_mode; 611struct cache_entry *ce, *alias; 612unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; 613int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 614int pretend = flags & ADD_CACHE_PRETEND; 615int intent_only = flags & ADD_CACHE_INTENT; 616int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 617(intent_only ? ADD_CACHE_NEW_ONLY :0)); 618 619if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 620returnerror("%s: can only add regular files, symbolic links or git-directories", path); 621 622 namelen =strlen(path); 623if(S_ISDIR(st_mode)) { 624while(namelen && path[namelen-1] =='/') 625 namelen--; 626} 627 size =cache_entry_size(namelen); 628 ce =xcalloc(1, size); 629memcpy(ce->name, path, namelen); 630 ce->ce_namelen = namelen; 631if(!intent_only) 632fill_stat_cache_info(ce, st); 633else 634 ce->ce_flags |= CE_INTENT_TO_ADD; 635 636if(trust_executable_bit && has_symlinks) 637 ce->ce_mode =create_ce_mode(st_mode); 638else{ 639/* If there is an existing entry, pick the mode bits and type 640 * from it, otherwise assume unexecutable regular file. 641 */ 642struct cache_entry *ent; 643int pos =index_name_pos_also_unmerged(istate, path, namelen); 644 645 ent = (0<= pos) ? istate->cache[pos] : NULL; 646 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 647} 648 649/* When core.ignorecase=true, determine if a directory of the same name but differing 650 * case already exists within the Git repository. If it does, ensure the directory 651 * case of the file being added to the repository matches (is folded into) the existing 652 * entry's directory case. 653 */ 654if(ignore_case) { 655const char*startPtr = ce->name; 656const char*ptr = startPtr; 657while(*ptr) { 658while(*ptr && *ptr !='/') 659++ptr; 660if(*ptr =='/') { 661struct cache_entry *foundce; 662++ptr; 663 foundce =index_dir_exists(istate, ce->name, ptr - ce->name -1); 664if(foundce) { 665memcpy((void*)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); 666 startPtr = ptr; 667} 668} 669} 670} 671 672 alias =index_file_exists(istate, ce->name,ce_namelen(ce), ignore_case); 673if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 674/* Nothing changed, really */ 675free(ce); 676if(!S_ISGITLINK(alias->ce_mode)) 677ce_mark_uptodate(alias); 678 alias->ce_flags |= CE_ADDED; 679return0; 680} 681if(!intent_only) { 682if(index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) 683returnerror("unable to index file%s", path); 684}else 685set_object_name_for_intent_to_add_entry(ce); 686 687if(ignore_case && alias &&different_name(ce, alias)) 688 ce =create_alias_ce(istate, ce, alias); 689 ce->ce_flags |= CE_ADDED; 690 691/* It was suspected to be racily clean, but it turns out to be Ok */ 692 was_same = (alias && 693!ce_stage(alias) && 694!hashcmp(alias->sha1, ce->sha1) && 695 ce->ce_mode == alias->ce_mode); 696 697if(pretend) 698; 699else if(add_index_entry(istate, ce, add_option)) 700returnerror("unable to add%sto index",path); 701if(verbose && !was_same) 702printf("add '%s'\n", path); 703return0; 704} 705 706intadd_file_to_index(struct index_state *istate,const char*path,int flags) 707{ 708struct stat st; 709if(lstat(path, &st)) 710die_errno("unable to stat '%s'", path); 711returnadd_to_index(istate, path, &st, flags); 712} 713 714struct cache_entry *make_cache_entry(unsigned int mode, 715const unsigned char*sha1,const char*path,int stage, 716unsigned int refresh_options) 717{ 718int size, len; 719struct cache_entry *ce; 720 721if(!verify_path(path)) { 722error("Invalid path '%s'", path); 723return NULL; 724} 725 726 len =strlen(path); 727 size =cache_entry_size(len); 728 ce =xcalloc(1, size); 729 730hashcpy(ce->sha1, sha1); 731memcpy(ce->name, path, len); 732 ce->ce_flags =create_ce_flags(stage); 733 ce->ce_namelen = len; 734 ce->ce_mode =create_ce_mode(mode); 735 736returnrefresh_cache_entry(ce, refresh_options); 737} 738 739intce_same_name(const struct cache_entry *a,const struct cache_entry *b) 740{ 741int len =ce_namelen(a); 742returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 743} 744 745/* 746 * We fundamentally don't like some paths: we don't want 747 * dot or dot-dot anywhere, and for obvious reasons don't 748 * want to recurse into ".git" either. 749 * 750 * Also, we don't want double slashes or slashes at the 751 * end that can make pathnames ambiguous. 752 */ 753static intverify_dotfile(const char*rest) 754{ 755/* 756 * The first character was '.', but that 757 * has already been discarded, we now test 758 * the rest. 759 */ 760 761/* "." is not allowed */ 762if(*rest =='\0'||is_dir_sep(*rest)) 763return0; 764 765switch(*rest) { 766/* 767 * ".git" followed by NUL or slash is bad. This 768 * shares the path end test with the ".." case. 769 */ 770case'g': 771if(rest[1] !='i') 772break; 773if(rest[2] !='t') 774break; 775 rest +=2; 776/* fallthrough */ 777case'.': 778if(rest[1] =='\0'||is_dir_sep(rest[1])) 779return0; 780} 781return1; 782} 783 784intverify_path(const char*path) 785{ 786char c; 787 788if(has_dos_drive_prefix(path)) 789return0; 790 791goto inside; 792for(;;) { 793if(!c) 794return1; 795if(is_dir_sep(c)) { 796inside: 797 c = *path++; 798if((c =='.'&& !verify_dotfile(path)) || 799is_dir_sep(c) || c =='\0') 800return0; 801} 802 c = *path++; 803} 804} 805 806/* 807 * Do we have another file that has the beginning components being a 808 * proper superset of the name we're trying to add? 809 */ 810static inthas_file_name(struct index_state *istate, 811const struct cache_entry *ce,int pos,int ok_to_replace) 812{ 813int retval =0; 814int len =ce_namelen(ce); 815int stage =ce_stage(ce); 816const char*name = ce->name; 817 818while(pos < istate->cache_nr) { 819struct cache_entry *p = istate->cache[pos++]; 820 821if(len >=ce_namelen(p)) 822break; 823if(memcmp(name, p->name, len)) 824break; 825if(ce_stage(p) != stage) 826continue; 827if(p->name[len] !='/') 828continue; 829if(p->ce_flags & CE_REMOVE) 830continue; 831 retval = -1; 832if(!ok_to_replace) 833break; 834remove_index_entry_at(istate, --pos); 835} 836return retval; 837} 838 839/* 840 * Do we have another file with a pathname that is a proper 841 * subset of the name we're trying to add? 842 */ 843static inthas_dir_name(struct index_state *istate, 844const struct cache_entry *ce,int pos,int ok_to_replace) 845{ 846int retval =0; 847int stage =ce_stage(ce); 848const char*name = ce->name; 849const char*slash = name +ce_namelen(ce); 850 851for(;;) { 852int len; 853 854for(;;) { 855if(*--slash =='/') 856break; 857if(slash <= ce->name) 858return retval; 859} 860 len = slash - name; 861 862 pos =index_name_stage_pos(istate, name, len, stage); 863if(pos >=0) { 864/* 865 * Found one, but not so fast. This could 866 * be a marker that says "I was here, but 867 * I am being removed". Such an entry is 868 * not a part of the resulting tree, and 869 * it is Ok to have a directory at the same 870 * path. 871 */ 872if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 873 retval = -1; 874if(!ok_to_replace) 875break; 876remove_index_entry_at(istate, pos); 877continue; 878} 879} 880else 881 pos = -pos-1; 882 883/* 884 * Trivial optimization: if we find an entry that 885 * already matches the sub-directory, then we know 886 * we're ok, and we can exit. 887 */ 888while(pos < istate->cache_nr) { 889struct cache_entry *p = istate->cache[pos]; 890if((ce_namelen(p) <= len) || 891(p->name[len] !='/') || 892memcmp(p->name, name, len)) 893break;/* not our subdirectory */ 894if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 895/* 896 * p is at the same stage as our entry, and 897 * is a subdirectory of what we are looking 898 * at, so we cannot have conflicts at our 899 * level or anything shorter. 900 */ 901return retval; 902 pos++; 903} 904} 905return retval; 906} 907 908/* We may be in a situation where we already have path/file and path 909 * is being added, or we already have path and path/file is being 910 * added. Either one would result in a nonsense tree that has path 911 * twice when git-write-tree tries to write it out. Prevent it. 912 * 913 * If ok-to-replace is specified, we remove the conflicting entries 914 * from the cache so the caller should recompute the insert position. 915 * When this happens, we return non-zero. 916 */ 917static intcheck_file_directory_conflict(struct index_state *istate, 918const struct cache_entry *ce, 919int pos,int ok_to_replace) 920{ 921int retval; 922 923/* 924 * When ce is an "I am going away" entry, we allow it to be added 925 */ 926if(ce->ce_flags & CE_REMOVE) 927return0; 928 929/* 930 * We check if the path is a sub-path of a subsequent pathname 931 * first, since removing those will not change the position 932 * in the array. 933 */ 934 retval =has_file_name(istate, ce, pos, ok_to_replace); 935 936/* 937 * Then check if the path might have a clashing sub-directory 938 * before it. 939 */ 940return retval +has_dir_name(istate, ce, pos, ok_to_replace); 941} 942 943static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 944{ 945int pos; 946int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 947int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 948int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 949int new_only = option & ADD_CACHE_NEW_ONLY; 950 951cache_tree_invalidate_path(istate, ce->name); 952 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 953 954/* existing match? Just replace it. */ 955if(pos >=0) { 956if(!new_only) 957replace_index_entry(istate, pos, ce); 958return0; 959} 960 pos = -pos-1; 961 962/* 963 * Inserting a merged entry ("stage 0") into the index 964 * will always replace all non-merged entries.. 965 */ 966if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 967while(ce_same_name(istate->cache[pos], ce)) { 968 ok_to_add =1; 969if(!remove_index_entry_at(istate, pos)) 970break; 971} 972} 973 974if(!ok_to_add) 975return-1; 976if(!verify_path(ce->name)) 977returnerror("Invalid path '%s'", ce->name); 978 979if(!skip_df_check && 980check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 981if(!ok_to_replace) 982returnerror("'%s' appears as both a file and as a directory", 983 ce->name); 984 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 985 pos = -pos-1; 986} 987return pos +1; 988} 989 990intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 991{ 992int pos; 993 994if(option & ADD_CACHE_JUST_APPEND) 995 pos = istate->cache_nr; 996else{ 997int ret; 998 ret =add_index_entry_with_check(istate, ce, option); 999if(ret <=0)1000return ret;1001 pos = ret -1;1002}10031004/* Make sure the array is big enough .. */1005ALLOC_GROW(istate->cache, istate->cache_nr +1, istate->cache_alloc);10061007/* Add it in.. */1008 istate->cache_nr++;1009if(istate->cache_nr > pos +1)1010memmove(istate->cache + pos +1,1011 istate->cache + pos,1012(istate->cache_nr - pos -1) *sizeof(ce));1013set_index_entry(istate, pos, ce);1014 istate->cache_changed |= CE_ENTRY_ADDED;1015return0;1016}10171018/*1019 * "refresh" does not calculate a new sha1 file or bring the1020 * cache up-to-date for mode/content changes. But what it1021 * _does_ do is to "re-match" the stat information of a file1022 * with the cache, so that you can refresh the cache for a1023 * file that hasn't been changed but where the stat entry is1024 * out of date.1025 *1026 * For example, you'd want to do this after doing a "git-read-tree",1027 * to link up the stat cache details with the proper files.1028 */1029static struct cache_entry *refresh_cache_ent(struct index_state *istate,1030struct cache_entry *ce,1031unsigned int options,int*err,1032int*changed_ret)1033{1034struct stat st;1035struct cache_entry *updated;1036int changed, size;1037int refresh = options & CE_MATCH_REFRESH;1038int ignore_valid = options & CE_MATCH_IGNORE_VALID;1039int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;1040int ignore_missing = options & CE_MATCH_IGNORE_MISSING;10411042if(!refresh ||ce_uptodate(ce))1043return ce;10441045/*1046 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1047 * that the change to the work tree does not matter and told1048 * us not to worry.1049 */1050if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1051ce_mark_uptodate(ce);1052return ce;1053}1054if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1055ce_mark_uptodate(ce);1056return ce;1057}10581059if(lstat(ce->name, &st) <0) {1060if(ignore_missing && errno == ENOENT)1061return ce;1062if(err)1063*err = errno;1064return NULL;1065}10661067 changed =ie_match_stat(istate, ce, &st, options);1068if(changed_ret)1069*changed_ret = changed;1070if(!changed) {1071/*1072 * The path is unchanged. If we were told to ignore1073 * valid bit, then we did the actual stat check and1074 * found that the entry is unmodified. If the entry1075 * is not marked VALID, this is the place to mark it1076 * valid again, under "assume unchanged" mode.1077 */1078if(ignore_valid && assume_unchanged &&1079!(ce->ce_flags & CE_VALID))1080;/* mark this one VALID again */1081else{1082/*1083 * We do not mark the index itself "modified"1084 * because CE_UPTODATE flag is in-core only;1085 * we are not going to write this change out.1086 */1087if(!S_ISGITLINK(ce->ce_mode))1088ce_mark_uptodate(ce);1089return ce;1090}1091}10921093if(ie_modified(istate, ce, &st, options)) {1094if(err)1095*err = EINVAL;1096return NULL;1097}10981099 size =ce_size(ce);1100 updated =xmalloc(size);1101memcpy(updated, ce, size);1102fill_stat_cache_info(updated, &st);1103/*1104 * If ignore_valid is not set, we should leave CE_VALID bit1105 * alone. Otherwise, paths marked with --no-assume-unchanged1106 * (i.e. things to be edited) will reacquire CE_VALID bit1107 * automatically, which is not really what we want.1108 */1109if(!ignore_valid && assume_unchanged &&1110!(ce->ce_flags & CE_VALID))1111 updated->ce_flags &= ~CE_VALID;11121113/* istate->cache_changed is updated in the caller */1114return updated;1115}11161117static voidshow_file(const char* fmt,const char* name,int in_porcelain,1118int* first,const char*header_msg)1119{1120if(in_porcelain && *first && header_msg) {1121printf("%s\n", header_msg);1122*first =0;1123}1124printf(fmt, name);1125}11261127intrefresh_index(struct index_state *istate,unsigned int flags,1128const struct pathspec *pathspec,1129char*seen,const char*header_msg)1130{1131int i;1132int has_errors =0;1133int really = (flags & REFRESH_REALLY) !=0;1134int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1135int quiet = (flags & REFRESH_QUIET) !=0;1136int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1137int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1138int first =1;1139int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1140unsigned int options = (CE_MATCH_REFRESH |1141(really ? CE_MATCH_IGNORE_VALID :0) |1142(not_new ? CE_MATCH_IGNORE_MISSING :0));1143const char*modified_fmt;1144const char*deleted_fmt;1145const char*typechange_fmt;1146const char*added_fmt;1147const char*unmerged_fmt;11481149 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1150 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1151 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1152 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1153 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1154for(i =0; i < istate->cache_nr; i++) {1155struct cache_entry *ce, *new;1156int cache_errno =0;1157int changed =0;1158int filtered =0;11591160 ce = istate->cache[i];1161if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1162continue;11631164if(pathspec && !ce_path_match(ce, pathspec, seen))1165 filtered =1;11661167if(ce_stage(ce)) {1168while((i < istate->cache_nr) &&1169!strcmp(istate->cache[i]->name, ce->name))1170 i++;1171 i--;1172if(allow_unmerged)1173continue;1174if(!filtered)1175show_file(unmerged_fmt, ce->name, in_porcelain,1176&first, header_msg);1177 has_errors =1;1178continue;1179}11801181if(filtered)1182continue;11831184new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1185if(new== ce)1186continue;1187if(!new) {1188const char*fmt;11891190if(really && cache_errno == EINVAL) {1191/* If we are doing --really-refresh that1192 * means the index is not valid anymore.1193 */1194 ce->ce_flags &= ~CE_VALID;1195 istate->cache_changed |= CE_ENTRY_CHANGED;1196}1197if(quiet)1198continue;11991200if(cache_errno == ENOENT)1201 fmt = deleted_fmt;1202else if(ce->ce_flags & CE_INTENT_TO_ADD)1203 fmt = added_fmt;/* must be before other checks */1204else if(changed & TYPE_CHANGED)1205 fmt = typechange_fmt;1206else1207 fmt = modified_fmt;1208show_file(fmt,1209 ce->name, in_porcelain, &first, header_msg);1210 has_errors =1;1211continue;1212}12131214replace_index_entry(istate, i,new);1215}1216return has_errors;1217}12181219static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,1220unsigned int options)1221{1222returnrefresh_cache_ent(&the_index, ce, options, NULL, NULL);1223}122412251226/*****************************************************************1227 * Index File I/O1228 *****************************************************************/12291230#define INDEX_FORMAT_DEFAULT 312311232static intindex_format_config(const char*var,const char*value,void*cb)1233{1234unsigned int*version = cb;1235if(!strcmp(var,"index.version")) {1236*version =git_config_int(var, value);1237return0;1238}1239return1;1240}12411242static unsigned intget_index_format_default(void)1243{1244char*envversion =getenv("GIT_INDEX_VERSION");1245char*endp;1246unsigned int version = INDEX_FORMAT_DEFAULT;12471248if(!envversion) {1249git_config(index_format_config, &version);1250if(version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1251warning(_("index.version set, but the value is invalid.\n"1252"Using version%i"), INDEX_FORMAT_DEFAULT);1253return INDEX_FORMAT_DEFAULT;1254}1255return version;1256}12571258 version =strtoul(envversion, &endp,10);1259if(*endp ||1260 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1261warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"1262"Using version%i"), INDEX_FORMAT_DEFAULT);1263 version = INDEX_FORMAT_DEFAULT;1264}1265return version;1266}12671268/*1269 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1270 * Again - this is just a (very strong in practice) heuristic that1271 * the inode hasn't changed.1272 *1273 * We save the fields in big-endian order to allow using the1274 * index file over NFS transparently.1275 */1276struct ondisk_cache_entry {1277struct cache_time ctime;1278struct cache_time mtime;1279uint32_t dev;1280uint32_t ino;1281uint32_t mode;1282uint32_t uid;1283uint32_t gid;1284uint32_t size;1285unsigned char sha1[20];1286uint16_t flags;1287char name[FLEX_ARRAY];/* more */1288};12891290/*1291 * This struct is used when CE_EXTENDED bit is 11292 * The struct must match ondisk_cache_entry exactly from1293 * ctime till flags1294 */1295struct ondisk_cache_entry_extended {1296struct cache_time ctime;1297struct cache_time mtime;1298uint32_t dev;1299uint32_t ino;1300uint32_t mode;1301uint32_t uid;1302uint32_t gid;1303uint32_t size;1304unsigned char sha1[20];1305uint16_t flags;1306uint16_t flags2;1307char name[FLEX_ARRAY];/* more */1308};13091310/* These are only used for v3 or lower */1311#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1312#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1313#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1314#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1315 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1316 ondisk_cache_entry_size(ce_namelen(ce)))13171318static intverify_hdr(struct cache_header *hdr,unsigned long size)1319{1320 git_SHA_CTX c;1321unsigned char sha1[20];1322int hdr_version;13231324if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1325returnerror("bad signature");1326 hdr_version =ntohl(hdr->hdr_version);1327if(hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)1328returnerror("bad index version%d", hdr_version);1329git_SHA1_Init(&c);1330git_SHA1_Update(&c, hdr, size -20);1331git_SHA1_Final(sha1, &c);1332if(hashcmp(sha1, (unsigned char*)hdr + size -20))1333returnerror("bad index file sha1 signature");1334return0;1335}13361337static intread_index_extension(struct index_state *istate,1338const char*ext,void*data,unsigned long sz)1339{1340switch(CACHE_EXT(ext)) {1341case CACHE_EXT_TREE:1342 istate->cache_tree =cache_tree_read(data, sz);1343break;1344case CACHE_EXT_RESOLVE_UNDO:1345 istate->resolve_undo =resolve_undo_read(data, sz);1346break;1347case CACHE_EXT_LINK:1348if(read_link_extension(istate, data, sz))1349return-1;1350break;1351default:1352if(*ext <'A'||'Z'< *ext)1353returnerror("index uses %.4s extension, which we do not understand",1354 ext);1355fprintf(stderr,"ignoring %.4s extension\n", ext);1356break;1357}1358return0;1359}13601361intread_index(struct index_state *istate)1362{1363returnread_index_from(istate,get_index_file());1364}13651366static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1367unsigned int flags,1368const char*name,1369size_t len)1370{1371struct cache_entry *ce =xmalloc(cache_entry_size(len));13721373 ce->ce_stat_data.sd_ctime.sec =get_be32(&ondisk->ctime.sec);1374 ce->ce_stat_data.sd_mtime.sec =get_be32(&ondisk->mtime.sec);1375 ce->ce_stat_data.sd_ctime.nsec =get_be32(&ondisk->ctime.nsec);1376 ce->ce_stat_data.sd_mtime.nsec =get_be32(&ondisk->mtime.nsec);1377 ce->ce_stat_data.sd_dev =get_be32(&ondisk->dev);1378 ce->ce_stat_data.sd_ino =get_be32(&ondisk->ino);1379 ce->ce_mode =get_be32(&ondisk->mode);1380 ce->ce_stat_data.sd_uid =get_be32(&ondisk->uid);1381 ce->ce_stat_data.sd_gid =get_be32(&ondisk->gid);1382 ce->ce_stat_data.sd_size =get_be32(&ondisk->size);1383 ce->ce_flags = flags & ~CE_NAMEMASK;1384 ce->ce_namelen = len;1385 ce->index =0;1386hashcpy(ce->sha1, ondisk->sha1);1387memcpy(ce->name, name, len);1388 ce->name[len] ='\0';1389return ce;1390}13911392/*1393 * Adjacent cache entries tend to share the leading paths, so it makes1394 * sense to only store the differences in later entries. In the v41395 * on-disk format of the index, each on-disk cache entry stores the1396 * number of bytes to be stripped from the end of the previous name,1397 * and the bytes to append to the result, to come up with its name.1398 */1399static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1400{1401const unsigned char*ep, *cp = (const unsigned char*)cp_;1402size_t len =decode_varint(&cp);14031404if(name->len < len)1405die("malformed name field in the index");1406strbuf_remove(name, name->len - len, len);1407for(ep = cp; *ep; ep++)1408;/* find the end */1409strbuf_add(name, cp, ep - cp);1410return(const char*)ep +1- cp_;1411}14121413static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1414unsigned long*ent_size,1415struct strbuf *previous_name)1416{1417struct cache_entry *ce;1418size_t len;1419const char*name;1420unsigned int flags;14211422/* On-disk flags are just 16 bits */1423 flags =get_be16(&ondisk->flags);1424 len = flags & CE_NAMEMASK;14251426if(flags & CE_EXTENDED) {1427struct ondisk_cache_entry_extended *ondisk2;1428int extended_flags;1429 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1430 extended_flags =get_be16(&ondisk2->flags2) <<16;1431/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1432if(extended_flags & ~CE_EXTENDED_FLAGS)1433die("Unknown index entry format%08x", extended_flags);1434 flags |= extended_flags;1435 name = ondisk2->name;1436}1437else1438 name = ondisk->name;14391440if(!previous_name) {1441/* v3 and earlier */1442if(len == CE_NAMEMASK)1443 len =strlen(name);1444 ce =cache_entry_from_ondisk(ondisk, flags, name, len);14451446*ent_size =ondisk_ce_size(ce);1447}else{1448unsigned long consumed;1449 consumed =expand_name_field(previous_name, name);1450 ce =cache_entry_from_ondisk(ondisk, flags,1451 previous_name->buf,1452 previous_name->len);14531454*ent_size = (name - ((char*)ondisk)) + consumed;1455}1456return ce;1457}14581459/* remember to discard_cache() before reading a different cache! */1460static intdo_read_index(struct index_state *istate,const char*path,1461int must_exist)1462{1463int fd, i;1464struct stat st;1465unsigned long src_offset;1466struct cache_header *hdr;1467void*mmap;1468size_t mmap_size;1469struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;14701471if(istate->initialized)1472return istate->cache_nr;14731474 istate->timestamp.sec =0;1475 istate->timestamp.nsec =0;1476 fd =open(path, O_RDONLY);1477if(fd <0) {1478if(!must_exist && errno == ENOENT)1479return0;1480die_errno("%s: index file open failed", path);1481}14821483if(fstat(fd, &st))1484die_errno("cannot stat the open index");14851486 mmap_size =xsize_t(st.st_size);1487if(mmap_size <sizeof(struct cache_header) +20)1488die("index file smaller than expected");14891490 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1491if(mmap == MAP_FAILED)1492die_errno("unable to map index file");1493close(fd);14941495 hdr = mmap;1496if(verify_hdr(hdr, mmap_size) <0)1497goto unmap;14981499hashcpy(istate->sha1, (const unsigned char*)hdr + mmap_size -20);1500 istate->version =ntohl(hdr->hdr_version);1501 istate->cache_nr =ntohl(hdr->hdr_entries);1502 istate->cache_alloc =alloc_nr(istate->cache_nr);1503 istate->cache =xcalloc(istate->cache_alloc,sizeof(*istate->cache));1504 istate->initialized =1;15051506if(istate->version ==4)1507 previous_name = &previous_name_buf;1508else1509 previous_name = NULL;15101511 src_offset =sizeof(*hdr);1512for(i =0; i < istate->cache_nr; i++) {1513struct ondisk_cache_entry *disk_ce;1514struct cache_entry *ce;1515unsigned long consumed;15161517 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1518 ce =create_from_disk(disk_ce, &consumed, previous_name);1519set_index_entry(istate, i, ce);15201521 src_offset += consumed;1522}1523strbuf_release(&previous_name_buf);1524 istate->timestamp.sec = st.st_mtime;1525 istate->timestamp.nsec =ST_MTIME_NSEC(st);15261527while(src_offset <= mmap_size -20-8) {1528/* After an array of active_nr index entries,1529 * there can be arbitrary number of extended1530 * sections, each of which is prefixed with1531 * extension name (4-byte) and section length1532 * in 4-byte network byte order.1533 */1534uint32_t extsize;1535memcpy(&extsize, (char*)mmap + src_offset +4,4);1536 extsize =ntohl(extsize);1537if(read_index_extension(istate,1538(const char*) mmap + src_offset,1539(char*) mmap + src_offset +8,1540 extsize) <0)1541goto unmap;1542 src_offset +=8;1543 src_offset += extsize;1544}1545munmap(mmap, mmap_size);1546return istate->cache_nr;15471548unmap:1549munmap(mmap, mmap_size);1550die("index file corrupt");1551}15521553intread_index_from(struct index_state *istate,const char*path)1554{1555struct split_index *split_index;1556int ret;15571558/* istate->initialized covers both .git/index and .git/sharedindex.xxx */1559if(istate->initialized)1560return istate->cache_nr;15611562 ret =do_read_index(istate, path,0);1563 split_index = istate->split_index;1564if(!split_index)1565return ret;15661567if(is_null_sha1(split_index->base_sha1))1568return ret;1569if(istate->cache_nr)1570die("index in split-index mode must contain no entries");15711572if(split_index->base)1573discard_index(split_index->base);1574else1575 split_index->base =xcalloc(1,sizeof(*split_index->base));1576 ret =do_read_index(split_index->base,1577git_path("sharedindex.%s",1578sha1_to_hex(split_index->base_sha1)),1);1579if(hashcmp(split_index->base_sha1, split_index->base->sha1))1580die("broken index, expect%sin%s, got%s",1581sha1_to_hex(split_index->base_sha1),1582git_path("sharedindex.%s",1583sha1_to_hex(split_index->base_sha1)),1584sha1_to_hex(split_index->base->sha1));1585merge_base_index(istate);1586return ret;1587}15881589intis_index_unborn(struct index_state *istate)1590{1591return(!istate->cache_nr && !istate->timestamp.sec);1592}15931594intdiscard_index(struct index_state *istate)1595{1596int i;15971598for(i =0; i < istate->cache_nr; i++) {1599if(istate->cache[i]->index &&1600 istate->split_index &&1601 istate->split_index->base &&1602 istate->cache[i]->index <= istate->split_index->base->cache_nr &&1603 istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index -1])1604continue;1605free(istate->cache[i]);1606}1607resolve_undo_clear_index(istate);1608 istate->cache_nr =0;1609 istate->cache_changed =0;1610 istate->timestamp.sec =0;1611 istate->timestamp.nsec =0;1612free_name_hash(istate);1613cache_tree_free(&(istate->cache_tree));1614 istate->initialized =0;1615free(istate->cache);1616 istate->cache = NULL;1617 istate->cache_alloc =0;1618discard_split_index(istate);1619return0;1620}16211622intunmerged_index(const struct index_state *istate)1623{1624int i;1625for(i =0; i < istate->cache_nr; i++) {1626if(ce_stage(istate->cache[i]))1627return1;1628}1629return0;1630}16311632#define WRITE_BUFFER_SIZE 81921633static unsigned char write_buffer[WRITE_BUFFER_SIZE];1634static unsigned long write_buffer_len;16351636static intce_write_flush(git_SHA_CTX *context,int fd)1637{1638unsigned int buffered = write_buffer_len;1639if(buffered) {1640git_SHA1_Update(context, write_buffer, buffered);1641if(write_in_full(fd, write_buffer, buffered) != buffered)1642return-1;1643 write_buffer_len =0;1644}1645return0;1646}16471648static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1649{1650while(len) {1651unsigned int buffered = write_buffer_len;1652unsigned int partial = WRITE_BUFFER_SIZE - buffered;1653if(partial > len)1654 partial = len;1655memcpy(write_buffer + buffered, data, partial);1656 buffered += partial;1657if(buffered == WRITE_BUFFER_SIZE) {1658 write_buffer_len = buffered;1659if(ce_write_flush(context, fd))1660return-1;1661 buffered =0;1662}1663 write_buffer_len = buffered;1664 len -= partial;1665 data = (char*) data + partial;1666}1667return0;1668}16691670static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1671unsigned int ext,unsigned int sz)1672{1673 ext =htonl(ext);1674 sz =htonl(sz);1675return((ce_write(context, fd, &ext,4) <0) ||1676(ce_write(context, fd, &sz,4) <0)) ? -1:0;1677}16781679static intce_flush(git_SHA_CTX *context,int fd,unsigned char*sha1)1680{1681unsigned int left = write_buffer_len;16821683if(left) {1684 write_buffer_len =0;1685git_SHA1_Update(context, write_buffer, left);1686}16871688/* Flush first if not enough space for SHA1 signature */1689if(left +20> WRITE_BUFFER_SIZE) {1690if(write_in_full(fd, write_buffer, left) != left)1691return-1;1692 left =0;1693}16941695/* Append the SHA1 signature at the end */1696git_SHA1_Final(write_buffer + left, context);1697hashcpy(sha1, write_buffer + left);1698 left +=20;1699return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1700}17011702static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1703{1704/*1705 * The only thing we care about in this function is to smudge the1706 * falsely clean entry due to touch-update-touch race, so we leave1707 * everything else as they are. We are called for entries whose1708 * ce_stat_data.sd_mtime match the index file mtime.1709 *1710 * Note that this actually does not do much for gitlinks, for1711 * which ce_match_stat_basic() always goes to the actual1712 * contents. The caller checks with is_racy_timestamp() which1713 * always says "no" for gitlinks, so we are not called for them ;-)1714 */1715struct stat st;17161717if(lstat(ce->name, &st) <0)1718return;1719if(ce_match_stat_basic(ce, &st))1720return;1721if(ce_modified_check_fs(ce, &st)) {1722/* This is "racily clean"; smudge it. Note that this1723 * is a tricky code. At first glance, it may appear1724 * that it can break with this sequence:1725 *1726 * $ echo xyzzy >frotz1727 * $ git-update-index --add frotz1728 * $ : >frotz1729 * $ sleep 31730 * $ echo filfre >nitfol1731 * $ git-update-index --add nitfol1732 *1733 * but it does not. When the second update-index runs,1734 * it notices that the entry "frotz" has the same timestamp1735 * as index, and if we were to smudge it by resetting its1736 * size to zero here, then the object name recorded1737 * in index is the 6-byte file but the cached stat information1738 * becomes zero --- which would then match what we would1739 * obtain from the filesystem next time we stat("frotz").1740 *1741 * However, the second update-index, before calling1742 * this function, notices that the cached size is 61743 * bytes and what is on the filesystem is an empty1744 * file, and never calls us, so the cached size information1745 * for "frotz" stays 6 which does not match the filesystem.1746 */1747 ce->ce_stat_data.sd_size =0;1748}1749}17501751/* Copy miscellaneous fields but not the name */1752static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1753struct cache_entry *ce)1754{1755short flags;17561757 ondisk->ctime.sec =htonl(ce->ce_stat_data.sd_ctime.sec);1758 ondisk->mtime.sec =htonl(ce->ce_stat_data.sd_mtime.sec);1759 ondisk->ctime.nsec =htonl(ce->ce_stat_data.sd_ctime.nsec);1760 ondisk->mtime.nsec =htonl(ce->ce_stat_data.sd_mtime.nsec);1761 ondisk->dev =htonl(ce->ce_stat_data.sd_dev);1762 ondisk->ino =htonl(ce->ce_stat_data.sd_ino);1763 ondisk->mode =htonl(ce->ce_mode);1764 ondisk->uid =htonl(ce->ce_stat_data.sd_uid);1765 ondisk->gid =htonl(ce->ce_stat_data.sd_gid);1766 ondisk->size =htonl(ce->ce_stat_data.sd_size);1767hashcpy(ondisk->sha1, ce->sha1);17681769 flags = ce->ce_flags & ~CE_NAMEMASK;1770 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK :ce_namelen(ce));1771 ondisk->flags =htons(flags);1772if(ce->ce_flags & CE_EXTENDED) {1773struct ondisk_cache_entry_extended *ondisk2;1774 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1775 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1776return ondisk2->name;1777}1778else{1779return ondisk->name;1780}1781}17821783static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce,1784struct strbuf *previous_name)1785{1786int size;1787struct ondisk_cache_entry *ondisk;1788char*name;1789int result;17901791if(!previous_name) {1792 size =ondisk_ce_size(ce);1793 ondisk =xcalloc(1, size);1794 name =copy_cache_entry_to_ondisk(ondisk, ce);1795memcpy(name, ce->name,ce_namelen(ce));1796}else{1797int common, to_remove, prefix_size;1798unsigned char to_remove_vi[16];1799for(common =0;1800(ce->name[common] &&1801 common < previous_name->len &&1802 ce->name[common] == previous_name->buf[common]);1803 common++)1804;/* still matching */1805 to_remove = previous_name->len - common;1806 prefix_size =encode_varint(to_remove, to_remove_vi);18071808if(ce->ce_flags & CE_EXTENDED)1809 size =offsetof(struct ondisk_cache_entry_extended, name);1810else1811 size =offsetof(struct ondisk_cache_entry, name);1812 size += prefix_size + (ce_namelen(ce) - common +1);18131814 ondisk =xcalloc(1, size);1815 name =copy_cache_entry_to_ondisk(ondisk, ce);1816memcpy(name, to_remove_vi, prefix_size);1817memcpy(name + prefix_size, ce->name + common,ce_namelen(ce) - common);18181819strbuf_splice(previous_name, common, to_remove,1820 ce->name + common,ce_namelen(ce) - common);1821}18221823 result =ce_write(c, fd, ondisk, size);1824free(ondisk);1825return result;1826}18271828static inthas_racy_timestamp(struct index_state *istate)1829{1830int entries = istate->cache_nr;1831int i;18321833for(i =0; i < entries; i++) {1834struct cache_entry *ce = istate->cache[i];1835if(is_racy_timestamp(istate, ce))1836return1;1837}1838return0;1839}18401841/*1842 * Opportunistically update the index but do not complain if we can't1843 */1844voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1845{1846if((istate->cache_changed ||has_racy_timestamp(istate)) &&1847write_locked_index(istate, lockfile, COMMIT_LOCK))1848rollback_lock_file(lockfile);1849}18501851static intdo_write_index(struct index_state *istate,int newfd)1852{1853 git_SHA_CTX c;1854struct cache_header hdr;1855int i, err, removed, extended, hdr_version;1856struct cache_entry **cache = istate->cache;1857int entries = istate->cache_nr;1858struct stat st;1859struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;18601861for(i = removed = extended =0; i < entries; i++) {1862if(cache[i]->ce_flags & CE_REMOVE)1863 removed++;18641865/* reduce extended entries if possible */1866 cache[i]->ce_flags &= ~CE_EXTENDED;1867if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1868 extended++;1869 cache[i]->ce_flags |= CE_EXTENDED;1870}1871}18721873if(!istate->version)1874 istate->version =get_index_format_default();18751876/* demote version 3 to version 2 when the latter suffices */1877if(istate->version ==3|| istate->version ==2)1878 istate->version = extended ?3:2;18791880 hdr_version = istate->version;18811882 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1883 hdr.hdr_version =htonl(hdr_version);1884 hdr.hdr_entries =htonl(entries - removed);18851886git_SHA1_Init(&c);1887if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1888return-1;18891890 previous_name = (hdr_version ==4) ? &previous_name_buf : NULL;1891for(i =0; i < entries; i++) {1892struct cache_entry *ce = cache[i];1893if(ce->ce_flags & CE_REMOVE)1894continue;1895if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1896ce_smudge_racily_clean_entry(ce);1897if(is_null_sha1(ce->sha1)) {1898static const char msg[] ="cache entry has null sha1:%s";1899static int allow = -1;19001901if(allow <0)1902 allow =git_env_bool("GIT_ALLOW_NULL_SHA1",0);1903if(allow)1904warning(msg, ce->name);1905else1906returnerror(msg, ce->name);1907}1908if(ce_write_entry(&c, newfd, ce, previous_name) <0)1909return-1;1910}1911strbuf_release(&previous_name_buf);19121913/* Write extension data here */1914if(istate->split_index) {1915struct strbuf sb = STRBUF_INIT;19161917 err =write_link_extension(&sb, istate) <0||1918write_index_ext_header(&c, newfd, CACHE_EXT_LINK,1919 sb.len) <0||1920ce_write(&c, newfd, sb.buf, sb.len) <0;1921strbuf_release(&sb);1922if(err)1923return-1;1924}1925if(istate->cache_tree) {1926struct strbuf sb = STRBUF_INIT;19271928cache_tree_write(&sb, istate->cache_tree);1929 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01930||ce_write(&c, newfd, sb.buf, sb.len) <0;1931strbuf_release(&sb);1932if(err)1933return-1;1934}1935if(istate->resolve_undo) {1936struct strbuf sb = STRBUF_INIT;19371938resolve_undo_write(&sb, istate->resolve_undo);1939 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,1940 sb.len) <01941||ce_write(&c, newfd, sb.buf, sb.len) <0;1942strbuf_release(&sb);1943if(err)1944return-1;1945}19461947if(ce_flush(&c, newfd, istate->sha1) ||fstat(newfd, &st))1948return-1;1949 istate->timestamp.sec = (unsigned int)st.st_mtime;1950 istate->timestamp.nsec =ST_MTIME_NSEC(st);1951return0;1952}19531954voidset_alternate_index_output(const char*name)1955{1956 alternate_index_output = name;1957}19581959static intcommit_locked_index(struct lock_file *lk)1960{1961if(alternate_index_output) {1962if(lk->fd >=0&&close_lock_file(lk))1963return-1;1964if(rename(lk->filename, alternate_index_output))1965return-1;1966 lk->filename[0] =0;1967return0;1968}else{1969returncommit_lock_file(lk);1970}1971}19721973static intdo_write_locked_index(struct index_state *istate,struct lock_file *lock,1974unsigned flags)1975{1976int ret =do_write_index(istate, lock->fd);1977if(ret)1978return ret;1979assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=1980(COMMIT_LOCK | CLOSE_LOCK));1981if(flags & COMMIT_LOCK)1982returncommit_locked_index(lock);1983else if(flags & CLOSE_LOCK)1984returnclose_lock_file(lock);1985else1986return ret;1987}19881989static intwrite_split_index(struct index_state *istate,1990struct lock_file *lock,1991unsigned flags)1992{1993int ret;1994prepare_to_write_split_index(istate);1995 ret =do_write_locked_index(istate, lock, flags);1996finish_writing_split_index(istate);1997return ret;1998}19992000intwrite_locked_index(struct index_state *istate,struct lock_file *lock,2001unsigned flags)2002{2003struct split_index *si = istate->split_index;20042005if(!si || (istate->cache_changed & ~EXTMASK)) {2006if(si)2007hashclr(si->base_sha1);2008returndo_write_locked_index(istate, lock, flags);2009}20102011returnwrite_split_index(istate, lock, flags);2012}20132014/*2015 * Read the index file that is potentially unmerged into given2016 * index_state, dropping any unmerged entries. Returns true if2017 * the index is unmerged. Callers who want to refuse to work2018 * from an unmerged state can call this and check its return value,2019 * instead of calling read_cache().2020 */2021intread_index_unmerged(struct index_state *istate)2022{2023int i;2024int unmerged =0;20252026read_index(istate);2027for(i =0; i < istate->cache_nr; i++) {2028struct cache_entry *ce = istate->cache[i];2029struct cache_entry *new_ce;2030int size, len;20312032if(!ce_stage(ce))2033continue;2034 unmerged =1;2035 len =ce_namelen(ce);2036 size =cache_entry_size(len);2037 new_ce =xcalloc(1, size);2038memcpy(new_ce->name, ce->name, len);2039 new_ce->ce_flags =create_ce_flags(0) | CE_CONFLICTED;2040 new_ce->ce_namelen = len;2041 new_ce->ce_mode = ce->ce_mode;2042if(add_index_entry(istate, new_ce,0))2043returnerror("%s: cannot drop to stage #0",2044 new_ce->name);2045 i =index_name_pos(istate, new_ce->name, len);2046}2047return unmerged;2048}20492050/*2051 * Returns 1 if the path is an "other" path with respect to2052 * the index; that is, the path is not mentioned in the index at all,2053 * either as a file, a directory with some files in the index,2054 * or as an unmerged entry.2055 *2056 * We helpfully remove a trailing "/" from directories so that2057 * the output of read_directory can be used as-is.2058 */2059intindex_name_is_other(const struct index_state *istate,const char*name,2060int namelen)2061{2062int pos;2063if(namelen && name[namelen -1] =='/')2064 namelen--;2065 pos =index_name_pos(istate, name, namelen);2066if(0<= pos)2067return0;/* exact match */2068 pos = -pos -1;2069if(pos < istate->cache_nr) {2070struct cache_entry *ce = istate->cache[pos];2071if(ce_namelen(ce) == namelen &&2072!memcmp(ce->name, name, namelen))2073return0;/* Yup, this one exists unmerged */2074}2075return1;2076}20772078void*read_blob_data_from_index(struct index_state *istate,const char*path,unsigned long*size)2079{2080int pos, len;2081unsigned long sz;2082enum object_type type;2083void*data;20842085 len =strlen(path);2086 pos =index_name_pos(istate, path, len);2087if(pos <0) {2088/*2089 * We might be in the middle of a merge, in which2090 * case we would read stage #2 (ours).2091 */2092int i;2093for(i = -pos -1;2094(pos <0&& i < istate->cache_nr &&2095!strcmp(istate->cache[i]->name, path));2096 i++)2097if(ce_stage(istate->cache[i]) ==2)2098 pos = i;2099}2100if(pos <0)2101return NULL;2102 data =read_sha1_file(istate->cache[pos]->sha1, &type, &sz);2103if(!data || type != OBJ_BLOB) {2104free(data);2105return NULL;2106}2107if(size)2108*size = sz;2109return data;2110}21112112voidstat_validity_clear(struct stat_validity *sv)2113{2114free(sv->sd);2115 sv->sd = NULL;2116}21172118intstat_validity_check(struct stat_validity *sv,const char*path)2119{2120struct stat st;21212122if(stat(path, &st) <0)2123return sv->sd == NULL;2124if(!sv->sd)2125return0;2126returnS_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);2127}21282129voidstat_validity_update(struct stat_validity *sv,int fd)2130{2131struct stat st;21322133if(fstat(fd, &st) <0|| !S_ISREG(st.st_mode))2134stat_validity_clear(sv);2135else{2136if(!sv->sd)2137 sv->sd =xcalloc(1,sizeof(struct stat_data));2138fill_stat_data(sv->sd, &st);2139}2140}