1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#define NO_THE_INDEX_COMPATIBILITY_MACROS 7#include"cache.h" 8#include"cache-tree.h" 9#include"refs.h" 10#include"dir.h" 11#include"tree.h" 12#include"commit.h" 13#include"blob.h" 14#include"resolve-undo.h" 15#include"strbuf.h" 16#include"varint.h" 17 18static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really); 19 20/* Index extensions. 21 * 22 * The first letter should be 'A'..'Z' for extensions that are not 23 * necessary for a correct operation (i.e. optimization data). 24 * When new extensions are added that _needs_ to be understood in 25 * order to correctly interpret the index file, pick character that 26 * is outside the range, to cause the reader to abort. 27 */ 28 29#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 30#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 31#define CACHE_EXT_RESOLVE_UNDO 0x52455543/* "REUC" */ 32 33struct index_state the_index; 34 35static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 36{ 37 istate->cache[nr] = ce; 38add_name_hash(istate, ce); 39} 40 41static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 42{ 43struct cache_entry *old = istate->cache[nr]; 44 45remove_name_hash(old); 46set_index_entry(istate, nr, ce); 47 istate->cache_changed =1; 48} 49 50voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 51{ 52struct cache_entry *old = istate->cache[nr], *new; 53int namelen =strlen(new_name); 54 55new=xmalloc(cache_entry_size(namelen)); 56copy_cache_entry(new, old); 57new->ce_flags &= ~(CE_STATE_MASK | CE_NAMEMASK); 58new->ce_flags |= (namelen >= CE_NAMEMASK ? CE_NAMEMASK : namelen); 59memcpy(new->name, new_name, namelen +1); 60 61cache_tree_invalidate_path(istate->cache_tree, old->name); 62remove_index_entry_at(istate, nr); 63add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 64} 65 66/* 67 * This only updates the "non-critical" parts of the directory 68 * cache, ie the parts that aren't tracked by GIT, and only used 69 * to validate the cache. 70 */ 71voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 72{ 73 ce->ce_ctime.sec = (unsigned int)st->st_ctime; 74 ce->ce_mtime.sec = (unsigned int)st->st_mtime; 75 ce->ce_ctime.nsec =ST_CTIME_NSEC(*st); 76 ce->ce_mtime.nsec =ST_MTIME_NSEC(*st); 77 ce->ce_dev = st->st_dev; 78 ce->ce_ino = st->st_ino; 79 ce->ce_uid = st->st_uid; 80 ce->ce_gid = st->st_gid; 81 ce->ce_size = st->st_size; 82 83if(assume_unchanged) 84 ce->ce_flags |= CE_VALID; 85 86if(S_ISREG(st->st_mode)) 87ce_mark_uptodate(ce); 88} 89 90static intce_compare_data(struct cache_entry *ce,struct stat *st) 91{ 92int match = -1; 93int fd =open(ce->name, O_RDONLY); 94 95if(fd >=0) { 96unsigned char sha1[20]; 97if(!index_fd(sha1, fd, st, OBJ_BLOB, ce->name,0)) 98 match =hashcmp(sha1, ce->sha1); 99/* index_fd() closed the file descriptor already */ 100} 101return match; 102} 103 104static intce_compare_link(struct cache_entry *ce,size_t expected_size) 105{ 106int match = -1; 107void*buffer; 108unsigned long size; 109enum object_type type; 110struct strbuf sb = STRBUF_INIT; 111 112if(strbuf_readlink(&sb, ce->name, expected_size)) 113return-1; 114 115 buffer =read_sha1_file(ce->sha1, &type, &size); 116if(buffer) { 117if(size == sb.len) 118 match =memcmp(buffer, sb.buf, size); 119free(buffer); 120} 121strbuf_release(&sb); 122return match; 123} 124 125static intce_compare_gitlink(struct cache_entry *ce) 126{ 127unsigned char sha1[20]; 128 129/* 130 * We don't actually require that the .git directory 131 * under GITLINK directory be a valid git directory. It 132 * might even be missing (in case nobody populated that 133 * sub-project). 134 * 135 * If so, we consider it always to match. 136 */ 137if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 138return0; 139returnhashcmp(sha1, ce->sha1); 140} 141 142static intce_modified_check_fs(struct cache_entry *ce,struct stat *st) 143{ 144switch(st->st_mode & S_IFMT) { 145case S_IFREG: 146if(ce_compare_data(ce, st)) 147return DATA_CHANGED; 148break; 149case S_IFLNK: 150if(ce_compare_link(ce,xsize_t(st->st_size))) 151return DATA_CHANGED; 152break; 153case S_IFDIR: 154if(S_ISGITLINK(ce->ce_mode)) 155returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 156default: 157return TYPE_CHANGED; 158} 159return0; 160} 161 162static intis_empty_blob_sha1(const unsigned char*sha1) 163{ 164static const unsigned char empty_blob_sha1[20] = { 1650xe6,0x9d,0xe2,0x9b,0xb2,0xd1,0xd6,0x43,0x4b,0x8b, 1660x29,0xae,0x77,0x5a,0xd8,0xc2,0xe4,0x8c,0x53,0x91 167}; 168 169return!hashcmp(sha1, empty_blob_sha1); 170} 171 172static intce_match_stat_basic(struct cache_entry *ce,struct stat *st) 173{ 174unsigned int changed =0; 175 176if(ce->ce_flags & CE_REMOVE) 177return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 178 179switch(ce->ce_mode & S_IFMT) { 180case S_IFREG: 181 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 182/* We consider only the owner x bit to be relevant for 183 * "mode changes" 184 */ 185if(trust_executable_bit && 186(0100& (ce->ce_mode ^ st->st_mode))) 187 changed |= MODE_CHANGED; 188break; 189case S_IFLNK: 190if(!S_ISLNK(st->st_mode) && 191(has_symlinks || !S_ISREG(st->st_mode))) 192 changed |= TYPE_CHANGED; 193break; 194case S_IFGITLINK: 195/* We ignore most of the st_xxx fields for gitlinks */ 196if(!S_ISDIR(st->st_mode)) 197 changed |= TYPE_CHANGED; 198else if(ce_compare_gitlink(ce)) 199 changed |= DATA_CHANGED; 200return changed; 201default: 202die("internal error: ce_mode is%o", ce->ce_mode); 203} 204if(ce->ce_mtime.sec != (unsigned int)st->st_mtime) 205 changed |= MTIME_CHANGED; 206if(trust_ctime && ce->ce_ctime.sec != (unsigned int)st->st_ctime) 207 changed |= CTIME_CHANGED; 208 209#ifdef USE_NSEC 210if(ce->ce_mtime.nsec !=ST_MTIME_NSEC(*st)) 211 changed |= MTIME_CHANGED; 212if(trust_ctime && ce->ce_ctime.nsec !=ST_CTIME_NSEC(*st)) 213 changed |= CTIME_CHANGED; 214#endif 215 216if(ce->ce_uid != (unsigned int) st->st_uid || 217 ce->ce_gid != (unsigned int) st->st_gid) 218 changed |= OWNER_CHANGED; 219if(ce->ce_ino != (unsigned int) st->st_ino) 220 changed |= INODE_CHANGED; 221 222#ifdef USE_STDEV 223/* 224 * st_dev breaks on network filesystems where different 225 * clients will have different views of what "device" 226 * the filesystem is on 227 */ 228if(ce->ce_dev != (unsigned int) st->st_dev) 229 changed |= INODE_CHANGED; 230#endif 231 232if(ce->ce_size != (unsigned int) st->st_size) 233 changed |= DATA_CHANGED; 234 235/* Racily smudged entry? */ 236if(!ce->ce_size) { 237if(!is_empty_blob_sha1(ce->sha1)) 238 changed |= DATA_CHANGED; 239} 240 241return changed; 242} 243 244static intis_racy_timestamp(const struct index_state *istate,struct cache_entry *ce) 245{ 246return(!S_ISGITLINK(ce->ce_mode) && 247 istate->timestamp.sec && 248#ifdef USE_NSEC 249/* nanosecond timestamped files can also be racy! */ 250(istate->timestamp.sec < ce->ce_mtime.sec || 251(istate->timestamp.sec == ce->ce_mtime.sec && 252 istate->timestamp.nsec <= ce->ce_mtime.nsec)) 253#else 254 istate->timestamp.sec <= ce->ce_mtime.sec 255#endif 256); 257} 258 259intie_match_stat(const struct index_state *istate, 260struct cache_entry *ce,struct stat *st, 261unsigned int options) 262{ 263unsigned int changed; 264int ignore_valid = options & CE_MATCH_IGNORE_VALID; 265int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE; 266int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 267 268/* 269 * If it's marked as always valid in the index, it's 270 * valid whatever the checked-out copy says. 271 * 272 * skip-worktree has the same effect with higher precedence 273 */ 274if(!ignore_skip_worktree &&ce_skip_worktree(ce)) 275return0; 276if(!ignore_valid && (ce->ce_flags & CE_VALID)) 277return0; 278 279/* 280 * Intent-to-add entries have not been added, so the index entry 281 * by definition never matches what is in the work tree until it 282 * actually gets added. 283 */ 284if(ce->ce_flags & CE_INTENT_TO_ADD) 285return DATA_CHANGED | TYPE_CHANGED | MODE_CHANGED; 286 287 changed =ce_match_stat_basic(ce, st); 288 289/* 290 * Within 1 second of this sequence: 291 * echo xyzzy >file && git-update-index --add file 292 * running this command: 293 * echo frotz >file 294 * would give a falsely clean cache entry. The mtime and 295 * length match the cache, and other stat fields do not change. 296 * 297 * We could detect this at update-index time (the cache entry 298 * being registered/updated records the same time as "now") 299 * and delay the return from git-update-index, but that would 300 * effectively mean we can make at most one commit per second, 301 * which is not acceptable. Instead, we check cache entries 302 * whose mtime are the same as the index file timestamp more 303 * carefully than others. 304 */ 305if(!changed &&is_racy_timestamp(istate, ce)) { 306if(assume_racy_is_modified) 307 changed |= DATA_CHANGED; 308else 309 changed |=ce_modified_check_fs(ce, st); 310} 311 312return changed; 313} 314 315intie_modified(const struct index_state *istate, 316struct cache_entry *ce,struct stat *st,unsigned int options) 317{ 318int changed, changed_fs; 319 320 changed =ie_match_stat(istate, ce, st, options); 321if(!changed) 322return0; 323/* 324 * If the mode or type has changed, there's no point in trying 325 * to refresh the entry - it's not going to match 326 */ 327if(changed & (MODE_CHANGED | TYPE_CHANGED)) 328return changed; 329 330/* 331 * Immediately after read-tree or update-index --cacheinfo, 332 * the length field is zero, as we have never even read the 333 * lstat(2) information once, and we cannot trust DATA_CHANGED 334 * returned by ie_match_stat() which in turn was returned by 335 * ce_match_stat_basic() to signal that the filesize of the 336 * blob changed. We have to actually go to the filesystem to 337 * see if the contents match, and if so, should answer "unchanged". 338 * 339 * The logic does not apply to gitlinks, as ce_match_stat_basic() 340 * already has checked the actual HEAD from the filesystem in the 341 * subproject. If ie_match_stat() already said it is different, 342 * then we know it is. 343 */ 344if((changed & DATA_CHANGED) && 345(S_ISGITLINK(ce->ce_mode) || ce->ce_size !=0)) 346return changed; 347 348 changed_fs =ce_modified_check_fs(ce, st); 349if(changed_fs) 350return changed | changed_fs; 351return0; 352} 353 354intbase_name_compare(const char*name1,int len1,int mode1, 355const char*name2,int len2,int mode2) 356{ 357unsigned char c1, c2; 358int len = len1 < len2 ? len1 : len2; 359int cmp; 360 361 cmp =memcmp(name1, name2, len); 362if(cmp) 363return cmp; 364 c1 = name1[len]; 365 c2 = name2[len]; 366if(!c1 &&S_ISDIR(mode1)) 367 c1 ='/'; 368if(!c2 &&S_ISDIR(mode2)) 369 c2 ='/'; 370return(c1 < c2) ? -1: (c1 > c2) ?1:0; 371} 372 373/* 374 * df_name_compare() is identical to base_name_compare(), except it 375 * compares conflicting directory/file entries as equal. Note that 376 * while a directory name compares as equal to a regular file, they 377 * then individually compare _differently_ to a filename that has 378 * a dot after the basename (because '\0' < '.' < '/'). 379 * 380 * This is used by routines that want to traverse the git namespace 381 * but then handle conflicting entries together when possible. 382 */ 383intdf_name_compare(const char*name1,int len1,int mode1, 384const char*name2,int len2,int mode2) 385{ 386int len = len1 < len2 ? len1 : len2, cmp; 387unsigned char c1, c2; 388 389 cmp =memcmp(name1, name2, len); 390if(cmp) 391return cmp; 392/* Directories and files compare equal (same length, same name) */ 393if(len1 == len2) 394return0; 395 c1 = name1[len]; 396if(!c1 &&S_ISDIR(mode1)) 397 c1 ='/'; 398 c2 = name2[len]; 399if(!c2 &&S_ISDIR(mode2)) 400 c2 ='/'; 401if(c1 =='/'&& !c2) 402return0; 403if(c2 =='/'&& !c1) 404return0; 405return c1 - c2; 406} 407 408intcache_name_compare(const char*name1,int flags1,const char*name2,int flags2) 409{ 410int len1 = flags1 & CE_NAMEMASK; 411int len2 = flags2 & CE_NAMEMASK; 412int len = len1 < len2 ? len1 : len2; 413int cmp; 414 415 cmp =memcmp(name1, name2, len); 416if(cmp) 417return cmp; 418if(len1 < len2) 419return-1; 420if(len1 > len2) 421return1; 422 423/* Compare stages */ 424 flags1 &= CE_STAGEMASK; 425 flags2 &= CE_STAGEMASK; 426 427if(flags1 < flags2) 428return-1; 429if(flags1 > flags2) 430return1; 431return0; 432} 433 434intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 435{ 436int first, last; 437 438 first =0; 439 last = istate->cache_nr; 440while(last > first) { 441int next = (last + first) >>1; 442struct cache_entry *ce = istate->cache[next]; 443int cmp =cache_name_compare(name, namelen, ce->name, ce->ce_flags); 444if(!cmp) 445return next; 446if(cmp <0) { 447 last = next; 448continue; 449} 450 first = next+1; 451} 452return-first-1; 453} 454 455/* Remove entry, return true if there are more entries to go.. */ 456intremove_index_entry_at(struct index_state *istate,int pos) 457{ 458struct cache_entry *ce = istate->cache[pos]; 459 460record_resolve_undo(istate, ce); 461remove_name_hash(ce); 462 istate->cache_changed =1; 463 istate->cache_nr--; 464if(pos >= istate->cache_nr) 465return0; 466memmove(istate->cache + pos, 467 istate->cache + pos +1, 468(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 469return1; 470} 471 472/* 473 * Remove all cache ententries marked for removal, that is where 474 * CE_REMOVE is set in ce_flags. This is much more effective than 475 * calling remove_index_entry_at() for each entry to be removed. 476 */ 477voidremove_marked_cache_entries(struct index_state *istate) 478{ 479struct cache_entry **ce_array = istate->cache; 480unsigned int i, j; 481 482for(i = j =0; i < istate->cache_nr; i++) { 483if(ce_array[i]->ce_flags & CE_REMOVE) 484remove_name_hash(ce_array[i]); 485else 486 ce_array[j++] = ce_array[i]; 487} 488 istate->cache_changed =1; 489 istate->cache_nr = j; 490} 491 492intremove_file_from_index(struct index_state *istate,const char*path) 493{ 494int pos =index_name_pos(istate, path,strlen(path)); 495if(pos <0) 496 pos = -pos-1; 497cache_tree_invalidate_path(istate->cache_tree, path); 498while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 499remove_index_entry_at(istate, pos); 500return0; 501} 502 503static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 504{ 505return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 506} 507 508static intindex_name_pos_also_unmerged(struct index_state *istate, 509const char*path,int namelen) 510{ 511int pos =index_name_pos(istate, path, namelen); 512struct cache_entry *ce; 513 514if(pos >=0) 515return pos; 516 517/* maybe unmerged? */ 518 pos = -1- pos; 519if(pos >= istate->cache_nr || 520compare_name((ce = istate->cache[pos]), path, namelen)) 521return-1; 522 523/* order of preference: stage 2, 1, 3 */ 524if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 525ce_stage((ce = istate->cache[pos +1])) ==2&& 526!compare_name(ce, path, namelen)) 527 pos++; 528return pos; 529} 530 531static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 532{ 533int len =ce_namelen(ce); 534returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 535} 536 537/* 538 * If we add a filename that aliases in the cache, we will use the 539 * name that we already have - but we don't want to update the same 540 * alias twice, because that implies that there were actually two 541 * different files with aliasing names! 542 * 543 * So we use the CE_ADDED flag to verify that the alias was an old 544 * one before we accept it as 545 */ 546static struct cache_entry *create_alias_ce(struct cache_entry *ce,struct cache_entry *alias) 547{ 548int len; 549struct cache_entry *new; 550 551if(alias->ce_flags & CE_ADDED) 552die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 553 554/* Ok, create the new entry using the name of the existing alias */ 555 len =ce_namelen(alias); 556new=xcalloc(1,cache_entry_size(len)); 557memcpy(new->name, alias->name, len); 558copy_cache_entry(new, ce); 559free(ce); 560return new; 561} 562 563static voidrecord_intent_to_add(struct cache_entry *ce) 564{ 565unsigned char sha1[20]; 566if(write_sha1_file("",0, blob_type, sha1)) 567die("cannot create an empty blob in the object database"); 568hashcpy(ce->sha1, sha1); 569} 570 571intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 572{ 573int size, namelen, was_same; 574 mode_t st_mode = st->st_mode; 575struct cache_entry *ce, *alias; 576unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_IGNORE_SKIP_WORKTREE|CE_MATCH_RACY_IS_DIRTY; 577int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 578int pretend = flags & ADD_CACHE_PRETEND; 579int intent_only = flags & ADD_CACHE_INTENT; 580int add_option = (ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE| 581(intent_only ? ADD_CACHE_NEW_ONLY :0)); 582 583if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 584returnerror("%s: can only add regular files, symbolic links or git-directories", path); 585 586 namelen =strlen(path); 587if(S_ISDIR(st_mode)) { 588while(namelen && path[namelen-1] =='/') 589 namelen--; 590} 591 size =cache_entry_size(namelen); 592 ce =xcalloc(1, size); 593memcpy(ce->name, path, namelen); 594 ce->ce_flags = namelen; 595if(!intent_only) 596fill_stat_cache_info(ce, st); 597else 598 ce->ce_flags |= CE_INTENT_TO_ADD; 599 600if(trust_executable_bit && has_symlinks) 601 ce->ce_mode =create_ce_mode(st_mode); 602else{ 603/* If there is an existing entry, pick the mode bits and type 604 * from it, otherwise assume unexecutable regular file. 605 */ 606struct cache_entry *ent; 607int pos =index_name_pos_also_unmerged(istate, path, namelen); 608 609 ent = (0<= pos) ? istate->cache[pos] : NULL; 610 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 611} 612 613/* When core.ignorecase=true, determine if a directory of the same name but differing 614 * case already exists within the Git repository. If it does, ensure the directory 615 * case of the file being added to the repository matches (is folded into) the existing 616 * entry's directory case. 617 */ 618if(ignore_case) { 619const char*startPtr = ce->name; 620const char*ptr = startPtr; 621while(*ptr) { 622while(*ptr && *ptr !='/') 623++ptr; 624if(*ptr =='/') { 625struct cache_entry *foundce; 626++ptr; 627 foundce =index_name_exists(&the_index, ce->name, ptr - ce->name, ignore_case); 628if(foundce) { 629memcpy((void*)startPtr, foundce->name + (startPtr - ce->name), ptr - startPtr); 630 startPtr = ptr; 631} 632} 633} 634} 635 636 alias =index_name_exists(istate, ce->name,ce_namelen(ce), ignore_case); 637if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 638/* Nothing changed, really */ 639free(ce); 640if(!S_ISGITLINK(alias->ce_mode)) 641ce_mark_uptodate(alias); 642 alias->ce_flags |= CE_ADDED; 643return0; 644} 645if(!intent_only) { 646if(index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) 647returnerror("unable to index file%s", path); 648}else 649record_intent_to_add(ce); 650 651if(ignore_case && alias &&different_name(ce, alias)) 652 ce =create_alias_ce(ce, alias); 653 ce->ce_flags |= CE_ADDED; 654 655/* It was suspected to be racily clean, but it turns out to be Ok */ 656 was_same = (alias && 657!ce_stage(alias) && 658!hashcmp(alias->sha1, ce->sha1) && 659 ce->ce_mode == alias->ce_mode); 660 661if(pretend) 662; 663else if(add_index_entry(istate, ce, add_option)) 664returnerror("unable to add%sto index",path); 665if(verbose && !was_same) 666printf("add '%s'\n", path); 667return0; 668} 669 670intadd_file_to_index(struct index_state *istate,const char*path,int flags) 671{ 672struct stat st; 673if(lstat(path, &st)) 674die_errno("unable to stat '%s'", path); 675returnadd_to_index(istate, path, &st, flags); 676} 677 678struct cache_entry *make_cache_entry(unsigned int mode, 679const unsigned char*sha1,const char*path,int stage, 680int refresh) 681{ 682int size, len; 683struct cache_entry *ce; 684 685if(!verify_path(path)) { 686error("Invalid path '%s'", path); 687return NULL; 688} 689 690 len =strlen(path); 691 size =cache_entry_size(len); 692 ce =xcalloc(1, size); 693 694hashcpy(ce->sha1, sha1); 695memcpy(ce->name, path, len); 696 ce->ce_flags =create_ce_flags(len, stage); 697 ce->ce_mode =create_ce_mode(mode); 698 699if(refresh) 700returnrefresh_cache_entry(ce,0); 701 702return ce; 703} 704 705intce_same_name(struct cache_entry *a,struct cache_entry *b) 706{ 707int len =ce_namelen(a); 708returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 709} 710 711intce_path_match(const struct cache_entry *ce,const struct pathspec *pathspec) 712{ 713returnmatch_pathspec_depth(pathspec, ce->name,ce_namelen(ce),0, NULL); 714} 715 716/* 717 * We fundamentally don't like some paths: we don't want 718 * dot or dot-dot anywhere, and for obvious reasons don't 719 * want to recurse into ".git" either. 720 * 721 * Also, we don't want double slashes or slashes at the 722 * end that can make pathnames ambiguous. 723 */ 724static intverify_dotfile(const char*rest) 725{ 726/* 727 * The first character was '.', but that 728 * has already been discarded, we now test 729 * the rest. 730 */ 731 732/* "." is not allowed */ 733if(*rest =='\0'||is_dir_sep(*rest)) 734return0; 735 736switch(*rest) { 737/* 738 * ".git" followed by NUL or slash is bad. This 739 * shares the path end test with the ".." case. 740 */ 741case'g': 742if(rest[1] !='i') 743break; 744if(rest[2] !='t') 745break; 746 rest +=2; 747/* fallthrough */ 748case'.': 749if(rest[1] =='\0'||is_dir_sep(rest[1])) 750return0; 751} 752return1; 753} 754 755intverify_path(const char*path) 756{ 757char c; 758 759if(has_dos_drive_prefix(path)) 760return0; 761 762goto inside; 763for(;;) { 764if(!c) 765return1; 766if(is_dir_sep(c)) { 767inside: 768 c = *path++; 769if((c =='.'&& !verify_dotfile(path)) || 770is_dir_sep(c) || c =='\0') 771return0; 772} 773 c = *path++; 774} 775} 776 777/* 778 * Do we have another file that has the beginning components being a 779 * proper superset of the name we're trying to add? 780 */ 781static inthas_file_name(struct index_state *istate, 782const struct cache_entry *ce,int pos,int ok_to_replace) 783{ 784int retval =0; 785int len =ce_namelen(ce); 786int stage =ce_stage(ce); 787const char*name = ce->name; 788 789while(pos < istate->cache_nr) { 790struct cache_entry *p = istate->cache[pos++]; 791 792if(len >=ce_namelen(p)) 793break; 794if(memcmp(name, p->name, len)) 795break; 796if(ce_stage(p) != stage) 797continue; 798if(p->name[len] !='/') 799continue; 800if(p->ce_flags & CE_REMOVE) 801continue; 802 retval = -1; 803if(!ok_to_replace) 804break; 805remove_index_entry_at(istate, --pos); 806} 807return retval; 808} 809 810/* 811 * Do we have another file with a pathname that is a proper 812 * subset of the name we're trying to add? 813 */ 814static inthas_dir_name(struct index_state *istate, 815const struct cache_entry *ce,int pos,int ok_to_replace) 816{ 817int retval =0; 818int stage =ce_stage(ce); 819const char*name = ce->name; 820const char*slash = name +ce_namelen(ce); 821 822for(;;) { 823int len; 824 825for(;;) { 826if(*--slash =='/') 827break; 828if(slash <= ce->name) 829return retval; 830} 831 len = slash - name; 832 833 pos =index_name_pos(istate, name,create_ce_flags(len, stage)); 834if(pos >=0) { 835/* 836 * Found one, but not so fast. This could 837 * be a marker that says "I was here, but 838 * I am being removed". Such an entry is 839 * not a part of the resulting tree, and 840 * it is Ok to have a directory at the same 841 * path. 842 */ 843if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 844 retval = -1; 845if(!ok_to_replace) 846break; 847remove_index_entry_at(istate, pos); 848continue; 849} 850} 851else 852 pos = -pos-1; 853 854/* 855 * Trivial optimization: if we find an entry that 856 * already matches the sub-directory, then we know 857 * we're ok, and we can exit. 858 */ 859while(pos < istate->cache_nr) { 860struct cache_entry *p = istate->cache[pos]; 861if((ce_namelen(p) <= len) || 862(p->name[len] !='/') || 863memcmp(p->name, name, len)) 864break;/* not our subdirectory */ 865if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 866/* 867 * p is at the same stage as our entry, and 868 * is a subdirectory of what we are looking 869 * at, so we cannot have conflicts at our 870 * level or anything shorter. 871 */ 872return retval; 873 pos++; 874} 875} 876return retval; 877} 878 879/* We may be in a situation where we already have path/file and path 880 * is being added, or we already have path and path/file is being 881 * added. Either one would result in a nonsense tree that has path 882 * twice when git-write-tree tries to write it out. Prevent it. 883 * 884 * If ok-to-replace is specified, we remove the conflicting entries 885 * from the cache so the caller should recompute the insert position. 886 * When this happens, we return non-zero. 887 */ 888static intcheck_file_directory_conflict(struct index_state *istate, 889const struct cache_entry *ce, 890int pos,int ok_to_replace) 891{ 892int retval; 893 894/* 895 * When ce is an "I am going away" entry, we allow it to be added 896 */ 897if(ce->ce_flags & CE_REMOVE) 898return0; 899 900/* 901 * We check if the path is a sub-path of a subsequent pathname 902 * first, since removing those will not change the position 903 * in the array. 904 */ 905 retval =has_file_name(istate, ce, pos, ok_to_replace); 906 907/* 908 * Then check if the path might have a clashing sub-directory 909 * before it. 910 */ 911return retval +has_dir_name(istate, ce, pos, ok_to_replace); 912} 913 914static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 915{ 916int pos; 917int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 918int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 919int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 920int new_only = option & ADD_CACHE_NEW_ONLY; 921 922cache_tree_invalidate_path(istate->cache_tree, ce->name); 923 pos =index_name_pos(istate, ce->name, ce->ce_flags); 924 925/* existing match? Just replace it. */ 926if(pos >=0) { 927if(!new_only) 928replace_index_entry(istate, pos, ce); 929return0; 930} 931 pos = -pos-1; 932 933/* 934 * Inserting a merged entry ("stage 0") into the index 935 * will always replace all non-merged entries.. 936 */ 937if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 938while(ce_same_name(istate->cache[pos], ce)) { 939 ok_to_add =1; 940if(!remove_index_entry_at(istate, pos)) 941break; 942} 943} 944 945if(!ok_to_add) 946return-1; 947if(!verify_path(ce->name)) 948returnerror("Invalid path '%s'", ce->name); 949 950if(!skip_df_check && 951check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 952if(!ok_to_replace) 953returnerror("'%s' appears as both a file and as a directory", 954 ce->name); 955 pos =index_name_pos(istate, ce->name, ce->ce_flags); 956 pos = -pos-1; 957} 958return pos +1; 959} 960 961intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 962{ 963int pos; 964 965if(option & ADD_CACHE_JUST_APPEND) 966 pos = istate->cache_nr; 967else{ 968int ret; 969 ret =add_index_entry_with_check(istate, ce, option); 970if(ret <=0) 971return ret; 972 pos = ret -1; 973} 974 975/* Make sure the array is big enough .. */ 976if(istate->cache_nr == istate->cache_alloc) { 977 istate->cache_alloc =alloc_nr(istate->cache_alloc); 978 istate->cache =xrealloc(istate->cache, 979 istate->cache_alloc *sizeof(struct cache_entry *)); 980} 981 982/* Add it in.. */ 983 istate->cache_nr++; 984if(istate->cache_nr > pos +1) 985memmove(istate->cache + pos +1, 986 istate->cache + pos, 987(istate->cache_nr - pos -1) *sizeof(ce)); 988set_index_entry(istate, pos, ce); 989 istate->cache_changed =1; 990return0; 991} 992 993/* 994 * "refresh" does not calculate a new sha1 file or bring the 995 * cache up-to-date for mode/content changes. But what it 996 * _does_ do is to "re-match" the stat information of a file 997 * with the cache, so that you can refresh the cache for a 998 * file that hasn't been changed but where the stat entry is 999 * out of date.1000 *1001 * For example, you'd want to do this after doing a "git-read-tree",1002 * to link up the stat cache details with the proper files.1003 */1004static struct cache_entry *refresh_cache_ent(struct index_state *istate,1005struct cache_entry *ce,1006unsigned int options,int*err,1007int*changed_ret)1008{1009struct stat st;1010struct cache_entry *updated;1011int changed, size;1012int ignore_valid = options & CE_MATCH_IGNORE_VALID;1013int ignore_skip_worktree = options & CE_MATCH_IGNORE_SKIP_WORKTREE;10141015if(ce_uptodate(ce))1016return ce;10171018/*1019 * CE_VALID or CE_SKIP_WORKTREE means the user promised us1020 * that the change to the work tree does not matter and told1021 * us not to worry.1022 */1023if(!ignore_skip_worktree &&ce_skip_worktree(ce)) {1024ce_mark_uptodate(ce);1025return ce;1026}1027if(!ignore_valid && (ce->ce_flags & CE_VALID)) {1028ce_mark_uptodate(ce);1029return ce;1030}10311032if(lstat(ce->name, &st) <0) {1033if(err)1034*err = errno;1035return NULL;1036}10371038 changed =ie_match_stat(istate, ce, &st, options);1039if(changed_ret)1040*changed_ret = changed;1041if(!changed) {1042/*1043 * The path is unchanged. If we were told to ignore1044 * valid bit, then we did the actual stat check and1045 * found that the entry is unmodified. If the entry1046 * is not marked VALID, this is the place to mark it1047 * valid again, under "assume unchanged" mode.1048 */1049if(ignore_valid && assume_unchanged &&1050!(ce->ce_flags & CE_VALID))1051;/* mark this one VALID again */1052else{1053/*1054 * We do not mark the index itself "modified"1055 * because CE_UPTODATE flag is in-core only;1056 * we are not going to write this change out.1057 */1058if(!S_ISGITLINK(ce->ce_mode))1059ce_mark_uptodate(ce);1060return ce;1061}1062}10631064if(ie_modified(istate, ce, &st, options)) {1065if(err)1066*err = EINVAL;1067return NULL;1068}10691070 size =ce_size(ce);1071 updated =xmalloc(size);1072memcpy(updated, ce, size);1073fill_stat_cache_info(updated, &st);1074/*1075 * If ignore_valid is not set, we should leave CE_VALID bit1076 * alone. Otherwise, paths marked with --no-assume-unchanged1077 * (i.e. things to be edited) will reacquire CE_VALID bit1078 * automatically, which is not really what we want.1079 */1080if(!ignore_valid && assume_unchanged &&1081!(ce->ce_flags & CE_VALID))1082 updated->ce_flags &= ~CE_VALID;10831084return updated;1085}10861087static voidshow_file(const char* fmt,const char* name,int in_porcelain,1088int* first,const char*header_msg)1089{1090if(in_porcelain && *first && header_msg) {1091printf("%s\n", header_msg);1092*first =0;1093}1094printf(fmt, name);1095}10961097intrefresh_index(struct index_state *istate,unsigned int flags,const char**pathspec,1098char*seen,const char*header_msg)1099{1100int i;1101int has_errors =0;1102int really = (flags & REFRESH_REALLY) !=0;1103int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1104int quiet = (flags & REFRESH_QUIET) !=0;1105int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1106int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1107int first =1;1108int in_porcelain = (flags & REFRESH_IN_PORCELAIN);1109unsigned int options = really ? CE_MATCH_IGNORE_VALID :0;1110const char*modified_fmt;1111const char*deleted_fmt;1112const char*typechange_fmt;1113const char*added_fmt;1114const char*unmerged_fmt;11151116 modified_fmt = (in_porcelain ?"M\t%s\n":"%s: needs update\n");1117 deleted_fmt = (in_porcelain ?"D\t%s\n":"%s: needs update\n");1118 typechange_fmt = (in_porcelain ?"T\t%s\n":"%sneeds update\n");1119 added_fmt = (in_porcelain ?"A\t%s\n":"%sneeds update\n");1120 unmerged_fmt = (in_porcelain ?"U\t%s\n":"%s: needs merge\n");1121for(i =0; i < istate->cache_nr; i++) {1122struct cache_entry *ce, *new;1123int cache_errno =0;1124int changed =0;1125int filtered =0;11261127 ce = istate->cache[i];1128if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1129continue;11301131if(pathspec &&1132!match_pathspec(pathspec, ce->name,strlen(ce->name),0, seen))1133 filtered =1;11341135if(ce_stage(ce)) {1136while((i < istate->cache_nr) &&1137!strcmp(istate->cache[i]->name, ce->name))1138 i++;1139 i--;1140if(allow_unmerged)1141continue;1142if(!filtered)1143show_file(unmerged_fmt, ce->name, in_porcelain,1144&first, header_msg);1145 has_errors =1;1146continue;1147}11481149if(filtered)1150continue;11511152new=refresh_cache_ent(istate, ce, options, &cache_errno, &changed);1153if(new== ce)1154continue;1155if(!new) {1156const char*fmt;11571158if(not_new && cache_errno == ENOENT)1159continue;1160if(really && cache_errno == EINVAL) {1161/* If we are doing --really-refresh that1162 * means the index is not valid anymore.1163 */1164 ce->ce_flags &= ~CE_VALID;1165 istate->cache_changed =1;1166}1167if(quiet)1168continue;11691170if(cache_errno == ENOENT)1171 fmt = deleted_fmt;1172else if(ce->ce_flags & CE_INTENT_TO_ADD)1173 fmt = added_fmt;/* must be before other checks */1174else if(changed & TYPE_CHANGED)1175 fmt = typechange_fmt;1176else1177 fmt = modified_fmt;1178show_file(fmt,1179 ce->name, in_porcelain, &first, header_msg);1180 has_errors =1;1181continue;1182}11831184replace_index_entry(istate, i,new);1185}1186return has_errors;1187}11881189static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really)1190{1191returnrefresh_cache_ent(&the_index, ce, really, NULL, NULL);1192}119311941195/*****************************************************************1196 * Index File I/O1197 *****************************************************************/11981199/*1200 * dev/ino/uid/gid/size are also just tracked to the low 32 bits1201 * Again - this is just a (very strong in practice) heuristic that1202 * the inode hasn't changed.1203 *1204 * We save the fields in big-endian order to allow using the1205 * index file over NFS transparently.1206 */1207struct ondisk_cache_entry {1208struct cache_time ctime;1209struct cache_time mtime;1210unsigned int dev;1211unsigned int ino;1212unsigned int mode;1213unsigned int uid;1214unsigned int gid;1215unsigned int size;1216unsigned char sha1[20];1217unsigned short flags;1218char name[FLEX_ARRAY];/* more */1219};12201221/*1222 * This struct is used when CE_EXTENDED bit is 11223 * The struct must match ondisk_cache_entry exactly from1224 * ctime till flags1225 */1226struct ondisk_cache_entry_extended {1227struct cache_time ctime;1228struct cache_time mtime;1229unsigned int dev;1230unsigned int ino;1231unsigned int mode;1232unsigned int uid;1233unsigned int gid;1234unsigned int size;1235unsigned char sha1[20];1236unsigned short flags;1237unsigned short flags2;1238char name[FLEX_ARRAY];/* more */1239};12401241/* These are only used for v3 or lower */1242#define align_flex_name(STRUCT,len) ((offsetof(struct STRUCT,name) + (len) + 8) & ~7)1243#define ondisk_cache_entry_size(len) align_flex_name(ondisk_cache_entry,len)1244#define ondisk_cache_entry_extended_size(len) align_flex_name(ondisk_cache_entry_extended,len)1245#define ondisk_ce_size(ce) (((ce)->ce_flags & CE_EXTENDED) ? \1246 ondisk_cache_entry_extended_size(ce_namelen(ce)) : \1247 ondisk_cache_entry_size(ce_namelen(ce)))12481249static intverify_hdr(struct cache_header *hdr,unsigned long size)1250{1251 git_SHA_CTX c;1252unsigned char sha1[20];1253int hdr_version;12541255if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1256returnerror("bad signature");1257 hdr_version =ntohl(hdr->hdr_version);1258if(hdr_version <2||4< hdr_version)1259returnerror("bad index version%d", hdr_version);1260git_SHA1_Init(&c);1261git_SHA1_Update(&c, hdr, size -20);1262git_SHA1_Final(sha1, &c);1263if(hashcmp(sha1, (unsigned char*)hdr + size -20))1264returnerror("bad index file sha1 signature");1265return0;1266}12671268static intread_index_extension(struct index_state *istate,1269const char*ext,void*data,unsigned long sz)1270{1271switch(CACHE_EXT(ext)) {1272case CACHE_EXT_TREE:1273 istate->cache_tree =cache_tree_read(data, sz);1274break;1275case CACHE_EXT_RESOLVE_UNDO:1276 istate->resolve_undo =resolve_undo_read(data, sz);1277break;1278default:1279if(*ext <'A'||'Z'< *ext)1280returnerror("index uses %.4s extension, which we do not understand",1281 ext);1282fprintf(stderr,"ignoring %.4s extension\n", ext);1283break;1284}1285return0;1286}12871288intread_index(struct index_state *istate)1289{1290returnread_index_from(istate,get_index_file());1291}12921293#ifndef NEEDS_ALIGNED_ACCESS1294#define ntoh_s(var) ntohs(var)1295#define ntoh_l(var) ntohl(var)1296#else1297staticinlineuint16_tntoh_s_force_align(void*p)1298{1299uint16_t x;1300memcpy(&x, p,sizeof(x));1301returnntohs(x);1302}1303staticinlineuint32_tntoh_l_force_align(void*p)1304{1305uint32_t x;1306memcpy(&x, p,sizeof(x));1307returnntohl(x);1308}1309#define ntoh_s(var) ntoh_s_force_align(&(var))1310#define ntoh_l(var) ntoh_l_force_align(&(var))1311#endif13121313static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,1314unsigned int flags,1315const char*name,1316size_t len)1317{1318struct cache_entry *ce =xmalloc(cache_entry_size(len));13191320 ce->ce_ctime.sec =ntoh_l(ondisk->ctime.sec);1321 ce->ce_mtime.sec =ntoh_l(ondisk->mtime.sec);1322 ce->ce_ctime.nsec =ntoh_l(ondisk->ctime.nsec);1323 ce->ce_mtime.nsec =ntoh_l(ondisk->mtime.nsec);1324 ce->ce_dev =ntoh_l(ondisk->dev);1325 ce->ce_ino =ntoh_l(ondisk->ino);1326 ce->ce_mode =ntoh_l(ondisk->mode);1327 ce->ce_uid =ntoh_l(ondisk->uid);1328 ce->ce_gid =ntoh_l(ondisk->gid);1329 ce->ce_size =ntoh_l(ondisk->size);1330 ce->ce_flags = flags;1331hashcpy(ce->sha1, ondisk->sha1);1332memcpy(ce->name, name, len);1333 ce->name[len] ='\0';1334return ce;1335}13361337/*1338 * Adjacent cache entries tend to share the leading paths, so it makes1339 * sense to only store the differences in later entries. In the v41340 * on-disk format of the index, each on-disk cache entry stores the1341 * number of bytes to be stripped from the end of the previous name,1342 * and the bytes to append to the result, to come up with its name.1343 */1344static unsigned longexpand_name_field(struct strbuf *name,const char*cp_)1345{1346const unsigned char*ep, *cp = (const unsigned char*)cp_;1347size_t len =decode_varint(&cp);13481349if(name->len < len)1350die("malformed name field in the index");1351strbuf_remove(name, name->len - len, len);1352for(ep = cp; *ep; ep++)1353;/* find the end */1354strbuf_add(name, cp, ep - cp);1355return(const char*)ep +1- cp_;1356}13571358static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,1359unsigned long*ent_size,1360struct strbuf *previous_name)1361{1362struct cache_entry *ce;1363size_t len;1364const char*name;1365unsigned int flags;13661367/* On-disk flags are just 16 bits */1368 flags =ntoh_s(ondisk->flags);1369 len = flags & CE_NAMEMASK;13701371if(flags & CE_EXTENDED) {1372struct ondisk_cache_entry_extended *ondisk2;1373int extended_flags;1374 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1375 extended_flags =ntoh_s(ondisk2->flags2) <<16;1376/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */1377if(extended_flags & ~CE_EXTENDED_FLAGS)1378die("Unknown index entry format%08x", extended_flags);1379 flags |= extended_flags;1380 name = ondisk2->name;1381}1382else1383 name = ondisk->name;13841385if(!previous_name) {1386/* v3 and earlier */1387if(len == CE_NAMEMASK)1388 len =strlen(name);1389 ce =cache_entry_from_ondisk(ondisk, flags, name, len);13901391*ent_size =ondisk_ce_size(ce);1392}else{1393unsigned long consumed;1394 consumed =expand_name_field(previous_name, name);1395 ce =cache_entry_from_ondisk(ondisk, flags,1396 previous_name->buf,1397 previous_name->len);13981399*ent_size = (name - ((char*)ondisk)) + consumed;1400}1401return ce;1402}14031404/* remember to discard_cache() before reading a different cache! */1405intread_index_from(struct index_state *istate,const char*path)1406{1407int fd, i;1408struct stat st;1409unsigned long src_offset;1410struct cache_header *hdr;1411void*mmap;1412size_t mmap_size;1413struct strbuf previous_name_buf = STRBUF_INIT, *previous_name;14141415 errno = EBUSY;1416if(istate->initialized)1417return istate->cache_nr;14181419 errno = ENOENT;1420 istate->timestamp.sec =0;1421 istate->timestamp.nsec =0;1422 fd =open(path, O_RDONLY);1423if(fd <0) {1424if(errno == ENOENT)1425return0;1426die_errno("index file open failed");1427}14281429if(fstat(fd, &st))1430die_errno("cannot stat the open index");14311432 errno = EINVAL;1433 mmap_size =xsize_t(st.st_size);1434if(mmap_size <sizeof(struct cache_header) +20)1435die("index file smaller than expected");14361437 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1438close(fd);1439if(mmap == MAP_FAILED)1440die_errno("unable to map index file");14411442 hdr = mmap;1443if(verify_hdr(hdr, mmap_size) <0)1444goto unmap;14451446 istate->cache_nr =ntohl(hdr->hdr_entries);1447 istate->cache_alloc =alloc_nr(istate->cache_nr);1448 istate->cache =xcalloc(istate->cache_alloc,sizeof(struct cache_entry *));1449 istate->initialized =1;14501451if(hdr->hdr_version ==htonl(4))1452 previous_name = &previous_name_buf;1453else1454 previous_name = NULL;14551456 src_offset =sizeof(*hdr);1457for(i =0; i < istate->cache_nr; i++) {1458struct ondisk_cache_entry *disk_ce;1459struct cache_entry *ce;1460unsigned long consumed;14611462 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1463 ce =create_from_disk(disk_ce, &consumed, previous_name);1464set_index_entry(istate, i, ce);14651466 src_offset += consumed;1467}1468strbuf_release(&previous_name_buf);1469 istate->timestamp.sec = st.st_mtime;1470 istate->timestamp.nsec =ST_MTIME_NSEC(st);14711472while(src_offset <= mmap_size -20-8) {1473/* After an array of active_nr index entries,1474 * there can be arbitrary number of extended1475 * sections, each of which is prefixed with1476 * extension name (4-byte) and section length1477 * in 4-byte network byte order.1478 */1479uint32_t extsize;1480memcpy(&extsize, (char*)mmap + src_offset +4,4);1481 extsize =ntohl(extsize);1482if(read_index_extension(istate,1483(const char*) mmap + src_offset,1484(char*) mmap + src_offset +8,1485 extsize) <0)1486goto unmap;1487 src_offset +=8;1488 src_offset += extsize;1489}1490munmap(mmap, mmap_size);1491return istate->cache_nr;14921493unmap:1494munmap(mmap, mmap_size);1495 errno = EINVAL;1496die("index file corrupt");1497}14981499intis_index_unborn(struct index_state *istate)1500{1501return(!istate->cache_nr && !istate->timestamp.sec);1502}15031504intdiscard_index(struct index_state *istate)1505{1506int i;15071508for(i =0; i < istate->cache_nr; i++)1509free(istate->cache[i]);1510resolve_undo_clear_index(istate);1511 istate->cache_nr =0;1512 istate->cache_changed =0;1513 istate->timestamp.sec =0;1514 istate->timestamp.nsec =0;1515 istate->name_hash_initialized =0;1516free_hash(&istate->name_hash);1517cache_tree_free(&(istate->cache_tree));1518 istate->initialized =0;15191520/* no need to throw away allocated active_cache */1521return0;1522}15231524intunmerged_index(const struct index_state *istate)1525{1526int i;1527for(i =0; i < istate->cache_nr; i++) {1528if(ce_stage(istate->cache[i]))1529return1;1530}1531return0;1532}15331534#define WRITE_BUFFER_SIZE 81921535static unsigned char write_buffer[WRITE_BUFFER_SIZE];1536static unsigned long write_buffer_len;15371538static intce_write_flush(git_SHA_CTX *context,int fd)1539{1540unsigned int buffered = write_buffer_len;1541if(buffered) {1542git_SHA1_Update(context, write_buffer, buffered);1543if(write_in_full(fd, write_buffer, buffered) != buffered)1544return-1;1545 write_buffer_len =0;1546}1547return0;1548}15491550static intce_write(git_SHA_CTX *context,int fd,void*data,unsigned int len)1551{1552while(len) {1553unsigned int buffered = write_buffer_len;1554unsigned int partial = WRITE_BUFFER_SIZE - buffered;1555if(partial > len)1556 partial = len;1557memcpy(write_buffer + buffered, data, partial);1558 buffered += partial;1559if(buffered == WRITE_BUFFER_SIZE) {1560 write_buffer_len = buffered;1561if(ce_write_flush(context, fd))1562return-1;1563 buffered =0;1564}1565 write_buffer_len = buffered;1566 len -= partial;1567 data = (char*) data + partial;1568}1569return0;1570}15711572static intwrite_index_ext_header(git_SHA_CTX *context,int fd,1573unsigned int ext,unsigned int sz)1574{1575 ext =htonl(ext);1576 sz =htonl(sz);1577return((ce_write(context, fd, &ext,4) <0) ||1578(ce_write(context, fd, &sz,4) <0)) ? -1:0;1579}15801581static intce_flush(git_SHA_CTX *context,int fd)1582{1583unsigned int left = write_buffer_len;15841585if(left) {1586 write_buffer_len =0;1587git_SHA1_Update(context, write_buffer, left);1588}15891590/* Flush first if not enough space for SHA1 signature */1591if(left +20> WRITE_BUFFER_SIZE) {1592if(write_in_full(fd, write_buffer, left) != left)1593return-1;1594 left =0;1595}15961597/* Append the SHA1 signature at the end */1598git_SHA1_Final(write_buffer + left, context);1599 left +=20;1600return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1601}16021603static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1604{1605/*1606 * The only thing we care about in this function is to smudge the1607 * falsely clean entry due to touch-update-touch race, so we leave1608 * everything else as they are. We are called for entries whose1609 * ce_mtime match the index file mtime.1610 *1611 * Note that this actually does not do much for gitlinks, for1612 * which ce_match_stat_basic() always goes to the actual1613 * contents. The caller checks with is_racy_timestamp() which1614 * always says "no" for gitlinks, so we are not called for them ;-)1615 */1616struct stat st;16171618if(lstat(ce->name, &st) <0)1619return;1620if(ce_match_stat_basic(ce, &st))1621return;1622if(ce_modified_check_fs(ce, &st)) {1623/* This is "racily clean"; smudge it. Note that this1624 * is a tricky code. At first glance, it may appear1625 * that it can break with this sequence:1626 *1627 * $ echo xyzzy >frotz1628 * $ git-update-index --add frotz1629 * $ : >frotz1630 * $ sleep 31631 * $ echo filfre >nitfol1632 * $ git-update-index --add nitfol1633 *1634 * but it does not. When the second update-index runs,1635 * it notices that the entry "frotz" has the same timestamp1636 * as index, and if we were to smudge it by resetting its1637 * size to zero here, then the object name recorded1638 * in index is the 6-byte file but the cached stat information1639 * becomes zero --- which would then match what we would1640 * obtain from the filesystem next time we stat("frotz").1641 *1642 * However, the second update-index, before calling1643 * this function, notices that the cached size is 61644 * bytes and what is on the filesystem is an empty1645 * file, and never calls us, so the cached size information1646 * for "frotz" stays 6 which does not match the filesystem.1647 */1648 ce->ce_size =0;1649}1650}16511652/* Copy miscellaneous fields but not the name */1653static char*copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk,1654struct cache_entry *ce)1655{1656 ondisk->ctime.sec =htonl(ce->ce_ctime.sec);1657 ondisk->mtime.sec =htonl(ce->ce_mtime.sec);1658 ondisk->ctime.nsec =htonl(ce->ce_ctime.nsec);1659 ondisk->mtime.nsec =htonl(ce->ce_mtime.nsec);1660 ondisk->dev =htonl(ce->ce_dev);1661 ondisk->ino =htonl(ce->ce_ino);1662 ondisk->mode =htonl(ce->ce_mode);1663 ondisk->uid =htonl(ce->ce_uid);1664 ondisk->gid =htonl(ce->ce_gid);1665 ondisk->size =htonl(ce->ce_size);1666hashcpy(ondisk->sha1, ce->sha1);1667 ondisk->flags =htons(ce->ce_flags);1668if(ce->ce_flags & CE_EXTENDED) {1669struct ondisk_cache_entry_extended *ondisk2;1670 ondisk2 = (struct ondisk_cache_entry_extended *)ondisk;1671 ondisk2->flags2 =htons((ce->ce_flags & CE_EXTENDED_FLAGS) >>16);1672return ondisk2->name;1673}1674else{1675return ondisk->name;1676}1677}16781679static intce_write_entry(git_SHA_CTX *c,int fd,struct cache_entry *ce)1680{1681int size =ondisk_ce_size(ce);1682struct ondisk_cache_entry *ondisk =xcalloc(1, size);1683char*name;1684int result;16851686 name =copy_cache_entry_to_ondisk(ondisk, ce);1687memcpy(name, ce->name,ce_namelen(ce));16881689 result =ce_write(c, fd, ondisk, size);1690free(ondisk);1691return result;1692}16931694static inthas_racy_timestamp(struct index_state *istate)1695{1696int entries = istate->cache_nr;1697int i;16981699for(i =0; i < entries; i++) {1700struct cache_entry *ce = istate->cache[i];1701if(is_racy_timestamp(istate, ce))1702return1;1703}1704return0;1705}17061707/*1708 * Opportunisticly update the index but do not complain if we can't1709 */1710voidupdate_index_if_able(struct index_state *istate,struct lock_file *lockfile)1711{1712if((istate->cache_changed ||has_racy_timestamp(istate)) &&1713!write_index(istate, lockfile->fd))1714commit_locked_index(lockfile);1715else1716rollback_lock_file(lockfile);1717}17181719intwrite_index(struct index_state *istate,int newfd)1720{1721 git_SHA_CTX c;1722struct cache_header hdr;1723int i, err, removed, extended;1724struct cache_entry **cache = istate->cache;1725int entries = istate->cache_nr;1726struct stat st;17271728for(i = removed = extended =0; i < entries; i++) {1729if(cache[i]->ce_flags & CE_REMOVE)1730 removed++;17311732/* reduce extended entries if possible */1733 cache[i]->ce_flags &= ~CE_EXTENDED;1734if(cache[i]->ce_flags & CE_EXTENDED_FLAGS) {1735 extended++;1736 cache[i]->ce_flags |= CE_EXTENDED;1737}1738}17391740 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1741/* for extended format, increase version so older git won't try to read it */1742 hdr.hdr_version =htonl(extended ?3:2);1743 hdr.hdr_entries =htonl(entries - removed);17441745git_SHA1_Init(&c);1746if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1747return-1;17481749for(i =0; i < entries; i++) {1750struct cache_entry *ce = cache[i];1751if(ce->ce_flags & CE_REMOVE)1752continue;1753if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1754ce_smudge_racily_clean_entry(ce);1755if(ce_write_entry(&c, newfd, ce) <0)1756return-1;1757}17581759/* Write extension data here */1760if(istate->cache_tree) {1761struct strbuf sb = STRBUF_INIT;17621763cache_tree_write(&sb, istate->cache_tree);1764 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01765||ce_write(&c, newfd, sb.buf, sb.len) <0;1766strbuf_release(&sb);1767if(err)1768return-1;1769}1770if(istate->resolve_undo) {1771struct strbuf sb = STRBUF_INIT;17721773resolve_undo_write(&sb, istate->resolve_undo);1774 err =write_index_ext_header(&c, newfd, CACHE_EXT_RESOLVE_UNDO,1775 sb.len) <01776||ce_write(&c, newfd, sb.buf, sb.len) <0;1777strbuf_release(&sb);1778if(err)1779return-1;1780}17811782if(ce_flush(&c, newfd) ||fstat(newfd, &st))1783return-1;1784 istate->timestamp.sec = (unsigned int)st.st_mtime;1785 istate->timestamp.nsec =ST_MTIME_NSEC(st);1786return0;1787}17881789/*1790 * Read the index file that is potentially unmerged into given1791 * index_state, dropping any unmerged entries. Returns true if1792 * the index is unmerged. Callers who want to refuse to work1793 * from an unmerged state can call this and check its return value,1794 * instead of calling read_cache().1795 */1796intread_index_unmerged(struct index_state *istate)1797{1798int i;1799int unmerged =0;18001801read_index(istate);1802for(i =0; i < istate->cache_nr; i++) {1803struct cache_entry *ce = istate->cache[i];1804struct cache_entry *new_ce;1805int size, len;18061807if(!ce_stage(ce))1808continue;1809 unmerged =1;1810 len =strlen(ce->name);1811 size =cache_entry_size(len);1812 new_ce =xcalloc(1, size);1813memcpy(new_ce->name, ce->name, len);1814 new_ce->ce_flags =create_ce_flags(len,0) | CE_CONFLICTED;1815 new_ce->ce_mode = ce->ce_mode;1816if(add_index_entry(istate, new_ce,0))1817returnerror("%s: cannot drop to stage #0",1818 ce->name);1819 i =index_name_pos(istate, new_ce->name, len);1820}1821return unmerged;1822}18231824/*1825 * Returns 1 if the path is an "other" path with respect to1826 * the index; that is, the path is not mentioned in the index at all,1827 * either as a file, a directory with some files in the index,1828 * or as an unmerged entry.1829 *1830 * We helpfully remove a trailing "/" from directories so that1831 * the output of read_directory can be used as-is.1832 */1833intindex_name_is_other(const struct index_state *istate,const char*name,1834int namelen)1835{1836int pos;1837if(namelen && name[namelen -1] =='/')1838 namelen--;1839 pos =index_name_pos(istate, name, namelen);1840if(0<= pos)1841return0;/* exact match */1842 pos = -pos -1;1843if(pos < istate->cache_nr) {1844struct cache_entry *ce = istate->cache[pos];1845if(ce_namelen(ce) == namelen &&1846!memcmp(ce->name, name, namelen))1847return0;/* Yup, this one exists unmerged */1848}1849return1;1850}