dir.con commit Merge branch 'js/rebase' into HEAD (5153399)
   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,
  18        const char *path, const char *base, int baselen,
  19        int check_only, const struct path_simplify *simplify);
  20
  21int common_prefix(const char **pathspec)
  22{
  23        const char *path, *slash, *next;
  24        int prefix;
  25
  26        if (!pathspec)
  27                return 0;
  28
  29        path = *pathspec;
  30        slash = strrchr(path, '/');
  31        if (!slash)
  32                return 0;
  33
  34        prefix = slash - path + 1;
  35        while ((next = *++pathspec) != NULL) {
  36                int len = strlen(next);
  37                if (len >= prefix && !memcmp(path, next, prefix))
  38                        continue;
  39                len = prefix - 1;
  40                for (;;) {
  41                        if (!len)
  42                                return 0;
  43                        if (next[--len] != '/')
  44                                continue;
  45                        if (memcmp(path, next, len+1))
  46                                continue;
  47                        prefix = len + 1;
  48                        break;
  49                }
  50        }
  51        return prefix;
  52}
  53
  54/*
  55 * Does 'match' matches the given name?
  56 * A match is found if
  57 *
  58 * (1) the 'match' string is leading directory of 'name', or
  59 * (2) the 'match' string is a wildcard and matches 'name', or
  60 * (3) the 'match' string is exactly the same as 'name'.
  61 *
  62 * and the return value tells which case it was.
  63 *
  64 * It returns 0 when there is no match.
  65 */
  66static int match_one(const char *match, const char *name, int namelen)
  67{
  68        int matchlen;
  69
  70        /* If the match was just the prefix, we matched */
  71        matchlen = strlen(match);
  72        if (!matchlen)
  73                return MATCHED_RECURSIVELY;
  74
  75        /*
  76         * If we don't match the matchstring exactly,
  77         * we need to match by fnmatch
  78         */
  79        if (strncmp(match, name, matchlen))
  80                return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
  81
  82        if (!name[matchlen])
  83                return MATCHED_EXACTLY;
  84        if (match[matchlen-1] == '/' || name[matchlen] == '/')
  85                return MATCHED_RECURSIVELY;
  86        return 0;
  87}
  88
  89/*
  90 * Given a name and a list of pathspecs, see if the name matches
  91 * any of the pathspecs.  The caller is also interested in seeing
  92 * all pathspec matches some names it calls this function with
  93 * (otherwise the user could have mistyped the unmatched pathspec),
  94 * and a mark is left in seen[] array for pathspec element that
  95 * actually matched anything.
  96 */
  97int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
  98{
  99        int retval;
 100        const char *match;
 101
 102        name += prefix;
 103        namelen -= prefix;
 104
 105        for (retval = 0; (match = *pathspec++) != NULL; seen++) {
 106                int how;
 107                if (retval && *seen == MATCHED_EXACTLY)
 108                        continue;
 109                match += prefix;
 110                how = match_one(match, name, namelen);
 111                if (how) {
 112                        if (retval < how)
 113                                retval = how;
 114                        if (*seen < how)
 115                                *seen = how;
 116                }
 117        }
 118        return retval;
 119}
 120
 121static int no_wildcard(const char *string)
 122{
 123        return string[strcspn(string, "*?[{")] == '\0';
 124}
 125
 126void add_exclude(const char *string, const char *base,
 127                 int baselen, struct exclude_list *which)
 128{
 129        struct exclude *x = xmalloc(sizeof (*x));
 130
 131        x->to_exclude = 1;
 132        if (*string == '!') {
 133                x->to_exclude = 0;
 134                string++;
 135        }
 136        x->pattern = string;
 137        x->patternlen = strlen(string);
 138        x->base = base;
 139        x->baselen = baselen;
 140        x->flags = 0;
 141        if (!strchr(string, '/'))
 142                x->flags |= EXC_FLAG_NODIR;
 143        if (no_wildcard(string))
 144                x->flags |= EXC_FLAG_NOWILDCARD;
 145        if (*string == '*' && no_wildcard(string+1))
 146                x->flags |= EXC_FLAG_ENDSWITH;
 147        if (which->nr == which->alloc) {
 148                which->alloc = alloc_nr(which->alloc);
 149                which->excludes = xrealloc(which->excludes,
 150                                           which->alloc * sizeof(x));
 151        }
 152        which->excludes[which->nr++] = x;
 153}
 154
 155static int add_excludes_from_file_1(const char *fname,
 156                                    const char *base,
 157                                    int baselen,
 158                                    struct exclude_list *which)
 159{
 160        struct stat st;
 161        int fd, i;
 162        size_t size;
 163        char *buf, *entry;
 164
 165        fd = open(fname, O_RDONLY);
 166        if (fd < 0 || fstat(fd, &st) < 0)
 167                goto err;
 168        size = xsize_t(st.st_size);
 169        if (size == 0) {
 170                close(fd);
 171                return 0;
 172        }
 173        buf = xmalloc(size+1);
 174        if (read_in_full(fd, buf, size) != size)
 175                goto err;
 176        close(fd);
 177
 178        buf[size++] = '\n';
 179        entry = buf;
 180        for (i = 0; i < size; i++) {
 181                if (buf[i] == '\n') {
 182                        if (entry != buf + i && entry[0] != '#') {
 183                                buf[i - (i && buf[i-1] == '\r')] = 0;
 184                                add_exclude(entry, base, baselen, which);
 185                        }
 186                        entry = buf + i + 1;
 187                }
 188        }
 189        return 0;
 190
 191 err:
 192        if (0 <= fd)
 193                close(fd);
 194        return -1;
 195}
 196
 197void add_excludes_from_file(struct dir_struct *dir, const char *fname)
 198{
 199        if (add_excludes_from_file_1(fname, "", 0,
 200                                     &dir->exclude_list[EXC_FILE]) < 0)
 201                die("cannot use %s as an exclude file", fname);
 202}
 203
 204int push_exclude_per_directory(struct dir_struct *dir, const char *base, int baselen)
 205{
 206        char exclude_file[PATH_MAX];
 207        struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
 208        int current_nr = el->nr;
 209
 210        if (dir->exclude_per_dir) {
 211                memcpy(exclude_file, base, baselen);
 212                strcpy(exclude_file + baselen, dir->exclude_per_dir);
 213                add_excludes_from_file_1(exclude_file, base, baselen, el);
 214        }
 215        return current_nr;
 216}
 217
 218void pop_exclude_per_directory(struct dir_struct *dir, int stk)
 219{
 220        struct exclude_list *el = &dir->exclude_list[EXC_DIRS];
 221
 222        while (stk < el->nr)
 223                free(el->excludes[--el->nr]);
 224}
 225
 226/* Scan the list and let the last match determines the fate.
 227 * Return 1 for exclude, 0 for include and -1 for undecided.
 228 */
 229static int excluded_1(const char *pathname,
 230                      int pathlen, const char *basename,
 231                      struct exclude_list *el)
 232{
 233        int i;
 234
 235        if (el->nr) {
 236                for (i = el->nr - 1; 0 <= i; i--) {
 237                        struct exclude *x = el->excludes[i];
 238                        const char *exclude = x->pattern;
 239                        int to_exclude = x->to_exclude;
 240
 241                        if (x->flags & EXC_FLAG_NODIR) {
 242                                /* match basename */
 243                                if (x->flags & EXC_FLAG_NOWILDCARD) {
 244                                        if (!strcmp(exclude, basename))
 245                                                return to_exclude;
 246                                } else if (x->flags & EXC_FLAG_ENDSWITH) {
 247                                        if (x->patternlen - 1 <= pathlen &&
 248                                            !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
 249                                                return to_exclude;
 250                                } else {
 251                                        if (fnmatch(exclude, basename, 0) == 0)
 252                                                return to_exclude;
 253                                }
 254                        }
 255                        else {
 256                                /* match with FNM_PATHNAME:
 257                                 * exclude has base (baselen long) implicitly
 258                                 * in front of it.
 259                                 */
 260                                int baselen = x->baselen;
 261                                if (*exclude == '/')
 262                                        exclude++;
 263
 264                                if (pathlen < baselen ||
 265                                    (baselen && pathname[baselen-1] != '/') ||
 266                                    strncmp(pathname, x->base, baselen))
 267                                    continue;
 268
 269                                if (x->flags & EXC_FLAG_NOWILDCARD) {
 270                                        if (!strcmp(exclude, pathname + baselen))
 271                                                return to_exclude;
 272                                } else {
 273                                        if (fnmatch(exclude, pathname+baselen,
 274                                                    FNM_PATHNAME) == 0)
 275                                            return to_exclude;
 276                                }
 277                        }
 278                }
 279        }
 280        return -1; /* undecided */
 281}
 282
 283int excluded(struct dir_struct *dir, const char *pathname)
 284{
 285        int pathlen = strlen(pathname);
 286        int st;
 287        const char *basename = strrchr(pathname, '/');
 288        basename = (basename) ? basename+1 : pathname;
 289
 290        for (st = EXC_CMDL; st <= EXC_FILE; st++) {
 291                switch (excluded_1(pathname, pathlen, basename, &dir->exclude_list[st])) {
 292                case 0:
 293                        return 0;
 294                case 1:
 295                        return 1;
 296                }
 297        }
 298        return 0;
 299}
 300
 301static struct dir_entry *dir_entry_new(const char *pathname, int len) {
 302        struct dir_entry *ent;
 303
 304        ent = xmalloc(sizeof(*ent) + len + 1);
 305        ent->len = len;
 306        memcpy(ent->name, pathname, len);
 307        ent->name[len] = 0;
 308        return ent;
 309}
 310
 311struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
 312{
 313        if (cache_name_pos(pathname, len) >= 0)
 314                return NULL;
 315
 316        ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
 317        return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
 318}
 319
 320struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
 321{
 322        if (cache_name_pos(pathname, len) >= 0)
 323                return NULL;
 324
 325        ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
 326        return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
 327}
 328
 329enum exist_status {
 330        index_nonexistent = 0,
 331        index_directory,
 332        index_gitdir,
 333};
 334
 335/*
 336 * The index sorts alphabetically by entry name, which
 337 * means that a gitlink sorts as '\0' at the end, while
 338 * a directory (which is defined not as an entry, but as
 339 * the files it contains) will sort with the '/' at the
 340 * end.
 341 */
 342static enum exist_status directory_exists_in_index(const char *dirname, int len)
 343{
 344        int pos = cache_name_pos(dirname, len);
 345        if (pos < 0)
 346                pos = -pos-1;
 347        while (pos < active_nr) {
 348                struct cache_entry *ce = active_cache[pos++];
 349                unsigned char endchar;
 350
 351                if (strncmp(ce->name, dirname, len))
 352                        break;
 353                endchar = ce->name[len];
 354                if (endchar > '/')
 355                        break;
 356                if (endchar == '/')
 357                        return index_directory;
 358                if (!endchar && S_ISGITLINK(ntohl(ce->ce_mode)))
 359                        return index_gitdir;
 360        }
 361        return index_nonexistent;
 362}
 363
 364/*
 365 * When we find a directory when traversing the filesystem, we
 366 * have three distinct cases:
 367 *
 368 *  - ignore it
 369 *  - see it as a directory
 370 *  - recurse into it
 371 *
 372 * and which one we choose depends on a combination of existing
 373 * git index contents and the flags passed into the directory
 374 * traversal routine.
 375 *
 376 * Case 1: If we *already* have entries in the index under that
 377 * directory name, we always recurse into the directory to see
 378 * all the files.
 379 *
 380 * Case 2: If we *already* have that directory name as a gitlink,
 381 * we always continue to see it as a gitlink, regardless of whether
 382 * there is an actual git directory there or not (it might not
 383 * be checked out as a subproject!)
 384 *
 385 * Case 3: if we didn't have it in the index previously, we
 386 * have a few sub-cases:
 387 *
 388 *  (a) if "show_other_directories" is true, we show it as
 389 *      just a directory, unless "hide_empty_directories" is
 390 *      also true and the directory is empty, in which case
 391 *      we just ignore it entirely.
 392 *  (b) if it looks like a git directory, and we don't have
 393 *      'no_gitlinks' set we treat it as a gitlink, and show it
 394 *      as a directory.
 395 *  (c) otherwise, we recurse into it.
 396 */
 397enum directory_treatment {
 398        show_directory,
 399        ignore_directory,
 400        recurse_into_directory,
 401};
 402
 403static enum directory_treatment treat_directory(struct dir_struct *dir,
 404        const char *dirname, int len,
 405        const struct path_simplify *simplify)
 406{
 407        /* The "len-1" is to strip the final '/' */
 408        switch (directory_exists_in_index(dirname, len-1)) {
 409        case index_directory:
 410                return recurse_into_directory;
 411
 412        case index_gitdir:
 413                if (dir->show_other_directories)
 414                        return ignore_directory;
 415                return show_directory;
 416
 417        case index_nonexistent:
 418                if (dir->show_other_directories)
 419                        break;
 420                if (!dir->no_gitlinks) {
 421                        unsigned char sha1[20];
 422                        if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
 423                                return show_directory;
 424                }
 425                return recurse_into_directory;
 426        }
 427
 428        /* This is the "show_other_directories" case */
 429        if (!dir->hide_empty_directories)
 430                return show_directory;
 431        if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
 432                return ignore_directory;
 433        return show_directory;
 434}
 435
 436/*
 437 * This is an inexact early pruning of any recursive directory
 438 * reading - if the path cannot possibly be in the pathspec,
 439 * return true, and we'll skip it early.
 440 */
 441static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
 442{
 443        if (simplify) {
 444                for (;;) {
 445                        const char *match = simplify->path;
 446                        int len = simplify->len;
 447
 448                        if (!match)
 449                                break;
 450                        if (len > pathlen)
 451                                len = pathlen;
 452                        if (!memcmp(path, match, len))
 453                                return 0;
 454                        simplify++;
 455                }
 456                return 1;
 457        }
 458        return 0;
 459}
 460
 461static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
 462{
 463        if (simplify) {
 464                for (; simplify->path; simplify++) {
 465                        if (len == simplify->len
 466                            && !memcmp(path, simplify->path, len))
 467                                return 1;
 468                }
 469        }
 470        return 0;
 471}
 472
 473static int get_dtype(struct dirent *de, const char *path)
 474{
 475        int dtype = DTYPE(de);
 476        struct stat st;
 477
 478        if (dtype != DT_UNKNOWN)
 479                return dtype;
 480        if (lstat(path, &st))
 481                return dtype;
 482        if (S_ISREG(st.st_mode))
 483                return DT_REG;
 484        if (S_ISDIR(st.st_mode))
 485                return DT_DIR;
 486        if (S_ISLNK(st.st_mode))
 487                return DT_LNK;
 488        return dtype;
 489}
 490
 491/*
 492 * Read a directory tree. We currently ignore anything but
 493 * directories, regular files and symlinks. That's because git
 494 * doesn't handle them at all yet. Maybe that will change some
 495 * day.
 496 *
 497 * Also, we ignore the name ".git" (even if it is not a directory).
 498 * That likely will not change.
 499 */
 500static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
 501{
 502        DIR *fdir = opendir(path);
 503        int contents = 0;
 504
 505        if (fdir) {
 506                int exclude_stk;
 507                struct dirent *de;
 508                char fullname[PATH_MAX + 1];
 509                memcpy(fullname, base, baselen);
 510
 511                exclude_stk = push_exclude_per_directory(dir, base, baselen);
 512
 513                while ((de = readdir(fdir)) != NULL) {
 514                        int len, dtype;
 515                        int exclude;
 516
 517                        if ((de->d_name[0] == '.') &&
 518                            (de->d_name[1] == 0 ||
 519                             !strcmp(de->d_name + 1, ".") ||
 520                             !strcmp(de->d_name + 1, "git")))
 521                                continue;
 522                        len = strlen(de->d_name);
 523                        /* Ignore overly long pathnames! */
 524                        if (len + baselen + 8 > sizeof(fullname))
 525                                continue;
 526                        memcpy(fullname + baselen, de->d_name, len+1);
 527                        if (simplify_away(fullname, baselen + len, simplify))
 528                                continue;
 529
 530                        exclude = excluded(dir, fullname);
 531                        if (exclude && dir->collect_ignored
 532                            && in_pathspec(fullname, baselen + len, simplify))
 533                                dir_add_ignored(dir, fullname, baselen + len);
 534
 535                        /*
 536                         * Excluded? If we don't explicitly want to show
 537                         * ignored files, ignore it
 538                         */
 539                        if (exclude && !dir->show_ignored)
 540                                continue;
 541
 542                        dtype = get_dtype(de, fullname);
 543
 544                        /*
 545                         * Do we want to see just the ignored files?
 546                         * We still need to recurse into directories,
 547                         * even if we don't ignore them, since the
 548                         * directory may contain files that we do..
 549                         */
 550                        if (!exclude && dir->show_ignored) {
 551                                if (dtype != DT_DIR)
 552                                        continue;
 553                        }
 554
 555                        switch (dtype) {
 556                        default:
 557                                continue;
 558                        case DT_DIR:
 559                                memcpy(fullname + baselen + len, "/", 2);
 560                                len++;
 561                                switch (treat_directory(dir, fullname, baselen + len, simplify)) {
 562                                case show_directory:
 563                                        if (exclude != dir->show_ignored)
 564                                                continue;
 565                                        break;
 566                                case recurse_into_directory:
 567                                        contents += read_directory_recursive(dir,
 568                                                fullname, fullname, baselen + len, 0, simplify);
 569                                        continue;
 570                                case ignore_directory:
 571                                        continue;
 572                                }
 573                                break;
 574                        case DT_REG:
 575                        case DT_LNK:
 576                                break;
 577                        }
 578                        contents++;
 579                        if (check_only)
 580                                goto exit_early;
 581                        else
 582                                dir_add_name(dir, fullname, baselen + len);
 583                }
 584exit_early:
 585                closedir(fdir);
 586
 587                pop_exclude_per_directory(dir, exclude_stk);
 588        }
 589
 590        return contents;
 591}
 592
 593static int cmp_name(const void *p1, const void *p2)
 594{
 595        const struct dir_entry *e1 = *(const struct dir_entry **)p1;
 596        const struct dir_entry *e2 = *(const struct dir_entry **)p2;
 597
 598        return cache_name_compare(e1->name, e1->len,
 599                                  e2->name, e2->len);
 600}
 601
 602/*
 603 * Return the length of the "simple" part of a path match limiter.
 604 */
 605static int simple_length(const char *match)
 606{
 607        const char special[256] = {
 608                [0] = 1, ['?'] = 1,
 609                ['\\'] = 1, ['*'] = 1,
 610                ['['] = 1
 611        };
 612        int len = -1;
 613
 614        for (;;) {
 615                unsigned char c = *match++;
 616                len++;
 617                if (special[c])
 618                        return len;
 619        }
 620}
 621
 622static struct path_simplify *create_simplify(const char **pathspec)
 623{
 624        int nr, alloc = 0;
 625        struct path_simplify *simplify = NULL;
 626
 627        if (!pathspec)
 628                return NULL;
 629
 630        for (nr = 0 ; ; nr++) {
 631                const char *match;
 632                if (nr >= alloc) {
 633                        alloc = alloc_nr(alloc);
 634                        simplify = xrealloc(simplify, alloc * sizeof(*simplify));
 635                }
 636                match = *pathspec++;
 637                if (!match)
 638                        break;
 639                simplify[nr].path = match;
 640                simplify[nr].len = simple_length(match);
 641        }
 642        simplify[nr].path = NULL;
 643        simplify[nr].len = 0;
 644        return simplify;
 645}
 646
 647static void free_simplify(struct path_simplify *simplify)
 648{
 649        if (simplify)
 650                free(simplify);
 651}
 652
 653int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
 654{
 655        struct path_simplify *simplify = create_simplify(pathspec);
 656
 657        /*
 658         * Make sure to do the per-directory exclude for all the
 659         * directories leading up to our base.
 660         */
 661        if (baselen) {
 662                if (dir->exclude_per_dir) {
 663                        char *p, *pp = xmalloc(baselen+1);
 664                        memcpy(pp, base, baselen+1);
 665                        p = pp;
 666                        while (1) {
 667                                char save = *p;
 668                                *p = 0;
 669                                push_exclude_per_directory(dir, pp, p-pp);
 670                                *p++ = save;
 671                                if (!save)
 672                                        break;
 673                                p = strchr(p, '/');
 674                                if (p)
 675                                        p++;
 676                                else
 677                                        p = pp + baselen;
 678                        }
 679                        free(pp);
 680                }
 681        }
 682
 683        read_directory_recursive(dir, path, base, baselen, 0, simplify);
 684        free_simplify(simplify);
 685        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
 686        qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
 687        return dir->nr;
 688}
 689
 690int
 691file_exists(const char *f)
 692{
 693  struct stat sb;
 694  return stat(f, &sb) == 0;
 695}
 696
 697/*
 698 * get_relative_cwd() gets the prefix of the current working directory
 699 * relative to 'dir'.  If we are not inside 'dir', it returns NULL.
 700 *
 701 * As a convenience, it also returns NULL if 'dir' is already NULL.  The
 702 * reason for this behaviour is that it is natural for functions returning
 703 * directory names to return NULL to say "this directory does not exist"
 704 * or "this directory is invalid".  These cases are usually handled the
 705 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
 706 * returns NULL for both of them.
 707 *
 708 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
 709 * unifies the handling of "outside work tree" with "no work tree at all".
 710 */
 711char *get_relative_cwd(char *buffer, int size, const char *dir)
 712{
 713        char *cwd = buffer;
 714
 715        if (!dir)
 716                return NULL;
 717        if (!getcwd(buffer, size))
 718                die("can't find the current directory: %s", strerror(errno));
 719
 720        if (!is_absolute_path(dir))
 721                dir = make_absolute_path(dir);
 722
 723        while (*dir && *dir == *cwd) {
 724                dir++;
 725                cwd++;
 726        }
 727        if (*dir)
 728                return NULL;
 729        if (*cwd == '/')
 730                return cwd + 1;
 731        return cwd;
 732}
 733
 734int is_inside_dir(const char *dir)
 735{
 736        char buffer[PATH_MAX];
 737        return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
 738}
 739
 740int remove_dir_recursively(struct strbuf *path, int only_empty)
 741{
 742        DIR *dir = opendir(path->buf);
 743        struct dirent *e;
 744        int ret = 0, original_len = path->len, len;
 745
 746        if (!dir)
 747                return -1;
 748        if (path->buf[original_len - 1] != '/')
 749                strbuf_addch(path, '/');
 750
 751        len = path->len;
 752        while ((e = readdir(dir)) != NULL) {
 753                struct stat st;
 754                if ((e->d_name[0] == '.') &&
 755                    ((e->d_name[1] == 0) ||
 756                     ((e->d_name[1] == '.') && e->d_name[2] == 0)))
 757                        continue; /* "." and ".." */
 758
 759                strbuf_setlen(path, len);
 760                strbuf_addstr(path, e->d_name);
 761                if (lstat(path->buf, &st))
 762                        ; /* fall thru */
 763                else if (S_ISDIR(st.st_mode)) {
 764                        if (!remove_dir_recursively(path, only_empty))
 765                                continue; /* happy */
 766                } else if (!only_empty && !unlink(path->buf))
 767                        continue; /* happy, too */
 768
 769                /* path too long, stat fails, or non-directory still exists */
 770                ret = -1;
 771                break;
 772        }
 773        closedir(dir);
 774
 775        strbuf_setlen(path, original_len);
 776        if (!ret)
 777                ret = rmdir(path->buf);
 778        return ret;
 779}