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