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 */ 684if(!S_ISGITLINK(alias->ce_mode)) 685ce_mark_uptodate(alias); 686 alias->ce_flags |= CE_ADDED; 687 688free(ce); 689return0; 690} 691if(!intent_only) { 692if(index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) { 693free(ce); 694returnerror("unable to index file%s", path); 695} 696}else 697set_object_name_for_intent_to_add_entry(ce); 698 699if(ignore_case && alias &&different_name(ce, alias)) 700 ce =create_alias_ce(istate, ce, alias); 701 ce->ce_flags |= CE_ADDED; 702 703/* It was suspected to be racily clean, but it turns out to be Ok */ 704 was_same = (alias && 705!ce_stage(alias) && 706!hashcmp(alias->sha1, ce->sha1) && 707 ce->ce_mode == alias->ce_mode); 708 709if(pretend) 710free(ce); 711else if(add_index_entry(istate, ce, add_option)) { 712free(ce); 713returnerror("unable to add%sto index", path); 714} 715if(verbose && !was_same) 716printf("add '%s'\n", path); 717return0; 718} 719 720intadd_file_to_index(struct index_state *istate,const char*path,int flags) 721{ 722struct stat st; 723if(lstat(path, &st)) 724die_errno("unable to stat '%s'", path); 725returnadd_to_index(istate, path, &st, flags); 726} 727 728struct cache_entry *make_cache_entry(unsigned int mode, 729const unsigned char*sha1,const char*path,int stage, 730unsigned int refresh_options) 731{ 732int size, len; 733struct cache_entry *ce, *ret; 734 735if(!verify_path(path)) { 736error("Invalid path '%s'", path); 737return NULL; 738} 739 740 len =strlen(path); 741 size =cache_entry_size(len); 742 ce =xcalloc(1, size); 743 744hashcpy(ce->sha1, sha1); 745memcpy(ce->name, path, len); 746 ce->ce_flags =create_ce_flags(stage); 747 ce->ce_namelen = len; 748 ce->ce_mode =create_ce_mode(mode); 749 750 ret =refresh_cache_entry(ce, refresh_options); 751if(!ret) { 752free(ce); 753return NULL; 754}else{ 755return ret; 756} 757} 758 759intce_same_name(const struct cache_entry *a,const struct cache_entry *b) 760{ 761int len =ce_namelen(a); 762returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 763} 764 765/* 766 * We fundamentally don't like some paths: we don't want 767 * dot or dot-dot anywhere, and for obvious reasons don't 768 * want to recurse into ".git" either. 769 * 770 * Also, we don't want double slashes or slashes at the 771 * end that can make pathnames ambiguous. 772 */ 773static intverify_dotfile(const char*rest) 774{ 775/* 776 * The first character was '.', but that 777 * has already been discarded, we now test 778 * the rest. 779 */ 780 781/* "." is not allowed */ 782if(*rest =='\0'||is_dir_sep(*rest)) 783return0; 784 785switch(*rest) { 786/* 787 * ".git" followed by NUL or slash is bad. This 788 * shares the path end test with the ".." case. 789 */ 790case'g': 791case'G': 792if(rest[1] !='i'&& rest[1] !='I') 793break; 794if(rest[2] !='t'&& rest[2] !='T') 795break; 796 rest +=2; 797/* fallthrough */ 798case'.': 799if(rest[1] =='\0'||is_dir_sep(rest[1])) 800return0; 801} 802return1; 803} 804 805intverify_path(const char*path) 806{ 807char c; 808 809if(has_dos_drive_prefix(path)) 810return0; 811 812goto inside; 813for(;;) { 814if(!c) 815return1; 816if(is_dir_sep(c)) { 817inside: 818if(protect_hfs &&is_hfs_dotgit(path)) 819return0; 820if(protect_ntfs &&is_ntfs_dotgit(path)) 821return0; 822 c = *path++; 823if((c =='.'&& !verify_dotfile(path)) || 824is_dir_sep(c) || c =='\0') 825return0; 826} 827 c = *path++; 828} 829} 830 831/* 832 * Do we have another file that has the beginning components being a 833 * proper superset of the name we're trying to add? 834 */ 835static inthas_file_name(struct index_state *istate, 836const struct cache_entry *ce,int pos,int ok_to_replace) 837{ 838int retval =0; 839int len =ce_namelen(ce); 840int stage =ce_stage(ce); 841const char*name = ce->name; 842 843while(pos < istate->cache_nr) { 844struct cache_entry *p = istate->cache[pos++]; 845 846if(len >=ce_namelen(p)) 847break; 848if(memcmp(name, p->name, len)) 849break; 850if(ce_stage(p) != stage) 851continue; 852if(p->name[len] !='/') 853continue; 854if(p->ce_flags & CE_REMOVE) 855continue; 856 retval = -1; 857if(!ok_to_replace) 858break; 859remove_index_entry_at(istate, --pos); 860} 861return retval; 862} 863 864/* 865 * Do we have another file with a pathname that is a proper 866 * subset of the name we're trying to add? 867 */ 868static inthas_dir_name(struct index_state *istate, 869const struct cache_entry *ce,int pos,int ok_to_replace) 870{ 871int retval =0; 872int stage =ce_stage(ce); 873const char*name = ce->name; 874const char*slash = name +ce_namelen(ce); 875 876for(;;) { 877int len; 878 879for(;;) { 880if(*--slash =='/') 881break; 882if(slash <= ce->name) 883return retval; 884} 885 len = slash - name; 886 887 pos =index_name_stage_pos(istate, name, len, stage); 888if(pos >=0) { 889/* 890 * Found one, but not so fast. This could 891 * be a marker that says "I was here, but 892 * I am being removed". Such an entry is 893 * not a part of the resulting tree, and 894 * it is Ok to have a directory at the same 895 * path. 896 */ 897if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 898 retval = -1; 899if(!ok_to_replace) 900break; 901remove_index_entry_at(istate, pos); 902continue; 903} 904} 905else 906 pos = -pos-1; 907 908/* 909 * Trivial optimization: if we find an entry that 910 * already matches the sub-directory, then we know 911 * we're ok, and we can exit. 912 */ 913while(pos < istate->cache_nr) { 914struct cache_entry *p = istate->cache[pos]; 915if((ce_namelen(p) <= len) || 916(p->name[len] !='/') || 917memcmp(p->name, name, len)) 918break;/* not our subdirectory */ 919if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 920/* 921 * p is at the same stage as our entry, and 922 * is a subdirectory of what we are looking 923 * at, so we cannot have conflicts at our 924 * level or anything shorter. 925 */ 926return retval; 927 pos++; 928} 929} 930return retval; 931} 932 933/* We may be in a situation where we already have path/file and path 934 * is being added, or we already have path and path/file is being 935 * added. Either one would result in a nonsense tree that has path 936 * twice when git-write-tree tries to write it out. Prevent it. 937 * 938 * If ok-to-replace is specified, we remove the conflicting entries 939 * from the cache so the caller should recompute the insert position. 940 * When this happens, we return non-zero. 941 */ 942static intcheck_file_directory_conflict(struct index_state *istate, 943const struct cache_entry *ce, 944int pos,int ok_to_replace) 945{ 946int retval; 947 948/* 949 * When ce is an "I am going away" entry, we allow it to be added 950 */ 951if(ce->ce_flags & CE_REMOVE) 952return0; 953 954/* 955 * We check if the path is a sub-path of a subsequent pathname 956 * first, since removing those will not change the position 957 * in the array. 958 */ 959 retval =has_file_name(istate, ce, pos, ok_to_replace); 960 961/* 962 * Then check if the path might have a clashing sub-directory 963 * before it. 964 */ 965return retval +has_dir_name(istate, ce, pos, ok_to_replace); 966} 967 968static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 969{ 970int pos; 971int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 972int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 973int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 974int new_only = option & ADD_CACHE_NEW_ONLY; 975 976if(!(option & ADD_CACHE_KEEP_CACHE_TREE)) 977cache_tree_invalidate_path(istate, ce->name); 978 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce)); 979 980/* existing match? Just replace it. */ 981if(pos >=0) { 982if(!new_only) 983replace_index_entry(istate, pos, ce); 984return0; 985} 986 pos = -pos-1; 987 988/* 989 * Inserting a merged entry ("stage 0") into the index 990 * will always replace all non-merged entries.. 991 */ 992if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 993while(ce_same_name(istate->cache[pos], ce)) { 994 ok_to_add =1; 995if(!remove_index_entry_at(istate, pos)) 996break; 997} 998} 9991000if(!ok_to_add)1001return-1;1002if(!verify_path(ce->name))1003returnerror("Invalid path '%s'", ce->name);10041005if(!skip_df_check &&1006check_file_directory_conflict(istate, ce, pos, ok_to_replace)) {1007if(!ok_to_replace)1008returnerror("'%s' appears as both a file and as a directory",1009 ce->name);1010 pos =index_name_stage_pos(istate, ce->name,ce_namelen(ce),ce_stage(ce));1011 pos = -pos-1;1012}1013return pos +1;1014}10151016intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option)1017{1018int pos;10191020if(option & ADD_CACHE_JUST_APPEND)1021 pos = istate->cache_nr;1022else{1023int ret;1024 ret =add_index_entry_with_check(istate, ce, option);1025if(ret <=0)1026return ret;1027 pos = ret -1;1028}10291030/* Make sure the array is big enough .. */1031ALLOC_GROW(istate->cache, istate->cache_nr +1, istate->cache_alloc);10321033/* Add it in.. */1034 istate->cache_nr++;1035if(istate->cache_nr > pos +1)1036memmove(istate->cache + pos +1,1037 istate->cache + pos,1038(istate->cache_nr - pos -1) *sizeof(ce));1039set_index_entry(istate, pos, ce);1040 istate->cache_changed |= CE_ENTRY_ADDED;1041return0;1042}10431044/*1045 * "refresh" does not calculate a new sha1 file or bring the1046 * cache up-to-date for mode/content changes. But what it1047 * _does_ do is to "re-match" the stat information of a file1048 * with the cache, so that you can refresh the cache for a1049 * file that hasn't been changed but where the stat entry is1050 * out of date.1051 *1052 * For example, you'd want to do this after doing a "git-read-tree",1053 * to link up the stat cache details with the proper files.1054 */1055static struct cache_entry *refresh_cache_ent(struct index_state *istate,1056struct cache_entry *ce,1057unsigned int options,int*err,1058int*changed_ret)1059{1060struct stat st;1061struct cache_entry *updated;1062int changed, size;1063int refresh = options & CE_MATCH_REFRESH;1064int ignore_valid = options & CE_MATCH_IGNORE_VALID;1065int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;1066int ignore_missing = options & CE_MATCH_IGNORE_MISSING;10671068if(!refresh ||ce_uptodate(ce))1069return ce;10701071/*1072 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1073 * that the change to the work tree does not matter and told1074 * us not to worry.1075 */1076if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1077ce_mark_uptodate(ce);1078return ce;1079}1080if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1081ce_mark_uptodate(ce);1082return ce;1083}10841085if(has_symlink_leading_path(ce->name,ce_namelen(ce))) {1086if(ignore_missing)1087return ce;1088if(err)1089*err = ENOENT;1090return NULL;1091}10921093if(lstat(ce->name, &st) <0) {1094if(ignore_missing && errno == ENOENT)1095return ce;1096if(err)1097*err = errno;1098return NULL;1099}11001101 changed =ie_match_stat(istate, ce, &st, options);1102if(changed_ret)1103*changed_ret = changed;1104if(!changed) {1105/*1106 * The path is unchanged. If we were told to ignore1107 * valid bit, then we did the actual stat check and1108 * found that the entry is unmodified. If the entry1109 * is not marked VALID, this is the place to mark it1110 * valid again, under "assume unchanged" mode.1111 */1112if(ignore_valid && assume_unchanged &&1113!(ce->ce_flags & CE_VALID))1114;/* mark this one VALID again */1115else{1116/*1117 * We do not mark the index itself "modified"1118 * because CE_UPTODATE flag is in-core only;1119 * we are not going to write this change out.1120 */1121if(!S_ISGITLINK(ce->ce_mode))1122ce_mark_uptodate(ce);1123return ce;1124}1125}11261127if(ie_modified(istate, ce, &st, options)) {1128if(err)1129*err = EINVAL;1130return NULL;1131}11321133 size =ce_size(ce);1134 updated =xmalloc(size);1135memcpy(updated, ce, size);1136fill_stat_cache_info(updated, &st);1137/*1138 * If ignore_valid is not set, we should leave CE_VALID bit1139 * alone. Otherwise, paths marked with --no-assume-unchanged1140 * (i.e. things to be edited) will reacquire CE_VALID bit1141 * automatically, which is not really what we want.1142 */1143if(!ignore_valid && assume_unchanged &&1144!(ce->ce_flags & CE_VALID))1145 updated->ce_flags &= ~CE_VALID;11461147/* istate->cache_changed is updated in the caller */1148return updated;1149}11501151static voidshow_file(const char* fmt,const char* name,int in_porcelain,1152int* first,const char*header_msg)1153{1154if(in_porcelain && *first && header_msg) {1155printf("%s\n", header_msg);1156*first =0;1157}1158printf(fmt, name);1159}11601161intrefresh_index(struct index_state *istate,unsigned int flags,1162const struct pathspec *pathspec,1163char*seen,const char*header_msg)1164{1165int i;1166int has_errors =0;1167int really = (flags & REFRESH_REALLY) !=0;1168int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1169int quiet = (flags & REFRESH_QUIET) !=0;1170int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1171int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1172int first =1;1173int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1174unsigned int options = (CE_MATCH_REFRESH |1175(really ? CE_MATCH_IGNORE_VALID :0) |1176(not_new ? CE_MATCH_IGNORE_MISSING :0));1177const char*modified_fmt;1178const char*deleted_fmt;1179const char*typechange_fmt;1180const char*added_fmt;1181const char*unmerged_fmt;11821183 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1184 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1185 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1186 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1187 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1188for(i =0; i < istate->cache_nr; i++) {1189struct cache_entry *ce, *new;1190int cache_errno =0;1191int changed =0;1192int filtered =0;11931194 ce = istate->cache[i];1195if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1196continue;11971198if(pathspec && !ce_path_match(ce, pathspec, seen))1199 filtered =1;12001201if(ce_stage(ce)) {1202while((i < istate->cache_nr) &&1203!strcmp(istate->cache[i]->name, ce->name))1204 i++;1205 i--;1206if(allow_unmerged)1207continue;1208if(!filtered)1209show_file(unmerged_fmt, ce->name, in_porcelain,1210&first, header_msg);1211 has_errors =1;1212continue;1213}12141215if(filtered)1216continue;12171218new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1219if(new== ce)1220continue;1221if(!new) {1222const char*fmt;12231224if(really && cache_errno == EINVAL) {1225/* If we are doing --really-refresh that1226 * means the index is not valid anymore.1227 */1228 ce->ce_flags &= ~CE_VALID;1229 ce->ce_flags |= CE_UPDATE_IN_BASE;1230 istate->cache_changed |= CE_ENTRY_CHANGED;1231}1232if(quiet)1233continue;12341235if(cache_errno == ENOENT)1236 fmt = deleted_fmt;1237else if(ce->ce_flags & CE_INTENT_TO_ADD)1238 fmt = added_fmt;/* must be before other checks */1239else if(changed & TYPE_CHANGED)1240 fmt = typechange_fmt;1241else1242 fmt = modified_fmt;1243show_file(fmt,1244 ce->name, in_porcelain, &first, header_msg);1245 has_errors =1;1246continue;1247}12481249replace_index_entry(istate, i,new);1250}1251return has_errors;1252}12531254static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,1255unsigned int options)1256{1257returnrefresh_cache_ent(&the_index, ce, options, NULL, NULL);1258}125912601261/*****************************************************************1262 * Index File I/O1263 *****************************************************************/12641265#define INDEX_FORMAT_DEFAULT 312661267static unsigned intget_index_format_default(void)1268{1269char*envversion =getenv("GIT_INDEX_VERSION");1270char*endp;1271int value;1272unsigned int version = INDEX_FORMAT_DEFAULT;12731274if(!envversion) {1275if(!git_config_get_int("index.version", &value))1276 version = value;1277if(version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1278warning(_("index.version set, but the value is invalid.\n"1279"Using version%i"), INDEX_FORMAT_DEFAULT);1280return INDEX_FORMAT_DEFAULT;1281}1282return version;1283}12841285 version =strtoul(envversion, &endp,10);1286if(*endp ||1287 version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {1288warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"1289"Using version%i"), INDEX_FORMAT_DEFAULT);1290 version = INDEX_FORMAT_DEFAULT;1291}1292return version;1293}12941295/*1296 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1297 * Again - this is just a (very strong in practice) heuristic that1298 * the inode hasn't changed.1299 *1300 * We save the fields in big-endian order to allow using the1301 * index file over NFS transparently.1302 */1303struct ondisk_cache_entry {1304struct cache_time ctime;1305struct cache_time mtime;1306uint32_t dev;1307uint32_t ino;1308uint32_t mode;1309uint32_t uid;1310uint32_t gid;1311uint32_t size;1312unsigned char sha1[20];1313uint16_t flags;1314char name[FLEX_ARRAY];/* more */1315};13161317/*1318 * This struct is used when CE_EXTENDED bit is 11319 * The struct must match ondisk_cache_entry exactly from1320 * ctime till flags1321 */1322struct ondisk_cache_entry_extended {1323struct cache_time ctime;1324struct cache_time mtime;1325uint32_t dev;1326uint32_t ino;1327uint32_t mode;1328uint32_t uid;1329uint32_t gid;1330uint32_t size;1331unsigned char sha1[20];1332uint16_t flags;1333uint16_t flags2;1334char name[FLEX_ARRAY];/* more */1335};13361337/* These are only used for v3 or lower */1338#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1339#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1340#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1341#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1342 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1343 ondisk_cache_entry_size(ce_namelen(ce)))13441345static intverify_hdr(struct cache_header *hdr,unsigned long size)1346{1347 git_SHA_CTX c;1348unsigned char sha1[20];1349int hdr_version;13501351if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1352returnerror("bad signature");1353 hdr_version =ntohl(hdr->hdr_version);1354if(hdr_version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < hdr_version)1355returnerror("bad index version%d", hdr_version);1356git_SHA1_Init(&c);1357git_SHA1_Update(&c, hdr, size -20);1358git_SHA1_Final(sha1, &c);1359if(hashcmp(sha1, (unsigned char*)hdr + size -20))1360returnerror("bad index file sha1 signature");1361return0;1362}13631364static intread_index_extension(struct index_state *istate,1365const char*ext,void*data,unsigned long sz)1366{1367switch(CACHE_EXT(ext)) {1368case CACHE_EXT_TREE:1369 istate->cache_tree =cache_tree_read(data, sz);1370break;1371case CACHE_EXT_RESOLVE_UNDO:1372 istate->resolve_undo =resolve_undo_read(data, sz);1373break;1374case CACHE_EXT_LINK:1375if(read_link_extension(istate, data, sz))1376return-1;1377break;1378default:1379if(*ext <'A'||'Z'< *ext)1380returnerror("index uses %.4s extension, which we do not understand",1381 ext);1382fprintf(stderr,"ignoring %.4s extension\n", ext);1383break;1384}1385return0;1386}13871388inthold_locked_index(struct lock_file *lk,int die_on_error)1389{1390returnhold_lock_file_for_update(lk,get_index_file(),1391 die_on_error1392? LOCK_DIE_ON_ERROR1393:0);1394}13951396intread_index(struct index_state *istate)1397{1398returnread_index_from(istate,get_index_file());1399}14001401static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1402unsigned int flags,1403const char*name,1404size_t len)1405{1406struct cache_entry *ce =xmalloc(cache_entry_size(len));14071408 ce->ce_stat_data.sd_ctime.sec =get_be32(&ondisk->ctime.sec);1409 ce->ce_stat_data.sd_mtime.sec =get_be32(&ondisk->mtime.sec);1410 ce->ce_stat_data.sd_ctime.nsec =get_be32(&ondisk->ctime.nsec);1411 ce->ce_stat_data.sd_mtime.nsec =get_be32(&ondisk->mtime.nsec);1412 ce->ce_stat_data.sd_dev =get_be32(&ondisk->dev);1413 ce->ce_stat_data.sd_ino =get_be32(&ondisk->ino);1414 ce->ce_mode =get_be32(&ondisk->mode);1415 ce->ce_stat_data.sd_uid =get_be32(&ondisk->uid);1416 ce->ce_stat_data.sd_gid =get_be32(&ondisk->gid);1417 ce->ce_stat_data.sd_size =get_be32(&ondisk->size);1418 ce->ce_flags = flags & ~CE_NAMEMASK;1419 ce->ce_namelen = len;1420 ce->index =0;1421hashcpy(ce->sha1, ondisk->sha1);1422memcpy(ce->name, name, len);1423 ce->name[len] ='\0';1424return ce;1425}14261427/*1428 * Adjacent cache entries tend to share the leading paths, so it makes1429 * sense to only store the differences in later entries. In the v41430 * on-disk format of the index, each on-disk cache entry stores the1431 * number of bytes to be stripped from the end of the previous name,1432 * and the bytes to append to the result, to come up with its name.1433 */1434static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1435{1436const unsigned char*ep, *cp = (const unsigned char*)cp_;1437size_t len =decode_varint(&cp);14381439if(name->len < len)1440die("malformed name field in the index");1441strbuf_remove(name, name->len - len, len);1442for(ep = cp; *ep; ep++)1443;/* find the end */1444strbuf_add(name, cp, ep - cp);1445return(const char*)ep +1- cp_;1446}14471448static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1449unsigned long*ent_size,1450struct strbuf *previous_name)1451{1452struct cache_entry *ce;1453size_t len;1454const char*name;1455unsigned int flags;14561457/* On-disk flags are just 16 bits */1458 flags =get_be16(&ondisk->flags);1459 len = flags & CE_NAMEMASK;14601461if(flags & CE_EXTENDED) {1462struct ondisk_cache_entry_extended *ondisk2;1463int extended_flags;1464 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1465 extended_flags =get_be16(&ondisk2->flags2) <<16;1466/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1467if(extended_flags & ~CE_EXTENDED_FLAGS)1468die("Unknown index entry format%08x", extended_flags);1469 flags |= extended_flags;1470 name = ondisk2->name;1471}1472else1473 name = ondisk->name;14741475if(!previous_name) {1476/* v3 and earlier */1477if(len == CE_NAMEMASK)1478 len =strlen(name);1479 ce =cache_entry_from_ondisk(ondisk, flags, name, len);14801481*ent_size =ondisk_ce_size(ce);1482}else{1483unsigned long consumed;1484 consumed =expand_name_field(previous_name, name);1485 ce =cache_entry_from_ondisk(ondisk, flags,1486 previous_name->buf,1487 previous_name->len);14881489*ent_size = (name - ((char*)ondisk)) + consumed;1490}1491return ce;1492}14931494static voidcheck_ce_order(struct cache_entry *ce,struct cache_entry *next_ce)1495{1496int name_compare =strcmp(ce->name, next_ce->name);1497if(0< name_compare)1498die("unordered stage entries in index");1499if(!name_compare) {1500if(!ce_stage(ce))1501die("multiple stage entries for merged file '%s'",1502 ce->name);1503if(ce_stage(ce) >ce_stage(next_ce))1504die("unordered stage entries for '%s'",1505 ce->name);1506}1507}15081509/* remember to discard_cache() before reading a different cache! */1510intdo_read_index(struct index_state *istate,const char*path,int must_exist)1511{1512int fd, i;1513struct stat st;1514unsigned long src_offset;1515struct cache_header *hdr;1516void*mmap;1517size_t mmap_size;1518struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;15191520if(istate->initialized)1521return istate->cache_nr;15221523 istate->timestamp.sec =0;1524 istate->timestamp.nsec =0;1525 fd =open(path, O_RDONLY);1526if(fd <0) {1527if(!must_exist && errno == ENOENT)1528return0;1529die_errno("%s: index file open failed", path);1530}15311532if(fstat(fd, &st))1533die_errno("cannot stat the open index");15341535 mmap_size =xsize_t(st.st_size);1536if(mmap_size <sizeof(struct cache_header) +20)1537die("index file smaller than expected");15381539 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1540if(mmap == MAP_FAILED)1541die_errno("unable to map index file");1542close(fd);15431544 hdr = mmap;1545if(verify_hdr(hdr, mmap_size) <0)1546goto unmap;15471548hashcpy(istate->sha1, (const unsigned char*)hdr + mmap_size -20);1549 istate->version =ntohl(hdr->hdr_version);1550 istate->cache_nr =ntohl(hdr->hdr_entries);1551 istate->cache_alloc =alloc_nr(istate->cache_nr);1552 istate->cache =xcalloc(istate->cache_alloc,sizeof(*istate->cache));1553 istate->initialized =1;15541555if(istate->version ==4)1556 previous_name = &previous_name_buf;1557else1558 previous_name = NULL;15591560 src_offset =sizeof(*hdr);1561for(i =0; i < istate->cache_nr; i++) {1562struct ondisk_cache_entry *disk_ce;1563struct cache_entry *ce;1564unsigned long consumed;15651566 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1567 ce =create_from_disk(disk_ce, &consumed, previous_name);1568set_index_entry(istate, i, ce);15691570if(i >0)1571check_ce_order(istate->cache[i -1], ce);15721573 src_offset += consumed;1574}1575strbuf_release(&previous_name_buf);1576 istate->timestamp.sec = st.st_mtime;1577 istate->timestamp.nsec =ST_MTIME_NSEC(st);15781579while(src_offset <= mmap_size -20-8) {1580/* After an array of active_nr index entries,1581 * there can be arbitrary number of extended1582 * sections, each of which is prefixed with1583 * extension name (4-byte) and section length1584 * in 4-byte network byte order.1585 */1586uint32_t extsize;1587memcpy(&extsize, (char*)mmap + src_offset +4,4);1588 extsize =ntohl(extsize);1589if(read_index_extension(istate,1590(const char*) mmap + src_offset,1591(char*) mmap + src_offset +8,1592 extsize) <0)1593goto unmap;1594 src_offset +=8;1595 src_offset += extsize;1596}1597munmap(mmap, mmap_size);1598return istate->cache_nr;15991600unmap:1601munmap(mmap, mmap_size);1602die("index file corrupt");1603}16041605intread_index_from(struct index_state *istate,const char*path)1606{1607struct split_index *split_index;1608int ret;16091610/* istate->initialized covers both .git/index and .git/sharedindex.xxx */1611if(istate->initialized)1612return istate->cache_nr;16131614 ret =do_read_index(istate, path,0);1615 split_index = istate->split_index;1616if(!split_index)1617return ret;16181619if(is_null_sha1(split_index->base_sha1))1620return ret;16211622if(split_index->base)1623discard_index(split_index->base);1624else1625 split_index->base =xcalloc(1,sizeof(*split_index->base));1626 ret =do_read_index(split_index->base,1627git_path("sharedindex.%s",1628sha1_to_hex(split_index->base_sha1)),1);1629if(hashcmp(split_index->base_sha1, split_index->base->sha1))1630die("broken index, expect%sin%s, got%s",1631sha1_to_hex(split_index->base_sha1),1632git_path("sharedindex.%s",1633sha1_to_hex(split_index->base_sha1)),1634sha1_to_hex(split_index->base->sha1));1635merge_base_index(istate);1636return ret;1637}16381639intis_index_unborn(struct index_state *istate)1640{1641return(!istate->cache_nr && !istate->timestamp.sec);1642}16431644intdiscard_index(struct index_state *istate)1645{1646int i;16471648for(i =0; i < istate->cache_nr; i++) {1649if(istate->cache[i]->index &&1650 istate->split_index &&1651 istate->split_index->base &&1652 istate->cache[i]->index <= istate->split_index->base->cache_nr &&1653 istate->cache[i] == istate->split_index->base->cache[istate->cache[i]->index -1])1654continue;1655free(istate->cache[i]);1656}1657resolve_undo_clear_index(istate);1658 istate->cache_nr =0;1659 istate->cache_changed =0;1660 istate->timestamp.sec =0;1661 istate->timestamp.nsec =0;1662free_name_hash(istate);1663cache_tree_free(&(istate->cache_tree));1664 istate->initialized =0;1665free(istate->cache);1666 istate->cache = NULL;1667 istate->cache_alloc =0;1668discard_split_index(istate);1669return0;1670}16711672intunmerged_index(const struct index_state *istate)1673{1674int i;1675for(i =0; i < istate->cache_nr; i++) {1676if(ce_stage(istate->cache[i]))1677return1;1678}1679return0;1680}16811682#define WRITE_BUFFER_SIZE 81921683static unsigned char write_buffer[WRITE_BUFFER_SIZE];1684static unsigned long write_buffer_len;16851686static intce_write_flush(git_SHA_CTX *context,int fd)1687{1688unsigned int buffered = write_buffer_len;1689if(buffered) {1690git_SHA1_Update(context, write_buffer, buffered);1691if(write_in_full(fd, write_buffer, buffered) != buffered)1692return-1;1693 write_buffer_len =0;1694}1695return0;1696}16971698static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1699{1700while(len) {1701unsigned int buffered = write_buffer_len;1702unsigned int partial = WRITE_BUFFER_SIZE - buffered;1703if(partial > len)1704 partial = len;1705memcpy(write_buffer + buffered, data, partial);1706 buffered += partial;1707if(buffered == WRITE_BUFFER_SIZE) {1708 write_buffer_len = buffered;1709if(ce_write_flush(context, fd))1710return-1;1711 buffered =0;1712}1713 write_buffer_len = buffered;1714 len -= partial;1715 data = (char*) data + partial;1716}1717return0;1718}17191720static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1721unsigned int ext,unsigned int sz)1722{1723 ext =htonl(ext);1724 sz =htonl(sz);1725return((ce_write(context, fd, &ext,4) <0) ||1726(ce_write(context, fd, &sz,4) <0)) ? -1:0;1727}17281729static intce_flush(git_SHA_CTX *context,int fd,unsigned char*sha1)1730{1731unsigned int left = write_buffer_len;17321733if(left) {1734 write_buffer_len =0;1735git_SHA1_Update(context, write_buffer, left);1736}17371738/* Flush first if not enough space for SHA1 signature */1739if(left +20> WRITE_BUFFER_SIZE) {1740if(write_in_full(fd, write_buffer, left) != left)1741return-1;1742 left =0;1743}17441745/* Append the SHA1 signature at the end */1746git_SHA1_Final(write_buffer + left, context);1747hashcpy(sha1, write_buffer + left);1748 left +=20;1749return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1750}17511752static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1753{1754/*1755 * The only thing we care about in this function is to smudge the1756 * falsely clean entry due to touch-update-touch race, so we leave1757 * everything else as they are. We are called for entries whose1758 * ce_stat_data.sd_mtime match the index file mtime.1759 *1760 * Note that this actually does not do much for gitlinks, for1761 * which ce_match_stat_basic() always goes to the actual1762 * contents. The caller checks with is_racy_timestamp() which1763 * always says "no" for gitlinks, so we are not called for them ;-)1764 */1765struct stat st;17661767if(lstat(ce->name, &st) <0)1768return;1769if(ce_match_stat_basic(ce, &st))1770return;1771if(ce_modified_check_fs(ce, &st)) {1772/* This is "racily clean"; smudge it. Note that this1773 * is a tricky code. At first glance, it may appear1774 * that it can break with this sequence:1775 *1776 * $ echo xyzzy >frotz1777 * $ git-update-index --add frotz1778 * $ : >frotz1779 * $ sleep 31780 * $ echo filfre >nitfol1781 * $ git-update-index --add nitfol1782 *1783 * but it does not. When the second update-index runs,1784 * it notices that the entry "frotz" has the same timestamp1785 * as index, and if we were to smudge it by resetting its1786 * size to zero here, then the object name recorded1787 * in index is the 6-byte file but the cached stat information1788 * becomes zero --- which would then match what we would1789 * obtain from the filesystem next time we stat("frotz").1790 *1791 * However, the second update-index, before calling1792 * this function, notices that the cached size is 61793 * bytes and what is on the filesystem is an empty1794 * file, and never calls us, so the cached size information1795 * for "frotz" stays 6 which does not match the filesystem.1796 */1797 ce->ce_stat_data.sd_size =0;1798}1799}18001801/* Copy miscellaneous fields but not the name */1802static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1803struct cache_entry *ce)1804{1805short flags;18061807 ondisk->ctime.sec =htonl(ce->ce_stat_data.sd_ctime.sec);1808 ondisk->mtime.sec =htonl(ce->ce_stat_data.sd_mtime.sec);1809 ondisk->ctime.nsec =htonl(ce->ce_stat_data.sd_ctime.nsec);1810 ondisk->mtime.nsec =htonl(ce->ce_stat_data.sd_mtime.nsec);1811 ondisk->dev =htonl(ce->ce_stat_data.sd_dev);1812 ondisk->ino =htonl(ce->ce_stat_data.sd_ino);1813 ondisk->mode =htonl(ce->ce_mode);1814 ondisk->uid =htonl(ce->ce_stat_data.sd_uid);1815 ondisk->gid =htonl(ce->ce_stat_data.sd_gid);1816 ondisk->size =htonl(ce->ce_stat_data.sd_size);1817hashcpy(ondisk->sha1, ce->sha1);18181819 flags = ce->ce_flags & ~CE_NAMEMASK;1820 flags |= (ce_namelen(ce) >= CE_NAMEMASK ? CE_NAMEMASK :ce_namelen(ce));1821 ondisk->flags =htons(flags);1822if(ce->ce_flags & CE_EXTENDED) {1823struct ondisk_cache_entry_extended *ondisk2;1824 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1825 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1826return ondisk2->name;1827}1828else{1829return ondisk->name;1830}1831}18321833static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce,1834struct strbuf *previous_name)1835{1836int size;1837struct ondisk_cache_entry *ondisk;1838int saved_namelen = saved_namelen;/* compiler workaround */1839char*name;1840int result;18411842if(ce->ce_flags & CE_STRIP_NAME) {1843 saved_namelen =ce_namelen(ce);1844 ce->ce_namelen =0;1845}18461847if(!previous_name) {1848 size =ondisk_ce_size(ce);1849 ondisk =xcalloc(1, size);1850 name =copy_cache_entry_to_ondisk(ondisk, ce);1851memcpy(name, ce->name,ce_namelen(ce));1852}else{1853int common, to_remove, prefix_size;1854unsigned char to_remove_vi[16];1855for(common =0;1856(ce->name[common] &&1857 common < previous_name->len &&1858 ce->name[common] == previous_name->buf[common]);1859 common++)1860;/* still matching */1861 to_remove = previous_name->len - common;1862 prefix_size =encode_varint(to_remove, to_remove_vi);18631864if(ce->ce_flags & CE_EXTENDED)1865 size =offsetof(struct ondisk_cache_entry_extended, name);1866else1867 size =offsetof(struct ondisk_cache_entry, name);1868 size += prefix_size + (ce_namelen(ce) - common +1);18691870 ondisk =xcalloc(1, size);1871 name =copy_cache_entry_to_ondisk(ondisk, ce);1872memcpy(name, to_remove_vi, prefix_size);1873memcpy(name + prefix_size, ce->name + common,ce_namelen(ce) - common);18741875strbuf_splice(previous_name, common, to_remove,1876 ce->name + common,ce_namelen(ce) - common);1877}1878if(ce->ce_flags & CE_STRIP_NAME) {1879 ce->ce_namelen = saved_namelen;1880 ce->ce_flags &= ~CE_STRIP_NAME;1881}18821883 result =ce_write(c, fd, ondisk, size);1884free(ondisk);1885return result;1886}18871888/*1889 * This function verifies if index_state has the correct sha1 of the1890 * index file. Don't die if we have any other failure, just return 0.1891 */1892static intverify_index_from(const struct index_state *istate,const char*path)1893{1894int fd;1895 ssize_t n;1896struct stat st;1897unsigned char sha1[20];18981899if(!istate->initialized)1900return0;19011902 fd =open(path, O_RDONLY);1903if(fd <0)1904return0;19051906if(fstat(fd, &st))1907goto out;19081909if(st.st_size <sizeof(struct cache_header) +20)1910goto out;19111912 n =pread_in_full(fd, sha1,20, st.st_size -20);1913if(n !=20)1914goto out;19151916if(hashcmp(istate->sha1, sha1))1917goto out;19181919close(fd);1920return1;19211922out:1923close(fd);1924return0;1925}19261927static intverify_index(const struct index_state *istate)1928{1929returnverify_index_from(istate,get_index_file());1930}19311932static inthas_racy_timestamp(struct index_state *istate)1933{1934int entries = istate->cache_nr;1935int i;19361937for(i =0; i < entries; i++) {1938struct cache_entry *ce = istate->cache[i];1939if(is_racy_timestamp(istate, ce))1940return1;1941}1942return0;1943}19441945/*1946 * Opportunistically update the index but do not complain if we can't1947 */1948voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1949{1950if((istate->cache_changed ||has_racy_timestamp(istate)) &&1951verify_index(istate) &&1952write_locked_index(istate, lockfile, COMMIT_LOCK))1953rollback_lock_file(lockfile);1954}19551956static intdo_write_index(struct index_state *istate,int newfd,1957int strip_extensions)1958{1959 git_SHA_CTX c;1960struct cache_header hdr;1961int i, err, removed, extended, hdr_version;1962struct cache_entry **cache = istate->cache;1963int entries = istate->cache_nr;1964struct stat st;1965struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;19661967for(i = removed = extended =0; i < entries; i++) {1968if(cache[i]->ce_flags & CE_REMOVE)1969 removed++;19701971/* reduce extended entries if possible */1972 cache[i]->ce_flags &= ~CE_EXTENDED;1973if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1974 extended++;1975 cache[i]->ce_flags |= CE_EXTENDED;1976}1977}19781979if(!istate->version) {1980 istate->version =get_index_format_default();1981if(getenv("GIT_TEST_SPLIT_INDEX"))1982init_split_index(istate);1983}19841985/* demote version 3 to version 2 when the latter suffices */1986if(istate->version ==3|| istate->version ==2)1987 istate->version = extended ?3:2;19881989 hdr_version = istate->version;19901991 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1992 hdr.hdr_version =htonl(hdr_version);1993 hdr.hdr_entries =htonl(entries - removed);19941995git_SHA1_Init(&c);1996if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1997return-1;19981999 previous_name = (hdr_version ==4) ? &previous_name_buf : NULL;2000for(i =0; i < entries; i++) {2001struct cache_entry *ce = cache[i];2002if(ce->ce_flags & CE_REMOVE)2003continue;2004if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))2005ce_smudge_racily_clean_entry(ce);2006if(is_null_sha1(ce->sha1)) {2007static const char msg[] ="cache entry has null sha1:%s";2008static int allow = -1;20092010if(allow <0)2011 allow =git_env_bool("GIT_ALLOW_NULL_SHA1",0);2012if(allow)2013warning(msg, ce->name);2014else2015returnerror(msg, ce->name);2016}2017if(ce_write_entry(&c, newfd, ce, previous_name) <0)2018return-1;2019}2020strbuf_release(&previous_name_buf);20212022/* Write extension data here */2023if(!strip_extensions && istate->split_index) {2024struct strbuf sb = STRBUF_INIT;20252026 err =write_link_extension(&sb, istate) <0||2027write_index_ext_header(&c, newfd, CACHE_EXT_LINK,2028 sb.len) <0||2029ce_write(&c, newfd, sb.buf, sb.len) <0;2030strbuf_release(&sb);2031if(err)2032return-1;2033}2034if(!strip_extensions && istate->cache_tree) {2035struct strbuf sb = STRBUF_INIT;20362037cache_tree_write(&sb, istate->cache_tree);2038 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <02039||ce_write(&c, newfd, sb.buf, sb.len) <0;2040strbuf_release(&sb);2041if(err)2042return-1;2043}2044if(!strip_extensions && istate->resolve_undo) {2045struct strbuf sb = STRBUF_INIT;20462047resolve_undo_write(&sb, istate->resolve_undo);2048 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,2049 sb.len) <02050||ce_write(&c, newfd, sb.buf, sb.len) <0;2051strbuf_release(&sb);2052if(err)2053return-1;2054}20552056if(ce_flush(&c, newfd, istate->sha1) ||fstat(newfd, &st))2057return-1;2058 istate->timestamp.sec = (unsigned int)st.st_mtime;2059 istate->timestamp.nsec =ST_MTIME_NSEC(st);2060return0;2061}20622063voidset_alternate_index_output(const char*name)2064{2065 alternate_index_output = name;2066}20672068static intcommit_locked_index(struct lock_file *lk)2069{2070if(alternate_index_output)2071returncommit_lock_file_to(lk, alternate_index_output);2072else2073returncommit_lock_file(lk);2074}20752076static intdo_write_locked_index(struct index_state *istate,struct lock_file *lock,2077unsigned flags)2078{2079int ret =do_write_index(istate, lock->fd,0);2080if(ret)2081return ret;2082assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=2083(COMMIT_LOCK | CLOSE_LOCK));2084if(flags & COMMIT_LOCK)2085returncommit_locked_index(lock);2086else if(flags & CLOSE_LOCK)2087returnclose_lock_file(lock);2088else2089return ret;2090}20912092static intwrite_split_index(struct index_state *istate,2093struct lock_file *lock,2094unsigned flags)2095{2096int ret;2097prepare_to_write_split_index(istate);2098 ret =do_write_locked_index(istate, lock, flags);2099finish_writing_split_index(istate);2100return ret;2101}21022103static char*temporary_sharedindex;21042105static voidremove_temporary_sharedindex(void)2106{2107if(temporary_sharedindex) {2108unlink_or_warn(temporary_sharedindex);2109free(temporary_sharedindex);2110 temporary_sharedindex = NULL;2111}2112}21132114static voidremove_temporary_sharedindex_on_signal(int signo)2115{2116remove_temporary_sharedindex();2117sigchain_pop(signo);2118raise(signo);2119}21202121static intwrite_shared_index(struct index_state *istate,2122struct lock_file *lock,unsigned flags)2123{2124struct split_index *si = istate->split_index;2125static int installed_handler;2126int fd, ret;21272128 temporary_sharedindex =git_pathdup("sharedindex_XXXXXX");2129 fd =mkstemp(temporary_sharedindex);2130if(fd <0) {2131free(temporary_sharedindex);2132 temporary_sharedindex = NULL;2133hashclr(si->base_sha1);2134returndo_write_locked_index(istate, lock, flags);2135}2136if(!installed_handler) {2137atexit(remove_temporary_sharedindex);2138sigchain_push_common(remove_temporary_sharedindex_on_signal);2139}2140move_cache_to_base_index(istate);2141 ret =do_write_index(si->base, fd,1);2142close(fd);2143if(ret) {2144remove_temporary_sharedindex();2145return ret;2146}2147 ret =rename(temporary_sharedindex,2148git_path("sharedindex.%s",sha1_to_hex(si->base->sha1)));2149free(temporary_sharedindex);2150 temporary_sharedindex = NULL;2151if(!ret)2152hashcpy(si->base_sha1, si->base->sha1);2153return ret;2154}21552156intwrite_locked_index(struct index_state *istate,struct lock_file *lock,2157unsigned flags)2158{2159struct split_index *si = istate->split_index;21602161if(!si || alternate_index_output ||2162(istate->cache_changed & ~EXTMASK)) {2163if(si)2164hashclr(si->base_sha1);2165returndo_write_locked_index(istate, lock, flags);2166}21672168if(getenv("GIT_TEST_SPLIT_INDEX")) {2169int v = si->base_sha1[0];2170if((v &15) <6)2171 istate->cache_changed |= SPLIT_INDEX_ORDERED;2172}2173if(istate->cache_changed & SPLIT_INDEX_ORDERED) {2174int ret =write_shared_index(istate, lock, flags);2175if(ret)2176return ret;2177}21782179returnwrite_split_index(istate, lock, flags);2180}21812182/*2183 * Read the index file that is potentially unmerged into given2184 * index_state, dropping any unmerged entries. Returns true if2185 * the index is unmerged. Callers who want to refuse to work2186 * from an unmerged state can call this and check its return value,2187 * instead of calling read_cache().2188 */2189intread_index_unmerged(struct index_state *istate)2190{2191int i;2192int unmerged =0;21932194read_index(istate);2195for(i =0; i < istate->cache_nr; i++) {2196struct cache_entry *ce = istate->cache[i];2197struct cache_entry *new_ce;2198int size, len;21992200if(!ce_stage(ce))2201continue;2202 unmerged =1;2203 len =ce_namelen(ce);2204 size =cache_entry_size(len);2205 new_ce =xcalloc(1, size);2206memcpy(new_ce->name, ce->name, len);2207 new_ce->ce_flags =create_ce_flags(0) | CE_CONFLICTED;2208 new_ce->ce_namelen = len;2209 new_ce->ce_mode = ce->ce_mode;2210if(add_index_entry(istate, new_ce,0))2211returnerror("%s: cannot drop to stage #0",2212 new_ce->name);2213}2214return unmerged;2215}22162217/*2218 * Returns 1 if the path is an "other" path with respect to2219 * the index; that is, the path is not mentioned in the index at all,2220 * either as a file, a directory with some files in the index,2221 * or as an unmerged entry.2222 *2223 * We helpfully remove a trailing "/" from directories so that2224 * the output of read_directory can be used as-is.2225 */2226intindex_name_is_other(const struct index_state *istate,const char*name,2227int namelen)2228{2229int pos;2230if(namelen && name[namelen -1] =='/')2231 namelen--;2232 pos =index_name_pos(istate, name, namelen);2233if(0<= pos)2234return0;/* exact match */2235 pos = -pos -1;2236if(pos < istate->cache_nr) {2237struct cache_entry *ce = istate->cache[pos];2238if(ce_namelen(ce) == namelen &&2239!memcmp(ce->name, name, namelen))2240return0;/* Yup, this one exists unmerged */2241}2242return1;2243}22442245void*read_blob_data_from_index(struct index_state *istate,const char*path,unsigned long*size)2246{2247int pos, len;2248unsigned long sz;2249enum object_type type;2250void*data;22512252 len =strlen(path);2253 pos =index_name_pos(istate, path, len);2254if(pos <0) {2255/*2256 * We might be in the middle of a merge, in which2257 * case we would read stage #2 (ours).2258 */2259int i;2260for(i = -pos -1;2261(pos <0&& i < istate->cache_nr &&2262!strcmp(istate->cache[i]->name, path));2263 i++)2264if(ce_stage(istate->cache[i]) ==2)2265 pos = i;2266}2267if(pos <0)2268return NULL;2269 data =read_sha1_file(istate->cache[pos]->sha1, &type, &sz);2270if(!data || type != OBJ_BLOB) {2271free(data);2272return NULL;2273}2274if(size)2275*size = sz;2276return data;2277}22782279voidstat_validity_clear(struct stat_validity *sv)2280{2281free(sv->sd);2282 sv->sd = NULL;2283}22842285intstat_validity_check(struct stat_validity *sv,const char*path)2286{2287struct stat st;22882289if(stat(path, &st) <0)2290return sv->sd == NULL;2291if(!sv->sd)2292return0;2293returnS_ISREG(st.st_mode) && !match_stat_data(sv->sd, &st);2294}22952296voidstat_validity_update(struct stat_validity *sv,int fd)2297{2298struct stat st;22992300if(fstat(fd, &st) <0|| !S_ISREG(st.st_mode))2301stat_validity_clear(sv);2302else{2303if(!sv->sd)2304 sv->sd =xcalloc(1,sizeof(struct stat_data));2305fill_stat_data(sv->sd, &st);2306}2307}