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