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