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