f39024c6396bf456a9ecbfa8dc886e83aebeb3d4
   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 {
  17        int len;
  18        const 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
  34/*
  35 * Support data structure for our opendir/readdir/closedir wrappers
  36 */
  37struct cached_dir {
  38        DIR *fdir;
  39        struct untracked_cache_dir *untracked;
  40        int nr_files;
  41        int nr_dirs;
  42
  43        struct dirent *de;
  44        const char *file;
  45        struct untracked_cache_dir *ucd;
  46};
  47
  48static enum path_treatment read_directory_recursive(struct dir_struct *dir,
  49        const char *path, int len, struct untracked_cache_dir *untracked,
  50        int check_only, const struct path_simplify *simplify);
  51static int get_dtype(struct dirent *de, const char *path, int len);
  52
  53/* helper string functions with support for the ignore_case flag */
  54int strcmp_icase(const char *a, const char *b)
  55{
  56        return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
  57}
  58
  59int strncmp_icase(const char *a, const char *b, size_t count)
  60{
  61        return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
  62}
  63
  64int fnmatch_icase(const char *pattern, const char *string, int flags)
  65{
  66        return wildmatch(pattern, string,
  67                         flags | (ignore_case ? WM_CASEFOLD : 0),
  68                         NULL);
  69}
  70
  71int git_fnmatch(const struct pathspec_item *item,
  72                const char *pattern, const char *string,
  73                int prefix)
  74{
  75        if (prefix > 0) {
  76                if (ps_strncmp(item, pattern, string, prefix))
  77                        return WM_NOMATCH;
  78                pattern += prefix;
  79                string += prefix;
  80        }
  81        if (item->flags & PATHSPEC_ONESTAR) {
  82                int pattern_len = strlen(++pattern);
  83                int string_len = strlen(string);
  84                return string_len < pattern_len ||
  85                        ps_strcmp(item, pattern,
  86                                  string + string_len - pattern_len);
  87        }
  88        if (item->magic & PATHSPEC_GLOB)
  89                return wildmatch(pattern, string,
  90                                 WM_PATHNAME |
  91                                 (item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0),
  92                                 NULL);
  93        else
  94                /* wildmatch has not learned no FNM_PATHNAME mode yet */
  95                return wildmatch(pattern, string,
  96                                 item->magic & PATHSPEC_ICASE ? WM_CASEFOLD : 0,
  97                                 NULL);
  98}
  99
 100static int fnmatch_icase_mem(const char *pattern, int patternlen,
 101                             const char *string, int stringlen,
 102                             int flags)
 103{
 104        int match_status;
 105        struct strbuf pat_buf = STRBUF_INIT;
 106        struct strbuf str_buf = STRBUF_INIT;
 107        const char *use_pat = pattern;
 108        const char *use_str = string;
 109
 110        if (pattern[patternlen]) {
 111                strbuf_add(&pat_buf, pattern, patternlen);
 112                use_pat = pat_buf.buf;
 113        }
 114        if (string[stringlen]) {
 115                strbuf_add(&str_buf, string, stringlen);
 116                use_str = str_buf.buf;
 117        }
 118
 119        if (ignore_case)
 120                flags |= WM_CASEFOLD;
 121        match_status = wildmatch(use_pat, use_str, flags, NULL);
 122
 123        strbuf_release(&pat_buf);
 124        strbuf_release(&str_buf);
 125
 126        return match_status;
 127}
 128
 129static size_t common_prefix_len(const struct pathspec *pathspec)
 130{
 131        int n;
 132        size_t max = 0;
 133
 134        /*
 135         * ":(icase)path" is treated as a pathspec full of
 136         * wildcard. In other words, only prefix is considered common
 137         * prefix. If the pathspec is abc/foo abc/bar, running in
 138         * subdir xyz, the common prefix is still xyz, not xuz/abc as
 139         * in non-:(icase).
 140         */
 141        GUARD_PATHSPEC(pathspec,
 142                       PATHSPEC_FROMTOP |
 143                       PATHSPEC_MAXDEPTH |
 144                       PATHSPEC_LITERAL |
 145                       PATHSPEC_GLOB |
 146                       PATHSPEC_ICASE |
 147                       PATHSPEC_EXCLUDE);
 148
 149        for (n = 0; n < pathspec->nr; n++) {
 150                size_t i = 0, len = 0, item_len;
 151                if (pathspec->items[n].magic & PATHSPEC_EXCLUDE)
 152                        continue;
 153                if (pathspec->items[n].magic & PATHSPEC_ICASE)
 154                        item_len = pathspec->items[n].prefix;
 155                else
 156                        item_len = pathspec->items[n].nowildcard_len;
 157                while (i < item_len && (n == 0 || i < max)) {
 158                        char c = pathspec->items[n].match[i];
 159                        if (c != pathspec->items[0].match[i])
 160                                break;
 161                        if (c == '/')
 162                                len = i + 1;
 163                        i++;
 164                }
 165                if (n == 0 || len < max) {
 166                        max = len;
 167                        if (!max)
 168                                break;
 169                }
 170        }
 171        return max;
 172}
 173
 174/*
 175 * Returns a copy of the longest leading path common among all
 176 * pathspecs.
 177 */
 178char *common_prefix(const struct pathspec *pathspec)
 179{
 180        unsigned long len = common_prefix_len(pathspec);
 181
 182        return len ? xmemdupz(pathspec->items[0].match, len) : NULL;
 183}
 184
 185int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec)
 186{
 187        size_t len;
 188
 189        /*
 190         * Calculate common prefix for the pathspec, and
 191         * use that to optimize the directory walk
 192         */
 193        len = common_prefix_len(pathspec);
 194
 195        /* Read the directory and prune it */
 196        read_directory(dir, pathspec->nr ? pathspec->_raw[0] : "", len, pathspec);
 197        return len;
 198}
 199
 200int within_depth(const char *name, int namelen,
 201                        int depth, int max_depth)
 202{
 203        const char *cp = name, *cpe = name + namelen;
 204
 205        while (cp < cpe) {
 206                if (*cp++ != '/')
 207                        continue;
 208                depth++;
 209                if (depth > max_depth)
 210                        return 0;
 211        }
 212        return 1;
 213}
 214
 215#define DO_MATCH_EXCLUDE   1
 216#define DO_MATCH_DIRECTORY 2
 217
 218/*
 219 * Does 'match' match the given name?
 220 * A match is found if
 221 *
 222 * (1) the 'match' string is leading directory of 'name', or
 223 * (2) the 'match' string is a wildcard and matches 'name', or
 224 * (3) the 'match' string is exactly the same as 'name'.
 225 *
 226 * and the return value tells which case it was.
 227 *
 228 * It returns 0 when there is no match.
 229 */
 230static int match_pathspec_item(const struct pathspec_item *item, int prefix,
 231                               const char *name, int namelen, unsigned flags)
 232{
 233        /* name/namelen has prefix cut off by caller */
 234        const char *match = item->match + prefix;
 235        int matchlen = item->len - prefix;
 236
 237        /*
 238         * The normal call pattern is:
 239         * 1. prefix = common_prefix_len(ps);
 240         * 2. prune something, or fill_directory
 241         * 3. match_pathspec()
 242         *
 243         * 'prefix' at #1 may be shorter than the command's prefix and
 244         * it's ok for #2 to match extra files. Those extras will be
 245         * trimmed at #3.
 246         *
 247         * Suppose the pathspec is 'foo' and '../bar' running from
 248         * subdir 'xyz'. The common prefix at #1 will be empty, thanks
 249         * to "../". We may have xyz/foo _and_ XYZ/foo after #2. The
 250         * user does not want XYZ/foo, only the "foo" part should be
 251         * case-insensitive. We need to filter out XYZ/foo here. In
 252         * other words, we do not trust the caller on comparing the
 253         * prefix part when :(icase) is involved. We do exact
 254         * comparison ourselves.
 255         *
 256         * Normally the caller (common_prefix_len() in fact) does
 257         * _exact_ matching on name[-prefix+1..-1] and we do not need
 258         * to check that part. Be defensive and check it anyway, in
 259         * case common_prefix_len is changed, or a new caller is
 260         * introduced that does not use common_prefix_len.
 261         *
 262         * If the penalty turns out too high when prefix is really
 263         * long, maybe change it to
 264         * strncmp(match, name, item->prefix - prefix)
 265         */
 266        if (item->prefix && (item->magic & PATHSPEC_ICASE) &&
 267            strncmp(item->match, name - prefix, item->prefix))
 268                return 0;
 269
 270        /* If the match was just the prefix, we matched */
 271        if (!*match)
 272                return MATCHED_RECURSIVELY;
 273
 274        if (matchlen <= namelen && !ps_strncmp(item, match, name, matchlen)) {
 275                if (matchlen == namelen)
 276                        return MATCHED_EXACTLY;
 277
 278                if (match[matchlen-1] == '/' || name[matchlen] == '/')
 279                        return MATCHED_RECURSIVELY;
 280        } else if ((flags & DO_MATCH_DIRECTORY) &&
 281                   match[matchlen - 1] == '/' &&
 282                   namelen == matchlen - 1 &&
 283                   !ps_strncmp(item, match, name, namelen))
 284                return MATCHED_EXACTLY;
 285
 286        if (item->nowildcard_len < item->len &&
 287            !git_fnmatch(item, match, name,
 288                         item->nowildcard_len - prefix))
 289                return MATCHED_FNMATCH;
 290
 291        return 0;
 292}
 293
 294/*
 295 * Given a name and a list of pathspecs, returns the nature of the
 296 * closest (i.e. most specific) match of the name to any of the
 297 * pathspecs.
 298 *
 299 * The caller typically calls this multiple times with the same
 300 * pathspec and seen[] array but with different name/namelen
 301 * (e.g. entries from the index) and is interested in seeing if and
 302 * how each pathspec matches all the names it calls this function
 303 * with.  A mark is left in the seen[] array for each pathspec element
 304 * indicating the closest type of match that element achieved, so if
 305 * seen[n] remains zero after multiple invocations, that means the nth
 306 * pathspec did not match any names, which could indicate that the
 307 * user mistyped the nth pathspec.
 308 */
 309static int do_match_pathspec(const struct pathspec *ps,
 310                             const char *name, int namelen,
 311                             int prefix, char *seen,
 312                             unsigned flags)
 313{
 314        int i, retval = 0, exclude = flags & DO_MATCH_EXCLUDE;
 315
 316        GUARD_PATHSPEC(ps,
 317                       PATHSPEC_FROMTOP |
 318                       PATHSPEC_MAXDEPTH |
 319                       PATHSPEC_LITERAL |
 320                       PATHSPEC_GLOB |
 321                       PATHSPEC_ICASE |
 322                       PATHSPEC_EXCLUDE);
 323
 324        if (!ps->nr) {
 325                if (!ps->recursive ||
 326                    !(ps->magic & PATHSPEC_MAXDEPTH) ||
 327                    ps->max_depth == -1)
 328                        return MATCHED_RECURSIVELY;
 329
 330                if (within_depth(name, namelen, 0, ps->max_depth))
 331                        return MATCHED_EXACTLY;
 332                else
 333                        return 0;
 334        }
 335
 336        name += prefix;
 337        namelen -= prefix;
 338
 339        for (i = ps->nr - 1; i >= 0; i--) {
 340                int how;
 341
 342                if ((!exclude &&   ps->items[i].magic & PATHSPEC_EXCLUDE) ||
 343                    ( exclude && !(ps->items[i].magic & PATHSPEC_EXCLUDE)))
 344                        continue;
 345
 346                if (seen && seen[i] == MATCHED_EXACTLY)
 347                        continue;
 348                /*
 349                 * Make exclude patterns optional and never report
 350                 * "pathspec ':(exclude)foo' matches no files"
 351                 */
 352                if (seen && ps->items[i].magic & PATHSPEC_EXCLUDE)
 353                        seen[i] = MATCHED_FNMATCH;
 354                how = match_pathspec_item(ps->items+i, prefix, name,
 355                                          namelen, flags);
 356                if (ps->recursive &&
 357                    (ps->magic & PATHSPEC_MAXDEPTH) &&
 358                    ps->max_depth != -1 &&
 359                    how && how != MATCHED_FNMATCH) {
 360                        int len = ps->items[i].len;
 361                        if (name[len] == '/')
 362                                len++;
 363                        if (within_depth(name+len, namelen-len, 0, ps->max_depth))
 364                                how = MATCHED_EXACTLY;
 365                        else
 366                                how = 0;
 367                }
 368                if (how) {
 369                        if (retval < how)
 370                                retval = how;
 371                        if (seen && seen[i] < how)
 372                                seen[i] = how;
 373                }
 374        }
 375        return retval;
 376}
 377
 378int match_pathspec(const struct pathspec *ps,
 379                   const char *name, int namelen,
 380                   int prefix, char *seen, int is_dir)
 381{
 382        int positive, negative;
 383        unsigned flags = is_dir ? DO_MATCH_DIRECTORY : 0;
 384        positive = do_match_pathspec(ps, name, namelen,
 385                                     prefix, seen, flags);
 386        if (!(ps->magic & PATHSPEC_EXCLUDE) || !positive)
 387                return positive;
 388        negative = do_match_pathspec(ps, name, namelen,
 389                                     prefix, seen,
 390                                     flags | DO_MATCH_EXCLUDE);
 391        return negative ? 0 : positive;
 392}
 393
 394/*
 395 * Return the length of the "simple" part of a path match limiter.
 396 */
 397int simple_length(const char *match)
 398{
 399        int len = -1;
 400
 401        for (;;) {
 402                unsigned char c = *match++;
 403                len++;
 404                if (c == '\0' || is_glob_special(c))
 405                        return len;
 406        }
 407}
 408
 409int no_wildcard(const char *string)
 410{
 411        return string[simple_length(string)] == '\0';
 412}
 413
 414void parse_exclude_pattern(const char **pattern,
 415                           int *patternlen,
 416                           int *flags,
 417                           int *nowildcardlen)
 418{
 419        const char *p = *pattern;
 420        size_t i, len;
 421
 422        *flags = 0;
 423        if (*p == '!') {
 424                *flags |= EXC_FLAG_NEGATIVE;
 425                p++;
 426        }
 427        len = strlen(p);
 428        if (len && p[len - 1] == '/') {
 429                len--;
 430                *flags |= EXC_FLAG_MUSTBEDIR;
 431        }
 432        for (i = 0; i < len; i++) {
 433                if (p[i] == '/')
 434                        break;
 435        }
 436        if (i == len)
 437                *flags |= EXC_FLAG_NODIR;
 438        *nowildcardlen = simple_length(p);
 439        /*
 440         * we should have excluded the trailing slash from 'p' too,
 441         * but that's one more allocation. Instead just make sure
 442         * nowildcardlen does not exceed real patternlen
 443         */
 444        if (*nowildcardlen > len)
 445                *nowildcardlen = len;
 446        if (*p == '*' && no_wildcard(p + 1))
 447                *flags |= EXC_FLAG_ENDSWITH;
 448        *pattern = p;
 449        *patternlen = len;
 450}
 451
 452void add_exclude(const char *string, const char *base,
 453                 int baselen, struct exclude_list *el, int srcpos)
 454{
 455        struct exclude *x;
 456        int patternlen;
 457        int flags;
 458        int nowildcardlen;
 459
 460        parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
 461        if (flags & EXC_FLAG_MUSTBEDIR) {
 462                char *s;
 463                x = xmalloc(sizeof(*x) + patternlen + 1);
 464                s = (char *)(x+1);
 465                memcpy(s, string, patternlen);
 466                s[patternlen] = '\0';
 467                x->pattern = s;
 468        } else {
 469                x = xmalloc(sizeof(*x));
 470                x->pattern = string;
 471        }
 472        x->patternlen = patternlen;
 473        x->nowildcardlen = nowildcardlen;
 474        x->base = base;
 475        x->baselen = baselen;
 476        x->flags = flags;
 477        x->srcpos = srcpos;
 478        ALLOC_GROW(el->excludes, el->nr + 1, el->alloc);
 479        el->excludes[el->nr++] = x;
 480        x->el = el;
 481}
 482
 483static void *read_skip_worktree_file_from_index(const char *path, size_t *size,
 484                                                struct sha1_stat *sha1_stat)
 485{
 486        int pos, len;
 487        unsigned long sz;
 488        enum object_type type;
 489        void *data;
 490
 491        len = strlen(path);
 492        pos = cache_name_pos(path, len);
 493        if (pos < 0)
 494                return NULL;
 495        if (!ce_skip_worktree(active_cache[pos]))
 496                return NULL;
 497        data = read_sha1_file(active_cache[pos]->sha1, &type, &sz);
 498        if (!data || type != OBJ_BLOB) {
 499                free(data);
 500                return NULL;
 501        }
 502        *size = xsize_t(sz);
 503        if (sha1_stat) {
 504                memset(&sha1_stat->stat, 0, sizeof(sha1_stat->stat));
 505                hashcpy(sha1_stat->sha1, active_cache[pos]->sha1);
 506        }
 507        return data;
 508}
 509
 510/*
 511 * Frees memory within el which was allocated for exclude patterns and
 512 * the file buffer.  Does not free el itself.
 513 */
 514void clear_exclude_list(struct exclude_list *el)
 515{
 516        int i;
 517
 518        for (i = 0; i < el->nr; i++)
 519                free(el->excludes[i]);
 520        free(el->excludes);
 521        free(el->filebuf);
 522
 523        el->nr = 0;
 524        el->excludes = NULL;
 525        el->filebuf = NULL;
 526}
 527
 528static void trim_trailing_spaces(char *buf)
 529{
 530        char *p, *last_space = NULL;
 531
 532        for (p = buf; *p; p++)
 533                switch (*p) {
 534                case ' ':
 535                        if (!last_space)
 536                                last_space = p;
 537                        break;
 538                case '\\':
 539                        p++;
 540                        if (!*p)
 541                                return;
 542                        /* fallthrough */
 543                default:
 544                        last_space = NULL;
 545                }
 546
 547        if (last_space)
 548                *last_space = '\0';
 549}
 550
 551/*
 552 * Given a subdirectory name and "dir" of the current directory,
 553 * search the subdir in "dir" and return it, or create a new one if it
 554 * does not exist in "dir".
 555 *
 556 * If "name" has the trailing slash, it'll be excluded in the search.
 557 */
 558static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc,
 559                                                    struct untracked_cache_dir *dir,
 560                                                    const char *name, int len)
 561{
 562        int first, last;
 563        struct untracked_cache_dir *d;
 564        if (!dir)
 565                return NULL;
 566        if (len && name[len - 1] == '/')
 567                len--;
 568        first = 0;
 569        last = dir->dirs_nr;
 570        while (last > first) {
 571                int cmp, next = (last + first) >> 1;
 572                d = dir->dirs[next];
 573                cmp = strncmp(name, d->name, len);
 574                if (!cmp && strlen(d->name) > len)
 575                        cmp = -1;
 576                if (!cmp)
 577                        return d;
 578                if (cmp < 0) {
 579                        last = next;
 580                        continue;
 581                }
 582                first = next+1;
 583        }
 584
 585        uc->dir_created++;
 586        d = xmalloc(sizeof(*d) + len + 1);
 587        memset(d, 0, sizeof(*d));
 588        memcpy(d->name, name, len);
 589        d->name[len] = '\0';
 590
 591        ALLOC_GROW(dir->dirs, dir->dirs_nr + 1, dir->dirs_alloc);
 592        memmove(dir->dirs + first + 1, dir->dirs + first,
 593                (dir->dirs_nr - first) * sizeof(*dir->dirs));
 594        dir->dirs_nr++;
 595        dir->dirs[first] = d;
 596        return d;
 597}
 598
 599static void do_invalidate_gitignore(struct untracked_cache_dir *dir)
 600{
 601        int i;
 602        dir->valid = 0;
 603        dir->untracked_nr = 0;
 604        for (i = 0; i < dir->dirs_nr; i++)
 605                do_invalidate_gitignore(dir->dirs[i]);
 606}
 607
 608static void invalidate_gitignore(struct untracked_cache *uc,
 609                                 struct untracked_cache_dir *dir)
 610{
 611        uc->gitignore_invalidated++;
 612        do_invalidate_gitignore(dir);
 613}
 614
 615static void invalidate_directory(struct untracked_cache *uc,
 616                                 struct untracked_cache_dir *dir)
 617{
 618        int i;
 619        uc->dir_invalidated++;
 620        dir->valid = 0;
 621        dir->untracked_nr = 0;
 622        for (i = 0; i < dir->dirs_nr; i++)
 623                dir->dirs[i]->recurse = 0;
 624}
 625
 626/*
 627 * Given a file with name "fname", read it (either from disk, or from
 628 * the index if "check_index" is non-zero), parse it and store the
 629 * exclude rules in "el".
 630 *
 631 * If "ss" is not NULL, compute SHA-1 of the exclude file and fill
 632 * stat data from disk (only valid if add_excludes returns zero). If
 633 * ss_valid is non-zero, "ss" must contain good value as input.
 634 */
 635static int add_excludes(const char *fname, const char *base, int baselen,
 636                        struct exclude_list *el, int check_index,
 637                        struct sha1_stat *sha1_stat)
 638{
 639        struct stat st;
 640        int fd, i, lineno = 1;
 641        size_t size = 0;
 642        char *buf, *entry;
 643
 644        fd = open(fname, O_RDONLY);
 645        if (fd < 0 || fstat(fd, &st) < 0) {
 646                if (errno != ENOENT)
 647                        warn_on_inaccessible(fname);
 648                if (0 <= fd)
 649                        close(fd);
 650                if (!check_index ||
 651                    (buf = read_skip_worktree_file_from_index(fname, &size, sha1_stat)) == NULL)
 652                        return -1;
 653                if (size == 0) {
 654                        free(buf);
 655                        return 0;
 656                }
 657                if (buf[size-1] != '\n') {
 658                        buf = xrealloc(buf, size+1);
 659                        buf[size++] = '\n';
 660                }
 661        } else {
 662                size = xsize_t(st.st_size);
 663                if (size == 0) {
 664                        if (sha1_stat) {
 665                                fill_stat_data(&sha1_stat->stat, &st);
 666                                hashcpy(sha1_stat->sha1, EMPTY_BLOB_SHA1_BIN);
 667                                sha1_stat->valid = 1;
 668                        }
 669                        close(fd);
 670                        return 0;
 671                }
 672                buf = xmalloc(size+1);
 673                if (read_in_full(fd, buf, size) != size) {
 674                        free(buf);
 675                        close(fd);
 676                        return -1;
 677                }
 678                buf[size++] = '\n';
 679                close(fd);
 680                if (sha1_stat) {
 681                        int pos;
 682                        if (sha1_stat->valid &&
 683                            !match_stat_data(&sha1_stat->stat, &st))
 684                                ; /* no content change, ss->sha1 still good */
 685                        else if (check_index &&
 686                                 (pos = cache_name_pos(fname, strlen(fname))) >= 0 &&
 687                                 !ce_stage(active_cache[pos]) &&
 688                                 ce_uptodate(active_cache[pos]) &&
 689                                 !would_convert_to_git(fname))
 690                                hashcpy(sha1_stat->sha1, active_cache[pos]->sha1);
 691                        else
 692                                hash_sha1_file(buf, size, "blob", sha1_stat->sha1);
 693                        fill_stat_data(&sha1_stat->stat, &st);
 694                        sha1_stat->valid = 1;
 695                }
 696        }
 697
 698        el->filebuf = buf;
 699        entry = buf;
 700        for (i = 0; i < size; i++) {
 701                if (buf[i] == '\n') {
 702                        if (entry != buf + i && entry[0] != '#') {
 703                                buf[i - (i && buf[i-1] == '\r')] = 0;
 704                                trim_trailing_spaces(entry);
 705                                add_exclude(entry, base, baselen, el, lineno);
 706                        }
 707                        lineno++;
 708                        entry = buf + i + 1;
 709                }
 710        }
 711        return 0;
 712}
 713
 714int add_excludes_from_file_to_list(const char *fname, const char *base,
 715                                   int baselen, struct exclude_list *el,
 716                                   int check_index)
 717{
 718        return add_excludes(fname, base, baselen, el, check_index, NULL);
 719}
 720
 721struct exclude_list *add_exclude_list(struct dir_struct *dir,
 722                                      int group_type, const char *src)
 723{
 724        struct exclude_list *el;
 725        struct exclude_list_group *group;
 726
 727        group = &dir->exclude_list_group[group_type];
 728        ALLOC_GROW(group->el, group->nr + 1, group->alloc);
 729        el = &group->el[group->nr++];
 730        memset(el, 0, sizeof(*el));
 731        el->src = src;
 732        return el;
 733}
 734
 735/*
 736 * Used to set up core.excludesfile and .git/info/exclude lists.
 737 */
 738static void add_excludes_from_file_1(struct dir_struct *dir, const char *fname,
 739                                     struct sha1_stat *sha1_stat)
 740{
 741        struct exclude_list *el;
 742        /*
 743         * catch setup_standard_excludes() that's called before
 744         * dir->untracked is assigned. That function behaves
 745         * differently when dir->untracked is non-NULL.
 746         */
 747        if (!dir->untracked)
 748                dir->unmanaged_exclude_files++;
 749        el = add_exclude_list(dir, EXC_FILE, fname);
 750        if (add_excludes(fname, "", 0, el, 0, sha1_stat) < 0)
 751                die("cannot use %s as an exclude file", fname);
 752}
 753
 754void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 755{
 756        dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */
 757        add_excludes_from_file_1(dir, fname, NULL);
 758}
 759
 760int match_basename(const char *basename, int basenamelen,
 761                   const char *pattern, int prefix, int patternlen,
 762                   int flags)
 763{
 764        if (prefix == patternlen) {
 765                if (patternlen == basenamelen &&
 766                    !strncmp_icase(pattern, basename, basenamelen))
 767                        return 1;
 768        } else if (flags & EXC_FLAG_ENDSWITH) {
 769                /* "*literal" matching against "fooliteral" */
 770                if (patternlen - 1 <= basenamelen &&
 771                    !strncmp_icase(pattern + 1,
 772                                   basename + basenamelen - (patternlen - 1),
 773                                   patternlen - 1))
 774                        return 1;
 775        } else {
 776                if (fnmatch_icase_mem(pattern, patternlen,
 777                                      basename, basenamelen,
 778                                      0) == 0)
 779                        return 1;
 780        }
 781        return 0;
 782}
 783
 784int match_pathname(const char *pathname, int pathlen,
 785                   const char *base, int baselen,
 786                   const char *pattern, int prefix, int patternlen,
 787                   int flags)
 788{
 789        const char *name;
 790        int namelen;
 791
 792        /*
 793         * match with FNM_PATHNAME; the pattern has base implicitly
 794         * in front of it.
 795         */
 796        if (*pattern == '/') {
 797                pattern++;
 798                patternlen--;
 799                prefix--;
 800        }
 801
 802        /*
 803         * baselen does not count the trailing slash. base[] may or
 804         * may not end with a trailing slash though.
 805         */
 806        if (pathlen < baselen + 1 ||
 807            (baselen && pathname[baselen] != '/') ||
 808            strncmp_icase(pathname, base, baselen))
 809                return 0;
 810
 811        namelen = baselen ? pathlen - baselen - 1 : pathlen;
 812        name = pathname + pathlen - namelen;
 813
 814        if (prefix) {
 815                /*
 816                 * if the non-wildcard part is longer than the
 817                 * remaining pathname, surely it cannot match.
 818                 */
 819                if (prefix > namelen)
 820                        return 0;
 821
 822                if (strncmp_icase(pattern, name, prefix))
 823                        return 0;
 824                pattern += prefix;
 825                patternlen -= prefix;
 826                name    += prefix;
 827                namelen -= prefix;
 828
 829                /*
 830                 * If the whole pattern did not have a wildcard,
 831                 * then our prefix match is all we need; we
 832                 * do not need to call fnmatch at all.
 833                 */
 834                if (!patternlen && !namelen)
 835                        return 1;
 836        }
 837
 838        return fnmatch_icase_mem(pattern, patternlen,
 839                                 name, namelen,
 840                                 WM_PATHNAME) == 0;
 841}
 842
 843/*
 844 * Scan the given exclude list in reverse to see whether pathname
 845 * should be ignored.  The first match (i.e. the last on the list), if
 846 * any, determines the fate.  Returns the exclude_list element which
 847 * matched, or NULL for undecided.
 848 */
 849static struct exclude *last_exclude_matching_from_list(const char *pathname,
 850                                                       int pathlen,
 851                                                       const char *basename,
 852                                                       int *dtype,
 853                                                       struct exclude_list *el)
 854{
 855        int i;
 856
 857        if (!el->nr)
 858                return NULL;    /* undefined */
 859
 860        for (i = el->nr - 1; 0 <= i; i--) {
 861                struct exclude *x = el->excludes[i];
 862                const char *exclude = x->pattern;
 863                int prefix = x->nowildcardlen;
 864
 865                if (x->flags & EXC_FLAG_MUSTBEDIR) {
 866                        if (*dtype == DT_UNKNOWN)
 867                                *dtype = get_dtype(NULL, pathname, pathlen);
 868                        if (*dtype != DT_DIR)
 869                                continue;
 870                }
 871
 872                if (x->flags & EXC_FLAG_NODIR) {
 873                        if (match_basename(basename,
 874                                           pathlen - (basename - pathname),
 875                                           exclude, prefix, x->patternlen,
 876                                           x->flags))
 877                                return x;
 878                        continue;
 879                }
 880
 881                assert(x->baselen == 0 || x->base[x->baselen - 1] == '/');
 882                if (match_pathname(pathname, pathlen,
 883                                   x->base, x->baselen ? x->baselen - 1 : 0,
 884                                   exclude, prefix, x->patternlen, x->flags))
 885                        return x;
 886        }
 887        return NULL; /* undecided */
 888}
 889
 890/*
 891 * Scan the list and let the last match determine the fate.
 892 * Return 1 for exclude, 0 for include and -1 for undecided.
 893 */
 894int is_excluded_from_list(const char *pathname,
 895                          int pathlen, const char *basename, int *dtype,
 896                          struct exclude_list *el)
 897{
 898        struct exclude *exclude;
 899        exclude = last_exclude_matching_from_list(pathname, pathlen, basename, dtype, el);
 900        if (exclude)
 901                return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
 902        return -1; /* undecided */
 903}
 904
 905static struct exclude *last_exclude_matching_from_lists(struct dir_struct *dir,
 906                const char *pathname, int pathlen, const char *basename,
 907                int *dtype_p)
 908{
 909        int i, j;
 910        struct exclude_list_group *group;
 911        struct exclude *exclude;
 912        for (i = EXC_CMDL; i <= EXC_FILE; i++) {
 913                group = &dir->exclude_list_group[i];
 914                for (j = group->nr - 1; j >= 0; j--) {
 915                        exclude = last_exclude_matching_from_list(
 916                                pathname, pathlen, basename, dtype_p,
 917                                &group->el[j]);
 918                        if (exclude)
 919                                return exclude;
 920                }
 921        }
 922        return NULL;
 923}
 924
 925/*
 926 * Loads the per-directory exclude list for the substring of base
 927 * which has a char length of baselen.
 928 */
 929static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
 930{
 931        struct exclude_list_group *group;
 932        struct exclude_list *el;
 933        struct exclude_stack *stk = NULL;
 934        struct untracked_cache_dir *untracked;
 935        int current;
 936
 937        group = &dir->exclude_list_group[EXC_DIRS];
 938
 939        /*
 940         * Pop the exclude lists from the EXCL_DIRS exclude_list_group
 941         * which originate from directories not in the prefix of the
 942         * path being checked.
 943         */
 944        while ((stk = dir->exclude_stack) != NULL) {
 945                if (stk->baselen <= baselen &&
 946                    !strncmp(dir->basebuf.buf, base, stk->baselen))
 947                        break;
 948                el = &group->el[dir->exclude_stack->exclude_ix];
 949                dir->exclude_stack = stk->prev;
 950                dir->exclude = NULL;
 951                free((char *)el->src); /* see strbuf_detach() below */
 952                clear_exclude_list(el);
 953                free(stk);
 954                group->nr--;
 955        }
 956
 957        /* Skip traversing into sub directories if the parent is excluded */
 958        if (dir->exclude)
 959                return;
 960
 961        /*
 962         * Lazy initialization. All call sites currently just
 963         * memset(dir, 0, sizeof(*dir)) before use. Changing all of
 964         * them seems lots of work for little benefit.
 965         */
 966        if (!dir->basebuf.buf)
 967                strbuf_init(&dir->basebuf, PATH_MAX);
 968
 969        /* Read from the parent directories and push them down. */
 970        current = stk ? stk->baselen : -1;
 971        strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current);
 972        if (dir->untracked)
 973                untracked = stk ? stk->ucd : dir->untracked->root;
 974        else
 975                untracked = NULL;
 976
 977        while (current < baselen) {
 978                const char *cp;
 979                struct sha1_stat sha1_stat;
 980
 981                stk = xcalloc(1, sizeof(*stk));
 982                if (current < 0) {
 983                        cp = base;
 984                        current = 0;
 985                } else {
 986                        cp = strchr(base + current + 1, '/');
 987                        if (!cp)
 988                                die("oops in prep_exclude");
 989                        cp++;
 990                        untracked =
 991                                lookup_untracked(dir->untracked, untracked,
 992                                                 base + current,
 993                                                 cp - base - current);
 994                }
 995                stk->prev = dir->exclude_stack;
 996                stk->baselen = cp - base;
 997                stk->exclude_ix = group->nr;
 998                stk->ucd = untracked;
 999                el = add_exclude_list(dir, EXC_DIRS, NULL);
1000                strbuf_add(&dir->basebuf, base + current, stk->baselen - current);
1001                assert(stk->baselen == dir->basebuf.len);
1002
1003                /* Abort if the directory is excluded */
1004                if (stk->baselen) {
1005                        int dt = DT_DIR;
1006                        dir->basebuf.buf[stk->baselen - 1] = 0;
1007                        dir->exclude = last_exclude_matching_from_lists(dir,
1008                                dir->basebuf.buf, stk->baselen - 1,
1009                                dir->basebuf.buf + current, &dt);
1010                        dir->basebuf.buf[stk->baselen - 1] = '/';
1011                        if (dir->exclude &&
1012                            dir->exclude->flags & EXC_FLAG_NEGATIVE)
1013                                dir->exclude = NULL;
1014                        if (dir->exclude) {
1015                                dir->exclude_stack = stk;
1016                                return;
1017                        }
1018                }
1019
1020                /* Try to read per-directory file */
1021                hashclr(sha1_stat.sha1);
1022                sha1_stat.valid = 0;
1023                if (dir->exclude_per_dir &&
1024                    /*
1025                     * If we know that no files have been added in
1026                     * this directory (i.e. valid_cached_dir() has
1027                     * been executed and set untracked->valid) ..
1028                     */
1029                    (!untracked || !untracked->valid ||
1030                     /*
1031                      * .. and .gitignore does not exist before
1032                      * (i.e. null exclude_sha1 and skip_worktree is
1033                      * not set). Then we can skip loading .gitignore,
1034                      * which would result in ENOENT anyway.
1035                      * skip_worktree is taken care in read_directory()
1036                      */
1037                     !is_null_sha1(untracked->exclude_sha1))) {
1038                        /*
1039                         * dir->basebuf gets reused by the traversal, but we
1040                         * need fname to remain unchanged to ensure the src
1041                         * member of each struct exclude correctly
1042                         * back-references its source file.  Other invocations
1043                         * of add_exclude_list provide stable strings, so we
1044                         * strbuf_detach() and free() here in the caller.
1045                         */
1046                        struct strbuf sb = STRBUF_INIT;
1047                        strbuf_addbuf(&sb, &dir->basebuf);
1048                        strbuf_addstr(&sb, dir->exclude_per_dir);
1049                        el->src = strbuf_detach(&sb, NULL);
1050                        add_excludes(el->src, el->src, stk->baselen, el, 1,
1051                                     untracked ? &sha1_stat : NULL);
1052                }
1053                /*
1054                 * NEEDSWORK: when untracked cache is enabled, prep_exclude()
1055                 * will first be called in valid_cached_dir() then maybe many
1056                 * times more in last_exclude_matching(). When the cache is
1057                 * used, last_exclude_matching() will not be called and
1058                 * reading .gitignore content will be a waste.
1059                 *
1060                 * So when it's called by valid_cached_dir() and we can get
1061                 * .gitignore SHA-1 from the index (i.e. .gitignore is not
1062                 * modified on work tree), we could delay reading the
1063                 * .gitignore content until we absolutely need it in
1064                 * last_exclude_matching(). Be careful about ignore rule
1065                 * order, though, if you do that.
1066                 */
1067                if (untracked &&
1068                    hashcmp(sha1_stat.sha1, untracked->exclude_sha1)) {
1069                        invalidate_gitignore(dir->untracked, untracked);
1070                        hashcpy(untracked->exclude_sha1, sha1_stat.sha1);
1071                }
1072                dir->exclude_stack = stk;
1073                current = stk->baselen;
1074        }
1075        strbuf_setlen(&dir->basebuf, baselen);
1076}
1077
1078/*
1079 * Loads the exclude lists for the directory containing pathname, then
1080 * scans all exclude lists to determine whether pathname is excluded.
1081 * Returns the exclude_list element which matched, or NULL for
1082 * undecided.
1083 */
1084struct exclude *last_exclude_matching(struct dir_struct *dir,
1085                                             const char *pathname,
1086                                             int *dtype_p)
1087{
1088        int pathlen = strlen(pathname);
1089        const char *basename = strrchr(pathname, '/');
1090        basename = (basename) ? basename+1 : pathname;
1091
1092        prep_exclude(dir, pathname, basename-pathname);
1093
1094        if (dir->exclude)
1095                return dir->exclude;
1096
1097        return last_exclude_matching_from_lists(dir, pathname, pathlen,
1098                        basename, dtype_p);
1099}
1100
1101/*
1102 * Loads the exclude lists for the directory containing pathname, then
1103 * scans all exclude lists to determine whether pathname is excluded.
1104 * Returns 1 if true, otherwise 0.
1105 */
1106int is_excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
1107{
1108        struct exclude *exclude =
1109                last_exclude_matching(dir, pathname, dtype_p);
1110        if (exclude)
1111                return exclude->flags & EXC_FLAG_NEGATIVE ? 0 : 1;
1112        return 0;
1113}
1114
1115static struct dir_entry *dir_entry_new(const char *pathname, int len)
1116{
1117        struct dir_entry *ent;
1118
1119        ent = xmalloc(sizeof(*ent) + len + 1);
1120        ent->len = len;
1121        memcpy(ent->name, pathname, len);
1122        ent->name[len] = 0;
1123        return ent;
1124}
1125
1126static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
1127{
1128        if (cache_file_exists(pathname, len, ignore_case))
1129                return NULL;
1130
1131        ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
1132        return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
1133}
1134
1135struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
1136{
1137        if (!cache_name_is_other(pathname, len))
1138                return NULL;
1139
1140        ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
1141        return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
1142}
1143
1144enum exist_status {
1145        index_nonexistent = 0,
1146        index_directory,
1147        index_gitdir
1148};
1149
1150/*
1151 * Do not use the alphabetically sorted index to look up
1152 * the directory name; instead, use the case insensitive
1153 * directory hash.
1154 */
1155static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
1156{
1157        const struct cache_entry *ce = cache_dir_exists(dirname, len);
1158        unsigned char endchar;
1159
1160        if (!ce)
1161                return index_nonexistent;
1162        endchar = ce->name[len];
1163
1164        /*
1165         * The cache_entry structure returned will contain this dirname
1166         * and possibly additional path components.
1167         */
1168        if (endchar == '/')
1169                return index_directory;
1170
1171        /*
1172         * If there are no additional path components, then this cache_entry
1173         * represents a submodule.  Submodules, despite being directories,
1174         * are stored in the cache without a closing slash.
1175         */
1176        if (!endchar && S_ISGITLINK(ce->ce_mode))
1177                return index_gitdir;
1178
1179        /* This should never be hit, but it exists just in case. */
1180        return index_nonexistent;
1181}
1182
1183/*
1184 * The index sorts alphabetically by entry name, which
1185 * means that a gitlink sorts as '\0' at the end, while
1186 * a directory (which is defined not as an entry, but as
1187 * the files it contains) will sort with the '/' at the
1188 * end.
1189 */
1190static enum exist_status directory_exists_in_index(const char *dirname, int len)
1191{
1192        int pos;
1193
1194        if (ignore_case)
1195                return directory_exists_in_index_icase(dirname, len);
1196
1197        pos = cache_name_pos(dirname, len);
1198        if (pos < 0)
1199                pos = -pos-1;
1200        while (pos < active_nr) {
1201                const struct cache_entry *ce = active_cache[pos++];
1202                unsigned char endchar;
1203
1204                if (strncmp(ce->name, dirname, len))
1205                        break;
1206                endchar = ce->name[len];
1207                if (endchar > '/')
1208                        break;
1209                if (endchar == '/')
1210                        return index_directory;
1211                if (!endchar && S_ISGITLINK(ce->ce_mode))
1212                        return index_gitdir;
1213        }
1214        return index_nonexistent;
1215}
1216
1217/*
1218 * When we find a directory when traversing the filesystem, we
1219 * have three distinct cases:
1220 *
1221 *  - ignore it
1222 *  - see it as a directory
1223 *  - recurse into it
1224 *
1225 * and which one we choose depends on a combination of existing
1226 * git index contents and the flags passed into the directory
1227 * traversal routine.
1228 *
1229 * Case 1: If we *already* have entries in the index under that
1230 * directory name, we always recurse into the directory to see
1231 * all the files.
1232 *
1233 * Case 2: If we *already* have that directory name as a gitlink,
1234 * we always continue to see it as a gitlink, regardless of whether
1235 * there is an actual git directory there or not (it might not
1236 * be checked out as a subproject!)
1237 *
1238 * Case 3: if we didn't have it in the index previously, we
1239 * have a few sub-cases:
1240 *
1241 *  (a) if "show_other_directories" is true, we show it as
1242 *      just a directory, unless "hide_empty_directories" is
1243 *      also true, in which case we need to check if it contains any
1244 *      untracked and / or ignored files.
1245 *  (b) if it looks like a git directory, and we don't have
1246 *      'no_gitlinks' set we treat it as a gitlink, and show it
1247 *      as a directory.
1248 *  (c) otherwise, we recurse into it.
1249 */
1250static enum path_treatment treat_directory(struct dir_struct *dir,
1251        struct untracked_cache_dir *untracked,
1252        const char *dirname, int len, int exclude,
1253        const struct path_simplify *simplify)
1254{
1255        /* The "len-1" is to strip the final '/' */
1256        switch (directory_exists_in_index(dirname, len-1)) {
1257        case index_directory:
1258                return path_recurse;
1259
1260        case index_gitdir:
1261                return path_none;
1262
1263        case index_nonexistent:
1264                if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
1265                        break;
1266                if (!(dir->flags & DIR_NO_GITLINKS)) {
1267                        unsigned char sha1[20];
1268                        if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
1269                                return path_untracked;
1270                }
1271                return path_recurse;
1272        }
1273
1274        /* This is the "show_other_directories" case */
1275
1276        if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
1277                return exclude ? path_excluded : path_untracked;
1278
1279        untracked = lookup_untracked(dir->untracked, untracked, dirname, len);
1280        return read_directory_recursive(dir, dirname, len,
1281                                        untracked, 1, simplify);
1282}
1283
1284/*
1285 * This is an inexact early pruning of any recursive directory
1286 * reading - if the path cannot possibly be in the pathspec,
1287 * return true, and we'll skip it early.
1288 */
1289static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
1290{
1291        if (simplify) {
1292                for (;;) {
1293                        const char *match = simplify->path;
1294                        int len = simplify->len;
1295
1296                        if (!match)
1297                                break;
1298                        if (len > pathlen)
1299                                len = pathlen;
1300                        if (!memcmp(path, match, len))
1301                                return 0;
1302                        simplify++;
1303                }
1304                return 1;
1305        }
1306        return 0;
1307}
1308
1309/*
1310 * This function tells us whether an excluded path matches a
1311 * list of "interesting" pathspecs. That is, whether a path matched
1312 * by any of the pathspecs could possibly be ignored by excluding
1313 * the specified path. This can happen if:
1314 *
1315 *   1. the path is mentioned explicitly in the pathspec
1316 *
1317 *   2. the path is a directory prefix of some element in the
1318 *      pathspec
1319 */
1320static int exclude_matches_pathspec(const char *path, int len,
1321                const struct path_simplify *simplify)
1322{
1323        if (simplify) {
1324                for (; simplify->path; simplify++) {
1325                        if (len == simplify->len
1326                            && !memcmp(path, simplify->path, len))
1327                                return 1;
1328                        if (len < simplify->len
1329                            && simplify->path[len] == '/'
1330                            && !memcmp(path, simplify->path, len))
1331                                return 1;
1332                }
1333        }
1334        return 0;
1335}
1336
1337static int get_index_dtype(const char *path, int len)
1338{
1339        int pos;
1340        const struct cache_entry *ce;
1341
1342        ce = cache_file_exists(path, len, 0);
1343        if (ce) {
1344                if (!ce_uptodate(ce))
1345                        return DT_UNKNOWN;
1346                if (S_ISGITLINK(ce->ce_mode))
1347                        return DT_DIR;
1348                /*
1349                 * Nobody actually cares about the
1350                 * difference between DT_LNK and DT_REG
1351                 */
1352                return DT_REG;
1353        }
1354
1355        /* Try to look it up as a directory */
1356        pos = cache_name_pos(path, len);
1357        if (pos >= 0)
1358                return DT_UNKNOWN;
1359        pos = -pos-1;
1360        while (pos < active_nr) {
1361                ce = active_cache[pos++];
1362                if (strncmp(ce->name, path, len))
1363                        break;
1364                if (ce->name[len] > '/')
1365                        break;
1366                if (ce->name[len] < '/')
1367                        continue;
1368                if (!ce_uptodate(ce))
1369                        break;  /* continue? */
1370                return DT_DIR;
1371        }
1372        return DT_UNKNOWN;
1373}
1374
1375static int get_dtype(struct dirent *de, const char *path, int len)
1376{
1377        int dtype = de ? DTYPE(de) : DT_UNKNOWN;
1378        struct stat st;
1379
1380        if (dtype != DT_UNKNOWN)
1381                return dtype;
1382        dtype = get_index_dtype(path, len);
1383        if (dtype != DT_UNKNOWN)
1384                return dtype;
1385        if (lstat(path, &st))
1386                return dtype;
1387        if (S_ISREG(st.st_mode))
1388                return DT_REG;
1389        if (S_ISDIR(st.st_mode))
1390                return DT_DIR;
1391        if (S_ISLNK(st.st_mode))
1392                return DT_LNK;
1393        return dtype;
1394}
1395
1396static enum path_treatment treat_one_path(struct dir_struct *dir,
1397                                          struct untracked_cache_dir *untracked,
1398                                          struct strbuf *path,
1399                                          const struct path_simplify *simplify,
1400                                          int dtype, struct dirent *de)
1401{
1402        int exclude;
1403        int has_path_in_index = !!cache_file_exists(path->buf, path->len, ignore_case);
1404
1405        if (dtype == DT_UNKNOWN)
1406                dtype = get_dtype(de, path->buf, path->len);
1407
1408        /* Always exclude indexed files */
1409        if (dtype != DT_DIR && has_path_in_index)
1410                return path_none;
1411
1412        /*
1413         * When we are looking at a directory P in the working tree,
1414         * there are three cases:
1415         *
1416         * (1) P exists in the index.  Everything inside the directory P in
1417         * the working tree needs to go when P is checked out from the
1418         * index.
1419         *
1420         * (2) P does not exist in the index, but there is P/Q in the index.
1421         * We know P will stay a directory when we check out the contents
1422         * of the index, but we do not know yet if there is a directory
1423         * P/Q in the working tree to be killed, so we need to recurse.
1424         *
1425         * (3) P does not exist in the index, and there is no P/Q in the index
1426         * to require P to be a directory, either.  Only in this case, we
1427         * know that everything inside P will not be killed without
1428         * recursing.
1429         */
1430        if ((dir->flags & DIR_COLLECT_KILLED_ONLY) &&
1431            (dtype == DT_DIR) &&
1432            !has_path_in_index &&
1433            (directory_exists_in_index(path->buf, path->len) == index_nonexistent))
1434                return path_none;
1435
1436        exclude = is_excluded(dir, path->buf, &dtype);
1437
1438        /*
1439         * Excluded? If we don't explicitly want to show
1440         * ignored files, ignore it
1441         */
1442        if (exclude && !(dir->flags & (DIR_SHOW_IGNORED|DIR_SHOW_IGNORED_TOO)))
1443                return path_excluded;
1444
1445        switch (dtype) {
1446        default:
1447                return path_none;
1448        case DT_DIR:
1449                strbuf_addch(path, '/');
1450                return treat_directory(dir, untracked, path->buf, path->len, exclude,
1451                        simplify);
1452        case DT_REG:
1453        case DT_LNK:
1454                return exclude ? path_excluded : path_untracked;
1455        }
1456}
1457
1458static enum path_treatment treat_path_fast(struct dir_struct *dir,
1459                                           struct untracked_cache_dir *untracked,
1460                                           struct cached_dir *cdir,
1461                                           struct strbuf *path,
1462                                           int baselen,
1463                                           const struct path_simplify *simplify)
1464{
1465        strbuf_setlen(path, baselen);
1466        if (!cdir->ucd) {
1467                strbuf_addstr(path, cdir->file);
1468                return path_untracked;
1469        }
1470        strbuf_addstr(path, cdir->ucd->name);
1471        /* treat_one_path() does this before it calls treat_directory() */
1472        if (path->buf[path->len - 1] != '/')
1473                strbuf_addch(path, '/');
1474        if (cdir->ucd->check_only)
1475                /*
1476                 * check_only is set as a result of treat_directory() getting
1477                 * to its bottom. Verify again the same set of directories
1478                 * with check_only set.
1479                 */
1480                return read_directory_recursive(dir, path->buf, path->len,
1481                                                cdir->ucd, 1, simplify);
1482        /*
1483         * We get path_recurse in the first run when
1484         * directory_exists_in_index() returns index_nonexistent. We
1485         * are sure that new changes in the index does not impact the
1486         * outcome. Return now.
1487         */
1488        return path_recurse;
1489}
1490
1491static enum path_treatment treat_path(struct dir_struct *dir,
1492                                      struct untracked_cache_dir *untracked,
1493                                      struct cached_dir *cdir,
1494                                      struct strbuf *path,
1495                                      int baselen,
1496                                      const struct path_simplify *simplify)
1497{
1498        int dtype;
1499        struct dirent *de = cdir->de;
1500
1501        if (!de)
1502                return treat_path_fast(dir, untracked, cdir, path,
1503                                       baselen, simplify);
1504        if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
1505                return path_none;
1506        strbuf_setlen(path, baselen);
1507        strbuf_addstr(path, de->d_name);
1508        if (simplify_away(path->buf, path->len, simplify))
1509                return path_none;
1510
1511        dtype = DTYPE(de);
1512        return treat_one_path(dir, untracked, path, simplify, dtype, de);
1513}
1514
1515static void add_untracked(struct untracked_cache_dir *dir, const char *name)
1516{
1517        if (!dir)
1518                return;
1519        ALLOC_GROW(dir->untracked, dir->untracked_nr + 1,
1520                   dir->untracked_alloc);
1521        dir->untracked[dir->untracked_nr++] = xstrdup(name);
1522}
1523
1524static int valid_cached_dir(struct dir_struct *dir,
1525                            struct untracked_cache_dir *untracked,
1526                            struct strbuf *path,
1527                            int check_only)
1528{
1529        struct stat st;
1530
1531        if (!untracked)
1532                return 0;
1533
1534        if (stat(path->len ? path->buf : ".", &st)) {
1535                invalidate_directory(dir->untracked, untracked);
1536                memset(&untracked->stat_data, 0, sizeof(untracked->stat_data));
1537                return 0;
1538        }
1539        if (!untracked->valid ||
1540            match_stat_data(&untracked->stat_data, &st)) {
1541                if (untracked->valid)
1542                        invalidate_directory(dir->untracked, untracked);
1543                fill_stat_data(&untracked->stat_data, &st);
1544                return 0;
1545        }
1546
1547        if (untracked->check_only != !!check_only) {
1548                invalidate_directory(dir->untracked, untracked);
1549                return 0;
1550        }
1551
1552        /*
1553         * prep_exclude will be called eventually on this directory,
1554         * but it's called much later in last_exclude_matching(). We
1555         * need it now to determine the validity of the cache for this
1556         * path. The next calls will be nearly no-op, the way
1557         * prep_exclude() is designed.
1558         */
1559        if (path->len && path->buf[path->len - 1] != '/') {
1560                strbuf_addch(path, '/');
1561                prep_exclude(dir, path->buf, path->len);
1562                strbuf_setlen(path, path->len - 1);
1563        } else
1564                prep_exclude(dir, path->buf, path->len);
1565
1566        /* hopefully prep_exclude() haven't invalidated this entry... */
1567        return untracked->valid;
1568}
1569
1570static int open_cached_dir(struct cached_dir *cdir,
1571                           struct dir_struct *dir,
1572                           struct untracked_cache_dir *untracked,
1573                           struct strbuf *path,
1574                           int check_only)
1575{
1576        memset(cdir, 0, sizeof(*cdir));
1577        cdir->untracked = untracked;
1578        if (valid_cached_dir(dir, untracked, path, check_only))
1579                return 0;
1580        cdir->fdir = opendir(path->len ? path->buf : ".");
1581        if (dir->untracked)
1582                dir->untracked->dir_opened++;
1583        if (!cdir->fdir)
1584                return -1;
1585        return 0;
1586}
1587
1588static int read_cached_dir(struct cached_dir *cdir)
1589{
1590        if (cdir->fdir) {
1591                cdir->de = readdir(cdir->fdir);
1592                if (!cdir->de)
1593                        return -1;
1594                return 0;
1595        }
1596        while (cdir->nr_dirs < cdir->untracked->dirs_nr) {
1597                struct untracked_cache_dir *d = cdir->untracked->dirs[cdir->nr_dirs];
1598                if (!d->recurse) {
1599                        cdir->nr_dirs++;
1600                        continue;
1601                }
1602                cdir->ucd = d;
1603                cdir->nr_dirs++;
1604                return 0;
1605        }
1606        cdir->ucd = NULL;
1607        if (cdir->nr_files < cdir->untracked->untracked_nr) {
1608                struct untracked_cache_dir *d = cdir->untracked;
1609                cdir->file = d->untracked[cdir->nr_files++];
1610                return 0;
1611        }
1612        return -1;
1613}
1614
1615static void close_cached_dir(struct cached_dir *cdir)
1616{
1617        if (cdir->fdir)
1618                closedir(cdir->fdir);
1619        /*
1620         * We have gone through this directory and found no untracked
1621         * entries. Mark it valid.
1622         */
1623        if (cdir->untracked) {
1624                cdir->untracked->valid = 1;
1625                cdir->untracked->recurse = 1;
1626        }
1627}
1628
1629/*
1630 * Read a directory tree. We currently ignore anything but
1631 * directories, regular files and symlinks. That's because git
1632 * doesn't handle them at all yet. Maybe that will change some
1633 * day.
1634 *
1635 * Also, we ignore the name ".git" (even if it is not a directory).
1636 * That likely will not change.
1637 *
1638 * Returns the most significant path_treatment value encountered in the scan.
1639 */
1640static enum path_treatment read_directory_recursive(struct dir_struct *dir,
1641                                    const char *base, int baselen,
1642                                    struct untracked_cache_dir *untracked, int check_only,
1643                                    const struct path_simplify *simplify)
1644{
1645        struct cached_dir cdir;
1646        enum path_treatment state, subdir_state, dir_state = path_none;
1647        struct strbuf path = STRBUF_INIT;
1648
1649        strbuf_add(&path, base, baselen);
1650
1651        if (open_cached_dir(&cdir, dir, untracked, &path, check_only))
1652                goto out;
1653
1654        if (untracked)
1655                untracked->check_only = !!check_only;
1656
1657        while (!read_cached_dir(&cdir)) {
1658                /* check how the file or directory should be treated */
1659                state = treat_path(dir, untracked, &cdir, &path, baselen, simplify);
1660
1661                if (state > dir_state)
1662                        dir_state = state;
1663
1664                /* recurse into subdir if instructed by treat_path */
1665                if (state == path_recurse) {
1666                        struct untracked_cache_dir *ud;
1667                        ud = lookup_untracked(dir->untracked, untracked,
1668                                              path.buf + baselen,
1669                                              path.len - baselen);
1670                        subdir_state =
1671                                read_directory_recursive(dir, path.buf, path.len,
1672                                                         ud, check_only, simplify);
1673                        if (subdir_state > dir_state)
1674                                dir_state = subdir_state;
1675                }
1676
1677                if (check_only) {
1678                        /* abort early if maximum state has been reached */
1679                        if (dir_state == path_untracked) {
1680                                if (cdir.fdir)
1681                                        add_untracked(untracked, path.buf + baselen);
1682                                break;
1683                        }
1684                        /* skip the dir_add_* part */
1685                        continue;
1686                }
1687
1688                /* add the path to the appropriate result list */
1689                switch (state) {
1690                case path_excluded:
1691                        if (dir->flags & DIR_SHOW_IGNORED)
1692                                dir_add_name(dir, path.buf, path.len);
1693                        else if ((dir->flags & DIR_SHOW_IGNORED_TOO) ||
1694                                ((dir->flags & DIR_COLLECT_IGNORED) &&
1695                                exclude_matches_pathspec(path.buf, path.len,
1696                                        simplify)))
1697                                dir_add_ignored(dir, path.buf, path.len);
1698                        break;
1699
1700                case path_untracked:
1701                        if (dir->flags & DIR_SHOW_IGNORED)
1702                                break;
1703                        dir_add_name(dir, path.buf, path.len);
1704                        if (cdir.fdir)
1705                                add_untracked(untracked, path.buf + baselen);
1706                        break;
1707
1708                default:
1709                        break;
1710                }
1711        }
1712        close_cached_dir(&cdir);
1713 out:
1714        strbuf_release(&path);
1715
1716        return dir_state;
1717}
1718
1719static int cmp_name(const void *p1, const void *p2)
1720{
1721        const struct dir_entry *e1 = *(const struct dir_entry **)p1;
1722        const struct dir_entry *e2 = *(const struct dir_entry **)p2;
1723
1724        return name_compare(e1->name, e1->len, e2->name, e2->len);
1725}
1726
1727static struct path_simplify *create_simplify(const char **pathspec)
1728{
1729        int nr, alloc = 0;
1730        struct path_simplify *simplify = NULL;
1731
1732        if (!pathspec)
1733                return NULL;
1734
1735        for (nr = 0 ; ; nr++) {
1736                const char *match;
1737                ALLOC_GROW(simplify, nr + 1, alloc);
1738                match = *pathspec++;
1739                if (!match)
1740                        break;
1741                simplify[nr].path = match;
1742                simplify[nr].len = simple_length(match);
1743        }
1744        simplify[nr].path = NULL;
1745        simplify[nr].len = 0;
1746        return simplify;
1747}
1748
1749static void free_simplify(struct path_simplify *simplify)
1750{
1751        free(simplify);
1752}
1753
1754static int treat_leading_path(struct dir_struct *dir,
1755                              const char *path, int len,
1756                              const struct path_simplify *simplify)
1757{
1758        struct strbuf sb = STRBUF_INIT;
1759        int baselen, rc = 0;
1760        const char *cp;
1761        int old_flags = dir->flags;
1762
1763        while (len && path[len - 1] == '/')
1764                len--;
1765        if (!len)
1766                return 1;
1767        baselen = 0;
1768        dir->flags &= ~DIR_SHOW_OTHER_DIRECTORIES;
1769        while (1) {
1770                cp = path + baselen + !!baselen;
1771                cp = memchr(cp, '/', path + len - cp);
1772                if (!cp)
1773                        baselen = len;
1774                else
1775                        baselen = cp - path;
1776                strbuf_setlen(&sb, 0);
1777                strbuf_add(&sb, path, baselen);
1778                if (!is_directory(sb.buf))
1779                        break;
1780                if (simplify_away(sb.buf, sb.len, simplify))
1781                        break;
1782                if (treat_one_path(dir, NULL, &sb, simplify,
1783                                   DT_DIR, NULL) == path_none)
1784                        break; /* do not recurse into it */
1785                if (len <= baselen) {
1786                        rc = 1;
1787                        break; /* finished checking */
1788                }
1789        }
1790        strbuf_release(&sb);
1791        dir->flags = old_flags;
1792        return rc;
1793}
1794
1795static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *dir,
1796                                                      int base_len,
1797                                                      const struct pathspec *pathspec)
1798{
1799        struct untracked_cache_dir *root;
1800        int i;
1801
1802        if (!dir->untracked)
1803                return NULL;
1804
1805        /*
1806         * We only support $GIT_DIR/info/exclude and core.excludesfile
1807         * as the global ignore rule files. Any other additions
1808         * (e.g. from command line) invalidate the cache. This
1809         * condition also catches running setup_standard_excludes()
1810         * before setting dir->untracked!
1811         */
1812        if (dir->unmanaged_exclude_files)
1813                return NULL;
1814
1815        /*
1816         * Optimize for the main use case only: whole-tree git
1817         * status. More work involved in treat_leading_path() if we
1818         * use cache on just a subset of the worktree. pathspec
1819         * support could make the matter even worse.
1820         */
1821        if (base_len || (pathspec && pathspec->nr))
1822                return NULL;
1823
1824        /* Different set of flags may produce different results */
1825        if (dir->flags != dir->untracked->dir_flags ||
1826            /*
1827             * See treat_directory(), case index_nonexistent. Without
1828             * this flag, we may need to also cache .git file content
1829             * for the resolve_gitlink_ref() call, which we don't.
1830             */
1831            !(dir->flags & DIR_SHOW_OTHER_DIRECTORIES) ||
1832            /* We don't support collecting ignore files */
1833            (dir->flags & (DIR_SHOW_IGNORED | DIR_SHOW_IGNORED_TOO |
1834                           DIR_COLLECT_IGNORED)))
1835                return NULL;
1836
1837        /*
1838         * If we use .gitignore in the cache and now you change it to
1839         * .gitexclude, everything will go wrong.
1840         */
1841        if (dir->exclude_per_dir != dir->untracked->exclude_per_dir &&
1842            strcmp(dir->exclude_per_dir, dir->untracked->exclude_per_dir))
1843                return NULL;
1844
1845        /*
1846         * EXC_CMDL is not considered in the cache. If people set it,
1847         * skip the cache.
1848         */
1849        if (dir->exclude_list_group[EXC_CMDL].nr)
1850                return NULL;
1851
1852        /*
1853         * An optimization in prep_exclude() does not play well with
1854         * CE_SKIP_WORKTREE. It's a rare case anyway, if a single
1855         * entry has that bit set, disable the whole untracked cache.
1856         */
1857        for (i = 0; i < active_nr; i++)
1858                if (ce_skip_worktree(active_cache[i]))
1859                        return NULL;
1860
1861        if (!dir->untracked->root) {
1862                const int len = sizeof(*dir->untracked->root);
1863                dir->untracked->root = xmalloc(len);
1864                memset(dir->untracked->root, 0, len);
1865        }
1866
1867        /* Validate $GIT_DIR/info/exclude and core.excludesfile */
1868        root = dir->untracked->root;
1869        if (hashcmp(dir->ss_info_exclude.sha1,
1870                    dir->untracked->ss_info_exclude.sha1)) {
1871                invalidate_gitignore(dir->untracked, root);
1872                dir->untracked->ss_info_exclude = dir->ss_info_exclude;
1873        }
1874        if (hashcmp(dir->ss_excludes_file.sha1,
1875                    dir->untracked->ss_excludes_file.sha1)) {
1876                invalidate_gitignore(dir->untracked, root);
1877                dir->untracked->ss_excludes_file = dir->ss_excludes_file;
1878        }
1879
1880        /* Make sure this directory is not dropped out at saving phase */
1881        root->recurse = 1;
1882        return root;
1883}
1884
1885int read_directory(struct dir_struct *dir, const char *path, int len, const struct pathspec *pathspec)
1886{
1887        struct path_simplify *simplify;
1888        struct untracked_cache_dir *untracked;
1889
1890        /*
1891         * Check out create_simplify()
1892         */
1893        if (pathspec)
1894                GUARD_PATHSPEC(pathspec,
1895                               PATHSPEC_FROMTOP |
1896                               PATHSPEC_MAXDEPTH |
1897                               PATHSPEC_LITERAL |
1898                               PATHSPEC_GLOB |
1899                               PATHSPEC_ICASE |
1900                               PATHSPEC_EXCLUDE);
1901
1902        if (has_symlink_leading_path(path, len))
1903                return dir->nr;
1904
1905        /*
1906         * exclude patterns are treated like positive ones in
1907         * create_simplify. Usually exclude patterns should be a
1908         * subset of positive ones, which has no impacts on
1909         * create_simplify().
1910         */
1911        simplify = create_simplify(pathspec ? pathspec->_raw : NULL);
1912        untracked = validate_untracked_cache(dir, len, pathspec);
1913        if (!untracked)
1914                /*
1915                 * make sure untracked cache code path is disabled,
1916                 * e.g. prep_exclude()
1917                 */
1918                dir->untracked = NULL;
1919        if (!len || treat_leading_path(dir, path, len, simplify))
1920                read_directory_recursive(dir, path, len, untracked, 0, simplify);
1921        free_simplify(simplify);
1922        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
1923        qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
1924        return dir->nr;
1925}
1926
1927int file_exists(const char *f)
1928{
1929        struct stat sb;
1930        return lstat(f, &sb) == 0;
1931}
1932
1933/*
1934 * Given two normalized paths (a trailing slash is ok), if subdir is
1935 * outside dir, return -1.  Otherwise return the offset in subdir that
1936 * can be used as relative path to dir.
1937 */
1938int dir_inside_of(const char *subdir, const char *dir)
1939{
1940        int offset = 0;
1941
1942        assert(dir && subdir && *dir && *subdir);
1943
1944        while (*dir && *subdir && *dir == *subdir) {
1945                dir++;
1946                subdir++;
1947                offset++;
1948        }
1949
1950        /* hel[p]/me vs hel[l]/yeah */
1951        if (*dir && *subdir)
1952                return -1;
1953
1954        if (!*subdir)
1955                return !*dir ? offset : -1; /* same dir */
1956
1957        /* foo/[b]ar vs foo/[] */
1958        if (is_dir_sep(dir[-1]))
1959                return is_dir_sep(subdir[-1]) ? offset : -1;
1960
1961        /* foo[/]bar vs foo[] */
1962        return is_dir_sep(*subdir) ? offset + 1 : -1;
1963}
1964
1965int is_inside_dir(const char *dir)
1966{
1967        char *cwd;
1968        int rc;
1969
1970        if (!dir)
1971                return 0;
1972
1973        cwd = xgetcwd();
1974        rc = (dir_inside_of(cwd, dir) >= 0);
1975        free(cwd);
1976        return rc;
1977}
1978
1979int is_empty_dir(const char *path)
1980{
1981        DIR *dir = opendir(path);
1982        struct dirent *e;
1983        int ret = 1;
1984
1985        if (!dir)
1986                return 0;
1987
1988        while ((e = readdir(dir)) != NULL)
1989                if (!is_dot_or_dotdot(e->d_name)) {
1990                        ret = 0;
1991                        break;
1992                }
1993
1994        closedir(dir);
1995        return ret;
1996}
1997
1998static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up)
1999{
2000        DIR *dir;
2001        struct dirent *e;
2002        int ret = 0, original_len = path->len, len, kept_down = 0;
2003        int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
2004        int keep_toplevel = (flag & REMOVE_DIR_KEEP_TOPLEVEL);
2005        unsigned char submodule_head[20];
2006
2007        if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
2008            !resolve_gitlink_ref(path->buf, "HEAD", submodule_head)) {
2009                /* Do not descend and nuke a nested git work tree. */
2010                if (kept_up)
2011                        *kept_up = 1;
2012                return 0;
2013        }
2014
2015        flag &= ~REMOVE_DIR_KEEP_TOPLEVEL;
2016        dir = opendir(path->buf);
2017        if (!dir) {
2018                if (errno == ENOENT)
2019                        return keep_toplevel ? -1 : 0;
2020                else if (errno == EACCES && !keep_toplevel)
2021                        /*
2022                         * An empty dir could be removable even if it
2023                         * is unreadable:
2024                         */
2025                        return rmdir(path->buf);
2026                else
2027                        return -1;
2028        }
2029        if (path->buf[original_len - 1] != '/')
2030                strbuf_addch(path, '/');
2031
2032        len = path->len;
2033        while ((e = readdir(dir)) != NULL) {
2034                struct stat st;
2035                if (is_dot_or_dotdot(e->d_name))
2036                        continue;
2037
2038                strbuf_setlen(path, len);
2039                strbuf_addstr(path, e->d_name);
2040                if (lstat(path->buf, &st)) {
2041                        if (errno == ENOENT)
2042                                /*
2043                                 * file disappeared, which is what we
2044                                 * wanted anyway
2045                                 */
2046                                continue;
2047                        /* fall thru */
2048                } else if (S_ISDIR(st.st_mode)) {
2049                        if (!remove_dir_recurse(path, flag, &kept_down))
2050                                continue; /* happy */
2051                } else if (!only_empty &&
2052                           (!unlink(path->buf) || errno == ENOENT)) {
2053                        continue; /* happy, too */
2054                }
2055
2056                /* path too long, stat fails, or non-directory still exists */
2057                ret = -1;
2058                break;
2059        }
2060        closedir(dir);
2061
2062        strbuf_setlen(path, original_len);
2063        if (!ret && !keep_toplevel && !kept_down)
2064                ret = (!rmdir(path->buf) || errno == ENOENT) ? 0 : -1;
2065        else if (kept_up)
2066                /*
2067                 * report the uplevel that it is not an error that we
2068                 * did not rmdir() our directory.
2069                 */
2070                *kept_up = !ret;
2071        return ret;
2072}
2073
2074int remove_dir_recursively(struct strbuf *path, int flag)
2075{
2076        return remove_dir_recurse(path, flag, NULL);
2077}
2078
2079void setup_standard_excludes(struct dir_struct *dir)
2080{
2081        const char *path;
2082        char *xdg_path;
2083
2084        dir->exclude_per_dir = ".gitignore";
2085        path = git_path("info/exclude");
2086        if (!excludes_file) {
2087                home_config_paths(NULL, &xdg_path, "ignore");
2088                excludes_file = xdg_path;
2089        }
2090        if (!access_or_warn(path, R_OK, 0))
2091                add_excludes_from_file_1(dir, path,
2092                                         dir->untracked ? &dir->ss_info_exclude : NULL);
2093        if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
2094                add_excludes_from_file_1(dir, excludes_file,
2095                                         dir->untracked ? &dir->ss_excludes_file : NULL);
2096}
2097
2098int remove_path(const char *name)
2099{
2100        char *slash;
2101
2102        if (unlink(name) && errno != ENOENT && errno != ENOTDIR)
2103                return -1;
2104
2105        slash = strrchr(name, '/');
2106        if (slash) {
2107                char *dirs = xstrdup(name);
2108                slash = dirs + (slash - name);
2109                do {
2110                        *slash = '\0';
2111                } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
2112                free(dirs);
2113        }
2114        return 0;
2115}
2116
2117/*
2118 * Frees memory within dir which was allocated for exclude lists and
2119 * the exclude_stack.  Does not free dir itself.
2120 */
2121void clear_directory(struct dir_struct *dir)
2122{
2123        int i, j;
2124        struct exclude_list_group *group;
2125        struct exclude_list *el;
2126        struct exclude_stack *stk;
2127
2128        for (i = EXC_CMDL; i <= EXC_FILE; i++) {
2129                group = &dir->exclude_list_group[i];
2130                for (j = 0; j < group->nr; j++) {
2131                        el = &group->el[j];
2132                        if (i == EXC_DIRS)
2133                                free((char *)el->src);
2134                        clear_exclude_list(el);
2135                }
2136                free(group->el);
2137        }
2138
2139        stk = dir->exclude_stack;
2140        while (stk) {
2141                struct exclude_stack *prev = stk->prev;
2142                free(stk);
2143                stk = prev;
2144        }
2145        strbuf_release(&dir->basebuf);
2146}