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 15struct path_simplify { 16int len; 17const char*path; 18}; 19 20static intread_directory_recursive(struct dir_struct *dir,const char*path,int len, 21int check_only,const struct path_simplify *simplify); 22static intget_dtype(struct dirent *de,const char*path,int len); 23 24/* helper string functions with support for the ignore_case flag */ 25intstrcmp_icase(const char*a,const char*b) 26{ 27return ignore_case ?strcasecmp(a, b) :strcmp(a, b); 28} 29 30intstrncmp_icase(const char*a,const char*b,size_t count) 31{ 32return ignore_case ?strncasecmp(a, b, count) :strncmp(a, b, count); 33} 34 35intfnmatch_icase(const char*pattern,const char*string,int flags) 36{ 37returnfnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD :0)); 38} 39 40inlineintgit_fnmatch(const char*pattern,const char*string, 41int flags,int prefix) 42{ 43int fnm_flags =0; 44if(flags & GFNM_PATHNAME) 45 fnm_flags |= FNM_PATHNAME; 46if(prefix >0) { 47if(strncmp(pattern, string, prefix)) 48return FNM_NOMATCH; 49 pattern += prefix; 50 string += prefix; 51} 52if(flags & GFNM_ONESTAR) { 53int pattern_len =strlen(++pattern); 54int string_len =strlen(string); 55return string_len < pattern_len || 56strcmp(pattern, 57 string + string_len - pattern_len); 58} 59returnfnmatch(pattern, string, fnm_flags); 60} 61 62static size_tcommon_prefix_len(const char**pathspec) 63{ 64const char*n, *first; 65size_t max =0; 66int literal =limit_pathspec_to_literal(); 67 68if(!pathspec) 69return max; 70 71 first = *pathspec; 72while((n = *pathspec++)) { 73size_t i, len =0; 74for(i =0; first == n || i < max; i++) { 75char c = n[i]; 76if(!c || c != first[i] || (!literal &&is_glob_special(c))) 77break; 78if(c =='/') 79 len = i +1; 80} 81if(first == n || len < max) { 82 max = len; 83if(!max) 84break; 85} 86} 87return max; 88} 89 90/* 91 * Returns a copy of the longest leading path common among all 92 * pathspecs. 93 */ 94char*common_prefix(const char**pathspec) 95{ 96unsigned long len =common_prefix_len(pathspec); 97 98return len ?xmemdupz(*pathspec, len) : NULL; 99} 100 101intfill_directory(struct dir_struct *dir,const char**pathspec) 102{ 103size_t len; 104 105/* 106 * Calculate common prefix for the pathspec, and 107 * use that to optimize the directory walk 108 */ 109 len =common_prefix_len(pathspec); 110 111/* Read the directory and prune it */ 112read_directory(dir, pathspec ? *pathspec :"", len, pathspec); 113return len; 114} 115 116intwithin_depth(const char*name,int namelen, 117int depth,int max_depth) 118{ 119const char*cp = name, *cpe = name + namelen; 120 121while(cp < cpe) { 122if(*cp++ !='/') 123continue; 124 depth++; 125if(depth > max_depth) 126return0; 127} 128return1; 129} 130 131/* 132 * Does 'match' match the given name? 133 * A match is found if 134 * 135 * (1) the 'match' string is leading directory of 'name', or 136 * (2) the 'match' string is a wildcard and matches 'name', or 137 * (3) the 'match' string is exactly the same as 'name'. 138 * 139 * and the return value tells which case it was. 140 * 141 * It returns 0 when there is no match. 142 */ 143static intmatch_one(const char*match,const char*name,int namelen) 144{ 145int matchlen; 146int literal =limit_pathspec_to_literal(); 147 148/* If the match was just the prefix, we matched */ 149if(!*match) 150return MATCHED_RECURSIVELY; 151 152if(ignore_case) { 153for(;;) { 154unsigned char c1 =tolower(*match); 155unsigned char c2 =tolower(*name); 156if(c1 =='\0'|| (!literal &&is_glob_special(c1))) 157break; 158if(c1 != c2) 159return0; 160 match++; 161 name++; 162 namelen--; 163} 164}else{ 165for(;;) { 166unsigned char c1 = *match; 167unsigned char c2 = *name; 168if(c1 =='\0'|| (!literal &&is_glob_special(c1))) 169break; 170if(c1 != c2) 171return0; 172 match++; 173 name++; 174 namelen--; 175} 176} 177 178/* 179 * If we don't match the matchstring exactly, 180 * we need to match by fnmatch 181 */ 182 matchlen =strlen(match); 183if(strncmp_icase(match, name, matchlen)) { 184if(literal) 185return0; 186return!fnmatch_icase(match, name,0) ? MATCHED_FNMATCH :0; 187} 188 189if(namelen == matchlen) 190return MATCHED_EXACTLY; 191if(match[matchlen-1] =='/'|| name[matchlen] =='/') 192return MATCHED_RECURSIVELY; 193return0; 194} 195 196/* 197 * Given a name and a list of pathspecs, returns the nature of the 198 * closest (i.e. most specific) match of the name to any of the 199 * pathspecs. 200 * 201 * The caller typically calls this multiple times with the same 202 * pathspec and seen[] array but with different name/namelen 203 * (e.g. entries from the index) and is interested in seeing if and 204 * how each pathspec matches all the names it calls this function 205 * with. A mark is left in the seen[] array for each pathspec element 206 * indicating the closest type of match that element achieved, so if 207 * seen[n] remains zero after multiple invocations, that means the nth 208 * pathspec did not match any names, which could indicate that the 209 * user mistyped the nth pathspec. 210 */ 211intmatch_pathspec(const char**pathspec,const char*name,int namelen, 212int prefix,char*seen) 213{ 214int i, retval =0; 215 216if(!pathspec) 217return1; 218 219 name += prefix; 220 namelen -= prefix; 221 222for(i =0; pathspec[i] != NULL; i++) { 223int how; 224const char*match = pathspec[i] + prefix; 225if(seen && seen[i] == MATCHED_EXACTLY) 226continue; 227 how =match_one(match, name, namelen); 228if(how) { 229if(retval < how) 230 retval = how; 231if(seen && seen[i] < how) 232 seen[i] = how; 233} 234} 235return retval; 236} 237 238/* 239 * Does 'match' match the given name? 240 * A match is found if 241 * 242 * (1) the 'match' string is leading directory of 'name', or 243 * (2) the 'match' string is a wildcard and matches 'name', or 244 * (3) the 'match' string is exactly the same as 'name'. 245 * 246 * and the return value tells which case it was. 247 * 248 * It returns 0 when there is no match. 249 */ 250static intmatch_pathspec_item(const struct pathspec_item *item,int prefix, 251const char*name,int namelen) 252{ 253/* name/namelen has prefix cut off by caller */ 254const char*match = item->match + prefix; 255int matchlen = item->len - prefix; 256 257/* If the match was just the prefix, we matched */ 258if(!*match) 259return MATCHED_RECURSIVELY; 260 261if(matchlen <= namelen && !strncmp(match, name, matchlen)) { 262if(matchlen == namelen) 263return MATCHED_EXACTLY; 264 265if(match[matchlen-1] =='/'|| name[matchlen] =='/') 266return MATCHED_RECURSIVELY; 267} 268 269if(item->nowildcard_len < item->len && 270!git_fnmatch(match, name, 271 item->flags & PATHSPEC_ONESTAR ? GFNM_ONESTAR :0, 272 item->nowildcard_len - prefix)) 273return MATCHED_FNMATCH; 274 275return0; 276} 277 278/* 279 * Given a name and a list of pathspecs, returns the nature of the 280 * closest (i.e. most specific) match of the name to any of the 281 * pathspecs. 282 * 283 * The caller typically calls this multiple times with the same 284 * pathspec and seen[] array but with different name/namelen 285 * (e.g. entries from the index) and is interested in seeing if and 286 * how each pathspec matches all the names it calls this function 287 * with. A mark is left in the seen[] array for each pathspec element 288 * indicating the closest type of match that element achieved, so if 289 * seen[n] remains zero after multiple invocations, that means the nth 290 * pathspec did not match any names, which could indicate that the 291 * user mistyped the nth pathspec. 292 */ 293intmatch_pathspec_depth(const struct pathspec *ps, 294const char*name,int namelen, 295int prefix,char*seen) 296{ 297int i, retval =0; 298 299if(!ps->nr) { 300if(!ps->recursive || ps->max_depth == -1) 301return MATCHED_RECURSIVELY; 302 303if(within_depth(name, namelen,0, ps->max_depth)) 304return MATCHED_EXACTLY; 305else 306return0; 307} 308 309 name += prefix; 310 namelen -= prefix; 311 312for(i = ps->nr -1; i >=0; i--) { 313int how; 314if(seen && seen[i] == MATCHED_EXACTLY) 315continue; 316 how =match_pathspec_item(ps->items+i, prefix, name, namelen); 317if(ps->recursive && ps->max_depth != -1&& 318 how && how != MATCHED_FNMATCH) { 319int len = ps->items[i].len; 320if(name[len] =='/') 321 len++; 322if(within_depth(name+len, namelen-len,0, ps->max_depth)) 323 how = MATCHED_EXACTLY; 324else 325 how =0; 326} 327if(how) { 328if(retval < how) 329 retval = how; 330if(seen && seen[i] < how) 331 seen[i] = how; 332} 333} 334return retval; 335} 336 337/* 338 * Return the length of the "simple" part of a path match limiter. 339 */ 340static intsimple_length(const char*match) 341{ 342int len = -1; 343 344for(;;) { 345unsigned char c = *match++; 346 len++; 347if(c =='\0'||is_glob_special(c)) 348return len; 349} 350} 351 352static intno_wildcard(const char*string) 353{ 354return string[simple_length(string)] =='\0'; 355} 356 357voidparse_exclude_pattern(const char**pattern, 358int*patternlen, 359int*flags, 360int*nowildcardlen) 361{ 362const char*p = *pattern; 363size_t i, len; 364 365*flags =0; 366if(*p =='!') { 367*flags |= EXC_FLAG_NEGATIVE; 368 p++; 369} 370 len =strlen(p); 371if(len && p[len -1] =='/') { 372 len--; 373*flags |= EXC_FLAG_MUSTBEDIR; 374} 375for(i =0; i < len; i++) { 376if(p[i] =='/') 377break; 378} 379if(i == len) 380*flags |= EXC_FLAG_NODIR; 381*nowildcardlen =simple_length(p); 382/* 383 * we should have excluded the trailing slash from 'p' too, 384 * but that's one more allocation. Instead just make sure 385 * nowildcardlen does not exceed real patternlen 386 */ 387if(*nowildcardlen > len) 388*nowildcardlen = len; 389if(*p =='*'&&no_wildcard(p +1)) 390*flags |= EXC_FLAG_ENDSWITH; 391*pattern = p; 392*patternlen = len; 393} 394 395voidadd_exclude(const char*string,const char*base, 396int baselen,struct exclude_list *el,int srcpos) 397{ 398struct exclude *x; 399int patternlen; 400int flags; 401int nowildcardlen; 402 403parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 404if(flags & EXC_FLAG_MUSTBEDIR) { 405char*s; 406 x =xmalloc(sizeof(*x) + patternlen +1); 407 s = (char*)(x+1); 408memcpy(s, string, patternlen); 409 s[patternlen] ='\0'; 410 x->pattern = s; 411}else{ 412 x =xmalloc(sizeof(*x)); 413 x->pattern = string; 414} 415 x->patternlen = patternlen; 416 x->nowildcardlen = nowildcardlen; 417 x->base = base; 418 x->baselen = baselen; 419 x->flags = flags; 420 x->srcpos = srcpos; 421ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 422 el->excludes[el->nr++] = x; 423 x->el = el; 424} 425 426static void*read_skip_worktree_file_from_index(const char*path,size_t*size) 427{ 428int pos, len; 429unsigned long sz; 430enum object_type type; 431void*data; 432struct index_state *istate = &the_index; 433 434 len =strlen(path); 435 pos =index_name_pos(istate, path, len); 436if(pos <0) 437return NULL; 438if(!ce_skip_worktree(istate->cache[pos])) 439return NULL; 440 data =read_sha1_file(istate->cache[pos]->sha1, &type, &sz); 441if(!data || type != OBJ_BLOB) { 442free(data); 443return NULL; 444} 445*size =xsize_t(sz); 446return data; 447} 448 449/* 450 * Frees memory within el which was allocated for exclude patterns and 451 * the file buffer. Does not free el itself. 452 */ 453voidclear_exclude_list(struct exclude_list *el) 454{ 455int i; 456 457for(i =0; i < el->nr; i++) 458free(el->excludes[i]); 459free(el->excludes); 460free(el->filebuf); 461 462 el->nr =0; 463 el->excludes = NULL; 464 el->filebuf = NULL; 465} 466 467intadd_excludes_from_file_to_list(const char*fname, 468const char*base, 469int baselen, 470struct exclude_list *el, 471int check_index) 472{ 473struct stat st; 474int fd, i, lineno =1; 475size_t size =0; 476char*buf, *entry; 477 478 fd =open(fname, O_RDONLY); 479if(fd <0||fstat(fd, &st) <0) { 480if(errno != ENOENT) 481warn_on_inaccessible(fname); 482if(0<= fd) 483close(fd); 484if(!check_index || 485(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 486return-1; 487if(size ==0) { 488free(buf); 489return0; 490} 491if(buf[size-1] !='\n') { 492 buf =xrealloc(buf, size+1); 493 buf[size++] ='\n'; 494} 495} 496else{ 497 size =xsize_t(st.st_size); 498if(size ==0) { 499close(fd); 500return0; 501} 502 buf =xmalloc(size+1); 503if(read_in_full(fd, buf, size) != size) { 504free(buf); 505close(fd); 506return-1; 507} 508 buf[size++] ='\n'; 509close(fd); 510} 511 512 el->filebuf = buf; 513 entry = buf; 514for(i =0; i < size; i++) { 515if(buf[i] =='\n') { 516if(entry != buf + i && entry[0] !='#') { 517 buf[i - (i && buf[i-1] =='\r')] =0; 518add_exclude(entry, base, baselen, el, lineno); 519} 520 lineno++; 521 entry = buf + i +1; 522} 523} 524return0; 525} 526 527struct exclude_list *add_exclude_list(struct dir_struct *dir, 528int group_type,const char*src) 529{ 530struct exclude_list *el; 531struct exclude_list_group *group; 532 533 group = &dir->exclude_list_group[group_type]; 534ALLOC_GROW(group->el, group->nr +1, group->alloc); 535 el = &group->el[group->nr++]; 536memset(el,0,sizeof(*el)); 537 el->src = src; 538return el; 539} 540 541/* 542 * Used to set up core.excludesfile and .git/info/exclude lists. 543 */ 544voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 545{ 546struct exclude_list *el; 547 el =add_exclude_list(dir, EXC_FILE, fname); 548if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 549die("cannot use%sas an exclude file", fname); 550} 551 552/* 553 * Loads the per-directory exclude list for the substring of base 554 * which has a char length of baselen. 555 */ 556static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 557{ 558struct exclude_list_group *group; 559struct exclude_list *el; 560struct exclude_stack *stk = NULL; 561int current; 562 563if((!dir->exclude_per_dir) || 564(baselen +strlen(dir->exclude_per_dir) >= PATH_MAX)) 565return;/* too long a path -- ignore */ 566 567 group = &dir->exclude_list_group[EXC_DIRS]; 568 569/* Pop the exclude lists from the EXCL_DIRS exclude_list_group 570 * which originate from directories not in the prefix of the 571 * path being checked. */ 572while((stk = dir->exclude_stack) != NULL) { 573if(stk->baselen <= baselen && 574!strncmp(dir->basebuf, base, stk->baselen)) 575break; 576 el = &group->el[dir->exclude_stack->exclude_ix]; 577 dir->exclude_stack = stk->prev; 578free((char*)el->src);/* see strdup() below */ 579clear_exclude_list(el); 580free(stk); 581 group->nr--; 582} 583 584/* Read from the parent directories and push them down. */ 585 current = stk ? stk->baselen : -1; 586while(current < baselen) { 587struct exclude_stack *stk =xcalloc(1,sizeof(*stk)); 588const char*cp; 589 590if(current <0) { 591 cp = base; 592 current =0; 593} 594else{ 595 cp =strchr(base + current +1,'/'); 596if(!cp) 597die("oops in prep_exclude"); 598 cp++; 599} 600 stk->prev = dir->exclude_stack; 601 stk->baselen = cp - base; 602memcpy(dir->basebuf + current, base + current, 603 stk->baselen - current); 604strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir); 605/* 606 * dir->basebuf gets reused by the traversal, but we 607 * need fname to remain unchanged to ensure the src 608 * member of each struct exclude correctly 609 * back-references its source file. Other invocations 610 * of add_exclude_list provide stable strings, so we 611 * strdup() and free() here in the caller. 612 */ 613 el =add_exclude_list(dir, EXC_DIRS,strdup(dir->basebuf)); 614 stk->exclude_ix = group->nr -1; 615add_excludes_from_file_to_list(dir->basebuf, 616 dir->basebuf, stk->baselen, 617 el,1); 618 dir->exclude_stack = stk; 619 current = stk->baselen; 620} 621 dir->basebuf[baselen] ='\0'; 622} 623 624intmatch_basename(const char*basename,int basenamelen, 625const char*pattern,int prefix,int patternlen, 626int flags) 627{ 628if(prefix == patternlen) { 629if(!strcmp_icase(pattern, basename)) 630return1; 631}else if(flags & EXC_FLAG_ENDSWITH) { 632if(patternlen -1<= basenamelen && 633!strcmp_icase(pattern +1, 634 basename + basenamelen - patternlen +1)) 635return1; 636}else{ 637if(fnmatch_icase(pattern, basename,0) ==0) 638return1; 639} 640return0; 641} 642 643intmatch_pathname(const char*pathname,int pathlen, 644const char*base,int baselen, 645const char*pattern,int prefix,int patternlen, 646int flags) 647{ 648const char*name; 649int namelen; 650 651/* 652 * match with FNM_PATHNAME; the pattern has base implicitly 653 * in front of it. 654 */ 655if(*pattern =='/') { 656 pattern++; 657 prefix--; 658} 659 660/* 661 * baselen does not count the trailing slash. base[] may or 662 * may not end with a trailing slash though. 663 */ 664if(pathlen < baselen +1|| 665(baselen && pathname[baselen] !='/') || 666strncmp_icase(pathname, base, baselen)) 667return0; 668 669 namelen = baselen ? pathlen - baselen -1: pathlen; 670 name = pathname + pathlen - namelen; 671 672if(prefix) { 673/* 674 * if the non-wildcard part is longer than the 675 * remaining pathname, surely it cannot match. 676 */ 677if(prefix > namelen) 678return0; 679 680if(strncmp_icase(pattern, name, prefix)) 681return0; 682 pattern += prefix; 683 name += prefix; 684 namelen -= prefix; 685} 686 687returnwildmatch(pattern, name, 688 WM_PATHNAME | (ignore_case ? WM_CASEFOLD :0), 689 NULL) ==0; 690} 691 692/* 693 * Scan the given exclude list in reverse to see whether pathname 694 * should be ignored. The first match (i.e. the last on the list), if 695 * any, determines the fate. Returns the exclude_list element which 696 * matched, or NULL for undecided. 697 */ 698static struct exclude *last_exclude_matching_from_list(const char*pathname, 699int pathlen, 700const char*basename, 701int*dtype, 702struct exclude_list *el) 703{ 704int i; 705 706if(!el->nr) 707return NULL;/* undefined */ 708 709for(i = el->nr -1;0<= i; i--) { 710struct exclude *x = el->excludes[i]; 711const char*exclude = x->pattern; 712int prefix = x->nowildcardlen; 713 714if(x->flags & EXC_FLAG_MUSTBEDIR) { 715if(*dtype == DT_UNKNOWN) 716*dtype =get_dtype(NULL, pathname, pathlen); 717if(*dtype != DT_DIR) 718continue; 719} 720 721if(x->flags & EXC_FLAG_NODIR) { 722if(match_basename(basename, 723 pathlen - (basename - pathname), 724 exclude, prefix, x->patternlen, 725 x->flags)) 726return x; 727continue; 728} 729 730assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 731if(match_pathname(pathname, pathlen, 732 x->base, x->baselen ? x->baselen -1:0, 733 exclude, prefix, x->patternlen, x->flags)) 734return x; 735} 736return NULL;/* undecided */ 737} 738 739/* 740 * Scan the list and let the last match determine the fate. 741 * Return 1 for exclude, 0 for include and -1 for undecided. 742 */ 743intis_excluded_from_list(const char*pathname, 744int pathlen,const char*basename,int*dtype, 745struct exclude_list *el) 746{ 747struct exclude *exclude; 748 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 749if(exclude) 750return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 751return-1;/* undecided */ 752} 753 754/* 755 * Loads the exclude lists for the directory containing pathname, then 756 * scans all exclude lists to determine whether pathname is excluded. 757 * Returns the exclude_list element which matched, or NULL for 758 * undecided. 759 */ 760static struct exclude *last_exclude_matching(struct dir_struct *dir, 761const char*pathname, 762int*dtype_p) 763{ 764int pathlen =strlen(pathname); 765int i, j; 766struct exclude_list_group *group; 767struct exclude *exclude; 768const char*basename =strrchr(pathname,'/'); 769 basename = (basename) ? basename+1: pathname; 770 771prep_exclude(dir, pathname, basename-pathname); 772 773for(i = EXC_CMDL; i <= EXC_FILE; i++) { 774 group = &dir->exclude_list_group[i]; 775for(j = group->nr -1; j >=0; j--) { 776 exclude =last_exclude_matching_from_list( 777 pathname, pathlen, basename, dtype_p, 778&group->el[j]); 779if(exclude) 780return exclude; 781} 782} 783return NULL; 784} 785 786/* 787 * Loads the exclude lists for the directory containing pathname, then 788 * scans all exclude lists to determine whether pathname is excluded. 789 * Returns 1 if true, otherwise 0. 790 */ 791static intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 792{ 793struct exclude *exclude = 794last_exclude_matching(dir, pathname, dtype_p); 795if(exclude) 796return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 797return0; 798} 799 800voidpath_exclude_check_init(struct path_exclude_check *check, 801struct dir_struct *dir) 802{ 803 check->dir = dir; 804 check->exclude = NULL; 805strbuf_init(&check->path,256); 806} 807 808voidpath_exclude_check_clear(struct path_exclude_check *check) 809{ 810strbuf_release(&check->path); 811} 812 813/* 814 * For each subdirectory in name, starting with the top-most, checks 815 * to see if that subdirectory is excluded, and if so, returns the 816 * corresponding exclude structure. Otherwise, checks whether name 817 * itself (which is presumably a file) is excluded. 818 * 819 * A path to a directory known to be excluded is left in check->path to 820 * optimize for repeated checks for files in the same excluded directory. 821 */ 822struct exclude *last_exclude_matching_path(struct path_exclude_check *check, 823const char*name,int namelen, 824int*dtype) 825{ 826int i; 827struct strbuf *path = &check->path; 828struct exclude *exclude; 829 830/* 831 * we allow the caller to pass namelen as an optimization; it 832 * must match the length of the name, as we eventually call 833 * is_excluded() on the whole name string. 834 */ 835if(namelen <0) 836 namelen =strlen(name); 837 838/* 839 * If path is non-empty, and name is equal to path or a 840 * subdirectory of path, name should be excluded, because 841 * it's inside a directory which is already known to be 842 * excluded and was previously left in check->path. 843 */ 844if(path->len && 845 path->len <= namelen && 846!memcmp(name, path->buf, path->len) && 847(!name[path->len] || name[path->len] =='/')) 848return check->exclude; 849 850strbuf_setlen(path,0); 851for(i =0; name[i]; i++) { 852int ch = name[i]; 853 854if(ch =='/') { 855int dt = DT_DIR; 856 exclude =last_exclude_matching(check->dir, 857 path->buf, &dt); 858if(exclude) { 859 check->exclude = exclude; 860return exclude; 861} 862} 863strbuf_addch(path, ch); 864} 865 866/* An entry in the index; cannot be a directory with subentries */ 867strbuf_setlen(path,0); 868 869returnlast_exclude_matching(check->dir, name, dtype); 870} 871 872/* 873 * Is this name excluded? This is for a caller like show_files() that 874 * do not honor directory hierarchy and iterate through paths that are 875 * possibly in an ignored directory. 876 */ 877intis_path_excluded(struct path_exclude_check *check, 878const char*name,int namelen,int*dtype) 879{ 880struct exclude *exclude = 881last_exclude_matching_path(check, name, namelen, dtype); 882if(exclude) 883return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 884return0; 885} 886 887static struct dir_entry *dir_entry_new(const char*pathname,int len) 888{ 889struct dir_entry *ent; 890 891 ent =xmalloc(sizeof(*ent) + len +1); 892 ent->len = len; 893memcpy(ent->name, pathname, len); 894 ent->name[len] =0; 895return ent; 896} 897 898static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 899{ 900if(!(dir->flags & DIR_SHOW_IGNORED) && 901cache_name_exists(pathname, len, ignore_case)) 902return NULL; 903 904ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 905return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 906} 907 908struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 909{ 910if(!cache_name_is_other(pathname, len)) 911return NULL; 912 913ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 914return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 915} 916 917enum exist_status { 918 index_nonexistent =0, 919 index_directory, 920 index_gitdir 921}; 922 923/* 924 * Do not use the alphabetically stored index to look up 925 * the directory name; instead, use the case insensitive 926 * name hash. 927 */ 928static enum exist_status directory_exists_in_index_icase(const char*dirname,int len) 929{ 930struct cache_entry *ce =index_name_exists(&the_index, dirname, len +1, ignore_case); 931unsigned char endchar; 932 933if(!ce) 934return index_nonexistent; 935 endchar = ce->name[len]; 936 937/* 938 * The cache_entry structure returned will contain this dirname 939 * and possibly additional path components. 940 */ 941if(endchar =='/') 942return index_directory; 943 944/* 945 * If there are no additional path components, then this cache_entry 946 * represents a submodule. Submodules, despite being directories, 947 * are stored in the cache without a closing slash. 948 */ 949if(!endchar &&S_ISGITLINK(ce->ce_mode)) 950return index_gitdir; 951 952/* This should never be hit, but it exists just in case. */ 953return index_nonexistent; 954} 955 956/* 957 * The index sorts alphabetically by entry name, which 958 * means that a gitlink sorts as '\0' at the end, while 959 * a directory (which is defined not as an entry, but as 960 * the files it contains) will sort with the '/' at the 961 * end. 962 */ 963static enum exist_status directory_exists_in_index(const char*dirname,int len) 964{ 965int pos; 966 967if(ignore_case) 968returndirectory_exists_in_index_icase(dirname, len); 969 970 pos =cache_name_pos(dirname, len); 971if(pos <0) 972 pos = -pos-1; 973while(pos < active_nr) { 974struct cache_entry *ce = active_cache[pos++]; 975unsigned char endchar; 976 977if(strncmp(ce->name, dirname, len)) 978break; 979 endchar = ce->name[len]; 980if(endchar >'/') 981break; 982if(endchar =='/') 983return index_directory; 984if(!endchar &&S_ISGITLINK(ce->ce_mode)) 985return index_gitdir; 986} 987return index_nonexistent; 988} 989 990/* 991 * When we find a directory when traversing the filesystem, we 992 * have three distinct cases: 993 * 994 * - ignore it 995 * - see it as a directory 996 * - recurse into it 997 * 998 * and which one we choose depends on a combination of existing 999 * git index contents and the flags passed into the directory1000 * traversal routine.1001 *1002 * Case 1: If we *already* have entries in the index under that1003 * directory name, we recurse into the directory to see all the files,1004 * unless the directory is excluded and we want to show ignored1005 * directories1006 *1007 * Case 2: If we *already* have that directory name as a gitlink,1008 * we always continue to see it as a gitlink, regardless of whether1009 * there is an actual git directory there or not (it might not1010 * be checked out as a subproject!)1011 *1012 * Case 3: if we didn't have it in the index previously, we1013 * have a few sub-cases:1014 *1015 * (a) if "show_other_directories" is true, we show it as1016 * just a directory, unless "hide_empty_directories" is1017 * also true and the directory is empty, in which case1018 * we just ignore it entirely.1019 * if we are looking for ignored directories, look if it1020 * contains only ignored files to decide if it must be shown as1021 * ignored or not.1022 * (b) if it looks like a git directory, and we don't have1023 * 'no_gitlinks' set we treat it as a gitlink, and show it1024 * as a directory.1025 * (c) otherwise, we recurse into it.1026 */1027enum directory_treatment {1028 show_directory,1029 ignore_directory,1030 recurse_into_directory1031};10321033static enum directory_treatment treat_directory(struct dir_struct *dir,1034const char*dirname,int len,int exclude,1035const struct path_simplify *simplify)1036{1037/* The "len-1" is to strip the final '/' */1038switch(directory_exists_in_index(dirname, len-1)) {1039case index_directory:1040if((dir->flags & DIR_SHOW_OTHER_DIRECTORIES) && exclude)1041break;10421043return recurse_into_directory;10441045case index_gitdir:1046if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1047return ignore_directory;1048return show_directory;10491050case index_nonexistent:1051if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1052break;1053if(!(dir->flags & DIR_NO_GITLINKS)) {1054unsigned char sha1[20];1055if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1056return show_directory;1057}1058return recurse_into_directory;1059}10601061/* This is the "show_other_directories" case */10621063/*1064 * We are looking for ignored files and our directory is not ignored,1065 * check if it contains only ignored files1066 */1067if((dir->flags & DIR_SHOW_IGNORED) && !exclude) {1068int ignored;1069 dir->flags &= ~DIR_SHOW_IGNORED;1070 dir->flags |= DIR_HIDE_EMPTY_DIRECTORIES;1071 ignored =read_directory_recursive(dir, dirname, len,1, simplify);1072 dir->flags &= ~DIR_HIDE_EMPTY_DIRECTORIES;1073 dir->flags |= DIR_SHOW_IGNORED;10741075return ignored ? ignore_directory : show_directory;1076}1077if(!(dir->flags & DIR_SHOW_IGNORED) &&1078!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1079return show_directory;1080if(!read_directory_recursive(dir, dirname, len,1, simplify))1081return ignore_directory;1082return show_directory;1083}10841085/*1086 * Decide what to do when we find a file while traversing the1087 * filesystem. Mostly two cases:1088 *1089 * 1. We are looking for ignored files1090 * (a) File is ignored, include it1091 * (b) File is in ignored path, include it1092 * (c) File is not ignored, exclude it1093 *1094 * 2. Other scenarios, include the file if not excluded1095 *1096 * Return 1 for exclude, 0 for include.1097 */1098static inttreat_file(struct dir_struct *dir,struct strbuf *path,int exclude,int*dtype)1099{1100struct path_exclude_check check;1101int exclude_file =0;11021103if(exclude)1104 exclude_file = !(dir->flags & DIR_SHOW_IGNORED);1105else if(dir->flags & DIR_SHOW_IGNORED) {1106/* Always exclude indexed files */1107struct cache_entry *ce =index_name_exists(&the_index,1108 path->buf, path->len, ignore_case);11091110if(ce)1111return1;11121113path_exclude_check_init(&check, dir);11141115if(!is_path_excluded(&check, path->buf, path->len, dtype))1116 exclude_file =1;11171118path_exclude_check_clear(&check);1119}11201121return exclude_file;1122}11231124/*1125 * This is an inexact early pruning of any recursive directory1126 * reading - if the path cannot possibly be in the pathspec,1127 * return true, and we'll skip it early.1128 */1129static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1130{1131if(simplify) {1132for(;;) {1133const char*match = simplify->path;1134int len = simplify->len;11351136if(!match)1137break;1138if(len > pathlen)1139 len = pathlen;1140if(!memcmp(path, match, len))1141return0;1142 simplify++;1143}1144return1;1145}1146return0;1147}11481149/*1150 * This function tells us whether an excluded path matches a1151 * list of "interesting" pathspecs. That is, whether a path matched1152 * by any of the pathspecs could possibly be ignored by excluding1153 * the specified path. This can happen if:1154 *1155 * 1. the path is mentioned explicitly in the pathspec1156 *1157 * 2. the path is a directory prefix of some element in the1158 * pathspec1159 */1160static intexclude_matches_pathspec(const char*path,int len,1161const struct path_simplify *simplify)1162{1163if(simplify) {1164for(; simplify->path; simplify++) {1165if(len == simplify->len1166&& !memcmp(path, simplify->path, len))1167return1;1168if(len < simplify->len1169&& simplify->path[len] =='/'1170&& !memcmp(path, simplify->path, len))1171return1;1172}1173}1174return0;1175}11761177static intget_index_dtype(const char*path,int len)1178{1179int pos;1180struct cache_entry *ce;11811182 ce =cache_name_exists(path, len,0);1183if(ce) {1184if(!ce_uptodate(ce))1185return DT_UNKNOWN;1186if(S_ISGITLINK(ce->ce_mode))1187return DT_DIR;1188/*1189 * Nobody actually cares about the1190 * difference between DT_LNK and DT_REG1191 */1192return DT_REG;1193}11941195/* Try to look it up as a directory */1196 pos =cache_name_pos(path, len);1197if(pos >=0)1198return DT_UNKNOWN;1199 pos = -pos-1;1200while(pos < active_nr) {1201 ce = active_cache[pos++];1202if(strncmp(ce->name, path, len))1203break;1204if(ce->name[len] >'/')1205break;1206if(ce->name[len] <'/')1207continue;1208if(!ce_uptodate(ce))1209break;/* continue? */1210return DT_DIR;1211}1212return DT_UNKNOWN;1213}12141215static intget_dtype(struct dirent *de,const char*path,int len)1216{1217int dtype = de ?DTYPE(de) : DT_UNKNOWN;1218struct stat st;12191220if(dtype != DT_UNKNOWN)1221return dtype;1222 dtype =get_index_dtype(path, len);1223if(dtype != DT_UNKNOWN)1224return dtype;1225if(lstat(path, &st))1226return dtype;1227if(S_ISREG(st.st_mode))1228return DT_REG;1229if(S_ISDIR(st.st_mode))1230return DT_DIR;1231if(S_ISLNK(st.st_mode))1232return DT_LNK;1233return dtype;1234}12351236enum path_treatment {1237 path_ignored,1238 path_handled,1239 path_recurse1240};12411242static enum path_treatment treat_one_path(struct dir_struct *dir,1243struct strbuf *path,1244const struct path_simplify *simplify,1245int dtype,struct dirent *de)1246{1247int exclude =is_excluded(dir, path->buf, &dtype);1248if(exclude && (dir->flags & DIR_COLLECT_IGNORED)1249&&exclude_matches_pathspec(path->buf, path->len, simplify))1250dir_add_ignored(dir, path->buf, path->len);12511252/*1253 * Excluded? If we don't explicitly want to show1254 * ignored files, ignore it1255 */1256if(exclude && !(dir->flags & DIR_SHOW_IGNORED))1257return path_ignored;12581259if(dtype == DT_UNKNOWN)1260 dtype =get_dtype(de, path->buf, path->len);12611262switch(dtype) {1263default:1264return path_ignored;1265case DT_DIR:1266strbuf_addch(path,'/');12671268switch(treat_directory(dir, path->buf, path->len, exclude, simplify)) {1269case show_directory:1270break;1271case recurse_into_directory:1272return path_recurse;1273case ignore_directory:1274return path_ignored;1275}1276break;1277case DT_REG:1278case DT_LNK:1279switch(treat_file(dir, path, exclude, &dtype)) {1280case1:1281return path_ignored;1282default:1283break;1284}1285}1286return path_handled;1287}12881289static enum path_treatment treat_path(struct dir_struct *dir,1290struct dirent *de,1291struct strbuf *path,1292int baselen,1293const struct path_simplify *simplify)1294{1295int dtype;12961297if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1298return path_ignored;1299strbuf_setlen(path, baselen);1300strbuf_addstr(path, de->d_name);1301if(simplify_away(path->buf, path->len, simplify))1302return path_ignored;13031304 dtype =DTYPE(de);1305returntreat_one_path(dir, path, simplify, dtype, de);1306}13071308/*1309 * Read a directory tree. We currently ignore anything but1310 * directories, regular files and symlinks. That's because git1311 * doesn't handle them at all yet. Maybe that will change some1312 * day.1313 *1314 * Also, we ignore the name ".git" (even if it is not a directory).1315 * That likely will not change.1316 */1317static intread_directory_recursive(struct dir_struct *dir,1318const char*base,int baselen,1319int check_only,1320const struct path_simplify *simplify)1321{1322DIR*fdir;1323int contents =0;1324struct dirent *de;1325struct strbuf path = STRBUF_INIT;13261327strbuf_add(&path, base, baselen);13281329 fdir =opendir(path.len ? path.buf :".");1330if(!fdir)1331goto out;13321333while((de =readdir(fdir)) != NULL) {1334switch(treat_path(dir, de, &path, baselen, simplify)) {1335case path_recurse:1336 contents +=read_directory_recursive(dir, path.buf,1337 path.len,0,1338 simplify);1339continue;1340case path_ignored:1341continue;1342case path_handled:1343break;1344}1345 contents++;1346if(check_only)1347break;1348dir_add_name(dir, path.buf, path.len);1349}1350closedir(fdir);1351 out:1352strbuf_release(&path);13531354return contents;1355}13561357static intcmp_name(const void*p1,const void*p2)1358{1359const struct dir_entry *e1 = *(const struct dir_entry **)p1;1360const struct dir_entry *e2 = *(const struct dir_entry **)p2;13611362returncache_name_compare(e1->name, e1->len,1363 e2->name, e2->len);1364}13651366static struct path_simplify *create_simplify(const char**pathspec)1367{1368int nr, alloc =0;1369struct path_simplify *simplify = NULL;13701371if(!pathspec)1372return NULL;13731374for(nr =0; ; nr++) {1375const char*match;1376if(nr >= alloc) {1377 alloc =alloc_nr(alloc);1378 simplify =xrealloc(simplify, alloc *sizeof(*simplify));1379}1380 match = *pathspec++;1381if(!match)1382break;1383 simplify[nr].path = match;1384 simplify[nr].len =simple_length(match);1385}1386 simplify[nr].path = NULL;1387 simplify[nr].len =0;1388return simplify;1389}13901391static voidfree_simplify(struct path_simplify *simplify)1392{1393free(simplify);1394}13951396static inttreat_leading_path(struct dir_struct *dir,1397const char*path,int len,1398const struct path_simplify *simplify)1399{1400struct strbuf sb = STRBUF_INIT;1401int baselen, rc =0;1402const char*cp;14031404while(len && path[len -1] =='/')1405 len--;1406if(!len)1407return1;1408 baselen =0;1409while(1) {1410 cp = path + baselen + !!baselen;1411 cp =memchr(cp,'/', path + len - cp);1412if(!cp)1413 baselen = len;1414else1415 baselen = cp - path;1416strbuf_setlen(&sb,0);1417strbuf_add(&sb, path, baselen);1418if(!is_directory(sb.buf))1419break;1420if(simplify_away(sb.buf, sb.len, simplify))1421break;1422if(treat_one_path(dir, &sb, simplify,1423 DT_DIR, NULL) == path_ignored)1424break;/* do not recurse into it */1425if(len <= baselen) {1426 rc =1;1427break;/* finished checking */1428}1429}1430strbuf_release(&sb);1431return rc;1432}14331434intread_directory(struct dir_struct *dir,const char*path,int len,const char**pathspec)1435{1436struct path_simplify *simplify;14371438if(has_symlink_leading_path(path, len))1439return dir->nr;14401441 simplify =create_simplify(pathspec);1442if(!len ||treat_leading_path(dir, path, len, simplify))1443read_directory_recursive(dir, path, len,0, simplify);1444free_simplify(simplify);1445qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1446qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1447return dir->nr;1448}14491450intfile_exists(const char*f)1451{1452struct stat sb;1453returnlstat(f, &sb) ==0;1454}14551456/*1457 * Given two normalized paths (a trailing slash is ok), if subdir is1458 * outside dir, return -1. Otherwise return the offset in subdir that1459 * can be used as relative path to dir.1460 */1461intdir_inside_of(const char*subdir,const char*dir)1462{1463int offset =0;14641465assert(dir && subdir && *dir && *subdir);14661467while(*dir && *subdir && *dir == *subdir) {1468 dir++;1469 subdir++;1470 offset++;1471}14721473/* hel[p]/me vs hel[l]/yeah */1474if(*dir && *subdir)1475return-1;14761477if(!*subdir)1478return!*dir ? offset : -1;/* same dir */14791480/* foo/[b]ar vs foo/[] */1481if(is_dir_sep(dir[-1]))1482returnis_dir_sep(subdir[-1]) ? offset : -1;14831484/* foo[/]bar vs foo[] */1485returnis_dir_sep(*subdir) ? offset +1: -1;1486}14871488intis_inside_dir(const char*dir)1489{1490char cwd[PATH_MAX];1491if(!dir)1492return0;1493if(!getcwd(cwd,sizeof(cwd)))1494die_errno("can't find the current directory");1495returndir_inside_of(cwd, dir) >=0;1496}14971498intis_empty_dir(const char*path)1499{1500DIR*dir =opendir(path);1501struct dirent *e;1502int ret =1;15031504if(!dir)1505return0;15061507while((e =readdir(dir)) != NULL)1508if(!is_dot_or_dotdot(e->d_name)) {1509 ret =0;1510break;1511}15121513closedir(dir);1514return ret;1515}15161517static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1518{1519DIR*dir;1520struct dirent *e;1521int ret =0, original_len = path->len, len, kept_down =0;1522int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1523int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1524unsigned char submodule_head[20];15251526if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1527!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1528/* Do not descend and nuke a nested git work tree. */1529if(kept_up)1530*kept_up =1;1531return0;1532}15331534 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1535 dir =opendir(path->buf);1536if(!dir) {1537/* an empty dir could be removed even if it is unreadble */1538if(!keep_toplevel)1539returnrmdir(path->buf);1540else1541return-1;1542}1543if(path->buf[original_len -1] !='/')1544strbuf_addch(path,'/');15451546 len = path->len;1547while((e =readdir(dir)) != NULL) {1548struct stat st;1549if(is_dot_or_dotdot(e->d_name))1550continue;15511552strbuf_setlen(path, len);1553strbuf_addstr(path, e->d_name);1554if(lstat(path->buf, &st))1555;/* fall thru */1556else if(S_ISDIR(st.st_mode)) {1557if(!remove_dir_recurse(path, flag, &kept_down))1558continue;/* happy */1559}else if(!only_empty && !unlink(path->buf))1560continue;/* happy, too */15611562/* path too long, stat fails, or non-directory still exists */1563 ret = -1;1564break;1565}1566closedir(dir);15671568strbuf_setlen(path, original_len);1569if(!ret && !keep_toplevel && !kept_down)1570 ret =rmdir(path->buf);1571else if(kept_up)1572/*1573 * report the uplevel that it is not an error that we1574 * did not rmdir() our directory.1575 */1576*kept_up = !ret;1577return ret;1578}15791580intremove_dir_recursively(struct strbuf *path,int flag)1581{1582returnremove_dir_recurse(path, flag, NULL);1583}15841585voidsetup_standard_excludes(struct dir_struct *dir)1586{1587const char*path;1588char*xdg_path;15891590 dir->exclude_per_dir =".gitignore";1591 path =git_path("info/exclude");1592if(!excludes_file) {1593home_config_paths(NULL, &xdg_path,"ignore");1594 excludes_file = xdg_path;1595}1596if(!access_or_warn(path, R_OK))1597add_excludes_from_file(dir, path);1598if(excludes_file && !access_or_warn(excludes_file, R_OK))1599add_excludes_from_file(dir, excludes_file);1600}16011602intremove_path(const char*name)1603{1604char*slash;16051606if(unlink(name) && errno != ENOENT)1607return-1;16081609 slash =strrchr(name,'/');1610if(slash) {1611char*dirs =xstrdup(name);1612 slash = dirs + (slash - name);1613do{1614*slash ='\0';1615}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1616free(dirs);1617}1618return0;1619}16201621static intpathspec_item_cmp(const void*a_,const void*b_)1622{1623struct pathspec_item *a, *b;16241625 a = (struct pathspec_item *)a_;1626 b = (struct pathspec_item *)b_;1627returnstrcmp(a->match, b->match);1628}16291630intinit_pathspec(struct pathspec *pathspec,const char**paths)1631{1632const char**p = paths;1633int i;16341635memset(pathspec,0,sizeof(*pathspec));1636if(!p)1637return0;1638while(*p)1639 p++;1640 pathspec->raw = paths;1641 pathspec->nr = p - paths;1642if(!pathspec->nr)1643return0;16441645 pathspec->items =xmalloc(sizeof(struct pathspec_item)*pathspec->nr);1646for(i =0; i < pathspec->nr; i++) {1647struct pathspec_item *item = pathspec->items+i;1648const char*path = paths[i];16491650 item->match = path;1651 item->len =strlen(path);1652 item->flags =0;1653if(limit_pathspec_to_literal()) {1654 item->nowildcard_len = item->len;1655}else{1656 item->nowildcard_len =simple_length(path);1657if(item->nowildcard_len < item->len) {1658 pathspec->has_wildcard =1;1659if(path[item->nowildcard_len] =='*'&&1660no_wildcard(path + item->nowildcard_len +1))1661 item->flags |= PATHSPEC_ONESTAR;1662}1663}1664}16651666qsort(pathspec->items, pathspec->nr,1667sizeof(struct pathspec_item), pathspec_item_cmp);16681669return0;1670}16711672voidfree_pathspec(struct pathspec *pathspec)1673{1674free(pathspec->items);1675 pathspec->items = NULL;1676}16771678intlimit_pathspec_to_literal(void)1679{1680static int flag = -1;1681if(flag <0)1682 flag =git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT,0);1683return flag;1684}16851686/*1687 * Frees memory within dir which was allocated for exclude lists and1688 * the exclude_stack. Does not free dir itself.1689 */1690voidclear_directory(struct dir_struct *dir)1691{1692int i, j;1693struct exclude_list_group *group;1694struct exclude_list *el;1695struct exclude_stack *stk;16961697for(i = EXC_CMDL; i <= EXC_FILE; i++) {1698 group = &dir->exclude_list_group[i];1699for(j =0; j < group->nr; j++) {1700 el = &group->el[j];1701if(i == EXC_DIRS)1702free((char*)el->src);1703clear_exclude_list(el);1704}1705free(group->el);1706}17071708 stk = dir->exclude_stack;1709while(stk) {1710struct exclude_stack *prev = stk->prev;1711free(stk);1712 stk = prev;1713}1714}