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