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; 22static time_t index_file_timestamp; 23unsigned int active_nr, active_alloc, active_cache_changed; 24 25struct cache_tree *active_cache_tree; 26 27static void*cache_mmap; 28static size_t cache_mmap_size; 29 30/* 31 * This only updates the "non-critical" parts of the directory 32 * cache, ie the parts that aren't tracked by GIT, and only used 33 * to validate the cache. 34 */ 35voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 36{ 37 ce->ce_ctime.sec =htonl(st->st_ctime); 38 ce->ce_mtime.sec =htonl(st->st_mtime); 39#ifdef USE_NSEC 40 ce->ce_ctime.nsec =htonl(st->st_ctim.tv_nsec); 41 ce->ce_mtime.nsec =htonl(st->st_mtim.tv_nsec); 42#endif 43 ce->ce_dev =htonl(st->st_dev); 44 ce->ce_ino =htonl(st->st_ino); 45 ce->ce_uid =htonl(st->st_uid); 46 ce->ce_gid =htonl(st->st_gid); 47 ce->ce_size =htonl(st->st_size); 48 49if(assume_unchanged) 50 ce->ce_flags |=htons(CE_VALID); 51} 52 53static intce_compare_data(struct cache_entry *ce,struct stat *st) 54{ 55int match = -1; 56int fd =open(ce->name, O_RDONLY); 57 58if(fd >=0) { 59unsigned char sha1[20]; 60if(!index_fd(sha1, fd, st,0, OBJ_BLOB, ce->name)) 61 match =hashcmp(sha1, ce->sha1); 62/* index_fd() closed the file descriptor already */ 63} 64return match; 65} 66 67static intce_compare_link(struct cache_entry *ce,size_t expected_size) 68{ 69int match = -1; 70char*target; 71void*buffer; 72unsigned long size; 73enum object_type type; 74int len; 75 76 target =xmalloc(expected_size); 77 len =readlink(ce->name, target, expected_size); 78if(len != expected_size) { 79free(target); 80return-1; 81} 82 buffer =read_sha1_file(ce->sha1, &type, &size); 83if(!buffer) { 84free(target); 85return-1; 86} 87if(size == expected_size) 88 match =memcmp(buffer, target, size); 89free(buffer); 90free(target); 91return match; 92} 93 94static intce_modified_check_fs(struct cache_entry *ce,struct stat *st) 95{ 96switch(st->st_mode & S_IFMT) { 97case S_IFREG: 98if(ce_compare_data(ce, st)) 99return DATA_CHANGED; 100break; 101case S_IFLNK: 102if(ce_compare_link(ce,xsize_t(st->st_size))) 103return DATA_CHANGED; 104break; 105default: 106return TYPE_CHANGED; 107} 108return0; 109} 110 111static intce_match_stat_basic(struct cache_entry *ce,struct stat *st) 112{ 113unsigned int changed =0; 114 115switch(ntohl(ce->ce_mode) & S_IFMT) { 116case S_IFREG: 117 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 118/* We consider only the owner x bit to be relevant for 119 * "mode changes" 120 */ 121if(trust_executable_bit && 122(0100& (ntohl(ce->ce_mode) ^ st->st_mode))) 123 changed |= MODE_CHANGED; 124break; 125case S_IFLNK: 126if(!S_ISLNK(st->st_mode) && 127(has_symlinks || !S_ISREG(st->st_mode))) 128 changed |= TYPE_CHANGED; 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_cache(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 347if(trust_executable_bit && has_symlinks) 348 ce->ce_mode =create_ce_mode(st.st_mode); 349else{ 350/* If there is an existing entry, pick the mode bits and type 351 * from it, otherwise assume unexecutable regular file. 352 */ 353struct cache_entry *ent; 354int pos =cache_name_pos(path, namelen); 355 356 ent = (0<= pos) ? active_cache[pos] : NULL; 357 ce->ce_mode =ce_mode_from_stat(ent, st.st_mode); 358} 359 360if(index_path(ce->sha1, path, &st,1)) 361die("unable to index file%s", path); 362if(add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE)) 363die("unable to add%sto index",path); 364if(verbose) 365printf("add '%s'\n", path); 366cache_tree_invalidate_path(active_cache_tree, path); 367return0; 368} 369 370intce_same_name(struct cache_entry *a,struct cache_entry *b) 371{ 372int len =ce_namelen(a); 373returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 374} 375 376intce_path_match(const struct cache_entry *ce,const char**pathspec) 377{ 378const char*match, *name; 379int len; 380 381if(!pathspec) 382return1; 383 384 len =ce_namelen(ce); 385 name = ce->name; 386while((match = *pathspec++) != NULL) { 387int matchlen =strlen(match); 388if(matchlen > len) 389continue; 390if(memcmp(name, match, matchlen)) 391continue; 392if(matchlen && name[matchlen-1] =='/') 393return1; 394if(name[matchlen] =='/'|| !name[matchlen]) 395return1; 396if(!matchlen) 397return1; 398} 399return0; 400} 401 402/* 403 * We fundamentally don't like some paths: we don't want 404 * dot or dot-dot anywhere, and for obvious reasons don't 405 * want to recurse into ".git" either. 406 * 407 * Also, we don't want double slashes or slashes at the 408 * end that can make pathnames ambiguous. 409 */ 410static intverify_dotfile(const char*rest) 411{ 412/* 413 * The first character was '.', but that 414 * has already been discarded, we now test 415 * the rest. 416 */ 417switch(*rest) { 418/* "." is not allowed */ 419case'\0':case'/': 420return0; 421 422/* 423 * ".git" followed by NUL or slash is bad. This 424 * shares the path end test with the ".." case. 425 */ 426case'g': 427if(rest[1] !='i') 428break; 429if(rest[2] !='t') 430break; 431 rest +=2; 432/* fallthrough */ 433case'.': 434if(rest[1] =='\0'|| rest[1] =='/') 435return0; 436} 437return1; 438} 439 440intverify_path(const char*path) 441{ 442char c; 443 444goto inside; 445for(;;) { 446if(!c) 447return1; 448if(c =='/') { 449inside: 450 c = *path++; 451switch(c) { 452default: 453continue; 454case'/':case'\0': 455break; 456case'.': 457if(verify_dotfile(path)) 458continue; 459} 460return0; 461} 462 c = *path++; 463} 464} 465 466/* 467 * Do we have another file that has the beginning components being a 468 * proper superset of the name we're trying to add? 469 */ 470static inthas_file_name(const struct cache_entry *ce,int pos,int ok_to_replace) 471{ 472int retval =0; 473int len =ce_namelen(ce); 474int stage =ce_stage(ce); 475const char*name = ce->name; 476 477while(pos < active_nr) { 478struct cache_entry *p = active_cache[pos++]; 479 480if(len >=ce_namelen(p)) 481break; 482if(memcmp(name, p->name, len)) 483break; 484if(ce_stage(p) != stage) 485continue; 486if(p->name[len] !='/') 487continue; 488if(!ce_stage(p) && !p->ce_mode) 489continue; 490 retval = -1; 491if(!ok_to_replace) 492break; 493remove_cache_entry_at(--pos); 494} 495return retval; 496} 497 498/* 499 * Do we have another file with a pathname that is a proper 500 * subset of the name we're trying to add? 501 */ 502static inthas_dir_name(const struct cache_entry *ce,int pos,int ok_to_replace) 503{ 504int retval =0; 505int stage =ce_stage(ce); 506const char*name = ce->name; 507const char*slash = name +ce_namelen(ce); 508 509for(;;) { 510int len; 511 512for(;;) { 513if(*--slash =='/') 514break; 515if(slash <= ce->name) 516return retval; 517} 518 len = slash - name; 519 520 pos =cache_name_pos(name,ntohs(create_ce_flags(len, stage))); 521if(pos >=0) { 522/* 523 * Found one, but not so fast. This could 524 * be a marker that says "I was here, but 525 * I am being removed". Such an entry is 526 * not a part of the resulting tree, and 527 * it is Ok to have a directory at the same 528 * path. 529 */ 530if(stage || active_cache[pos]->ce_mode) { 531 retval = -1; 532if(!ok_to_replace) 533break; 534remove_cache_entry_at(pos); 535continue; 536} 537} 538else 539 pos = -pos-1; 540 541/* 542 * Trivial optimization: if we find an entry that 543 * already matches the sub-directory, then we know 544 * we're ok, and we can exit. 545 */ 546while(pos < active_nr) { 547struct cache_entry *p = active_cache[pos]; 548if((ce_namelen(p) <= len) || 549(p->name[len] !='/') || 550memcmp(p->name, name, len)) 551break;/* not our subdirectory */ 552if(ce_stage(p) == stage && (stage || p->ce_mode)) 553/* p is at the same stage as our entry, and 554 * is a subdirectory of what we are looking 555 * at, so we cannot have conflicts at our 556 * level or anything shorter. 557 */ 558return retval; 559 pos++; 560} 561} 562return retval; 563} 564 565/* We may be in a situation where we already have path/file and path 566 * is being added, or we already have path and path/file is being 567 * added. Either one would result in a nonsense tree that has path 568 * twice when git-write-tree tries to write it out. Prevent it. 569 * 570 * If ok-to-replace is specified, we remove the conflicting entries 571 * from the cache so the caller should recompute the insert position. 572 * When this happens, we return non-zero. 573 */ 574static intcheck_file_directory_conflict(const struct cache_entry *ce,int pos,int ok_to_replace) 575{ 576int retval; 577 578/* 579 * When ce is an "I am going away" entry, we allow it to be added 580 */ 581if(!ce_stage(ce) && !ce->ce_mode) 582return0; 583 584/* 585 * We check if the path is a sub-path of a subsequent pathname 586 * first, since removing those will not change the position 587 * in the array. 588 */ 589 retval =has_file_name(ce, pos, ok_to_replace); 590 591/* 592 * Then check if the path might have a clashing sub-directory 593 * before it. 594 */ 595return retval +has_dir_name(ce, pos, ok_to_replace); 596} 597 598intadd_cache_entry(struct cache_entry *ce,int option) 599{ 600int pos; 601int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 602int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 603int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 604 605 pos =cache_name_pos(ce->name,ntohs(ce->ce_flags)); 606 607/* existing match? Just replace it. */ 608if(pos >=0) { 609 active_cache_changed =1; 610 active_cache[pos] = ce; 611return0; 612} 613 pos = -pos-1; 614 615/* 616 * Inserting a merged entry ("stage 0") into the index 617 * will always replace all non-merged entries.. 618 */ 619if(pos < active_nr &&ce_stage(ce) ==0) { 620while(ce_same_name(active_cache[pos], ce)) { 621 ok_to_add =1; 622if(!remove_cache_entry_at(pos)) 623break; 624} 625} 626 627if(!ok_to_add) 628return-1; 629if(!verify_path(ce->name)) 630return-1; 631 632if(!skip_df_check && 633check_file_directory_conflict(ce, pos, ok_to_replace)) { 634if(!ok_to_replace) 635returnerror("'%s' appears as both a file and as a directory", ce->name); 636 pos =cache_name_pos(ce->name,ntohs(ce->ce_flags)); 637 pos = -pos-1; 638} 639 640/* Make sure the array is big enough .. */ 641if(active_nr == active_alloc) { 642 active_alloc =alloc_nr(active_alloc); 643 active_cache =xrealloc(active_cache, active_alloc *sizeof(struct cache_entry *)); 644} 645 646/* Add it in.. */ 647 active_nr++; 648if(active_nr > pos) 649memmove(active_cache + pos +1, active_cache + pos, (active_nr - pos -1) *sizeof(ce)); 650 active_cache[pos] = ce; 651 active_cache_changed =1; 652return0; 653} 654 655/* 656 * "refresh" does not calculate a new sha1 file or bring the 657 * cache up-to-date for mode/content changes. But what it 658 * _does_ do is to "re-match" the stat information of a file 659 * with the cache, so that you can refresh the cache for a 660 * file that hasn't been changed but where the stat entry is 661 * out of date. 662 * 663 * For example, you'd want to do this after doing a "git-read-tree", 664 * to link up the stat cache details with the proper files. 665 */ 666static struct cache_entry *refresh_cache_ent(struct cache_entry *ce,int really,int*err) 667{ 668struct stat st; 669struct cache_entry *updated; 670int changed, size; 671 672if(lstat(ce->name, &st) <0) { 673if(err) 674*err = errno; 675return NULL; 676} 677 678 changed =ce_match_stat(ce, &st, really); 679if(!changed) { 680if(really && assume_unchanged && 681!(ce->ce_flags &htons(CE_VALID))) 682;/* mark this one VALID again */ 683else 684return ce; 685} 686 687if(ce_modified(ce, &st, really)) { 688if(err) 689*err = EINVAL; 690return NULL; 691} 692 693 size =ce_size(ce); 694 updated =xmalloc(size); 695memcpy(updated, ce, size); 696fill_stat_cache_info(updated, &st); 697 698/* In this case, if really is not set, we should leave 699 * CE_VALID bit alone. Otherwise, paths marked with 700 * --no-assume-unchanged (i.e. things to be edited) will 701 * reacquire CE_VALID bit automatically, which is not 702 * really what we want. 703 */ 704if(!really && assume_unchanged && !(ce->ce_flags &htons(CE_VALID))) 705 updated->ce_flags &= ~htons(CE_VALID); 706 707return updated; 708} 709 710intrefresh_cache(unsigned int flags) 711{ 712int i; 713int has_errors =0; 714int really = (flags & REFRESH_REALLY) !=0; 715int allow_unmerged = (flags & REFRESH_UNMERGED) !=0; 716int quiet = (flags & REFRESH_QUIET) !=0; 717int not_new = (flags & REFRESH_IGNORE_MISSING) !=0; 718 719for(i =0; i < active_nr; i++) { 720struct cache_entry *ce, *new; 721int cache_errno =0; 722 723 ce = active_cache[i]; 724if(ce_stage(ce)) { 725while((i < active_nr) && 726!strcmp(active_cache[i]->name, ce->name)) 727 i++; 728 i--; 729if(allow_unmerged) 730continue; 731printf("%s: needs merge\n", ce->name); 732 has_errors =1; 733continue; 734} 735 736new=refresh_cache_ent(ce, really, &cache_errno); 737if(new== ce) 738continue; 739if(!new) { 740if(not_new && cache_errno == ENOENT) 741continue; 742if(really && cache_errno == EINVAL) { 743/* If we are doing --really-refresh that 744 * means the index is not valid anymore. 745 */ 746 ce->ce_flags &= ~htons(CE_VALID); 747 active_cache_changed =1; 748} 749if(quiet) 750continue; 751printf("%s: needs update\n", ce->name); 752 has_errors =1; 753continue; 754} 755 active_cache_changed =1; 756/* You can NOT just free active_cache[i] here, since it 757 * might not be necessarily malloc()ed but can also come 758 * from mmap(). */ 759 active_cache[i] =new; 760} 761return has_errors; 762} 763 764struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really) 765{ 766returnrefresh_cache_ent(ce, really, NULL); 767} 768 769static intverify_hdr(struct cache_header *hdr,unsigned long size) 770{ 771 SHA_CTX c; 772unsigned char sha1[20]; 773 774if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE)) 775returnerror("bad signature"); 776if(hdr->hdr_version !=htonl(2)) 777returnerror("bad index version"); 778SHA1_Init(&c); 779SHA1_Update(&c, hdr, size -20); 780SHA1_Final(sha1, &c); 781if(hashcmp(sha1, (unsigned char*)hdr + size -20)) 782returnerror("bad index file sha1 signature"); 783return0; 784} 785 786static intread_index_extension(const char*ext,void*data,unsigned long sz) 787{ 788switch(CACHE_EXT(ext)) { 789case CACHE_EXT_TREE: 790 active_cache_tree =cache_tree_read(data, sz); 791break; 792default: 793if(*ext <'A'||'Z'< *ext) 794returnerror("index uses %.4s extension, which we do not understand", 795 ext); 796fprintf(stderr,"ignoring %.4s extension\n", ext); 797break; 798} 799return0; 800} 801 802intread_cache(void) 803{ 804returnread_cache_from(get_index_file()); 805} 806 807/* remember to discard_cache() before reading a different cache! */ 808intread_cache_from(const char*path) 809{ 810int fd, i; 811struct stat st; 812unsigned long offset; 813struct cache_header *hdr; 814 815 errno = EBUSY; 816if(cache_mmap) 817return active_nr; 818 819 errno = ENOENT; 820 index_file_timestamp =0; 821 fd =open(path, O_RDONLY); 822if(fd <0) { 823if(errno == ENOENT) 824return0; 825die("index file open failed (%s)",strerror(errno)); 826} 827 828if(!fstat(fd, &st)) { 829 cache_mmap_size =xsize_t(st.st_size); 830 errno = EINVAL; 831if(cache_mmap_size >=sizeof(struct cache_header) +20) 832 cache_mmap =xmmap(NULL, cache_mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0); 833else 834die("index file smaller than expected"); 835}else 836die("cannot stat the open index (%s)",strerror(errno)); 837close(fd); 838 839 hdr = cache_mmap; 840if(verify_hdr(hdr, cache_mmap_size) <0) 841goto unmap; 842 843 active_nr =ntohl(hdr->hdr_entries); 844 active_alloc =alloc_nr(active_nr); 845 active_cache =xcalloc(active_alloc,sizeof(struct cache_entry *)); 846 847 offset =sizeof(*hdr); 848for(i =0; i < active_nr; i++) { 849struct cache_entry *ce = (struct cache_entry *) ((char*) cache_mmap + offset); 850 offset = offset +ce_size(ce); 851 active_cache[i] = ce; 852} 853 index_file_timestamp = st.st_mtime; 854while(offset <= cache_mmap_size -20-8) { 855/* After an array of active_nr index entries, 856 * there can be arbitrary number of extended 857 * sections, each of which is prefixed with 858 * extension name (4-byte) and section length 859 * in 4-byte network byte order. 860 */ 861unsigned long extsize; 862memcpy(&extsize, (char*) cache_mmap + offset +4,4); 863 extsize =ntohl(extsize); 864if(read_index_extension(((const char*) cache_mmap) + offset, 865(char*) cache_mmap + offset +8, 866 extsize) <0) 867goto unmap; 868 offset +=8; 869 offset += extsize; 870} 871return active_nr; 872 873unmap: 874munmap(cache_mmap, cache_mmap_size); 875 errno = EINVAL; 876die("index file corrupt"); 877} 878 879intdiscard_cache(void) 880{ 881int ret; 882 883 active_nr = active_cache_changed =0; 884 index_file_timestamp =0; 885cache_tree_free(&active_cache_tree); 886if(cache_mmap == NULL) 887return0; 888 ret =munmap(cache_mmap, cache_mmap_size); 889 cache_mmap = NULL; 890 cache_mmap_size =0; 891 892/* no need to throw away allocated active_cache */ 893return ret; 894} 895 896#define WRITE_BUFFER_SIZE 8192 897static unsigned char write_buffer[WRITE_BUFFER_SIZE]; 898static unsigned long write_buffer_len; 899 900static intce_write_flush(SHA_CTX *context,int fd) 901{ 902unsigned int buffered = write_buffer_len; 903if(buffered) { 904SHA1_Update(context, write_buffer, buffered); 905if(write_in_full(fd, write_buffer, buffered) != buffered) 906return-1; 907 write_buffer_len =0; 908} 909return0; 910} 911 912static intce_write(SHA_CTX *context,int fd,void*data,unsigned int len) 913{ 914while(len) { 915unsigned int buffered = write_buffer_len; 916unsigned int partial = WRITE_BUFFER_SIZE - buffered; 917if(partial > len) 918 partial = len; 919memcpy(write_buffer + buffered, data, partial); 920 buffered += partial; 921if(buffered == WRITE_BUFFER_SIZE) { 922 write_buffer_len = buffered; 923if(ce_write_flush(context, fd)) 924return-1; 925 buffered =0; 926} 927 write_buffer_len = buffered; 928 len -= partial; 929 data = (char*) data + partial; 930} 931return0; 932} 933 934static intwrite_index_ext_header(SHA_CTX *context,int fd, 935unsigned int ext,unsigned int sz) 936{ 937 ext =htonl(ext); 938 sz =htonl(sz); 939return((ce_write(context, fd, &ext,4) <0) || 940(ce_write(context, fd, &sz,4) <0)) ? -1:0; 941} 942 943static intce_flush(SHA_CTX *context,int fd) 944{ 945unsigned int left = write_buffer_len; 946 947if(left) { 948 write_buffer_len =0; 949SHA1_Update(context, write_buffer, left); 950} 951 952/* Flush first if not enough space for SHA1 signature */ 953if(left +20> WRITE_BUFFER_SIZE) { 954if(write_in_full(fd, write_buffer, left) != left) 955return-1; 956 left =0; 957} 958 959/* Append the SHA1 signature at the end */ 960SHA1_Final(write_buffer + left, context); 961 left +=20; 962return(write_in_full(fd, write_buffer, left) != left) ? -1:0; 963} 964 965static voidce_smudge_racily_clean_entry(struct cache_entry *ce) 966{ 967/* 968 * The only thing we care about in this function is to smudge the 969 * falsely clean entry due to touch-update-touch race, so we leave 970 * everything else as they are. We are called for entries whose 971 * ce_mtime match the index file mtime. 972 */ 973struct stat st; 974 975if(lstat(ce->name, &st) <0) 976return; 977if(ce_match_stat_basic(ce, &st)) 978return; 979if(ce_modified_check_fs(ce, &st)) { 980/* This is "racily clean"; smudge it. Note that this 981 * is a tricky code. At first glance, it may appear 982 * that it can break with this sequence: 983 * 984 * $ echo xyzzy >frotz 985 * $ git-update-index --add frotz 986 * $ : >frotz 987 * $ sleep 3 988 * $ echo filfre >nitfol 989 * $ git-update-index --add nitfol 990 * 991 * but it does not. When the second update-index runs, 992 * it notices that the entry "frotz" has the same timestamp 993 * as index, and if we were to smudge it by resetting its 994 * size to zero here, then the object name recorded 995 * in index is the 6-byte file but the cached stat information 996 * becomes zero --- which would then match what we would 997 * obtain from the filesystem next time we stat("frotz"). 998 * 999 * However, the second update-index, before calling1000 * this function, notices that the cached size is 61001 * bytes and what is on the filesystem is an empty1002 * file, and never calls us, so the cached size information1003 * for "frotz" stays 6 which does not match the filesystem.1004 */1005 ce->ce_size =htonl(0);1006}1007}10081009intwrite_cache(int newfd,struct cache_entry **cache,int entries)1010{1011 SHA_CTX c;1012struct cache_header hdr;1013int i, removed;10141015for(i = removed =0; i < entries; i++)1016if(!cache[i]->ce_mode)1017 removed++;10181019 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1020 hdr.hdr_version =htonl(2);1021 hdr.hdr_entries =htonl(entries - removed);10221023SHA1_Init(&c);1024if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1025return-1;10261027for(i =0; i < entries; i++) {1028struct cache_entry *ce = cache[i];1029if(!ce->ce_mode)1030continue;1031if(index_file_timestamp &&1032 index_file_timestamp <=ntohl(ce->ce_mtime.sec))1033ce_smudge_racily_clean_entry(ce);1034if(ce_write(&c, newfd, ce,ce_size(ce)) <0)1035return-1;1036}10371038/* Write extension data here */1039if(active_cache_tree) {1040unsigned long sz;1041void*data =cache_tree_write(active_cache_tree, &sz);1042if(data &&1043!write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sz) &&1044!ce_write(&c, newfd, data, sz))1045free(data);1046else{1047free(data);1048return-1;1049}1050}1051returnce_flush(&c, newfd);1052}