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 504memset(el,0,sizeof(*el)); 505} 506 507static voidtrim_trailing_spaces(char*buf) 508{ 509char*p, *last_space = NULL; 510 511for(p = buf; *p; p++) 512switch(*p) { 513case' ': 514if(!last_space) 515 last_space = p; 516break; 517case'\\': 518 p++; 519if(!*p) 520return; 521/* fallthrough */ 522default: 523 last_space = NULL; 524} 525 526if(last_space) 527*last_space ='\0'; 528} 529 530intadd_excludes_from_file_to_list(const char*fname, 531const char*base, 532int baselen, 533struct exclude_list *el, 534int check_index) 535{ 536struct stat st; 537int fd, i, lineno =1; 538size_t size =0; 539char*buf, *entry; 540 541 fd =open(fname, O_RDONLY); 542if(fd <0||fstat(fd, &st) <0) { 543if(errno != ENOENT) 544warn_on_inaccessible(fname); 545if(0<= fd) 546close(fd); 547if(!check_index || 548(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 549return-1; 550if(size ==0) { 551free(buf); 552return0; 553} 554if(buf[size-1] !='\n') { 555 buf =xrealloc(buf, size+1); 556 buf[size++] ='\n'; 557} 558}else{ 559 size =xsize_t(st.st_size); 560if(size ==0) { 561close(fd); 562return0; 563} 564 buf =xmalloc(size+1); 565if(read_in_full(fd, buf, size) != size) { 566free(buf); 567close(fd); 568return-1; 569} 570 buf[size++] ='\n'; 571close(fd); 572} 573 574 el->filebuf = buf; 575 entry = buf; 576for(i =0; i < size; i++) { 577if(buf[i] =='\n') { 578if(entry != buf + i && entry[0] !='#') { 579 buf[i - (i && buf[i-1] =='\r')] =0; 580trim_trailing_spaces(entry); 581add_exclude(entry, base, baselen, el, lineno); 582} 583 lineno++; 584 entry = buf + i +1; 585} 586} 587return0; 588} 589 590struct exclude_list *add_exclude_list(struct dir_struct *dir, 591int group_type,const char*src) 592{ 593struct exclude_list *el; 594struct exclude_list_group *group; 595 596 group = &dir->exclude_list_group[group_type]; 597ALLOC_GROW(group->el, group->nr +1, group->alloc); 598 el = &group->el[group->nr++]; 599memset(el,0,sizeof(*el)); 600 el->src = src; 601return el; 602} 603 604/* 605 * Used to set up core.excludesfile and .git/info/exclude lists. 606 */ 607voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 608{ 609struct exclude_list *el; 610 el =add_exclude_list(dir, EXC_FILE, fname); 611if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 612die("cannot use%sas an exclude file", fname); 613} 614 615intmatch_basename(const char*basename,int basenamelen, 616const char*pattern,int prefix,int patternlen, 617int flags) 618{ 619if(prefix == patternlen) { 620if(patternlen == basenamelen && 621!strncmp_icase(pattern, basename, basenamelen)) 622return1; 623}else if(flags & EXC_FLAG_ENDSWITH) { 624/* "*literal" matching against "fooliteral" */ 625if(patternlen -1<= basenamelen && 626!strncmp_icase(pattern +1, 627 basename + basenamelen - (patternlen -1), 628 patternlen -1)) 629return1; 630}else{ 631if(fnmatch_icase_mem(pattern, patternlen, 632 basename, basenamelen, 6330) ==0) 634return1; 635} 636return0; 637} 638 639intmatch_pathname(const char*pathname,int pathlen, 640const char*base,int baselen, 641const char*pattern,int prefix,int patternlen, 642int flags) 643{ 644const char*name; 645int namelen; 646 647/* 648 * match with FNM_PATHNAME; the pattern has base implicitly 649 * in front of it. 650 */ 651if(*pattern =='/') { 652 pattern++; 653 patternlen--; 654 prefix--; 655} 656 657/* 658 * baselen does not count the trailing slash. base[] may or 659 * may not end with a trailing slash though. 660 */ 661if(pathlen < baselen +1|| 662(baselen && pathname[baselen] !='/') || 663strncmp_icase(pathname, base, baselen)) 664return0; 665 666 namelen = baselen ? pathlen - baselen -1: pathlen; 667 name = pathname + pathlen - namelen; 668 669if(prefix) { 670/* 671 * if the non-wildcard part is longer than the 672 * remaining pathname, surely it cannot match. 673 */ 674if(prefix > namelen) 675return0; 676 677if(strncmp_icase(pattern, name, prefix)) 678return0; 679 pattern += prefix; 680 patternlen -= prefix; 681 name += prefix; 682 namelen -= prefix; 683 684/* 685 * If the whole pattern did not have a wildcard, 686 * then our prefix match is all we need; we 687 * do not need to call fnmatch at all. 688 */ 689if(!patternlen && !namelen) 690return1; 691} 692 693returnfnmatch_icase_mem(pattern, patternlen, 694 name, namelen, 695 WM_PATHNAME) ==0; 696} 697 698/* 699 * Scan the given exclude list in reverse to see whether pathname 700 * should be ignored. The first match (i.e. the last on the list), if 701 * any, determines the fate. Returns the exclude_list element which 702 * matched, or NULL for undecided. 703 */ 704static struct exclude *last_exclude_matching_from_list(const char*pathname, 705int pathlen, 706const char*basename, 707int*dtype, 708struct exclude_list *el) 709{ 710int i; 711 712if(!el->nr) 713return NULL;/* undefined */ 714 715for(i = el->nr -1;0<= i; i--) { 716struct exclude *x = el->excludes[i]; 717const char*exclude = x->pattern; 718int prefix = x->nowildcardlen; 719 720if(x->flags & EXC_FLAG_MUSTBEDIR) { 721if(*dtype == DT_UNKNOWN) 722*dtype =get_dtype(NULL, pathname, pathlen); 723if(*dtype != DT_DIR) 724continue; 725} 726 727if(x->flags & EXC_FLAG_NODIR) { 728if(match_basename(basename, 729 pathlen - (basename - pathname), 730 exclude, prefix, x->patternlen, 731 x->flags)) 732return x; 733continue; 734} 735 736assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 737if(match_pathname(pathname, pathlen, 738 x->base, x->baselen ? x->baselen -1:0, 739 exclude, prefix, x->patternlen, x->flags)) 740return x; 741} 742return NULL;/* undecided */ 743} 744 745/* 746 * Scan the list and let the last match determine the fate. 747 * Return 1 for exclude, 0 for include and -1 for undecided. 748 */ 749intis_excluded_from_list(const char*pathname, 750int pathlen,const char*basename,int*dtype, 751struct exclude_list *el) 752{ 753struct exclude *exclude; 754 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 755if(exclude) 756return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 757return-1;/* undecided */ 758} 759 760static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 761const char*pathname,int pathlen,const char*basename, 762int*dtype_p) 763{ 764int i, j; 765struct exclude_list_group *group; 766struct exclude *exclude; 767for(i = EXC_CMDL; i <= EXC_FILE; i++) { 768 group = &dir->exclude_list_group[i]; 769for(j = group->nr -1; j >=0; j--) { 770 exclude =last_exclude_matching_from_list( 771 pathname, pathlen, basename, dtype_p, 772&group->el[j]); 773if(exclude) 774return exclude; 775} 776} 777return NULL; 778} 779 780/* 781 * Loads the per-directory exclude list for the substring of base 782 * which has a char length of baselen. 783 */ 784static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 785{ 786struct exclude_list_group *group; 787struct exclude_list *el; 788struct exclude_stack *stk = NULL; 789int current; 790 791 group = &dir->exclude_list_group[EXC_DIRS]; 792 793/* 794 * Pop the exclude lists from the EXCL_DIRS exclude_list_group 795 * which originate from directories not in the prefix of the 796 * path being checked. 797 */ 798while((stk = dir->exclude_stack) != NULL) { 799if(stk->baselen <= baselen && 800!strncmp(dir->basebuf.buf, base, stk->baselen)) 801break; 802 el = &group->el[dir->exclude_stack->exclude_ix]; 803 dir->exclude_stack = stk->prev; 804 dir->exclude = NULL; 805free((char*)el->src);/* see strbuf_detach() below */ 806clear_exclude_list(el); 807free(stk); 808 group->nr--; 809} 810 811/* Skip traversing into sub directories if the parent is excluded */ 812if(dir->exclude) 813return; 814 815/* 816 * Lazy initialization. All call sites currently just 817 * memset(dir, 0, sizeof(*dir)) before use. Changing all of 818 * them seems lots of work for little benefit. 819 */ 820if(!dir->basebuf.buf) 821strbuf_init(&dir->basebuf, PATH_MAX); 822 823/* Read from the parent directories and push them down. */ 824 current = stk ? stk->baselen : -1; 825strbuf_setlen(&dir->basebuf, current <0?0: current); 826while(current < baselen) { 827const char*cp; 828 829 stk =xcalloc(1,sizeof(*stk)); 830if(current <0) { 831 cp = base; 832 current =0; 833}else{ 834 cp =strchr(base + current +1,'/'); 835if(!cp) 836die("oops in prep_exclude"); 837 cp++; 838} 839 stk->prev = dir->exclude_stack; 840 stk->baselen = cp - base; 841 stk->exclude_ix = group->nr; 842 el =add_exclude_list(dir, EXC_DIRS, NULL); 843strbuf_add(&dir->basebuf, base + current, stk->baselen - current); 844assert(stk->baselen == dir->basebuf.len); 845 846/* Abort if the directory is excluded */ 847if(stk->baselen) { 848int dt = DT_DIR; 849 dir->basebuf.buf[stk->baselen -1] =0; 850 dir->exclude =last_exclude_matching_from_lists(dir, 851 dir->basebuf.buf, stk->baselen -1, 852 dir->basebuf.buf + current, &dt); 853 dir->basebuf.buf[stk->baselen -1] ='/'; 854if(dir->exclude && 855 dir->exclude->flags & EXC_FLAG_NEGATIVE) 856 dir->exclude = NULL; 857if(dir->exclude) { 858 dir->exclude_stack = stk; 859return; 860} 861} 862 863/* Try to read per-directory file */ 864if(dir->exclude_per_dir) { 865/* 866 * dir->basebuf gets reused by the traversal, but we 867 * need fname to remain unchanged to ensure the src 868 * member of each struct exclude correctly 869 * back-references its source file. Other invocations 870 * of add_exclude_list provide stable strings, so we 871 * strbuf_detach() and free() here in the caller. 872 */ 873struct strbuf sb = STRBUF_INIT; 874strbuf_addbuf(&sb, &dir->basebuf); 875strbuf_addstr(&sb, dir->exclude_per_dir); 876 el->src =strbuf_detach(&sb, NULL); 877add_excludes_from_file_to_list(el->src, el->src, 878 stk->baselen, el,1); 879} 880 dir->exclude_stack = stk; 881 current = stk->baselen; 882} 883strbuf_setlen(&dir->basebuf, baselen); 884} 885 886/* 887 * Loads the exclude lists for the directory containing pathname, then 888 * scans all exclude lists to determine whether pathname is excluded. 889 * Returns the exclude_list element which matched, or NULL for 890 * undecided. 891 */ 892struct exclude *last_exclude_matching(struct dir_struct *dir, 893const char*pathname, 894int*dtype_p) 895{ 896int pathlen =strlen(pathname); 897const char*basename =strrchr(pathname,'/'); 898 basename = (basename) ? basename+1: pathname; 899 900prep_exclude(dir, pathname, basename-pathname); 901 902if(dir->exclude) 903return dir->exclude; 904 905returnlast_exclude_matching_from_lists(dir, pathname, pathlen, 906 basename, dtype_p); 907} 908 909/* 910 * Loads the exclude lists for the directory containing pathname, then 911 * scans all exclude lists to determine whether pathname is excluded. 912 * Returns 1 if true, otherwise 0. 913 */ 914intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 915{ 916struct exclude *exclude = 917last_exclude_matching(dir, pathname, dtype_p); 918if(exclude) 919return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 920return0; 921} 922 923static struct dir_entry *dir_entry_new(const char*pathname,int len) 924{ 925struct dir_entry *ent; 926 927 ent =xmalloc(sizeof(*ent) + len +1); 928 ent->len = len; 929memcpy(ent->name, pathname, len); 930 ent->name[len] =0; 931return ent; 932} 933 934static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 935{ 936if(cache_file_exists(pathname, len, ignore_case)) 937return NULL; 938 939ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 940return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 941} 942 943struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 944{ 945if(!cache_name_is_other(pathname, len)) 946return NULL; 947 948ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 949return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 950} 951 952enum exist_status { 953 index_nonexistent =0, 954 index_directory, 955 index_gitdir 956}; 957 958/* 959 * Do not use the alphabetically sorted index to look up 960 * the directory name; instead, use the case insensitive 961 * directory hash. 962 */ 963static enum exist_status directory_exists_in_index_icase(const char*dirname,int len) 964{ 965const struct cache_entry *ce =cache_dir_exists(dirname, len); 966unsigned char endchar; 967 968if(!ce) 969return index_nonexistent; 970 endchar = ce->name[len]; 971 972/* 973 * The cache_entry structure returned will contain this dirname 974 * and possibly additional path components. 975 */ 976if(endchar =='/') 977return index_directory; 978 979/* 980 * If there are no additional path components, then this cache_entry 981 * represents a submodule. Submodules, despite being directories, 982 * are stored in the cache without a closing slash. 983 */ 984if(!endchar &&S_ISGITLINK(ce->ce_mode)) 985return index_gitdir; 986 987/* This should never be hit, but it exists just in case. */ 988return index_nonexistent; 989} 990 991/* 992 * The index sorts alphabetically by entry name, which 993 * means that a gitlink sorts as '\0' at the end, while 994 * a directory (which is defined not as an entry, but as 995 * the files it contains) will sort with the '/' at the 996 * end. 997 */ 998static enum exist_status directory_exists_in_index(const char*dirname,int len) 999{1000int pos;10011002if(ignore_case)1003returndirectory_exists_in_index_icase(dirname, len);10041005 pos =cache_name_pos(dirname, len);1006if(pos <0)1007 pos = -pos-1;1008while(pos < active_nr) {1009const struct cache_entry *ce = active_cache[pos++];1010unsigned char endchar;10111012if(strncmp(ce->name, dirname, len))1013break;1014 endchar = ce->name[len];1015if(endchar >'/')1016break;1017if(endchar =='/')1018return index_directory;1019if(!endchar &&S_ISGITLINK(ce->ce_mode))1020return index_gitdir;1021}1022return index_nonexistent;1023}10241025/*1026 * When we find a directory when traversing the filesystem, we1027 * have three distinct cases:1028 *1029 * - ignore it1030 * - see it as a directory1031 * - recurse into it1032 *1033 * and which one we choose depends on a combination of existing1034 * git index contents and the flags passed into the directory1035 * traversal routine.1036 *1037 * Case 1: If we *already* have entries in the index under that1038 * directory name, we always recurse into the directory to see1039 * all the files.1040 *1041 * Case 2: If we *already* have that directory name as a gitlink,1042 * we always continue to see it as a gitlink, regardless of whether1043 * there is an actual git directory there or not (it might not1044 * be checked out as a subproject!)1045 *1046 * Case 3: if we didn't have it in the index previously, we1047 * have a few sub-cases:1048 *1049 * (a) if "show_other_directories" is true, we show it as1050 * just a directory, unless "hide_empty_directories" is1051 * also true, in which case we need to check if it contains any1052 * untracked and / or ignored files.1053 * (b) if it looks like a git directory, and we don't have1054 * 'no_gitlinks' set we treat it as a gitlink, and show it1055 * as a directory.1056 * (c) otherwise, we recurse into it.1057 */1058static enum path_treatment treat_directory(struct dir_struct *dir,1059const char*dirname,int len,int exclude,1060const struct path_simplify *simplify)1061{1062/* The "len-1" is to strip the final '/' */1063switch(directory_exists_in_index(dirname, len-1)) {1064case index_directory:1065return path_recurse;10661067case index_gitdir:1068return path_none;10691070case index_nonexistent:1071if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1072break;1073if(!(dir->flags & DIR_NO_GITLINKS)) {1074unsigned char sha1[20];1075if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1076return path_untracked;1077}1078return path_recurse;1079}10801081/* This is the "show_other_directories" case */10821083if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1084return exclude ? path_excluded : path_untracked;10851086returnread_directory_recursive(dir, dirname, len,1, simplify);1087}10881089/*1090 * This is an inexact early pruning of any recursive directory1091 * reading - if the path cannot possibly be in the pathspec,1092 * return true, and we'll skip it early.1093 */1094static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1095{1096if(simplify) {1097for(;;) {1098const char*match = simplify->path;1099int len = simplify->len;11001101if(!match)1102break;1103if(len > pathlen)1104 len = pathlen;1105if(!memcmp(path, match, len))1106return0;1107 simplify++;1108}1109return1;1110}1111return0;1112}11131114/*1115 * This function tells us whether an excluded path matches a1116 * list of "interesting" pathspecs. That is, whether a path matched1117 * by any of the pathspecs could possibly be ignored by excluding1118 * the specified path. This can happen if:1119 *1120 * 1. the path is mentioned explicitly in the pathspec1121 *1122 * 2. the path is a directory prefix of some element in the1123 * pathspec1124 */1125static intexclude_matches_pathspec(const char*path,int len,1126const struct path_simplify *simplify)1127{1128if(simplify) {1129for(; simplify->path; simplify++) {1130if(len == simplify->len1131&& !memcmp(path, simplify->path, len))1132return1;1133if(len < simplify->len1134&& simplify->path[len] =='/'1135&& !memcmp(path, simplify->path, len))1136return1;1137}1138}1139return0;1140}11411142static intget_index_dtype(const char*path,int len)1143{1144int pos;1145const struct cache_entry *ce;11461147 ce =cache_file_exists(path, len,0);1148if(ce) {1149if(!ce_uptodate(ce))1150return DT_UNKNOWN;1151if(S_ISGITLINK(ce->ce_mode))1152return DT_DIR;1153/*1154 * Nobody actually cares about the1155 * difference between DT_LNK and DT_REG1156 */1157return DT_REG;1158}11591160/* Try to look it up as a directory */1161 pos =cache_name_pos(path, len);1162if(pos >=0)1163return DT_UNKNOWN;1164 pos = -pos-1;1165while(pos < active_nr) {1166 ce = active_cache[pos++];1167if(strncmp(ce->name, path, len))1168break;1169if(ce->name[len] >'/')1170break;1171if(ce->name[len] <'/')1172continue;1173if(!ce_uptodate(ce))1174break;/* continue? */1175return DT_DIR;1176}1177return DT_UNKNOWN;1178}11791180static intget_dtype(struct dirent *de,const char*path,int len)1181{1182int dtype = de ?DTYPE(de) : DT_UNKNOWN;1183struct stat st;11841185if(dtype != DT_UNKNOWN)1186return dtype;1187 dtype =get_index_dtype(path, len);1188if(dtype != DT_UNKNOWN)1189return dtype;1190if(lstat(path, &st))1191return dtype;1192if(S_ISREG(st.st_mode))1193return DT_REG;1194if(S_ISDIR(st.st_mode))1195return DT_DIR;1196if(S_ISLNK(st.st_mode))1197return DT_LNK;1198return dtype;1199}12001201static enum path_treatment treat_one_path(struct dir_struct *dir,1202struct strbuf *path,1203const struct path_simplify *simplify,1204int dtype,struct dirent *de)1205{1206int exclude;1207int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);12081209if(dtype == DT_UNKNOWN)1210 dtype =get_dtype(de, path->buf, path->len);12111212/* Always exclude indexed files */1213if(dtype != DT_DIR && has_path_in_index)1214return path_none;12151216/*1217 * When we are looking at a directory P in the working tree,1218 * there are three cases:1219 *1220 * (1) P exists in the index. Everything inside the directory P in1221 * the working tree needs to go when P is checked out from the1222 * index.1223 *1224 * (2) P does not exist in the index, but there is P/Q in the index.1225 * We know P will stay a directory when we check out the contents1226 * of the index, but we do not know yet if there is a directory1227 * P/Q in the working tree to be killed, so we need to recurse.1228 *1229 * (3) P does not exist in the index, and there is no P/Q in the index1230 * to require P to be a directory, either. Only in this case, we1231 * know that everything inside P will not be killed without1232 * recursing.1233 */1234if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1235(dtype == DT_DIR) &&1236!has_path_in_index &&1237(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1238return path_none;12391240 exclude =is_excluded(dir, path->buf, &dtype);12411242/*1243 * Excluded? If we don't explicitly want to show1244 * ignored files, ignore it1245 */1246if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1247return path_excluded;12481249switch(dtype) {1250default:1251return path_none;1252case DT_DIR:1253strbuf_addch(path,'/');1254returntreat_directory(dir, path->buf, path->len, exclude,1255 simplify);1256case DT_REG:1257case DT_LNK:1258return exclude ? path_excluded : path_untracked;1259}1260}12611262static enum path_treatment treat_path(struct dir_struct *dir,1263struct dirent *de,1264struct strbuf *path,1265int baselen,1266const struct path_simplify *simplify)1267{1268int dtype;12691270if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1271return path_none;1272strbuf_setlen(path, baselen);1273strbuf_addstr(path, de->d_name);1274if(simplify_away(path->buf, path->len, simplify))1275return path_none;12761277 dtype =DTYPE(de);1278returntreat_one_path(dir, path, simplify, dtype, de);1279}12801281/*1282 * Read a directory tree. We currently ignore anything but1283 * directories, regular files and symlinks. That's because git1284 * doesn't handle them at all yet. Maybe that will change some1285 * day.1286 *1287 * Also, we ignore the name ".git" (even if it is not a directory).1288 * That likely will not change.1289 *1290 * Returns the most significant path_treatment value encountered in the scan.1291 */1292static enum path_treatment read_directory_recursive(struct dir_struct *dir,1293const char*base,int baselen,1294int check_only,1295const struct path_simplify *simplify)1296{1297DIR*fdir;1298enum path_treatment state, subdir_state, dir_state = path_none;1299struct dirent *de;1300struct strbuf path = STRBUF_INIT;13011302strbuf_add(&path, base, baselen);13031304 fdir =opendir(path.len ? path.buf :".");1305if(!fdir)1306goto out;13071308while((de =readdir(fdir)) != NULL) {1309/* check how the file or directory should be treated */1310 state =treat_path(dir, de, &path, baselen, simplify);1311if(state > dir_state)1312 dir_state = state;13131314/* recurse into subdir if instructed by treat_path */1315if(state == path_recurse) {1316 subdir_state =read_directory_recursive(dir, path.buf,1317 path.len, check_only, simplify);1318if(subdir_state > dir_state)1319 dir_state = subdir_state;1320}13211322if(check_only) {1323/* abort early if maximum state has been reached */1324if(dir_state == path_untracked)1325break;1326/* skip the dir_add_* part */1327continue;1328}13291330/* add the path to the appropriate result list */1331switch(state) {1332case path_excluded:1333if(dir->flags & DIR_SHOW_IGNORED)1334dir_add_name(dir, path.buf, path.len);1335else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1336((dir->flags & DIR_COLLECT_IGNORED) &&1337exclude_matches_pathspec(path.buf, path.len,1338 simplify)))1339dir_add_ignored(dir, path.buf, path.len);1340break;13411342case path_untracked:1343if(!(dir->flags & DIR_SHOW_IGNORED))1344dir_add_name(dir, path.buf, path.len);1345break;13461347default:1348break;1349}1350}1351closedir(fdir);1352 out:1353strbuf_release(&path);13541355return dir_state;1356}13571358static intcmp_name(const void*p1,const void*p2)1359{1360const struct dir_entry *e1 = *(const struct dir_entry **)p1;1361const struct dir_entry *e2 = *(const struct dir_entry **)p2;13621363returnname_compare(e1->name, e1->len, 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;1376ALLOC_GROW(simplify, nr +1, alloc);1377 match = *pathspec++;1378if(!match)1379break;1380 simplify[nr].path = match;1381 simplify[nr].len =simple_length(match);1382}1383 simplify[nr].path = NULL;1384 simplify[nr].len =0;1385return simplify;1386}13871388static voidfree_simplify(struct path_simplify *simplify)1389{1390free(simplify);1391}13921393static inttreat_leading_path(struct dir_struct *dir,1394const char*path,int len,1395const struct path_simplify *simplify)1396{1397struct strbuf sb = STRBUF_INIT;1398int baselen, rc =0;1399const char*cp;1400int old_flags = dir->flags;14011402while(len && path[len -1] =='/')1403 len--;1404if(!len)1405return1;1406 baselen =0;1407 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1408while(1) {1409 cp = path + baselen + !!baselen;1410 cp =memchr(cp,'/', path + len - cp);1411if(!cp)1412 baselen = len;1413else1414 baselen = cp - path;1415strbuf_setlen(&sb,0);1416strbuf_add(&sb, path, baselen);1417if(!is_directory(sb.buf))1418break;1419if(simplify_away(sb.buf, sb.len, simplify))1420break;1421if(treat_one_path(dir, &sb, simplify,1422 DT_DIR, NULL) == path_none)1423break;/* do not recurse into it */1424if(len <= baselen) {1425 rc =1;1426break;/* finished checking */1427}1428}1429strbuf_release(&sb);1430 dir->flags = old_flags;1431return rc;1432}14331434intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1435{1436struct path_simplify *simplify;14371438/*1439 * Check out create_simplify()1440 */1441if(pathspec)1442GUARD_PATHSPEC(pathspec,1443 PATHSPEC_FROMTOP |1444 PATHSPEC_MAXDEPTH |1445 PATHSPEC_LITERAL |1446 PATHSPEC_GLOB |1447 PATHSPEC_ICASE |1448 PATHSPEC_EXCLUDE);14491450if(has_symlink_leading_path(path, len))1451return dir->nr;14521453/*1454 * exclude patterns are treated like positive ones in1455 * create_simplify. Usually exclude patterns should be a1456 * subset of positive ones, which has no impacts on1457 * create_simplify().1458 */1459 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1460if(!len ||treat_leading_path(dir, path, len, simplify))1461read_directory_recursive(dir, path, len,0, simplify);1462free_simplify(simplify);1463qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1464qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1465return dir->nr;1466}14671468intfile_exists(const char*f)1469{1470struct stat sb;1471returnlstat(f, &sb) ==0;1472}14731474/*1475 * Given two normalized paths (a trailing slash is ok), if subdir is1476 * outside dir, return -1. Otherwise return the offset in subdir that1477 * can be used as relative path to dir.1478 */1479intdir_inside_of(const char*subdir,const char*dir)1480{1481int offset =0;14821483assert(dir && subdir && *dir && *subdir);14841485while(*dir && *subdir && *dir == *subdir) {1486 dir++;1487 subdir++;1488 offset++;1489}14901491/* hel[p]/me vs hel[l]/yeah */1492if(*dir && *subdir)1493return-1;14941495if(!*subdir)1496return!*dir ? offset : -1;/* same dir */14971498/* foo/[b]ar vs foo/[] */1499if(is_dir_sep(dir[-1]))1500returnis_dir_sep(subdir[-1]) ? offset : -1;15011502/* foo[/]bar vs foo[] */1503returnis_dir_sep(*subdir) ? offset +1: -1;1504}15051506intis_inside_dir(const char*dir)1507{1508char*cwd;1509int rc;15101511if(!dir)1512return0;15131514 cwd =xgetcwd();1515 rc = (dir_inside_of(cwd, dir) >=0);1516free(cwd);1517return rc;1518}15191520intis_empty_dir(const char*path)1521{1522DIR*dir =opendir(path);1523struct dirent *e;1524int ret =1;15251526if(!dir)1527return0;15281529while((e =readdir(dir)) != NULL)1530if(!is_dot_or_dotdot(e->d_name)) {1531 ret =0;1532break;1533}15341535closedir(dir);1536return ret;1537}15381539static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1540{1541DIR*dir;1542struct dirent *e;1543int ret =0, original_len = path->len, len, kept_down =0;1544int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1545int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1546unsigned char submodule_head[20];15471548if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1549!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1550/* Do not descend and nuke a nested git work tree. */1551if(kept_up)1552*kept_up =1;1553return0;1554}15551556 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1557 dir =opendir(path->buf);1558if(!dir) {1559if(errno == ENOENT)1560return keep_toplevel ? -1:0;1561else if(errno == EACCES && !keep_toplevel)1562/*1563 * An empty dir could be removable even if it1564 * is unreadable:1565 */1566returnrmdir(path->buf);1567else1568return-1;1569}1570if(path->buf[original_len -1] !='/')1571strbuf_addch(path,'/');15721573 len = path->len;1574while((e =readdir(dir)) != NULL) {1575struct stat st;1576if(is_dot_or_dotdot(e->d_name))1577continue;15781579strbuf_setlen(path, len);1580strbuf_addstr(path, e->d_name);1581if(lstat(path->buf, &st)) {1582if(errno == ENOENT)1583/*1584 * file disappeared, which is what we1585 * wanted anyway1586 */1587continue;1588/* fall thru */1589}else if(S_ISDIR(st.st_mode)) {1590if(!remove_dir_recurse(path, flag, &kept_down))1591continue;/* happy */1592}else if(!only_empty &&1593(!unlink(path->buf) || errno == ENOENT)) {1594continue;/* happy, too */1595}15961597/* path too long, stat fails, or non-directory still exists */1598 ret = -1;1599break;1600}1601closedir(dir);16021603strbuf_setlen(path, original_len);1604if(!ret && !keep_toplevel && !kept_down)1605 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1606else if(kept_up)1607/*1608 * report the uplevel that it is not an error that we1609 * did not rmdir() our directory.1610 */1611*kept_up = !ret;1612return ret;1613}16141615intremove_dir_recursively(struct strbuf *path,int flag)1616{1617returnremove_dir_recurse(path, flag, NULL);1618}16191620voidsetup_standard_excludes(struct dir_struct *dir)1621{1622const char*path;1623char*xdg_path;16241625 dir->exclude_per_dir =".gitignore";1626 path =git_path("info/exclude");1627if(!excludes_file) {1628home_config_paths(NULL, &xdg_path,"ignore");1629 excludes_file = xdg_path;1630}1631if(!access_or_warn(path, R_OK,0))1632add_excludes_from_file(dir, path);1633if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1634add_excludes_from_file(dir, excludes_file);1635}16361637intremove_path(const char*name)1638{1639char*slash;16401641if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1642return-1;16431644 slash =strrchr(name,'/');1645if(slash) {1646char*dirs =xstrdup(name);1647 slash = dirs + (slash - name);1648do{1649*slash ='\0';1650}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1651free(dirs);1652}1653return0;1654}16551656/*1657 * Frees memory within dir which was allocated for exclude lists and1658 * the exclude_stack. Does not free dir itself.1659 */1660voidclear_directory(struct dir_struct *dir)1661{1662int i, j;1663struct exclude_list_group *group;1664struct exclude_list *el;1665struct exclude_stack *stk;16661667for(i = EXC_CMDL; i <= EXC_FILE; i++) {1668 group = &dir->exclude_list_group[i];1669for(j =0; j < group->nr; j++) {1670 el = &group->el[j];1671if(i == EXC_DIRS)1672free((char*)el->src);1673clear_exclude_list(el);1674}1675free(group->el);1676}16771678 stk = dir->exclude_stack;1679while(stk) {1680struct exclude_stack *prev = stk->prev;1681free(stk);1682 stk = prev;1683}1684strbuf_release(&dir->basebuf);1685}