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{ 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) 470{ 471int pos, len; 472unsigned long sz; 473enum object_type type; 474void*data; 475 476 len =strlen(path); 477 pos =cache_name_pos(path, len); 478if(pos <0) 479return NULL; 480if(!ce_skip_worktree(active_cache[pos])) 481return NULL; 482 data =read_sha1_file(active_cache[pos]->sha1, &type, &sz); 483if(!data || type != OBJ_BLOB) { 484free(data); 485return NULL; 486} 487*size =xsize_t(sz); 488return data; 489} 490 491/* 492 * Frees memory within el which was allocated for exclude patterns and 493 * the file buffer. Does not free el itself. 494 */ 495voidclear_exclude_list(struct exclude_list *el) 496{ 497int i; 498 499for(i =0; i < el->nr; i++) 500free(el->excludes[i]); 501free(el->excludes); 502free(el->filebuf); 503 504 el->nr =0; 505 el->excludes = NULL; 506 el->filebuf = NULL; 507} 508 509static voidtrim_trailing_spaces(char*buf) 510{ 511int i, last_space = -1, nr_spaces, len =strlen(buf); 512for(i =0; i < len; i++) 513if(buf[i] =='\\') 514 i++; 515else if(buf[i] ==' ') { 516if(last_space == -1) { 517 last_space = i; 518 nr_spaces =1; 519}else 520 nr_spaces++; 521}else 522 last_space = -1; 523 524if(last_space != -1&& last_space + nr_spaces == len) 525 buf[last_space] ='\0'; 526} 527 528intadd_excludes_from_file_to_list(const char*fname, 529const char*base, 530int baselen, 531struct exclude_list *el, 532int check_index) 533{ 534struct stat st; 535int fd, i, lineno =1; 536size_t size =0; 537char*buf, *entry; 538 539 fd =open(fname, O_RDONLY); 540if(fd <0||fstat(fd, &st) <0) { 541if(errno != ENOENT) 542warn_on_inaccessible(fname); 543if(0<= fd) 544close(fd); 545if(!check_index || 546(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 547return-1; 548if(size ==0) { 549free(buf); 550return0; 551} 552if(buf[size-1] !='\n') { 553 buf =xrealloc(buf, size+1); 554 buf[size++] ='\n'; 555} 556} 557else{ 558 size =xsize_t(st.st_size); 559if(size ==0) { 560close(fd); 561return0; 562} 563 buf =xmalloc(size+1); 564if(read_in_full(fd, buf, size) != size) { 565free(buf); 566close(fd); 567return-1; 568} 569 buf[size++] ='\n'; 570close(fd); 571} 572 573 el->filebuf = buf; 574 entry = buf; 575for(i =0; i < size; i++) { 576if(buf[i] =='\n') { 577if(entry != buf + i && entry[0] !='#') { 578 buf[i - (i && buf[i-1] =='\r')] =0; 579trim_trailing_spaces(entry); 580add_exclude(entry, base, baselen, el, lineno); 581} 582 lineno++; 583 entry = buf + i +1; 584} 585} 586return0; 587} 588 589struct exclude_list *add_exclude_list(struct dir_struct *dir, 590int group_type,const char*src) 591{ 592struct exclude_list *el; 593struct exclude_list_group *group; 594 595 group = &dir->exclude_list_group[group_type]; 596ALLOC_GROW(group->el, group->nr +1, group->alloc); 597 el = &group->el[group->nr++]; 598memset(el,0,sizeof(*el)); 599 el->src = src; 600return el; 601} 602 603/* 604 * Used to set up core.excludesfile and .git/info/exclude lists. 605 */ 606voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 607{ 608struct exclude_list *el; 609 el =add_exclude_list(dir, EXC_FILE, fname); 610if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 611die("cannot use%sas an exclude file", fname); 612} 613 614intmatch_basename(const char*basename,int basenamelen, 615const char*pattern,int prefix,int patternlen, 616int flags) 617{ 618if(prefix == patternlen) { 619if(patternlen == basenamelen && 620!strncmp_icase(pattern, basename, basenamelen)) 621return1; 622}else if(flags & EXC_FLAG_ENDSWITH) { 623/* "*literal" matching against "fooliteral" */ 624if(patternlen -1<= basenamelen && 625!strncmp_icase(pattern +1, 626 basename + basenamelen - (patternlen -1), 627 patternlen -1)) 628return1; 629}else{ 630if(fnmatch_icase_mem(pattern, patternlen, 631 basename, basenamelen, 6320) ==0) 633return1; 634} 635return0; 636} 637 638intmatch_pathname(const char*pathname,int pathlen, 639const char*base,int baselen, 640const char*pattern,int prefix,int patternlen, 641int flags) 642{ 643const char*name; 644int namelen; 645 646/* 647 * match with FNM_PATHNAME; the pattern has base implicitly 648 * in front of it. 649 */ 650if(*pattern =='/') { 651 pattern++; 652 patternlen--; 653 prefix--; 654} 655 656/* 657 * baselen does not count the trailing slash. base[] may or 658 * may not end with a trailing slash though. 659 */ 660if(pathlen < baselen +1|| 661(baselen && pathname[baselen] !='/') || 662strncmp_icase(pathname, base, baselen)) 663return0; 664 665 namelen = baselen ? pathlen - baselen -1: pathlen; 666 name = pathname + pathlen - namelen; 667 668if(prefix) { 669/* 670 * if the non-wildcard part is longer than the 671 * remaining pathname, surely it cannot match. 672 */ 673if(prefix > namelen) 674return0; 675 676if(strncmp_icase(pattern, name, prefix)) 677return0; 678 pattern += prefix; 679 patternlen -= prefix; 680 name += prefix; 681 namelen -= prefix; 682 683/* 684 * If the whole pattern did not have a wildcard, 685 * then our prefix match is all we need; we 686 * do not need to call fnmatch at all. 687 */ 688if(!patternlen && !namelen) 689return1; 690} 691 692returnfnmatch_icase_mem(pattern, patternlen, 693 name, namelen, 694 WM_PATHNAME) ==0; 695} 696 697/* 698 * Scan the given exclude list in reverse to see whether pathname 699 * should be ignored. The first match (i.e. the last on the list), if 700 * any, determines the fate. Returns the exclude_list element which 701 * matched, or NULL for undecided. 702 */ 703static struct exclude *last_exclude_matching_from_list(const char*pathname, 704int pathlen, 705const char*basename, 706int*dtype, 707struct exclude_list *el) 708{ 709int i; 710 711if(!el->nr) 712return NULL;/* undefined */ 713 714for(i = el->nr -1;0<= i; i--) { 715struct exclude *x = el->excludes[i]; 716const char*exclude = x->pattern; 717int prefix = x->nowildcardlen; 718 719if(x->flags & EXC_FLAG_MUSTBEDIR) { 720if(*dtype == DT_UNKNOWN) 721*dtype =get_dtype(NULL, pathname, pathlen); 722if(*dtype != DT_DIR) 723continue; 724} 725 726if(x->flags & EXC_FLAG_NODIR) { 727if(match_basename(basename, 728 pathlen - (basename - pathname), 729 exclude, prefix, x->patternlen, 730 x->flags)) 731return x; 732continue; 733} 734 735assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 736if(match_pathname(pathname, pathlen, 737 x->base, x->baselen ? x->baselen -1:0, 738 exclude, prefix, x->patternlen, x->flags)) 739return x; 740} 741return NULL;/* undecided */ 742} 743 744/* 745 * Scan the list and let the last match determine the fate. 746 * Return 1 for exclude, 0 for include and -1 for undecided. 747 */ 748intis_excluded_from_list(const char*pathname, 749int pathlen,const char*basename,int*dtype, 750struct exclude_list *el) 751{ 752struct exclude *exclude; 753 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 754if(exclude) 755return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 756return-1;/* undecided */ 757} 758 759static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 760const char*pathname,int pathlen,const char*basename, 761int*dtype_p) 762{ 763int i, j; 764struct exclude_list_group *group; 765struct exclude *exclude; 766for(i = EXC_CMDL; i <= EXC_FILE; i++) { 767 group = &dir->exclude_list_group[i]; 768for(j = group->nr -1; j >=0; j--) { 769 exclude =last_exclude_matching_from_list( 770 pathname, pathlen, basename, dtype_p, 771&group->el[j]); 772if(exclude) 773return exclude; 774} 775} 776return NULL; 777} 778 779/* 780 * Loads the per-directory exclude list for the substring of base 781 * which has a char length of baselen. 782 */ 783static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 784{ 785struct exclude_list_group *group; 786struct exclude_list *el; 787struct exclude_stack *stk = NULL; 788int current; 789 790 group = &dir->exclude_list_group[EXC_DIRS]; 791 792/* Pop the exclude lists from the EXCL_DIRS exclude_list_group 793 * which originate from directories not in the prefix of the 794 * path being checked. */ 795while((stk = dir->exclude_stack) != NULL) { 796if(stk->baselen <= baselen && 797!strncmp(dir->basebuf, base, stk->baselen)) 798break; 799 el = &group->el[dir->exclude_stack->exclude_ix]; 800 dir->exclude_stack = stk->prev; 801 dir->exclude = NULL; 802free((char*)el->src);/* see strdup() below */ 803clear_exclude_list(el); 804free(stk); 805 group->nr--; 806} 807 808/* Skip traversing into sub directories if the parent is excluded */ 809if(dir->exclude) 810return; 811 812/* Read from the parent directories and push them down. */ 813 current = stk ? stk->baselen : -1; 814while(current < baselen) { 815struct exclude_stack *stk =xcalloc(1,sizeof(*stk)); 816const char*cp; 817 818if(current <0) { 819 cp = base; 820 current =0; 821} 822else{ 823 cp =strchr(base + current +1,'/'); 824if(!cp) 825die("oops in prep_exclude"); 826 cp++; 827} 828 stk->prev = dir->exclude_stack; 829 stk->baselen = cp - base; 830 stk->exclude_ix = group->nr; 831 el =add_exclude_list(dir, EXC_DIRS, NULL); 832memcpy(dir->basebuf + current, base + current, 833 stk->baselen - current); 834 835/* Abort if the directory is excluded */ 836if(stk->baselen) { 837int dt = DT_DIR; 838 dir->basebuf[stk->baselen -1] =0; 839 dir->exclude =last_exclude_matching_from_lists(dir, 840 dir->basebuf, stk->baselen -1, 841 dir->basebuf + current, &dt); 842 dir->basebuf[stk->baselen -1] ='/'; 843if(dir->exclude && 844 dir->exclude->flags & EXC_FLAG_NEGATIVE) 845 dir->exclude = NULL; 846if(dir->exclude) { 847 dir->basebuf[stk->baselen] =0; 848 dir->exclude_stack = stk; 849return; 850} 851} 852 853/* Try to read per-directory file unless path is too long */ 854if(dir->exclude_per_dir && 855 stk->baselen +strlen(dir->exclude_per_dir) < PATH_MAX) { 856strcpy(dir->basebuf + stk->baselen, 857 dir->exclude_per_dir); 858/* 859 * dir->basebuf gets reused by the traversal, but we 860 * need fname to remain unchanged to ensure the src 861 * member of each struct exclude correctly 862 * back-references its source file. Other invocations 863 * of add_exclude_list provide stable strings, so we 864 * strdup() and free() here in the caller. 865 */ 866 el->src =strdup(dir->basebuf); 867add_excludes_from_file_to_list(dir->basebuf, 868 dir->basebuf, stk->baselen, el,1); 869} 870 dir->exclude_stack = stk; 871 current = stk->baselen; 872} 873 dir->basebuf[baselen] ='\0'; 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 the exclude_list element which matched, or NULL for 880 * undecided. 881 */ 882struct exclude *last_exclude_matching(struct dir_struct *dir, 883const char*pathname, 884int*dtype_p) 885{ 886int pathlen =strlen(pathname); 887const char*basename =strrchr(pathname,'/'); 888 basename = (basename) ? basename+1: pathname; 889 890prep_exclude(dir, pathname, basename-pathname); 891 892if(dir->exclude) 893return dir->exclude; 894 895returnlast_exclude_matching_from_lists(dir, pathname, pathlen, 896 basename, dtype_p); 897} 898 899/* 900 * Loads the exclude lists for the directory containing pathname, then 901 * scans all exclude lists to determine whether pathname is excluded. 902 * Returns 1 if true, otherwise 0. 903 */ 904intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 905{ 906struct exclude *exclude = 907last_exclude_matching(dir, pathname, dtype_p); 908if(exclude) 909return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 910return0; 911} 912 913static struct dir_entry *dir_entry_new(const char*pathname,int len) 914{ 915struct dir_entry *ent; 916 917 ent =xmalloc(sizeof(*ent) + len +1); 918 ent->len = len; 919memcpy(ent->name, pathname, len); 920 ent->name[len] =0; 921return ent; 922} 923 924static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 925{ 926if(cache_file_exists(pathname, len, ignore_case)) 927return NULL; 928 929ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 930return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 931} 932 933struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 934{ 935if(!cache_name_is_other(pathname, len)) 936return NULL; 937 938ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 939return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 940} 941 942enum exist_status { 943 index_nonexistent =0, 944 index_directory, 945 index_gitdir 946}; 947 948/* 949 * Do not use the alphabetically sorted index to look up 950 * the directory name; instead, use the case insensitive 951 * directory hash. 952 */ 953static enum exist_status directory_exists_in_index_icase(const char*dirname,int len) 954{ 955const struct cache_entry *ce =cache_dir_exists(dirname, len); 956unsigned char endchar; 957 958if(!ce) 959return index_nonexistent; 960 endchar = ce->name[len]; 961 962/* 963 * The cache_entry structure returned will contain this dirname 964 * and possibly additional path components. 965 */ 966if(endchar =='/') 967return index_directory; 968 969/* 970 * If there are no additional path components, then this cache_entry 971 * represents a submodule. Submodules, despite being directories, 972 * are stored in the cache without a closing slash. 973 */ 974if(!endchar &&S_ISGITLINK(ce->ce_mode)) 975return index_gitdir; 976 977/* This should never be hit, but it exists just in case. */ 978return index_nonexistent; 979} 980 981/* 982 * The index sorts alphabetically by entry name, which 983 * means that a gitlink sorts as '\0' at the end, while 984 * a directory (which is defined not as an entry, but as 985 * the files it contains) will sort with the '/' at the 986 * end. 987 */ 988static enum exist_status directory_exists_in_index(const char*dirname,int len) 989{ 990int pos; 991 992if(ignore_case) 993returndirectory_exists_in_index_icase(dirname, len); 994 995 pos =cache_name_pos(dirname, len); 996if(pos <0) 997 pos = -pos-1; 998while(pos < active_nr) { 999const struct cache_entry *ce = active_cache[pos++];1000unsigned char endchar;10011002if(strncmp(ce->name, dirname, len))1003break;1004 endchar = ce->name[len];1005if(endchar >'/')1006break;1007if(endchar =='/')1008return index_directory;1009if(!endchar &&S_ISGITLINK(ce->ce_mode))1010return index_gitdir;1011}1012return index_nonexistent;1013}10141015/*1016 * When we find a directory when traversing the filesystem, we1017 * have three distinct cases:1018 *1019 * - ignore it1020 * - see it as a directory1021 * - recurse into it1022 *1023 * and which one we choose depends on a combination of existing1024 * git index contents and the flags passed into the directory1025 * traversal routine.1026 *1027 * Case 1: If we *already* have entries in the index under that1028 * directory name, we always recurse into the directory to see1029 * all the files.1030 *1031 * Case 2: If we *already* have that directory name as a gitlink,1032 * we always continue to see it as a gitlink, regardless of whether1033 * there is an actual git directory there or not (it might not1034 * be checked out as a subproject!)1035 *1036 * Case 3: if we didn't have it in the index previously, we1037 * have a few sub-cases:1038 *1039 * (a) if "show_other_directories" is true, we show it as1040 * just a directory, unless "hide_empty_directories" is1041 * also true, in which case we need to check if it contains any1042 * untracked and / or ignored files.1043 * (b) if it looks like a git directory, and we don't have1044 * 'no_gitlinks' set we treat it as a gitlink, and show it1045 * as a directory.1046 * (c) otherwise, we recurse into it.1047 */1048static enum path_treatment treat_directory(struct dir_struct *dir,1049const char*dirname,int len,int exclude,1050const struct path_simplify *simplify)1051{1052/* The "len-1" is to strip the final '/' */1053switch(directory_exists_in_index(dirname, len-1)) {1054case index_directory:1055return path_recurse;10561057case index_gitdir:1058return path_none;10591060case index_nonexistent:1061if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1062break;1063if(!(dir->flags & DIR_NO_GITLINKS)) {1064unsigned char sha1[20];1065if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1066return path_untracked;1067}1068return path_recurse;1069}10701071/* This is the "show_other_directories" case */10721073if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1074return exclude ? path_excluded : path_untracked;10751076returnread_directory_recursive(dir, dirname, len,1, simplify);1077}10781079/*1080 * This is an inexact early pruning of any recursive directory1081 * reading - if the path cannot possibly be in the pathspec,1082 * return true, and we'll skip it early.1083 */1084static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1085{1086if(simplify) {1087for(;;) {1088const char*match = simplify->path;1089int len = simplify->len;10901091if(!match)1092break;1093if(len > pathlen)1094 len = pathlen;1095if(!memcmp(path, match, len))1096return0;1097 simplify++;1098}1099return1;1100}1101return0;1102}11031104/*1105 * This function tells us whether an excluded path matches a1106 * list of "interesting" pathspecs. That is, whether a path matched1107 * by any of the pathspecs could possibly be ignored by excluding1108 * the specified path. This can happen if:1109 *1110 * 1. the path is mentioned explicitly in the pathspec1111 *1112 * 2. the path is a directory prefix of some element in the1113 * pathspec1114 */1115static intexclude_matches_pathspec(const char*path,int len,1116const struct path_simplify *simplify)1117{1118if(simplify) {1119for(; simplify->path; simplify++) {1120if(len == simplify->len1121&& !memcmp(path, simplify->path, len))1122return1;1123if(len < simplify->len1124&& simplify->path[len] =='/'1125&& !memcmp(path, simplify->path, len))1126return1;1127}1128}1129return0;1130}11311132static intget_index_dtype(const char*path,int len)1133{1134int pos;1135const struct cache_entry *ce;11361137 ce =cache_file_exists(path, len,0);1138if(ce) {1139if(!ce_uptodate(ce))1140return DT_UNKNOWN;1141if(S_ISGITLINK(ce->ce_mode))1142return DT_DIR;1143/*1144 * Nobody actually cares about the1145 * difference between DT_LNK and DT_REG1146 */1147return DT_REG;1148}11491150/* Try to look it up as a directory */1151 pos =cache_name_pos(path, len);1152if(pos >=0)1153return DT_UNKNOWN;1154 pos = -pos-1;1155while(pos < active_nr) {1156 ce = active_cache[pos++];1157if(strncmp(ce->name, path, len))1158break;1159if(ce->name[len] >'/')1160break;1161if(ce->name[len] <'/')1162continue;1163if(!ce_uptodate(ce))1164break;/* continue? */1165return DT_DIR;1166}1167return DT_UNKNOWN;1168}11691170static intget_dtype(struct dirent *de,const char*path,int len)1171{1172int dtype = de ?DTYPE(de) : DT_UNKNOWN;1173struct stat st;11741175if(dtype != DT_UNKNOWN)1176return dtype;1177 dtype =get_index_dtype(path, len);1178if(dtype != DT_UNKNOWN)1179return dtype;1180if(lstat(path, &st))1181return dtype;1182if(S_ISREG(st.st_mode))1183return DT_REG;1184if(S_ISDIR(st.st_mode))1185return DT_DIR;1186if(S_ISLNK(st.st_mode))1187return DT_LNK;1188return dtype;1189}11901191static enum path_treatment treat_one_path(struct dir_struct *dir,1192struct strbuf *path,1193const struct path_simplify *simplify,1194int dtype,struct dirent *de)1195{1196int exclude;1197int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);11981199if(dtype == DT_UNKNOWN)1200 dtype =get_dtype(de, path->buf, path->len);12011202/* Always exclude indexed files */1203if(dtype != DT_DIR && has_path_in_index)1204return path_none;12051206/*1207 * When we are looking at a directory P in the working tree,1208 * there are three cases:1209 *1210 * (1) P exists in the index. Everything inside the directory P in1211 * the working tree needs to go when P is checked out from the1212 * index.1213 *1214 * (2) P does not exist in the index, but there is P/Q in the index.1215 * We know P will stay a directory when we check out the contents1216 * of the index, but we do not know yet if there is a directory1217 * P/Q in the working tree to be killed, so we need to recurse.1218 *1219 * (3) P does not exist in the index, and there is no P/Q in the index1220 * to require P to be a directory, either. Only in this case, we1221 * know that everything inside P will not be killed without1222 * recursing.1223 */1224if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1225(dtype == DT_DIR) &&1226!has_path_in_index &&1227(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1228return path_none;12291230 exclude =is_excluded(dir, path->buf, &dtype);12311232/*1233 * Excluded? If we don't explicitly want to show1234 * ignored files, ignore it1235 */1236if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1237return path_excluded;12381239switch(dtype) {1240default:1241return path_none;1242case DT_DIR:1243strbuf_addch(path,'/');1244returntreat_directory(dir, path->buf, path->len, exclude,1245 simplify);1246case DT_REG:1247case DT_LNK:1248return exclude ? path_excluded : path_untracked;1249}1250}12511252static enum path_treatment treat_path(struct dir_struct *dir,1253struct dirent *de,1254struct strbuf *path,1255int baselen,1256const struct path_simplify *simplify)1257{1258int dtype;12591260if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1261return path_none;1262strbuf_setlen(path, baselen);1263strbuf_addstr(path, de->d_name);1264if(simplify_away(path->buf, path->len, simplify))1265return path_none;12661267 dtype =DTYPE(de);1268returntreat_one_path(dir, path, simplify, dtype, de);1269}12701271/*1272 * Read a directory tree. We currently ignore anything but1273 * directories, regular files and symlinks. That's because git1274 * doesn't handle them at all yet. Maybe that will change some1275 * day.1276 *1277 * Also, we ignore the name ".git" (even if it is not a directory).1278 * That likely will not change.1279 *1280 * Returns the most significant path_treatment value encountered in the scan.1281 */1282static enum path_treatment read_directory_recursive(struct dir_struct *dir,1283const char*base,int baselen,1284int check_only,1285const struct path_simplify *simplify)1286{1287DIR*fdir;1288enum path_treatment state, subdir_state, dir_state = path_none;1289struct dirent *de;1290struct strbuf path = STRBUF_INIT;12911292strbuf_add(&path, base, baselen);12931294 fdir =opendir(path.len ? path.buf :".");1295if(!fdir)1296goto out;12971298while((de =readdir(fdir)) != NULL) {1299/* check how the file or directory should be treated */1300 state =treat_path(dir, de, &path, baselen, simplify);1301if(state > dir_state)1302 dir_state = state;13031304/* recurse into subdir if instructed by treat_path */1305if(state == path_recurse) {1306 subdir_state =read_directory_recursive(dir, path.buf,1307 path.len, check_only, simplify);1308if(subdir_state > dir_state)1309 dir_state = subdir_state;1310}13111312if(check_only) {1313/* abort early if maximum state has been reached */1314if(dir_state == path_untracked)1315break;1316/* skip the dir_add_* part */1317continue;1318}13191320/* add the path to the appropriate result list */1321switch(state) {1322case path_excluded:1323if(dir->flags & DIR_SHOW_IGNORED)1324dir_add_name(dir, path.buf, path.len);1325else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1326((dir->flags & DIR_COLLECT_IGNORED) &&1327exclude_matches_pathspec(path.buf, path.len,1328 simplify)))1329dir_add_ignored(dir, path.buf, path.len);1330break;13311332case path_untracked:1333if(!(dir->flags & DIR_SHOW_IGNORED))1334dir_add_name(dir, path.buf, path.len);1335break;13361337default:1338break;1339}1340}1341closedir(fdir);1342 out:1343strbuf_release(&path);13441345return dir_state;1346}13471348static intcmp_name(const void*p1,const void*p2)1349{1350const struct dir_entry *e1 = *(const struct dir_entry **)p1;1351const struct dir_entry *e2 = *(const struct dir_entry **)p2;13521353returncache_name_compare(e1->name, e1->len,1354 e2->name, e2->len);1355}13561357static struct path_simplify *create_simplify(const char**pathspec)1358{1359int nr, alloc =0;1360struct path_simplify *simplify = NULL;13611362if(!pathspec)1363return NULL;13641365for(nr =0; ; nr++) {1366const char*match;1367ALLOC_GROW(simplify, nr +1, alloc);1368 match = *pathspec++;1369if(!match)1370break;1371 simplify[nr].path = match;1372 simplify[nr].len =simple_length(match);1373}1374 simplify[nr].path = NULL;1375 simplify[nr].len =0;1376return simplify;1377}13781379static voidfree_simplify(struct path_simplify *simplify)1380{1381free(simplify);1382}13831384static inttreat_leading_path(struct dir_struct *dir,1385const char*path,int len,1386const struct path_simplify *simplify)1387{1388struct strbuf sb = STRBUF_INIT;1389int baselen, rc =0;1390const char*cp;1391int old_flags = dir->flags;13921393while(len && path[len -1] =='/')1394 len--;1395if(!len)1396return1;1397 baselen =0;1398 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1399while(1) {1400 cp = path + baselen + !!baselen;1401 cp =memchr(cp,'/', path + len - cp);1402if(!cp)1403 baselen = len;1404else1405 baselen = cp - path;1406strbuf_setlen(&sb,0);1407strbuf_add(&sb, path, baselen);1408if(!is_directory(sb.buf))1409break;1410if(simplify_away(sb.buf, sb.len, simplify))1411break;1412if(treat_one_path(dir, &sb, simplify,1413 DT_DIR, NULL) == path_none)1414break;/* do not recurse into it */1415if(len <= baselen) {1416 rc =1;1417break;/* finished checking */1418}1419}1420strbuf_release(&sb);1421 dir->flags = old_flags;1422return rc;1423}14241425intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1426{1427struct path_simplify *simplify;14281429/*1430 * Check out create_simplify()1431 */1432if(pathspec)1433GUARD_PATHSPEC(pathspec,1434 PATHSPEC_FROMTOP |1435 PATHSPEC_MAXDEPTH |1436 PATHSPEC_LITERAL |1437 PATHSPEC_GLOB |1438 PATHSPEC_ICASE |1439 PATHSPEC_EXCLUDE);14401441if(has_symlink_leading_path(path, len))1442return dir->nr;14431444/*1445 * exclude patterns are treated like positive ones in1446 * create_simplify. Usually exclude patterns should be a1447 * subset of positive ones, which has no impacts on1448 * create_simplify().1449 */1450 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1451if(!len ||treat_leading_path(dir, path, len, simplify))1452read_directory_recursive(dir, path, len,0, simplify);1453free_simplify(simplify);1454qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1455qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1456return dir->nr;1457}14581459intfile_exists(const char*f)1460{1461struct stat sb;1462returnlstat(f, &sb) ==0;1463}14641465/*1466 * Given two normalized paths (a trailing slash is ok), if subdir is1467 * outside dir, return -1. Otherwise return the offset in subdir that1468 * can be used as relative path to dir.1469 */1470intdir_inside_of(const char*subdir,const char*dir)1471{1472int offset =0;14731474assert(dir && subdir && *dir && *subdir);14751476while(*dir && *subdir && *dir == *subdir) {1477 dir++;1478 subdir++;1479 offset++;1480}14811482/* hel[p]/me vs hel[l]/yeah */1483if(*dir && *subdir)1484return-1;14851486if(!*subdir)1487return!*dir ? offset : -1;/* same dir */14881489/* foo/[b]ar vs foo/[] */1490if(is_dir_sep(dir[-1]))1491returnis_dir_sep(subdir[-1]) ? offset : -1;14921493/* foo[/]bar vs foo[] */1494returnis_dir_sep(*subdir) ? offset +1: -1;1495}14961497intis_inside_dir(const char*dir)1498{1499char cwd[PATH_MAX];1500if(!dir)1501return0;1502if(!getcwd(cwd,sizeof(cwd)))1503die_errno("can't find the current directory");1504returndir_inside_of(cwd, dir) >=0;1505}15061507intis_empty_dir(const char*path)1508{1509DIR*dir =opendir(path);1510struct dirent *e;1511int ret =1;15121513if(!dir)1514return0;15151516while((e =readdir(dir)) != NULL)1517if(!is_dot_or_dotdot(e->d_name)) {1518 ret =0;1519break;1520}15211522closedir(dir);1523return ret;1524}15251526static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1527{1528DIR*dir;1529struct dirent *e;1530int ret =0, original_len = path->len, len, kept_down =0;1531int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1532int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1533unsigned char submodule_head[20];15341535if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1536!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1537/* Do not descend and nuke a nested git work tree. */1538if(kept_up)1539*kept_up =1;1540return0;1541}15421543 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1544 dir =opendir(path->buf);1545if(!dir) {1546if(errno == ENOENT)1547return keep_toplevel ? -1:0;1548else if(errno == EACCES && !keep_toplevel)1549/*1550 * An empty dir could be removable even if it1551 * is unreadable:1552 */1553returnrmdir(path->buf);1554else1555return-1;1556}1557if(path->buf[original_len -1] !='/')1558strbuf_addch(path,'/');15591560 len = path->len;1561while((e =readdir(dir)) != NULL) {1562struct stat st;1563if(is_dot_or_dotdot(e->d_name))1564continue;15651566strbuf_setlen(path, len);1567strbuf_addstr(path, e->d_name);1568if(lstat(path->buf, &st)) {1569if(errno == ENOENT)1570/*1571 * file disappeared, which is what we1572 * wanted anyway1573 */1574continue;1575/* fall thru */1576}else if(S_ISDIR(st.st_mode)) {1577if(!remove_dir_recurse(path, flag, &kept_down))1578continue;/* happy */1579}else if(!only_empty &&1580(!unlink(path->buf) || errno == ENOENT)) {1581continue;/* happy, too */1582}15831584/* path too long, stat fails, or non-directory still exists */1585 ret = -1;1586break;1587}1588closedir(dir);15891590strbuf_setlen(path, original_len);1591if(!ret && !keep_toplevel && !kept_down)1592 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1593else if(kept_up)1594/*1595 * report the uplevel that it is not an error that we1596 * did not rmdir() our directory.1597 */1598*kept_up = !ret;1599return ret;1600}16011602intremove_dir_recursively(struct strbuf *path,int flag)1603{1604returnremove_dir_recurse(path, flag, NULL);1605}16061607voidsetup_standard_excludes(struct dir_struct *dir)1608{1609const char*path;1610char*xdg_path;16111612 dir->exclude_per_dir =".gitignore";1613 path =git_path("info/exclude");1614if(!excludes_file) {1615home_config_paths(NULL, &xdg_path,"ignore");1616 excludes_file = xdg_path;1617}1618if(!access_or_warn(path, R_OK,0))1619add_excludes_from_file(dir, path);1620if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1621add_excludes_from_file(dir, excludes_file);1622}16231624intremove_path(const char*name)1625{1626char*slash;16271628if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1629return-1;16301631 slash =strrchr(name,'/');1632if(slash) {1633char*dirs =xstrdup(name);1634 slash = dirs + (slash - name);1635do{1636*slash ='\0';1637}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1638free(dirs);1639}1640return0;1641}16421643/*1644 * Frees memory within dir which was allocated for exclude lists and1645 * the exclude_stack. Does not free dir itself.1646 */1647voidclear_directory(struct dir_struct *dir)1648{1649int i, j;1650struct exclude_list_group *group;1651struct exclude_list *el;1652struct exclude_stack *stk;16531654for(i = EXC_CMDL; i <= EXC_FILE; i++) {1655 group = &dir->exclude_list_group[i];1656for(j =0; j < group->nr; j++) {1657 el = &group->el[j];1658if(i == EXC_DIRS)1659free((char*)el->src);1660clear_exclude_list(el);1661}1662free(group->el);1663}16641665 stk = dir->exclude_stack;1666while(stk) {1667struct exclude_stack *prev = stk->prev;1668free(stk);1669 stk = prev;1670}1671}