b0100f553323998e74005edacdab44b87b5ca315
   1/*
   2 * This handles recursive filename detection with exclude
   3 * files, index knowledge etc..
   4 *
   5 * Copyright (C) Linus Torvalds, 2005-2006
   6 *               Junio Hamano, 2005-2006
   7 */
   8#include "cache.h"
   9#include "dir.h"
  10#include "refs.h"
  11
  12struct path_simplify {
  13        int len;
  14        const char *path;
  15};
  16
  17static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
  18        int check_only, const struct path_simplify *simplify);
  19static int get_dtype(struct dirent *de, const char *path, int len);
  20
  21/* helper string functions with support for the ignore_case flag */
  22int strcmp_icase(const char *a, const char *b)
  23{
  24        return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
  25}
  26
  27int strncmp_icase(const char *a, const char *b, size_t count)
  28{
  29        return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count);
  30}
  31
  32int fnmatch_icase(const char *pattern, const char *string, int flags)
  33{
  34        return fnmatch(pattern, string, flags | (ignore_case ? FNM_CASEFOLD : 0));
  35}
  36
  37static int common_prefix(const char **pathspec)
  38{
  39        const char *path, *slash, *next;
  40        int prefix;
  41
  42        if (!pathspec)
  43                return 0;
  44
  45        path = *pathspec;
  46        slash = strrchr(path, '/');
  47        if (!slash)
  48                return 0;
  49
  50        /*
  51         * The first 'prefix' characters of 'path' are common leading
  52         * path components among the pathspecs we have seen so far,
  53         * including the trailing slash.
  54         */
  55        prefix = slash - path + 1;
  56        while ((next = *++pathspec) != NULL) {
  57                int len, last_matching_slash = -1;
  58                for (len = 0; len < prefix && next[len] == path[len]; len++)
  59                        if (next[len] == '/')
  60                                last_matching_slash = len;
  61                if (len == prefix)
  62                        continue;
  63                if (last_matching_slash < 0)
  64                        return 0;
  65                prefix = last_matching_slash + 1;
  66        }
  67        return prefix;
  68}
  69
  70int fill_directory(struct dir_struct *dir, const char **pathspec)
  71{
  72        const char *path;
  73        int len;
  74
  75        /*
  76         * Calculate common prefix for the pathspec, and
  77         * use that to optimize the directory walk
  78         */
  79        len = common_prefix(pathspec);
  80        path = "";
  81
  82        if (len)
  83                path = xmemdupz(*pathspec, len);
  84
  85        /* Read the directory and prune it */
  86        read_directory(dir, path, len, pathspec);
  87        return len;
  88}
  89
  90/*
  91 * Does 'match' match the given name?
  92 * A match is found if
  93 *
  94 * (1) the 'match' string is leading directory of 'name', or
  95 * (2) the 'match' string is a wildcard and matches 'name', or
  96 * (3) the 'match' string is exactly the same as 'name'.
  97 *
  98 * and the return value tells which case it was.
  99 *
 100 * It returns 0 when there is no match.
 101 */
 102static int match_one(const char *match, const char *name, int namelen)
 103{
 104        int matchlen;
 105
 106        /* If the match was just the prefix, we matched */
 107        if (!*match)
 108                return MATCHED_RECURSIVELY;
 109
 110        if (ignore_case) {
 111                for (;;) {
 112                        unsigned char c1 = tolower(*match);
 113                        unsigned char c2 = tolower(*name);
 114                        if (c1 == '\0' || is_glob_special(c1))
 115                                break;
 116                        if (c1 != c2)
 117                                return 0;
 118                        match++;
 119                        name++;
 120                        namelen--;
 121                }
 122        } else {
 123                for (;;) {
 124                        unsigned char c1 = *match;
 125                        unsigned char c2 = *name;
 126                        if (c1 == '\0' || is_glob_special(c1))
 127                                break;
 128                        if (c1 != c2)
 129                                return 0;
 130                        match++;
 131                        name++;
 132                        namelen--;
 133                }
 134        }
 135
 136
 137        /*
 138         * If we don't match the matchstring exactly,
 139         * we need to match by fnmatch
 140         */
 141        matchlen = strlen(match);
 142        if (strncmp_icase(match, name, matchlen))
 143                return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
 144
 145        if (namelen == matchlen)
 146                return MATCHED_EXACTLY;
 147        if (match[matchlen-1] == '/' || name[matchlen] == '/')
 148                return MATCHED_RECURSIVELY;
 149        return 0;
 150}
 151
 152/*
 153 * Given a name and a list of pathspecs, see if the name matches
 154 * any of the pathspecs.  The caller is also interested in seeing
 155 * all pathspec matches some names it calls this function with
 156 * (otherwise the user could have mistyped the unmatched pathspec),
 157 * and a mark is left in seen[] array for pathspec element that
 158 * actually matched anything.
 159 */
 160int match_pathspec(const char **pathspec, const char *name, int namelen,
 161                int prefix, char *seen)
 162{
 163        int i, retval = 0;
 164
 165        if (!pathspec)
 166                return 1;
 167
 168        name += prefix;
 169        namelen -= prefix;
 170
 171        for (i = 0; pathspec[i] != NULL; i++) {
 172                int how;
 173                const char *match = pathspec[i] + prefix;
 174                if (seen && seen[i] == MATCHED_EXACTLY)
 175                        continue;
 176                how = match_one(match, name, namelen);
 177                if (how) {
 178                        if (retval < how)
 179                                retval = how;
 180                        if (seen && seen[i] < how)
 181                                seen[i] = how;
 182                }
 183        }
 184        return retval;
 185}
 186
 187static int no_wildcard(const char *string)
 188{
 189        return string[strcspn(string, "*?[{\\")] == '\0';
 190}
 191
 192void add_exclude(const char *string, const char *base,
 193                 int baselen, struct exclude_list *which)
 194{
 195        struct exclude *x;
 196        size_t len;
 197        int to_exclude = 1;
 198        int flags = 0;
 199
 200        if (*string == '!') {
 201                to_exclude = 0;
 202                string++;
 203        }
 204        len = strlen(string);
 205        if (len && string[len - 1] == '/') {
 206                char *s;
 207                x = xmalloc(sizeof(*x) + len);
 208                s = (char *)(x+1);
 209                memcpy(s, string, len - 1);
 210                s[len - 1] = '\0';
 211                string = s;
 212                x->pattern = s;
 213                flags = EXC_FLAG_MUSTBEDIR;
 214        } else {
 215                x = xmalloc(sizeof(*x));
 216                x->pattern = string;
 217        }
 218        x->to_exclude = to_exclude;
 219        x->patternlen = strlen(string);
 220        x->base = base;
 221        x->baselen = baselen;
 222        x->flags = flags;
 223        if (!strchr(string, '/'))
 224                x->flags |= EXC_FLAG_NODIR;
 225        if (no_wildcard(string))
 226                x->flags |= EXC_FLAG_NOWILDCARD;
 227        if (*string == '*' && no_wildcard(string+1))
 228                x->flags |= EXC_FLAG_ENDSWITH;
 229        ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
 230        which->excludes[which->nr++] = x;
 231}
 232
 233static void *read_skip_worktree_file_from_index(const char *path, size_t *size)
 234{
 235        int pos, len;
 236        unsigned long sz;
 237        enum object_type type;
 238        void *data;
 239        struct index_state *istate = &the_index;
 240
 241        len = strlen(path);
 242        pos = index_name_pos(istate, path, len);
 243        if (pos < 0)
 244                return NULL;
 245        if (!ce_skip_worktree(istate->cache[pos]))
 246                return NULL;
 247        data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
 248        if (!data || type != OBJ_BLOB) {
 249                free(data);
 250                return NULL;
 251        }
 252        *size = xsize_t(sz);
 253        return data;
 254}
 255
 256void free_excludes(struct exclude_list *el)
 257{
 258        int i;
 259
 260        for (i = 0; i < el->nr; i++)
 261                free(el->excludes[i]);
 262        free(el->excludes);
 263
 264        el->nr = 0;
 265        el->excludes = NULL;
 266}
 267
 268int add_excludes_from_file_to_list(const char *fname,
 269                                   const char *base,
 270                                   int baselen,
 271                                   char **buf_p,
 272                                   struct exclude_list *which,
 273                                   int check_index)
 274{
 275        struct stat st;
 276        int fd, i;
 277        size_t size = 0;
 278        char *buf, *entry;
 279
 280        fd = open(fname, O_RDONLY);
 281        if (fd < 0 || fstat(fd, &st) < 0) {
 282                if (0 <= fd)
 283                        close(fd);
 284                if (!check_index ||
 285                    (buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
 286                        return -1;
 287                if (size == 0) {
 288                        free(buf);
 289                        return 0;
 290                }
 291                if (buf[size-1] != '\n') {
 292                        buf = xrealloc(buf, size+1);
 293                        buf[size++] = '\n';
 294                }
 295        }
 296        else {
 297                size = xsize_t(st.st_size);
 298                if (size == 0) {
 299                        close(fd);
 300                        return 0;
 301                }
 302                buf = xmalloc(size+1);
 303                if (read_in_full(fd, buf, size) != size) {
 304                        free(buf);
 305                        close(fd);
 306                        return -1;
 307                }
 308                buf[size++] = '\n';
 309                close(fd);
 310        }
 311
 312        if (buf_p)
 313                *buf_p = buf;
 314        entry = buf;
 315        for (i = 0; i < size; i++) {
 316                if (buf[i] == '\n') {
 317                        if (entry != buf + i && entry[0] != '#') {
 318                                buf[i - (i && buf[i-1] == '\r')] = 0;
 319                                add_exclude(entry, base, baselen, which);
 320                        }
 321                        entry = buf + i + 1;
 322                }
 323        }
 324        return 0;
 325}
 326
 327void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 328{
 329        if (add_excludes_from_file_to_list(fname, "", 0, NULL,
 330                                           &dir->exclude_list[EXC_FILE], 0) < 0)
 331                die("cannot use %s as an exclude file", fname);
 332}
 333
 334static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
 335{
 336        struct exclude_list *el;
 337        struct exclude_stack *stk = NULL;
 338        int current;
 339
 340        if ((!dir->exclude_per_dir) ||
 341            (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
 342                return; /* too long a path -- ignore */
 343
 344        /* Pop the ones that are not the prefix of the path being checked. */
 345        el = &dir->exclude_list[EXC_DIRS];
 346        while ((stk = dir->exclude_stack) != NULL) {
 347                if (stk->baselen <= baselen &&
 348                    !strncmp(dir->basebuf, base, stk->baselen))
 349                        break;
 350                dir->exclude_stack = stk->prev;
 351                while (stk->exclude_ix < el->nr)
 352                        free(el->excludes[--el->nr]);
 353                free(stk->filebuf);
 354                free(stk);
 355        }
 356
 357        /* Read from the parent directories and push them down. */
 358        current = stk ? stk->baselen : -1;
 359        while (current < baselen) {
 360                struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
 361                const char *cp;
 362
 363                if (current < 0) {
 364                        cp = base;
 365                        current = 0;
 366                }
 367                else {
 368                        cp = strchr(base + current + 1, '/');
 369                        if (!cp)
 370                                die("oops in prep_exclude");
 371                        cp++;
 372                }
 373                stk->prev = dir->exclude_stack;
 374                stk->baselen = cp - base;
 375                stk->exclude_ix = el->nr;
 376                memcpy(dir->basebuf + current, base + current,
 377                       stk->baselen - current);
 378                strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
 379                add_excludes_from_file_to_list(dir->basebuf,
 380                                               dir->basebuf, stk->baselen,
 381                                               &stk->filebuf, el, 1);
 382                dir->exclude_stack = stk;
 383                current = stk->baselen;
 384        }
 385        dir->basebuf[baselen] = '\0';
 386}
 387
 388/* Scan the list and let the last match determine the fate.
 389 * Return 1 for exclude, 0 for include and -1 for undecided.
 390 */
 391int excluded_from_list(const char *pathname,
 392                       int pathlen, const char *basename, int *dtype,
 393                       struct exclude_list *el)
 394{
 395        int i;
 396
 397        if (el->nr) {
 398                for (i = el->nr - 1; 0 <= i; i--) {
 399                        struct exclude *x = el->excludes[i];
 400                        const char *exclude = x->pattern;
 401                        int to_exclude = x->to_exclude;
 402
 403                        if (x->flags & EXC_FLAG_MUSTBEDIR) {
 404                                if (*dtype == DT_UNKNOWN)
 405                                        *dtype = get_dtype(NULL, pathname, pathlen);
 406                                if (*dtype != DT_DIR)
 407                                        continue;
 408                        }
 409
 410                        if (x->flags & EXC_FLAG_NODIR) {
 411                                /* match basename */
 412                                if (x->flags & EXC_FLAG_NOWILDCARD) {
 413                                        if (!strcmp_icase(exclude, basename))
 414                                                return to_exclude;
 415                                } else if (x->flags & EXC_FLAG_ENDSWITH) {
 416                                        if (x->patternlen - 1 <= pathlen &&
 417                                            !strcmp_icase(exclude + 1, pathname + pathlen - x->patternlen + 1))
 418                                                return to_exclude;
 419                                } else {
 420                                        if (fnmatch_icase(exclude, basename, 0) == 0)
 421                                                return to_exclude;
 422                                }
 423                        }
 424                        else {
 425                                /* match with FNM_PATHNAME:
 426                                 * exclude has base (baselen long) implicitly
 427                                 * in front of it.
 428                                 */
 429                                int baselen = x->baselen;
 430                                if (*exclude == '/')
 431                                        exclude++;
 432
 433                                if (pathlen < baselen ||
 434                                    (baselen && pathname[baselen-1] != '/') ||
 435                                    strncmp_icase(pathname, x->base, baselen))
 436                                    continue;
 437
 438                                if (x->flags & EXC_FLAG_NOWILDCARD) {
 439                                        if (!strcmp_icase(exclude, pathname + baselen))
 440                                                return to_exclude;
 441                                } else {
 442                                        if (fnmatch_icase(exclude, pathname+baselen,
 443                                                    FNM_PATHNAME) == 0)
 444                                            return to_exclude;
 445                                }
 446                        }
 447                }
 448        }
 449        return -1; /* undecided */
 450}
 451
 452int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
 453{
 454        int pathlen = strlen(pathname);
 455        int st;
 456        const char *basename = strrchr(pathname, '/');
 457        basename = (basename) ? basename+1 : pathname;
 458
 459        prep_exclude(dir, pathname, basename-pathname);
 460        for (st = EXC_CMDL; st <= EXC_FILE; st++) {
 461                switch (excluded_from_list(pathname, pathlen, basename,
 462                                           dtype_p, &dir->exclude_list[st])) {
 463                case 0:
 464                        return 0;
 465                case 1:
 466                        return 1;
 467                }
 468        }
 469        return 0;
 470}
 471
 472static struct dir_entry *dir_entry_new(const char *pathname, int len)
 473{
 474        struct dir_entry *ent;
 475
 476        ent = xmalloc(sizeof(*ent) + len + 1);
 477        ent->len = len;
 478        memcpy(ent->name, pathname, len);
 479        ent->name[len] = 0;
 480        return ent;
 481}
 482
 483static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
 484{
 485        if (cache_name_exists(pathname, len, ignore_case))
 486                return NULL;
 487
 488        ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
 489        return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
 490}
 491
 492struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
 493{
 494        if (!cache_name_is_other(pathname, len))
 495                return NULL;
 496
 497        ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
 498        return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
 499}
 500
 501enum exist_status {
 502        index_nonexistent = 0,
 503        index_directory,
 504        index_gitdir
 505};
 506
 507/*
 508 * Do not use the alphabetically stored index to look up
 509 * the directory name; instead, use the case insensitive
 510 * name hash.
 511 */
 512static enum exist_status directory_exists_in_index_icase(const char *dirname, int len)
 513{
 514        struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case);
 515        unsigned char endchar;
 516
 517        if (!ce)
 518                return index_nonexistent;
 519        endchar = ce->name[len];
 520
 521        /*
 522         * The cache_entry structure returned will contain this dirname
 523         * and possibly additional path components.
 524         */
 525        if (endchar == '/')
 526                return index_directory;
 527
 528        /*
 529         * If there are no additional path components, then this cache_entry
 530         * represents a submodule.  Submodules, despite being directories,
 531         * are stored in the cache without a closing slash.
 532         */
 533        if (!endchar && S_ISGITLINK(ce->ce_mode))
 534                return index_gitdir;
 535
 536        /* This should never be hit, but it exists just in case. */
 537        return index_nonexistent;
 538}
 539
 540/*
 541 * The index sorts alphabetically by entry name, which
 542 * means that a gitlink sorts as '\0' at the end, while
 543 * a directory (which is defined not as an entry, but as
 544 * the files it contains) will sort with the '/' at the
 545 * end.
 546 */
 547static enum exist_status directory_exists_in_index(const char *dirname, int len)
 548{
 549        int pos;
 550
 551        if (ignore_case)
 552                return directory_exists_in_index_icase(dirname, len);
 553
 554        pos = cache_name_pos(dirname, len);
 555        if (pos < 0)
 556                pos = -pos-1;
 557        while (pos < active_nr) {
 558                struct cache_entry *ce = active_cache[pos++];
 559                unsigned char endchar;
 560
 561                if (strncmp(ce->name, dirname, len))
 562                        break;
 563                endchar = ce->name[len];
 564                if (endchar > '/')
 565                        break;
 566                if (endchar == '/')
 567                        return index_directory;
 568                if (!endchar && S_ISGITLINK(ce->ce_mode))
 569                        return index_gitdir;
 570        }
 571        return index_nonexistent;
 572}
 573
 574/*
 575 * When we find a directory when traversing the filesystem, we
 576 * have three distinct cases:
 577 *
 578 *  - ignore it
 579 *  - see it as a directory
 580 *  - recurse into it
 581 *
 582 * and which one we choose depends on a combination of existing
 583 * git index contents and the flags passed into the directory
 584 * traversal routine.
 585 *
 586 * Case 1: If we *already* have entries in the index under that
 587 * directory name, we always recurse into the directory to see
 588 * all the files.
 589 *
 590 * Case 2: If we *already* have that directory name as a gitlink,
 591 * we always continue to see it as a gitlink, regardless of whether
 592 * there is an actual git directory there or not (it might not
 593 * be checked out as a subproject!)
 594 *
 595 * Case 3: if we didn't have it in the index previously, we
 596 * have a few sub-cases:
 597 *
 598 *  (a) if "show_other_directories" is true, we show it as
 599 *      just a directory, unless "hide_empty_directories" is
 600 *      also true and the directory is empty, in which case
 601 *      we just ignore it entirely.
 602 *  (b) if it looks like a git directory, and we don't have
 603 *      'no_gitlinks' set we treat it as a gitlink, and show it
 604 *      as a directory.
 605 *  (c) otherwise, we recurse into it.
 606 */
 607enum directory_treatment {
 608        show_directory,
 609        ignore_directory,
 610        recurse_into_directory
 611};
 612
 613static enum directory_treatment treat_directory(struct dir_struct *dir,
 614        const char *dirname, int len,
 615        const struct path_simplify *simplify)
 616{
 617        /* The "len-1" is to strip the final '/' */
 618        switch (directory_exists_in_index(dirname, len-1)) {
 619        case index_directory:
 620                return recurse_into_directory;
 621
 622        case index_gitdir:
 623                if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
 624                        return ignore_directory;
 625                return show_directory;
 626
 627        case index_nonexistent:
 628                if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
 629                        break;
 630                if (!(dir->flags & DIR_NO_GITLINKS)) {
 631                        unsigned char sha1[20];
 632                        if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
 633                                return show_directory;
 634                }
 635                return recurse_into_directory;
 636        }
 637
 638        /* This is the "show_other_directories" case */
 639        if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
 640                return show_directory;
 641        if (!read_directory_recursive(dir, dirname, len, 1, simplify))
 642                return ignore_directory;
 643        return show_directory;
 644}
 645
 646/*
 647 * This is an inexact early pruning of any recursive directory
 648 * reading - if the path cannot possibly be in the pathspec,
 649 * return true, and we'll skip it early.
 650 */
 651static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
 652{
 653        if (simplify) {
 654                for (;;) {
 655                        const char *match = simplify->path;
 656                        int len = simplify->len;
 657
 658                        if (!match)
 659                                break;
 660                        if (len > pathlen)
 661                                len = pathlen;
 662                        if (!memcmp(path, match, len))
 663                                return 0;
 664                        simplify++;
 665                }
 666                return 1;
 667        }
 668        return 0;
 669}
 670
 671/*
 672 * This function tells us whether an excluded path matches a
 673 * list of "interesting" pathspecs. That is, whether a path matched
 674 * by any of the pathspecs could possibly be ignored by excluding
 675 * the specified path. This can happen if:
 676 *
 677 *   1. the path is mentioned explicitly in the pathspec
 678 *
 679 *   2. the path is a directory prefix of some element in the
 680 *      pathspec
 681 */
 682static int exclude_matches_pathspec(const char *path, int len,
 683                const struct path_simplify *simplify)
 684{
 685        if (simplify) {
 686                for (; simplify->path; simplify++) {
 687                        if (len == simplify->len
 688                            && !memcmp(path, simplify->path, len))
 689                                return 1;
 690                        if (len < simplify->len
 691                            && simplify->path[len] == '/'
 692                            && !memcmp(path, simplify->path, len))
 693                                return 1;
 694                }
 695        }
 696        return 0;
 697}
 698
 699static int get_index_dtype(const char *path, int len)
 700{
 701        int pos;
 702        struct cache_entry *ce;
 703
 704        ce = cache_name_exists(path, len, 0);
 705        if (ce) {
 706                if (!ce_uptodate(ce))
 707                        return DT_UNKNOWN;
 708                if (S_ISGITLINK(ce->ce_mode))
 709                        return DT_DIR;
 710                /*
 711                 * Nobody actually cares about the
 712                 * difference between DT_LNK and DT_REG
 713                 */
 714                return DT_REG;
 715        }
 716
 717        /* Try to look it up as a directory */
 718        pos = cache_name_pos(path, len);
 719        if (pos >= 0)
 720                return DT_UNKNOWN;
 721        pos = -pos-1;
 722        while (pos < active_nr) {
 723                ce = active_cache[pos++];
 724                if (strncmp(ce->name, path, len))
 725                        break;
 726                if (ce->name[len] > '/')
 727                        break;
 728                if (ce->name[len] < '/')
 729                        continue;
 730                if (!ce_uptodate(ce))
 731                        break;  /* continue? */
 732                return DT_DIR;
 733        }
 734        return DT_UNKNOWN;
 735}
 736
 737static int get_dtype(struct dirent *de, const char *path, int len)
 738{
 739        int dtype = de ? DTYPE(de) : DT_UNKNOWN;
 740        struct stat st;
 741
 742        if (dtype != DT_UNKNOWN)
 743                return dtype;
 744        dtype = get_index_dtype(path, len);
 745        if (dtype != DT_UNKNOWN)
 746                return dtype;
 747        if (lstat(path, &st))
 748                return dtype;
 749        if (S_ISREG(st.st_mode))
 750                return DT_REG;
 751        if (S_ISDIR(st.st_mode))
 752                return DT_DIR;
 753        if (S_ISLNK(st.st_mode))
 754                return DT_LNK;
 755        return dtype;
 756}
 757
 758enum path_treatment {
 759        path_ignored,
 760        path_handled,
 761        path_recurse
 762};
 763
 764static enum path_treatment treat_one_path(struct dir_struct *dir,
 765                                          char *path, int *len,
 766                                          const struct path_simplify *simplify,
 767                                          int dtype, struct dirent *de)
 768{
 769        int exclude = excluded(dir, path, &dtype);
 770        if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
 771            && exclude_matches_pathspec(path, *len, simplify))
 772                dir_add_ignored(dir, path, *len);
 773
 774        /*
 775         * Excluded? If we don't explicitly want to show
 776         * ignored files, ignore it
 777         */
 778        if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
 779                return path_ignored;
 780
 781        if (dtype == DT_UNKNOWN)
 782                dtype = get_dtype(de, path, *len);
 783
 784        /*
 785         * Do we want to see just the ignored files?
 786         * We still need to recurse into directories,
 787         * even if we don't ignore them, since the
 788         * directory may contain files that we do..
 789         */
 790        if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
 791                if (dtype != DT_DIR)
 792                        return path_ignored;
 793        }
 794
 795        switch (dtype) {
 796        default:
 797                return path_ignored;
 798        case DT_DIR:
 799                memcpy(path + *len, "/", 2);
 800                (*len)++;
 801                switch (treat_directory(dir, path, *len, simplify)) {
 802                case show_directory:
 803                        if (exclude != !!(dir->flags
 804                                          & DIR_SHOW_IGNORED))
 805                                return path_ignored;
 806                        break;
 807                case recurse_into_directory:
 808                        return path_recurse;
 809                case ignore_directory:
 810                        return path_ignored;
 811                }
 812                break;
 813        case DT_REG:
 814        case DT_LNK:
 815                break;
 816        }
 817        return path_handled;
 818}
 819
 820static enum path_treatment treat_path(struct dir_struct *dir,
 821                                      struct dirent *de,
 822                                      char *path, int path_max,
 823                                      int baselen,
 824                                      const struct path_simplify *simplify,
 825                                      int *len)
 826{
 827        int dtype;
 828
 829        if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
 830                return path_ignored;
 831        *len = strlen(de->d_name);
 832        /* Ignore overly long pathnames! */
 833        if (*len + baselen + 8 > path_max)
 834                return path_ignored;
 835        memcpy(path + baselen, de->d_name, *len + 1);
 836        *len += baselen;
 837        if (simplify_away(path, *len, simplify))
 838                return path_ignored;
 839
 840        dtype = DTYPE(de);
 841        return treat_one_path(dir, path, len, simplify, dtype, de);
 842}
 843
 844/*
 845 * Read a directory tree. We currently ignore anything but
 846 * directories, regular files and symlinks. That's because git
 847 * doesn't handle them at all yet. Maybe that will change some
 848 * day.
 849 *
 850 * Also, we ignore the name ".git" (even if it is not a directory).
 851 * That likely will not change.
 852 */
 853static int read_directory_recursive(struct dir_struct *dir,
 854                                    const char *base, int baselen,
 855                                    int check_only,
 856                                    const struct path_simplify *simplify)
 857{
 858        DIR *fdir = opendir(*base ? base : ".");
 859        int contents = 0;
 860
 861        if (fdir) {
 862                struct dirent *de;
 863                char path[PATH_MAX + 1];
 864                memcpy(path, base, baselen);
 865
 866                while ((de = readdir(fdir)) != NULL) {
 867                        int len;
 868                        switch (treat_path(dir, de, path, sizeof(path),
 869                                           baselen, simplify, &len)) {
 870                        case path_recurse:
 871                                contents += read_directory_recursive
 872                                        (dir, path, len, 0, simplify);
 873                                continue;
 874                        case path_ignored:
 875                                continue;
 876                        case path_handled:
 877                                break;
 878                        }
 879                        contents++;
 880                        if (check_only)
 881                                goto exit_early;
 882                        else
 883                                dir_add_name(dir, path, len);
 884                }
 885exit_early:
 886                closedir(fdir);
 887        }
 888
 889        return contents;
 890}
 891
 892static int cmp_name(const void *p1, const void *p2)
 893{
 894        const struct dir_entry *e1 = *(const struct dir_entry **)p1;
 895        const struct dir_entry *e2 = *(const struct dir_entry **)p2;
 896
 897        return cache_name_compare(e1->name, e1->len,
 898                                  e2->name, e2->len);
 899}
 900
 901/*
 902 * Return the length of the "simple" part of a path match limiter.
 903 */
 904static int simple_length(const char *match)
 905{
 906        int len = -1;
 907
 908        for (;;) {
 909                unsigned char c = *match++;
 910                len++;
 911                if (c == '\0' || is_glob_special(c))
 912                        return len;
 913        }
 914}
 915
 916static struct path_simplify *create_simplify(const char **pathspec)
 917{
 918        int nr, alloc = 0;
 919        struct path_simplify *simplify = NULL;
 920
 921        if (!pathspec)
 922                return NULL;
 923
 924        for (nr = 0 ; ; nr++) {
 925                const char *match;
 926                if (nr >= alloc) {
 927                        alloc = alloc_nr(alloc);
 928                        simplify = xrealloc(simplify, alloc * sizeof(*simplify));
 929                }
 930                match = *pathspec++;
 931                if (!match)
 932                        break;
 933                simplify[nr].path = match;
 934                simplify[nr].len = simple_length(match);
 935        }
 936        simplify[nr].path = NULL;
 937        simplify[nr].len = 0;
 938        return simplify;
 939}
 940
 941static void free_simplify(struct path_simplify *simplify)
 942{
 943        free(simplify);
 944}
 945
 946static int treat_leading_path(struct dir_struct *dir,
 947                              const char *path, int len,
 948                              const struct path_simplify *simplify)
 949{
 950        char pathbuf[PATH_MAX];
 951        int baselen, blen;
 952        const char *cp;
 953
 954        while (len && path[len - 1] == '/')
 955                len--;
 956        if (!len)
 957                return 1;
 958        baselen = 0;
 959        while (1) {
 960                cp = path + baselen + !!baselen;
 961                cp = memchr(cp, '/', path + len - cp);
 962                if (!cp)
 963                        baselen = len;
 964                else
 965                        baselen = cp - path;
 966                memcpy(pathbuf, path, baselen);
 967                pathbuf[baselen] = '\0';
 968                if (!is_directory(pathbuf))
 969                        return 0;
 970                if (simplify_away(pathbuf, baselen, simplify))
 971                        return 0;
 972                blen = baselen;
 973                if (treat_one_path(dir, pathbuf, &blen, simplify,
 974                                   DT_DIR, NULL) == path_ignored)
 975                        return 0; /* do not recurse into it */
 976                if (len <= baselen)
 977                        return 1; /* finished checking */
 978        }
 979}
 980
 981int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
 982{
 983        struct path_simplify *simplify;
 984
 985        if (has_symlink_leading_path(path, len))
 986                return dir->nr;
 987
 988        simplify = create_simplify(pathspec);
 989        if (!len || treat_leading_path(dir, path, len, simplify))
 990                read_directory_recursive(dir, path, len, 0, simplify);
 991        free_simplify(simplify);
 992        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
 993        qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
 994        return dir->nr;
 995}
 996
 997int file_exists(const char *f)
 998{
 999        struct stat sb;
1000        return lstat(f, &sb) == 0;
1001}
1002
1003/*
1004 * get_relative_cwd() gets the prefix of the current working directory
1005 * relative to 'dir'.  If we are not inside 'dir', it returns NULL.
1006 *
1007 * As a convenience, it also returns NULL if 'dir' is already NULL.  The
1008 * reason for this behaviour is that it is natural for functions returning
1009 * directory names to return NULL to say "this directory does not exist"
1010 * or "this directory is invalid".  These cases are usually handled the
1011 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
1012 * returns NULL for both of them.
1013 *
1014 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
1015 * unifies the handling of "outside work tree" with "no work tree at all".
1016 */
1017char *get_relative_cwd(char *buffer, int size, const char *dir)
1018{
1019        char *cwd = buffer;
1020
1021        if (!dir)
1022                return NULL;
1023        if (!getcwd(buffer, size))
1024                die_errno("can't find the current directory");
1025
1026        if (!is_absolute_path(dir))
1027                dir = make_absolute_path(dir);
1028
1029        while (*dir && *dir == *cwd) {
1030                dir++;
1031                cwd++;
1032        }
1033        if (*dir)
1034                return NULL;
1035        switch (*cwd) {
1036        case '\0':
1037                return cwd;
1038        case '/':
1039                return cwd + 1;
1040        default:
1041                /*
1042                 * dir can end with a path separator when it's root
1043                 * directory. Return proper prefix in that case.
1044                 */
1045                if (dir[-1] == '/')
1046                        return cwd;
1047                return NULL;
1048        }
1049}
1050
1051 * Given two normalized paths (a trailing slash is ok), if subdir is
1052 * outside dir, return -1.  Otherwise return the offset in subdir that
1053 * can be used as relative path to dir.
1054 */
1055int dir_inside_of(const char *subdir, const char *dir)
1056{
1057        int offset = 0;
1058
1059        assert(dir && subdir && *dir && *subdir);
1060
1061        while (*dir && *subdir && *dir == *subdir) {
1062                dir++;
1063                subdir++;
1064                offset++;
1065        }
1066
1067        /* hel[p]/me vs hel[l]/yeah */
1068        if (*dir && *subdir)
1069                return -1;
1070
1071        if (!*subdir)
1072                return !*dir ? offset : -1; /* same dir */
1073
1074        /* foo/[b]ar vs foo/[] */
1075        if (is_dir_sep(dir[-1]))
1076                return is_dir_sep(subdir[-1]) ? offset : -1;
1077
1078        /* foo[/]bar vs foo[] */
1079        return is_dir_sep(*subdir) ? offset + 1 : -1;
1080}
1081
1082int is_inside_dir(const char *dir)
1083{
1084        char buffer[PATH_MAX];
1085        return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
1086}
1087
1088int is_empty_dir(const char *path)
1089{
1090        DIR *dir = opendir(path);
1091        struct dirent *e;
1092        int ret = 1;
1093
1094        if (!dir)
1095                return 0;
1096
1097        while ((e = readdir(dir)) != NULL)
1098                if (!is_dot_or_dotdot(e->d_name)) {
1099                        ret = 0;
1100                        break;
1101                }
1102
1103        closedir(dir);
1104        return ret;
1105}
1106
1107int remove_dir_recursively(struct strbuf *path, int flag)
1108{
1109        DIR *dir;
1110        struct dirent *e;
1111        int ret = 0, original_len = path->len, len;
1112        int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
1113        unsigned char submodule_head[20];
1114
1115        if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
1116            !resolve_gitlink_ref(path->buf, "HEAD", submodule_head))
1117                /* Do not descend and nuke a nested git work tree. */
1118                return 0;
1119
1120        dir = opendir(path->buf);
1121        if (!dir)
1122                return -1;
1123        if (path->buf[original_len - 1] != '/')
1124                strbuf_addch(path, '/');
1125
1126        len = path->len;
1127        while ((e = readdir(dir)) != NULL) {
1128                struct stat st;
1129                if (is_dot_or_dotdot(e->d_name))
1130                        continue;
1131
1132                strbuf_setlen(path, len);
1133                strbuf_addstr(path, e->d_name);
1134                if (lstat(path->buf, &st))
1135                        ; /* fall thru */
1136                else if (S_ISDIR(st.st_mode)) {
1137                        if (!remove_dir_recursively(path, only_empty))
1138                                continue; /* happy */
1139                } else if (!only_empty && !unlink(path->buf))
1140                        continue; /* happy, too */
1141
1142                /* path too long, stat fails, or non-directory still exists */
1143                ret = -1;
1144                break;
1145        }
1146        closedir(dir);
1147
1148        strbuf_setlen(path, original_len);
1149        if (!ret)
1150                ret = rmdir(path->buf);
1151        return ret;
1152}
1153
1154void setup_standard_excludes(struct dir_struct *dir)
1155{
1156        const char *path;
1157
1158        dir->exclude_per_dir = ".gitignore";
1159        path = git_path("info/exclude");
1160        if (!access(path, R_OK))
1161                add_excludes_from_file(dir, path);
1162        if (excludes_file && !access(excludes_file, R_OK))
1163                add_excludes_from_file(dir, excludes_file);
1164}
1165
1166int remove_path(const char *name)
1167{
1168        char *slash;
1169
1170        if (unlink(name) && errno != ENOENT)
1171                return -1;
1172
1173        slash = strrchr(name, '/');
1174        if (slash) {
1175                char *dirs = xstrdup(name);
1176                slash = dirs + (slash - name);
1177                do {
1178                        *slash = '\0';
1179                } while (rmdir(dirs) == 0 && (slash = strrchr(dirs, '/')));
1180                free(dirs);
1181        }
1182        return 0;
1183}
1184