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 16struct path_simplify { 17int len; 18const char*path; 19}; 20 21/* 22 * Tells read_directory_recursive how a file or directory should be treated. 23 * Values are ordered by significance, e.g. if a directory contains both 24 * excluded and untracked files, it is listed as untracked because 25 * path_untracked > path_excluded. 26 */ 27enum path_treatment { 28 path_none =0, 29 path_recurse, 30 path_excluded, 31 path_untracked 32}; 33 34static enum path_treatment read_directory_recursive(struct dir_struct *dir, 35const char*path,int len,struct untracked_cache_dir *untracked, 36int check_only,const struct path_simplify *simplify); 37static intget_dtype(struct dirent *de,const char*path,int len); 38 39/* helper string functions with support for the ignore_case flag */ 40intstrcmp_icase(const char*a,const char*b) 41{ 42return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 43} 44 45intstrncmp_icase(const char*a,const char*b,size_t count) 46{ 47return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 48} 49 50intfnmatch_icase(const char*pattern,const char*string,int flags) 51{ 52returnwildmatch(pattern, string, 53 flags | (ignore_case ? WM_CASEFOLD :0), 54 NULL); 55} 56 57intgit_fnmatch(const struct pathspec_item *item, 58const char*pattern,const char*string, 59int prefix) 60{ 61if(prefix >0) { 62if(ps_strncmp(item, pattern, string, prefix)) 63return WM_NOMATCH; 64 pattern += prefix; 65 string += prefix; 66} 67if(item->flags & PATHSPEC_ONESTAR) { 68int pattern_len =strlen(++pattern); 69int string_len =strlen(string); 70return string_len < pattern_len || 71ps_strcmp(item, pattern, 72 string + string_len - pattern_len); 73} 74if(item->magic & PATHSPEC_GLOB) 75returnwildmatch(pattern, string, 76 WM_PATHNAME | 77(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0), 78 NULL); 79else 80/* wildmatch has not learned no FNM_PATHNAME mode yet */ 81returnwildmatch(pattern, string, 82 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0, 83 NULL); 84} 85 86static intfnmatch_icase_mem(const char*pattern,int patternlen, 87const char*string,int stringlen, 88int flags) 89{ 90int match_status; 91struct strbuf pat_buf = STRBUF_INIT; 92struct strbuf str_buf = STRBUF_INIT; 93const char*use_pat = pattern; 94const char*use_str = string; 95 96if(pattern[patternlen]) { 97strbuf_add(&pat_buf, pattern, patternlen); 98 use_pat = pat_buf.buf; 99} 100if(string[stringlen]) { 101strbuf_add(&str_buf, string, stringlen); 102 use_str = str_buf.buf; 103} 104 105if(ignore_case) 106 flags |= WM_CASEFOLD; 107 match_status =wildmatch(use_pat, use_str, flags, NULL); 108 109strbuf_release(&pat_buf); 110strbuf_release(&str_buf); 111 112return match_status; 113} 114 115static size_tcommon_prefix_len(const struct pathspec *pathspec) 116{ 117int n; 118size_t max =0; 119 120/* 121 * ":(icase)path" is treated as a pathspec full of 122 * wildcard. In other words, only prefix is considered common 123 * prefix. If the pathspec is abc/foo abc/bar, running in 124 * subdir xyz, the common prefix is still xyz, not xuz/abc as 125 * in non-:(icase). 126 */ 127GUARD_PATHSPEC(pathspec, 128 PATHSPEC_FROMTOP | 129 PATHSPEC_MAXDEPTH | 130 PATHSPEC_LITERAL | 131 PATHSPEC_GLOB | 132 PATHSPEC_ICASE | 133 PATHSPEC_EXCLUDE); 134 135for(n =0; n < pathspec->nr; n++) { 136size_t i =0, len =0, item_len; 137if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 138continue; 139if(pathspec->items[n].magic & PATHSPEC_ICASE) 140 item_len = pathspec->items[n].prefix; 141else 142 item_len = pathspec->items[n].nowildcard_len; 143while(i < item_len && (n ==0|| i < max)) { 144char c = pathspec->items[n].match[i]; 145if(c != pathspec->items[0].match[i]) 146break; 147if(c =='/') 148 len = i +1; 149 i++; 150} 151if(n ==0|| len < max) { 152 max = len; 153if(!max) 154break; 155} 156} 157return max; 158} 159 160/* 161 * Returns a copy of the longest leading path common among all 162 * pathspecs. 163 */ 164char*common_prefix(const struct pathspec *pathspec) 165{ 166unsigned long len =common_prefix_len(pathspec); 167 168return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 169} 170 171intfill_directory(struct dir_struct *dir,const struct pathspec *pathspec) 172{ 173size_t len; 174 175/* 176 * Calculate common prefix for the pathspec, and 177 * use that to optimize the directory walk 178 */ 179 len =common_prefix_len(pathspec); 180 181/* Read the directory and prune it */ 182read_directory(dir, pathspec->nr ? pathspec->_raw[0] :"", len, pathspec); 183return len; 184} 185 186intwithin_depth(const char*name,int namelen, 187int depth,int max_depth) 188{ 189const char*cp = name, *cpe = name + namelen; 190 191while(cp < cpe) { 192if(*cp++ !='/') 193continue; 194 depth++; 195if(depth > max_depth) 196return0; 197} 198return1; 199} 200 201#define DO_MATCH_EXCLUDE 1 202#define DO_MATCH_DIRECTORY 2 203 204/* 205 * Does 'match' match the given name? 206 * A match is found if 207 * 208 * (1) the 'match' string is leading directory of 'name', or 209 * (2) the 'match' string is a wildcard and matches 'name', or 210 * (3) the 'match' string is exactly the same as 'name'. 211 * 212 * and the return value tells which case it was. 213 * 214 * It returns 0 when there is no match. 215 */ 216static intmatch_pathspec_item(const struct pathspec_item *item,int prefix, 217const char*name,int namelen,unsigned flags) 218{ 219/* name/namelen has prefix cut off by caller */ 220const char*match = item->match + prefix; 221int matchlen = item->len - prefix; 222 223/* 224 * The normal call pattern is: 225 * 1. prefix = common_prefix_len(ps); 226 * 2. prune something, or fill_directory 227 * 3. match_pathspec() 228 * 229 * 'prefix' at #1 may be shorter than the command's prefix and 230 * it's ok for #2 to match extra files. Those extras will be 231 * trimmed at #3. 232 * 233 * Suppose the pathspec is 'foo' and '../bar' running from 234 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 235 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 236 * user does not want XYZ/foo, only the "foo" part should be 237 * case-insensitive. We need to filter out XYZ/foo here. In 238 * other words, we do not trust the caller on comparing the 239 * prefix part when :(icase) is involved. We do exact 240 * comparison ourselves. 241 * 242 * Normally the caller (common_prefix_len() in fact) does 243 * _exact_ matching on name[-prefix+1..-1] and we do not need 244 * to check that part. Be defensive and check it anyway, in 245 * case common_prefix_len is changed, or a new caller is 246 * introduced that does not use common_prefix_len. 247 * 248 * If the penalty turns out too high when prefix is really 249 * long, maybe change it to 250 * strncmp(match, name, item->prefix - prefix) 251 */ 252if(item->prefix && (item->magic & PATHSPEC_ICASE) && 253strncmp(item->match, name - prefix, item->prefix)) 254return0; 255 256/* If the match was just the prefix, we matched */ 257if(!*match) 258return MATCHED_RECURSIVELY; 259 260if(matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 261if(matchlen == namelen) 262return MATCHED_EXACTLY; 263 264if(match[matchlen-1] =='/'|| name[matchlen] =='/') 265return MATCHED_RECURSIVELY; 266}else if((flags & DO_MATCH_DIRECTORY) && 267 match[matchlen -1] =='/'&& 268 namelen == matchlen -1&& 269!ps_strncmp(item, match, name, namelen)) 270return MATCHED_EXACTLY; 271 272if(item->nowildcard_len < item->len && 273!git_fnmatch(item, match, name, 274 item->nowildcard_len - prefix)) 275return MATCHED_FNMATCH; 276 277return0; 278} 279 280/* 281 * Given a name and a list of pathspecs, returns the nature of the 282 * closest (i.e. most specific) match of the name to any of the 283 * pathspecs. 284 * 285 * The caller typically calls this multiple times with the same 286 * pathspec and seen[] array but with different name/namelen 287 * (e.g. entries from the index) and is interested in seeing if and 288 * how each pathspec matches all the names it calls this function 289 * with. A mark is left in the seen[] array for each pathspec element 290 * indicating the closest type of match that element achieved, so if 291 * seen[n] remains zero after multiple invocations, that means the nth 292 * pathspec did not match any names, which could indicate that the 293 * user mistyped the nth pathspec. 294 */ 295static intdo_match_pathspec(const struct pathspec *ps, 296const char*name,int namelen, 297int prefix,char*seen, 298unsigned flags) 299{ 300int i, retval =0, exclude = flags & DO_MATCH_EXCLUDE; 301 302GUARD_PATHSPEC(ps, 303 PATHSPEC_FROMTOP | 304 PATHSPEC_MAXDEPTH | 305 PATHSPEC_LITERAL | 306 PATHSPEC_GLOB | 307 PATHSPEC_ICASE | 308 PATHSPEC_EXCLUDE); 309 310if(!ps->nr) { 311if(!ps->recursive || 312!(ps->magic & PATHSPEC_MAXDEPTH) || 313 ps->max_depth == -1) 314return MATCHED_RECURSIVELY; 315 316if(within_depth(name, namelen,0, ps->max_depth)) 317return MATCHED_EXACTLY; 318else 319return0; 320} 321 322 name += prefix; 323 namelen -= prefix; 324 325for(i = ps->nr -1; i >=0; i--) { 326int how; 327 328if((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 329( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 330continue; 331 332if(seen && seen[i] == MATCHED_EXACTLY) 333continue; 334/* 335 * Make exclude patterns optional and never report 336 * "pathspec ':(exclude)foo' matches no files" 337 */ 338if(seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 339 seen[i] = MATCHED_FNMATCH; 340 how =match_pathspec_item(ps->items+i, prefix, name, 341 namelen, flags); 342if(ps->recursive && 343(ps->magic & PATHSPEC_MAXDEPTH) && 344 ps->max_depth != -1&& 345 how && how != MATCHED_FNMATCH) { 346int len = ps->items[i].len; 347if(name[len] =='/') 348 len++; 349if(within_depth(name+len, namelen-len,0, ps->max_depth)) 350 how = MATCHED_EXACTLY; 351else 352 how =0; 353} 354if(how) { 355if(retval < how) 356 retval = how; 357if(seen && seen[i] < how) 358 seen[i] = how; 359} 360} 361return retval; 362} 363 364intmatch_pathspec(const struct pathspec *ps, 365const char*name,int namelen, 366int prefix,char*seen,int is_dir) 367{ 368int positive, negative; 369unsigned flags = is_dir ? DO_MATCH_DIRECTORY :0; 370 positive =do_match_pathspec(ps, name, namelen, 371 prefix, seen, flags); 372if(!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 373return positive; 374 negative =do_match_pathspec(ps, name, namelen, 375 prefix, seen, 376 flags | DO_MATCH_EXCLUDE); 377return negative ?0: positive; 378} 379 380/* 381 * Return the length of the "simple" part of a path match limiter. 382 */ 383intsimple_length(const char*match) 384{ 385int len = -1; 386 387for(;;) { 388unsigned char c = *match++; 389 len++; 390if(c =='\0'||is_glob_special(c)) 391return len; 392} 393} 394 395intno_wildcard(const char*string) 396{ 397return string[simple_length(string)] =='\0'; 398} 399 400voidparse_exclude_pattern(const char**pattern, 401int*patternlen, 402int*flags, 403int*nowildcardlen) 404{ 405const char*p = *pattern; 406size_t i, len; 407 408*flags =0; 409if(*p =='!') { 410*flags |= EXC_FLAG_NEGATIVE; 411 p++; 412} 413 len =strlen(p); 414if(len && p[len -1] =='/') { 415 len--; 416*flags |= EXC_FLAG_MUSTBEDIR; 417} 418for(i =0; i < len; i++) { 419if(p[i] =='/') 420break; 421} 422if(i == len) 423*flags |= EXC_FLAG_NODIR; 424*nowildcardlen =simple_length(p); 425/* 426 * we should have excluded the trailing slash from 'p' too, 427 * but that's one more allocation. Instead just make sure 428 * nowildcardlen does not exceed real patternlen 429 */ 430if(*nowildcardlen > len) 431*nowildcardlen = len; 432if(*p =='*'&&no_wildcard(p +1)) 433*flags |= EXC_FLAG_ENDSWITH; 434*pattern = p; 435*patternlen = len; 436} 437 438voidadd_exclude(const char*string,const char*base, 439int baselen,struct exclude_list *el,int srcpos) 440{ 441struct exclude *x; 442int patternlen; 443int flags; 444int nowildcardlen; 445 446parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 447if(flags & EXC_FLAG_MUSTBEDIR) { 448char*s; 449 x =xmalloc(sizeof(*x) + patternlen +1); 450 s = (char*)(x+1); 451memcpy(s, string, patternlen); 452 s[patternlen] ='\0'; 453 x->pattern = s; 454}else{ 455 x =xmalloc(sizeof(*x)); 456 x->pattern = string; 457} 458 x->patternlen = patternlen; 459 x->nowildcardlen = nowildcardlen; 460 x->base = base; 461 x->baselen = baselen; 462 x->flags = flags; 463 x->srcpos = srcpos; 464ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 465 el->excludes[el->nr++] = x; 466 x->el = el; 467} 468 469static void*read_skip_worktree_file_from_index(const char*path,size_t*size, 470struct sha1_stat *sha1_stat) 471{ 472int pos, len; 473unsigned long sz; 474enum object_type type; 475void*data; 476 477 len =strlen(path); 478 pos =cache_name_pos(path, len); 479if(pos <0) 480return NULL; 481if(!ce_skip_worktree(active_cache[pos])) 482return NULL; 483 data =read_sha1_file(active_cache[pos]->sha1, &type, &sz); 484if(!data || type != OBJ_BLOB) { 485free(data); 486return NULL; 487} 488*size =xsize_t(sz); 489if(sha1_stat) { 490memset(&sha1_stat->stat,0,sizeof(sha1_stat->stat)); 491hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); 492} 493return data; 494} 495 496/* 497 * Frees memory within el which was allocated for exclude patterns and 498 * the file buffer. Does not free el itself. 499 */ 500voidclear_exclude_list(struct exclude_list *el) 501{ 502int i; 503 504for(i =0; i < el->nr; i++) 505free(el->excludes[i]); 506free(el->excludes); 507free(el->filebuf); 508 509 el->nr =0; 510 el->excludes = NULL; 511 el->filebuf = NULL; 512} 513 514static voidtrim_trailing_spaces(char*buf) 515{ 516char*p, *last_space = NULL; 517 518for(p = buf; *p; p++) 519switch(*p) { 520case' ': 521if(!last_space) 522 last_space = p; 523break; 524case'\\': 525 p++; 526if(!*p) 527return; 528/* fallthrough */ 529default: 530 last_space = NULL; 531} 532 533if(last_space) 534*last_space ='\0'; 535} 536 537/* 538 * Given a subdirectory name and "dir" of the current directory, 539 * search the subdir in "dir" and return it, or create a new one if it 540 * does not exist in "dir". 541 * 542 * If "name" has the trailing slash, it'll be excluded in the search. 543 */ 544static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, 545struct untracked_cache_dir *dir, 546const char*name,int len) 547{ 548int first, last; 549struct untracked_cache_dir *d; 550if(!dir) 551return NULL; 552if(len && name[len -1] =='/') 553 len--; 554 first =0; 555 last = dir->dirs_nr; 556while(last > first) { 557int cmp, next = (last + first) >>1; 558 d = dir->dirs[next]; 559 cmp =strncmp(name, d->name, len); 560if(!cmp &&strlen(d->name) > len) 561 cmp = -1; 562if(!cmp) 563return d; 564if(cmp <0) { 565 last = next; 566continue; 567} 568 first = next+1; 569} 570 571 uc->dir_created++; 572 d =xmalloc(sizeof(*d) + len +1); 573memset(d,0,sizeof(*d)); 574memcpy(d->name, name, len); 575 d->name[len] ='\0'; 576 577ALLOC_GROW(dir->dirs, dir->dirs_nr +1, dir->dirs_alloc); 578memmove(dir->dirs + first +1, dir->dirs + first, 579(dir->dirs_nr - first) *sizeof(*dir->dirs)); 580 dir->dirs_nr++; 581 dir->dirs[first] = d; 582return d; 583} 584 585/* 586 * Given a file with name "fname", read it (either from disk, or from 587 * the index if "check_index" is non-zero), parse it and store the 588 * exclude rules in "el". 589 * 590 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill 591 * stat data from disk (only valid if add_excludes returns zero). If 592 * ss_valid is non-zero, "ss" must contain good value as input. 593 */ 594static intadd_excludes(const char*fname,const char*base,int baselen, 595struct exclude_list *el,int check_index, 596struct sha1_stat *sha1_stat) 597{ 598struct stat st; 599int fd, i, lineno =1; 600size_t size =0; 601char*buf, *entry; 602 603 fd =open(fname, O_RDONLY); 604if(fd <0||fstat(fd, &st) <0) { 605if(errno != ENOENT) 606warn_on_inaccessible(fname); 607if(0<= fd) 608close(fd); 609if(!check_index || 610(buf =read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL) 611return-1; 612if(size ==0) { 613free(buf); 614return0; 615} 616if(buf[size-1] !='\n') { 617 buf =xrealloc(buf, size+1); 618 buf[size++] ='\n'; 619} 620}else{ 621 size =xsize_t(st.st_size); 622if(size ==0) { 623if(sha1_stat) { 624fill_stat_data(&sha1_stat->stat, &st); 625hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN); 626 sha1_stat->valid =1; 627} 628close(fd); 629return0; 630} 631 buf =xmalloc(size+1); 632if(read_in_full(fd, buf, size) != size) { 633free(buf); 634close(fd); 635return-1; 636} 637 buf[size++] ='\n'; 638close(fd); 639if(sha1_stat) { 640int pos; 641if(sha1_stat->valid && 642!match_stat_data(&sha1_stat->stat, &st)) 643;/* no content change, ss->sha1 still good */ 644else if(check_index && 645(pos =cache_name_pos(fname,strlen(fname))) >=0&& 646!ce_stage(active_cache[pos]) && 647ce_uptodate(active_cache[pos]) && 648!would_convert_to_git(fname)) 649hashcpy(sha1_stat->sha1, active_cache[pos]->sha1); 650else 651hash_sha1_file(buf, size,"blob", sha1_stat->sha1); 652fill_stat_data(&sha1_stat->stat, &st); 653 sha1_stat->valid =1; 654} 655} 656 657 el->filebuf = buf; 658 entry = buf; 659for(i =0; i < size; i++) { 660if(buf[i] =='\n') { 661if(entry != buf + i && entry[0] !='#') { 662 buf[i - (i && buf[i-1] =='\r')] =0; 663trim_trailing_spaces(entry); 664add_exclude(entry, base, baselen, el, lineno); 665} 666 lineno++; 667 entry = buf + i +1; 668} 669} 670return0; 671} 672 673intadd_excludes_from_file_to_list(const char*fname,const char*base, 674int baselen,struct exclude_list *el, 675int check_index) 676{ 677returnadd_excludes(fname, base, baselen, el, check_index, NULL); 678} 679 680struct exclude_list *add_exclude_list(struct dir_struct *dir, 681int group_type,const char*src) 682{ 683struct exclude_list *el; 684struct exclude_list_group *group; 685 686 group = &dir->exclude_list_group[group_type]; 687ALLOC_GROW(group->el, group->nr +1, group->alloc); 688 el = &group->el[group->nr++]; 689memset(el,0,sizeof(*el)); 690 el->src = src; 691return el; 692} 693 694/* 695 * Used to set up core.excludesfile and .git/info/exclude lists. 696 */ 697static voidadd_excludes_from_file_1(struct dir_struct *dir,const char*fname, 698struct sha1_stat *sha1_stat) 699{ 700struct exclude_list *el; 701 el =add_exclude_list(dir, EXC_FILE, fname); 702if(add_excludes(fname,"",0, el,0, sha1_stat) <0) 703die("cannot use%sas an exclude file", fname); 704} 705 706voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 707{ 708add_excludes_from_file_1(dir, fname, NULL); 709} 710 711intmatch_basename(const char*basename,int basenamelen, 712const char*pattern,int prefix,int patternlen, 713int flags) 714{ 715if(prefix == patternlen) { 716if(patternlen == basenamelen && 717!strncmp_icase(pattern, basename, basenamelen)) 718return1; 719}else if(flags & EXC_FLAG_ENDSWITH) { 720/* "*literal" matching against "fooliteral" */ 721if(patternlen -1<= basenamelen && 722!strncmp_icase(pattern +1, 723 basename + basenamelen - (patternlen -1), 724 patternlen -1)) 725return1; 726}else{ 727if(fnmatch_icase_mem(pattern, patternlen, 728 basename, basenamelen, 7290) ==0) 730return1; 731} 732return0; 733} 734 735intmatch_pathname(const char*pathname,int pathlen, 736const char*base,int baselen, 737const char*pattern,int prefix,int patternlen, 738int flags) 739{ 740const char*name; 741int namelen; 742 743/* 744 * match with FNM_PATHNAME; the pattern has base implicitly 745 * in front of it. 746 */ 747if(*pattern =='/') { 748 pattern++; 749 patternlen--; 750 prefix--; 751} 752 753/* 754 * baselen does not count the trailing slash. base[] may or 755 * may not end with a trailing slash though. 756 */ 757if(pathlen < baselen +1|| 758(baselen && pathname[baselen] !='/') || 759strncmp_icase(pathname, base, baselen)) 760return0; 761 762 namelen = baselen ? pathlen - baselen -1: pathlen; 763 name = pathname + pathlen - namelen; 764 765if(prefix) { 766/* 767 * if the non-wildcard part is longer than the 768 * remaining pathname, surely it cannot match. 769 */ 770if(prefix > namelen) 771return0; 772 773if(strncmp_icase(pattern, name, prefix)) 774return0; 775 pattern += prefix; 776 patternlen -= prefix; 777 name += prefix; 778 namelen -= prefix; 779 780/* 781 * If the whole pattern did not have a wildcard, 782 * then our prefix match is all we need; we 783 * do not need to call fnmatch at all. 784 */ 785if(!patternlen && !namelen) 786return1; 787} 788 789returnfnmatch_icase_mem(pattern, patternlen, 790 name, namelen, 791 WM_PATHNAME) ==0; 792} 793 794/* 795 * Scan the given exclude list in reverse to see whether pathname 796 * should be ignored. The first match (i.e. the last on the list), if 797 * any, determines the fate. Returns the exclude_list element which 798 * matched, or NULL for undecided. 799 */ 800static struct exclude *last_exclude_matching_from_list(const char*pathname, 801int pathlen, 802const char*basename, 803int*dtype, 804struct exclude_list *el) 805{ 806int i; 807 808if(!el->nr) 809return NULL;/* undefined */ 810 811for(i = el->nr -1;0<= i; i--) { 812struct exclude *x = el->excludes[i]; 813const char*exclude = x->pattern; 814int prefix = x->nowildcardlen; 815 816if(x->flags & EXC_FLAG_MUSTBEDIR) { 817if(*dtype == DT_UNKNOWN) 818*dtype =get_dtype(NULL, pathname, pathlen); 819if(*dtype != DT_DIR) 820continue; 821} 822 823if(x->flags & EXC_FLAG_NODIR) { 824if(match_basename(basename, 825 pathlen - (basename - pathname), 826 exclude, prefix, x->patternlen, 827 x->flags)) 828return x; 829continue; 830} 831 832assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 833if(match_pathname(pathname, pathlen, 834 x->base, x->baselen ? x->baselen -1:0, 835 exclude, prefix, x->patternlen, x->flags)) 836return x; 837} 838return NULL;/* undecided */ 839} 840 841/* 842 * Scan the list and let the last match determine the fate. 843 * Return 1 for exclude, 0 for include and -1 for undecided. 844 */ 845intis_excluded_from_list(const char*pathname, 846int pathlen,const char*basename,int*dtype, 847struct exclude_list *el) 848{ 849struct exclude *exclude; 850 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 851if(exclude) 852return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 853return-1;/* undecided */ 854} 855 856static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 857const char*pathname,int pathlen,const char*basename, 858int*dtype_p) 859{ 860int i, j; 861struct exclude_list_group *group; 862struct exclude *exclude; 863for(i = EXC_CMDL; i <= EXC_FILE; i++) { 864 group = &dir->exclude_list_group[i]; 865for(j = group->nr -1; j >=0; j--) { 866 exclude =last_exclude_matching_from_list( 867 pathname, pathlen, basename, dtype_p, 868&group->el[j]); 869if(exclude) 870return exclude; 871} 872} 873return NULL; 874} 875 876/* 877 * Loads the per-directory exclude list for the substring of base 878 * which has a char length of baselen. 879 */ 880static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 881{ 882struct exclude_list_group *group; 883struct exclude_list *el; 884struct exclude_stack *stk = NULL; 885struct untracked_cache_dir *untracked; 886int current; 887 888 group = &dir->exclude_list_group[EXC_DIRS]; 889 890/* 891 * Pop the exclude lists from the EXCL_DIRS exclude_list_group 892 * which originate from directories not in the prefix of the 893 * path being checked. 894 */ 895while((stk = dir->exclude_stack) != NULL) { 896if(stk->baselen <= baselen && 897!strncmp(dir->basebuf.buf, base, stk->baselen)) 898break; 899 el = &group->el[dir->exclude_stack->exclude_ix]; 900 dir->exclude_stack = stk->prev; 901 dir->exclude = NULL; 902free((char*)el->src);/* see strbuf_detach() below */ 903clear_exclude_list(el); 904free(stk); 905 group->nr--; 906} 907 908/* Skip traversing into sub directories if the parent is excluded */ 909if(dir->exclude) 910return; 911 912/* 913 * Lazy initialization. All call sites currently just 914 * memset(dir, 0, sizeof(*dir)) before use. Changing all of 915 * them seems lots of work for little benefit. 916 */ 917if(!dir->basebuf.buf) 918strbuf_init(&dir->basebuf, PATH_MAX); 919 920/* Read from the parent directories and push them down. */ 921 current = stk ? stk->baselen : -1; 922strbuf_setlen(&dir->basebuf, current <0?0: current); 923if(dir->untracked) 924 untracked = stk ? stk->ucd : dir->untracked->root; 925else 926 untracked = NULL; 927 928while(current < baselen) { 929const char*cp; 930struct sha1_stat sha1_stat; 931 932 stk =xcalloc(1,sizeof(*stk)); 933if(current <0) { 934 cp = base; 935 current =0; 936}else{ 937 cp =strchr(base + current +1,'/'); 938if(!cp) 939die("oops in prep_exclude"); 940 cp++; 941 untracked = 942lookup_untracked(dir->untracked, untracked, 943 base + current, 944 cp - base - current); 945} 946 stk->prev = dir->exclude_stack; 947 stk->baselen = cp - base; 948 stk->exclude_ix = group->nr; 949 stk->ucd = untracked; 950 el =add_exclude_list(dir, EXC_DIRS, NULL); 951strbuf_add(&dir->basebuf, base + current, stk->baselen - current); 952assert(stk->baselen == dir->basebuf.len); 953 954/* Abort if the directory is excluded */ 955if(stk->baselen) { 956int dt = DT_DIR; 957 dir->basebuf.buf[stk->baselen -1] =0; 958 dir->exclude =last_exclude_matching_from_lists(dir, 959 dir->basebuf.buf, stk->baselen -1, 960 dir->basebuf.buf + current, &dt); 961 dir->basebuf.buf[stk->baselen -1] ='/'; 962if(dir->exclude && 963 dir->exclude->flags & EXC_FLAG_NEGATIVE) 964 dir->exclude = NULL; 965if(dir->exclude) { 966 dir->exclude_stack = stk; 967return; 968} 969} 970 971/* Try to read per-directory file */ 972hashclr(sha1_stat.sha1); 973 sha1_stat.valid =0; 974if(dir->exclude_per_dir) { 975/* 976 * dir->basebuf gets reused by the traversal, but we 977 * need fname to remain unchanged to ensure the src 978 * member of each struct exclude correctly 979 * back-references its source file. Other invocations 980 * of add_exclude_list provide stable strings, so we 981 * strbuf_detach() and free() here in the caller. 982 */ 983struct strbuf sb = STRBUF_INIT; 984strbuf_addbuf(&sb, &dir->basebuf); 985strbuf_addstr(&sb, dir->exclude_per_dir); 986 el->src =strbuf_detach(&sb, NULL); 987add_excludes(el->src, el->src, stk->baselen, el,1, 988 untracked ? &sha1_stat : NULL); 989} 990if(untracked) { 991hashcpy(untracked->exclude_sha1, sha1_stat.sha1); 992} 993 dir->exclude_stack = stk; 994 current = stk->baselen; 995} 996strbuf_setlen(&dir->basebuf, baselen); 997} 998 999/*1000 * Loads the exclude lists for the directory containing pathname, then1001 * scans all exclude lists to determine whether pathname is excluded.1002 * Returns the exclude_list element which matched, or NULL for1003 * undecided.1004 */1005struct exclude *last_exclude_matching(struct dir_struct *dir,1006const char*pathname,1007int*dtype_p)1008{1009int pathlen =strlen(pathname);1010const char*basename =strrchr(pathname,'/');1011 basename = (basename) ? basename+1: pathname;10121013prep_exclude(dir, pathname, basename-pathname);10141015if(dir->exclude)1016return dir->exclude;10171018returnlast_exclude_matching_from_lists(dir, pathname, pathlen,1019 basename, dtype_p);1020}10211022/*1023 * Loads the exclude lists for the directory containing pathname, then1024 * scans all exclude lists to determine whether pathname is excluded.1025 * Returns 1 if true, otherwise 0.1026 */1027intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p)1028{1029struct exclude *exclude =1030last_exclude_matching(dir, pathname, dtype_p);1031if(exclude)1032return exclude->flags & EXC_FLAG_NEGATIVE ?0:1;1033return0;1034}10351036static struct dir_entry *dir_entry_new(const char*pathname,int len)1037{1038struct dir_entry *ent;10391040 ent =xmalloc(sizeof(*ent) + len +1);1041 ent->len = len;1042memcpy(ent->name, pathname, len);1043 ent->name[len] =0;1044return ent;1045}10461047static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len)1048{1049if(cache_file_exists(pathname, len, ignore_case))1050return NULL;10511052ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);1053return dir->entries[dir->nr++] =dir_entry_new(pathname, len);1054}10551056struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len)1057{1058if(!cache_name_is_other(pathname, len))1059return NULL;10601061ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);1062return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len);1063}10641065enum exist_status {1066 index_nonexistent =0,1067 index_directory,1068 index_gitdir1069};10701071/*1072 * Do not use the alphabetically sorted index to look up1073 * the directory name; instead, use the case insensitive1074 * directory hash.1075 */1076static enum exist_status directory_exists_in_index_icase(const char*dirname,int len)1077{1078const struct cache_entry *ce =cache_dir_exists(dirname, len);1079unsigned char endchar;10801081if(!ce)1082return index_nonexistent;1083 endchar = ce->name[len];10841085/*1086 * The cache_entry structure returned will contain this dirname1087 * and possibly additional path components.1088 */1089if(endchar =='/')1090return index_directory;10911092/*1093 * If there are no additional path components, then this cache_entry1094 * represents a submodule. Submodules, despite being directories,1095 * are stored in the cache without a closing slash.1096 */1097if(!endchar &&S_ISGITLINK(ce->ce_mode))1098return index_gitdir;10991100/* This should never be hit, but it exists just in case. */1101return index_nonexistent;1102}11031104/*1105 * The index sorts alphabetically by entry name, which1106 * means that a gitlink sorts as '\0' at the end, while1107 * a directory (which is defined not as an entry, but as1108 * the files it contains) will sort with the '/' at the1109 * end.1110 */1111static enum exist_status directory_exists_in_index(const char*dirname,int len)1112{1113int pos;11141115if(ignore_case)1116returndirectory_exists_in_index_icase(dirname, len);11171118 pos =cache_name_pos(dirname, len);1119if(pos <0)1120 pos = -pos-1;1121while(pos < active_nr) {1122const struct cache_entry *ce = active_cache[pos++];1123unsigned char endchar;11241125if(strncmp(ce->name, dirname, len))1126break;1127 endchar = ce->name[len];1128if(endchar >'/')1129break;1130if(endchar =='/')1131return index_directory;1132if(!endchar &&S_ISGITLINK(ce->ce_mode))1133return index_gitdir;1134}1135return index_nonexistent;1136}11371138/*1139 * When we find a directory when traversing the filesystem, we1140 * have three distinct cases:1141 *1142 * - ignore it1143 * - see it as a directory1144 * - recurse into it1145 *1146 * and which one we choose depends on a combination of existing1147 * git index contents and the flags passed into the directory1148 * traversal routine.1149 *1150 * Case 1: If we *already* have entries in the index under that1151 * directory name, we always recurse into the directory to see1152 * all the files.1153 *1154 * Case 2: If we *already* have that directory name as a gitlink,1155 * we always continue to see it as a gitlink, regardless of whether1156 * there is an actual git directory there or not (it might not1157 * be checked out as a subproject!)1158 *1159 * Case 3: if we didn't have it in the index previously, we1160 * have a few sub-cases:1161 *1162 * (a) if "show_other_directories" is true, we show it as1163 * just a directory, unless "hide_empty_directories" is1164 * also true, in which case we need to check if it contains any1165 * untracked and / or ignored files.1166 * (b) if it looks like a git directory, and we don't have1167 * 'no_gitlinks' set we treat it as a gitlink, and show it1168 * as a directory.1169 * (c) otherwise, we recurse into it.1170 */1171static enum path_treatment treat_directory(struct dir_struct *dir,1172struct untracked_cache_dir *untracked,1173const char*dirname,int len,int exclude,1174const struct path_simplify *simplify)1175{1176/* The "len-1" is to strip the final '/' */1177switch(directory_exists_in_index(dirname, len-1)) {1178case index_directory:1179return path_recurse;11801181case index_gitdir:1182return path_none;11831184case index_nonexistent:1185if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1186break;1187if(!(dir->flags & DIR_NO_GITLINKS)) {1188unsigned char sha1[20];1189if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1190return path_untracked;1191}1192return path_recurse;1193}11941195/* This is the "show_other_directories" case */11961197if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1198return exclude ? path_excluded : path_untracked;11991200 untracked =lookup_untracked(dir->untracked, untracked, dirname, len);1201returnread_directory_recursive(dir, dirname, len,1202 untracked,1, simplify);1203}12041205/*1206 * This is an inexact early pruning of any recursive directory1207 * reading - if the path cannot possibly be in the pathspec,1208 * return true, and we'll skip it early.1209 */1210static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1211{1212if(simplify) {1213for(;;) {1214const char*match = simplify->path;1215int len = simplify->len;12161217if(!match)1218break;1219if(len > pathlen)1220 len = pathlen;1221if(!memcmp(path, match, len))1222return0;1223 simplify++;1224}1225return1;1226}1227return0;1228}12291230/*1231 * This function tells us whether an excluded path matches a1232 * list of "interesting" pathspecs. That is, whether a path matched1233 * by any of the pathspecs could possibly be ignored by excluding1234 * the specified path. This can happen if:1235 *1236 * 1. the path is mentioned explicitly in the pathspec1237 *1238 * 2. the path is a directory prefix of some element in the1239 * pathspec1240 */1241static intexclude_matches_pathspec(const char*path,int len,1242const struct path_simplify *simplify)1243{1244if(simplify) {1245for(; simplify->path; simplify++) {1246if(len == simplify->len1247&& !memcmp(path, simplify->path, len))1248return1;1249if(len < simplify->len1250&& simplify->path[len] =='/'1251&& !memcmp(path, simplify->path, len))1252return1;1253}1254}1255return0;1256}12571258static intget_index_dtype(const char*path,int len)1259{1260int pos;1261const struct cache_entry *ce;12621263 ce =cache_file_exists(path, len,0);1264if(ce) {1265if(!ce_uptodate(ce))1266return DT_UNKNOWN;1267if(S_ISGITLINK(ce->ce_mode))1268return DT_DIR;1269/*1270 * Nobody actually cares about the1271 * difference between DT_LNK and DT_REG1272 */1273return DT_REG;1274}12751276/* Try to look it up as a directory */1277 pos =cache_name_pos(path, len);1278if(pos >=0)1279return DT_UNKNOWN;1280 pos = -pos-1;1281while(pos < active_nr) {1282 ce = active_cache[pos++];1283if(strncmp(ce->name, path, len))1284break;1285if(ce->name[len] >'/')1286break;1287if(ce->name[len] <'/')1288continue;1289if(!ce_uptodate(ce))1290break;/* continue? */1291return DT_DIR;1292}1293return DT_UNKNOWN;1294}12951296static intget_dtype(struct dirent *de,const char*path,int len)1297{1298int dtype = de ?DTYPE(de) : DT_UNKNOWN;1299struct stat st;13001301if(dtype != DT_UNKNOWN)1302return dtype;1303 dtype =get_index_dtype(path, len);1304if(dtype != DT_UNKNOWN)1305return dtype;1306if(lstat(path, &st))1307return dtype;1308if(S_ISREG(st.st_mode))1309return DT_REG;1310if(S_ISDIR(st.st_mode))1311return DT_DIR;1312if(S_ISLNK(st.st_mode))1313return DT_LNK;1314return dtype;1315}13161317static enum path_treatment treat_one_path(struct dir_struct *dir,1318struct untracked_cache_dir *untracked,1319struct strbuf *path,1320const struct path_simplify *simplify,1321int dtype,struct dirent *de)1322{1323int exclude;1324int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);13251326if(dtype == DT_UNKNOWN)1327 dtype =get_dtype(de, path->buf, path->len);13281329/* Always exclude indexed files */1330if(dtype != DT_DIR && has_path_in_index)1331return path_none;13321333/*1334 * When we are looking at a directory P in the working tree,1335 * there are three cases:1336 *1337 * (1) P exists in the index. Everything inside the directory P in1338 * the working tree needs to go when P is checked out from the1339 * index.1340 *1341 * (2) P does not exist in the index, but there is P/Q in the index.1342 * We know P will stay a directory when we check out the contents1343 * of the index, but we do not know yet if there is a directory1344 * P/Q in the working tree to be killed, so we need to recurse.1345 *1346 * (3) P does not exist in the index, and there is no P/Q in the index1347 * to require P to be a directory, either. Only in this case, we1348 * know that everything inside P will not be killed without1349 * recursing.1350 */1351if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1352(dtype == DT_DIR) &&1353!has_path_in_index &&1354(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1355return path_none;13561357 exclude =is_excluded(dir, path->buf, &dtype);13581359/*1360 * Excluded? If we don't explicitly want to show1361 * ignored files, ignore it1362 */1363if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1364return path_excluded;13651366switch(dtype) {1367default:1368return path_none;1369case DT_DIR:1370strbuf_addch(path,'/');1371returntreat_directory(dir, untracked, path->buf, path->len, exclude,1372 simplify);1373case DT_REG:1374case DT_LNK:1375return exclude ? path_excluded : path_untracked;1376}1377}13781379static enum path_treatment treat_path(struct dir_struct *dir,1380struct untracked_cache_dir *untracked,1381struct dirent *de,1382struct strbuf *path,1383int baselen,1384const struct path_simplify *simplify)1385{1386int dtype;13871388if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1389return path_none;1390strbuf_setlen(path, baselen);1391strbuf_addstr(path, de->d_name);1392if(simplify_away(path->buf, path->len, simplify))1393return path_none;13941395 dtype =DTYPE(de);1396returntreat_one_path(dir, untracked, path, simplify, dtype, de);1397}13981399static voidadd_untracked(struct untracked_cache_dir *dir,const char*name)1400{1401if(!dir)1402return;1403ALLOC_GROW(dir->untracked, dir->untracked_nr +1,1404 dir->untracked_alloc);1405 dir->untracked[dir->untracked_nr++] =xstrdup(name);1406}14071408/*1409 * Read a directory tree. We currently ignore anything but1410 * directories, regular files and symlinks. That's because git1411 * doesn't handle them at all yet. Maybe that will change some1412 * day.1413 *1414 * Also, we ignore the name ".git" (even if it is not a directory).1415 * That likely will not change.1416 *1417 * Returns the most significant path_treatment value encountered in the scan.1418 */1419static enum path_treatment read_directory_recursive(struct dir_struct *dir,1420const char*base,int baselen,1421struct untracked_cache_dir *untracked,int check_only,1422const struct path_simplify *simplify)1423{1424DIR*fdir;1425enum path_treatment state, subdir_state, dir_state = path_none;1426struct dirent *de;1427struct strbuf path = STRBUF_INIT;14281429strbuf_add(&path, base, baselen);14301431 fdir =opendir(path.len ? path.buf :".");1432if(!fdir)1433goto out;14341435if(untracked)1436 untracked->check_only = !!check_only;14371438while((de =readdir(fdir)) != NULL) {1439/* check how the file or directory should be treated */1440 state =treat_path(dir, untracked, de, &path, baselen, simplify);14411442if(state > dir_state)1443 dir_state = state;14441445/* recurse into subdir if instructed by treat_path */1446if(state == path_recurse) {1447struct untracked_cache_dir *ud;1448 ud =lookup_untracked(dir->untracked, untracked,1449 path.buf + baselen,1450 path.len - baselen);1451 subdir_state =1452read_directory_recursive(dir, path.buf, path.len,1453 ud, check_only, simplify);1454if(subdir_state > dir_state)1455 dir_state = subdir_state;1456}14571458if(check_only) {1459/* abort early if maximum state has been reached */1460if(dir_state == path_untracked) {1461if(untracked)1462add_untracked(untracked, path.buf + baselen);1463break;1464}1465/* skip the dir_add_* part */1466continue;1467}14681469/* add the path to the appropriate result list */1470switch(state) {1471case path_excluded:1472if(dir->flags & DIR_SHOW_IGNORED)1473dir_add_name(dir, path.buf, path.len);1474else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1475((dir->flags & DIR_COLLECT_IGNORED) &&1476exclude_matches_pathspec(path.buf, path.len,1477 simplify)))1478dir_add_ignored(dir, path.buf, path.len);1479break;14801481case path_untracked:1482if(dir->flags & DIR_SHOW_IGNORED)1483break;1484dir_add_name(dir, path.buf, path.len);1485if(untracked)1486add_untracked(untracked, path.buf + baselen);1487break;14881489default:1490break;1491}1492}1493closedir(fdir);1494 out:1495strbuf_release(&path);14961497return dir_state;1498}14991500static intcmp_name(const void*p1,const void*p2)1501{1502const struct dir_entry *e1 = *(const struct dir_entry **)p1;1503const struct dir_entry *e2 = *(const struct dir_entry **)p2;15041505returnname_compare(e1->name, e1->len, e2->name, e2->len);1506}15071508static struct path_simplify *create_simplify(const char**pathspec)1509{1510int nr, alloc =0;1511struct path_simplify *simplify = NULL;15121513if(!pathspec)1514return NULL;15151516for(nr =0; ; nr++) {1517const char*match;1518ALLOC_GROW(simplify, nr +1, alloc);1519 match = *pathspec++;1520if(!match)1521break;1522 simplify[nr].path = match;1523 simplify[nr].len =simple_length(match);1524}1525 simplify[nr].path = NULL;1526 simplify[nr].len =0;1527return simplify;1528}15291530static voidfree_simplify(struct path_simplify *simplify)1531{1532free(simplify);1533}15341535static inttreat_leading_path(struct dir_struct *dir,1536const char*path,int len,1537const struct path_simplify *simplify)1538{1539struct strbuf sb = STRBUF_INIT;1540int baselen, rc =0;1541const char*cp;1542int old_flags = dir->flags;15431544while(len && path[len -1] =='/')1545 len--;1546if(!len)1547return1;1548 baselen =0;1549 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1550while(1) {1551 cp = path + baselen + !!baselen;1552 cp =memchr(cp,'/', path + len - cp);1553if(!cp)1554 baselen = len;1555else1556 baselen = cp - path;1557strbuf_setlen(&sb,0);1558strbuf_add(&sb, path, baselen);1559if(!is_directory(sb.buf))1560break;1561if(simplify_away(sb.buf, sb.len, simplify))1562break;1563if(treat_one_path(dir, NULL, &sb, simplify,1564 DT_DIR, NULL) == path_none)1565break;/* do not recurse into it */1566if(len <= baselen) {1567 rc =1;1568break;/* finished checking */1569}1570}1571strbuf_release(&sb);1572 dir->flags = old_flags;1573return rc;1574}15751576intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1577{1578struct path_simplify *simplify;15791580/*1581 * Check out create_simplify()1582 */1583if(pathspec)1584GUARD_PATHSPEC(pathspec,1585 PATHSPEC_FROMTOP |1586 PATHSPEC_MAXDEPTH |1587 PATHSPEC_LITERAL |1588 PATHSPEC_GLOB |1589 PATHSPEC_ICASE |1590 PATHSPEC_EXCLUDE);15911592if(has_symlink_leading_path(path, len))1593return dir->nr;15941595/*1596 * exclude patterns are treated like positive ones in1597 * create_simplify. Usually exclude patterns should be a1598 * subset of positive ones, which has no impacts on1599 * create_simplify().1600 */1601 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1602if(!len ||treat_leading_path(dir, path, len, simplify))1603read_directory_recursive(dir, path, len,1604 dir->untracked ? dir->untracked->root : NULL,16050, simplify);1606free_simplify(simplify);1607qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1608qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1609return dir->nr;1610}16111612intfile_exists(const char*f)1613{1614struct stat sb;1615returnlstat(f, &sb) ==0;1616}16171618/*1619 * Given two normalized paths (a trailing slash is ok), if subdir is1620 * outside dir, return -1. Otherwise return the offset in subdir that1621 * can be used as relative path to dir.1622 */1623intdir_inside_of(const char*subdir,const char*dir)1624{1625int offset =0;16261627assert(dir && subdir && *dir && *subdir);16281629while(*dir && *subdir && *dir == *subdir) {1630 dir++;1631 subdir++;1632 offset++;1633}16341635/* hel[p]/me vs hel[l]/yeah */1636if(*dir && *subdir)1637return-1;16381639if(!*subdir)1640return!*dir ? offset : -1;/* same dir */16411642/* foo/[b]ar vs foo/[] */1643if(is_dir_sep(dir[-1]))1644returnis_dir_sep(subdir[-1]) ? offset : -1;16451646/* foo[/]bar vs foo[] */1647returnis_dir_sep(*subdir) ? offset +1: -1;1648}16491650intis_inside_dir(const char*dir)1651{1652char*cwd;1653int rc;16541655if(!dir)1656return0;16571658 cwd =xgetcwd();1659 rc = (dir_inside_of(cwd, dir) >=0);1660free(cwd);1661return rc;1662}16631664intis_empty_dir(const char*path)1665{1666DIR*dir =opendir(path);1667struct dirent *e;1668int ret =1;16691670if(!dir)1671return0;16721673while((e =readdir(dir)) != NULL)1674if(!is_dot_or_dotdot(e->d_name)) {1675 ret =0;1676break;1677}16781679closedir(dir);1680return ret;1681}16821683static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1684{1685DIR*dir;1686struct dirent *e;1687int ret =0, original_len = path->len, len, kept_down =0;1688int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1689int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1690unsigned char submodule_head[20];16911692if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1693!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1694/* Do not descend and nuke a nested git work tree. */1695if(kept_up)1696*kept_up =1;1697return0;1698}16991700 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1701 dir =opendir(path->buf);1702if(!dir) {1703if(errno == ENOENT)1704return keep_toplevel ? -1:0;1705else if(errno == EACCES && !keep_toplevel)1706/*1707 * An empty dir could be removable even if it1708 * is unreadable:1709 */1710returnrmdir(path->buf);1711else1712return-1;1713}1714if(path->buf[original_len -1] !='/')1715strbuf_addch(path,'/');17161717 len = path->len;1718while((e =readdir(dir)) != NULL) {1719struct stat st;1720if(is_dot_or_dotdot(e->d_name))1721continue;17221723strbuf_setlen(path, len);1724strbuf_addstr(path, e->d_name);1725if(lstat(path->buf, &st)) {1726if(errno == ENOENT)1727/*1728 * file disappeared, which is what we1729 * wanted anyway1730 */1731continue;1732/* fall thru */1733}else if(S_ISDIR(st.st_mode)) {1734if(!remove_dir_recurse(path, flag, &kept_down))1735continue;/* happy */1736}else if(!only_empty &&1737(!unlink(path->buf) || errno == ENOENT)) {1738continue;/* happy, too */1739}17401741/* path too long, stat fails, or non-directory still exists */1742 ret = -1;1743break;1744}1745closedir(dir);17461747strbuf_setlen(path, original_len);1748if(!ret && !keep_toplevel && !kept_down)1749 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1750else if(kept_up)1751/*1752 * report the uplevel that it is not an error that we1753 * did not rmdir() our directory.1754 */1755*kept_up = !ret;1756return ret;1757}17581759intremove_dir_recursively(struct strbuf *path,int flag)1760{1761returnremove_dir_recurse(path, flag, NULL);1762}17631764voidsetup_standard_excludes(struct dir_struct *dir)1765{1766const char*path;1767char*xdg_path;17681769 dir->exclude_per_dir =".gitignore";1770 path =git_path("info/exclude");1771if(!excludes_file) {1772home_config_paths(NULL, &xdg_path,"ignore");1773 excludes_file = xdg_path;1774}1775if(!access_or_warn(path, R_OK,0))1776add_excludes_from_file_1(dir, path,1777 dir->untracked ? &dir->ss_info_exclude : NULL);1778if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1779add_excludes_from_file_1(dir, excludes_file,1780 dir->untracked ? &dir->ss_excludes_file : NULL);1781}17821783intremove_path(const char*name)1784{1785char*slash;17861787if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1788return-1;17891790 slash =strrchr(name,'/');1791if(slash) {1792char*dirs =xstrdup(name);1793 slash = dirs + (slash - name);1794do{1795*slash ='\0';1796}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1797free(dirs);1798}1799return0;1800}18011802/*1803 * Frees memory within dir which was allocated for exclude lists and1804 * the exclude_stack. Does not free dir itself.1805 */1806voidclear_directory(struct dir_struct *dir)1807{1808int i, j;1809struct exclude_list_group *group;1810struct exclude_list *el;1811struct exclude_stack *stk;18121813for(i = EXC_CMDL; i <= EXC_FILE; i++) {1814 group = &dir->exclude_list_group[i];1815for(j =0; j < group->nr; j++) {1816 el = &group->el[j];1817if(i == EXC_DIRS)1818free((char*)el->src);1819clear_exclude_list(el);1820}1821free(group->el);1822}18231824 stk = dir->exclude_stack;1825while(stk) {1826struct exclude_stack *prev = stk->prev;1827free(stk);1828 stk = prev;1829}1830strbuf_release(&dir->basebuf);1831}