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