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"lockfile.h" 9#include"cache-tree.h" 10#include"refs.h" 11#include"dir.h" 12#include"tree.h" 13#include"commit.h" 14#include"blob.h" 15#include"resolve-undo.h" 16#include"strbuf.h" 17#include"varint.h" 18#include"split-index.h" 19#include"sigchain.h" 20#include"utf8.h" 21 22static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, 23unsigned int options); 24 25/* Mask for the name length in ce_flags in the on-disk index */ 26 27#define CE_NAMEMASK (0x0fff) 28 29/* Index extensions. 30 * 31 * The first letter should be 'A'..'Z' for extensions that are not 32 * necessary for a correct operation (i.e. optimization data). 33 * When new extensions are added that _needs_ to be understood in 34 * order to correctly interpret the index file, pick character that 35 * is outside the range, to cause the reader to abort. 36 */ 37 38#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 39#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 40#define CACHE_EXT_RESOLVE_UNDO 0x52455543/* "REUC" */ 41#define CACHE_EXT_LINK 0x6c696e6b/* "link" */ 42 43/* changes that can be kept in $GIT_DIR/index (basically all extensions) */ 44#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ 45 CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \ 46 SPLIT_INDEX_ORDERED) 47 48struct index_state the_index; 49static const char*alternate_index_output; 50 51static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 52{ 53 istate->cache[nr] = ce; 54add_name_hash(istate, ce); 55} 56 57static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 58{ 59struct cache_entry *old = istate->cache[nr]; 60 61replace_index_entry_in_base(istate, old, ce); 62remove_name_hash(istate, old); 63free(old); 64set_index_entry(istate, nr, ce); 65 ce->ce_flags |= CE_UPDATE_IN_BASE; 66 istate->cache_changed |= CE_ENTRY_CHANGED; 67} 68 69voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 70{ 71struct cache_entry *old = istate->cache[nr], *new; 72int namelen =strlen(new_name); 73 74new=xmalloc(cache_entry_size(namelen)); 75copy_cache_entry(new, old); 76new->ce_flags &= ~CE_HASHED; 77new->ce_namelen = namelen; 78new->index =0; 79memcpy(new->name, new_name, namelen +1); 80 81cache_tree_invalidate_path(istate, old->name); 82remove_index_entry_at(istate, nr); 83add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 84} 85 86voidfill_stat_data(struct stat_data *sd,struct stat *st) 87{ 88 sd->sd_ctime.sec = (unsigned int)st->st_ctime; 89 sd->sd_mtime.sec = (unsigned int)st->st_mtime; 90 sd->sd_ctime.nsec =ST_CTIME_NSEC(*st); 91 sd->sd_mtime.nsec =ST_MTIME_NSEC(*st); 92 sd->sd_dev = st->st_dev; 93 sd->sd_ino = st->st_ino; 94 sd->sd_uid = st->st_uid; 95 sd->sd_gid = st->st_gid; 96 sd->sd_size = st->st_size; 97} 98 99intmatch_stat_data(const struct stat_data *sd,struct stat *st) 100{ 101int changed =0; 102 103if(sd->sd_mtime.sec != (unsigned int)st->st_mtime) 104 changed |= MTIME_CHANGED; 105if(trust_ctime && check_stat && 106 sd->sd_ctime.sec != (unsigned int)st->st_ctime) 107 changed |= CTIME_CHANGED; 108 109#ifdef USE_NSEC 110if(check_stat && sd->sd_mtime.nsec !=ST_MTIME_NSEC(*st)) 111 changed |= MTIME_CHANGED; 112if(trust_ctime && check_stat && 113 sd->sd_ctime.nsec !=ST_CTIME_NSEC(*st)) 114 changed |= CTIME_CHANGED; 115#endif 116 117if(check_stat) { 118if(sd->sd_uid != (unsigned int) st->st_uid || 119 sd->sd_gid != (unsigned int) st->st_gid) 120 changed |= OWNER_CHANGED; 121if(sd->sd_ino != (unsigned int) st->st_ino) 122 changed |= INODE_CHANGED; 123} 124 125#ifdef USE_STDEV 126/* 127 * st_dev breaks on network filesystems where different 128 * clients will have different views of what "device" 129 * the filesystem is on 130 */ 131if(check_stat && sd->sd_dev != (unsigned int) st->st_dev) 132 changed |= INODE_CHANGED; 133#endif 134 135if(sd->sd_size != (unsigned int) st->st_size) 136 changed |= DATA_CHANGED; 137 138return changed; 139} 140 141/* 142 * This only updates the "non-critical" parts of the directory 143 * cache, ie the parts that aren't tracked by GIT, and only used 144 * to validate the cache. 145 */ 146voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 147{ 148fill_stat_data(&ce->ce_stat_data, st); 149 150if(assume_unchanged) 151 ce->ce_flags |= CE_VALID; 152 153if(S_ISREG(st->st_mode)) 154ce_mark_uptodate(ce); 155} 156 157static intce_compare_data(const struct cache_entry *ce,struct stat *st) 158{ 159int match = -1; 160int fd =open(ce->name, O_RDONLY); 161 162if(fd >=0) { 163unsigned char sha1[20]; 164if(!index_fd(sha1, fd, st, OBJ_BLOB, ce->name,0)) 165 match =hashcmp(sha1, ce->sha1); 166/* index_fd() closed the file descriptor already */ 167} 168return match; 169} 170 171static intce_compare_link(const struct cache_entry *ce,size_t expected_size) 172{ 173int match = -1; 174void*buffer; 175unsigned long size; 176enum object_type type; 177struct strbuf sb = STRBUF_INIT; 178 179if(strbuf_readlink(&sb, ce->name, expected_size)) 180return-1; 181 182 buffer =read_sha1_file(ce->sha1, &type, &size); 183if(buffer) { 184if(size == sb.len) 185 match =memcmp(buffer, sb.buf, size); 186free(buffer); 187} 188strbuf_release(&sb); 189return match; 190} 191 192static intce_compare_gitlink(const struct cache_entry *ce) 193{ 194unsigned char sha1[20]; 195 196/* 197 * We don't actually require that the .git directory 198 * under GITLINK directory be a valid git directory. It 199 * might even be missing (in case nobody populated that 200 * sub-project). 201 * 202 * If so, we consider it always to match. 203 */ 204if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 205return0; 206returnhashcmp(sha1, ce->sha1); 207} 208 209static intce_modified_check_fs(const struct cache_entry *ce,struct stat *st) 210{ 211switch(st->st_mode & S_IFMT) { 212case S_IFREG: 213if(ce_compare_data(ce, st)) 214return DATA_CHANGED; 215break; 216case S_IFLNK: 217if(ce_compare_link(ce,xsize_t(st->st_size))) 218return DATA_CHANGED; 219break; 220case S_IFDIR: 221if(S_ISGITLINK(ce->ce_mode)) 222returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 223default: 224return TYPE_CHANGED; 225} 226return0; 227} 228 229static intce_match_stat_basic(const struct cache_entry *ce,struct stat *st) 230{ 231unsigned int changed =0; 232 233if(ce->ce_flags & CE_REMOVE) 234return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 235 236switch(ce->ce_mode & S_IFMT) { 237case S_IFREG: 238 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 239/* We consider only the owner x bit to be relevant for 240 * "mode changes" 241 */ 242if(trust_executable_bit && 243(0100& (ce->ce_mode ^ st->st_mode))) 244 changed |= MODE_CHANGED; 245break; 246case S_IFLNK: 247if(!S_ISLNK(st->st_mode) && 248(has_symlinks || !S_ISREG(st->st_mode))) 249 changed |= TYPE_CHANGED; 250break; 251case S_IFGITLINK: 252/* We ignore most of the st_xxx fields for gitlinks */ 253if(!S_ISDIR(st->st_mode)) 254 changed |= TYPE_CHANGED; 255else if(ce_compare_gitlink(ce)) 256 changed |= DATA_CHANGED; 257return changed; 258default: 259die("internal error: ce_mode is%o", ce->ce_mode); 260} 261 262 changed |=match_stat_data(&ce->ce_stat_data, st); 263 264/* Racily smudged entry? */ 265if(!ce->ce_stat_data.sd_size) { 266if(!is_empty_blob_sha1(ce->sha1)) 267 changed |= DATA_CHANGED; 268} 269 270return changed; 271} 272 273static intis_racy_timestamp(const struct index_state *istate, 274const struct cache_entry *ce) 275{ 276return(!S_ISGITLINK(ce->ce_mode) && 277 istate->timestamp.sec && 278#ifdef USE_NSEC 279/* nanosecond timestamped files can also be racy! */ 280(istate->timestamp.sec < ce->ce_stat_data.sd_mtime.sec || 281(istate->timestamp.sec == ce->ce_stat_data.sd_mtime.sec && 282 istate->timestamp.nsec <= ce->ce_stat_data.sd_mtime.nsec)) 283#else 284 istate->timestamp.sec <= ce->ce_stat_data.sd_mtime.sec 285#endif 286); 287} 288 289intie_match_stat(const struct index_state *istate, 290const struct cache_entry *ce,struct stat *st, 291unsigned int options) 292{ 293unsigned int changed; 294int ignore_valid = options & CE_MATCH_IGNORE_VALID; 295int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; 296int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 297 298/* 299 * If it's marked as always valid in the index, it's 300 * valid whatever the checked-out copy says. 301 * 302 * skip-worktree has the same effect with higher precedence 303 */ 304if(!ignore_skip_worktree &&ce_skip_worktree(ce)) 305return0; 306if(!ignore_valid && (ce->ce_flags & CE_VALID)) 307return0; 308 309/* 310 * Intent-to-add entries have not been added, so the index entry 311 * by definition never matches what is in the work tree until it 312 * actually gets added. 313 */ 314if(ce->ce_flags & CE_INTENT_TO_ADD) 315return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; 316 317 changed =ce_match_stat_basic(ce, st); 318 319/* 320 * Within 1 second of this sequence: 321 * echo xyzzy >file && git-update-index --add file 322 * running this command: 323 * echo frotz >file 324 * would give a falsely clean cache entry. The mtime and 325 * length match the cache, and other stat fields do not change. 326 * 327 * We could detect this at update-index time (the cache entry 328 * being registered/updated records the same time as "now") 329 * and delay the return from git-update-index, but that would 330 * effectively mean we can make at most one commit per second, 331 * which is not acceptable. Instead, we check cache entries 332 * whose mtime are the same as the index file timestamp more 333 * carefully than others. 334 */ 335if(!changed &&is_racy_timestamp(istate, ce)) { 336if(assume_racy_is_modified) 337 changed |= DATA_CHANGED; 338else 339 changed |=ce_modified_check_fs(ce, st); 340} 341 342return changed; 343} 344 345intie_modified(const struct index_state *istate, 346const struct cache_entry *ce, 347struct stat *st,unsigned int options) 348{ 349int changed, changed_fs; 350 351 changed =ie_match_stat(istate, ce, st, options); 352if(!changed) 353return0; 354/* 355 * If the mode or type has changed, there's no point in trying 356 * to refresh the entry - it's not going to match 357 */ 358if(changed & (MODE_CHANGED | TYPE_CHANGED)) 359return changed; 360 361/* 362 * Immediately after read-tree or update-index --cacheinfo, 363 * the length field is zero, as we have never even read the 364 * lstat(2) information once, and we cannot trust DATA_CHANGED 365 * returned by ie_match_stat() which in turn was returned by 366 * ce_match_stat_basic() to signal that the filesize of the 367 * blob changed. We have to actually go to the filesystem to 368 * see if the contents match, and if so, should answer "unchanged". 369 * 370 * The logic does not apply to gitlinks, as ce_match_stat_basic() 371 * already has checked the actual HEAD from the filesystem in the 372 * subproject. If ie_match_stat() already said it is different, 373 * then we know it is. 374 */ 375if((changed & DATA_CHANGED) && 376(S_ISGITLINK(ce->ce_mode) || ce->ce_stat_data.sd_size !=0)) 377return changed; 378 379 changed_fs =ce_modified_check_fs(ce, st); 380if(changed_fs) 381return changed | changed_fs; 382return0; 383} 384 385intbase_name_compare(const char*name1,int len1,int mode1, 386const char*name2,int len2,int mode2) 387{ 388unsigned char c1, c2; 389int len = len1 < len2 ? len1 : len2; 390int cmp; 391 392 cmp =memcmp(name1, name2, len); 393if(cmp) 394return cmp; 395 c1 = name1[len]; 396 c2 = name2[len]; 397if(!c1 &&S_ISDIR(mode1)) 398 c1 ='/'; 399if(!c2 &&S_ISDIR(mode2)) 400 c2 ='/'; 401return(c1 < c2) ? -1: (c1 > c2) ?1:0; 402} 403 404/* 405 * df_name_compare() is identical to base_name_compare(), except it 406 * compares conflicting directory/file entries as equal. Note that 407 * while a directory name compares as equal to a regular file, they 408 * then individually compare _differently_ to a filename that has 409 * a dot after the basename (because '\0' < '.' < '/'). 410 * 411 * This is used by routines that want to traverse the git namespace 412 * but then handle conflicting entries together when possible. 413 */ 414intdf_name_compare(const char*name1,int len1,int mode1, 415const char*name2,int len2,int mode2) 416{ 417int len = len1 < len2 ? len1 : len2, cmp; 418unsigned char c1, c2; 419 420 cmp =memcmp(name1, name2, len); 421if(cmp) 422return cmp; 423/* Directories and files compare equal (same length, same name) */ 424if(len1 == len2) 425return0; 426 c1 = name1[len]; 427if(!c1 &&S_ISDIR(mode1)) 428 c1 ='/'; 429 c2 = name2[len]; 430if(!c2 &&S_ISDIR(mode2)) 431 c2 ='/'; 432if(c1 =='/'&& !c2) 433return0; 434if(c2 =='/'&& !c1) 435return0; 436return c1 - c2; 437} 438 439intname_compare(const char*name1,size_t len1,const char*name2,size_t len2) 440{ 441size_t min_len = (len1 < len2) ? len1 : len2; 442int cmp =memcmp(name1, name2, min_len); 443if(cmp) 444return cmp; 445if(len1 < len2) 446return-1; 447if(len1 > len2) 448return1; 449return0; 450} 451 452intcache_name_stage_compare(const char*name1,int len1,int stage1,const char*name2,int len2,int stage2) 453{ 454int cmp; 455 456 cmp =name_compare(name1, len1, name2, len2); 457if(cmp) 458return cmp; 459 460if(stage1 < stage2) 461return-1; 462if(stage1 > stage2) 463return1; 464return0; 465} 466 467static intindex_name_stage_pos(const struct index_state *istate,const char*name,int namelen,int stage) 468{ 469int first, last; 470 471 first =0; 472 last = istate->cache_nr; 473while(last > first) { 474int next = (last + first) >>1; 475struct cache_entry *ce = istate->cache[next]; 476int cmp =cache_name_stage_compare(name, namelen, stage, ce->name,ce_namelen(ce),ce_stage(ce)); 477if(!cmp) 478return next; 479if(cmp <0) { 480 last = next; 481continue; 482} 483 first = next+1; 484} 485return-first-1; 486} 487 488intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 489{ 490returnindex_name_stage_pos(istate, name, namelen,0); 491} 492 493/* Remove entry, return true if there are more entries to go.. */ 494intremove_index_entry_at(struct index_state *istate,int pos) 495{ 496struct cache_entry *ce = istate->cache[pos]; 497 498record_resolve_undo(istate, ce); 499remove_name_hash(istate, ce); 500save_or_free_index_entry(istate, ce); 501 istate->cache_changed |= CE_ENTRY_REMOVED; 502 istate->cache_nr--; 503if(pos >= istate->cache_nr) 504return0; 505memmove(istate->cache + pos, 506 istate->cache + pos +1, 507(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 508return1; 509} 510 511/* 512 * Remove all cache entries marked for removal, that is where 513 * CE_REMOVE is set in ce_flags. This is much more effective than 514 * calling remove_index_entry_at() for each entry to be removed. 515 */ 516voidremove_marked_cache_entries(struct index_state *istate) 517{ 518struct cache_entry **ce_array = istate->cache; 519unsigned int i, j; 520 521for(i = j =0; i < istate->cache_nr; i++) { 522if(ce_array[i]->ce_flags & CE_REMOVE) { 523remove_name_hash(istate, ce_array[i]); 524save_or_free_index_entry(istate, ce_array[i]); 525} 526else 527 ce_array[j++] = ce_array[i]; 528} 529if(j == istate->cache_nr) 530return; 531 istate->cache_changed |= CE_ENTRY_REMOVED; 532 istate->cache_nr = j; 533} 534 535intremove_file_from_index(struct index_state *istate,const char*path) 536{ 537int pos =index_name_pos(istate, path,strlen(path)); 538if(pos <0) 539 pos = -pos-1; 540cache_tree_invalidate_path(istate, path); 541while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 542remove_index_entry_at(istate, pos); 543return0; 544} 545 546static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 547{ 548return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 549} 550 551static intindex_name_pos_also_unmerged(struct index_state *istate, 552const char*path,int namelen) 553{ 554int pos =index_name_pos(istate, path, namelen); 555struct cache_entry *ce; 556 557if(pos >=0) 558return pos; 559 560/* maybe unmerged? */ 561 pos = -1- pos; 562if(pos >= istate->cache_nr || 563compare_name((ce = istate->cache[pos]), path, namelen)) 564return-1; 565 566/* order of preference: stage 2, 1, 3 */ 567if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 568ce_stage((ce = istate->cache[pos +1])) ==2&& 569!compare_name(ce, path, namelen)) 570 pos++; 571return pos; 572} 573 574static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 575{ 576int len =ce_namelen(ce); 577returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 578} 579 580/* 581 * If we add a filename that aliases in the cache, we will use the 582 * name that we already have - but we don't want to update the same 583 * alias twice, because that implies that there were actually two 584 * different files with aliasing names! 585 * 586 * So we use the CE_ADDED flag to verify that the alias was an old 587 * one before we accept it as 588 */ 589static struct cache_entry *create_alias_ce(struct index_state *istate, 590struct cache_entry *ce, 591struct cache_entry *alias) 592{ 593int len; 594struct cache_entry *new; 595 596if(alias->ce_flags & CE_ADDED) 597die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 598 599/* Ok, create the new entry using the name of the existing alias */ 600 len =ce_namelen(alias); 601new=xcalloc(1,cache_entry_size(len)); 602memcpy(new->name, alias->name, len); 603copy_cache_entry(new, ce); 604save_or_free_index_entry(istate, ce); 605return new; 606} 607 608voidset_object_name_for_intent_to_add_entry(struct cache_entry *ce) 609{ 610unsigned char sha1[20]; 611if(write_sha1_file("",0, blob_type, sha1)) 612die("cannot create an empty blob in the object database"); 613hashcpy(ce->sha1, sha1); 614} 615 616intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 617{ 618int size, namelen, was_same; 619 mode_t st_mode = st->st_mode; 620struct cache_entry *ce, *alias; 621unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; 622int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 623int pretend = flags & ADD_CACHE_PRETEND; 624int intent_only = flags & ADD_CACHE_INTENT; 625int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 626(intent_only ? ADD_CACHE_NEW_ONLY :0)); 627 628if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 629returnerror("%s: can only add regular files, symbolic links or git-directories", path); 630 631 namelen =strlen(path); 632if(S_ISDIR(st_mode)) { 633while(namelen && path[namelen-1] =='/') 634 namelen--; 635} 636 size =cache_entry_size(namelen); 637 ce =xcalloc(1, size); 638memcpy(ce->name, path, namelen); 639 ce->ce_namelen = namelen; 640if(!intent_only) 641fill_stat_cache_info(ce, st); 642else 643 ce->ce_flags |= CE_INTENT_TO_ADD; 644 645if(trust_executable_bit && has_symlinks) 646 ce->ce_mode =create_ce_mode(st_mode); 647else{ 648/* If there is an existing entry, pick the mode bits and type 649 * from it, otherwise assume unexecutable regular file. 650 */ 651struct cache_entry *ent; 652int pos =index_name_pos_also_unmerged(istate, path, namelen); 653 654 ent = (0<= pos) ? istate->cache[pos] : NULL; 655 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 656} 657 658/* When core.ignorecase=true, determine if a directory of the same name but differing 659 * case already exists within the Git repository. If it does, ensure the directory 660 * case of the file being added to the repository matches (is folded into) the existing 661 * entry's directory case. 662 */ 663if(ignore_case) { 664const char*startPtr = ce->name; 665const char*ptr = startPtr; 666while(*ptr) { 667while(*ptr && *ptr !='/') 668++ptr; 669if(*ptr =='/') { 670struct cache_entry *foundce; 671++ptr; 672 foundce =index_dir_exists(istate, ce->name, ptr - ce->name -1); 673if(foundce) { 674memcpy((void*)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); 675 startPtr = ptr; 676} 677} 678} 679} 680 681 alias =index_file_exists(istate, ce->name,ce_namelen(ce), ignore_case); 682if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 683/* Nothing changed, really */ 684free(ce); 685if(!S_ISGITLINK(alias->ce_mode)) 686ce_mark_uptodate(alias); 687 alias->ce_flags |= CE_ADDED; 688return0; 689} 690if(!intent_only) { 691if(index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) 692returnerror("unable to index file%s", path); 693}else 694set_object_name_for_intent_to_add_entry(ce); 695 696if(ignore_case && alias &&different_name(ce, alias)) 697 ce =create_alias_ce(istate, ce, alias); 698 ce->ce_flags |= CE_ADDED; 699 700/* It was suspected to be racily clean, but it turns out to be Ok */ 701 was_same = (alias && 702!ce_stage(alias) && 703!hashcmp(alias->sha1, ce->sha1) && 704 ce->ce_mode == alias->ce_mode); 705 706if(pretend) 707; 708else if(add_index_entry(istate, ce, add_option)) 709returnerror("unable to add%sto index",path); 710if(verbose && !was_same) 711printf("add '%s'\n", path); 712return0; 713} 714 715intadd_file_to_index(struct index_state *istate,const char*path,int flags) 716{ 717struct stat st; 718if(lstat(path, &st)) 719die_errno("unable to stat '%s'", path); 720returnadd_to_index(istate, path, &st, flags); 721} 722 723struct cache_entry *make_cache_entry(unsigned int mode, 724const unsigned char*sha1,const char*path,int stage, 725unsigned int refresh_options) 726{ 727int size, len; 728struct cache_entry *ce, *ret; 729 730if(!verify_path(path)) { 731error("Invalid path '%s'", path); 732return NULL; 733} 734 735 len =strlen(path); 736 size =cache_entry_size(len); 737 ce =xcalloc(1, size); 738 739hashcpy(ce->sha1, sha1); 740memcpy(ce->name, path, len); 741 ce->ce_flags =create_ce_flags(stage); 742 ce->ce_namelen = len; 743 ce->ce_mode =create_ce_mode(mode); 744 745 ret =refresh_cache_entry(ce, refresh_options); 746if(!ret) { 747free(ce); 748return NULL; 749}else{ 750return ret; 751} 752} 753 754intce_same_name(const struct cache_entry *a,const struct cache_entry *b) 755{ 756int len =ce_namelen(a); 757returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 758} 759 760/* 761 * We fundamentally don't like some paths: we don't want 762 * dot or dot-dot anywhere, and for obvious reasons don't 763 * want to recurse into ".git" either. 764 * 765 * Also, we don't want double slashes or slashes at the 766 * end that can make pathnames ambiguous. 767 */ 768static intverify_dotfile(const char*rest) 769{ 770/* 771 * The first character was '.', but that 772 * has already been discarded, we now test 773 * the rest. 774 */ 775 776/* "." is not allowed */ 777if(*rest =='\0'||is_dir_sep(*rest)) 778return0; 779 780switch(*rest) { 781/* 782 * ".git" followed by NUL or slash is bad. This 783 * shares the path end test with the ".." case. 784 */ 785case'g': 786case'G': 787if(rest[1] !='i'&& rest[1] !='I') 788break; 789if(rest[2] !='t'&& rest[2] !='T') 790break; 791 rest +=2; 792/* fallthrough */ 793case'.': 794if(rest[1] =='\0'||is_dir_sep(rest[1])) 795return0; 796} 797return1; 798} 799 800intverify_path(const char*path) 801{ 802char c; 803 804if(has_dos_drive_prefix(path)) 805return0; 806 807goto inside; 808for(;;) { 809if(!c) 810return1; 811if(is_dir_sep(c)) { 812inside: 813if(protect_hfs &&is_hfs_dotgit(path)) 814return0; 815if(protect_ntfs &&is_ntfs_dotgit(path)) 816return0; 817 c = *path++; 818if((c =='.'&& !verify_dotfile(path)) || 819is_dir_sep(c) || c =='\0') 820return0; 821} 822 c = *path++; 823} 824} 825 826/* 827 * Do we have another file that has the beginning components being a 828 * proper superset of the name we're trying to add? 829 */ 830static inthas_file_name(struct index_state *istate, 831const struct cache_entry *ce,int pos,int ok_to_replace) 832{ 833int retval =0; 834int len =ce_namelen(ce); 835int stage =ce_stage(ce); 836const char*name = ce->name; 837 838while(pos < istate->cache_nr) { 839struct cache_entry *p = istate->cache[pos++]; 840 841if(len >=ce_namelen(p)) 842break; 843if(memcmp(name, p->name, len)) 844break; 845if(ce_stage(p) != stage) 846continue; 847if(p->name[len] !='/') 848continue; 849if(p->ce_flags & CE_REMOVE) 850continue; 851 retval = -1; 852if(!ok_to_replace) 853break; 854remove_index_entry_at(istate, --pos); 855} 856return retval; 857} 858 859/* 860 * Do we have another file with a pathname that is a proper 861 * subset of the name we're trying to add? 862 */ 863static inthas_dir_name(struct index_state *istate, 864const struct cache_entry *ce,int pos,int ok_to_replace) 865{ 866int retval =0; 867int stage =ce_stage(ce); 868const char*name = ce->name; 869const char*slash = name +ce_namelen(ce); 870 871for(;;) { 872int len; 873 874for(;;) { 875if(*--slash =='/') 876break; 877if(slash <= ce->name) 878return retval; 879} 880 len = slash - name; 881 882 pos =index_name_stage_pos(istate, name, len, stage); 883if(pos >=0) { 884/* 885 * Found one, but not so fast. This could 886 * be a marker that says "I was here, but 887 * I am being removed". Such an entry is 888 * not a part of the resulting tree, and 889 * it is Ok to have a directory at the same 890 * path. 891 */ 892if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 893 retval = -1; 894if(!ok_to_replace) 895break; 896remove_index_entry_at(istate, pos); 897continue; 898} 899} 900else 901 pos = -pos-1; 902 903/* 904 * Trivial optimization: if we find an entry that 905 * already matches the sub-directory, then we know 906 * we're ok, and we can exit. 907 */ 908while(pos < istate->cache_nr) { 909struct cache_entry *p = istate->cache[pos]; 910if((ce_namelen(p) <= len) || 911(p->name[len] !='/') || 912memcmp(p->name, name, len)) 913break;/* not our subdirectory */ 914if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 915/* 916 * p is at the same stage as our entry, and 917 * is a subdirectory of what we are looking 918 * at, so we cannot have conflicts at our 919 * level or anything shorter. 920 */ 921return retval; 922 pos++; 923} 924} 925return retval; 926} 927 928/* We may be in a situation where we already have path/file and path 929 * is being added, or we already have path and path/file is being 930 * added. Either one would result in a nonsense tree that has path 931 * twice when git-write-tree tries to write it out. Prevent it. 932 * 933 * If ok-to-replace is specified, we remove the conflicting entries 934 * from the cache so the caller should recompute the insert position. 935 * When this happens, we return non-zero. 936 */ 937static intcheck_file_directory_conflict(struct index_state *istate, 938const struct cache_entry *ce, 939int pos,int ok_to_replace) 940{ 941int retval; 942 943/* 944 * When ce is an "I am going away" entry, we allow it to be added 945 */ 946if(ce->ce_flags & CE_REMOVE) 947return0; 948 949/* 950 * We check if the path is a sub-path of a subsequent pathname 951 * first, since removing those will not change the position 952 * in the array. 953 */ 954 retval =has_file_name(istate, ce, pos, ok_to_replace); 955 956/* 957 * Then check if the path might have a clashing sub-directory 958 * before it. 959 */ 960return retval +has_dir_name(istate, ce, pos, ok_to_replace); 961} 962 963static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 964{ 965int pos; 966int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 967int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 968int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 969int new_only = option & ADD_CACHE_NEW_ONLY; 970 971if(!(option & ADD_CACHE_KEEP_CACHE_TREE)) 972cache_tree_invalidate_path(istate, ce->name); 973 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 974 975/* existing match? Just replace it. */ 976if(pos >=0) { 977if(!new_only) 978replace_index_entry(istate, pos, ce); 979return0; 980} 981 pos = -pos-1; 982 983/* 984 * Inserting a merged entry ("stage 0") into the index 985 * will always replace all non-merged entries.. 986 */ 987if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 988while(ce_same_name(istate->cache[pos], ce)) { 989 ok_to_add =1; 990if(!remove_index_entry_at(istate, pos)) 991break; 992} 993} 994 995if(!ok_to_add) 996return-1; 997if(!verify_path(ce->name)) 998returnerror("Invalid path '%s'", ce->name); 9991000if(!skip_df_check &&1001check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {1002if(!ok_to_replace)1003returnerror("'%s' appears as both a file and as a directory",1004 ce->name);1005 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce));1006 pos = -pos-1;1007}1008return pos +1;1009}10101011intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option)1012{1013int pos;10141015if(option & ADD_CACHE_JUST_APPEND)1016 pos = istate->cache_nr;1017else{1018int ret;1019 ret =add_index_entry_with_check(istate, ce, option);1020if(ret <=0)1021return ret;1022 pos = ret -1;1023}10241025/* Make sure the array is big enough .. */1026ALLOC_GROW(istate->cache, istate->cache_nr +1, istate->cache_alloc);10271028/* Add it in.. */1029 istate->cache_nr++;1030if(istate->cache_nr > pos +1)1031memmove(istate->cache + pos +1,1032 istate->cache + pos,1033(istate->cache_nr - pos -1) *sizeof(ce));1034set_index_entry(istate, pos, ce);1035 istate->cache_changed |= CE_ENTRY_ADDED;1036return0;1037}10381039/*1040 * "refresh" does not calculate a new sha1 file or bring the1041 * cache up-to-date for mode/content changes. But what it1042 * _does_ do is to "re-match" the stat information of a file1043 * with the cache, so that you can refresh the cache for a1044 * file that hasn't been changed but where the stat entry is1045 * out of date.1046 *1047 * For example, you'd want to do this after doing a "git-read-tree",1048 * to link up the stat cache details with the proper files.1049 */1050static struct cache_entry *refresh_cache_ent(struct index_state *istate,1051struct cache_entry *ce,1052unsigned int options,int*err,1053int*changed_ret)1054{1055struct stat st;1056struct cache_entry *updated;1057int changed, size;1058int refresh = options & CE_MATCH_REFRESH;1059int ignore_valid = options & CE_MATCH_IGNORE_VALID;1060int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;1061int ignore_missing = options & CE_MATCH_IGNORE_MISSING;10621063if(!refresh ||ce_uptodate(ce))1064return ce;10651066/*1067 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1068 * that the change to the work tree does not matter and told1069 * us not to worry.1070 */1071if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1072ce_mark_uptodate(ce);1073return ce;1074}1075if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1076ce_mark_uptodate(ce);1077return ce;1078}10791080if(has_symlink_leading_path(ce->name,ce_namelen(ce))) {1081if(ignore_missing)1082return ce;1083if(err)1084*err = ENOENT;1085return NULL;1086}10871088if(lstat(ce->name, &st) <0) {1089if(ignore_missing && errno == ENOENT)1090return ce;1091if(err)1092*err = errno;1093return NULL;1094}10951096 changed =ie_match_stat(istate, ce, &st, options);1097if(changed_ret)1098*changed_ret = changed;1099if(!changed) {1100/*1101 * The path is unchanged. If we were told to ignore1102 * valid bit, then we did the actual stat check and1103 * found that the entry is unmodified. If the entry1104 * is not marked VALID, this is the place to mark it1105 * valid again, under "assume unchanged" mode.1106 */1107if(ignore_valid && assume_unchanged &&1108!(ce->ce_flags & CE_VALID))1109;/* mark this one VALID again */1110else{1111/*1112 * We do not mark the index itself "modified"1113 * because CE_UPTODATE flag is in-core only;1114 * we are not going to write this change out.1115 */1116if(!S_ISGITLINK(ce->ce_mode))1117ce_mark_uptodate(ce);1118return ce;1119}1120}11211122if(ie_modified(istate, ce, &st, options)) {1123if(err)1124*err = EINVAL;1125return NULL;1126}11271128 size =ce_size(ce);1129 updated =xmalloc(size);1130memcpy(updated, ce, size);1131fill_stat_cache_info(updated, &st);1132/*1133 * If ignore_valid is not set, we should leave CE_VALID bit1134 * alone. Otherwise, paths marked with --no-assume-unchanged1135 * (i.e. things to be edited) will reacquire CE_VALID bit1136 * automatically, which is not really what we want.1137 */1138if(!ignore_valid && assume_unchanged &&1139!(ce->ce_flags & CE_VALID))1140 updated->ce_flags &= ~CE_VALID;11411142/* istate->cache_changed is updated in the caller */1143return updated;1144}11451146static voidshow_file(const char* fmt,const char* name,int in_porcelain,1147int* first,const char*header_msg)1148{1149if(in_porcelain && *first && header_msg) {1150printf("%s\n", header_msg);1151*first =0;1152}1153printf(fmt, name);1154}11551156intrefresh_index(struct index_state *istate,unsigned int flags,1157const struct pathspec *pathspec,1158char*seen,const char*header_msg)1159{1160int i;1161int has_errors =0;1162int really = (flags & REFRESH_REALLY) !=0;1163int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1164int quiet = (flags & REFRESH_QUIET) !=0;1165int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1166int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1167int first =1;1168int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1169unsigned int options = (CE_MATCH_REFRESH |1170(really ? CE_MATCH_IGNORE_VALID :0) |1171(not_new ? CE_MATCH_IGNORE_MISSING :0));1172const char*modified_fmt;1173const char*deleted_fmt;1174const char*typechange_fmt;1175const char*added_fmt;1176const char*unmerged_fmt;11771178 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1179 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1180 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1181 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1182 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1183for(i =0; i < istate->cache_nr; i++) {1184struct cache_entry *ce, *new;1185int cache_errno =0;1186int changed =0;1187int filtered =0;11881189 ce = istate->cache[i];1190if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1191continue;11921193if(pathspec && !ce_path_match(ce, pathspec, seen))1194 filtered =1;11951196if(ce_stage(ce)) {1197while((i < istate->cache_nr) &&1198!strcmp(istate->cache[i]->name, ce->name))1199 i++;1200 i--;1201if(allow_unmerged)1202continue;1203if(!filtered)1204show_file(unmerged_fmt, ce->name, in_porcelain,1205&first, header_msg);1206 has_errors =1;1207continue;1208}12091210if(filtered)1211continue;12121213new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1214if(new== ce)1215continue;1216if(!new) {1217const char*fmt;12181219if(really && cache_errno == EINVAL) {1220/* If we are doing --really-refresh that1221 * means the index is not valid anymore.1222 */1223 ce->ce_flags &= ~CE_VALID;1224 ce->ce_flags |= CE_UPDATE_IN_BASE;1225 istate->cache_changed |= CE_ENTRY_CHANGED;1226}1227if(quiet)1228continue;12291230if(cache_errno == ENOENT)1231 fmt = deleted_fmt;1232else if(ce->ce_flags & CE_INTENT_TO_ADD)1233 fmt = added_fmt;/* must be before other checks */1234else if(changed & TYPE_CHANGED)1235 fmt = typechange_fmt;1236else1237 fmt = modified_fmt;1238show_file(fmt,1239 ce->name, in_porcelain, &first, header_msg);1240 has_errors =1;1241continue;1242}12431244replace_index_entry(istate, i,new);1245}1246return has_errors;1247}12481249static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,1250unsigned int options)1251{1252returnrefresh_cache_ent(&the_index, ce, options, NULL, NULL);1253}125412551256/*****************************************************************1257 * Index File I/O1258 *****************************************************************/12591260#define INDEX_FORMAT_DEFAULT 312611262static unsigned intget_index_format_default(void)1263{1264char*envversion =getenv("GIT_INDEX_VERSION");1265char*endp;1266int value;1267unsigned int version = INDEX_FORMAT_DEFAULT;12681269if(!envversion) {1270if(!git_config_get_int("index.version", &value))1271 version = value;1272if(version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1273warning(_("index.version set, but the value is invalid.\n"1274"Using version%i"), INDEX_FORMAT_DEFAULT);1275return INDEX_FORMAT_DEFAULT;1276}1277return version;1278}12791280 version =strtoul(envversion, &endp,10);1281if(*endp ||1282 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1283warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"1284"Using version%i"), INDEX_FORMAT_DEFAULT);1285 version = INDEX_FORMAT_DEFAULT;1286}1287return version;1288}12891290/*1291 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1292 * Again - this is just a (very strong in practice) heuristic that1293 * the inode hasn't changed.1294 *1295 * We save the fields in big-endian order to allow using the1296 * index file over NFS transparently.1297 */1298struct ondisk_cache_entry {1299struct cache_time ctime;1300struct cache_time mtime;1301uint32_t dev;1302uint32_t ino;1303uint32_t mode;1304uint32_t uid;1305uint32_t gid;1306uint32_t size;1307unsigned char sha1[20];1308uint16_t flags;1309char name[FLEX_ARRAY];/* more */1310};13111312/*1313 * This struct is used when CE_EXTENDED bit is 11314 * The struct must match ondisk_cache_entry exactly from1315 * ctime till flags1316 */1317struct ondisk_cache_entry_extended {1318struct cache_time ctime;1319struct cache_time mtime;1320uint32_t dev;1321uint32_t ino;1322uint32_t mode;1323uint32_t uid;1324uint32_t gid;1325uint32_t size;1326unsigned char sha1[20];1327uint16_t flags;1328uint16_t flags2;1329char name[FLEX_ARRAY];/* more */1330};13311332/* These are only used for v3 or lower */1333#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1334#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1335#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1336#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1337 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1338 ondisk_cache_entry_size(ce_namelen(ce)))13391340static intverify_hdr(struct cache_header *hdr,unsigned long size)1341{1342 git_SHA_CTX c;1343unsigned char sha1[20];1344int hdr_version;13451346if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1347returnerror("bad signature");1348 hdr_version =ntohl(hdr->hdr_version);1349if(hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)1350returnerror("bad index version%d", hdr_version);1351git_SHA1_Init(&c);1352git_SHA1_Update(&c, hdr, size -20);1353git_SHA1_Final(sha1, &c);1354if(hashcmp(sha1, (unsigned char*)hdr + size -20))1355returnerror("bad index file sha1 signature");1356return0;1357}13581359static intread_index_extension(struct index_state *istate,1360const char*ext,void*data,unsigned long sz)1361{1362switch(CACHE_EXT(ext)) {1363case CACHE_EXT_TREE:1364 istate->cache_tree =cache_tree_read(data, sz);1365break;1366case CACHE_EXT_RESOLVE_UNDO:1367 istate->resolve_undo =resolve_undo_read(data, sz);1368break;1369case CACHE_EXT_LINK:1370if(read_link_extension(istate, data, sz))1371return-1;1372break;1373default:1374if(*ext <'A'||'Z'< *ext)1375returnerror("index uses %.4s extension, which we do not understand",1376 ext);1377fprintf(stderr,"ignoring %.4s extension\n", ext);1378break;1379}1380return0;1381}13821383inthold_locked_index(struct lock_file *lk,int die_on_error)1384{1385returnhold_lock_file_for_update(lk,get_index_file(),1386 die_on_error1387? LOCK_DIE_ON_ERROR1388:0);1389}13901391intread_index(struct index_state *istate)1392{1393returnread_index_from(istate,get_index_file());1394}13951396static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1397unsigned int flags,1398const char*name,1399size_t len)1400{1401struct cache_entry *ce =xmalloc(cache_entry_size(len));14021403 ce->ce_stat_data.sd_ctime.sec =get_be32(&ondisk->ctime.sec);1404 ce->ce_stat_data.sd_mtime.sec =get_be32(&ondisk->mtime.sec);1405 ce->ce_stat_data.sd_ctime.nsec =get_be32(&ondisk->ctime.nsec);1406 ce->ce_stat_data.sd_mtime.nsec =get_be32(&ondisk->mtime.nsec);1407 ce->ce_stat_data.sd_dev =get_be32(&ondisk->dev);1408 ce->ce_stat_data.sd_ino =get_be32(&ondisk->ino);1409 ce->ce_mode =get_be32(&ondisk->mode);1410 ce->ce_stat_data.sd_uid =get_be32(&ondisk->uid);1411 ce->ce_stat_data.sd_gid =get_be32(&ondisk->gid);1412 ce->ce_stat_data.sd_size =get_be32(&ondisk->size);1413 ce->ce_flags = flags & ~CE_NAMEMASK;1414 ce->ce_namelen = len;1415 ce->index =0;1416hashcpy(ce->sha1, ondisk->sha1);1417memcpy(ce->name, name, len);1418 ce->name[len] ='\0';1419return ce;1420}14211422/*1423 * Adjacent cache entries tend to share the leading paths, so it makes1424 * sense to only store the differences in later entries. In the v41425 * on-disk format of the index, each on-disk cache entry stores the1426 * number of bytes to be stripped from the end of the previous name,1427 * and the bytes to append to the result, to come up with its name.1428 */1429static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1430{1431const unsigned char*ep, *cp = (const unsigned char*)cp_;1432size_t len =decode_varint(&cp);14331434if(name->len < len)1435die("malformed name field in the index");1436strbuf_remove(name, name->len - len, len);1437for(ep = cp; *ep; ep++)1438;/* find the end */1439strbuf_add(name, cp, ep - cp);1440return(const char*)ep +1- cp_;1441}14421443static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1444unsigned long*ent_size,1445struct strbuf *previous_name)1446{1447struct cache_entry *ce;1448size_t len;1449const char*name;1450unsigned int flags;14511452/* On-disk flags are just 16 bits */1453 flags =get_be16(&ondisk->flags);1454 len = flags & CE_NAMEMASK;14551456if(flags & CE_EXTENDED) {1457struct ondisk_cache_entry_extended *ondisk2;1458int extended_flags;1459 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1460 extended_flags =get_be16(&ondisk2->flags2) <<16;1461/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1462if(extended_flags & ~CE_EXTENDED_FLAGS)1463die("Unknown index entry format%08x", extended_flags);1464 flags |= extended_flags;1465 name = ondisk2->name;1466}1467else1468 name = ondisk->name;14691470if(!previous_name) {1471/* v3 and earlier */1472if(len == CE_NAMEMASK)1473 len =strlen(name);1474 ce =cache_entry_from_ondisk(ondisk, flags, name, len);14751476*ent_size =ondisk_ce_size(ce);1477}else{1478unsigned long consumed;1479 consumed =expand_name_field(previous_name, name);1480 ce =cache_entry_from_ondisk(ondisk, flags,1481 previous_name->buf,1482 previous_name->len);14831484*ent_size = (name - ((char*)ondisk)) + consumed;1485}1486return ce;1487}14881489static voidcheck_ce_order(struct cache_entry *ce,struct cache_entry *next_ce)1490{1491int name_compare =strcmp(ce->name, next_ce->name);1492if(0< name_compare)1493die("unordered stage entries in index");1494if(!name_compare) {1495if(!ce_stage(ce))1496die("multiple stage entries for merged file '%s'",1497 ce->name);1498if(ce_stage(ce) >ce_stage(next_ce))1499die("unordered stage entries for '%s'",1500 ce->name);1501}1502}15031504/* remember to discard_cache() before reading a different cache! */1505intdo_read_index(struct index_state *istate,const char*path,int must_exist)1506{1507int fd, i;1508struct stat st;1509unsigned long src_offset;1510struct cache_header *hdr;1511void*mmap;1512size_t mmap_size;1513struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;15141515if(istate->initialized)1516return istate->cache_nr;15171518 istate->timestamp.sec =0;1519 istate->timestamp.nsec =0;1520 fd =open(path, O_RDONLY);1521if(fd <0) {1522if(!must_exist && errno == ENOENT)1523return0;1524die_errno("%s: index file open failed", path);1525}15261527if(fstat(fd, &st))1528die_errno("cannot stat the open index");15291530 mmap_size =xsize_t(st.st_size);1531if(mmap_size <sizeof(struct cache_header) +20)1532die("index file smaller than expected");15331534 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1535if(mmap == MAP_FAILED)1536die_errno("unable to map index file");1537close(fd);15381539 hdr = mmap;1540if(verify_hdr(hdr, mmap_size) <0)1541goto unmap;15421543hashcpy(istate->sha1, (const unsigned char*)hdr + mmap_size -20);1544 istate->version =ntohl(hdr->hdr_version);1545 istate->cache_nr =ntohl(hdr->hdr_entries);1546 istate->cache_alloc =alloc_nr(istate->cache_nr);1547 istate->cache =xcalloc(istate->cache_alloc,sizeof(*istate->cache));1548 istate->initialized =1;15491550if(istate->version ==4)1551 previous_name = &previous_name_buf;1552else1553 previous_name = NULL;15541555 src_offset =sizeof(*hdr);1556for(i =0; i < istate->cache_nr; i++) {1557struct ondisk_cache_entry *disk_ce;1558struct cache_entry *ce;1559unsigned long consumed;15601561 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1562 ce =create_from_disk(disk_ce, &consumed, previous_name);1563set_index_entry(istate, i, ce);15641565if(i >0)1566check_ce_order(istate->cache[i -1], ce);15671568 src_offset += consumed;1569}1570strbuf_release(&previous_name_buf);1571 istate->timestamp.sec = st.st_mtime;1572 istate->timestamp.nsec =ST_MTIME_NSEC(st);15731574while(src_offset <= mmap_size -20-8) {1575/* After an array of active_nr index entries,1576 * there can be arbitrary number of extended1577 * sections, each of which is prefixed with1578 * extension name (4-byte) and section length1579 * in 4-byte network byte order.1580 */1581uint32_t extsize;1582memcpy(&extsize, (char*)mmap + src_offset +4,4);1583 extsize =ntohl(extsize);1584if(read_index_extension(istate,1585(const char*) mmap + src_offset,1586(char*) mmap + src_offset +8,1587 extsize) <0)1588goto unmap;1589 src_offset +=8;1590 src_offset += extsize;1591}1592munmap(mmap, mmap_size);1593return istate->cache_nr;15941595unmap:1596munmap(mmap, mmap_size);1597die("index file corrupt");1598}15991600intread_index_from(struct index_state *istate,const char*path)1601{1602struct split_index *split_index;1603int ret;16041605/* istate->initialized covers both .git/index and .git/sharedindex.xxx */1606if(istate->initialized)1607return istate->cache_nr;16081609 ret =do_read_index(istate, path,0);1610 split_index = istate->split_index;1611if(!split_index)1612return ret;16131614if(is_null_sha1(split_index->base_sha1))1615return ret;16161617if(split_index->base)1618discard_index(split_index->base);1619else1620 split_index->base =xcalloc(1,sizeof(*split_index->base));1621 ret =do_read_index(split_index->base,1622git_path("sharedindex.%s",1623sha1_to_hex(split_index->base_sha1)),1);1624if(hashcmp(split_index->base_sha1, split_index->base->sha1))1625die("broken index, expect%sin%s, got%s",1626sha1_to_hex(split_index->base_sha1),1627git_path("sharedindex.%s",1628sha1_to_hex(split_index->base_sha1)),1629sha1_to_hex(split_index->base->sha1));1630merge_base_index(istate);1631return ret;1632}16331634intis_index_unborn(struct index_state *istate)1635{1636return(!istate->cache_nr && !istate->timestamp.sec);1637}16381639intdiscard_index(struct index_state *istate)1640{1641int i;16421643for(i =0; i < istate->cache_nr; i++) {1644if(istate->cache[i]->index &&1645 istate->split_index &&1646 istate->split_index->base &&1647 istate->cache[i]->index <= istate->split_index->base->cache_nr &&1648 istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index -1])1649continue;1650free(istate->cache[i]);1651}1652resolve_undo_clear_index(istate);1653 istate->cache_nr =0;1654 istate->cache_changed =0;1655 istate->timestamp.sec =0;1656 istate->timestamp.nsec =0;1657free_name_hash(istate);1658cache_tree_free(&(istate->cache_tree));1659 istate->initialized =0;1660free(istate->cache);1661 istate->cache = NULL;1662 istate->cache_alloc =0;1663discard_split_index(istate);1664return0;1665}16661667intunmerged_index(const struct index_state *istate)1668{1669int i;1670for(i =0; i < istate->cache_nr; i++) {1671if(ce_stage(istate->cache[i]))1672return1;1673}1674return0;1675}16761677#define WRITE_BUFFER_SIZE 81921678static unsigned char write_buffer[WRITE_BUFFER_SIZE];1679static unsigned long write_buffer_len;16801681static intce_write_flush(git_SHA_CTX *context,int fd)1682{1683unsigned int buffered = write_buffer_len;1684if(buffered) {1685git_SHA1_Update(context, write_buffer, buffered);1686if(write_in_full(fd, write_buffer, buffered) != buffered)1687return-1;1688 write_buffer_len =0;1689}1690return0;1691}16921693static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1694{1695while(len) {1696unsigned int buffered = write_buffer_len;1697unsigned int partial = WRITE_BUFFER_SIZE - buffered;1698if(partial > len)1699 partial = len;1700memcpy(write_buffer + buffered, data, partial);1701 buffered += partial;1702if(buffered == WRITE_BUFFER_SIZE) {1703 write_buffer_len = buffered;1704if(ce_write_flush(context, fd))1705return-1;1706 buffered =0;1707}1708 write_buffer_len = buffered;1709 len -= partial;1710 data = (char*) data + partial;1711}1712return0;1713}17141715static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1716unsigned int ext,unsigned int sz)1717{1718 ext =htonl(ext);1719 sz =htonl(sz);1720return((ce_write(context, fd, &ext,4) <0) ||1721(ce_write(context, fd, &sz,4) <0)) ? -1:0;1722}17231724static intce_flush(git_SHA_CTX *context,int fd,unsigned char*sha1)1725{1726unsigned int left = write_buffer_len;17271728if(left) {1729 write_buffer_len =0;1730git_SHA1_Update(context, write_buffer, left);1731}17321733/* Flush first if not enough space for SHA1 signature */1734if(left +20> WRITE_BUFFER_SIZE) {1735if(write_in_full(fd, write_buffer, left) != left)1736return-1;1737 left =0;1738}17391740/* Append the SHA1 signature at the end */1741git_SHA1_Final(write_buffer + left, context);1742hashcpy(sha1, write_buffer + left);1743 left +=20;1744return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1745}17461747static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1748{1749/*1750 * The only thing we care about in this function is to smudge the1751 * falsely clean entry due to touch-update-touch race, so we leave1752 * everything else as they are. We are called for entries whose1753 * ce_stat_data.sd_mtime match the index file mtime.1754 *1755 * Note that this actually does not do much for gitlinks, for1756 * which ce_match_stat_basic() always goes to the actual1757 * contents. The caller checks with is_racy_timestamp() which1758 * always says "no" for gitlinks, so we are not called for them ;-)1759 */1760struct stat st;17611762if(lstat(ce->name, &st) <0)1763return;1764if(ce_match_stat_basic(ce, &st))1765return;1766if(ce_modified_check_fs(ce, &st)) {1767/* This is "racily clean"; smudge it. Note that this1768 * is a tricky code. At first glance, it may appear1769 * that it can break with this sequence:1770 *1771 * $ echo xyzzy >frotz1772 * $ git-update-index --add frotz1773 * $ : >frotz1774 * $ sleep 31775 * $ echo filfre >nitfol1776 * $ git-update-index --add nitfol1777 *1778 * but it does not. When the second update-index runs,1779 * it notices that the entry "frotz" has the same timestamp1780 * as index, and if we were to smudge it by resetting its1781 * size to zero here, then the object name recorded1782 * in index is the 6-byte file but the cached stat information1783 * becomes zero --- which would then match what we would1784 * obtain from the filesystem next time we stat("frotz").1785 *1786 * However, the second update-index, before calling1787 * this function, notices that the cached size is 61788 * bytes and what is on the filesystem is an empty1789 * file, and never calls us, so the cached size information1790 * for "frotz" stays 6 which does not match the filesystem.1791 */1792 ce->ce_stat_data.sd_size =0;1793}1794}17951796/* Copy miscellaneous fields but not the name */1797static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1798struct cache_entry *ce)1799{1800short flags;18011802 ondisk->ctime.sec =htonl(ce->ce_stat_data.sd_ctime.sec);1803 ondisk->mtime.sec =htonl(ce->ce_stat_data.sd_mtime.sec);1804 ondisk->ctime.nsec =htonl(ce->ce_stat_data.sd_ctime.nsec);1805 ondisk->mtime.nsec =htonl(ce->ce_stat_data.sd_mtime.nsec);1806 ondisk->dev =htonl(ce->ce_stat_data.sd_dev);1807 ondisk->ino =htonl(ce->ce_stat_data.sd_ino);1808 ondisk->mode =htonl(ce->ce_mode);1809 ondisk->uid =htonl(ce->ce_stat_data.sd_uid);1810 ondisk->gid =htonl(ce->ce_stat_data.sd_gid);1811 ondisk->size =htonl(ce->ce_stat_data.sd_size);1812hashcpy(ondisk->sha1, ce->sha1);18131814 flags = ce->ce_flags & ~CE_NAMEMASK;1815 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK :ce_namelen(ce));1816 ondisk->flags =htons(flags);1817if(ce->ce_flags & CE_EXTENDED) {1818struct ondisk_cache_entry_extended *ondisk2;1819 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1820 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1821return ondisk2->name;1822}1823else{1824return ondisk->name;1825}1826}18271828static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce,1829struct strbuf *previous_name)1830{1831int size;1832struct ondisk_cache_entry *ondisk;1833int saved_namelen = saved_namelen;/* compiler workaround */1834char*name;1835int result;18361837if(ce->ce_flags & CE_STRIP_NAME) {1838 saved_namelen =ce_namelen(ce);1839 ce->ce_namelen =0;1840}18411842if(!previous_name) {1843 size =ondisk_ce_size(ce);1844 ondisk =xcalloc(1, size);1845 name =copy_cache_entry_to_ondisk(ondisk, ce);1846memcpy(name, ce->name,ce_namelen(ce));1847}else{1848int common, to_remove, prefix_size;1849unsigned char to_remove_vi[16];1850for(common =0;1851(ce->name[common] &&1852 common < previous_name->len &&1853 ce->name[common] == previous_name->buf[common]);1854 common++)1855;/* still matching */1856 to_remove = previous_name->len - common;1857 prefix_size =encode_varint(to_remove, to_remove_vi);18581859if(ce->ce_flags & CE_EXTENDED)1860 size =offsetof(struct ondisk_cache_entry_extended, name);1861else1862 size =offsetof(struct ondisk_cache_entry, name);1863 size += prefix_size + (ce_namelen(ce) - common +1);18641865 ondisk =xcalloc(1, size);1866 name =copy_cache_entry_to_ondisk(ondisk, ce);1867memcpy(name, to_remove_vi, prefix_size);1868memcpy(name + prefix_size, ce->name + common,ce_namelen(ce) - common);18691870strbuf_splice(previous_name, common, to_remove,1871 ce->name + common,ce_namelen(ce) - common);1872}1873if(ce->ce_flags & CE_STRIP_NAME) {1874 ce->ce_namelen = saved_namelen;1875 ce->ce_flags &= ~CE_STRIP_NAME;1876}18771878 result =ce_write(c, fd, ondisk, size);1879free(ondisk);1880return result;1881}18821883/*1884 * This function verifies if index_state has the correct sha1 of the1885 * index file. Don't die if we have any other failure, just return 0.1886 */1887static intverify_index_from(const struct index_state *istate,const char*path)1888{1889int fd;1890 ssize_t n;1891struct stat st;1892unsigned char sha1[20];18931894if(!istate->initialized)1895return0;18961897 fd =open(path, O_RDONLY);1898if(fd <0)1899return0;19001901if(fstat(fd, &st))1902goto out;19031904if(st.st_size <sizeof(struct cache_header) +20)1905goto out;19061907 n =pread_in_full(fd, sha1,20, st.st_size -20);1908if(n !=20)1909goto out;19101911if(hashcmp(istate->sha1, sha1))1912goto out;19131914close(fd);1915return1;19161917out:1918close(fd);1919return0;1920}19211922static intverify_index(const struct index_state *istate)1923{1924returnverify_index_from(istate,get_index_file());1925}19261927static inthas_racy_timestamp(struct index_state *istate)1928{1929int entries = istate->cache_nr;1930int i;19311932for(i =0; i < entries; i++) {1933struct cache_entry *ce = istate->cache[i];1934if(is_racy_timestamp(istate, ce))1935return1;1936}1937return0;1938}19391940/*1941 * Opportunistically update the index but do not complain if we can't1942 */1943voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1944{1945if((istate->cache_changed ||has_racy_timestamp(istate)) &&1946verify_index(istate) &&1947write_locked_index(istate, lockfile, COMMIT_LOCK))1948rollback_lock_file(lockfile);1949}19501951static intdo_write_index(struct index_state *istate,int newfd,1952int strip_extensions)1953{1954 git_SHA_CTX c;1955struct cache_header hdr;1956int i, err, removed, extended, hdr_version;1957struct cache_entry **cache = istate->cache;1958int entries = istate->cache_nr;1959struct stat st;1960struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;19611962for(i = removed = extended =0; i < entries; i++) {1963if(cache[i]->ce_flags & CE_REMOVE)1964 removed++;19651966/* reduce extended entries if possible */1967 cache[i]->ce_flags &= ~CE_EXTENDED;1968if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1969 extended++;1970 cache[i]->ce_flags |= CE_EXTENDED;1971}1972}19731974if(!istate->version) {1975 istate->version =get_index_format_default();1976if(getenv("GIT_TEST_SPLIT_INDEX"))1977init_split_index(istate);1978}19791980/* demote version 3 to version 2 when the latter suffices */1981if(istate->version ==3|| istate->version ==2)1982 istate->version = extended ?3:2;19831984 hdr_version = istate->version;19851986 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1987 hdr.hdr_version =htonl(hdr_version);1988 hdr.hdr_entries =htonl(entries - removed);19891990git_SHA1_Init(&c);1991if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1992return-1;19931994 previous_name = (hdr_version ==4) ? &previous_name_buf : NULL;1995for(i =0; i < entries; i++) {1996struct cache_entry *ce = cache[i];1997if(ce->ce_flags & CE_REMOVE)1998continue;1999if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))2000ce_smudge_racily_clean_entry(ce);2001if(is_null_sha1(ce->sha1)) {2002static const char msg[] ="cache entry has null sha1:%s";2003static int allow = -1;20042005if(allow <0)2006 allow =git_env_bool("GIT_ALLOW_NULL_SHA1",0);2007if(allow)2008warning(msg, ce->name);2009else2010returnerror(msg, ce->name);2011}2012if(ce_write_entry(&c, newfd, ce, previous_name) <0)2013return-1;2014}2015strbuf_release(&previous_name_buf);20162017/* Write extension data here */2018if(!strip_extensions && istate->split_index) {2019struct strbuf sb = STRBUF_INIT;20202021 err =write_link_extension(&sb, istate) <0||2022write_index_ext_header(&c, newfd, CACHE_EXT_LINK,2023 sb.len) <0||2024ce_write(&c, newfd, sb.buf, sb.len) <0;2025strbuf_release(&sb);2026if(err)2027return-1;2028}2029if(!strip_extensions && istate->cache_tree) {2030struct strbuf sb = STRBUF_INIT;20312032cache_tree_write(&sb, istate->cache_tree);2033 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <02034||ce_write(&c, newfd, sb.buf, sb.len) <0;2035strbuf_release(&sb);2036if(err)2037return-1;2038}2039if(!strip_extensions && istate->resolve_undo) {2040struct strbuf sb = STRBUF_INIT;20412042resolve_undo_write(&sb, istate->resolve_undo);2043 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,2044 sb.len) <02045||ce_write(&c, newfd, sb.buf, sb.len) <0;2046strbuf_release(&sb);2047if(err)2048return-1;2049}20502051if(ce_flush(&c, newfd, istate->sha1) ||fstat(newfd, &st))2052return-1;2053 istate->timestamp.sec = (unsigned int)st.st_mtime;2054 istate->timestamp.nsec =ST_MTIME_NSEC(st);2055return0;2056}20572058voidset_alternate_index_output(const char*name)2059{2060 alternate_index_output = name;2061}20622063static intcommit_locked_index(struct lock_file *lk)2064{2065if(alternate_index_output)2066returncommit_lock_file_to(lk, alternate_index_output);2067else2068returncommit_lock_file(lk);2069}20702071static intdo_write_locked_index(struct index_state *istate,struct lock_file *lock,2072unsigned flags)2073{2074int ret =do_write_index(istate, lock->fd,0);2075if(ret)2076return ret;2077assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=2078(COMMIT_LOCK | CLOSE_LOCK));2079if(flags & COMMIT_LOCK)2080returncommit_locked_index(lock);2081else if(flags & CLOSE_LOCK)2082returnclose_lock_file(lock);2083else2084return ret;2085}20862087static intwrite_split_index(struct index_state *istate,2088struct lock_file *lock,2089unsigned flags)2090{2091int ret;2092prepare_to_write_split_index(istate);2093 ret =do_write_locked_index(istate, lock, flags);2094finish_writing_split_index(istate);2095return ret;2096}20972098static char*temporary_sharedindex;20992100static voidremove_temporary_sharedindex(void)2101{2102if(temporary_sharedindex) {2103unlink_or_warn(temporary_sharedindex);2104free(temporary_sharedindex);2105 temporary_sharedindex = NULL;2106}2107}21082109static voidremove_temporary_sharedindex_on_signal(int signo)2110{2111remove_temporary_sharedindex();2112sigchain_pop(signo);2113raise(signo);2114}21152116static intwrite_shared_index(struct index_state *istate,2117struct lock_file *lock,unsigned flags)2118{2119struct split_index *si = istate->split_index;2120static int installed_handler;2121int fd, ret;21222123 temporary_sharedindex =git_pathdup("sharedindex_XXXXXX");2124 fd =mkstemp(temporary_sharedindex);2125if(fd <0) {2126free(temporary_sharedindex);2127 temporary_sharedindex = NULL;2128hashclr(si->base_sha1);2129returndo_write_locked_index(istate, lock, flags);2130}2131if(!installed_handler) {2132atexit(remove_temporary_sharedindex);2133sigchain_push_common(remove_temporary_sharedindex_on_signal);2134}2135move_cache_to_base_index(istate);2136 ret =do_write_index(si->base, fd,1);2137close(fd);2138if(ret) {2139remove_temporary_sharedindex();2140return ret;2141}2142 ret =rename(temporary_sharedindex,2143git_path("sharedindex.%s",sha1_to_hex(si->base->sha1)));2144free(temporary_sharedindex);2145 temporary_sharedindex = NULL;2146if(!ret)2147hashcpy(si->base_sha1, si->base->sha1);2148return ret;2149}21502151intwrite_locked_index(struct index_state *istate,struct lock_file *lock,2152unsigned flags)2153{2154struct split_index *si = istate->split_index;21552156if(!si || alternate_index_output ||2157(istate->cache_changed & ~EXTMASK)) {2158if(si)2159hashclr(si->base_sha1);2160returndo_write_locked_index(istate, lock, flags);2161}21622163if(getenv("GIT_TEST_SPLIT_INDEX")) {2164int v = si->base_sha1[0];2165if((v &15) <6)2166 istate->cache_changed |= SPLIT_INDEX_ORDERED;2167}2168if(istate->cache_changed & SPLIT_INDEX_ORDERED) {2169int ret =write_shared_index(istate, lock, flags);2170if(ret)2171return ret;2172}21732174returnwrite_split_index(istate, lock, flags);2175}21762177/*2178 * Read the index file that is potentially unmerged into given2179 * index_state, dropping any unmerged entries. Returns true if2180 * the index is unmerged. Callers who want to refuse to work2181 * from an unmerged state can call this and check its return value,2182 * instead of calling read_cache().2183 */2184intread_index_unmerged(struct index_state *istate)2185{2186int i;2187int unmerged =0;21882189read_index(istate);2190for(i =0; i < istate->cache_nr; i++) {2191struct cache_entry *ce = istate->cache[i];2192struct cache_entry *new_ce;2193int size, len;21942195if(!ce_stage(ce))2196continue;2197 unmerged =1;2198 len =ce_namelen(ce);2199 size =cache_entry_size(len);2200 new_ce =xcalloc(1, size);2201memcpy(new_ce->name, ce->name, len);2202 new_ce->ce_flags =create_ce_flags(0) | CE_CONFLICTED;2203 new_ce->ce_namelen = len;2204 new_ce->ce_mode = ce->ce_mode;2205if(add_index_entry(istate, new_ce,0))2206returnerror("%s: cannot drop to stage #0",2207 new_ce->name);2208}2209return unmerged;2210}22112212/*2213 * Returns 1 if the path is an "other" path with respect to2214 * the index; that is, the path is not mentioned in the index at all,2215 * either as a file, a directory with some files in the index,2216 * or as an unmerged entry.2217 *2218 * We helpfully remove a trailing "/" from directories so that2219 * the output of read_directory can be used as-is.2220 */2221intindex_name_is_other(const struct index_state *istate,const char*name,2222int namelen)2223{2224int pos;2225if(namelen && name[namelen -1] =='/')2226 namelen--;2227 pos =index_name_pos(istate, name, namelen);2228if(0<= pos)2229return0;/* exact match */2230 pos = -pos -1;2231if(pos < istate->cache_nr) {2232struct cache_entry *ce = istate->cache[pos];2233if(ce_namelen(ce) == namelen &&2234!memcmp(ce->name, name, namelen))2235return0;/* Yup, this one exists unmerged */2236}2237return1;2238}22392240void*read_blob_data_from_index(struct index_state *istate,const char*path,unsigned long*size)2241{2242int pos, len;2243unsigned long sz;2244enum object_type type;2245void*data;22462247 len =strlen(path);2248 pos =index_name_pos(istate, path, len);2249if(pos <0) {2250/*2251 * We might be in the middle of a merge, in which2252 * case we would read stage #2 (ours).2253 */2254int i;2255for(i = -pos -1;2256(pos <0&& i < istate->cache_nr &&2257!strcmp(istate->cache[i]->name, path));2258 i++)2259if(ce_stage(istate->cache[i]) ==2)2260 pos = i;2261}2262if(pos <0)2263return NULL;2264 data =read_sha1_file(istate->cache[pos]->sha1, &type, &sz);2265if(!data || type != OBJ_BLOB) {2266free(data);2267return NULL;2268}2269if(size)2270*size = sz;2271return data;2272}22732274voidstat_validity_clear(struct stat_validity *sv)2275{2276free(sv->sd);2277 sv->sd = NULL;2278}22792280intstat_validity_check(struct stat_validity *sv,const char*path)2281{2282struct stat st;22832284if(stat(path, &st) <0)2285return sv->sd == NULL;2286if(!sv->sd)2287return0;2288returnS_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);2289}22902291voidstat_validity_update(struct stat_validity *sv,int fd)2292{2293struct stat st;22942295if(fstat(fd, &st) <0|| !S_ISREG(st.st_mode))2296stat_validity_clear(sv);2297else{2298if(!sv->sd)2299 sv->sd =xcalloc(1,sizeof(struct stat_data));2300fill_stat_data(sv->sd, &st);2301}2302}