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, 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{ 52returnfnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD :0)); 53} 54 55inlineintgit_fnmatch(const struct pathspec_item *item, 56const char*pattern,const char*string, 57int prefix) 58{ 59if(prefix >0) { 60if(ps_strncmp(item, pattern, string, prefix)) 61return FNM_NOMATCH; 62 pattern += prefix; 63 string += prefix; 64} 65if(item->flags & PATHSPEC_ONESTAR) { 66int pattern_len =strlen(++pattern); 67int string_len =strlen(string); 68return string_len < pattern_len || 69ps_strcmp(item, pattern, 70 string + string_len - pattern_len); 71} 72if(item->magic & PATHSPEC_GLOB) 73returnwildmatch(pattern, string, 74 WM_PATHNAME | 75(item->magic & PATHSPEC_ICASE ? WM_CASEFOLD :0), 76 NULL); 77else 78/* wildmatch has not learned no FNM_PATHNAME mode yet */ 79returnfnmatch(pattern, string, 80 item->magic & PATHSPEC_ICASE ? FNM_CASEFOLD :0); 81} 82 83static intfnmatch_icase_mem(const char*pattern,int patternlen, 84const char*string,int stringlen, 85int flags) 86{ 87int match_status; 88struct strbuf pat_buf = STRBUF_INIT; 89struct strbuf str_buf = STRBUF_INIT; 90const char*use_pat = pattern; 91const char*use_str = string; 92 93if(pattern[patternlen]) { 94strbuf_add(&pat_buf, pattern, patternlen); 95 use_pat = pat_buf.buf; 96} 97if(string[stringlen]) { 98strbuf_add(&str_buf, string, stringlen); 99 use_str = str_buf.buf; 100} 101 102if(ignore_case) 103 flags |= WM_CASEFOLD; 104 match_status =wildmatch(use_pat, use_str, flags, NULL); 105 106strbuf_release(&pat_buf); 107strbuf_release(&str_buf); 108 109return match_status; 110} 111 112static size_tcommon_prefix_len(const struct pathspec *pathspec) 113{ 114int n; 115size_t max =0; 116 117/* 118 * ":(icase)path" is treated as a pathspec full of 119 * wildcard. In other words, only prefix is considered common 120 * prefix. If the pathspec is abc/foo abc/bar, running in 121 * subdir xyz, the common prefix is still xyz, not xuz/abc as 122 * in non-:(icase). 123 */ 124GUARD_PATHSPEC(pathspec, 125 PATHSPEC_FROMTOP | 126 PATHSPEC_MAXDEPTH | 127 PATHSPEC_LITERAL | 128 PATHSPEC_GLOB | 129 PATHSPEC_ICASE | 130 PATHSPEC_EXCLUDE); 131 132for(n =0; n < pathspec->nr; n++) { 133size_t i =0, len =0, item_len; 134if(pathspec->items[n].magic & PATHSPEC_EXCLUDE) 135continue; 136if(pathspec->items[n].magic & PATHSPEC_ICASE) 137 item_len = pathspec->items[n].prefix; 138else 139 item_len = pathspec->items[n].nowildcard_len; 140while(i < item_len && (n ==0|| i < max)) { 141char c = pathspec->items[n].match[i]; 142if(c != pathspec->items[0].match[i]) 143break; 144if(c =='/') 145 len = i +1; 146 i++; 147} 148if(n ==0|| len < max) { 149 max = len; 150if(!max) 151break; 152} 153} 154return max; 155} 156 157/* 158 * Returns a copy of the longest leading path common among all 159 * pathspecs. 160 */ 161char*common_prefix(const struct pathspec *pathspec) 162{ 163unsigned long len =common_prefix_len(pathspec); 164 165return len ?xmemdupz(pathspec->items[0].match, len) : NULL; 166} 167 168intfill_directory(struct dir_struct *dir,const struct pathspec *pathspec) 169{ 170size_t len; 171 172/* 173 * Calculate common prefix for the pathspec, and 174 * use that to optimize the directory walk 175 */ 176 len =common_prefix_len(pathspec); 177 178/* Read the directory and prune it */ 179read_directory(dir, pathspec->nr ? pathspec->_raw[0] :"", len, pathspec); 180return len; 181} 182 183intwithin_depth(const char*name,int namelen, 184int depth,int max_depth) 185{ 186const char*cp = name, *cpe = name + namelen; 187 188while(cp < cpe) { 189if(*cp++ !='/') 190continue; 191 depth++; 192if(depth > max_depth) 193return0; 194} 195return1; 196} 197 198#define DO_MATCH_EXCLUDE 1 199#define DO_MATCH_DIRECTORY 2 200 201/* 202 * Does 'match' match the given name? 203 * A match is found if 204 * 205 * (1) the 'match' string is leading directory of 'name', or 206 * (2) the 'match' string is a wildcard and matches 'name', or 207 * (3) the 'match' string is exactly the same as 'name'. 208 * 209 * and the return value tells which case it was. 210 * 211 * It returns 0 when there is no match. 212 */ 213static intmatch_pathspec_item(const struct pathspec_item *item,int prefix, 214const char*name,int namelen,unsigned flags) 215{ 216/* name/namelen has prefix cut off by caller */ 217const char*match = item->match + prefix; 218int matchlen = item->len - prefix; 219 220/* 221 * The normal call pattern is: 222 * 1. prefix = common_prefix_len(ps); 223 * 2. prune something, or fill_directory 224 * 3. match_pathspec() 225 * 226 * 'prefix' at #1 may be shorter than the command's prefix and 227 * it's ok for #2 to match extra files. Those extras will be 228 * trimmed at #3. 229 * 230 * Suppose the pathspec is 'foo' and '../bar' running from 231 * subdir 'xyz'. The common prefix at #1 will be empty, thanks 232 * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The 233 * user does not want XYZ/foo, only the "foo" part should be 234 * case-insensitive. We need to filter out XYZ/foo here. In 235 * other words, we do not trust the caller on comparing the 236 * prefix part when :(icase) is involved. We do exact 237 * comparison ourselves. 238 * 239 * Normally the caller (common_prefix_len() in fact) does 240 * _exact_ matching on name[-prefix+1..-1] and we do not need 241 * to check that part. Be defensive and check it anyway, in 242 * case common_prefix_len is changed, or a new caller is 243 * introduced that does not use common_prefix_len. 244 * 245 * If the penalty turns out too high when prefix is really 246 * long, maybe change it to 247 * strncmp(match, name, item->prefix - prefix) 248 */ 249if(item->prefix && (item->magic & PATHSPEC_ICASE) && 250strncmp(item->match, name - prefix, item->prefix)) 251return0; 252 253/* If the match was just the prefix, we matched */ 254if(!*match) 255return MATCHED_RECURSIVELY; 256 257if(matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) { 258if(matchlen == namelen) 259return MATCHED_EXACTLY; 260 261if(match[matchlen-1] =='/'|| name[matchlen] =='/') 262return MATCHED_RECURSIVELY; 263}else if((flags & DO_MATCH_DIRECTORY) && 264 match[matchlen -1] =='/'&& 265 namelen == matchlen -1&& 266!ps_strncmp(item, match, name, namelen)) 267return MATCHED_EXACTLY; 268 269if(item->nowildcard_len < item->len && 270!git_fnmatch(item, match, name, 271 item->nowildcard_len - prefix)) 272return MATCHED_FNMATCH; 273 274return0; 275} 276 277/* 278 * Given a name and a list of pathspecs, returns the nature of the 279 * closest (i.e. most specific) match of the name to any of the 280 * pathspecs. 281 * 282 * The caller typically calls this multiple times with the same 283 * pathspec and seen[] array but with different name/namelen 284 * (e.g. entries from the index) and is interested in seeing if and 285 * how each pathspec matches all the names it calls this function 286 * with. A mark is left in the seen[] array for each pathspec element 287 * indicating the closest type of match that element achieved, so if 288 * seen[n] remains zero after multiple invocations, that means the nth 289 * pathspec did not match any names, which could indicate that the 290 * user mistyped the nth pathspec. 291 */ 292static intdo_match_pathspec(const struct pathspec *ps, 293const char*name,int namelen, 294int prefix,char*seen, 295unsigned flags) 296{ 297int i, retval =0, exclude = flags & DO_MATCH_EXCLUDE; 298 299GUARD_PATHSPEC(ps, 300 PATHSPEC_FROMTOP | 301 PATHSPEC_MAXDEPTH | 302 PATHSPEC_LITERAL | 303 PATHSPEC_GLOB | 304 PATHSPEC_ICASE | 305 PATHSPEC_EXCLUDE); 306 307if(!ps->nr) { 308if(!ps->recursive || 309!(ps->magic & PATHSPEC_MAXDEPTH) || 310 ps->max_depth == -1) 311return MATCHED_RECURSIVELY; 312 313if(within_depth(name, namelen,0, ps->max_depth)) 314return MATCHED_EXACTLY; 315else 316return0; 317} 318 319 name += prefix; 320 namelen -= prefix; 321 322for(i = ps->nr -1; i >=0; i--) { 323int how; 324 325if((!exclude && ps->items[i].magic & PATHSPEC_EXCLUDE) || 326( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE))) 327continue; 328 329if(seen && seen[i] == MATCHED_EXACTLY) 330continue; 331/* 332 * Make exclude patterns optional and never report 333 * "pathspec ':(exclude)foo' matches no files" 334 */ 335if(seen && ps->items[i].magic & PATHSPEC_EXCLUDE) 336 seen[i] = MATCHED_FNMATCH; 337 how =match_pathspec_item(ps->items+i, prefix, name, 338 namelen, flags); 339if(ps->recursive && 340(ps->magic & PATHSPEC_MAXDEPTH) && 341 ps->max_depth != -1&& 342 how && how != MATCHED_FNMATCH) { 343int len = ps->items[i].len; 344if(name[len] =='/') 345 len++; 346if(within_depth(name+len, namelen-len,0, ps->max_depth)) 347 how = MATCHED_EXACTLY; 348else 349 how =0; 350} 351if(how) { 352if(retval < how) 353 retval = how; 354if(seen && seen[i] < how) 355 seen[i] = how; 356} 357} 358return retval; 359} 360 361intmatch_pathspec(const struct pathspec *ps, 362const char*name,int namelen, 363int prefix,char*seen,int is_dir) 364{ 365int positive, negative; 366unsigned flags = is_dir ? DO_MATCH_DIRECTORY :0; 367 positive =do_match_pathspec(ps, name, namelen, 368 prefix, seen, flags); 369if(!(ps->magic & PATHSPEC_EXCLUDE) || !positive) 370return positive; 371 negative =do_match_pathspec(ps, name, namelen, 372 prefix, seen, 373 flags | DO_MATCH_EXCLUDE); 374return negative ?0: positive; 375} 376 377/* 378 * Return the length of the "simple" part of a path match limiter. 379 */ 380intsimple_length(const char*match) 381{ 382int len = -1; 383 384for(;;) { 385unsigned char c = *match++; 386 len++; 387if(c =='\0'||is_glob_special(c)) 388return len; 389} 390} 391 392intno_wildcard(const char*string) 393{ 394return string[simple_length(string)] =='\0'; 395} 396 397voidparse_exclude_pattern(const char**pattern, 398int*patternlen, 399int*flags, 400int*nowildcardlen) 401{ 402const char*p = *pattern; 403size_t i, len; 404 405*flags =0; 406if(*p =='!') { 407*flags |= EXC_FLAG_NEGATIVE; 408 p++; 409} 410 len =strlen(p); 411if(len && p[len -1] =='/') { 412 len--; 413*flags |= EXC_FLAG_MUSTBEDIR; 414} 415for(i =0; i < len; i++) { 416if(p[i] =='/') 417break; 418} 419if(i == len) 420*flags |= EXC_FLAG_NODIR; 421*nowildcardlen =simple_length(p); 422/* 423 * we should have excluded the trailing slash from 'p' too, 424 * but that's one more allocation. Instead just make sure 425 * nowildcardlen does not exceed real patternlen 426 */ 427if(*nowildcardlen > len) 428*nowildcardlen = len; 429if(*p =='*'&&no_wildcard(p +1)) 430*flags |= EXC_FLAG_ENDSWITH; 431*pattern = p; 432*patternlen = len; 433} 434 435voidadd_exclude(const char*string,const char*base, 436int baselen,struct exclude_list *el,int srcpos) 437{ 438struct exclude *x; 439int patternlen; 440int flags; 441int nowildcardlen; 442 443parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen); 444if(flags & EXC_FLAG_MUSTBEDIR) { 445char*s; 446 x =xmalloc(sizeof(*x) + patternlen +1); 447 s = (char*)(x+1); 448memcpy(s, string, patternlen); 449 s[patternlen] ='\0'; 450 x->pattern = s; 451}else{ 452 x =xmalloc(sizeof(*x)); 453 x->pattern = string; 454} 455 x->patternlen = patternlen; 456 x->nowildcardlen = nowildcardlen; 457 x->base = base; 458 x->baselen = baselen; 459 x->flags = flags; 460 x->srcpos = srcpos; 461ALLOC_GROW(el->excludes, el->nr +1, el->alloc); 462 el->excludes[el->nr++] = x; 463 x->el = el; 464} 465 466static void*read_skip_worktree_file_from_index(const char*path,size_t*size) 467{ 468int pos, len; 469unsigned long sz; 470enum object_type type; 471void*data; 472 473 len =strlen(path); 474 pos =cache_name_pos(path, len); 475if(pos <0) 476return NULL; 477if(!ce_skip_worktree(active_cache[pos])) 478return NULL; 479 data =read_sha1_file(active_cache[pos]->sha1, &type, &sz); 480if(!data || type != OBJ_BLOB) { 481free(data); 482return NULL; 483} 484*size =xsize_t(sz); 485return data; 486} 487 488/* 489 * Frees memory within el which was allocated for exclude patterns and 490 * the file buffer. Does not free el itself. 491 */ 492voidclear_exclude_list(struct exclude_list *el) 493{ 494int i; 495 496for(i =0; i < el->nr; i++) 497free(el->excludes[i]); 498free(el->excludes); 499free(el->filebuf); 500 501 el->nr =0; 502 el->excludes = NULL; 503 el->filebuf = NULL; 504} 505 506intadd_excludes_from_file_to_list(const char*fname, 507const char*base, 508int baselen, 509struct exclude_list *el, 510int check_index) 511{ 512struct stat st; 513int fd, i, lineno =1; 514size_t size =0; 515char*buf, *entry; 516 517 fd =open(fname, O_RDONLY); 518if(fd <0||fstat(fd, &st) <0) { 519if(errno != ENOENT) 520warn_on_inaccessible(fname); 521if(0<= fd) 522close(fd); 523if(!check_index || 524(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 525return-1; 526if(size ==0) { 527free(buf); 528return0; 529} 530if(buf[size-1] !='\n') { 531 buf =xrealloc(buf, size+1); 532 buf[size++] ='\n'; 533} 534} 535else{ 536 size =xsize_t(st.st_size); 537if(size ==0) { 538close(fd); 539return0; 540} 541 buf =xmalloc(size+1); 542if(read_in_full(fd, buf, size) != size) { 543free(buf); 544close(fd); 545return-1; 546} 547 buf[size++] ='\n'; 548close(fd); 549} 550 551 el->filebuf = buf; 552 entry = buf; 553for(i =0; i < size; i++) { 554if(buf[i] =='\n') { 555if(entry != buf + i && entry[0] !='#') { 556 buf[i - (i && buf[i-1] =='\r')] =0; 557add_exclude(entry, base, baselen, el, lineno); 558} 559 lineno++; 560 entry = buf + i +1; 561} 562} 563return0; 564} 565 566struct exclude_list *add_exclude_list(struct dir_struct *dir, 567int group_type,const char*src) 568{ 569struct exclude_list *el; 570struct exclude_list_group *group; 571 572 group = &dir->exclude_list_group[group_type]; 573ALLOC_GROW(group->el, group->nr +1, group->alloc); 574 el = &group->el[group->nr++]; 575memset(el,0,sizeof(*el)); 576 el->src = src; 577return el; 578} 579 580/* 581 * Used to set up core.excludesfile and .git/info/exclude lists. 582 */ 583voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 584{ 585struct exclude_list *el; 586 el =add_exclude_list(dir, EXC_FILE, fname); 587if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 588die("cannot use%sas an exclude file", fname); 589} 590 591intmatch_basename(const char*basename,int basenamelen, 592const char*pattern,int prefix,int patternlen, 593int flags) 594{ 595if(prefix == patternlen) { 596if(patternlen == basenamelen && 597!strncmp_icase(pattern, basename, basenamelen)) 598return1; 599}else if(flags & EXC_FLAG_ENDSWITH) { 600/* "*literal" matching against "fooliteral" */ 601if(patternlen -1<= basenamelen && 602!strncmp_icase(pattern +1, 603 basename + basenamelen - (patternlen -1), 604 patternlen -1)) 605return1; 606}else{ 607if(fnmatch_icase_mem(pattern, patternlen, 608 basename, basenamelen, 6090) ==0) 610return1; 611} 612return0; 613} 614 615intmatch_pathname(const char*pathname,int pathlen, 616const char*base,int baselen, 617const char*pattern,int prefix,int patternlen, 618int flags) 619{ 620const char*name; 621int namelen; 622 623/* 624 * match with FNM_PATHNAME; the pattern has base implicitly 625 * in front of it. 626 */ 627if(*pattern =='/') { 628 pattern++; 629 patternlen--; 630 prefix--; 631} 632 633/* 634 * baselen does not count the trailing slash. base[] may or 635 * may not end with a trailing slash though. 636 */ 637if(pathlen < baselen +1|| 638(baselen && pathname[baselen] !='/') || 639strncmp_icase(pathname, base, baselen)) 640return0; 641 642 namelen = baselen ? pathlen - baselen -1: pathlen; 643 name = pathname + pathlen - namelen; 644 645if(prefix) { 646/* 647 * if the non-wildcard part is longer than the 648 * remaining pathname, surely it cannot match. 649 */ 650if(prefix > namelen) 651return0; 652 653if(strncmp_icase(pattern, name, prefix)) 654return0; 655 pattern += prefix; 656 patternlen -= prefix; 657 name += prefix; 658 namelen -= prefix; 659 660/* 661 * If the whole pattern did not have a wildcard, 662 * then our prefix match is all we need; we 663 * do not need to call fnmatch at all. 664 */ 665if(!patternlen && !namelen) 666return1; 667} 668 669returnfnmatch_icase_mem(pattern, patternlen, 670 name, namelen, 671 WM_PATHNAME) ==0; 672} 673 674/* 675 * Scan the given exclude list in reverse to see whether pathname 676 * should be ignored. The first match (i.e. the last on the list), if 677 * any, determines the fate. Returns the exclude_list element which 678 * matched, or NULL for undecided. 679 */ 680static struct exclude *last_exclude_matching_from_list(const char*pathname, 681int pathlen, 682const char*basename, 683int*dtype, 684struct exclude_list *el) 685{ 686int i; 687 688if(!el->nr) 689return NULL;/* undefined */ 690 691for(i = el->nr -1;0<= i; i--) { 692struct exclude *x = el->excludes[i]; 693const char*exclude = x->pattern; 694int prefix = x->nowildcardlen; 695 696if(x->flags & EXC_FLAG_MUSTBEDIR) { 697if(*dtype == DT_UNKNOWN) 698*dtype =get_dtype(NULL, pathname, pathlen); 699if(*dtype != DT_DIR) 700continue; 701} 702 703if(x->flags & EXC_FLAG_NODIR) { 704if(match_basename(basename, 705 pathlen - (basename - pathname), 706 exclude, prefix, x->patternlen, 707 x->flags)) 708return x; 709continue; 710} 711 712assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 713if(match_pathname(pathname, pathlen, 714 x->base, x->baselen ? x->baselen -1:0, 715 exclude, prefix, x->patternlen, x->flags)) 716return x; 717} 718return NULL;/* undecided */ 719} 720 721/* 722 * Scan the list and let the last match determine the fate. 723 * Return 1 for exclude, 0 for include and -1 for undecided. 724 */ 725intis_excluded_from_list(const char*pathname, 726int pathlen,const char*basename,int*dtype, 727struct exclude_list *el) 728{ 729struct exclude *exclude; 730 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 731if(exclude) 732return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 733return-1;/* undecided */ 734} 735 736static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 737const char*pathname,int pathlen,const char*basename, 738int*dtype_p) 739{ 740int i, j; 741struct exclude_list_group *group; 742struct exclude *exclude; 743for(i = EXC_CMDL; i <= EXC_FILE; i++) { 744 group = &dir->exclude_list_group[i]; 745for(j = group->nr -1; j >=0; j--) { 746 exclude =last_exclude_matching_from_list( 747 pathname, pathlen, basename, dtype_p, 748&group->el[j]); 749if(exclude) 750return exclude; 751} 752} 753return NULL; 754} 755 756/* 757 * Loads the per-directory exclude list for the substring of base 758 * which has a char length of baselen. 759 */ 760static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 761{ 762struct exclude_list_group *group; 763struct exclude_list *el; 764struct exclude_stack *stk = NULL; 765int current; 766 767 group = &dir->exclude_list_group[EXC_DIRS]; 768 769/* Pop the exclude lists from the EXCL_DIRS exclude_list_group 770 * which originate from directories not in the prefix of the 771 * path being checked. */ 772while((stk = dir->exclude_stack) != NULL) { 773if(stk->baselen <= baselen && 774!strncmp(dir->basebuf, base, stk->baselen)) 775break; 776 el = &group->el[dir->exclude_stack->exclude_ix]; 777 dir->exclude_stack = stk->prev; 778 dir->exclude = NULL; 779free((char*)el->src);/* see strdup() below */ 780clear_exclude_list(el); 781free(stk); 782 group->nr--; 783} 784 785/* Skip traversing into sub directories if the parent is excluded */ 786if(dir->exclude) 787return; 788 789/* Read from the parent directories and push them down. */ 790 current = stk ? stk->baselen : -1; 791while(current < baselen) { 792struct exclude_stack *stk =xcalloc(1,sizeof(*stk)); 793const char*cp; 794 795if(current <0) { 796 cp = base; 797 current =0; 798} 799else{ 800 cp =strchr(base + current +1,'/'); 801if(!cp) 802die("oops in prep_exclude"); 803 cp++; 804} 805 stk->prev = dir->exclude_stack; 806 stk->baselen = cp - base; 807 stk->exclude_ix = group->nr; 808 el =add_exclude_list(dir, EXC_DIRS, NULL); 809memcpy(dir->basebuf + current, base + current, 810 stk->baselen - current); 811 812/* Abort if the directory is excluded */ 813if(stk->baselen) { 814int dt = DT_DIR; 815 dir->basebuf[stk->baselen -1] =0; 816 dir->exclude =last_exclude_matching_from_lists(dir, 817 dir->basebuf, stk->baselen -1, 818 dir->basebuf + current, &dt); 819 dir->basebuf[stk->baselen -1] ='/'; 820if(dir->exclude && 821 dir->exclude->flags & EXC_FLAG_NEGATIVE) 822 dir->exclude = NULL; 823if(dir->exclude) { 824 dir->basebuf[stk->baselen] =0; 825 dir->exclude_stack = stk; 826return; 827} 828} 829 830/* Try to read per-directory file unless path is too long */ 831if(dir->exclude_per_dir && 832 stk->baselen +strlen(dir->exclude_per_dir) < PATH_MAX) { 833strcpy(dir->basebuf + stk->baselen, 834 dir->exclude_per_dir); 835/* 836 * dir->basebuf gets reused by the traversal, but we 837 * need fname to remain unchanged to ensure the src 838 * member of each struct exclude correctly 839 * back-references its source file. Other invocations 840 * of add_exclude_list provide stable strings, so we 841 * strdup() and free() here in the caller. 842 */ 843 el->src =strdup(dir->basebuf); 844add_excludes_from_file_to_list(dir->basebuf, 845 dir->basebuf, stk->baselen, el,1); 846} 847 dir->exclude_stack = stk; 848 current = stk->baselen; 849} 850 dir->basebuf[baselen] ='\0'; 851} 852 853/* 854 * Loads the exclude lists for the directory containing pathname, then 855 * scans all exclude lists to determine whether pathname is excluded. 856 * Returns the exclude_list element which matched, or NULL for 857 * undecided. 858 */ 859struct exclude *last_exclude_matching(struct dir_struct *dir, 860const char*pathname, 861int*dtype_p) 862{ 863int pathlen =strlen(pathname); 864const char*basename =strrchr(pathname,'/'); 865 basename = (basename) ? basename+1: pathname; 866 867prep_exclude(dir, pathname, basename-pathname); 868 869if(dir->exclude) 870return dir->exclude; 871 872returnlast_exclude_matching_from_lists(dir, pathname, pathlen, 873 basename, dtype_p); 874} 875 876/* 877 * Loads the exclude lists for the directory containing pathname, then 878 * scans all exclude lists to determine whether pathname is excluded. 879 * Returns 1 if true, otherwise 0. 880 */ 881intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 882{ 883struct exclude *exclude = 884last_exclude_matching(dir, pathname, dtype_p); 885if(exclude) 886return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 887return0; 888} 889 890static struct dir_entry *dir_entry_new(const char*pathname,int len) 891{ 892struct dir_entry *ent; 893 894 ent =xmalloc(sizeof(*ent) + len +1); 895 ent->len = len; 896memcpy(ent->name, pathname, len); 897 ent->name[len] =0; 898return ent; 899} 900 901static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 902{ 903if(cache_file_exists(pathname, len, ignore_case)) 904return NULL; 905 906ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 907return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 908} 909 910struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 911{ 912if(!cache_name_is_other(pathname, len)) 913return NULL; 914 915ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 916return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 917} 918 919enum exist_status { 920 index_nonexistent =0, 921 index_directory, 922 index_gitdir 923}; 924 925/* 926 * Do not use the alphabetically sorted index to look up 927 * the directory name; instead, use the case insensitive 928 * directory hash. 929 */ 930static enum exist_status directory_exists_in_index_icase(const char*dirname,int len) 931{ 932const struct cache_entry *ce =cache_dir_exists(dirname, len); 933unsigned char endchar; 934 935if(!ce) 936return index_nonexistent; 937 endchar = ce->name[len]; 938 939/* 940 * The cache_entry structure returned will contain this dirname 941 * and possibly additional path components. 942 */ 943if(endchar =='/') 944return index_directory; 945 946/* 947 * If there are no additional path components, then this cache_entry 948 * represents a submodule. Submodules, despite being directories, 949 * are stored in the cache without a closing slash. 950 */ 951if(!endchar &&S_ISGITLINK(ce->ce_mode)) 952return index_gitdir; 953 954/* This should never be hit, but it exists just in case. */ 955return index_nonexistent; 956} 957 958/* 959 * The index sorts alphabetically by entry name, which 960 * means that a gitlink sorts as '\0' at the end, while 961 * a directory (which is defined not as an entry, but as 962 * the files it contains) will sort with the '/' at the 963 * end. 964 */ 965static enum exist_status directory_exists_in_index(const char*dirname,int len) 966{ 967int pos; 968 969if(ignore_case) 970returndirectory_exists_in_index_icase(dirname, len); 971 972 pos =cache_name_pos(dirname, len); 973if(pos <0) 974 pos = -pos-1; 975while(pos < active_nr) { 976const struct cache_entry *ce = active_cache[pos++]; 977unsigned char endchar; 978 979if(strncmp(ce->name, dirname, len)) 980break; 981 endchar = ce->name[len]; 982if(endchar >'/') 983break; 984if(endchar =='/') 985return index_directory; 986if(!endchar &&S_ISGITLINK(ce->ce_mode)) 987return index_gitdir; 988} 989return index_nonexistent; 990} 991 992/* 993 * When we find a directory when traversing the filesystem, we 994 * have three distinct cases: 995 * 996 * - ignore it 997 * - see it as a directory 998 * - recurse into it 999 *1000 * and which one we choose depends on a combination of existing1001 * git index contents and the flags passed into the directory1002 * traversal routine.1003 *1004 * Case 1: If we *already* have entries in the index under that1005 * directory name, we always recurse into the directory to see1006 * all the files.1007 *1008 * Case 2: If we *already* have that directory name as a gitlink,1009 * we always continue to see it as a gitlink, regardless of whether1010 * there is an actual git directory there or not (it might not1011 * be checked out as a subproject!)1012 *1013 * Case 3: if we didn't have it in the index previously, we1014 * have a few sub-cases:1015 *1016 * (a) if "show_other_directories" is true, we show it as1017 * just a directory, unless "hide_empty_directories" is1018 * also true, in which case we need to check if it contains any1019 * untracked and / or ignored files.1020 * (b) if it looks like a git directory, and we don't have1021 * 'no_gitlinks' set we treat it as a gitlink, and show it1022 * as a directory.1023 * (c) otherwise, we recurse into it.1024 */1025static enum path_treatment treat_directory(struct dir_struct *dir,1026const char*dirname,int len,int exclude,1027const struct path_simplify *simplify)1028{1029/* The "len-1" is to strip the final '/' */1030switch(directory_exists_in_index(dirname, len-1)) {1031case index_directory:1032return path_recurse;10331034case index_gitdir:1035return path_none;10361037case index_nonexistent:1038if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1039break;1040if(!(dir->flags & DIR_NO_GITLINKS)) {1041unsigned char sha1[20];1042if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1043return path_untracked;1044}1045return path_recurse;1046}10471048/* This is the "show_other_directories" case */10491050if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1051return exclude ? path_excluded : path_untracked;10521053returnread_directory_recursive(dir, dirname, len,1, simplify);1054}10551056/*1057 * This is an inexact early pruning of any recursive directory1058 * reading - if the path cannot possibly be in the pathspec,1059 * return true, and we'll skip it early.1060 */1061static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1062{1063if(simplify) {1064for(;;) {1065const char*match = simplify->path;1066int len = simplify->len;10671068if(!match)1069break;1070if(len > pathlen)1071 len = pathlen;1072if(!memcmp(path, match, len))1073return0;1074 simplify++;1075}1076return1;1077}1078return0;1079}10801081/*1082 * This function tells us whether an excluded path matches a1083 * list of "interesting" pathspecs. That is, whether a path matched1084 * by any of the pathspecs could possibly be ignored by excluding1085 * the specified path. This can happen if:1086 *1087 * 1. the path is mentioned explicitly in the pathspec1088 *1089 * 2. the path is a directory prefix of some element in the1090 * pathspec1091 */1092static intexclude_matches_pathspec(const char*path,int len,1093const struct path_simplify *simplify)1094{1095if(simplify) {1096for(; simplify->path; simplify++) {1097if(len == simplify->len1098&& !memcmp(path, simplify->path, len))1099return1;1100if(len < simplify->len1101&& simplify->path[len] =='/'1102&& !memcmp(path, simplify->path, len))1103return1;1104}1105}1106return0;1107}11081109static intget_index_dtype(const char*path,int len)1110{1111int pos;1112const struct cache_entry *ce;11131114 ce =cache_file_exists(path, len,0);1115if(ce) {1116if(!ce_uptodate(ce))1117return DT_UNKNOWN;1118if(S_ISGITLINK(ce->ce_mode))1119return DT_DIR;1120/*1121 * Nobody actually cares about the1122 * difference between DT_LNK and DT_REG1123 */1124return DT_REG;1125}11261127/* Try to look it up as a directory */1128 pos =cache_name_pos(path, len);1129if(pos >=0)1130return DT_UNKNOWN;1131 pos = -pos-1;1132while(pos < active_nr) {1133 ce = active_cache[pos++];1134if(strncmp(ce->name, path, len))1135break;1136if(ce->name[len] >'/')1137break;1138if(ce->name[len] <'/')1139continue;1140if(!ce_uptodate(ce))1141break;/* continue? */1142return DT_DIR;1143}1144return DT_UNKNOWN;1145}11461147static intget_dtype(struct dirent *de,const char*path,int len)1148{1149int dtype = de ?DTYPE(de) : DT_UNKNOWN;1150struct stat st;11511152if(dtype != DT_UNKNOWN)1153return dtype;1154 dtype =get_index_dtype(path, len);1155if(dtype != DT_UNKNOWN)1156return dtype;1157if(lstat(path, &st))1158return dtype;1159if(S_ISREG(st.st_mode))1160return DT_REG;1161if(S_ISDIR(st.st_mode))1162return DT_DIR;1163if(S_ISLNK(st.st_mode))1164return DT_LNK;1165return dtype;1166}11671168static enum path_treatment treat_one_path(struct dir_struct *dir,1169struct strbuf *path,1170const struct path_simplify *simplify,1171int dtype,struct dirent *de)1172{1173int exclude;1174int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);11751176if(dtype == DT_UNKNOWN)1177 dtype =get_dtype(de, path->buf, path->len);11781179/* Always exclude indexed files */1180if(dtype != DT_DIR && has_path_in_index)1181return path_none;11821183/*1184 * When we are looking at a directory P in the working tree,1185 * there are three cases:1186 *1187 * (1) P exists in the index. Everything inside the directory P in1188 * the working tree needs to go when P is checked out from the1189 * index.1190 *1191 * (2) P does not exist in the index, but there is P/Q in the index.1192 * We know P will stay a directory when we check out the contents1193 * of the index, but we do not know yet if there is a directory1194 * P/Q in the working tree to be killed, so we need to recurse.1195 *1196 * (3) P does not exist in the index, and there is no P/Q in the index1197 * to require P to be a directory, either. Only in this case, we1198 * know that everything inside P will not be killed without1199 * recursing.1200 */1201if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1202(dtype == DT_DIR) &&1203!has_path_in_index &&1204(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1205return path_none;12061207 exclude =is_excluded(dir, path->buf, &dtype);12081209/*1210 * Excluded? If we don't explicitly want to show1211 * ignored files, ignore it1212 */1213if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1214return path_excluded;12151216switch(dtype) {1217default:1218return path_none;1219case DT_DIR:1220strbuf_addch(path,'/');1221returntreat_directory(dir, path->buf, path->len, exclude,1222 simplify);1223case DT_REG:1224case DT_LNK:1225return exclude ? path_excluded : path_untracked;1226}1227}12281229static enum path_treatment treat_path(struct dir_struct *dir,1230struct dirent *de,1231struct strbuf *path,1232int baselen,1233const struct path_simplify *simplify)1234{1235int dtype;12361237if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1238return path_none;1239strbuf_setlen(path, baselen);1240strbuf_addstr(path, de->d_name);1241if(simplify_away(path->buf, path->len, simplify))1242return path_none;12431244 dtype =DTYPE(de);1245returntreat_one_path(dir, path, simplify, dtype, de);1246}12471248/*1249 * Read a directory tree. We currently ignore anything but1250 * directories, regular files and symlinks. That's because git1251 * doesn't handle them at all yet. Maybe that will change some1252 * day.1253 *1254 * Also, we ignore the name ".git" (even if it is not a directory).1255 * That likely will not change.1256 *1257 * Returns the most significant path_treatment value encountered in the scan.1258 */1259static enum path_treatment read_directory_recursive(struct dir_struct *dir,1260const char*base,int baselen,1261int check_only,1262const struct path_simplify *simplify)1263{1264DIR*fdir;1265enum path_treatment state, subdir_state, dir_state = path_none;1266struct dirent *de;1267struct strbuf path = STRBUF_INIT;12681269strbuf_add(&path, base, baselen);12701271 fdir =opendir(path.len ? path.buf :".");1272if(!fdir)1273goto out;12741275while((de =readdir(fdir)) != NULL) {1276/* check how the file or directory should be treated */1277 state =treat_path(dir, de, &path, baselen, simplify);1278if(state > dir_state)1279 dir_state = state;12801281/* recurse into subdir if instructed by treat_path */1282if(state == path_recurse) {1283 subdir_state =read_directory_recursive(dir, path.buf,1284 path.len, check_only, simplify);1285if(subdir_state > dir_state)1286 dir_state = subdir_state;1287}12881289if(check_only) {1290/* abort early if maximum state has been reached */1291if(dir_state == path_untracked)1292break;1293/* skip the dir_add_* part */1294continue;1295}12961297/* add the path to the appropriate result list */1298switch(state) {1299case path_excluded:1300if(dir->flags & DIR_SHOW_IGNORED)1301dir_add_name(dir, path.buf, path.len);1302else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1303((dir->flags & DIR_COLLECT_IGNORED) &&1304exclude_matches_pathspec(path.buf, path.len,1305 simplify)))1306dir_add_ignored(dir, path.buf, path.len);1307break;13081309case path_untracked:1310if(!(dir->flags & DIR_SHOW_IGNORED))1311dir_add_name(dir, path.buf, path.len);1312break;13131314default:1315break;1316}1317}1318closedir(fdir);1319 out:1320strbuf_release(&path);13211322return dir_state;1323}13241325static intcmp_name(const void*p1,const void*p2)1326{1327const struct dir_entry *e1 = *(const struct dir_entry **)p1;1328const struct dir_entry *e2 = *(const struct dir_entry **)p2;13291330returncache_name_compare(e1->name, e1->len,1331 e2->name, e2->len);1332}13331334static struct path_simplify *create_simplify(const char**pathspec)1335{1336int nr, alloc =0;1337struct path_simplify *simplify = NULL;13381339if(!pathspec)1340return NULL;13411342for(nr =0; ; nr++) {1343const char*match;1344if(nr >= alloc) {1345 alloc =alloc_nr(alloc);1346 simplify =xrealloc(simplify, alloc *sizeof(*simplify));1347}1348 match = *pathspec++;1349if(!match)1350break;1351 simplify[nr].path = match;1352 simplify[nr].len =simple_length(match);1353}1354 simplify[nr].path = NULL;1355 simplify[nr].len =0;1356return simplify;1357}13581359static voidfree_simplify(struct path_simplify *simplify)1360{1361free(simplify);1362}13631364static inttreat_leading_path(struct dir_struct *dir,1365const char*path,int len,1366const struct path_simplify *simplify)1367{1368struct strbuf sb = STRBUF_INIT;1369int baselen, rc =0;1370const char*cp;1371int old_flags = dir->flags;13721373while(len && path[len -1] =='/')1374 len--;1375if(!len)1376return1;1377 baselen =0;1378 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1379while(1) {1380 cp = path + baselen + !!baselen;1381 cp =memchr(cp,'/', path + len - cp);1382if(!cp)1383 baselen = len;1384else1385 baselen = cp - path;1386strbuf_setlen(&sb,0);1387strbuf_add(&sb, path, baselen);1388if(!is_directory(sb.buf))1389break;1390if(simplify_away(sb.buf, sb.len, simplify))1391break;1392if(treat_one_path(dir, &sb, simplify,1393 DT_DIR, NULL) == path_none)1394break;/* do not recurse into it */1395if(len <= baselen) {1396 rc =1;1397break;/* finished checking */1398}1399}1400strbuf_release(&sb);1401 dir->flags = old_flags;1402return rc;1403}14041405intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1406{1407struct path_simplify *simplify;14081409/*1410 * Check out create_simplify()1411 */1412if(pathspec)1413GUARD_PATHSPEC(pathspec,1414 PATHSPEC_FROMTOP |1415 PATHSPEC_MAXDEPTH |1416 PATHSPEC_LITERAL |1417 PATHSPEC_GLOB |1418 PATHSPEC_ICASE |1419 PATHSPEC_EXCLUDE);14201421if(has_symlink_leading_path(path, len))1422return dir->nr;14231424/*1425 * exclude patterns are treated like positive ones in1426 * create_simplify. Usually exclude patterns should be a1427 * subset of positive ones, which has no impacts on1428 * create_simplify().1429 */1430 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1431if(!len ||treat_leading_path(dir, path, len, simplify))1432read_directory_recursive(dir, path, len,0, simplify);1433free_simplify(simplify);1434qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1435qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1436return dir->nr;1437}14381439intfile_exists(const char*f)1440{1441struct stat sb;1442returnlstat(f, &sb) ==0;1443}14441445/*1446 * Given two normalized paths (a trailing slash is ok), if subdir is1447 * outside dir, return -1. Otherwise return the offset in subdir that1448 * can be used as relative path to dir.1449 */1450intdir_inside_of(const char*subdir,const char*dir)1451{1452int offset =0;14531454assert(dir && subdir && *dir && *subdir);14551456while(*dir && *subdir && *dir == *subdir) {1457 dir++;1458 subdir++;1459 offset++;1460}14611462/* hel[p]/me vs hel[l]/yeah */1463if(*dir && *subdir)1464return-1;14651466if(!*subdir)1467return!*dir ? offset : -1;/* same dir */14681469/* foo/[b]ar vs foo/[] */1470if(is_dir_sep(dir[-1]))1471returnis_dir_sep(subdir[-1]) ? offset : -1;14721473/* foo[/]bar vs foo[] */1474returnis_dir_sep(*subdir) ? offset +1: -1;1475}14761477intis_inside_dir(const char*dir)1478{1479char cwd[PATH_MAX];1480if(!dir)1481return0;1482if(!getcwd(cwd,sizeof(cwd)))1483die_errno("can't find the current directory");1484returndir_inside_of(cwd, dir) >=0;1485}14861487intis_empty_dir(const char*path)1488{1489DIR*dir =opendir(path);1490struct dirent *e;1491int ret =1;14921493if(!dir)1494return0;14951496while((e =readdir(dir)) != NULL)1497if(!is_dot_or_dotdot(e->d_name)) {1498 ret =0;1499break;1500}15011502closedir(dir);1503return ret;1504}15051506static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1507{1508DIR*dir;1509struct dirent *e;1510int ret =0, original_len = path->len, len, kept_down =0;1511int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1512int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1513unsigned char submodule_head[20];15141515if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1516!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1517/* Do not descend and nuke a nested git work tree. */1518if(kept_up)1519*kept_up =1;1520return0;1521}15221523 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1524 dir =opendir(path->buf);1525if(!dir) {1526if(errno == ENOENT)1527return keep_toplevel ? -1:0;1528else if(errno == EACCES && !keep_toplevel)1529/*1530 * An empty dir could be removable even if it1531 * is unreadable:1532 */1533returnrmdir(path->buf);1534else1535return-1;1536}1537if(path->buf[original_len -1] !='/')1538strbuf_addch(path,'/');15391540 len = path->len;1541while((e =readdir(dir)) != NULL) {1542struct stat st;1543if(is_dot_or_dotdot(e->d_name))1544continue;15451546strbuf_setlen(path, len);1547strbuf_addstr(path, e->d_name);1548if(lstat(path->buf, &st)) {1549if(errno == ENOENT)1550/*1551 * file disappeared, which is what we1552 * wanted anyway1553 */1554continue;1555/* fall thru */1556}else if(S_ISDIR(st.st_mode)) {1557if(!remove_dir_recurse(path, flag, &kept_down))1558continue;/* happy */1559}else if(!only_empty &&1560(!unlink(path->buf) || errno == ENOENT)) {1561continue;/* happy, too */1562}15631564/* path too long, stat fails, or non-directory still exists */1565 ret = -1;1566break;1567}1568closedir(dir);15691570strbuf_setlen(path, original_len);1571if(!ret && !keep_toplevel && !kept_down)1572 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1573else if(kept_up)1574/*1575 * report the uplevel that it is not an error that we1576 * did not rmdir() our directory.1577 */1578*kept_up = !ret;1579return ret;1580}15811582intremove_dir_recursively(struct strbuf *path,int flag)1583{1584returnremove_dir_recurse(path, flag, NULL);1585}15861587voidsetup_standard_excludes(struct dir_struct *dir)1588{1589const char*path;1590char*xdg_path;15911592 dir->exclude_per_dir =".gitignore";1593 path =git_path("info/exclude");1594if(!excludes_file) {1595home_config_paths(NULL, &xdg_path,"ignore");1596 excludes_file = xdg_path;1597}1598if(!access_or_warn(path, R_OK,0))1599add_excludes_from_file(dir, path);1600if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1601add_excludes_from_file(dir, excludes_file);1602}16031604intremove_path(const char*name)1605{1606char*slash;16071608if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1609return-1;16101611 slash =strrchr(name,'/');1612if(slash) {1613char*dirs =xstrdup(name);1614 slash = dirs + (slash - name);1615do{1616*slash ='\0';1617}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1618free(dirs);1619}1620return0;1621}16221623/*1624 * Frees memory within dir which was allocated for exclude lists and1625 * the exclude_stack. Does not free dir itself.1626 */1627voidclear_directory(struct dir_struct *dir)1628{1629int i, j;1630struct exclude_list_group *group;1631struct exclude_list *el;1632struct exclude_stack *stk;16331634for(i = EXC_CMDL; i <= EXC_FILE; i++) {1635 group = &dir->exclude_list_group[i];1636for(j =0; j < group->nr; j++) {1637 el = &group->el[j];1638if(i == EXC_DIRS)1639free((char*)el->src);1640clear_exclude_list(el);1641}1642free(group->el);1643}16441645 stk = dir->exclude_stack;1646while(stk) {1647struct exclude_stack *prev = stk->prev;1648free(stk);1649 stk = prev;1650}1651}