1/* 2 * GIT - The information manager from hell 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6#include"cache.h" 7#include"cache-tree.h" 8 9/* Index extensions. 10 * 11 * The first letter should be 'A'..'Z' for extensions that are not 12 * necessary for a correct operation (i.e. optimization data). 13 * When new extensions are added that _needs_ to be understood in 14 * order to correctly interpret the index file, pick character that 15 * is outside the range, to cause the reader to abort. 16 */ 17 18#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 19#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 20 21struct cache_entry **active_cache = NULL; 22static time_t index_file_timestamp; 23unsigned int active_nr =0, active_alloc =0, active_cache_changed =0; 24 25struct cache_tree *active_cache_tree = NULL; 26 27int cache_errno =0; 28 29static void*cache_mmap = NULL; 30static size_t cache_mmap_size =0; 31 32/* 33 * This only updates the "non-critical" parts of the directory 34 * cache, ie the parts that aren't tracked by GIT, and only used 35 * to validate the cache. 36 */ 37voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 38{ 39 ce->ce_ctime.sec =htonl(st->st_ctime); 40 ce->ce_mtime.sec =htonl(st->st_mtime); 41#ifdef USE_NSEC 42 ce->ce_ctime.nsec =htonl(st->st_ctim.tv_nsec); 43 ce->ce_mtime.nsec =htonl(st->st_mtim.tv_nsec); 44#endif 45 ce->ce_dev =htonl(st->st_dev); 46 ce->ce_ino =htonl(st->st_ino); 47 ce->ce_uid =htonl(st->st_uid); 48 ce->ce_gid =htonl(st->st_gid); 49 ce->ce_size =htonl(st->st_size); 50 51if(assume_unchanged) 52 ce->ce_flags |=htons(CE_VALID); 53} 54 55static intce_compare_data(struct cache_entry *ce,struct stat *st) 56{ 57int match = -1; 58int fd =open(ce->name, O_RDONLY); 59 60if(fd >=0) { 61unsigned char sha1[20]; 62if(!index_fd(sha1, fd, st,0, NULL)) 63 match =memcmp(sha1, ce->sha1,20); 64/* index_fd() closed the file descriptor already */ 65} 66return match; 67} 68 69static intce_compare_link(struct cache_entry *ce,unsigned long expected_size) 70{ 71int match = -1; 72char*target; 73void*buffer; 74unsigned long size; 75char type[10]; 76int len; 77 78 target =xmalloc(expected_size); 79 len =readlink(ce->name, target, expected_size); 80if(len != expected_size) { 81free(target); 82return-1; 83} 84 buffer =read_sha1_file(ce->sha1, type, &size); 85if(!buffer) { 86free(target); 87return-1; 88} 89if(size == expected_size) 90 match =memcmp(buffer, target, size); 91free(buffer); 92free(target); 93return match; 94} 95 96static intce_modified_check_fs(struct cache_entry *ce,struct stat *st) 97{ 98switch(st->st_mode & S_IFMT) { 99case S_IFREG: 100if(ce_compare_data(ce, st)) 101return DATA_CHANGED; 102break; 103case S_IFLNK: 104if(ce_compare_link(ce, st->st_size)) 105return DATA_CHANGED; 106break; 107default: 108return TYPE_CHANGED; 109} 110return0; 111} 112 113static intce_match_stat_basic(struct cache_entry *ce,struct stat *st) 114{ 115unsigned int changed =0; 116 117switch(ntohl(ce->ce_mode) & S_IFMT) { 118case S_IFREG: 119 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 120/* We consider only the owner x bit to be relevant for 121 * "mode changes" 122 */ 123if(trust_executable_bit && 124(0100& (ntohl(ce->ce_mode) ^ st->st_mode))) 125 changed |= MODE_CHANGED; 126break; 127case S_IFLNK: 128 changed |= !S_ISLNK(st->st_mode) ? TYPE_CHANGED :0; 129break; 130default: 131die("internal error: ce_mode is%o",ntohl(ce->ce_mode)); 132} 133if(ce->ce_mtime.sec !=htonl(st->st_mtime)) 134 changed |= MTIME_CHANGED; 135if(ce->ce_ctime.sec !=htonl(st->st_ctime)) 136 changed |= CTIME_CHANGED; 137 138#ifdef USE_NSEC 139/* 140 * nsec seems unreliable - not all filesystems support it, so 141 * as long as it is in the inode cache you get right nsec 142 * but after it gets flushed, you get zero nsec. 143 */ 144if(ce->ce_mtime.nsec !=htonl(st->st_mtim.tv_nsec)) 145 changed |= MTIME_CHANGED; 146if(ce->ce_ctime.nsec !=htonl(st->st_ctim.tv_nsec)) 147 changed |= CTIME_CHANGED; 148#endif 149 150if(ce->ce_uid !=htonl(st->st_uid) || 151 ce->ce_gid !=htonl(st->st_gid)) 152 changed |= OWNER_CHANGED; 153if(ce->ce_ino !=htonl(st->st_ino)) 154 changed |= INODE_CHANGED; 155 156#ifdef USE_STDEV 157/* 158 * st_dev breaks on network filesystems where different 159 * clients will have different views of what "device" 160 * the filesystem is on 161 */ 162if(ce->ce_dev !=htonl(st->st_dev)) 163 changed |= INODE_CHANGED; 164#endif 165 166if(ce->ce_size !=htonl(st->st_size)) 167 changed |= DATA_CHANGED; 168 169return changed; 170} 171 172intce_match_stat(struct cache_entry *ce,struct stat *st,int options) 173{ 174unsigned int changed; 175int ignore_valid = options &01; 176int assume_racy_is_modified = options &02; 177 178/* 179 * If it's marked as always valid in the index, it's 180 * valid whatever the checked-out copy says. 181 */ 182if(!ignore_valid && (ce->ce_flags &htons(CE_VALID))) 183return0; 184 185 changed =ce_match_stat_basic(ce, st); 186 187/* 188 * Within 1 second of this sequence: 189 * echo xyzzy >file && git-update-index --add file 190 * running this command: 191 * echo frotz >file 192 * would give a falsely clean cache entry. The mtime and 193 * length match the cache, and other stat fields do not change. 194 * 195 * We could detect this at update-index time (the cache entry 196 * being registered/updated records the same time as "now") 197 * and delay the return from git-update-index, but that would 198 * effectively mean we can make at most one commit per second, 199 * which is not acceptable. Instead, we check cache entries 200 * whose mtime are the same as the index file timestamp more 201 * carefully than others. 202 */ 203if(!changed && 204 index_file_timestamp && 205 index_file_timestamp <=ntohl(ce->ce_mtime.sec)) { 206if(assume_racy_is_modified) 207 changed |= DATA_CHANGED; 208else 209 changed |=ce_modified_check_fs(ce, st); 210} 211 212return changed; 213} 214 215intce_modified(struct cache_entry *ce,struct stat *st,int really) 216{ 217int changed, changed_fs; 218 changed =ce_match_stat(ce, st, really); 219if(!changed) 220return0; 221/* 222 * If the mode or type has changed, there's no point in trying 223 * to refresh the entry - it's not going to match 224 */ 225if(changed & (MODE_CHANGED | TYPE_CHANGED)) 226return changed; 227 228/* Immediately after read-tree or update-index --cacheinfo, 229 * the length field is zero. For other cases the ce_size 230 * should match the SHA1 recorded in the index entry. 231 */ 232if((changed & DATA_CHANGED) && ce->ce_size !=htonl(0)) 233return changed; 234 235 changed_fs =ce_modified_check_fs(ce, st); 236if(changed_fs) 237return changed | changed_fs; 238return0; 239} 240 241intbase_name_compare(const char*name1,int len1,int mode1, 242const char*name2,int len2,int mode2) 243{ 244unsigned char c1, c2; 245int len = len1 < len2 ? len1 : len2; 246int cmp; 247 248 cmp =memcmp(name1, name2, len); 249if(cmp) 250return cmp; 251 c1 = name1[len]; 252 c2 = name2[len]; 253if(!c1 &&S_ISDIR(mode1)) 254 c1 ='/'; 255if(!c2 &&S_ISDIR(mode2)) 256 c2 ='/'; 257return(c1 < c2) ? -1: (c1 > c2) ?1:0; 258} 259 260intcache_name_compare(const char*name1,int flags1,const char*name2,int flags2) 261{ 262int len1 = flags1 & CE_NAMEMASK; 263int len2 = flags2 & CE_NAMEMASK; 264int len = len1 < len2 ? len1 : len2; 265int cmp; 266 267 cmp =memcmp(name1, name2, len); 268if(cmp) 269return cmp; 270if(len1 < len2) 271return-1; 272if(len1 > len2) 273return1; 274 275/* Compare stages */ 276 flags1 &= CE_STAGEMASK; 277 flags2 &= CE_STAGEMASK; 278 279if(flags1 < flags2) 280return-1; 281if(flags1 > flags2) 282return1; 283return0; 284} 285 286intcache_name_pos(const char*name,int namelen) 287{ 288int first, last; 289 290 first =0; 291 last = active_nr; 292while(last > first) { 293int next = (last + first) >>1; 294struct cache_entry *ce = active_cache[next]; 295int cmp =cache_name_compare(name, namelen, ce->name,ntohs(ce->ce_flags)); 296if(!cmp) 297return next; 298if(cmp <0) { 299 last = next; 300continue; 301} 302 first = next+1; 303} 304return-first-1; 305} 306 307/* Remove entry, return true if there are more entries to go.. */ 308intremove_cache_entry_at(int pos) 309{ 310 active_cache_changed =1; 311 active_nr--; 312if(pos >= active_nr) 313return0; 314memmove(active_cache + pos, active_cache + pos +1, (active_nr - pos) *sizeof(struct cache_entry *)); 315return1; 316} 317 318intremove_file_from_cache(const char*path) 319{ 320int pos =cache_name_pos(path,strlen(path)); 321if(pos <0) 322 pos = -pos-1; 323while(pos < active_nr && !strcmp(active_cache[pos]->name, path)) 324remove_cache_entry_at(pos); 325return0; 326} 327 328intadd_file_to_index(const char*path,int verbose) 329{ 330int size, namelen; 331struct stat st; 332struct cache_entry *ce; 333 334if(lstat(path, &st)) 335die("%s: unable to stat (%s)", path,strerror(errno)); 336 337if(!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)) 338die("%s: can only add regular files or symbolic links", path); 339 340 namelen =strlen(path); 341 size =cache_entry_size(namelen); 342 ce =xcalloc(1, size); 343memcpy(ce->name, path, namelen); 344 ce->ce_flags =htons(namelen); 345fill_stat_cache_info(ce, &st); 346 347 ce->ce_mode =create_ce_mode(st.st_mode); 348if(!trust_executable_bit) { 349/* If there is an existing entry, pick the mode bits 350 * from it. 351 */ 352int pos =cache_name_pos(path, namelen); 353if(pos >=0) 354 ce->ce_mode = active_cache[pos]->ce_mode; 355} 356 357if(index_path(ce->sha1, path, &st,1)) 358die("unable to index file%s", path); 359if(add_cache_entry(ce, ADD_CACHE_OK_TO_ADD)) 360die("unable to add%sto index",path); 361if(verbose) 362printf("add '%s'\n", path); 363cache_tree_invalidate_path(active_cache_tree, path); 364return0; 365} 366 367intce_same_name(struct cache_entry *a,struct cache_entry *b) 368{ 369int len =ce_namelen(a); 370returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 371} 372 373intce_path_match(const struct cache_entry *ce,const char**pathspec) 374{ 375const char*match, *name; 376int len; 377 378if(!pathspec) 379return1; 380 381 len =ce_namelen(ce); 382 name = ce->name; 383while((match = *pathspec++) != NULL) { 384int matchlen =strlen(match); 385if(matchlen > len) 386continue; 387if(memcmp(name, match, matchlen)) 388continue; 389if(matchlen && name[matchlen-1] =='/') 390return1; 391if(name[matchlen] =='/'|| !name[matchlen]) 392return1; 393if(!matchlen) 394return1; 395} 396return0; 397} 398 399/* 400 * We fundamentally don't like some paths: we don't want 401 * dot or dot-dot anywhere, and for obvious reasons don't 402 * want to recurse into ".git" either. 403 * 404 * Also, we don't want double slashes or slashes at the 405 * end that can make pathnames ambiguous. 406 */ 407static intverify_dotfile(const char*rest) 408{ 409/* 410 * The first character was '.', but that 411 * has already been discarded, we now test 412 * the rest. 413 */ 414switch(*rest) { 415/* "." is not allowed */ 416case'\0':case'/': 417return0; 418 419/* 420 * ".git" followed by NUL or slash is bad. This 421 * shares the path end test with the ".." case. 422 */ 423case'g': 424if(rest[1] !='i') 425break; 426if(rest[2] !='t') 427break; 428 rest +=2; 429/* fallthrough */ 430case'.': 431if(rest[1] =='\0'|| rest[1] =='/') 432return0; 433} 434return1; 435} 436 437intverify_path(const char*path) 438{ 439char c; 440 441goto inside; 442for(;;) { 443if(!c) 444return1; 445if(c =='/') { 446inside: 447 c = *path++; 448switch(c) { 449default: 450continue; 451case'/':case'\0': 452break; 453case'.': 454if(verify_dotfile(path)) 455continue; 456} 457return0; 458} 459 c = *path++; 460} 461} 462 463/* 464 * Do we have another file that has the beginning components being a 465 * proper superset of the name we're trying to add? 466 */ 467static inthas_file_name(const struct cache_entry *ce,int pos,int ok_to_replace) 468{ 469int retval =0; 470int len =ce_namelen(ce); 471int stage =ce_stage(ce); 472const char*name = ce->name; 473 474while(pos < active_nr) { 475struct cache_entry *p = active_cache[pos++]; 476 477if(len >=ce_namelen(p)) 478break; 479if(memcmp(name, p->name, len)) 480break; 481if(ce_stage(p) != stage) 482continue; 483if(p->name[len] !='/') 484continue; 485 retval = -1; 486if(!ok_to_replace) 487break; 488remove_cache_entry_at(--pos); 489} 490return retval; 491} 492 493/* 494 * Do we have another file with a pathname that is a proper 495 * subset of the name we're trying to add? 496 */ 497static inthas_dir_name(const struct cache_entry *ce,int pos,int ok_to_replace) 498{ 499int retval =0; 500int stage =ce_stage(ce); 501const char*name = ce->name; 502const char*slash = name +ce_namelen(ce); 503 504for(;;) { 505int len; 506 507for(;;) { 508if(*--slash =='/') 509break; 510if(slash <= ce->name) 511return retval; 512} 513 len = slash - name; 514 515 pos =cache_name_pos(name,ntohs(create_ce_flags(len, stage))); 516if(pos >=0) { 517 retval = -1; 518if(ok_to_replace) 519break; 520remove_cache_entry_at(pos); 521continue; 522} 523 524/* 525 * Trivial optimization: if we find an entry that 526 * already matches the sub-directory, then we know 527 * we're ok, and we can exit. 528 */ 529 pos = -pos-1; 530while(pos < active_nr) { 531struct cache_entry *p = active_cache[pos]; 532if((ce_namelen(p) <= len) || 533(p->name[len] !='/') || 534memcmp(p->name, name, len)) 535break;/* not our subdirectory */ 536if(ce_stage(p) == stage) 537/* p is at the same stage as our entry, and 538 * is a subdirectory of what we are looking 539 * at, so we cannot have conflicts at our 540 * level or anything shorter. 541 */ 542return retval; 543 pos++; 544} 545} 546return retval; 547} 548 549/* We may be in a situation where we already have path/file and path 550 * is being added, or we already have path and path/file is being 551 * added. Either one would result in a nonsense tree that has path 552 * twice when git-write-tree tries to write it out. Prevent it. 553 * 554 * If ok-to-replace is specified, we remove the conflicting entries 555 * from the cache so the caller should recompute the insert position. 556 * When this happens, we return non-zero. 557 */ 558static intcheck_file_directory_conflict(const struct cache_entry *ce,int pos,int ok_to_replace) 559{ 560/* 561 * We check if the path is a sub-path of a subsequent pathname 562 * first, since removing those will not change the position 563 * in the array 564 */ 565int retval =has_file_name(ce, pos, ok_to_replace); 566/* 567 * Then check if the path might have a clashing sub-directory 568 * before it. 569 */ 570return retval +has_dir_name(ce, pos, ok_to_replace); 571} 572 573intadd_cache_entry(struct cache_entry *ce,int option) 574{ 575int pos; 576int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 577int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 578int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 579 580 pos =cache_name_pos(ce->name,ntohs(ce->ce_flags)); 581 582/* existing match? Just replace it. */ 583if(pos >=0) { 584 active_cache_changed =1; 585 active_cache[pos] = ce; 586return0; 587} 588 pos = -pos-1; 589 590/* 591 * Inserting a merged entry ("stage 0") into the index 592 * will always replace all non-merged entries.. 593 */ 594if(pos < active_nr &&ce_stage(ce) ==0) { 595while(ce_same_name(active_cache[pos], ce)) { 596 ok_to_add =1; 597if(!remove_cache_entry_at(pos)) 598break; 599} 600} 601 602if(!ok_to_add) 603return-1; 604if(!verify_path(ce->name)) 605return-1; 606 607if(!skip_df_check && 608check_file_directory_conflict(ce, pos, ok_to_replace)) { 609if(!ok_to_replace) 610return-1; 611 pos =cache_name_pos(ce->name,ntohs(ce->ce_flags)); 612 pos = -pos-1; 613} 614 615/* Make sure the array is big enough .. */ 616if(active_nr == active_alloc) { 617 active_alloc =alloc_nr(active_alloc); 618 active_cache =xrealloc(active_cache, active_alloc *sizeof(struct cache_entry *)); 619} 620 621/* Add it in.. */ 622 active_nr++; 623if(active_nr > pos) 624memmove(active_cache + pos +1, active_cache + pos, (active_nr - pos -1) *sizeof(ce)); 625 active_cache[pos] = ce; 626 active_cache_changed =1; 627return0; 628} 629 630/* 631 * "refresh" does not calculate a new sha1 file or bring the 632 * cache up-to-date for mode/content changes. But what it 633 * _does_ do is to "re-match" the stat information of a file 634 * with the cache, so that you can refresh the cache for a 635 * file that hasn't been changed but where the stat entry is 636 * out of date. 637 * 638 * For example, you'd want to do this after doing a "git-read-tree", 639 * to link up the stat cache details with the proper files. 640 */ 641struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really) 642{ 643struct stat st; 644struct cache_entry *updated; 645int changed, size; 646 647if(lstat(ce->name, &st) <0) { 648 cache_errno = errno; 649return NULL; 650} 651 652 changed =ce_match_stat(ce, &st, really); 653if(!changed) { 654if(really && assume_unchanged && 655!(ce->ce_flags &htons(CE_VALID))) 656;/* mark this one VALID again */ 657else 658return ce; 659} 660 661if(ce_modified(ce, &st, really)) { 662 cache_errno = EINVAL; 663return NULL; 664} 665 666 size =ce_size(ce); 667 updated =xmalloc(size); 668memcpy(updated, ce, size); 669fill_stat_cache_info(updated, &st); 670 671/* In this case, if really is not set, we should leave 672 * CE_VALID bit alone. Otherwise, paths marked with 673 * --no-assume-unchanged (i.e. things to be edited) will 674 * reacquire CE_VALID bit automatically, which is not 675 * really what we want. 676 */ 677if(!really && assume_unchanged && !(ce->ce_flags &htons(CE_VALID))) 678 updated->ce_flags &= ~htons(CE_VALID); 679 680return updated; 681} 682 683intrefresh_cache(unsigned int flags) 684{ 685int i; 686int has_errors =0; 687int really = (flags & REFRESH_REALLY) !=0; 688int allow_unmerged = (flags & REFRESH_UNMERGED) !=0; 689int quiet = (flags & REFRESH_QUIET) !=0; 690int not_new = (flags & REFRESH_IGNORE_MISSING) !=0; 691 692for(i =0; i < active_nr; i++) { 693struct cache_entry *ce, *new; 694 ce = active_cache[i]; 695if(ce_stage(ce)) { 696while((i < active_nr) && 697!strcmp(active_cache[i]->name, ce->name)) 698 i++; 699 i--; 700if(allow_unmerged) 701continue; 702printf("%s: needs merge\n", ce->name); 703 has_errors =1; 704continue; 705} 706 707new=refresh_cache_entry(ce, really); 708if(new== ce) 709continue; 710if(!new) { 711if(not_new && cache_errno == ENOENT) 712continue; 713if(really && cache_errno == EINVAL) { 714/* If we are doing --really-refresh that 715 * means the index is not valid anymore. 716 */ 717 ce->ce_flags &= ~htons(CE_VALID); 718 active_cache_changed =1; 719} 720if(quiet) 721continue; 722printf("%s: needs update\n", ce->name); 723 has_errors =1; 724continue; 725} 726 active_cache_changed =1; 727/* You can NOT just free active_cache[i] here, since it 728 * might not be necessarily malloc()ed but can also come 729 * from mmap(). */ 730 active_cache[i] =new; 731} 732return has_errors; 733} 734 735static intverify_hdr(struct cache_header *hdr,unsigned long size) 736{ 737 SHA_CTX c; 738unsigned char sha1[20]; 739 740if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE)) 741returnerror("bad signature"); 742if(hdr->hdr_version !=htonl(2)) 743returnerror("bad index version"); 744SHA1_Init(&c); 745SHA1_Update(&c, hdr, size -20); 746SHA1_Final(sha1, &c); 747if(memcmp(sha1, (char*) hdr + size -20,20)) 748returnerror("bad index file sha1 signature"); 749return0; 750} 751 752static intread_index_extension(const char*ext,void*data,unsigned long sz) 753{ 754switch(CACHE_EXT(ext)) { 755case CACHE_EXT_TREE: 756 active_cache_tree =cache_tree_read(data, sz); 757break; 758default: 759if(*ext <'A'||'Z'< *ext) 760returnerror("index uses %.4s extension, which we do not understand", 761 ext); 762fprintf(stderr,"ignoring %.4s extension\n", ext); 763break; 764} 765return0; 766} 767 768intread_cache(void) 769{ 770returnread_cache_from(get_index_file()); 771} 772 773/* remember to discard_cache() before reading a different cache! */ 774intread_cache_from(const char*path) 775{ 776int fd, i; 777struct stat st; 778unsigned long offset; 779struct cache_header *hdr; 780 781 errno = EBUSY; 782if(cache_mmap) 783return active_nr; 784 785 errno = ENOENT; 786 index_file_timestamp =0; 787 fd =open(path, O_RDONLY); 788if(fd <0) { 789if(errno == ENOENT) 790return0; 791die("index file open failed (%s)",strerror(errno)); 792} 793 794 cache_mmap = MAP_FAILED; 795if(!fstat(fd, &st)) { 796 cache_mmap_size = st.st_size; 797 errno = EINVAL; 798if(cache_mmap_size >=sizeof(struct cache_header) +20) 799 cache_mmap =mmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0); 800} 801close(fd); 802if(cache_mmap == MAP_FAILED) 803die("index file mmap failed (%s)",strerror(errno)); 804 805 hdr = cache_mmap; 806if(verify_hdr(hdr, cache_mmap_size) <0) 807goto unmap; 808 809 active_nr =ntohl(hdr->hdr_entries); 810 active_alloc =alloc_nr(active_nr); 811 active_cache =xcalloc(active_alloc,sizeof(struct cache_entry *)); 812 813 offset =sizeof(*hdr); 814for(i =0; i < active_nr; i++) { 815struct cache_entry *ce = (struct cache_entry *) ((char*) cache_mmap + offset); 816 offset = offset +ce_size(ce); 817 active_cache[i] = ce; 818} 819 index_file_timestamp = st.st_mtime; 820while(offset <= cache_mmap_size -20-8) { 821/* After an array of active_nr index entries, 822 * there can be arbitrary number of extended 823 * sections, each of which is prefixed with 824 * extension name (4-byte) and section length 825 * in 4-byte network byte order. 826 */ 827unsigned long extsize; 828memcpy(&extsize, (char*) cache_mmap + offset +4,4); 829 extsize =ntohl(extsize); 830if(read_index_extension(((const char*) cache_mmap) + offset, 831(char*) cache_mmap + offset +8, 832 extsize) <0) 833goto unmap; 834 offset +=8; 835 offset += extsize; 836} 837return active_nr; 838 839unmap: 840munmap(cache_mmap, cache_mmap_size); 841 errno = EINVAL; 842die("index file corrupt"); 843} 844 845#define WRITE_BUFFER_SIZE 8192 846static unsigned char write_buffer[WRITE_BUFFER_SIZE]; 847static unsigned long write_buffer_len; 848 849static intce_write_flush(SHA_CTX *context,int fd) 850{ 851unsigned int buffered = write_buffer_len; 852if(buffered) { 853SHA1_Update(context, write_buffer, buffered); 854if(write(fd, write_buffer, buffered) != buffered) 855return-1; 856 write_buffer_len =0; 857} 858return0; 859} 860 861static intce_write(SHA_CTX *context,int fd,void*data,unsigned int len) 862{ 863while(len) { 864unsigned int buffered = write_buffer_len; 865unsigned int partial = WRITE_BUFFER_SIZE - buffered; 866if(partial > len) 867 partial = len; 868memcpy(write_buffer + buffered, data, partial); 869 buffered += partial; 870if(buffered == WRITE_BUFFER_SIZE) { 871 write_buffer_len = buffered; 872if(ce_write_flush(context, fd)) 873return-1; 874 buffered =0; 875} 876 write_buffer_len = buffered; 877 len -= partial; 878 data = (char*) data + partial; 879} 880return0; 881} 882 883static intwrite_index_ext_header(SHA_CTX *context,int fd, 884unsigned int ext,unsigned int sz) 885{ 886 ext =htonl(ext); 887 sz =htonl(sz); 888return((ce_write(context, fd, &ext,4) <0) || 889(ce_write(context, fd, &sz,4) <0)) ? -1:0; 890} 891 892static intce_flush(SHA_CTX *context,int fd) 893{ 894unsigned int left = write_buffer_len; 895 896if(left) { 897 write_buffer_len =0; 898SHA1_Update(context, write_buffer, left); 899} 900 901/* Flush first if not enough space for SHA1 signature */ 902if(left +20> WRITE_BUFFER_SIZE) { 903if(write(fd, write_buffer, left) != left) 904return-1; 905 left =0; 906} 907 908/* Append the SHA1 signature at the end */ 909SHA1_Final(write_buffer + left, context); 910 left +=20; 911return(write(fd, write_buffer, left) != left) ? -1:0; 912} 913 914static voidce_smudge_racily_clean_entry(struct cache_entry *ce) 915{ 916/* 917 * The only thing we care about in this function is to smudge the 918 * falsely clean entry due to touch-update-touch race, so we leave 919 * everything else as they are. We are called for entries whose 920 * ce_mtime match the index file mtime. 921 */ 922struct stat st; 923 924if(lstat(ce->name, &st) <0) 925return; 926if(ce_match_stat_basic(ce, &st)) 927return; 928if(ce_modified_check_fs(ce, &st)) { 929/* This is "racily clean"; smudge it. Note that this 930 * is a tricky code. At first glance, it may appear 931 * that it can break with this sequence: 932 * 933 * $ echo xyzzy >frotz 934 * $ git-update-index --add frotz 935 * $ : >frotz 936 * $ sleep 3 937 * $ echo filfre >nitfol 938 * $ git-update-index --add nitfol 939 * 940 * but it does not. When the second update-index runs, 941 * it notices that the entry "frotz" has the same timestamp 942 * as index, and if we were to smudge it by resetting its 943 * size to zero here, then the object name recorded 944 * in index is the 6-byte file but the cached stat information 945 * becomes zero --- which would then match what we would 946 * obtain from the filesystem next time we stat("frotz"). 947 * 948 * However, the second update-index, before calling 949 * this function, notices that the cached size is 6 950 * bytes and what is on the filesystem is an empty 951 * file, and never calls us, so the cached size information 952 * for "frotz" stays 6 which does not match the filesystem. 953 */ 954 ce->ce_size =htonl(0); 955} 956} 957 958intwrite_cache(int newfd,struct cache_entry **cache,int entries) 959{ 960 SHA_CTX c; 961struct cache_header hdr; 962int i, removed, recent; 963struct stat st; 964time_t now; 965 966for(i = removed =0; i < entries; i++) 967if(!cache[i]->ce_mode) 968 removed++; 969 970 hdr.hdr_signature =htonl(CACHE_SIGNATURE); 971 hdr.hdr_version =htonl(2); 972 hdr.hdr_entries =htonl(entries - removed); 973 974SHA1_Init(&c); 975if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0) 976return-1; 977 978for(i =0; i < entries; i++) { 979struct cache_entry *ce = cache[i]; 980if(!ce->ce_mode) 981continue; 982if(index_file_timestamp && 983 index_file_timestamp <=ntohl(ce->ce_mtime.sec)) 984ce_smudge_racily_clean_entry(ce); 985if(ce_write(&c, newfd, ce,ce_size(ce)) <0) 986return-1; 987} 988 989/* Write extension data here */ 990if(active_cache_tree) { 991unsigned long sz; 992void*data =cache_tree_write(active_cache_tree, &sz); 993if(data && 994!write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) && 995!ce_write(&c, newfd, data, sz)) 996; 997else{ 998free(data); 999return-1;1000}1001}10021003/*1004 * To prevent later ce_match_stat() from always falling into1005 * check_fs(), if we have too many entries that can trigger1006 * racily clean check, we are better off delaying the return.1007 * We arbitrarily say if more than 20 paths or 25% of total1008 * paths are very new, we delay the return until the index1009 * file gets a new timestamp.1010 *1011 * NOTE! NOTE! NOTE!1012 *1013 * This assumes that nobody is touching the working tree while1014 * we are updating the index.1015 */10161017/* Make sure that the new index file has st_mtime1018 * that is current enough -- ce_write() batches the data1019 * so it might not have written anything yet.1020 */1021ce_write_flush(&c, newfd);10221023 now =fstat(newfd, &st) ?0: st.st_mtime;1024if(now) {1025 recent =0;1026for(i =0; i < entries; i++) {1027struct cache_entry *ce = cache[i];1028time_t entry_time = (time_t)ntohl(ce->ce_mtime.sec);1029if(!ce->ce_mode)1030continue;1031if(now && now <= entry_time)1032 recent++;1033}1034if(20< recent && entries <= recent *4) {1035#if 01036fprintf(stderr,"entries%d\n", entries);1037fprintf(stderr,"recent%d\n", recent);1038fprintf(stderr,"now%lu\n", now);1039#endif1040while(!fstat(newfd, &st) && st.st_mtime <= now) {1041 off_t where =lseek(newfd,0, SEEK_CUR);1042sleep(1);1043if((where == (off_t) -1) ||1044(write(newfd,"",1) !=1) ||1045(lseek(newfd, -1, SEEK_CUR) != where) ||1046ftruncate(newfd, where))1047break;1048}1049}1050}1051returnce_flush(&c, newfd);1052}