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#include"dir.h" 11#include"tree.h" 12#include"commit.h" 13#include"diff.h" 14#include"diffcore.h" 15#include"revision.h" 16 17/* Index extensions. 18 * 19 * The first letter should be 'A'..'Z' for extensions that are not 20 * necessary for a correct operation (i.e. optimization data). 21 * When new extensions are added that _needs_ to be understood in 22 * order to correctly interpret the index file, pick character that 23 * is outside the range, to cause the reader to abort. 24 */ 25 26#define CACHE_EXT(s) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) ) 27#define CACHE_EXT_TREE 0x54524545/* "TREE" */ 28 29struct index_state the_index; 30 31static voidset_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 32{ 33 istate->cache[nr] = ce; 34add_name_hash(istate, ce); 35} 36 37static voidreplace_index_entry(struct index_state *istate,int nr,struct cache_entry *ce) 38{ 39struct cache_entry *old = istate->cache[nr]; 40 41remove_name_hash(old); 42set_index_entry(istate, nr, ce); 43 istate->cache_changed =1; 44} 45 46voidrename_index_entry_at(struct index_state *istate,int nr,const char*new_name) 47{ 48struct cache_entry *old = istate->cache[nr], *new; 49int namelen =strlen(new_name); 50 51new=xmalloc(cache_entry_size(namelen)); 52copy_cache_entry(new, old); 53new->ce_flags &= ~(CE_STATE_MASK | CE_NAMEMASK); 54new->ce_flags |= (namelen >= CE_NAMEMASK ? CE_NAMEMASK : namelen); 55memcpy(new->name, new_name, namelen +1); 56 57cache_tree_invalidate_path(istate->cache_tree, old->name); 58remove_index_entry_at(istate, nr); 59add_index_entry(istate,new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE); 60} 61 62/* 63 * This only updates the "non-critical" parts of the directory 64 * cache, ie the parts that aren't tracked by GIT, and only used 65 * to validate the cache. 66 */ 67voidfill_stat_cache_info(struct cache_entry *ce,struct stat *st) 68{ 69 ce->ce_ctime = st->st_ctime; 70 ce->ce_mtime = st->st_mtime; 71 ce->ce_dev = st->st_dev; 72 ce->ce_ino = st->st_ino; 73 ce->ce_uid = st->st_uid; 74 ce->ce_gid = st->st_gid; 75 ce->ce_size = st->st_size; 76 77if(assume_unchanged) 78 ce->ce_flags |= CE_VALID; 79 80if(S_ISREG(st->st_mode)) 81ce_mark_uptodate(ce); 82} 83 84static intce_compare_data(struct cache_entry *ce,struct stat *st) 85{ 86int match = -1; 87int fd =open(ce->name, O_RDONLY); 88 89if(fd >=0) { 90unsigned char sha1[20]; 91if(!index_fd(sha1, fd, st,0, OBJ_BLOB, ce->name)) 92 match =hashcmp(sha1, ce->sha1); 93/* index_fd() closed the file descriptor already */ 94} 95return match; 96} 97 98static intce_compare_link(struct cache_entry *ce,size_t expected_size) 99{ 100int match = -1; 101char*target; 102void*buffer; 103unsigned long size; 104enum object_type type; 105int len; 106 107 target =xmalloc(expected_size); 108 len =readlink(ce->name, target, expected_size); 109if(len != expected_size) { 110free(target); 111return-1; 112} 113 buffer =read_sha1_file(ce->sha1, &type, &size); 114if(!buffer) { 115free(target); 116return-1; 117} 118if(size == expected_size) 119 match =memcmp(buffer, target, size); 120free(buffer); 121free(target); 122return match; 123} 124 125static intce_compare_gitlink(struct cache_entry *ce) 126{ 127unsigned char sha1[20]; 128 129/* 130 * We don't actually require that the .git directory 131 * under GITLINK directory be a valid git directory. It 132 * might even be missing (in case nobody populated that 133 * sub-project). 134 * 135 * If so, we consider it always to match. 136 */ 137if(resolve_gitlink_ref(ce->name,"HEAD", sha1) <0) 138return0; 139returnhashcmp(sha1, ce->sha1); 140} 141 142static intce_modified_check_fs(struct cache_entry *ce,struct stat *st) 143{ 144switch(st->st_mode & S_IFMT) { 145case S_IFREG: 146if(ce_compare_data(ce, st)) 147return DATA_CHANGED; 148break; 149case S_IFLNK: 150if(ce_compare_link(ce,xsize_t(st->st_size))) 151return DATA_CHANGED; 152break; 153case S_IFDIR: 154if(S_ISGITLINK(ce->ce_mode)) 155returnce_compare_gitlink(ce) ? DATA_CHANGED :0; 156default: 157return TYPE_CHANGED; 158} 159return0; 160} 161 162static intis_empty_blob_sha1(const unsigned char*sha1) 163{ 164static const unsigned char empty_blob_sha1[20] = { 1650xe6,0x9d,0xe2,0x9b,0xb2,0xd1,0xd6,0x43,0x4b,0x8b, 1660x29,0xae,0x77,0x5a,0xd8,0xc2,0xe4,0x8c,0x53,0x91 167}; 168 169return!hashcmp(sha1, empty_blob_sha1); 170} 171 172static intce_match_stat_basic(struct cache_entry *ce,struct stat *st) 173{ 174unsigned int changed =0; 175 176if(ce->ce_flags & CE_REMOVE) 177return MODE_CHANGED | DATA_CHANGED | TYPE_CHANGED; 178 179switch(ce->ce_mode & S_IFMT) { 180case S_IFREG: 181 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED :0; 182/* We consider only the owner x bit to be relevant for 183 * "mode changes" 184 */ 185if(trust_executable_bit && 186(0100& (ce->ce_mode ^ st->st_mode))) 187 changed |= MODE_CHANGED; 188break; 189case S_IFLNK: 190if(!S_ISLNK(st->st_mode) && 191(has_symlinks || !S_ISREG(st->st_mode))) 192 changed |= TYPE_CHANGED; 193break; 194case S_IFGITLINK: 195/* We ignore most of the st_xxx fields for gitlinks */ 196if(!S_ISDIR(st->st_mode)) 197 changed |= TYPE_CHANGED; 198else if(ce_compare_gitlink(ce)) 199 changed |= DATA_CHANGED; 200return changed; 201default: 202die("internal error: ce_mode is%o", ce->ce_mode); 203} 204if(ce->ce_mtime != (unsigned int) st->st_mtime) 205 changed |= MTIME_CHANGED; 206if(trust_ctime && ce->ce_ctime != (unsigned int) st->st_ctime) 207 changed |= CTIME_CHANGED; 208 209if(ce->ce_uid != (unsigned int) st->st_uid || 210 ce->ce_gid != (unsigned int) st->st_gid) 211 changed |= OWNER_CHANGED; 212if(ce->ce_ino != (unsigned int) st->st_ino) 213 changed |= INODE_CHANGED; 214 215#ifdef USE_STDEV 216/* 217 * st_dev breaks on network filesystems where different 218 * clients will have different views of what "device" 219 * the filesystem is on 220 */ 221if(ce->ce_dev != (unsigned int) st->st_dev) 222 changed |= INODE_CHANGED; 223#endif 224 225if(ce->ce_size != (unsigned int) st->st_size) 226 changed |= DATA_CHANGED; 227 228/* Racily smudged entry? */ 229if(!ce->ce_size) { 230if(!is_empty_blob_sha1(ce->sha1)) 231 changed |= DATA_CHANGED; 232} 233 234return changed; 235} 236 237static intis_racy_timestamp(const struct index_state *istate,struct cache_entry *ce) 238{ 239return(!S_ISGITLINK(ce->ce_mode) && 240 istate->timestamp && 241((unsigned int)istate->timestamp) <= ce->ce_mtime); 242} 243 244intie_match_stat(const struct index_state *istate, 245struct cache_entry *ce,struct stat *st, 246unsigned int options) 247{ 248unsigned int changed; 249int ignore_valid = options & CE_MATCH_IGNORE_VALID; 250int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY; 251 252/* 253 * If it's marked as always valid in the index, it's 254 * valid whatever the checked-out copy says. 255 */ 256if(!ignore_valid && (ce->ce_flags & CE_VALID)) 257return0; 258 259 changed =ce_match_stat_basic(ce, st); 260 261/* 262 * Within 1 second of this sequence: 263 * echo xyzzy >file && git-update-index --add file 264 * running this command: 265 * echo frotz >file 266 * would give a falsely clean cache entry. The mtime and 267 * length match the cache, and other stat fields do not change. 268 * 269 * We could detect this at update-index time (the cache entry 270 * being registered/updated records the same time as "now") 271 * and delay the return from git-update-index, but that would 272 * effectively mean we can make at most one commit per second, 273 * which is not acceptable. Instead, we check cache entries 274 * whose mtime are the same as the index file timestamp more 275 * carefully than others. 276 */ 277if(!changed &&is_racy_timestamp(istate, ce)) { 278if(assume_racy_is_modified) 279 changed |= DATA_CHANGED; 280else 281 changed |=ce_modified_check_fs(ce, st); 282} 283 284return changed; 285} 286 287intie_modified(const struct index_state *istate, 288struct cache_entry *ce,struct stat *st,unsigned int options) 289{ 290int changed, changed_fs; 291 292 changed =ie_match_stat(istate, ce, st, options); 293if(!changed) 294return0; 295/* 296 * If the mode or type has changed, there's no point in trying 297 * to refresh the entry - it's not going to match 298 */ 299if(changed & (MODE_CHANGED | TYPE_CHANGED)) 300return changed; 301 302/* 303 * Immediately after read-tree or update-index --cacheinfo, 304 * the length field is zero, as we have never even read the 305 * lstat(2) information once, and we cannot trust DATA_CHANGED 306 * returned by ie_match_stat() which in turn was returned by 307 * ce_match_stat_basic() to signal that the filesize of the 308 * blob changed. We have to actually go to the filesystem to 309 * see if the contents match, and if so, should answer "unchanged". 310 * 311 * The logic does not apply to gitlinks, as ce_match_stat_basic() 312 * already has checked the actual HEAD from the filesystem in the 313 * subproject. If ie_match_stat() already said it is different, 314 * then we know it is. 315 */ 316if((changed & DATA_CHANGED) && 317(S_ISGITLINK(ce->ce_mode) || ce->ce_size !=0)) 318return changed; 319 320 changed_fs =ce_modified_check_fs(ce, st); 321if(changed_fs) 322return changed | changed_fs; 323return0; 324} 325 326intbase_name_compare(const char*name1,int len1,int mode1, 327const char*name2,int len2,int mode2) 328{ 329unsigned char c1, c2; 330int len = len1 < len2 ? len1 : len2; 331int cmp; 332 333 cmp =memcmp(name1, name2, len); 334if(cmp) 335return cmp; 336 c1 = name1[len]; 337 c2 = name2[len]; 338if(!c1 &&S_ISDIR(mode1)) 339 c1 ='/'; 340if(!c2 &&S_ISDIR(mode2)) 341 c2 ='/'; 342return(c1 < c2) ? -1: (c1 > c2) ?1:0; 343} 344 345/* 346 * df_name_compare() is identical to base_name_compare(), except it 347 * compares conflicting directory/file entries as equal. Note that 348 * while a directory name compares as equal to a regular file, they 349 * then individually compare _differently_ to a filename that has 350 * a dot after the basename (because '\0' < '.' < '/'). 351 * 352 * This is used by routines that want to traverse the git namespace 353 * but then handle conflicting entries together when possible. 354 */ 355intdf_name_compare(const char*name1,int len1,int mode1, 356const char*name2,int len2,int mode2) 357{ 358int len = len1 < len2 ? len1 : len2, cmp; 359unsigned char c1, c2; 360 361 cmp =memcmp(name1, name2, len); 362if(cmp) 363return cmp; 364/* Directories and files compare equal (same length, same name) */ 365if(len1 == len2) 366return0; 367 c1 = name1[len]; 368if(!c1 &&S_ISDIR(mode1)) 369 c1 ='/'; 370 c2 = name2[len]; 371if(!c2 &&S_ISDIR(mode2)) 372 c2 ='/'; 373if(c1 =='/'&& !c2) 374return0; 375if(c2 =='/'&& !c1) 376return0; 377return c1 - c2; 378} 379 380intcache_name_compare(const char*name1,int flags1,const char*name2,int flags2) 381{ 382int len1 = flags1 & CE_NAMEMASK; 383int len2 = flags2 & CE_NAMEMASK; 384int len = len1 < len2 ? len1 : len2; 385int cmp; 386 387 cmp =memcmp(name1, name2, len); 388if(cmp) 389return cmp; 390if(len1 < len2) 391return-1; 392if(len1 > len2) 393return1; 394 395/* Compare stages */ 396 flags1 &= CE_STAGEMASK; 397 flags2 &= CE_STAGEMASK; 398 399if(flags1 < flags2) 400return-1; 401if(flags1 > flags2) 402return1; 403return0; 404} 405 406intindex_name_pos(const struct index_state *istate,const char*name,int namelen) 407{ 408int first, last; 409 410 first =0; 411 last = istate->cache_nr; 412while(last > first) { 413int next = (last + first) >>1; 414struct cache_entry *ce = istate->cache[next]; 415int cmp =cache_name_compare(name, namelen, ce->name, ce->ce_flags); 416if(!cmp) 417return next; 418if(cmp <0) { 419 last = next; 420continue; 421} 422 first = next+1; 423} 424return-first-1; 425} 426 427/* Remove entry, return true if there are more entries to go.. */ 428intremove_index_entry_at(struct index_state *istate,int pos) 429{ 430struct cache_entry *ce = istate->cache[pos]; 431 432remove_name_hash(ce); 433 istate->cache_changed =1; 434 istate->cache_nr--; 435if(pos >= istate->cache_nr) 436return0; 437memmove(istate->cache + pos, 438 istate->cache + pos +1, 439(istate->cache_nr - pos) *sizeof(struct cache_entry *)); 440return1; 441} 442 443intremove_file_from_index(struct index_state *istate,const char*path) 444{ 445int pos =index_name_pos(istate, path,strlen(path)); 446if(pos <0) 447 pos = -pos-1; 448cache_tree_invalidate_path(istate->cache_tree, path); 449while(pos < istate->cache_nr && !strcmp(istate->cache[pos]->name, path)) 450remove_index_entry_at(istate, pos); 451return0; 452} 453 454static intcompare_name(struct cache_entry *ce,const char*path,int namelen) 455{ 456return namelen !=ce_namelen(ce) ||memcmp(path, ce->name, namelen); 457} 458 459static intindex_name_pos_also_unmerged(struct index_state *istate, 460const char*path,int namelen) 461{ 462int pos =index_name_pos(istate, path, namelen); 463struct cache_entry *ce; 464 465if(pos >=0) 466return pos; 467 468/* maybe unmerged? */ 469 pos = -1- pos; 470if(pos >= istate->cache_nr || 471compare_name((ce = istate->cache[pos]), path, namelen)) 472return-1; 473 474/* order of preference: stage 2, 1, 3 */ 475if(ce_stage(ce) ==1&& pos +1< istate->cache_nr && 476ce_stage((ce = istate->cache[pos +1])) ==2&& 477!compare_name(ce, path, namelen)) 478 pos++; 479return pos; 480} 481 482static intdifferent_name(struct cache_entry *ce,struct cache_entry *alias) 483{ 484int len =ce_namelen(ce); 485returnce_namelen(alias) != len ||memcmp(ce->name, alias->name, len); 486} 487 488/* 489 * If we add a filename that aliases in the cache, we will use the 490 * name that we already have - but we don't want to update the same 491 * alias twice, because that implies that there were actually two 492 * different files with aliasing names! 493 * 494 * So we use the CE_ADDED flag to verify that the alias was an old 495 * one before we accept it as 496 */ 497static struct cache_entry *create_alias_ce(struct cache_entry *ce,struct cache_entry *alias) 498{ 499int len; 500struct cache_entry *new; 501 502if(alias->ce_flags & CE_ADDED) 503die("Will not add file alias '%s' ('%s' already exists in index)", ce->name, alias->name); 504 505/* Ok, create the new entry using the name of the existing alias */ 506 len =ce_namelen(alias); 507new=xcalloc(1,cache_entry_size(len)); 508memcpy(new->name, alias->name, len); 509copy_cache_entry(new, ce); 510free(ce); 511return new; 512} 513 514intadd_to_index(struct index_state *istate,const char*path,struct stat *st,int flags) 515{ 516int size, namelen, was_same; 517 mode_t st_mode = st->st_mode; 518struct cache_entry *ce, *alias; 519unsigned ce_option = CE_MATCH_IGNORE_VALID|CE_MATCH_RACY_IS_DIRTY; 520int verbose = flags & (ADD_CACHE_VERBOSE | ADD_CACHE_PRETEND); 521int pretend = flags & ADD_CACHE_PRETEND; 522 523if(!S_ISREG(st_mode) && !S_ISLNK(st_mode) && !S_ISDIR(st_mode)) 524returnerror("%s: can only add regular files, symbolic links or git-directories", path); 525 526 namelen =strlen(path); 527if(S_ISDIR(st_mode)) { 528while(namelen && path[namelen-1] =='/') 529 namelen--; 530} 531 size =cache_entry_size(namelen); 532 ce =xcalloc(1, size); 533memcpy(ce->name, path, namelen); 534 ce->ce_flags = namelen; 535fill_stat_cache_info(ce, st); 536 537if(trust_executable_bit && has_symlinks) 538 ce->ce_mode =create_ce_mode(st_mode); 539else{ 540/* If there is an existing entry, pick the mode bits and type 541 * from it, otherwise assume unexecutable regular file. 542 */ 543struct cache_entry *ent; 544int pos =index_name_pos_also_unmerged(istate, path, namelen); 545 546 ent = (0<= pos) ? istate->cache[pos] : NULL; 547 ce->ce_mode =ce_mode_from_stat(ent, st_mode); 548} 549 550 alias =index_name_exists(istate, ce->name,ce_namelen(ce), ignore_case); 551if(alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) { 552/* Nothing changed, really */ 553free(ce); 554ce_mark_uptodate(alias); 555 alias->ce_flags |= CE_ADDED; 556return0; 557} 558if(index_path(ce->sha1, path, st,1)) 559returnerror("unable to index file%s", path); 560if(ignore_case && alias &&different_name(ce, alias)) 561 ce =create_alias_ce(ce, alias); 562 ce->ce_flags |= CE_ADDED; 563 564/* It was suspected to be racily clean, but it turns out to be Ok */ 565 was_same = (alias && 566!ce_stage(alias) && 567!hashcmp(alias->sha1, ce->sha1) && 568 ce->ce_mode == alias->ce_mode); 569 570if(pretend) 571; 572else if(add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE)) 573returnerror("unable to add%sto index",path); 574if(verbose && !was_same) 575printf("add '%s'\n", path); 576return0; 577} 578 579intadd_file_to_index(struct index_state *istate,const char*path,int flags) 580{ 581struct stat st; 582if(lstat(path, &st)) 583die("%s: unable to stat (%s)", path,strerror(errno)); 584returnadd_to_index(istate, path, &st, flags); 585} 586 587struct cache_entry *make_cache_entry(unsigned int mode, 588const unsigned char*sha1,const char*path,int stage, 589int refresh) 590{ 591int size, len; 592struct cache_entry *ce; 593 594if(!verify_path(path)) 595return NULL; 596 597 len =strlen(path); 598 size =cache_entry_size(len); 599 ce =xcalloc(1, size); 600 601hashcpy(ce->sha1, sha1); 602memcpy(ce->name, path, len); 603 ce->ce_flags =create_ce_flags(len, stage); 604 ce->ce_mode =create_ce_mode(mode); 605 606if(refresh) 607returnrefresh_cache_entry(ce,0); 608 609return ce; 610} 611 612intce_same_name(struct cache_entry *a,struct cache_entry *b) 613{ 614int len =ce_namelen(a); 615returnce_namelen(b) == len && !memcmp(a->name, b->name, len); 616} 617 618intce_path_match(const struct cache_entry *ce,const char**pathspec) 619{ 620const char*match, *name; 621int len; 622 623if(!pathspec) 624return1; 625 626 len =ce_namelen(ce); 627 name = ce->name; 628while((match = *pathspec++) != NULL) { 629int matchlen =strlen(match); 630if(matchlen > len) 631continue; 632if(memcmp(name, match, matchlen)) 633continue; 634if(matchlen && name[matchlen-1] =='/') 635return1; 636if(name[matchlen] =='/'|| !name[matchlen]) 637return1; 638if(!matchlen) 639return1; 640} 641return0; 642} 643 644/* 645 * We fundamentally don't like some paths: we don't want 646 * dot or dot-dot anywhere, and for obvious reasons don't 647 * want to recurse into ".git" either. 648 * 649 * Also, we don't want double slashes or slashes at the 650 * end that can make pathnames ambiguous. 651 */ 652static intverify_dotfile(const char*rest) 653{ 654/* 655 * The first character was '.', but that 656 * has already been discarded, we now test 657 * the rest. 658 */ 659switch(*rest) { 660/* "." is not allowed */ 661case'\0':case'/': 662return0; 663 664/* 665 * ".git" followed by NUL or slash is bad. This 666 * shares the path end test with the ".." case. 667 */ 668case'g': 669if(rest[1] !='i') 670break; 671if(rest[2] !='t') 672break; 673 rest +=2; 674/* fallthrough */ 675case'.': 676if(rest[1] =='\0'|| rest[1] =='/') 677return0; 678} 679return1; 680} 681 682intverify_path(const char*path) 683{ 684char c; 685 686goto inside; 687for(;;) { 688if(!c) 689return1; 690if(c =='/') { 691inside: 692 c = *path++; 693switch(c) { 694default: 695continue; 696case'/':case'\0': 697break; 698case'.': 699if(verify_dotfile(path)) 700continue; 701} 702return0; 703} 704 c = *path++; 705} 706} 707 708/* 709 * Do we have another file that has the beginning components being a 710 * proper superset of the name we're trying to add? 711 */ 712static inthas_file_name(struct index_state *istate, 713const struct cache_entry *ce,int pos,int ok_to_replace) 714{ 715int retval =0; 716int len =ce_namelen(ce); 717int stage =ce_stage(ce); 718const char*name = ce->name; 719 720while(pos < istate->cache_nr) { 721struct cache_entry *p = istate->cache[pos++]; 722 723if(len >=ce_namelen(p)) 724break; 725if(memcmp(name, p->name, len)) 726break; 727if(ce_stage(p) != stage) 728continue; 729if(p->name[len] !='/') 730continue; 731if(p->ce_flags & CE_REMOVE) 732continue; 733 retval = -1; 734if(!ok_to_replace) 735break; 736remove_index_entry_at(istate, --pos); 737} 738return retval; 739} 740 741/* 742 * Do we have another file with a pathname that is a proper 743 * subset of the name we're trying to add? 744 */ 745static inthas_dir_name(struct index_state *istate, 746const struct cache_entry *ce,int pos,int ok_to_replace) 747{ 748int retval =0; 749int stage =ce_stage(ce); 750const char*name = ce->name; 751const char*slash = name +ce_namelen(ce); 752 753for(;;) { 754int len; 755 756for(;;) { 757if(*--slash =='/') 758break; 759if(slash <= ce->name) 760return retval; 761} 762 len = slash - name; 763 764 pos =index_name_pos(istate, name,create_ce_flags(len, stage)); 765if(pos >=0) { 766/* 767 * Found one, but not so fast. This could 768 * be a marker that says "I was here, but 769 * I am being removed". Such an entry is 770 * not a part of the resulting tree, and 771 * it is Ok to have a directory at the same 772 * path. 773 */ 774if(!(istate->cache[pos]->ce_flags & CE_REMOVE)) { 775 retval = -1; 776if(!ok_to_replace) 777break; 778remove_index_entry_at(istate, pos); 779continue; 780} 781} 782else 783 pos = -pos-1; 784 785/* 786 * Trivial optimization: if we find an entry that 787 * already matches the sub-directory, then we know 788 * we're ok, and we can exit. 789 */ 790while(pos < istate->cache_nr) { 791struct cache_entry *p = istate->cache[pos]; 792if((ce_namelen(p) <= len) || 793(p->name[len] !='/') || 794memcmp(p->name, name, len)) 795break;/* not our subdirectory */ 796if(ce_stage(p) == stage && !(p->ce_flags & CE_REMOVE)) 797/* 798 * p is at the same stage as our entry, and 799 * is a subdirectory of what we are looking 800 * at, so we cannot have conflicts at our 801 * level or anything shorter. 802 */ 803return retval; 804 pos++; 805} 806} 807return retval; 808} 809 810/* We may be in a situation where we already have path/file and path 811 * is being added, or we already have path and path/file is being 812 * added. Either one would result in a nonsense tree that has path 813 * twice when git-write-tree tries to write it out. Prevent it. 814 * 815 * If ok-to-replace is specified, we remove the conflicting entries 816 * from the cache so the caller should recompute the insert position. 817 * When this happens, we return non-zero. 818 */ 819static intcheck_file_directory_conflict(struct index_state *istate, 820const struct cache_entry *ce, 821int pos,int ok_to_replace) 822{ 823int retval; 824 825/* 826 * When ce is an "I am going away" entry, we allow it to be added 827 */ 828if(ce->ce_flags & CE_REMOVE) 829return0; 830 831/* 832 * We check if the path is a sub-path of a subsequent pathname 833 * first, since removing those will not change the position 834 * in the array. 835 */ 836 retval =has_file_name(istate, ce, pos, ok_to_replace); 837 838/* 839 * Then check if the path might have a clashing sub-directory 840 * before it. 841 */ 842return retval +has_dir_name(istate, ce, pos, ok_to_replace); 843} 844 845static intadd_index_entry_with_check(struct index_state *istate,struct cache_entry *ce,int option) 846{ 847int pos; 848int ok_to_add = option & ADD_CACHE_OK_TO_ADD; 849int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE; 850int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK; 851 852cache_tree_invalidate_path(istate->cache_tree, ce->name); 853 pos =index_name_pos(istate, ce->name, ce->ce_flags); 854 855/* existing match? Just replace it. */ 856if(pos >=0) { 857replace_index_entry(istate, pos, ce); 858return0; 859} 860 pos = -pos-1; 861 862/* 863 * Inserting a merged entry ("stage 0") into the index 864 * will always replace all non-merged entries.. 865 */ 866if(pos < istate->cache_nr &&ce_stage(ce) ==0) { 867while(ce_same_name(istate->cache[pos], ce)) { 868 ok_to_add =1; 869if(!remove_index_entry_at(istate, pos)) 870break; 871} 872} 873 874if(!ok_to_add) 875return-1; 876if(!verify_path(ce->name)) 877return-1; 878 879if(!skip_df_check && 880check_file_directory_conflict(istate, ce, pos, ok_to_replace)) { 881if(!ok_to_replace) 882returnerror("'%s' appears as both a file and as a directory", 883 ce->name); 884 pos =index_name_pos(istate, ce->name, ce->ce_flags); 885 pos = -pos-1; 886} 887return pos +1; 888} 889 890intadd_index_entry(struct index_state *istate,struct cache_entry *ce,int option) 891{ 892int pos; 893 894if(option & ADD_CACHE_JUST_APPEND) 895 pos = istate->cache_nr; 896else{ 897int ret; 898 ret =add_index_entry_with_check(istate, ce, option); 899if(ret <=0) 900return ret; 901 pos = ret -1; 902} 903 904/* Make sure the array is big enough .. */ 905if(istate->cache_nr == istate->cache_alloc) { 906 istate->cache_alloc =alloc_nr(istate->cache_alloc); 907 istate->cache =xrealloc(istate->cache, 908 istate->cache_alloc *sizeof(struct cache_entry *)); 909} 910 911/* Add it in.. */ 912 istate->cache_nr++; 913if(istate->cache_nr > pos +1) 914memmove(istate->cache + pos +1, 915 istate->cache + pos, 916(istate->cache_nr - pos -1) *sizeof(ce)); 917set_index_entry(istate, pos, ce); 918 istate->cache_changed =1; 919return0; 920} 921 922/* 923 * "refresh" does not calculate a new sha1 file or bring the 924 * cache up-to-date for mode/content changes. But what it 925 * _does_ do is to "re-match" the stat information of a file 926 * with the cache, so that you can refresh the cache for a 927 * file that hasn't been changed but where the stat entry is 928 * out of date. 929 * 930 * For example, you'd want to do this after doing a "git-read-tree", 931 * to link up the stat cache details with the proper files. 932 */ 933static struct cache_entry *refresh_cache_ent(struct index_state *istate, 934struct cache_entry *ce, 935unsigned int options,int*err) 936{ 937struct stat st; 938struct cache_entry *updated; 939int changed, size; 940int ignore_valid = options & CE_MATCH_IGNORE_VALID; 941 942if(ce_uptodate(ce)) 943return ce; 944 945/* 946 * CE_VALID means the user promised us that the change to 947 * the work tree does not matter and told us not to worry. 948 */ 949if(!ignore_valid && (ce->ce_flags & CE_VALID)) { 950ce_mark_uptodate(ce); 951return ce; 952} 953 954if(lstat(ce->name, &st) <0) { 955if(err) 956*err = errno; 957return NULL; 958} 959 960 changed =ie_match_stat(istate, ce, &st, options); 961if(!changed) { 962/* 963 * The path is unchanged. If we were told to ignore 964 * valid bit, then we did the actual stat check and 965 * found that the entry is unmodified. If the entry 966 * is not marked VALID, this is the place to mark it 967 * valid again, under "assume unchanged" mode. 968 */ 969if(ignore_valid && assume_unchanged && 970!(ce->ce_flags & CE_VALID)) 971;/* mark this one VALID again */ 972else{ 973/* 974 * We do not mark the index itself "modified" 975 * because CE_UPTODATE flag is in-core only; 976 * we are not going to write this change out. 977 */ 978ce_mark_uptodate(ce); 979return ce; 980} 981} 982 983if(ie_modified(istate, ce, &st, options)) { 984if(err) 985*err = EINVAL; 986return NULL; 987} 988 989 size =ce_size(ce); 990 updated =xmalloc(size); 991memcpy(updated, ce, size); 992fill_stat_cache_info(updated, &st); 993/* 994 * If ignore_valid is not set, we should leave CE_VALID bit 995 * alone. Otherwise, paths marked with --no-assume-unchanged 996 * (i.e. things to be edited) will reacquire CE_VALID bit 997 * automatically, which is not really what we want. 998 */ 999if(!ignore_valid && assume_unchanged &&1000!(ce->ce_flags & CE_VALID))1001 updated->ce_flags &= ~CE_VALID;10021003return updated;1004}10051006intrefresh_index(struct index_state *istate,unsigned int flags,const char**pathspec,char*seen)1007{1008int i;1009int has_errors =0;1010int really = (flags & REFRESH_REALLY) !=0;1011int allow_unmerged = (flags & REFRESH_UNMERGED) !=0;1012int quiet = (flags & REFRESH_QUIET) !=0;1013int not_new = (flags & REFRESH_IGNORE_MISSING) !=0;1014int ignore_submodules = (flags & REFRESH_IGNORE_SUBMODULES) !=0;1015unsigned int options = really ? CE_MATCH_IGNORE_VALID :0;1016const char*needs_update_message;10171018 needs_update_message = ((flags & REFRESH_SAY_CHANGED)1019?"locally modified":"needs update");1020for(i =0; i < istate->cache_nr; i++) {1021struct cache_entry *ce, *new;1022int cache_errno =0;10231024 ce = istate->cache[i];1025if(ignore_submodules &&S_ISGITLINK(ce->ce_mode))1026continue;10271028if(ce_stage(ce)) {1029while((i < istate->cache_nr) &&1030!strcmp(istate->cache[i]->name, ce->name))1031 i++;1032 i--;1033if(allow_unmerged)1034continue;1035printf("%s: needs merge\n", ce->name);1036 has_errors =1;1037continue;1038}10391040if(pathspec && !match_pathspec(pathspec, ce->name,strlen(ce->name),0, seen))1041continue;10421043new=refresh_cache_ent(istate, ce, options, &cache_errno);1044if(new== ce)1045continue;1046if(!new) {1047if(not_new && cache_errno == ENOENT)1048continue;1049if(really && cache_errno == EINVAL) {1050/* If we are doing --really-refresh that1051 * means the index is not valid anymore.1052 */1053 ce->ce_flags &= ~CE_VALID;1054 istate->cache_changed =1;1055}1056if(quiet)1057continue;1058printf("%s:%s\n", ce->name, needs_update_message);1059 has_errors =1;1060continue;1061}10621063replace_index_entry(istate, i,new);1064}1065return has_errors;1066}10671068struct cache_entry *refresh_cache_entry(struct cache_entry *ce,int really)1069{1070returnrefresh_cache_ent(&the_index, ce, really, NULL);1071}10721073static intverify_hdr(struct cache_header *hdr,unsigned long size)1074{1075 SHA_CTX c;1076unsigned char sha1[20];10771078if(hdr->hdr_signature !=htonl(CACHE_SIGNATURE))1079returnerror("bad signature");1080if(hdr->hdr_version !=htonl(2))1081returnerror("bad index version");1082SHA1_Init(&c);1083SHA1_Update(&c, hdr, size -20);1084SHA1_Final(sha1, &c);1085if(hashcmp(sha1, (unsigned char*)hdr + size -20))1086returnerror("bad index file sha1 signature");1087return0;1088}10891090static intread_index_extension(struct index_state *istate,1091const char*ext,void*data,unsigned long sz)1092{1093switch(CACHE_EXT(ext)) {1094case CACHE_EXT_TREE:1095 istate->cache_tree =cache_tree_read(data, sz);1096break;1097default:1098if(*ext <'A'||'Z'< *ext)1099returnerror("index uses %.4s extension, which we do not understand",1100 ext);1101fprintf(stderr,"ignoring %.4s extension\n", ext);1102break;1103}1104return0;1105}11061107intread_index(struct index_state *istate)1108{1109returnread_index_from(istate,get_index_file());1110}11111112static voidconvert_from_disk(struct ondisk_cache_entry *ondisk,struct cache_entry *ce)1113{1114size_t len;11151116 ce->ce_ctime =ntohl(ondisk->ctime.sec);1117 ce->ce_mtime =ntohl(ondisk->mtime.sec);1118 ce->ce_dev =ntohl(ondisk->dev);1119 ce->ce_ino =ntohl(ondisk->ino);1120 ce->ce_mode =ntohl(ondisk->mode);1121 ce->ce_uid =ntohl(ondisk->uid);1122 ce->ce_gid =ntohl(ondisk->gid);1123 ce->ce_size =ntohl(ondisk->size);1124/* On-disk flags are just 16 bits */1125 ce->ce_flags =ntohs(ondisk->flags);11261127/* For future extension: we do not understand this entry yet */1128if(ce->ce_flags & CE_EXTENDED)1129die("Unknown index entry format");1130hashcpy(ce->sha1, ondisk->sha1);11311132 len = ce->ce_flags & CE_NAMEMASK;1133if(len == CE_NAMEMASK)1134 len =strlen(ondisk->name);1135/*1136 * NEEDSWORK: If the original index is crafted, this copy could1137 * go unchecked.1138 */1139memcpy(ce->name, ondisk->name, len +1);1140}11411142staticinlinesize_testimate_cache_size(size_t ondisk_size,unsigned int entries)1143{1144long per_entry;11451146 per_entry =sizeof(struct cache_entry) -sizeof(struct ondisk_cache_entry);11471148/*1149 * Alignment can cause differences. This should be "alignof", but1150 * since that's a gcc'ism, just use the size of a pointer.1151 */1152 per_entry +=sizeof(void*);1153return ondisk_size + entries*per_entry;1154}11551156/* remember to discard_cache() before reading a different cache! */1157intread_index_from(struct index_state *istate,const char*path)1158{1159int fd, i;1160struct stat st;1161unsigned long src_offset, dst_offset;1162struct cache_header *hdr;1163void*mmap;1164size_t mmap_size;11651166 errno = EBUSY;1167if(istate->initialized)1168return istate->cache_nr;11691170 errno = ENOENT;1171 istate->timestamp =0;1172 fd =open(path, O_RDONLY);1173if(fd <0) {1174if(errno == ENOENT)1175return0;1176die("index file open failed (%s)",strerror(errno));1177}11781179if(fstat(fd, &st))1180die("cannot stat the open index (%s)",strerror(errno));11811182 errno = EINVAL;1183 mmap_size =xsize_t(st.st_size);1184if(mmap_size <sizeof(struct cache_header) +20)1185die("index file smaller than expected");11861187 mmap =xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd,0);1188close(fd);1189if(mmap == MAP_FAILED)1190die("unable to map index file");11911192 hdr = mmap;1193if(verify_hdr(hdr, mmap_size) <0)1194goto unmap;11951196 istate->cache_nr =ntohl(hdr->hdr_entries);1197 istate->cache_alloc =alloc_nr(istate->cache_nr);1198 istate->cache =xcalloc(istate->cache_alloc,sizeof(struct cache_entry *));11991200/*1201 * The disk format is actually larger than the in-memory format,1202 * due to space for nsec etc, so even though the in-memory one1203 * has room for a few more flags, we can allocate using the same1204 * index size1205 */1206 istate->alloc =xmalloc(estimate_cache_size(mmap_size, istate->cache_nr));1207 istate->initialized =1;12081209 src_offset =sizeof(*hdr);1210 dst_offset =0;1211for(i =0; i < istate->cache_nr; i++) {1212struct ondisk_cache_entry *disk_ce;1213struct cache_entry *ce;12141215 disk_ce = (struct ondisk_cache_entry *)((char*)mmap + src_offset);1216 ce = (struct cache_entry *)((char*)istate->alloc + dst_offset);1217convert_from_disk(disk_ce, ce);1218set_index_entry(istate, i, ce);12191220 src_offset +=ondisk_ce_size(ce);1221 dst_offset +=ce_size(ce);1222}1223 istate->timestamp = st.st_mtime;1224while(src_offset <= mmap_size -20-8) {1225/* After an array of active_nr index entries,1226 * there can be arbitrary number of extended1227 * sections, each of which is prefixed with1228 * extension name (4-byte) and section length1229 * in 4-byte network byte order.1230 */1231unsigned long extsize;1232memcpy(&extsize, (char*)mmap + src_offset +4,4);1233 extsize =ntohl(extsize);1234if(read_index_extension(istate,1235(const char*) mmap + src_offset,1236(char*) mmap + src_offset +8,1237 extsize) <0)1238goto unmap;1239 src_offset +=8;1240 src_offset += extsize;1241}1242munmap(mmap, mmap_size);1243return istate->cache_nr;12441245unmap:1246munmap(mmap, mmap_size);1247 errno = EINVAL;1248die("index file corrupt");1249}12501251intdiscard_index(struct index_state *istate)1252{1253 istate->cache_nr =0;1254 istate->cache_changed =0;1255 istate->timestamp =0;1256 istate->name_hash_initialized =0;1257free_hash(&istate->name_hash);1258cache_tree_free(&(istate->cache_tree));1259free(istate->alloc);1260 istate->alloc = NULL;1261 istate->initialized =0;12621263/* no need to throw away allocated active_cache */1264return0;1265}12661267intunmerged_index(const struct index_state *istate)1268{1269int i;1270for(i =0; i < istate->cache_nr; i++) {1271if(ce_stage(istate->cache[i]))1272return1;1273}1274return0;1275}12761277#define WRITE_BUFFER_SIZE 81921278static unsigned char write_buffer[WRITE_BUFFER_SIZE];1279static unsigned long write_buffer_len;12801281static intce_write_flush(SHA_CTX *context,int fd)1282{1283unsigned int buffered = write_buffer_len;1284if(buffered) {1285SHA1_Update(context, write_buffer, buffered);1286if(write_in_full(fd, write_buffer, buffered) != buffered)1287return-1;1288 write_buffer_len =0;1289}1290return0;1291}12921293static intce_write(SHA_CTX *context,int fd,void*data,unsigned int len)1294{1295while(len) {1296unsigned int buffered = write_buffer_len;1297unsigned int partial = WRITE_BUFFER_SIZE - buffered;1298if(partial > len)1299 partial = len;1300memcpy(write_buffer + buffered, data, partial);1301 buffered += partial;1302if(buffered == WRITE_BUFFER_SIZE) {1303 write_buffer_len = buffered;1304if(ce_write_flush(context, fd))1305return-1;1306 buffered =0;1307}1308 write_buffer_len = buffered;1309 len -= partial;1310 data = (char*) data + partial;1311}1312return0;1313}13141315static intwrite_index_ext_header(SHA_CTX *context,int fd,1316unsigned int ext,unsigned int sz)1317{1318 ext =htonl(ext);1319 sz =htonl(sz);1320return((ce_write(context, fd, &ext,4) <0) ||1321(ce_write(context, fd, &sz,4) <0)) ? -1:0;1322}13231324static intce_flush(SHA_CTX *context,int fd)1325{1326unsigned int left = write_buffer_len;13271328if(left) {1329 write_buffer_len =0;1330SHA1_Update(context, write_buffer, left);1331}13321333/* Flush first if not enough space for SHA1 signature */1334if(left +20> WRITE_BUFFER_SIZE) {1335if(write_in_full(fd, write_buffer, left) != left)1336return-1;1337 left =0;1338}13391340/* Append the SHA1 signature at the end */1341SHA1_Final(write_buffer + left, context);1342 left +=20;1343return(write_in_full(fd, write_buffer, left) != left) ? -1:0;1344}13451346static voidce_smudge_racily_clean_entry(struct cache_entry *ce)1347{1348/*1349 * The only thing we care about in this function is to smudge the1350 * falsely clean entry due to touch-update-touch race, so we leave1351 * everything else as they are. We are called for entries whose1352 * ce_mtime match the index file mtime.1353 *1354 * Note that this actually does not do much for gitlinks, for1355 * which ce_match_stat_basic() always goes to the actual1356 * contents. The caller checks with is_racy_timestamp() which1357 * always says "no" for gitlinks, so we are not called for them ;-)1358 */1359struct stat st;13601361if(lstat(ce->name, &st) <0)1362return;1363if(ce_match_stat_basic(ce, &st))1364return;1365if(ce_modified_check_fs(ce, &st)) {1366/* This is "racily clean"; smudge it. Note that this1367 * is a tricky code. At first glance, it may appear1368 * that it can break with this sequence:1369 *1370 * $ echo xyzzy >frotz1371 * $ git-update-index --add frotz1372 * $ : >frotz1373 * $ sleep 31374 * $ echo filfre >nitfol1375 * $ git-update-index --add nitfol1376 *1377 * but it does not. When the second update-index runs,1378 * it notices that the entry "frotz" has the same timestamp1379 * as index, and if we were to smudge it by resetting its1380 * size to zero here, then the object name recorded1381 * in index is the 6-byte file but the cached stat information1382 * becomes zero --- which would then match what we would1383 * obtain from the filesystem next time we stat("frotz").1384 *1385 * However, the second update-index, before calling1386 * this function, notices that the cached size is 61387 * bytes and what is on the filesystem is an empty1388 * file, and never calls us, so the cached size information1389 * for "frotz" stays 6 which does not match the filesystem.1390 */1391 ce->ce_size =0;1392}1393}13941395static intce_write_entry(SHA_CTX *c,int fd,struct cache_entry *ce)1396{1397int size =ondisk_ce_size(ce);1398struct ondisk_cache_entry *ondisk =xcalloc(1, size);13991400 ondisk->ctime.sec =htonl(ce->ce_ctime);1401 ondisk->ctime.nsec =0;1402 ondisk->mtime.sec =htonl(ce->ce_mtime);1403 ondisk->mtime.nsec =0;1404 ondisk->dev =htonl(ce->ce_dev);1405 ondisk->ino =htonl(ce->ce_ino);1406 ondisk->mode =htonl(ce->ce_mode);1407 ondisk->uid =htonl(ce->ce_uid);1408 ondisk->gid =htonl(ce->ce_gid);1409 ondisk->size =htonl(ce->ce_size);1410hashcpy(ondisk->sha1, ce->sha1);1411 ondisk->flags =htons(ce->ce_flags);1412memcpy(ondisk->name, ce->name,ce_namelen(ce));14131414returnce_write(c, fd, ondisk, size);1415}14161417intwrite_index(const struct index_state *istate,int newfd)1418{1419 SHA_CTX c;1420struct cache_header hdr;1421int i, err, removed;1422struct cache_entry **cache = istate->cache;1423int entries = istate->cache_nr;14241425for(i = removed =0; i < entries; i++)1426if(cache[i]->ce_flags & CE_REMOVE)1427 removed++;14281429 hdr.hdr_signature =htonl(CACHE_SIGNATURE);1430 hdr.hdr_version =htonl(2);1431 hdr.hdr_entries =htonl(entries - removed);14321433SHA1_Init(&c);1434if(ce_write(&c, newfd, &hdr,sizeof(hdr)) <0)1435return-1;14361437for(i =0; i < entries; i++) {1438struct cache_entry *ce = cache[i];1439if(ce->ce_flags & CE_REMOVE)1440continue;1441if(!ce_uptodate(ce) &&is_racy_timestamp(istate, ce))1442ce_smudge_racily_clean_entry(ce);1443if(ce_write_entry(&c, newfd, ce) <0)1444return-1;1445}14461447/* Write extension data here */1448if(istate->cache_tree) {1449struct strbuf sb;14501451strbuf_init(&sb,0);1452cache_tree_write(&sb, istate->cache_tree);1453 err =write_index_ext_header(&c, newfd, CACHE_EXT_TREE, sb.len) <01454||ce_write(&c, newfd, sb.buf, sb.len) <0;1455strbuf_release(&sb);1456if(err)1457return-1;1458}1459returnce_flush(&c, newfd);1460}14611462/*1463 * Read the index file that is potentially unmerged into given1464 * index_state, dropping any unmerged entries. Returns true is1465 * the index is unmerged. Callers who want to refuse to work1466 * from an unmerged state can call this and check its return value,1467 * instead of calling read_cache().1468 */1469intread_index_unmerged(struct index_state *istate)1470{1471int i;1472struct cache_entry **dst;1473struct cache_entry *last = NULL;14741475read_index(istate);1476 dst = istate->cache;1477for(i =0; i < istate->cache_nr; i++) {1478struct cache_entry *ce = istate->cache[i];1479if(ce_stage(ce)) {1480remove_name_hash(ce);1481if(last && !strcmp(ce->name, last->name))1482continue;1483cache_tree_invalidate_path(istate->cache_tree, ce->name);1484 last = ce;1485continue;1486}1487*dst++ = ce;1488}1489 istate->cache_nr = dst - istate->cache;1490return!!last;1491}14921493struct update_callback_data1494{1495int flags;1496int add_errors;1497};14981499static voidupdate_callback(struct diff_queue_struct *q,1500struct diff_options *opt,void*cbdata)1501{1502int i;1503struct update_callback_data *data = cbdata;15041505for(i =0; i < q->nr; i++) {1506struct diff_filepair *p = q->queue[i];1507const char*path = p->one->path;1508switch(p->status) {1509default:1510die("unexpected diff status%c", p->status);1511case DIFF_STATUS_UNMERGED:1512case DIFF_STATUS_MODIFIED:1513case DIFF_STATUS_TYPE_CHANGED:1514if(add_file_to_index(&the_index, path, data->flags)) {1515if(!(data->flags & ADD_CACHE_IGNORE_ERRORS))1516die("updating files failed");1517 data->add_errors++;1518}1519break;1520case DIFF_STATUS_DELETED:1521if(data->flags & ADD_CACHE_IGNORE_REMOVAL)1522break;1523if(!(data->flags & ADD_CACHE_PRETEND))1524remove_file_from_index(&the_index, path);1525if(data->flags & (ADD_CACHE_PRETEND|ADD_CACHE_VERBOSE))1526printf("remove '%s'\n", path);1527break;1528}1529}1530}15311532intadd_files_to_cache(const char*prefix,const char**pathspec,int flags)1533{1534struct update_callback_data data;1535struct rev_info rev;1536init_revisions(&rev, prefix);1537setup_revisions(0, NULL, &rev, NULL);1538 rev.prune_data = pathspec;1539 rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;1540 rev.diffopt.format_callback = update_callback;1541 data.flags = flags;1542 data.add_errors =0;1543 rev.diffopt.format_callback_data = &data;1544run_diff_files(&rev, DIFF_RACY_IS_MODIFIED);1545return!!data.add_errors;1546}1547