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