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{ 511char*p, *last_space = NULL; 512 513for(p = buf; *p; p++) 514switch(*p) { 515case' ': 516if(!last_space) 517 last_space = p; 518break; 519case'\\': 520 p++; 521if(!*p) 522return; 523/* fallthrough */ 524default: 525 last_space = NULL; 526} 527 528if(last_space) 529*last_space ='\0'; 530} 531 532intadd_excludes_from_file_to_list(const char*fname, 533const char*base, 534int baselen, 535struct exclude_list *el, 536int check_index) 537{ 538struct stat st; 539int fd, i, lineno =1; 540size_t size =0; 541char*buf, *entry; 542 543 fd =open(fname, O_RDONLY); 544if(fd <0||fstat(fd, &st) <0) { 545if(errno != ENOENT) 546warn_on_inaccessible(fname); 547if(0<= fd) 548close(fd); 549if(!check_index || 550(buf =read_skip_worktree_file_from_index(fname, &size)) == NULL) 551return-1; 552if(size ==0) { 553free(buf); 554return0; 555} 556if(buf[size-1] !='\n') { 557 buf =xrealloc(buf, size+1); 558 buf[size++] ='\n'; 559} 560}else{ 561 size =xsize_t(st.st_size); 562if(size ==0) { 563close(fd); 564return0; 565} 566 buf =xmalloc(size+1); 567if(read_in_full(fd, buf, size) != size) { 568free(buf); 569close(fd); 570return-1; 571} 572 buf[size++] ='\n'; 573close(fd); 574} 575 576 el->filebuf = buf; 577 entry = buf; 578for(i =0; i < size; i++) { 579if(buf[i] =='\n') { 580if(entry != buf + i && entry[0] !='#') { 581 buf[i - (i && buf[i-1] =='\r')] =0; 582trim_trailing_spaces(entry); 583add_exclude(entry, base, baselen, el, lineno); 584} 585 lineno++; 586 entry = buf + i +1; 587} 588} 589return0; 590} 591 592struct exclude_list *add_exclude_list(struct dir_struct *dir, 593int group_type,const char*src) 594{ 595struct exclude_list *el; 596struct exclude_list_group *group; 597 598 group = &dir->exclude_list_group[group_type]; 599ALLOC_GROW(group->el, group->nr +1, group->alloc); 600 el = &group->el[group->nr++]; 601memset(el,0,sizeof(*el)); 602 el->src = src; 603return el; 604} 605 606/* 607 * Used to set up core.excludesfile and .git/info/exclude lists. 608 */ 609voidadd_excludes_from_file(struct dir_struct *dir,const char*fname) 610{ 611struct exclude_list *el; 612 el =add_exclude_list(dir, EXC_FILE, fname); 613if(add_excludes_from_file_to_list(fname,"",0, el,0) <0) 614die("cannot use%sas an exclude file", fname); 615} 616 617intmatch_basename(const char*basename,int basenamelen, 618const char*pattern,int prefix,int patternlen, 619int flags) 620{ 621if(prefix == patternlen) { 622if(patternlen == basenamelen && 623!strncmp_icase(pattern, basename, basenamelen)) 624return1; 625}else if(flags & EXC_FLAG_ENDSWITH) { 626/* "*literal" matching against "fooliteral" */ 627if(patternlen -1<= basenamelen && 628!strncmp_icase(pattern +1, 629 basename + basenamelen - (patternlen -1), 630 patternlen -1)) 631return1; 632}else{ 633if(fnmatch_icase_mem(pattern, patternlen, 634 basename, basenamelen, 6350) ==0) 636return1; 637} 638return0; 639} 640 641intmatch_pathname(const char*pathname,int pathlen, 642const char*base,int baselen, 643const char*pattern,int prefix,int patternlen, 644int flags) 645{ 646const char*name; 647int namelen; 648 649/* 650 * match with FNM_PATHNAME; the pattern has base implicitly 651 * in front of it. 652 */ 653if(*pattern =='/') { 654 pattern++; 655 patternlen--; 656 prefix--; 657} 658 659/* 660 * baselen does not count the trailing slash. base[] may or 661 * may not end with a trailing slash though. 662 */ 663if(pathlen < baselen +1|| 664(baselen && pathname[baselen] !='/') || 665strncmp_icase(pathname, base, baselen)) 666return0; 667 668 namelen = baselen ? pathlen - baselen -1: pathlen; 669 name = pathname + pathlen - namelen; 670 671if(prefix) { 672/* 673 * if the non-wildcard part is longer than the 674 * remaining pathname, surely it cannot match. 675 */ 676if(prefix > namelen) 677return0; 678 679if(strncmp_icase(pattern, name, prefix)) 680return0; 681 pattern += prefix; 682 patternlen -= prefix; 683 name += prefix; 684 namelen -= prefix; 685 686/* 687 * If the whole pattern did not have a wildcard, 688 * then our prefix match is all we need; we 689 * do not need to call fnmatch at all. 690 */ 691if(!patternlen && !namelen) 692return1; 693} 694 695returnfnmatch_icase_mem(pattern, patternlen, 696 name, namelen, 697 WM_PATHNAME) ==0; 698} 699 700/* 701 * Scan the given exclude list in reverse to see whether pathname 702 * should be ignored. The first match (i.e. the last on the list), if 703 * any, determines the fate. Returns the exclude_list element which 704 * matched, or NULL for undecided. 705 */ 706static struct exclude *last_exclude_matching_from_list(const char*pathname, 707int pathlen, 708const char*basename, 709int*dtype, 710struct exclude_list *el) 711{ 712int i; 713 714if(!el->nr) 715return NULL;/* undefined */ 716 717for(i = el->nr -1;0<= i; i--) { 718struct exclude *x = el->excludes[i]; 719const char*exclude = x->pattern; 720int prefix = x->nowildcardlen; 721 722if(x->flags & EXC_FLAG_MUSTBEDIR) { 723if(*dtype == DT_UNKNOWN) 724*dtype =get_dtype(NULL, pathname, pathlen); 725if(*dtype != DT_DIR) 726continue; 727} 728 729if(x->flags & EXC_FLAG_NODIR) { 730if(match_basename(basename, 731 pathlen - (basename - pathname), 732 exclude, prefix, x->patternlen, 733 x->flags)) 734return x; 735continue; 736} 737 738assert(x->baselen ==0|| x->base[x->baselen -1] =='/'); 739if(match_pathname(pathname, pathlen, 740 x->base, x->baselen ? x->baselen -1:0, 741 exclude, prefix, x->patternlen, x->flags)) 742return x; 743} 744return NULL;/* undecided */ 745} 746 747/* 748 * Scan the list and let the last match determine the fate. 749 * Return 1 for exclude, 0 for include and -1 for undecided. 750 */ 751intis_excluded_from_list(const char*pathname, 752int pathlen,const char*basename,int*dtype, 753struct exclude_list *el) 754{ 755struct exclude *exclude; 756 exclude =last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el); 757if(exclude) 758return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 759return-1;/* undecided */ 760} 761 762static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir, 763const char*pathname,int pathlen,const char*basename, 764int*dtype_p) 765{ 766int i, j; 767struct exclude_list_group *group; 768struct exclude *exclude; 769for(i = EXC_CMDL; i <= EXC_FILE; i++) { 770 group = &dir->exclude_list_group[i]; 771for(j = group->nr -1; j >=0; j--) { 772 exclude =last_exclude_matching_from_list( 773 pathname, pathlen, basename, dtype_p, 774&group->el[j]); 775if(exclude) 776return exclude; 777} 778} 779return NULL; 780} 781 782/* 783 * Loads the per-directory exclude list for the substring of base 784 * which has a char length of baselen. 785 */ 786static voidprep_exclude(struct dir_struct *dir,const char*base,int baselen) 787{ 788struct exclude_list_group *group; 789struct exclude_list *el; 790struct exclude_stack *stk = NULL; 791int current; 792 793 group = &dir->exclude_list_group[EXC_DIRS]; 794 795/* 796 * Pop the exclude lists from the EXCL_DIRS exclude_list_group 797 * which originate from directories not in the prefix of the 798 * path being checked. 799 */ 800while((stk = dir->exclude_stack) != NULL) { 801if(stk->baselen <= baselen && 802!strncmp(dir->basebuf.buf, base, stk->baselen)) 803break; 804 el = &group->el[dir->exclude_stack->exclude_ix]; 805 dir->exclude_stack = stk->prev; 806 dir->exclude = NULL; 807free((char*)el->src);/* see strbuf_detach() below */ 808clear_exclude_list(el); 809free(stk); 810 group->nr--; 811} 812 813/* Skip traversing into sub directories if the parent is excluded */ 814if(dir->exclude) 815return; 816 817/* 818 * Lazy initialization. All call sites currently just 819 * memset(dir, 0, sizeof(*dir)) before use. Changing all of 820 * them seems lots of work for little benefit. 821 */ 822if(!dir->basebuf.buf) 823strbuf_init(&dir->basebuf, PATH_MAX); 824 825/* Read from the parent directories and push them down. */ 826 current = stk ? stk->baselen : -1; 827strbuf_setlen(&dir->basebuf, current <0?0: current); 828while(current < baselen) { 829struct exclude_stack *stk =xcalloc(1,sizeof(*stk)); 830const char*cp; 831 832if(current <0) { 833 cp = base; 834 current =0; 835}else{ 836 cp =strchr(base + current +1,'/'); 837if(!cp) 838die("oops in prep_exclude"); 839 cp++; 840} 841 stk->prev = dir->exclude_stack; 842 stk->baselen = cp - base; 843 stk->exclude_ix = group->nr; 844 el =add_exclude_list(dir, EXC_DIRS, NULL); 845strbuf_add(&dir->basebuf, base + current, stk->baselen - current); 846assert(stk->baselen == dir->basebuf.len); 847 848/* Abort if the directory is excluded */ 849if(stk->baselen) { 850int dt = DT_DIR; 851 dir->basebuf.buf[stk->baselen -1] =0; 852 dir->exclude =last_exclude_matching_from_lists(dir, 853 dir->basebuf.buf, stk->baselen -1, 854 dir->basebuf.buf + current, &dt); 855 dir->basebuf.buf[stk->baselen -1] ='/'; 856if(dir->exclude && 857 dir->exclude->flags & EXC_FLAG_NEGATIVE) 858 dir->exclude = NULL; 859if(dir->exclude) { 860 dir->exclude_stack = stk; 861return; 862} 863} 864 865/* Try to read per-directory file */ 866if(dir->exclude_per_dir) { 867/* 868 * dir->basebuf gets reused by the traversal, but we 869 * need fname to remain unchanged to ensure the src 870 * member of each struct exclude correctly 871 * back-references its source file. Other invocations 872 * of add_exclude_list provide stable strings, so we 873 * strbuf_detach() and free() here in the caller. 874 */ 875struct strbuf sb = STRBUF_INIT; 876strbuf_addbuf(&sb, &dir->basebuf); 877strbuf_addstr(&sb, dir->exclude_per_dir); 878 el->src =strbuf_detach(&sb, NULL); 879add_excludes_from_file_to_list(el->src, el->src, 880 stk->baselen, el,1); 881} 882 dir->exclude_stack = stk; 883 current = stk->baselen; 884} 885strbuf_setlen(&dir->basebuf, baselen); 886} 887 888/* 889 * Loads the exclude lists for the directory containing pathname, then 890 * scans all exclude lists to determine whether pathname is excluded. 891 * Returns the exclude_list element which matched, or NULL for 892 * undecided. 893 */ 894struct exclude *last_exclude_matching(struct dir_struct *dir, 895const char*pathname, 896int*dtype_p) 897{ 898int pathlen =strlen(pathname); 899const char*basename =strrchr(pathname,'/'); 900 basename = (basename) ? basename+1: pathname; 901 902prep_exclude(dir, pathname, basename-pathname); 903 904if(dir->exclude) 905return dir->exclude; 906 907returnlast_exclude_matching_from_lists(dir, pathname, pathlen, 908 basename, dtype_p); 909} 910 911/* 912 * Loads the exclude lists for the directory containing pathname, then 913 * scans all exclude lists to determine whether pathname is excluded. 914 * Returns 1 if true, otherwise 0. 915 */ 916intis_excluded(struct dir_struct *dir,const char*pathname,int*dtype_p) 917{ 918struct exclude *exclude = 919last_exclude_matching(dir, pathname, dtype_p); 920if(exclude) 921return exclude->flags & EXC_FLAG_NEGATIVE ?0:1; 922return0; 923} 924 925static struct dir_entry *dir_entry_new(const char*pathname,int len) 926{ 927struct dir_entry *ent; 928 929 ent =xmalloc(sizeof(*ent) + len +1); 930 ent->len = len; 931memcpy(ent->name, pathname, len); 932 ent->name[len] =0; 933return ent; 934} 935 936static struct dir_entry *dir_add_name(struct dir_struct *dir,const char*pathname,int len) 937{ 938if(cache_file_exists(pathname, len, ignore_case)) 939return NULL; 940 941ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc); 942return dir->entries[dir->nr++] =dir_entry_new(pathname, len); 943} 944 945struct dir_entry *dir_add_ignored(struct dir_struct *dir,const char*pathname,int len) 946{ 947if(!cache_name_is_other(pathname, len)) 948return NULL; 949 950ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc); 951return dir->ignored[dir->ignored_nr++] =dir_entry_new(pathname, len); 952} 953 954enum exist_status { 955 index_nonexistent =0, 956 index_directory, 957 index_gitdir 958}; 959 960/* 961 * Do not use the alphabetically sorted index to look up 962 * the directory name; instead, use the case insensitive 963 * directory hash. 964 */ 965static enum exist_status directory_exists_in_index_icase(const char*dirname,int len) 966{ 967const struct cache_entry *ce =cache_dir_exists(dirname, len); 968unsigned char endchar; 969 970if(!ce) 971return index_nonexistent; 972 endchar = ce->name[len]; 973 974/* 975 * The cache_entry structure returned will contain this dirname 976 * and possibly additional path components. 977 */ 978if(endchar =='/') 979return index_directory; 980 981/* 982 * If there are no additional path components, then this cache_entry 983 * represents a submodule. Submodules, despite being directories, 984 * are stored in the cache without a closing slash. 985 */ 986if(!endchar &&S_ISGITLINK(ce->ce_mode)) 987return index_gitdir; 988 989/* This should never be hit, but it exists just in case. */ 990return index_nonexistent; 991} 992 993/* 994 * The index sorts alphabetically by entry name, which 995 * means that a gitlink sorts as '\0' at the end, while 996 * a directory (which is defined not as an entry, but as 997 * the files it contains) will sort with the '/' at the 998 * end. 999 */1000static enum exist_status directory_exists_in_index(const char*dirname,int len)1001{1002int pos;10031004if(ignore_case)1005returndirectory_exists_in_index_icase(dirname, len);10061007 pos =cache_name_pos(dirname, len);1008if(pos <0)1009 pos = -pos-1;1010while(pos < active_nr) {1011const struct cache_entry *ce = active_cache[pos++];1012unsigned char endchar;10131014if(strncmp(ce->name, dirname, len))1015break;1016 endchar = ce->name[len];1017if(endchar >'/')1018break;1019if(endchar =='/')1020return index_directory;1021if(!endchar &&S_ISGITLINK(ce->ce_mode))1022return index_gitdir;1023}1024return index_nonexistent;1025}10261027/*1028 * When we find a directory when traversing the filesystem, we1029 * have three distinct cases:1030 *1031 * - ignore it1032 * - see it as a directory1033 * - recurse into it1034 *1035 * and which one we choose depends on a combination of existing1036 * git index contents and the flags passed into the directory1037 * traversal routine.1038 *1039 * Case 1: If we *already* have entries in the index under that1040 * directory name, we always recurse into the directory to see1041 * all the files.1042 *1043 * Case 2: If we *already* have that directory name as a gitlink,1044 * we always continue to see it as a gitlink, regardless of whether1045 * there is an actual git directory there or not (it might not1046 * be checked out as a subproject!)1047 *1048 * Case 3: if we didn't have it in the index previously, we1049 * have a few sub-cases:1050 *1051 * (a) if "show_other_directories" is true, we show it as1052 * just a directory, unless "hide_empty_directories" is1053 * also true, in which case we need to check if it contains any1054 * untracked and / or ignored files.1055 * (b) if it looks like a git directory, and we don't have1056 * 'no_gitlinks' set we treat it as a gitlink, and show it1057 * as a directory.1058 * (c) otherwise, we recurse into it.1059 */1060static enum path_treatment treat_directory(struct dir_struct *dir,1061const char*dirname,int len,int exclude,1062const struct path_simplify *simplify)1063{1064/* The "len-1" is to strip the final '/' */1065switch(directory_exists_in_index(dirname, len-1)) {1066case index_directory:1067return path_recurse;10681069case index_gitdir:1070return path_none;10711072case index_nonexistent:1073if(dir->flags & DIR_SHOW_OTHER_DIRECTORIES)1074break;1075if(!(dir->flags & DIR_NO_GITLINKS)) {1076unsigned char sha1[20];1077if(resolve_gitlink_ref(dirname,"HEAD", sha1) ==0)1078return path_untracked;1079}1080return path_recurse;1081}10821083/* This is the "show_other_directories" case */10841085if(!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))1086return exclude ? path_excluded : path_untracked;10871088returnread_directory_recursive(dir, dirname, len,1, simplify);1089}10901091/*1092 * This is an inexact early pruning of any recursive directory1093 * reading - if the path cannot possibly be in the pathspec,1094 * return true, and we'll skip it early.1095 */1096static intsimplify_away(const char*path,int pathlen,const struct path_simplify *simplify)1097{1098if(simplify) {1099for(;;) {1100const char*match = simplify->path;1101int len = simplify->len;11021103if(!match)1104break;1105if(len > pathlen)1106 len = pathlen;1107if(!memcmp(path, match, len))1108return0;1109 simplify++;1110}1111return1;1112}1113return0;1114}11151116/*1117 * This function tells us whether an excluded path matches a1118 * list of "interesting" pathspecs. That is, whether a path matched1119 * by any of the pathspecs could possibly be ignored by excluding1120 * the specified path. This can happen if:1121 *1122 * 1. the path is mentioned explicitly in the pathspec1123 *1124 * 2. the path is a directory prefix of some element in the1125 * pathspec1126 */1127static intexclude_matches_pathspec(const char*path,int len,1128const struct path_simplify *simplify)1129{1130if(simplify) {1131for(; simplify->path; simplify++) {1132if(len == simplify->len1133&& !memcmp(path, simplify->path, len))1134return1;1135if(len < simplify->len1136&& simplify->path[len] =='/'1137&& !memcmp(path, simplify->path, len))1138return1;1139}1140}1141return0;1142}11431144static intget_index_dtype(const char*path,int len)1145{1146int pos;1147const struct cache_entry *ce;11481149 ce =cache_file_exists(path, len,0);1150if(ce) {1151if(!ce_uptodate(ce))1152return DT_UNKNOWN;1153if(S_ISGITLINK(ce->ce_mode))1154return DT_DIR;1155/*1156 * Nobody actually cares about the1157 * difference between DT_LNK and DT_REG1158 */1159return DT_REG;1160}11611162/* Try to look it up as a directory */1163 pos =cache_name_pos(path, len);1164if(pos >=0)1165return DT_UNKNOWN;1166 pos = -pos-1;1167while(pos < active_nr) {1168 ce = active_cache[pos++];1169if(strncmp(ce->name, path, len))1170break;1171if(ce->name[len] >'/')1172break;1173if(ce->name[len] <'/')1174continue;1175if(!ce_uptodate(ce))1176break;/* continue? */1177return DT_DIR;1178}1179return DT_UNKNOWN;1180}11811182static intget_dtype(struct dirent *de,const char*path,int len)1183{1184int dtype = de ?DTYPE(de) : DT_UNKNOWN;1185struct stat st;11861187if(dtype != DT_UNKNOWN)1188return dtype;1189 dtype =get_index_dtype(path, len);1190if(dtype != DT_UNKNOWN)1191return dtype;1192if(lstat(path, &st))1193return dtype;1194if(S_ISREG(st.st_mode))1195return DT_REG;1196if(S_ISDIR(st.st_mode))1197return DT_DIR;1198if(S_ISLNK(st.st_mode))1199return DT_LNK;1200return dtype;1201}12021203static enum path_treatment treat_one_path(struct dir_struct *dir,1204struct strbuf *path,1205const struct path_simplify *simplify,1206int dtype,struct dirent *de)1207{1208int exclude;1209int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);12101211if(dtype == DT_UNKNOWN)1212 dtype =get_dtype(de, path->buf, path->len);12131214/* Always exclude indexed files */1215if(dtype != DT_DIR && has_path_in_index)1216return path_none;12171218/*1219 * When we are looking at a directory P in the working tree,1220 * there are three cases:1221 *1222 * (1) P exists in the index. Everything inside the directory P in1223 * the working tree needs to go when P is checked out from the1224 * index.1225 *1226 * (2) P does not exist in the index, but there is P/Q in the index.1227 * We know P will stay a directory when we check out the contents1228 * of the index, but we do not know yet if there is a directory1229 * P/Q in the working tree to be killed, so we need to recurse.1230 *1231 * (3) P does not exist in the index, and there is no P/Q in the index1232 * to require P to be a directory, either. Only in this case, we1233 * know that everything inside P will not be killed without1234 * recursing.1235 */1236if((dir->flags & DIR_COLLECT_KILLED_ONLY) &&1237(dtype == DT_DIR) &&1238!has_path_in_index &&1239(directory_exists_in_index(path->buf, path->len) == index_nonexistent))1240return path_none;12411242 exclude =is_excluded(dir, path->buf, &dtype);12431244/*1245 * Excluded? If we don't explicitly want to show1246 * ignored files, ignore it1247 */1248if(exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))1249return path_excluded;12501251switch(dtype) {1252default:1253return path_none;1254case DT_DIR:1255strbuf_addch(path,'/');1256returntreat_directory(dir, path->buf, path->len, exclude,1257 simplify);1258case DT_REG:1259case DT_LNK:1260return exclude ? path_excluded : path_untracked;1261}1262}12631264static enum path_treatment treat_path(struct dir_struct *dir,1265struct dirent *de,1266struct strbuf *path,1267int baselen,1268const struct path_simplify *simplify)1269{1270int dtype;12711272if(is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name,".git"))1273return path_none;1274strbuf_setlen(path, baselen);1275strbuf_addstr(path, de->d_name);1276if(simplify_away(path->buf, path->len, simplify))1277return path_none;12781279 dtype =DTYPE(de);1280returntreat_one_path(dir, path, simplify, dtype, de);1281}12821283/*1284 * Read a directory tree. We currently ignore anything but1285 * directories, regular files and symlinks. That's because git1286 * doesn't handle them at all yet. Maybe that will change some1287 * day.1288 *1289 * Also, we ignore the name ".git" (even if it is not a directory).1290 * That likely will not change.1291 *1292 * Returns the most significant path_treatment value encountered in the scan.1293 */1294static enum path_treatment read_directory_recursive(struct dir_struct *dir,1295const char*base,int baselen,1296int check_only,1297const struct path_simplify *simplify)1298{1299DIR*fdir;1300enum path_treatment state, subdir_state, dir_state = path_none;1301struct dirent *de;1302struct strbuf path = STRBUF_INIT;13031304strbuf_add(&path, base, baselen);13051306 fdir =opendir(path.len ? path.buf :".");1307if(!fdir)1308goto out;13091310while((de =readdir(fdir)) != NULL) {1311/* check how the file or directory should be treated */1312 state =treat_path(dir, de, &path, baselen, simplify);1313if(state > dir_state)1314 dir_state = state;13151316/* recurse into subdir if instructed by treat_path */1317if(state == path_recurse) {1318 subdir_state =read_directory_recursive(dir, path.buf,1319 path.len, check_only, simplify);1320if(subdir_state > dir_state)1321 dir_state = subdir_state;1322}13231324if(check_only) {1325/* abort early if maximum state has been reached */1326if(dir_state == path_untracked)1327break;1328/* skip the dir_add_* part */1329continue;1330}13311332/* add the path to the appropriate result list */1333switch(state) {1334case path_excluded:1335if(dir->flags & DIR_SHOW_IGNORED)1336dir_add_name(dir, path.buf, path.len);1337else if((dir->flags & DIR_SHOW_IGNORED_TOO) ||1338((dir->flags & DIR_COLLECT_IGNORED) &&1339exclude_matches_pathspec(path.buf, path.len,1340 simplify)))1341dir_add_ignored(dir, path.buf, path.len);1342break;13431344case path_untracked:1345if(!(dir->flags & DIR_SHOW_IGNORED))1346dir_add_name(dir, path.buf, path.len);1347break;13481349default:1350break;1351}1352}1353closedir(fdir);1354 out:1355strbuf_release(&path);13561357return dir_state;1358}13591360static intcmp_name(const void*p1,const void*p2)1361{1362const struct dir_entry *e1 = *(const struct dir_entry **)p1;1363const struct dir_entry *e2 = *(const struct dir_entry **)p2;13641365returnname_compare(e1->name, e1->len, e2->name, e2->len);1366}13671368static struct path_simplify *create_simplify(const char**pathspec)1369{1370int nr, alloc =0;1371struct path_simplify *simplify = NULL;13721373if(!pathspec)1374return NULL;13751376for(nr =0; ; nr++) {1377const char*match;1378ALLOC_GROW(simplify, nr +1, alloc);1379 match = *pathspec++;1380if(!match)1381break;1382 simplify[nr].path = match;1383 simplify[nr].len =simple_length(match);1384}1385 simplify[nr].path = NULL;1386 simplify[nr].len =0;1387return simplify;1388}13891390static voidfree_simplify(struct path_simplify *simplify)1391{1392free(simplify);1393}13941395static inttreat_leading_path(struct dir_struct *dir,1396const char*path,int len,1397const struct path_simplify *simplify)1398{1399struct strbuf sb = STRBUF_INIT;1400int baselen, rc =0;1401const char*cp;1402int old_flags = dir->flags;14031404while(len && path[len -1] =='/')1405 len--;1406if(!len)1407return1;1408 baselen =0;1409 dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;1410while(1) {1411 cp = path + baselen + !!baselen;1412 cp =memchr(cp,'/', path + len - cp);1413if(!cp)1414 baselen = len;1415else1416 baselen = cp - path;1417strbuf_setlen(&sb,0);1418strbuf_add(&sb, path, baselen);1419if(!is_directory(sb.buf))1420break;1421if(simplify_away(sb.buf, sb.len, simplify))1422break;1423if(treat_one_path(dir, &sb, simplify,1424 DT_DIR, NULL) == path_none)1425break;/* do not recurse into it */1426if(len <= baselen) {1427 rc =1;1428break;/* finished checking */1429}1430}1431strbuf_release(&sb);1432 dir->flags = old_flags;1433return rc;1434}14351436intread_directory(struct dir_struct *dir,const char*path,int len,const struct pathspec *pathspec)1437{1438struct path_simplify *simplify;14391440/*1441 * Check out create_simplify()1442 */1443if(pathspec)1444GUARD_PATHSPEC(pathspec,1445 PATHSPEC_FROMTOP |1446 PATHSPEC_MAXDEPTH |1447 PATHSPEC_LITERAL |1448 PATHSPEC_GLOB |1449 PATHSPEC_ICASE |1450 PATHSPEC_EXCLUDE);14511452if(has_symlink_leading_path(path, len))1453return dir->nr;14541455/*1456 * exclude patterns are treated like positive ones in1457 * create_simplify. Usually exclude patterns should be a1458 * subset of positive ones, which has no impacts on1459 * create_simplify().1460 */1461 simplify =create_simplify(pathspec ? pathspec->_raw : NULL);1462if(!len ||treat_leading_path(dir, path, len, simplify))1463read_directory_recursive(dir, path, len,0, simplify);1464free_simplify(simplify);1465qsort(dir->entries, dir->nr,sizeof(struct dir_entry *), cmp_name);1466qsort(dir->ignored, dir->ignored_nr,sizeof(struct dir_entry *), cmp_name);1467return dir->nr;1468}14691470intfile_exists(const char*f)1471{1472struct stat sb;1473returnlstat(f, &sb) ==0;1474}14751476/*1477 * Given two normalized paths (a trailing slash is ok), if subdir is1478 * outside dir, return -1. Otherwise return the offset in subdir that1479 * can be used as relative path to dir.1480 */1481intdir_inside_of(const char*subdir,const char*dir)1482{1483int offset =0;14841485assert(dir && subdir && *dir && *subdir);14861487while(*dir && *subdir && *dir == *subdir) {1488 dir++;1489 subdir++;1490 offset++;1491}14921493/* hel[p]/me vs hel[l]/yeah */1494if(*dir && *subdir)1495return-1;14961497if(!*subdir)1498return!*dir ? offset : -1;/* same dir */14991500/* foo/[b]ar vs foo/[] */1501if(is_dir_sep(dir[-1]))1502returnis_dir_sep(subdir[-1]) ? offset : -1;15031504/* foo[/]bar vs foo[] */1505returnis_dir_sep(*subdir) ? offset +1: -1;1506}15071508intis_inside_dir(const char*dir)1509{1510char*cwd;1511int rc;15121513if(!dir)1514return0;15151516 cwd =xgetcwd();1517 rc = (dir_inside_of(cwd, dir) >=0);1518free(cwd);1519return rc;1520}15211522intis_empty_dir(const char*path)1523{1524DIR*dir =opendir(path);1525struct dirent *e;1526int ret =1;15271528if(!dir)1529return0;15301531while((e =readdir(dir)) != NULL)1532if(!is_dot_or_dotdot(e->d_name)) {1533 ret =0;1534break;1535}15361537closedir(dir);1538return ret;1539}15401541static intremove_dir_recurse(struct strbuf *path,int flag,int*kept_up)1542{1543DIR*dir;1544struct dirent *e;1545int ret =0, original_len = path->len, len, kept_down =0;1546int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);1547int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);1548unsigned char submodule_head[20];15491550if((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&1551!resolve_gitlink_ref(path->buf,"HEAD", submodule_head)) {1552/* Do not descend and nuke a nested git work tree. */1553if(kept_up)1554*kept_up =1;1555return0;1556}15571558 flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;1559 dir =opendir(path->buf);1560if(!dir) {1561if(errno == ENOENT)1562return keep_toplevel ? -1:0;1563else if(errno == EACCES && !keep_toplevel)1564/*1565 * An empty dir could be removable even if it1566 * is unreadable:1567 */1568returnrmdir(path->buf);1569else1570return-1;1571}1572if(path->buf[original_len -1] !='/')1573strbuf_addch(path,'/');15741575 len = path->len;1576while((e =readdir(dir)) != NULL) {1577struct stat st;1578if(is_dot_or_dotdot(e->d_name))1579continue;15801581strbuf_setlen(path, len);1582strbuf_addstr(path, e->d_name);1583if(lstat(path->buf, &st)) {1584if(errno == ENOENT)1585/*1586 * file disappeared, which is what we1587 * wanted anyway1588 */1589continue;1590/* fall thru */1591}else if(S_ISDIR(st.st_mode)) {1592if(!remove_dir_recurse(path, flag, &kept_down))1593continue;/* happy */1594}else if(!only_empty &&1595(!unlink(path->buf) || errno == ENOENT)) {1596continue;/* happy, too */1597}15981599/* path too long, stat fails, or non-directory still exists */1600 ret = -1;1601break;1602}1603closedir(dir);16041605strbuf_setlen(path, original_len);1606if(!ret && !keep_toplevel && !kept_down)1607 ret = (!rmdir(path->buf) || errno == ENOENT) ?0: -1;1608else if(kept_up)1609/*1610 * report the uplevel that it is not an error that we1611 * did not rmdir() our directory.1612 */1613*kept_up = !ret;1614return ret;1615}16161617intremove_dir_recursively(struct strbuf *path,int flag)1618{1619returnremove_dir_recurse(path, flag, NULL);1620}16211622voidsetup_standard_excludes(struct dir_struct *dir)1623{1624const char*path;1625char*xdg_path;16261627 dir->exclude_per_dir =".gitignore";1628 path =git_path("info/exclude");1629if(!excludes_file) {1630home_config_paths(NULL, &xdg_path,"ignore");1631 excludes_file = xdg_path;1632}1633if(!access_or_warn(path, R_OK,0))1634add_excludes_from_file(dir, path);1635if(excludes_file && !access_or_warn(excludes_file, R_OK,0))1636add_excludes_from_file(dir, excludes_file);1637}16381639intremove_path(const char*name)1640{1641char*slash;16421643if(unlink(name) && errno != ENOENT && errno != ENOTDIR)1644return-1;16451646 slash =strrchr(name,'/');1647if(slash) {1648char*dirs =xstrdup(name);1649 slash = dirs + (slash - name);1650do{1651*slash ='\0';1652}while(rmdir(dirs) ==0&& (slash =strrchr(dirs,'/')));1653free(dirs);1654}1655return0;1656}16571658/*1659 * Frees memory within dir which was allocated for exclude lists and1660 * the exclude_stack. Does not free dir itself.1661 */1662voidclear_directory(struct dir_struct *dir)1663{1664int i, j;1665struct exclude_list_group *group;1666struct exclude_list *el;1667struct exclude_stack *stk;16681669for(i = EXC_CMDL; i <= EXC_FILE; i++) {1670 group = &dir->exclude_list_group[i];1671for(j =0; j < group->nr; j++) {1672 el = &group->el[j];1673if(i == EXC_DIRS)1674free((char*)el->src);1675clear_exclude_list(el);1676}1677free(group->el);1678}16791680 stk = dir->exclude_stack;1681while(stk) {1682struct exclude_stack *prev = stk->prev;1683free(stk);1684 stk = prev;1685}1686strbuf_release(&dir->basebuf);1687}