1/* 2 * This handles recursive filename detection with exclude 3 * files, index knowledge etc.. 4 * 5 * See Documentation/technical/api-directory-listing.txt 6 * 7 * Copyright (C) Linus Torvalds, 2005-2006 8 * Junio Hamano, 2005-2006 9 */ 10#include"cache.h" 11#include"dir.h" 12#include"refs.h" 13#include"wildmatch.h" 14#include"pathspec.h" 15#include"varint.h" 16#include"ewah/ewok.h" 17 18struct path_simplify { 19int len; 20const char*path; 21}; 22 23/* 24 * Tells read_directory_recursive how a file or directory should be treated. 25 * Values are ordered by significance, e.g. if a directory contains both 26 * excluded and untracked files, it is listed as untracked because 27 * path_untracked > path_excluded. 28 */ 29enum path_treatment { 30 path_none =0, 31 path_recurse, 32 path_excluded, 33 path_untracked 34}; 35 36/* 37 * Support data structure for our opendir/readdir/closedir wrappers 38 */ 39struct cached_dir { 40DIR*fdir; 41struct untracked_cache_dir *untracked; 42int nr_files; 43int nr_dirs; 44 45struct dirent *de; 46const char*file; 47struct untracked_cache_dir *ucd; 48}; 49 50static enum path_treatment read_directory_recursive(struct dir_struct *dir, 51const char*path,int len,struct untracked_cache_dir *untracked, 52int check_only,const struct path_simplify *simplify); 53static intget_dtype(struct dirent *de,const char*path,int len); 54 55/* helper string functions with support for the ignore_case flag */ 56intstrcmp_icase(const char*a,const char*b) 57{ 58return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 59} 60 61intstrncmp_icase(const char*a,const char*b,size_t count) 62{ 63return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 64} 65 66intfnmatch_icase(const char*pattern,const char*string,int flags) 67{ 68returnwildmatch(pattern, string, 69 flags | (ignore_case ? WM_CASEFOLD :0), 70 NULL); 71} 72 73intgit_fnmatch(const struct pathspec_item *item, 74const char*pattern,const char*string, 75int prefix) 76{ 77if(prefix >0) { 78if(ps_strncmp(item, pattern, string, prefix)) 79return WM_NOMATCH; 80 pattern += prefix; 81 string += prefix; 82} 83if(item->flags & PATHSPEC_ONESTAR) { 84int pattern_len =strlen(++pattern); 85int string_len =strlen(string); 86return string_len < pattern_len || 87ps_strcmp(item, pattern, 88 string + string_len - pattern_len); 89} 90if(item->magic & PATHSPEC_GLOB) 91returnwildmatch(pattern, string, 92 WM_PATHNAME | 93(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0), 94 NULL); 95else 96/* wildmatch has not learned no FNM_PATHNAME mode yet */ 97returnwildmatch(pattern, string, 98 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0, 99 NULL); 100} 101 102static intfnmatch_icase_mem(const char*pattern,int patternlen, 103const char*string,int stringlen, 104int flags) 105{ 106int match_status; 107struct strbuf pat_buf = STRBUF_INIT; 108struct strbuf str_buf = STRBUF_INIT; 109const char*use_pat = pattern; 110const char*use_str = string; 111 112if(pattern[patternlen]) { 113strbuf_add(&pat_buf, pattern, patternlen); 114 use_pat = pat_buf.buf; 115} 116if(string[stringlen]) { 117strbuf_add(&str_buf, string, stringlen); 118 use_str = str_buf.buf; 119} 120 121if(ignore_case) 122 flags |= WM_CASEFOLD; 123 match_status =wildmatch(use_pat, use_str, flags, NULL); 124 125strbuf_release(&pat_buf); 126strbuf_release(&str_buf); 127 128return match_status; 129} 130 131static size_tcommon_prefix_len(const struct pathspec *pathspec) 132{ 133int n; 134size_t max =0; 135 136/* 137 * ":(icase)path" is treated as a pathspec full of 138 * wildcard. In other words, only prefix is considered common 139 * prefix. If the pathspec is abc/foo abc/bar, running in 140 * subdir xyz, the common prefix is still xyz, not xuz/abc as 141 * in non-:(icase). 142 */ 143GUARD_PATHSPEC(pathspec, 144 PATHSPEC_FROMTOP | 145 PATHSPEC_MAXDEPTH | 146 PATHSPEC_LITERAL | 147 PATHSPEC_GLOB | 148 PATHSPEC_ICASE | 149 PATHSPEC_EXCLUDE); 150 151for(n =0; n < pathspec->nr; n++) { 152size_t i =0, len =0, item_len; 153if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 154continue; 155if(pathspec->items[n].magic & PATHSPEC_ICASE) 156 item_len = pathspec->items[n].prefix; 157else 158 item_len = pathspec->items[n].nowildcard_len; 159while(i < item_len && (n ==0|| i < max)) { 160char c = pathspec->items[n].match[i]; 161if(c != pathspec->items[0].match[i]) 162break; 163if(c =='/') 164 len = i +1; 165 i++; 166} 167if(n ==0|| len < max) { 168 max = len; 169if(!max) 170break; 171} 172} 173return max; 174} 175 176/* 177 * Returns a copy of the longest leading path common among all 178 * pathspecs. 179 */ 180char*common_prefix(const struct pathspec *pathspec) 181{ 182unsigned long len =common_prefix_len(pathspec); 183 184return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 185} 186 187intfill_directory(struct dir_struct *dir,const struct pathspec *pathspec) 188{ 189size_t len; 190 191/* 192 * Calculate common prefix for the pathspec, and 193 * use that to optimize the directory walk 194 */ 195 len =common_prefix_len(pathspec); 196 197/* Read the directory and prune it */ 198read_directory(dir, pathspec->nr ? pathspec->_raw[0] :"", len, pathspec); 199return len; 200} 201 202intwithin_depth(const char*name,int namelen, 203int depth,int max_depth) 204{ 205const char*cp = name, *cpe = name + namelen; 206 207while(cp < cpe) { 208if(*cp++ !='/') 209continue; 210 depth++; 211if(depth > max_depth) 212return0; 213} 214return1; 215} 216 217#define DO_MATCH_EXCLUDE 1 218#define DO_MATCH_DIRECTORY 2 219 220/* 221 * Does 'match' match the given name? 222 * A match is found if 223 * 224 * (1) the 'match' string is leading directory of 'name', or 225 * (2) the 'match' string is a wildcard and matches 'name', or 226 * (3) the 'match' string is exactly the same as 'name'. 227 * 228 * and the return value tells which case it was. 229 * 230 * It returns 0 when there is no match. 231 */ 232static intmatch_pathspec_item(const struct pathspec_item *item,int prefix, 233const char*name,int namelen,unsigned flags) 234{ 235/* name/namelen has prefix cut off by caller */ 236const char*match = item->match + prefix; 237int matchlen = item->len - prefix; 238 239/* 240 * The normal call pattern is: 241 * 1. prefix = common_prefix_len(ps); 242 * 2. prune something, or fill_directory 243 * 3. match_pathspec() 244 * 245 * 'prefix' at #1 may be shorter than the command's prefix and 246 * it's ok for #2 to match extra files. Those extras will be 247 * trimmed at #3. 248 * 249 * Suppose the pathspec is 'foo' and '../bar' running from 250 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 251 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 252 * user does not want XYZ/foo, only the "foo" part should be 253 * case-insensitive. We need to filter out XYZ/foo here. In 254 * other words, we do not trust the caller on comparing the 255 * prefix part when :(icase) is involved. We do exact 256 * comparison ourselves. 257 * 258 * Normally the caller (common_prefix_len() in fact) does 259 * _exact_ matching on name[-prefix+1..-1] and we do not need 260 * to check that part. Be defensive and check it anyway, in 261 * case common_prefix_len is changed, or a new caller is 262 * introduced that does not use common_prefix_len. 263 * 264 * If the penalty turns out too high when prefix is really 265 * long, maybe change it to 266 * strncmp(match, name, item->prefix - prefix) 267 */ 268if(item->prefix && (item->magic & PATHSPEC_ICASE) && 269strncmp(item->match, name - prefix, item->prefix)) 270return0; 271 272/* If the match was just the prefix, we matched */ 273if(!*match) 274return MATCHED_RECURSIVELY; 275 276if(matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 277if(matchlen == namelen) 278return MATCHED_EXACTLY; 279 280if(match[matchlen-1] =='/'|| name[matchlen] =='/') 281return MATCHED_RECURSIVELY; 282}else if((flags & DO_MATCH_DIRECTORY) && 283 match[matchlen -1] =='/'&& 284 namelen == matchlen -1&& 285!ps_strncmp(item, match, name, namelen)) 286return MATCHED_EXACTLY; 287 288if(item->nowildcard_len < item->len && 289!git_fnmatch(item, match, name, 290 item->nowildcard_len - prefix)) 291return MATCHED_FNMATCH; 292 293return0; 294} 295 296/* 297 * Given a name and a list of pathspecs, returns the nature of the 298 * closest (i.e. most specific) match of the name to any of the 299 * pathspecs. 300 * 301 * The caller typically calls this multiple times with the same 302 * pathspec and seen[] array but with different name/namelen 303 * (e.g. entries from the index) and is interested in seeing if and 304 * how each pathspec matches all the names it calls this function 305 * with. A mark is left in the seen[] array for each pathspec element 306 * indicating the closest type of match that element achieved, so if 307 * seen[n] remains zero after multiple invocations, that means the nth 308 * pathspec did not match any names, which could indicate that the 309 * user mistyped the nth pathspec. 310 */ 311static intdo_match_pathspec(const struct pathspec *ps, 312const char*name,int namelen, 313int prefix,char*seen, 314unsigned flags) 315{ 316int i, retval =0, exclude = flags & DO_MATCH_EXCLUDE; 317 318GUARD_PATHSPEC(ps, 319 PATHSPEC_FROMTOP | 320 PATHSPEC_MAXDEPTH | 321 PATHSPEC_LITERAL | 322 PATHSPEC_GLOB | 323 PATHSPEC_ICASE | 324 PATHSPEC_EXCLUDE); 325 326if(!ps->nr) { 327if(!ps->recursive || 328!(ps->magic & PATHSPEC_MAXDEPTH) || 329 ps->max_depth == -1) 330return MATCHED_RECURSIVELY; 331 332if(within_depth(name, namelen,0, ps->max_depth)) 333return MATCHED_EXACTLY; 334else 335return0; 336} 337 338 name += prefix; 339 namelen -= prefix; 340 341for(i = ps->nr -1; i >=0; i--) { 342int how; 343 344if((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 345( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 346continue; 347 348if(seen && seen[i] == MATCHED_EXACTLY) 349continue; 350/* 351 * Make exclude patterns optional and never report 352 * "pathspec ':(exclude)foo' matches no files" 353 */ 354if(seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 355 seen[i] = MATCHED_FNMATCH; 356 how =match_pathspec_item(ps->items+i, prefix, name, 357 namelen, flags); 358if(ps->recursive && 359(ps->magic & PATHSPEC_MAXDEPTH) && 360 ps->max_depth != -1&& 361 how && how != MATCHED_FNMATCH) { 362int len = ps->items[i].len; 363if(name[len] =='/') 364 len++; 365if(within_depth(name+len, namelen-len,0, ps->max_depth)) 366 how = MATCHED_EXACTLY; 367else 368 how =0; 369} 370if(how) { 371if(retval < how) 372 retval = how; 373if(seen && seen[i] < how) 374 seen[i] = how; 375} 376} 377return retval; 378} 379 380intmatch_pathspec(const struct pathspec *ps, 381const char*name,int namelen, 382int prefix,char*seen,int is_dir) 383{ 384int positive, negative; 385unsigned flags = is_dir ? DO_MATCH_DIRECTORY :0; 386 positive =do_match_pathspec(ps, name, namelen, 387 prefix, seen, flags); 388if(!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 389return positive; 390 negative =do_match_pathspec(ps, name, namelen, 391 prefix, seen, 392 flags | DO_MATCH_EXCLUDE); 393return negative ?0: positive; 394} 395 396/* 397 * Return the length of the "simple" part of a path match limiter. 398 */ 399intsimple_length(const char*match) 400{ 401int len = -1; 402 403for(;;) { 404unsigned char c = *match++; 405 len++; 406if(c =='\0'||is_glob_special(c)) 407return len; 408} 409} 410 411intno_wildcard(const char*string) 412{ 413return string[simple_length(string)] =='\0'; 414} 415 416voidparse_exclude_pattern(const char**pattern, 417int*patternlen, 418int*flags, 419int*nowildcardlen) 420{ 421const char*p = *pattern; 422size_t i, len; 423 424*flags =0; 425if(*p =='!') { 426*flags |= EXC_FLAG_NEGATIVE; 427 p++; 428} 429 len =strlen(p); 430if(len && p[len -1] =='/') { 431 len--; 432*flags |= EXC_FLAG_MUSTBEDIR; 433} 434for(i =0; i < len; i++) { 435if(p[i] =='/') 436break; 437} 438if(i == len) 439*flags |= EXC_FLAG_NODIR; 440*nowildcardlen =simple_length(p); 441/* 442 * we should have excluded the trailing slash from 'p' too, 443 * but that's one more allocation. Instead just make sure 444 * nowildcardlen does not exceed real patternlen 445 */ 446if(*nowildcardlen > len) 447*nowildcardlen = len; 448if(*p =='*'&&no_wildcard(p +1)) 449*flags |= EXC_FLAG_ENDSWITH; 450*pattern = p; 451*patternlen = len; 452} 453 454voidadd_exclude(const char*string,const char*base, 455int baselen,struct exclude_list *el,int srcpos) 456{ 457struct exclude *x; 458int patternlen; 459int flags; 460int nowildcardlen; 461 462parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 463if(flags & EXC_FLAG_MUSTBEDIR) { 464char*s; 465 x =xmalloc(sizeof(*x) + patternlen +1); 466 s = (char*)(x+1); 467memcpy(s, string, patternlen); 468 s[patternlen] ='\0'; 469 x->pattern = s; 470}else{ 471 x =xmalloc(sizeof(*x)); 472 x->pattern = string; 473} 474 x->patternlen = patternlen; 475 x->nowildcardlen = nowildcardlen; 476 x->base = base; 477 x->baselen = baselen; 478 x->flags = flags; 479 x->srcpos = srcpos; 480ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 481 el->excludes[el->nr++] = x; 482 x->el = el; 483} 484 485static void*read_skip_worktree_file_from_index(const char*path,size_t*size, 486struct sha1_stat *sha1_stat) 487{ 488int pos, len; 489unsigned long sz; 490enum object_type type; 491void*data; 492 493 len =strlen(path); 494 pos =cache_name_pos(path, len); 495if(pos <0) 496return NULL; 497if(!ce_skip_worktree(active_cache[pos])) 498return NULL; 499 data =read_sha1_file(active_cache[pos]->sha1, &type, &sz); 500if(!data || type != OBJ_BLOB) { 501free(data); 502return NULL; 503} 504*size =xsize_t(sz); 505if(sha1_stat) { 506memset(&sha1_stat->stat,0,sizeof(sha1_stat->stat)); 507hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); 508} 509return data; 510} 511 512/* 513 * Frees memory within el which was allocated for exclude patterns and 514 * the file buffer. Does not free el itself. 515 */ 516voidclear_exclude_list(struct exclude_list *el) 517{ 518int i; 519 520for(i =0; i < el->nr; i++) 521free(el->excludes[i]); 522free(el->excludes); 523free(el->filebuf); 524 525 el->nr =0; 526 el->excludes = NULL; 527 el->filebuf = NULL; 528} 529 530static voidtrim_trailing_spaces(char*buf) 531{ 532char*p, *last_space = NULL; 533 534for(p = buf; *p; p++) 535switch(*p) { 536case' ': 537if(!last_space) 538 last_space = p; 539break; 540case'\\': 541 p++; 542if(!*p) 543return; 544/* fallthrough */ 545default: 546 last_space = NULL; 547} 548 549if(last_space) 550*last_space ='\0'; 551} 552 553/* 554 * Given a subdirectory name and "dir" of the current directory, 555 * search the subdir in "dir" and return it, or create a new one if it 556 * does not exist in "dir". 557 * 558 * If "name" has the trailing slash, it'll be excluded in the search. 559 */ 560static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, 561struct untracked_cache_dir *dir, 562const char*name,int len) 563{ 564int first, last; 565struct untracked_cache_dir *d; 566if(!dir) 567return NULL; 568if(len && name[len -1] =='/') 569 len--; 570 first =0; 571 last = dir->dirs_nr; 572while(last > first) { 573int cmp, next = (last + first) >>1; 574 d = dir->dirs[next]; 575 cmp =strncmp(name, d->name, len); 576if(!cmp &&strlen(d->name) > len) 577 cmp = -1; 578if(!cmp) 579return d; 580if(cmp <0) { 581 last = next; 582continue; 583} 584 first = next+1; 585} 586 587 uc->dir_created++; 588 d =xmalloc(sizeof(*d) + len +1); 589memset(d,0,sizeof(*d)); 590memcpy(d->name, name, len); 591 d->name[len] ='\0'; 592 593ALLOC_GROW(dir->dirs, dir->dirs_nr +1, dir->dirs_alloc); 594memmove(dir->dirs + first +1, dir->dirs + first, 595(dir->dirs_nr - first) *sizeof(*dir->dirs)); 596 dir->dirs_nr++; 597 dir->dirs[first] = d; 598return d; 599} 600 601static voiddo_invalidate_gitignore(struct untracked_cache_dir *dir) 602{ 603int i; 604 dir->valid =0; 605 dir->untracked_nr =0; 606for(i =0; i < dir->dirs_nr; i++) 607do_invalidate_gitignore(dir->dirs[i]); 608} 609 610static voidinvalidate_gitignore(struct untracked_cache *uc, 611struct untracked_cache_dir *dir) 612{ 613 uc->gitignore_invalidated++; 614do_invalidate_gitignore(dir); 615} 616 617static voidinvalidate_directory(struct untracked_cache *uc, 618struct untracked_cache_dir *dir) 619{ 620int i; 621 uc->dir_invalidated++; 622 dir->valid =0; 623 dir->untracked_nr =0; 624for(i =0; i < dir->dirs_nr; i++) 625 dir->dirs[i]->recurse =0; 626} 627 628/* 629 * Given a file with name "fname", read it (either from disk, or from 630 * the index if "check_index" is non-zero), parse it and store the 631 * exclude rules in "el". 632 * 633 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill 634 * stat data from disk (only valid if add_excludes returns zero). If 635 * ss_valid is non-zero, "ss" must contain good value as input. 636 */ 637static intadd_excludes(const char*fname,const char*base,int baselen, 638struct exclude_list *el,int check_index, 639struct sha1_stat *sha1_stat) 640{ 641struct stat st; 642int fd, i, lineno =1; 643size_t size =0; 644char*buf, *entry; 645 646 fd =open(fname, O_RDONLY); 647if(fd <0||fstat(fd, &st) <0) { 648if(errno != ENOENT) 649warn_on_inaccessible(fname); 650if(0<= fd) 651close(fd); 652if(!check_index || 653(buf =read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL) 654return-1; 655if(size ==0) { 656free(buf); 657return0; 658} 659if(buf[size-1] !='\n') { 660 buf =xrealloc(buf, size+1); 661 buf[size++] ='\n'; 662} 663}else{ 664 size =xsize_t(st.st_size); 665if(size ==0) { 666if(sha1_stat) { 667fill_stat_data(&sha1_stat->stat, &st); 668hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN); 669 sha1_stat->valid =1; 670} 671close(fd); 672return0; 673} 674 buf =xmalloc(size+1); 675if(read_in_full(fd, buf, size) != size) { 676free(buf); 677close(fd); 678return-1; 679} 680 buf[size++] ='\n'; 681close(fd); 682if(sha1_stat) { 683int pos; 684if(sha1_stat->valid && 685!match_stat_data_racy(&the_index, &sha1_stat->stat, &st)) 686;/* no content change, ss->sha1 still good */ 687else if(check_index && 688(pos =cache_name_pos(fname,strlen(fname))) >=0&& 689!ce_stage(active_cache[pos]) && 690ce_uptodate(active_cache[pos]) && 691!would_convert_to_git(fname)) 692hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); 693else 694hash_sha1_file(buf, size,"blob", sha1_stat->sha1); 695fill_stat_data(&sha1_stat->stat, &st); 696 sha1_stat->valid =1; 697} 698} 699 700 el->filebuf = buf; 701 entry = buf; 702for(i =0; i < size; i++) { 703if(buf[i] =='\n') { 704if(entry != buf + i && entry[0] !='#') { 705 buf[i - (i && buf[i-1] =='\r')] =0; 706trim_trailing_spaces(entry); 707add_exclude(entry, base, baselen, el, lineno); 708} 709 lineno++; 710 entry = buf + i +1; 711} 712} 713return0; 714} 715 716intadd_excludes_from_file_to_list(const char*fname,const char*base, 717int baselen,struct exclude_list *el, 718int check_index) 719{ 720returnadd_excludes(fname, base, baselen, el, check_index, NULL); 721} 722 723struct exclude_list *add_exclude_list(struct dir_struct *dir, 724int group_type,const char*src) 725{ 726struct exclude_list *el; 727struct exclude_list_group *group; 728 729 group = &dir->exclude_list_group[group_type]; 730ALLOC_GROW(group->el, group->nr +1, group->alloc); 731 el = &group->el[group->nr++]; 732memset(el,0,sizeof(*el)); 733 el->src = src; 734return el; 735} 736 737/* 738 * Used to set up core.excludesfile and .git/info/exclude lists. 739 */ 740static voidadd_excludes_from_file_1(struct dir_struct *dir,const char*fname, 741struct sha1_stat *sha1_stat) 742{ 743struct exclude_list *el; 744/* 745 * catch setup_standard_excludes() that's called before 746 * dir->untracked is assigned. That function behaves 747 * differently when dir->untracked is non-NULL. 748 */ 749if(!dir->untracked) 750 dir->unmanaged_exclude_files++; 751 el =add_exclude_list(dir, EXC_FILE, fname); 752if(add_excludes(fname,"",0, el,0, sha1_stat) <0) 753die("cannot use%sas an exclude file", fname); 754} 755 756voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 757{ 758 dir->unmanaged_exclude_files++;/* see validate_untracked_cache() */ 759add_excludes_from_file_1(dir, fname, NULL); 760} 761 762intmatch_basename(const char*basename,int basenamelen, 763const char*pattern,int prefix,int patternlen, 764int flags) 765{ 766if(prefix == patternlen) { 767if(patternlen == basenamelen && 768!strncmp_icase(pattern, basename, basenamelen)) 769return1; 770}else if(flags & EXC_FLAG_ENDSWITH) { 771/* "*literal" matching against "fooliteral" */ 772if(patternlen -1<= basenamelen && 773!strncmp_icase(pattern +1, 774 basename + basenamelen - (patternlen -1), 775 patternlen -1)) 776return1; 777}else{ 778if(fnmatch_icase_mem(pattern, patternlen, 779 basename, basenamelen, 7800) ==0) 781return1; 782} 783return0; 784} 785 786intmatch_pathname(const char*pathname,int pathlen, 787const char*base,int baselen, 788const char*pattern,int prefix,int patternlen, 789int flags) 790{ 791const char*name; 792int namelen; 793 794/* 795 * match with FNM_PATHNAME; the pattern has base implicitly 796 * in front of it. 797 */ 798if(*pattern =='/') { 799 pattern++; 800 patternlen--; 801 prefix--; 802} 803 804/* 805 * baselen does not count the trailing slash. base[] may or 806 * may not end with a trailing slash though. 807 */ 808if(pathlen < baselen +1|| 809(baselen && pathname[baselen] !='/') || 810strncmp_icase(pathname, base, baselen)) 811return0; 812 813 namelen = baselen ? pathlen - baselen -1: pathlen; 814 name = pathname + pathlen - namelen; 815 816if(prefix) { 817/* 818 * if the non-wildcard part is longer than the 819 * remaining pathname, surely it cannot match. 820 */ 821if(prefix > namelen) 822return0; 823 824if(strncmp_icase(pattern, name, prefix)) 825return0; 826 pattern += prefix; 827 patternlen -= prefix; 828 name += prefix; 829 namelen -= prefix; 830 831/* 832 * If the whole pattern did not have a wildcard, 833 * then our prefix match is all we need; we 834 * do not need to call fnmatch at all. 835 */ 836if(!patternlen && !namelen) 837return1; 838} 839 840returnfnmatch_icase_mem(pattern, patternlen, 841 name, namelen, 842 WM_PATHNAME) ==0; 843} 844 845/* 846 * Scan the given exclude list in reverse to see whether pathname 847 * should be ignored. The first match (i.e. the last on the list), if 848 * any, determines the fate. Returns the exclude_list element which 849 * matched, or NULL for undecided. 850 */ 851static struct exclude *last_exclude_matching_from_list(const char*pathname, 852int pathlen, 853const char*basename, 854int*dtype, 855struct exclude_list *el) 856{ 857int i; 858 859if(!el->nr) 860return NULL;/* undefined */ 861 862for(i = el->nr -1;0<= i; i--) { 863struct exclude *x = el->excludes[i]; 864const char*exclude = x->pattern; 865int prefix = x->nowildcardlen; 866 867if(x->flags & EXC_FLAG_MUSTBEDIR) { 868if(*dtype == DT_UNKNOWN) 869*dtype =get_dtype(NULL, pathname, pathlen); 870if(*dtype != DT_DIR) 871continue; 872} 873 874if(x->flags & EXC_FLAG_NODIR) { 875if(match_basename(basename, 876 pathlen - (basename - pathname), 877 exclude, prefix, x->patternlen, 878 x->flags)) 879return x; 880continue; 881} 882 883assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 884if(match_pathname(pathname, pathlen, 885 x->base, x->baselen ? x->baselen -1:0, 886 exclude, prefix, x->patternlen, x->flags)) 887return x; 888} 889return NULL;/* undecided */ 890} 891 892/* 893 * Scan the list and let the last match determine the fate. 894 * Return 1 for exclude, 0 for include and -1 for undecided. 895 */ 896intis_excluded_from_list(const char*pathname, 897int pathlen,const char*basename,int*dtype, 898struct exclude_list *el) 899{ 900struct exclude *exclude; 901 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 902if(exclude) 903return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 904return-1;/* undecided */ 905} 906 907static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 908const char*pathname,int pathlen,const char*basename, 909int*dtype_p) 910{ 911int i, j; 912struct exclude_list_group *group; 913struct exclude *exclude; 914for(i = EXC_CMDL; i <= EXC_FILE; i++) { 915 group = &dir->exclude_list_group[i]; 916for(j = group->nr -1; j >=0; j--) { 917 exclude =last_exclude_matching_from_list( 918 pathname, pathlen, basename, dtype_p, 919&group->el[j]); 920if(exclude) 921return exclude; 922} 923} 924return NULL; 925} 926 927/* 928 * Loads the per-directory exclude list for the substring of base 929 * which has a char length of baselen. 930 */ 931static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 932{ 933struct exclude_list_group *group; 934struct exclude_list *el; 935struct exclude_stack *stk = NULL; 936struct untracked_cache_dir *untracked; 937int current; 938 939 group = &dir->exclude_list_group[EXC_DIRS]; 940 941/* 942 * Pop the exclude lists from the EXCL_DIRS exclude_list_group 943 * which originate from directories not in the prefix of the 944 * path being checked. 945 */ 946while((stk = dir->exclude_stack) != NULL) { 947if(stk->baselen <= baselen && 948!strncmp(dir->basebuf.buf, base, stk->baselen)) 949break; 950 el = &group->el[dir->exclude_stack->exclude_ix]; 951 dir->exclude_stack = stk->prev; 952 dir->exclude = NULL; 953free((char*)el->src);/* see strbuf_detach() below */ 954clear_exclude_list(el); 955free(stk); 956 group->nr--; 957} 958 959/* Skip traversing into sub directories if the parent is excluded */ 960if(dir->exclude) 961return; 962 963/* 964 * Lazy initialization. All call sites currently just 965 * memset(dir, 0, sizeof(*dir)) before use. Changing all of 966 * them seems lots of work for little benefit. 967 */ 968if(!dir->basebuf.buf) 969strbuf_init(&dir->basebuf, PATH_MAX); 970 971/* Read from the parent directories and push them down. */ 972 current = stk ? stk->baselen : -1; 973strbuf_setlen(&dir->basebuf, current <0?0: current); 974if(dir->untracked) 975 untracked = stk ? stk->ucd : dir->untracked->root; 976else 977 untracked = NULL; 978 979while(current < baselen) { 980const char*cp; 981struct sha1_stat sha1_stat; 982 983 stk =xcalloc(1,sizeof(*stk)); 984if(current <0) { 985 cp = base; 986 current =0; 987}else{ 988 cp =strchr(base + current +1,'/'); 989if(!cp) 990die("oops in prep_exclude"); 991 cp++; 992 untracked = 993lookup_untracked(dir->untracked, untracked, 994 base + current, 995 cp - base - current); 996} 997 stk->prev = dir->exclude_stack; 998 stk->baselen = cp - base; 999 stk->exclude_ix = group->nr;1000 stk->ucd = untracked;1001 el =add_exclude_list(dir, EXC_DIRS, NULL);1002strbuf_add(&dir->basebuf, base + current, stk->baselen - current);1003assert(stk->baselen == dir->basebuf.len);10041005/* Abort if the directory is excluded */1006if(stk->baselen) {1007int dt = DT_DIR;1008 dir->basebuf.buf[stk->baselen -1] =0;1009 dir->exclude =last_exclude_matching_from_lists(dir,1010 dir->basebuf.buf, stk->baselen -1,1011 dir->basebuf.buf + current, &dt);1012 dir->basebuf.buf[stk->baselen -1] ='/';1013if(dir->exclude &&1014 dir->exclude->flags & EXC_FLAG_NEGATIVE)1015 dir->exclude = NULL;1016if(dir->exclude) {1017 dir->exclude_stack = stk;1018return;1019}1020}10211022/* Try to read per-directory file */1023hashclr(sha1_stat.sha1);1024 sha1_stat.valid =0;1025if(dir->exclude_per_dir &&1026/*1027 * If we know that no files have been added in1028 * this directory (i.e. valid_cached_dir() has1029 * been executed and set untracked->valid) ..1030 */1031(!untracked || !untracked->valid ||1032/*1033 * .. and .gitignore does not exist before1034 * (i.e. null exclude_sha1 and skip_worktree is1035 * not set). Then we can skip loading .gitignore,1036 * which would result in ENOENT anyway.1037 * skip_worktree is taken care in read_directory()1038 */1039!is_null_sha1(untracked->exclude_sha1))) {1040/*1041 * dir->basebuf gets reused by the traversal, but we1042 * need fname to remain unchanged to ensure the src1043 * member of each struct exclude correctly1044 * back-references its source file. Other invocations1045 * of add_exclude_list provide stable strings, so we1046 * strbuf_detach() and free() here in the caller.1047 */1048struct strbuf sb = STRBUF_INIT;1049strbuf_addbuf(&sb, &dir->basebuf);1050strbuf_addstr(&sb, dir->exclude_per_dir);1051 el->src =strbuf_detach(&sb, NULL);1052add_excludes(el->src, el->src, stk->baselen, el,1,1053 untracked ? &sha1_stat : NULL);1054}1055/*1056 * NEEDSWORK: when untracked cache is enabled, prep_exclude()1057 * will first be called in valid_cached_dir() then maybe many1058 * times more in last_exclude_matching(). When the cache is1059 * used, last_exclude_matching() will not be called and1060 * reading .gitignore content will be a waste.1061 *1062 * So when it's called by valid_cached_dir() and we can get1063 * .gitignore SHA-1 from the index (i.e. .gitignore is not1064 * modified on work tree), we could delay reading the1065 * .gitignore content until we absolutely need it in1066 * last_exclude_matching(). Be careful about ignore rule1067 * order, though, if you do that.1068 */1069if(untracked &&1070hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {1071invalidate_gitignore(dir->untracked, untracked);1072hashcpy(untracked->exclude_sha1, sha1_stat.sha1);1073}1074 dir->exclude_stack = stk;1075 current = stk->baselen;1076}1077strbuf_setlen(&dir->basebuf, baselen);1078}10791080/*1081 * Loads the exclude lists for the directory containing pathname, then1082 * scans all exclude lists to determine whether pathname is excluded.1083 * Returns the exclude_list element which matched, or NULL for1084 * undecided.1085 */1086struct exclude *last_exclude_matching(struct dir_struct *dir,1087const char*pathname,1088int*dtype_p)1089{1090int pathlen =strlen(pathname);1091const char*basename =strrchr(pathname,'/');1092 basename = (basename) ? basename+1: pathname;10931094prep_exclude(dir, pathname, basename-pathname);10951096if(dir->exclude)1097return dir->exclude;10981099returnlast_exclude_matching_from_lists(dir, pathname, pathlen,1100 basename, dtype_p);1101}11021103/*1104 * Loads the exclude lists for the directory containing pathname, then1105 * scans all exclude lists to determine whether pathname is excluded.1106 * Returns 1 if true, otherwise 0.1107 */1108intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p)1109{1110struct exclude *exclude =1111last_exclude_matching(dir, pathname, dtype_p);1112if(exclude)1113return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1114return0;1115}11161117static struct dir_entry *dir_entry_new(const char*pathname,int len)1118{1119struct dir_entry *ent;11201121 ent =xmalloc(sizeof(*ent) + len +1);1122 ent->len = len;1123memcpy(ent->name, pathname, len);1124 ent->name[len] =0;1125return ent;1126}11271128static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len)1129{1130if(cache_file_exists(pathname, len, ignore_case))1131return NULL;11321133ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);1134return dir->entries[dir->nr++] =dir_entry_new(pathname, len);1135}11361137struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len)1138{1139if(!cache_name_is_other(pathname, len))1140return NULL;11411142ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1143return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len);1144}11451146enum exist_status {1147 index_nonexistent =0,1148 index_directory,1149 index_gitdir1150};11511152/*1153 * Do not use the alphabetically sorted index to look up1154 * the directory name; instead, use the case insensitive1155 * directory hash.1156 */1157static enum exist_status directory_exists_in_index_icase(const char*dirname,int len)1158{1159const struct cache_entry *ce =cache_dir_exists(dirname, len);1160unsigned char endchar;11611162if(!ce)1163return index_nonexistent;1164 endchar = ce->name[len];11651166/*1167 * The cache_entry structure returned will contain this dirname1168 * and possibly additional path components.1169 */1170if(endchar =='/')1171return index_directory;11721173/*1174 * If there are no additional path components, then this cache_entry1175 * represents a submodule. Submodules, despite being directories,1176 * are stored in the cache without a closing slash.1177 */1178if(!endchar &&S_ISGITLINK(ce->ce_mode))1179return index_gitdir;11801181/* This should never be hit, but it exists just in case. */1182return index_nonexistent;1183}11841185/*1186 * The index sorts alphabetically by entry name, which1187 * means that a gitlink sorts as '\0' at the end, while1188 * a directory (which is defined not as an entry, but as1189 * the files it contains) will sort with the '/' at the1190 * end.1191 */1192static enum exist_status directory_exists_in_index(const char*dirname,int len)1193{1194int pos;11951196if(ignore_case)1197returndirectory_exists_in_index_icase(dirname, len);11981199 pos =cache_name_pos(dirname, len);1200if(pos <0)1201 pos = -pos-1;1202while(pos < active_nr) {1203const struct cache_entry *ce = active_cache[pos++];1204unsigned char endchar;12051206if(strncmp(ce->name, dirname, len))1207break;1208 endchar = ce->name[len];1209if(endchar >'/')1210break;1211if(endchar =='/')1212return index_directory;1213if(!endchar &&S_ISGITLINK(ce->ce_mode))1214return index_gitdir;1215}1216return index_nonexistent;1217}12181219/*1220 * When we find a directory when traversing the filesystem, we1221 * have three distinct cases:1222 *1223 * - ignore it1224 * - see it as a directory1225 * - recurse into it1226 *1227 * and which one we choose depends on a combination of existing1228 * git index contents and the flags passed into the directory1229 * traversal routine.1230 *1231 * Case 1: If we *already* have entries in the index under that1232 * directory name, we always recurse into the directory to see1233 * all the files.1234 *1235 * Case 2: If we *already* have that directory name as a gitlink,1236 * we always continue to see it as a gitlink, regardless of whether1237 * there is an actual git directory there or not (it might not1238 * be checked out as a subproject!)1239 *1240 * Case 3: if we didn't have it in the index previously, we1241 * have a few sub-cases:1242 *1243 * (a) if "show_other_directories" is true, we show it as1244 * just a directory, unless "hide_empty_directories" is1245 * also true, in which case we need to check if it contains any1246 * untracked and / or ignored files.1247 * (b) if it looks like a git directory, and we don't have1248 * 'no_gitlinks' set we treat it as a gitlink, and show it1249 * as a directory.1250 * (c) otherwise, we recurse into it.1251 */1252static enum path_treatment treat_directory(struct dir_struct *dir,1253struct untracked_cache_dir *untracked,1254const char*dirname,int len,int exclude,1255const struct path_simplify *simplify)1256{1257/* The "len-1" is to strip the final '/' */1258switch(directory_exists_in_index(dirname, len-1)) {1259case index_directory:1260return path_recurse;12611262case index_gitdir:1263return path_none;12641265case index_nonexistent:1266if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1267break;1268if(!(dir->flags & DIR_NO_GITLINKS)) {1269unsigned char sha1[20];1270if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1271return path_untracked;1272}1273return path_recurse;1274}12751276/* This is the "show_other_directories" case */12771278if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1279return exclude ? path_excluded : path_untracked;12801281 untracked =lookup_untracked(dir->untracked, untracked, dirname, len);1282returnread_directory_recursive(dir, dirname, len,1283 untracked,1, simplify);1284}12851286/*1287 * This is an inexact early pruning of any recursive directory1288 * reading - if the path cannot possibly be in the pathspec,1289 * return true, and we'll skip it early.1290 */1291static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1292{1293if(simplify) {1294for(;;) {1295const char*match = simplify->path;1296int len = simplify->len;12971298if(!match)1299break;1300if(len > pathlen)1301 len = pathlen;1302if(!memcmp(path, match, len))1303return0;1304 simplify++;1305}1306return1;1307}1308return0;1309}13101311/*1312 * This function tells us whether an excluded path matches a1313 * list of "interesting" pathspecs. That is, whether a path matched1314 * by any of the pathspecs could possibly be ignored by excluding1315 * the specified path. This can happen if:1316 *1317 * 1. the path is mentioned explicitly in the pathspec1318 *1319 * 2. the path is a directory prefix of some element in the1320 * pathspec1321 */1322static intexclude_matches_pathspec(const char*path,int len,1323const struct path_simplify *simplify)1324{1325if(simplify) {1326for(; simplify->path; simplify++) {1327if(len == simplify->len1328&& !memcmp(path, simplify->path, len))1329return1;1330if(len < simplify->len1331&& simplify->path[len] =='/'1332&& !memcmp(path, simplify->path, len))1333return1;1334}1335}1336return0;1337}13381339static intget_index_dtype(const char*path,int len)1340{1341int pos;1342const struct cache_entry *ce;13431344 ce =cache_file_exists(path, len,0);1345if(ce) {1346if(!ce_uptodate(ce))1347return DT_UNKNOWN;1348if(S_ISGITLINK(ce->ce_mode))1349return DT_DIR;1350/*1351 * Nobody actually cares about the1352 * difference between DT_LNK and DT_REG1353 */1354return DT_REG;1355}13561357/* Try to look it up as a directory */1358 pos =cache_name_pos(path, len);1359if(pos >=0)1360return DT_UNKNOWN;1361 pos = -pos-1;1362while(pos < active_nr) {1363 ce = active_cache[pos++];1364if(strncmp(ce->name, path, len))1365break;1366if(ce->name[len] >'/')1367break;1368if(ce->name[len] <'/')1369continue;1370if(!ce_uptodate(ce))1371break;/* continue? */1372return DT_DIR;1373}1374return DT_UNKNOWN;1375}13761377static intget_dtype(struct dirent *de,const char*path,int len)1378{1379int dtype = de ?DTYPE(de) : DT_UNKNOWN;1380struct stat st;13811382if(dtype != DT_UNKNOWN)1383return dtype;1384 dtype =get_index_dtype(path, len);1385if(dtype != DT_UNKNOWN)1386return dtype;1387if(lstat(path, &st))1388return dtype;1389if(S_ISREG(st.st_mode))1390return DT_REG;1391if(S_ISDIR(st.st_mode))1392return DT_DIR;1393if(S_ISLNK(st.st_mode))1394return DT_LNK;1395return dtype;1396}13971398static enum path_treatment treat_one_path(struct dir_struct *dir,1399struct untracked_cache_dir *untracked,1400struct strbuf *path,1401const struct path_simplify *simplify,1402int dtype,struct dirent *de)1403{1404int exclude;1405int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);14061407if(dtype == DT_UNKNOWN)1408 dtype =get_dtype(de, path->buf, path->len);14091410/* Always exclude indexed files */1411if(dtype != DT_DIR && has_path_in_index)1412return path_none;14131414/*1415 * When we are looking at a directory P in the working tree,1416 * there are three cases:1417 *1418 * (1) P exists in the index. Everything inside the directory P in1419 * the working tree needs to go when P is checked out from the1420 * index.1421 *1422 * (2) P does not exist in the index, but there is P/Q in the index.1423 * We know P will stay a directory when we check out the contents1424 * of the index, but we do not know yet if there is a directory1425 * P/Q in the working tree to be killed, so we need to recurse.1426 *1427 * (3) P does not exist in the index, and there is no P/Q in the index1428 * to require P to be a directory, either. Only in this case, we1429 * know that everything inside P will not be killed without1430 * recursing.1431 */1432if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1433(dtype == DT_DIR) &&1434!has_path_in_index &&1435(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1436return path_none;14371438 exclude =is_excluded(dir, path->buf, &dtype);14391440/*1441 * Excluded? If we don't explicitly want to show1442 * ignored files, ignore it1443 */1444if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1445return path_excluded;14461447switch(dtype) {1448default:1449return path_none;1450case DT_DIR:1451strbuf_addch(path,'/');1452returntreat_directory(dir, untracked, path->buf, path->len, exclude,1453 simplify);1454case DT_REG:1455case DT_LNK:1456return exclude ? path_excluded : path_untracked;1457}1458}14591460static enum path_treatment treat_path_fast(struct dir_struct *dir,1461struct untracked_cache_dir *untracked,1462struct cached_dir *cdir,1463struct strbuf *path,1464int baselen,1465const struct path_simplify *simplify)1466{1467strbuf_setlen(path, baselen);1468if(!cdir->ucd) {1469strbuf_addstr(path, cdir->file);1470return path_untracked;1471}1472strbuf_addstr(path, cdir->ucd->name);1473/* treat_one_path() does this before it calls treat_directory() */1474if(path->buf[path->len -1] !='/')1475strbuf_addch(path,'/');1476if(cdir->ucd->check_only)1477/*1478 * check_only is set as a result of treat_directory() getting1479 * to its bottom. Verify again the same set of directories1480 * with check_only set.1481 */1482returnread_directory_recursive(dir, path->buf, path->len,1483 cdir->ucd,1, simplify);1484/*1485 * We get path_recurse in the first run when1486 * directory_exists_in_index() returns index_nonexistent. We1487 * are sure that new changes in the index does not impact the1488 * outcome. Return now.1489 */1490return path_recurse;1491}14921493static enum path_treatment treat_path(struct dir_struct *dir,1494struct untracked_cache_dir *untracked,1495struct cached_dir *cdir,1496struct strbuf *path,1497int baselen,1498const struct path_simplify *simplify)1499{1500int dtype;1501struct dirent *de = cdir->de;15021503if(!de)1504returntreat_path_fast(dir, untracked, cdir, path,1505 baselen, simplify);1506if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1507return path_none;1508strbuf_setlen(path, baselen);1509strbuf_addstr(path, de->d_name);1510if(simplify_away(path->buf, path->len, simplify))1511return path_none;15121513 dtype =DTYPE(de);1514returntreat_one_path(dir, untracked, path, simplify, dtype, de);1515}15161517static voidadd_untracked(struct untracked_cache_dir *dir,const char*name)1518{1519if(!dir)1520return;1521ALLOC_GROW(dir->untracked, dir->untracked_nr +1,1522 dir->untracked_alloc);1523 dir->untracked[dir->untracked_nr++] =xstrdup(name);1524}15251526static intvalid_cached_dir(struct dir_struct *dir,1527struct untracked_cache_dir *untracked,1528struct strbuf *path,1529int check_only)1530{1531struct stat st;15321533if(!untracked)1534return0;15351536if(stat(path->len ? path->buf :".", &st)) {1537invalidate_directory(dir->untracked, untracked);1538memset(&untracked->stat_data,0,sizeof(untracked->stat_data));1539return0;1540}1541if(!untracked->valid ||1542match_stat_data_racy(&the_index, &untracked->stat_data, &st)) {1543if(untracked->valid)1544invalidate_directory(dir->untracked, untracked);1545fill_stat_data(&untracked->stat_data, &st);1546return0;1547}15481549if(untracked->check_only != !!check_only) {1550invalidate_directory(dir->untracked, untracked);1551return0;1552}15531554/*1555 * prep_exclude will be called eventually on this directory,1556 * but it's called much later in last_exclude_matching(). We1557 * need it now to determine the validity of the cache for this1558 * path. The next calls will be nearly no-op, the way1559 * prep_exclude() is designed.1560 */1561if(path->len && path->buf[path->len -1] !='/') {1562strbuf_addch(path,'/');1563prep_exclude(dir, path->buf, path->len);1564strbuf_setlen(path, path->len -1);1565}else1566prep_exclude(dir, path->buf, path->len);15671568/* hopefully prep_exclude() haven't invalidated this entry... */1569return untracked->valid;1570}15711572static intopen_cached_dir(struct cached_dir *cdir,1573struct dir_struct *dir,1574struct untracked_cache_dir *untracked,1575struct strbuf *path,1576int check_only)1577{1578memset(cdir,0,sizeof(*cdir));1579 cdir->untracked = untracked;1580if(valid_cached_dir(dir, untracked, path, check_only))1581return0;1582 cdir->fdir =opendir(path->len ? path->buf :".");1583if(dir->untracked)1584 dir->untracked->dir_opened++;1585if(!cdir->fdir)1586return-1;1587return0;1588}15891590static intread_cached_dir(struct cached_dir *cdir)1591{1592if(cdir->fdir) {1593 cdir->de =readdir(cdir->fdir);1594if(!cdir->de)1595return-1;1596return0;1597}1598while(cdir->nr_dirs < cdir->untracked->dirs_nr) {1599struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];1600if(!d->recurse) {1601 cdir->nr_dirs++;1602continue;1603}1604 cdir->ucd = d;1605 cdir->nr_dirs++;1606return0;1607}1608 cdir->ucd = NULL;1609if(cdir->nr_files < cdir->untracked->untracked_nr) {1610struct untracked_cache_dir *d = cdir->untracked;1611 cdir->file = d->untracked[cdir->nr_files++];1612return0;1613}1614return-1;1615}16161617static voidclose_cached_dir(struct cached_dir *cdir)1618{1619if(cdir->fdir)1620closedir(cdir->fdir);1621/*1622 * We have gone through this directory and found no untracked1623 * entries. Mark it valid.1624 */1625if(cdir->untracked) {1626 cdir->untracked->valid =1;1627 cdir->untracked->recurse =1;1628}1629}16301631/*1632 * Read a directory tree. We currently ignore anything but1633 * directories, regular files and symlinks. That's because git1634 * doesn't handle them at all yet. Maybe that will change some1635 * day.1636 *1637 * Also, we ignore the name ".git" (even if it is not a directory).1638 * That likely will not change.1639 *1640 * Returns the most significant path_treatment value encountered in the scan.1641 */1642static enum path_treatment read_directory_recursive(struct dir_struct *dir,1643const char*base,int baselen,1644struct untracked_cache_dir *untracked,int check_only,1645const struct path_simplify *simplify)1646{1647struct cached_dir cdir;1648enum path_treatment state, subdir_state, dir_state = path_none;1649struct strbuf path = STRBUF_INIT;16501651strbuf_add(&path, base, baselen);16521653if(open_cached_dir(&cdir, dir, untracked, &path, check_only))1654goto out;16551656if(untracked)1657 untracked->check_only = !!check_only;16581659while(!read_cached_dir(&cdir)) {1660/* check how the file or directory should be treated */1661 state =treat_path(dir, untracked, &cdir, &path, baselen, simplify);16621663if(state > dir_state)1664 dir_state = state;16651666/* recurse into subdir if instructed by treat_path */1667if(state == path_recurse) {1668struct untracked_cache_dir *ud;1669 ud =lookup_untracked(dir->untracked, untracked,1670 path.buf + baselen,1671 path.len - baselen);1672 subdir_state =1673read_directory_recursive(dir, path.buf, path.len,1674 ud, check_only, simplify);1675if(subdir_state > dir_state)1676 dir_state = subdir_state;1677}16781679if(check_only) {1680/* abort early if maximum state has been reached */1681if(dir_state == path_untracked) {1682if(cdir.fdir)1683add_untracked(untracked, path.buf + baselen);1684break;1685}1686/* skip the dir_add_* part */1687continue;1688}16891690/* add the path to the appropriate result list */1691switch(state) {1692case path_excluded:1693if(dir->flags & DIR_SHOW_IGNORED)1694dir_add_name(dir, path.buf, path.len);1695else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1696((dir->flags & DIR_COLLECT_IGNORED) &&1697exclude_matches_pathspec(path.buf, path.len,1698 simplify)))1699dir_add_ignored(dir, path.buf, path.len);1700break;17011702case path_untracked:1703if(dir->flags & DIR_SHOW_IGNORED)1704break;1705dir_add_name(dir, path.buf, path.len);1706if(cdir.fdir)1707add_untracked(untracked, path.buf + baselen);1708break;17091710default:1711break;1712}1713}1714close_cached_dir(&cdir);1715 out:1716strbuf_release(&path);17171718return dir_state;1719}17201721static intcmp_name(const void*p1,const void*p2)1722{1723const struct dir_entry *e1 = *(const struct dir_entry **)p1;1724const struct dir_entry *e2 = *(const struct dir_entry **)p2;17251726returnname_compare(e1->name, e1->len, e2->name, e2->len);1727}17281729static struct path_simplify *create_simplify(const char**pathspec)1730{1731int nr, alloc =0;1732struct path_simplify *simplify = NULL;17331734if(!pathspec)1735return NULL;17361737for(nr =0; ; nr++) {1738const char*match;1739ALLOC_GROW(simplify, nr +1, alloc);1740 match = *pathspec++;1741if(!match)1742break;1743 simplify[nr].path = match;1744 simplify[nr].len =simple_length(match);1745}1746 simplify[nr].path = NULL;1747 simplify[nr].len =0;1748return simplify;1749}17501751static voidfree_simplify(struct path_simplify *simplify)1752{1753free(simplify);1754}17551756static inttreat_leading_path(struct dir_struct *dir,1757const char*path,int len,1758const struct path_simplify *simplify)1759{1760struct strbuf sb = STRBUF_INIT;1761int baselen, rc =0;1762const char*cp;1763int old_flags = dir->flags;17641765while(len && path[len -1] =='/')1766 len--;1767if(!len)1768return1;1769 baselen =0;1770 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1771while(1) {1772 cp = path + baselen + !!baselen;1773 cp =memchr(cp,'/', path + len - cp);1774if(!cp)1775 baselen = len;1776else1777 baselen = cp - path;1778strbuf_setlen(&sb,0);1779strbuf_add(&sb, path, baselen);1780if(!is_directory(sb.buf))1781break;1782if(simplify_away(sb.buf, sb.len, simplify))1783break;1784if(treat_one_path(dir, NULL, &sb, simplify,1785 DT_DIR, NULL) == path_none)1786break;/* do not recurse into it */1787if(len <= baselen) {1788 rc =1;1789break;/* finished checking */1790}1791}1792strbuf_release(&sb);1793 dir->flags = old_flags;1794return rc;1795}17961797static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,1798int base_len,1799const struct pathspec *pathspec)1800{1801struct untracked_cache_dir *root;1802int i;18031804if(!dir->untracked)1805return NULL;18061807/*1808 * We only support $GIT_DIR/info/exclude and core.excludesfile1809 * as the global ignore rule files. Any other additions1810 * (e.g. from command line) invalidate the cache. This1811 * condition also catches running setup_standard_excludes()1812 * before setting dir->untracked!1813 */1814if(dir->unmanaged_exclude_files)1815return NULL;18161817/*1818 * Optimize for the main use case only: whole-tree git1819 * status. More work involved in treat_leading_path() if we1820 * use cache on just a subset of the worktree. pathspec1821 * support could make the matter even worse.1822 */1823if(base_len || (pathspec && pathspec->nr))1824return NULL;18251826/* Different set of flags may produce different results */1827if(dir->flags != dir->untracked->dir_flags ||1828/*1829 * See treat_directory(), case index_nonexistent. Without1830 * this flag, we may need to also cache .git file content1831 * for the resolve_gitlink_ref() call, which we don't.1832 */1833!(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||1834/* We don't support collecting ignore files */1835(dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |1836 DIR_COLLECT_IGNORED)))1837return NULL;18381839/*1840 * If we use .gitignore in the cache and now you change it to1841 * .gitexclude, everything will go wrong.1842 */1843if(dir->exclude_per_dir != dir->untracked->exclude_per_dir &&1844strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))1845return NULL;18461847/*1848 * EXC_CMDL is not considered in the cache. If people set it,1849 * skip the cache.1850 */1851if(dir->exclude_list_group[EXC_CMDL].nr)1852return NULL;18531854/*1855 * An optimization in prep_exclude() does not play well with1856 * CE_SKIP_WORKTREE. It's a rare case anyway, if a single1857 * entry has that bit set, disable the whole untracked cache.1858 */1859for(i =0; i < active_nr; i++)1860if(ce_skip_worktree(active_cache[i]))1861return NULL;18621863if(!dir->untracked->root) {1864const int len =sizeof(*dir->untracked->root);1865 dir->untracked->root =xmalloc(len);1866memset(dir->untracked->root,0, len);1867}18681869/* Validate $GIT_DIR/info/exclude and core.excludesfile */1870 root = dir->untracked->root;1871if(hashcmp(dir->ss_info_exclude.sha1,1872 dir->untracked->ss_info_exclude.sha1)) {1873invalidate_gitignore(dir->untracked, root);1874 dir->untracked->ss_info_exclude = dir->ss_info_exclude;1875}1876if(hashcmp(dir->ss_excludes_file.sha1,1877 dir->untracked->ss_excludes_file.sha1)) {1878invalidate_gitignore(dir->untracked, root);1879 dir->untracked->ss_excludes_file = dir->ss_excludes_file;1880}18811882/* Make sure this directory is not dropped out at saving phase */1883 root->recurse =1;1884return root;1885}18861887intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1888{1889struct path_simplify *simplify;1890struct untracked_cache_dir *untracked;18911892/*1893 * Check out create_simplify()1894 */1895if(pathspec)1896GUARD_PATHSPEC(pathspec,1897 PATHSPEC_FROMTOP |1898 PATHSPEC_MAXDEPTH |1899 PATHSPEC_LITERAL |1900 PATHSPEC_GLOB |1901 PATHSPEC_ICASE |1902 PATHSPEC_EXCLUDE);19031904if(has_symlink_leading_path(path, len))1905return dir->nr;19061907/*1908 * exclude patterns are treated like positive ones in1909 * create_simplify. Usually exclude patterns should be a1910 * subset of positive ones, which has no impacts on1911 * create_simplify().1912 */1913 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1914 untracked =validate_untracked_cache(dir, len, pathspec);1915if(!untracked)1916/*1917 * make sure untracked cache code path is disabled,1918 * e.g. prep_exclude()1919 */1920 dir->untracked = NULL;1921if(!len ||treat_leading_path(dir, path, len, simplify))1922read_directory_recursive(dir, path, len, untracked,0, simplify);1923free_simplify(simplify);1924qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1925qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1926if(dir->untracked) {1927static struct trace_key trace_untracked_stats =TRACE_KEY_INIT(UNTRACKED_STATS);1928trace_printf_key(&trace_untracked_stats,1929"node creation:%u\n"1930"gitignore invalidation:%u\n"1931"directory invalidation:%u\n"1932"opendir:%u\n",1933 dir->untracked->dir_created,1934 dir->untracked->gitignore_invalidated,1935 dir->untracked->dir_invalidated,1936 dir->untracked->dir_opened);1937}1938return dir->nr;1939}19401941intfile_exists(const char*f)1942{1943struct stat sb;1944returnlstat(f, &sb) ==0;1945}19461947/*1948 * Given two normalized paths (a trailing slash is ok), if subdir is1949 * outside dir, return -1. Otherwise return the offset in subdir that1950 * can be used as relative path to dir.1951 */1952intdir_inside_of(const char*subdir,const char*dir)1953{1954int offset =0;19551956assert(dir && subdir && *dir && *subdir);19571958while(*dir && *subdir && *dir == *subdir) {1959 dir++;1960 subdir++;1961 offset++;1962}19631964/* hel[p]/me vs hel[l]/yeah */1965if(*dir && *subdir)1966return-1;19671968if(!*subdir)1969return!*dir ? offset : -1;/* same dir */19701971/* foo/[b]ar vs foo/[] */1972if(is_dir_sep(dir[-1]))1973returnis_dir_sep(subdir[-1]) ? offset : -1;19741975/* foo[/]bar vs foo[] */1976returnis_dir_sep(*subdir) ? offset +1: -1;1977}19781979intis_inside_dir(const char*dir)1980{1981char*cwd;1982int rc;19831984if(!dir)1985return0;19861987 cwd =xgetcwd();1988 rc = (dir_inside_of(cwd, dir) >=0);1989free(cwd);1990return rc;1991}19921993intis_empty_dir(const char*path)1994{1995DIR*dir =opendir(path);1996struct dirent *e;1997int ret =1;19981999if(!dir)2000return0;20012002while((e =readdir(dir)) != NULL)2003if(!is_dot_or_dotdot(e->d_name)) {2004 ret =0;2005break;2006}20072008closedir(dir);2009return ret;2010}20112012static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)2013{2014DIR*dir;2015struct dirent *e;2016int ret =0, original_len = path->len, len, kept_down =0;2017int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);2018int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);2019unsigned char submodule_head[20];20202021if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&2022!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {2023/* Do not descend and nuke a nested git work tree. */2024if(kept_up)2025*kept_up =1;2026return0;2027}20282029 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;2030 dir =opendir(path->buf);2031if(!dir) {2032if(errno == ENOENT)2033return keep_toplevel ? -1:0;2034else if(errno == EACCES && !keep_toplevel)2035/*2036 * An empty dir could be removable even if it2037 * is unreadable:2038 */2039returnrmdir(path->buf);2040else2041return-1;2042}2043if(path->buf[original_len -1] !='/')2044strbuf_addch(path,'/');20452046 len = path->len;2047while((e =readdir(dir)) != NULL) {2048struct stat st;2049if(is_dot_or_dotdot(e->d_name))2050continue;20512052strbuf_setlen(path, len);2053strbuf_addstr(path, e->d_name);2054if(lstat(path->buf, &st)) {2055if(errno == ENOENT)2056/*2057 * file disappeared, which is what we2058 * wanted anyway2059 */2060continue;2061/* fall thru */2062}else if(S_ISDIR(st.st_mode)) {2063if(!remove_dir_recurse(path, flag, &kept_down))2064continue;/* happy */2065}else if(!only_empty &&2066(!unlink(path->buf) || errno == ENOENT)) {2067continue;/* happy, too */2068}20692070/* path too long, stat fails, or non-directory still exists */2071 ret = -1;2072break;2073}2074closedir(dir);20752076strbuf_setlen(path, original_len);2077if(!ret && !keep_toplevel && !kept_down)2078 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;2079else if(kept_up)2080/*2081 * report the uplevel that it is not an error that we2082 * did not rmdir() our directory.2083 */2084*kept_up = !ret;2085return ret;2086}20872088intremove_dir_recursively(struct strbuf *path,int flag)2089{2090returnremove_dir_recurse(path, flag, NULL);2091}20922093voidsetup_standard_excludes(struct dir_struct *dir)2094{2095const char*path;2096char*xdg_path;20972098 dir->exclude_per_dir =".gitignore";2099 path =git_path("info/exclude");2100if(!excludes_file) {2101home_config_paths(NULL, &xdg_path,"ignore");2102 excludes_file = xdg_path;2103}2104if(!access_or_warn(path, R_OK,0))2105add_excludes_from_file_1(dir, path,2106 dir->untracked ? &dir->ss_info_exclude : NULL);2107if(excludes_file && !access_or_warn(excludes_file, R_OK,0))2108add_excludes_from_file_1(dir, excludes_file,2109 dir->untracked ? &dir->ss_excludes_file : NULL);2110}21112112intremove_path(const char*name)2113{2114char*slash;21152116if(unlink(name) && errno != ENOENT && errno != ENOTDIR)2117return-1;21182119 slash =strrchr(name,'/');2120if(slash) {2121char*dirs =xstrdup(name);2122 slash = dirs + (slash - name);2123do{2124*slash ='\0';2125}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));2126free(dirs);2127}2128return0;2129}21302131/*2132 * Frees memory within dir which was allocated for exclude lists and2133 * the exclude_stack. Does not free dir itself.2134 */2135voidclear_directory(struct dir_struct *dir)2136{2137int i, j;2138struct exclude_list_group *group;2139struct exclude_list *el;2140struct exclude_stack *stk;21412142for(i = EXC_CMDL; i <= EXC_FILE; i++) {2143 group = &dir->exclude_list_group[i];2144for(j =0; j < group->nr; j++) {2145 el = &group->el[j];2146if(i == EXC_DIRS)2147free((char*)el->src);2148clear_exclude_list(el);2149}2150free(group->el);2151}21522153 stk = dir->exclude_stack;2154while(stk) {2155struct exclude_stack *prev = stk->prev;2156free(stk);2157 stk = prev;2158}2159strbuf_release(&dir->basebuf);2160}21612162struct ondisk_untracked_cache {2163struct stat_data info_exclude_stat;2164struct stat_data excludes_file_stat;2165uint32_t dir_flags;2166unsigned char info_exclude_sha1[20];2167unsigned char excludes_file_sha1[20];2168char exclude_per_dir[FLEX_ARRAY];2169};21702171#define ouc_size(len) (offsetof(struct ondisk_untracked_cache, exclude_per_dir) + len + 1)21722173struct write_data {2174int index;/* number of written untracked_cache_dir */2175struct ewah_bitmap *check_only;/* from untracked_cache_dir */2176struct ewah_bitmap *valid;/* from untracked_cache_dir */2177struct ewah_bitmap *sha1_valid;/* set if exclude_sha1 is not null */2178struct strbuf out;2179struct strbuf sb_stat;2180struct strbuf sb_sha1;2181};21822183static voidstat_data_to_disk(struct stat_data *to,const struct stat_data *from)2184{2185 to->sd_ctime.sec =htonl(from->sd_ctime.sec);2186 to->sd_ctime.nsec =htonl(from->sd_ctime.nsec);2187 to->sd_mtime.sec =htonl(from->sd_mtime.sec);2188 to->sd_mtime.nsec =htonl(from->sd_mtime.nsec);2189 to->sd_dev =htonl(from->sd_dev);2190 to->sd_ino =htonl(from->sd_ino);2191 to->sd_uid =htonl(from->sd_uid);2192 to->sd_gid =htonl(from->sd_gid);2193 to->sd_size =htonl(from->sd_size);2194}21952196static voidwrite_one_dir(struct untracked_cache_dir *untracked,2197struct write_data *wd)2198{2199struct stat_data stat_data;2200struct strbuf *out = &wd->out;2201unsigned char intbuf[16];2202unsigned int intlen, value;2203int i = wd->index++;22042205/*2206 * untracked_nr should be reset whenever valid is clear, but2207 * for safety..2208 */2209if(!untracked->valid) {2210 untracked->untracked_nr =0;2211 untracked->check_only =0;2212}22132214if(untracked->check_only)2215ewah_set(wd->check_only, i);2216if(untracked->valid) {2217ewah_set(wd->valid, i);2218stat_data_to_disk(&stat_data, &untracked->stat_data);2219strbuf_add(&wd->sb_stat, &stat_data,sizeof(stat_data));2220}2221if(!is_null_sha1(untracked->exclude_sha1)) {2222ewah_set(wd->sha1_valid, i);2223strbuf_add(&wd->sb_sha1, untracked->exclude_sha1,20);2224}22252226 intlen =encode_varint(untracked->untracked_nr, intbuf);2227strbuf_add(out, intbuf, intlen);22282229/* skip non-recurse directories */2230for(i =0, value =0; i < untracked->dirs_nr; i++)2231if(untracked->dirs[i]->recurse)2232 value++;2233 intlen =encode_varint(value, intbuf);2234strbuf_add(out, intbuf, intlen);22352236strbuf_add(out, untracked->name,strlen(untracked->name) +1);22372238for(i =0; i < untracked->untracked_nr; i++)2239strbuf_add(out, untracked->untracked[i],2240strlen(untracked->untracked[i]) +1);22412242for(i =0; i < untracked->dirs_nr; i++)2243if(untracked->dirs[i]->recurse)2244write_one_dir(untracked->dirs[i], wd);2245}22462247voidwrite_untracked_extension(struct strbuf *out,struct untracked_cache *untracked)2248{2249struct ondisk_untracked_cache *ouc;2250struct write_data wd;2251unsigned char varbuf[16];2252int len =0, varint_len;2253if(untracked->exclude_per_dir)2254 len =strlen(untracked->exclude_per_dir);2255 ouc =xmalloc(sizeof(*ouc) + len +1);2256stat_data_to_disk(&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);2257stat_data_to_disk(&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);2258hashcpy(ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);2259hashcpy(ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1);2260 ouc->dir_flags =htonl(untracked->dir_flags);2261memcpy(ouc->exclude_per_dir, untracked->exclude_per_dir, len +1);2262strbuf_add(out, ouc,ouc_size(len));2263free(ouc);2264 ouc = NULL;22652266if(!untracked->root) {2267 varint_len =encode_varint(0, varbuf);2268strbuf_add(out, varbuf, varint_len);2269return;2270}22712272 wd.index =0;2273 wd.check_only =ewah_new();2274 wd.valid =ewah_new();2275 wd.sha1_valid =ewah_new();2276strbuf_init(&wd.out,1024);2277strbuf_init(&wd.sb_stat,1024);2278strbuf_init(&wd.sb_sha1,1024);2279write_one_dir(untracked->root, &wd);22802281 varint_len =encode_varint(wd.index, varbuf);2282strbuf_add(out, varbuf, varint_len);2283strbuf_addbuf(out, &wd.out);2284ewah_serialize_strbuf(wd.valid, out);2285ewah_serialize_strbuf(wd.check_only, out);2286ewah_serialize_strbuf(wd.sha1_valid, out);2287strbuf_addbuf(out, &wd.sb_stat);2288strbuf_addbuf(out, &wd.sb_sha1);2289strbuf_addch(out,'\0');/* safe guard for string lists */22902291ewah_free(wd.valid);2292ewah_free(wd.check_only);2293ewah_free(wd.sha1_valid);2294strbuf_release(&wd.out);2295strbuf_release(&wd.sb_stat);2296strbuf_release(&wd.sb_sha1);2297}22982299static voidfree_untracked(struct untracked_cache_dir *ucd)2300{2301int i;2302if(!ucd)2303return;2304for(i =0; i < ucd->dirs_nr; i++)2305free_untracked(ucd->dirs[i]);2306for(i =0; i < ucd->untracked_nr; i++)2307free(ucd->untracked[i]);2308free(ucd->untracked);2309free(ucd->dirs);2310free(ucd);2311}23122313voidfree_untracked_cache(struct untracked_cache *uc)2314{2315if(uc)2316free_untracked(uc->root);2317free(uc);2318}23192320struct read_data {2321int index;2322struct untracked_cache_dir **ucd;2323struct ewah_bitmap *check_only;2324struct ewah_bitmap *valid;2325struct ewah_bitmap *sha1_valid;2326const unsigned char*data;2327const unsigned char*end;2328};23292330static voidstat_data_from_disk(struct stat_data *to,const struct stat_data *from)2331{2332 to->sd_ctime.sec =get_be32(&from->sd_ctime.sec);2333 to->sd_ctime.nsec =get_be32(&from->sd_ctime.nsec);2334 to->sd_mtime.sec =get_be32(&from->sd_mtime.sec);2335 to->sd_mtime.nsec =get_be32(&from->sd_mtime.nsec);2336 to->sd_dev =get_be32(&from->sd_dev);2337 to->sd_ino =get_be32(&from->sd_ino);2338 to->sd_uid =get_be32(&from->sd_uid);2339 to->sd_gid =get_be32(&from->sd_gid);2340 to->sd_size =get_be32(&from->sd_size);2341}23422343static intread_one_dir(struct untracked_cache_dir **untracked_,2344struct read_data *rd)2345{2346struct untracked_cache_dir ud, *untracked;2347const unsigned char*next, *data = rd->data, *end = rd->end;2348unsigned int value;2349int i, len;23502351memset(&ud,0,sizeof(ud));23522353 next = data;2354 value =decode_varint(&next);2355if(next > end)2356return-1;2357 ud.recurse =1;2358 ud.untracked_alloc = value;2359 ud.untracked_nr = value;2360if(ud.untracked_nr)2361 ud.untracked =xmalloc(sizeof(*ud.untracked) * ud.untracked_nr);2362 data = next;23632364 next = data;2365 ud.dirs_alloc = ud.dirs_nr =decode_varint(&next);2366if(next > end)2367return-1;2368 ud.dirs =xmalloc(sizeof(*ud.dirs) * ud.dirs_nr);2369 data = next;23702371 len =strlen((const char*)data);2372 next = data + len +1;2373if(next > rd->end)2374return-1;2375*untracked_ = untracked =xmalloc(sizeof(*untracked) + len);2376memcpy(untracked, &ud,sizeof(ud));2377memcpy(untracked->name, data, len +1);2378 data = next;23792380for(i =0; i < untracked->untracked_nr; i++) {2381 len =strlen((const char*)data);2382 next = data + len +1;2383if(next > rd->end)2384return-1;2385 untracked->untracked[i] =xstrdup((const char*)data);2386 data = next;2387}23882389 rd->ucd[rd->index++] = untracked;2390 rd->data = data;23912392for(i =0; i < untracked->dirs_nr; i++) {2393 len =read_one_dir(untracked->dirs + i, rd);2394if(len <0)2395return-1;2396}2397return0;2398}23992400static voidset_check_only(size_t pos,void*cb)2401{2402struct read_data *rd = cb;2403struct untracked_cache_dir *ud = rd->ucd[pos];2404 ud->check_only =1;2405}24062407static voidread_stat(size_t pos,void*cb)2408{2409struct read_data *rd = cb;2410struct untracked_cache_dir *ud = rd->ucd[pos];2411if(rd->data +sizeof(struct stat_data) > rd->end) {2412 rd->data = rd->end +1;2413return;2414}2415stat_data_from_disk(&ud->stat_data, (struct stat_data *)rd->data);2416 rd->data +=sizeof(struct stat_data);2417 ud->valid =1;2418}24192420static voidread_sha1(size_t pos,void*cb)2421{2422struct read_data *rd = cb;2423struct untracked_cache_dir *ud = rd->ucd[pos];2424if(rd->data +20> rd->end) {2425 rd->data = rd->end +1;2426return;2427}2428hashcpy(ud->exclude_sha1, rd->data);2429 rd->data +=20;2430}24312432static voidload_sha1_stat(struct sha1_stat *sha1_stat,2433const struct stat_data *stat,2434const unsigned char*sha1)2435{2436stat_data_from_disk(&sha1_stat->stat, stat);2437hashcpy(sha1_stat->sha1, sha1);2438 sha1_stat->valid =1;2439}24402441struct untracked_cache *read_untracked_extension(const void*data,unsigned long sz)2442{2443const struct ondisk_untracked_cache *ouc;2444struct untracked_cache *uc;2445struct read_data rd;2446const unsigned char*next = data, *end = (const unsigned char*)data + sz;2447int len;24482449if(sz <=1|| end[-1] !='\0')2450return NULL;2451 end--;24522453 ouc = (const struct ondisk_untracked_cache *)next;2454if(next +ouc_size(0) > end)2455return NULL;24562457 uc =xcalloc(1,sizeof(*uc));2458load_sha1_stat(&uc->ss_info_exclude, &ouc->info_exclude_stat,2459 ouc->info_exclude_sha1);2460load_sha1_stat(&uc->ss_excludes_file, &ouc->excludes_file_stat,2461 ouc->excludes_file_sha1);2462 uc->dir_flags =get_be32(&ouc->dir_flags);2463 uc->exclude_per_dir =xstrdup(ouc->exclude_per_dir);2464/* NUL after exclude_per_dir is covered by sizeof(*ouc) */2465 next +=ouc_size(strlen(ouc->exclude_per_dir));2466if(next >= end)2467goto done2;24682469 len =decode_varint(&next);2470if(next > end || len ==0)2471goto done2;24722473 rd.valid =ewah_new();2474 rd.check_only =ewah_new();2475 rd.sha1_valid =ewah_new();2476 rd.data = next;2477 rd.end = end;2478 rd.index =0;2479 rd.ucd =xmalloc(sizeof(*rd.ucd) * len);24802481if(read_one_dir(&uc->root, &rd) || rd.index != len)2482goto done;24832484 next = rd.data;2485 len =ewah_read_mmap(rd.valid, next, end - next);2486if(len <0)2487goto done;24882489 next += len;2490 len =ewah_read_mmap(rd.check_only, next, end - next);2491if(len <0)2492goto done;24932494 next += len;2495 len =ewah_read_mmap(rd.sha1_valid, next, end - next);2496if(len <0)2497goto done;24982499ewah_each_bit(rd.check_only, set_check_only, &rd);2500 rd.data = next + len;2501ewah_each_bit(rd.valid, read_stat, &rd);2502ewah_each_bit(rd.sha1_valid, read_sha1, &rd);2503 next = rd.data;25042505done:2506free(rd.ucd);2507ewah_free(rd.valid);2508ewah_free(rd.check_only);2509ewah_free(rd.sha1_valid);2510done2:2511if(next != end) {2512free_untracked_cache(uc);2513 uc = NULL;2514}2515return uc;2516}25172518voiduntracked_cache_invalidate_path(struct index_state *istate,2519const char*path)2520{2521const char*sep;2522struct untracked_cache_dir *d;2523if(!istate->untracked || !istate->untracked->root)2524return;2525 sep =strrchr(path,'/');2526if(sep)2527 d =lookup_untracked(istate->untracked,2528 istate->untracked->root,2529 path, sep - path);2530else2531 d = istate->untracked->root;2532 istate->untracked->dir_invalidated++;2533 d->valid =0;2534 d->untracked_nr =0;2535}25362537voiduntracked_cache_remove_from_index(struct index_state *istate,2538const char*path)2539{2540untracked_cache_invalidate_path(istate, path);2541}25422543voiduntracked_cache_add_to_index(struct index_state *istate,2544const char*path)2545{2546untracked_cache_invalidate_path(istate, path);2547}