]> git.ipfire.org Git - thirdparty/git.git/blame - dir.c
t0050: mark non-working test as such
[thirdparty/git.git] / dir.c
CommitLineData
453ec4bd
LT
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 */
453ec4bd
LT
8#include "cache.h"
9#include "dir.h"
09595258 10#include "refs.h"
453ec4bd 11
9fc42d60
LT
12struct path_simplify {
13 int len;
14 const char *path;
15};
16
dba2e203 17static int read_directory_recursive(struct dir_struct *dir, const char *path, int len,
09595258 18 int check_only, const struct path_simplify *simplify);
caa6b782 19static int get_dtype(struct dirent *de, const char *path, int len);
09595258 20
1d8842d9 21static int common_prefix(const char **pathspec)
3c6a370b
LT
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);
c7f34c18 37 if (len >= prefix && !memcmp(path, next, prefix))
3c6a370b 38 continue;
c7f34c18 39 len = prefix - 1;
3c6a370b
LT
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
1d8842d9
LT
54int fill_directory(struct dir_struct *dir, const char **pathspec)
55{
dba2e203
LT
56 const char *path;
57 int len;
1d8842d9
LT
58
59 /*
60 * Calculate common prefix for the pathspec, and
61 * use that to optimize the directory walk
62 */
dba2e203 63 len = common_prefix(pathspec);
1d8842d9 64 path = "";
1d8842d9 65
dba2e203
LT
66 if (len)
67 path = xmemdupz(*pathspec, len);
1d8842d9
LT
68
69 /* Read the directory and prune it */
dba2e203
LT
70 read_directory(dir, path, len, pathspec);
71 return len;
1d8842d9
LT
72}
73
e813d50e 74/*
2c5b0115 75 * Does 'match' match the given name?
e813d50e
JH
76 * A match is found if
77 *
78 * (1) the 'match' string is leading directory of 'name', or
79 * (2) the 'match' string is a wildcard and matches 'name', or
80 * (3) the 'match' string is exactly the same as 'name'.
81 *
82 * and the return value tells which case it was.
83 *
84 * It returns 0 when there is no match.
85 */
3c6a370b
LT
86static int match_one(const char *match, const char *name, int namelen)
87{
88 int matchlen;
89
90 /* If the match was just the prefix, we matched */
88ea8112 91 if (!*match)
e813d50e 92 return MATCHED_RECURSIVELY;
3c6a370b 93
88ea8112
LT
94 for (;;) {
95 unsigned char c1 = *match;
96 unsigned char c2 = *name;
8cc32992 97 if (c1 == '\0' || is_glob_special(c1))
88ea8112
LT
98 break;
99 if (c1 != c2)
100 return 0;
101 match++;
102 name++;
103 namelen--;
104 }
105
106
3c6a370b
LT
107 /*
108 * If we don't match the matchstring exactly,
109 * we need to match by fnmatch
110 */
88ea8112 111 matchlen = strlen(match);
3c6a370b 112 if (strncmp(match, name, matchlen))
e813d50e 113 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
3c6a370b 114
f2d0df71 115 if (namelen == matchlen)
e813d50e
JH
116 return MATCHED_EXACTLY;
117 if (match[matchlen-1] == '/' || name[matchlen] == '/')
118 return MATCHED_RECURSIVELY;
119 return 0;
3c6a370b
LT
120}
121
e813d50e
JH
122/*
123 * Given a name and a list of pathspecs, see if the name matches
124 * any of the pathspecs. The caller is also interested in seeing
125 * all pathspec matches some names it calls this function with
126 * (otherwise the user could have mistyped the unmatched pathspec),
127 * and a mark is left in seen[] array for pathspec element that
128 * actually matched anything.
129 */
0b50922a
CB
130int match_pathspec(const char **pathspec, const char *name, int namelen,
131 int prefix, char *seen)
3c6a370b 132{
0b50922a
CB
133 int i, retval = 0;
134
135 if (!pathspec)
136 return 1;
3c6a370b
LT
137
138 name += prefix;
139 namelen -= prefix;
140
0b50922a 141 for (i = 0; pathspec[i] != NULL; i++) {
e813d50e 142 int how;
0b50922a
CB
143 const char *match = pathspec[i] + prefix;
144 if (seen && seen[i] == MATCHED_EXACTLY)
3c6a370b 145 continue;
e813d50e
JH
146 how = match_one(match, name, namelen);
147 if (how) {
148 if (retval < how)
149 retval = how;
0b50922a
CB
150 if (seen && seen[i] < how)
151 seen[i] = how;
3c6a370b
LT
152 }
153 }
154 return retval;
155}
156
68492fc7
LK
157static int no_wildcard(const char *string)
158{
dd482eea 159 return string[strcspn(string, "*?[{\\")] == '\0';
68492fc7
LK
160}
161
453ec4bd
LT
162void add_exclude(const char *string, const char *base,
163 int baselen, struct exclude_list *which)
164{
d6b8fc30
JH
165 struct exclude *x;
166 size_t len;
167 int to_exclude = 1;
168 int flags = 0;
453ec4bd 169
68492fc7 170 if (*string == '!') {
d6b8fc30 171 to_exclude = 0;
68492fc7
LK
172 string++;
173 }
d6b8fc30
JH
174 len = strlen(string);
175 if (len && string[len - 1] == '/') {
176 char *s;
177 x = xmalloc(sizeof(*x) + len);
4b25d091 178 s = (char *)(x+1);
d6b8fc30
JH
179 memcpy(s, string, len - 1);
180 s[len - 1] = '\0';
181 string = s;
182 x->pattern = s;
183 flags = EXC_FLAG_MUSTBEDIR;
184 } else {
185 x = xmalloc(sizeof(*x));
186 x->pattern = string;
187 }
188 x->to_exclude = to_exclude;
68492fc7 189 x->patternlen = strlen(string);
453ec4bd
LT
190 x->base = base;
191 x->baselen = baselen;
d6b8fc30 192 x->flags = flags;
68492fc7
LK
193 if (!strchr(string, '/'))
194 x->flags |= EXC_FLAG_NODIR;
195 if (no_wildcard(string))
196 x->flags |= EXC_FLAG_NOWILDCARD;
197 if (*string == '*' && no_wildcard(string+1))
198 x->flags |= EXC_FLAG_ENDSWITH;
686a4a06 199 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
453ec4bd
LT
200 which->excludes[which->nr++] = x;
201}
202
203static int add_excludes_from_file_1(const char *fname,
204 const char *base,
205 int baselen,
63d285c8 206 char **buf_p,
453ec4bd
LT
207 struct exclude_list *which)
208{
c470701a 209 struct stat st;
453ec4bd 210 int fd, i;
dc49cd76 211 size_t size;
453ec4bd
LT
212 char *buf, *entry;
213
214 fd = open(fname, O_RDONLY);
c470701a 215 if (fd < 0 || fstat(fd, &st) < 0)
453ec4bd 216 goto err;
dc49cd76 217 size = xsize_t(st.st_size);
453ec4bd
LT
218 if (size == 0) {
219 close(fd);
220 return 0;
221 }
222 buf = xmalloc(size+1);
93d26e4c 223 if (read_in_full(fd, buf, size) != size)
6ba78238
LH
224 {
225 free(buf);
453ec4bd 226 goto err;
6ba78238 227 }
453ec4bd
LT
228 close(fd);
229
63d285c8
JH
230 if (buf_p)
231 *buf_p = buf;
453ec4bd
LT
232 buf[size++] = '\n';
233 entry = buf;
234 for (i = 0; i < size; i++) {
235 if (buf[i] == '\n') {
236 if (entry != buf + i && entry[0] != '#') {
237 buf[i - (i && buf[i-1] == '\r')] = 0;
238 add_exclude(entry, base, baselen, which);
239 }
240 entry = buf + i + 1;
241 }
242 }
243 return 0;
244
245 err:
246 if (0 <= fd)
247 close(fd);
248 return -1;
249}
250
251void add_excludes_from_file(struct dir_struct *dir, const char *fname)
252{
63d285c8 253 if (add_excludes_from_file_1(fname, "", 0, NULL,
453ec4bd
LT
254 &dir->exclude_list[EXC_FILE]) < 0)
255 die("cannot use %s as an exclude file", fname);
256}
257
63d285c8 258static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
453ec4bd 259{
63d285c8
JH
260 struct exclude_list *el;
261 struct exclude_stack *stk = NULL;
262 int current;
263
264 if ((!dir->exclude_per_dir) ||
265 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
266 return; /* too long a path -- ignore */
267
268 /* Pop the ones that are not the prefix of the path being checked. */
269 el = &dir->exclude_list[EXC_DIRS];
270 while ((stk = dir->exclude_stack) != NULL) {
271 if (stk->baselen <= baselen &&
272 !strncmp(dir->basebuf, base, stk->baselen))
273 break;
274 dir->exclude_stack = stk->prev;
275 while (stk->exclude_ix < el->nr)
276 free(el->excludes[--el->nr]);
277 free(stk->filebuf);
278 free(stk);
453ec4bd 279 }
453ec4bd 280
63d285c8
JH
281 /* Read from the parent directories and push them down. */
282 current = stk ? stk->baselen : -1;
283 while (current < baselen) {
284 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
285 const char *cp;
453ec4bd 286
63d285c8
JH
287 if (current < 0) {
288 cp = base;
289 current = 0;
290 }
291 else {
292 cp = strchr(base + current + 1, '/');
293 if (!cp)
294 die("oops in prep_exclude");
295 cp++;
296 }
297 stk->prev = dir->exclude_stack;
298 stk->baselen = cp - base;
299 stk->exclude_ix = el->nr;
300 memcpy(dir->basebuf + current, base + current,
301 stk->baselen - current);
302 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
303 add_excludes_from_file_1(dir->basebuf,
304 dir->basebuf, stk->baselen,
305 &stk->filebuf, el);
306 dir->exclude_stack = stk;
307 current = stk->baselen;
308 }
309 dir->basebuf[baselen] = '\0';
453ec4bd
LT
310}
311
2c5b0115 312/* Scan the list and let the last match determine the fate.
453ec4bd
LT
313 * Return 1 for exclude, 0 for include and -1 for undecided.
314 */
315static int excluded_1(const char *pathname,
6831a88a 316 int pathlen, const char *basename, int *dtype,
453ec4bd
LT
317 struct exclude_list *el)
318{
319 int i;
320
321 if (el->nr) {
322 for (i = el->nr - 1; 0 <= i; i--) {
323 struct exclude *x = el->excludes[i];
324 const char *exclude = x->pattern;
68492fc7 325 int to_exclude = x->to_exclude;
453ec4bd 326
6831a88a
JH
327 if (x->flags & EXC_FLAG_MUSTBEDIR) {
328 if (*dtype == DT_UNKNOWN)
caa6b782 329 *dtype = get_dtype(NULL, pathname, pathlen);
6831a88a
JH
330 if (*dtype != DT_DIR)
331 continue;
332 }
d6b8fc30 333
68492fc7 334 if (x->flags & EXC_FLAG_NODIR) {
453ec4bd 335 /* match basename */
68492fc7
LK
336 if (x->flags & EXC_FLAG_NOWILDCARD) {
337 if (!strcmp(exclude, basename))
338 return to_exclude;
339 } else if (x->flags & EXC_FLAG_ENDSWITH) {
340 if (x->patternlen - 1 <= pathlen &&
341 !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
342 return to_exclude;
343 } else {
344 if (fnmatch(exclude, basename, 0) == 0)
345 return to_exclude;
346 }
453ec4bd
LT
347 }
348 else {
349 /* match with FNM_PATHNAME:
350 * exclude has base (baselen long) implicitly
351 * in front of it.
352 */
353 int baselen = x->baselen;
354 if (*exclude == '/')
355 exclude++;
356
357 if (pathlen < baselen ||
358 (baselen && pathname[baselen-1] != '/') ||
359 strncmp(pathname, x->base, baselen))
360 continue;
361
68492fc7
LK
362 if (x->flags & EXC_FLAG_NOWILDCARD) {
363 if (!strcmp(exclude, pathname + baselen))
364 return to_exclude;
365 } else {
366 if (fnmatch(exclude, pathname+baselen,
367 FNM_PATHNAME) == 0)
368 return to_exclude;
369 }
453ec4bd
LT
370 }
371 }
372 }
373 return -1; /* undecided */
374}
375
6831a88a 376int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
453ec4bd
LT
377{
378 int pathlen = strlen(pathname);
379 int st;
68492fc7
LK
380 const char *basename = strrchr(pathname, '/');
381 basename = (basename) ? basename+1 : pathname;
453ec4bd 382
63d285c8 383 prep_exclude(dir, pathname, basename-pathname);
453ec4bd 384 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
d6b8fc30 385 switch (excluded_1(pathname, pathlen, basename,
6831a88a 386 dtype_p, &dir->exclude_list[st])) {
453ec4bd
LT
387 case 0:
388 return 0;
389 case 1:
390 return 1;
391 }
392 }
393 return 0;
394}
395
f3fa1838
JH
396static struct dir_entry *dir_entry_new(const char *pathname, int len)
397{
453ec4bd
LT
398 struct dir_entry *ent;
399
453ec4bd
LT
400 ent = xmalloc(sizeof(*ent) + len + 1);
401 ent->len = len;
402 memcpy(ent->name, pathname, len);
403 ent->name[len] = 0;
4d06f8ac 404 return ent;
453ec4bd
LT
405}
406
159b3212 407static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
6815e569 408{
0a9b88b7 409 if (cache_name_exists(pathname, len, ignore_case))
6815e569
JK
410 return NULL;
411
25fd2f7a 412 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
6815e569
JK
413 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
414}
415
159b3212 416static struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
2abd31b0 417{
6e4f981f 418 if (!cache_name_is_other(pathname, len))
2abd31b0
JK
419 return NULL;
420
25fd2f7a 421 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
2abd31b0
JK
422 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
423}
424
09595258
LT
425enum exist_status {
426 index_nonexistent = 0,
427 index_directory,
428 index_gitdir,
429};
430
431/*
432 * The index sorts alphabetically by entry name, which
433 * means that a gitlink sorts as '\0' at the end, while
434 * a directory (which is defined not as an entry, but as
435 * the files it contains) will sort with the '/' at the
436 * end.
437 */
438static enum exist_status directory_exists_in_index(const char *dirname, int len)
453ec4bd
LT
439{
440 int pos = cache_name_pos(dirname, len);
09595258
LT
441 if (pos < 0)
442 pos = -pos-1;
443 while (pos < active_nr) {
444 struct cache_entry *ce = active_cache[pos++];
445 unsigned char endchar;
446
447 if (strncmp(ce->name, dirname, len))
448 break;
449 endchar = ce->name[len];
450 if (endchar > '/')
451 break;
452 if (endchar == '/')
453 return index_directory;
7a51ed66 454 if (!endchar && S_ISGITLINK(ce->ce_mode))
09595258
LT
455 return index_gitdir;
456 }
457 return index_nonexistent;
458}
459
460/*
461 * When we find a directory when traversing the filesystem, we
462 * have three distinct cases:
463 *
464 * - ignore it
465 * - see it as a directory
466 * - recurse into it
467 *
468 * and which one we choose depends on a combination of existing
469 * git index contents and the flags passed into the directory
470 * traversal routine.
471 *
472 * Case 1: If we *already* have entries in the index under that
473 * directory name, we always recurse into the directory to see
474 * all the files.
475 *
476 * Case 2: If we *already* have that directory name as a gitlink,
477 * we always continue to see it as a gitlink, regardless of whether
478 * there is an actual git directory there or not (it might not
479 * be checked out as a subproject!)
480 *
481 * Case 3: if we didn't have it in the index previously, we
482 * have a few sub-cases:
483 *
484 * (a) if "show_other_directories" is true, we show it as
485 * just a directory, unless "hide_empty_directories" is
486 * also true and the directory is empty, in which case
487 * we just ignore it entirely.
488 * (b) if it looks like a git directory, and we don't have
302b9282 489 * 'no_gitlinks' set we treat it as a gitlink, and show it
09595258
LT
490 * as a directory.
491 * (c) otherwise, we recurse into it.
492 */
493enum directory_treatment {
494 show_directory,
495 ignore_directory,
496 recurse_into_directory,
497};
498
499static enum directory_treatment treat_directory(struct dir_struct *dir,
500 const char *dirname, int len,
501 const struct path_simplify *simplify)
502{
503 /* The "len-1" is to strip the final '/' */
504 switch (directory_exists_in_index(dirname, len-1)) {
505 case index_directory:
506 return recurse_into_directory;
507
508 case index_gitdir:
7c4c97c0 509 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
ab22aed3 510 return ignore_directory;
09595258
LT
511 return show_directory;
512
513 case index_nonexistent:
7c4c97c0 514 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
09595258 515 break;
7c4c97c0 516 if (!(dir->flags & DIR_NO_GITLINKS)) {
09595258
LT
517 unsigned char sha1[20];
518 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
519 return show_directory;
520 }
521 return recurse_into_directory;
522 }
523
524 /* This is the "show_other_directories" case */
7c4c97c0 525 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
09595258 526 return show_directory;
dba2e203 527 if (!read_directory_recursive(dir, dirname, len, 1, simplify))
09595258
LT
528 return ignore_directory;
529 return show_directory;
453ec4bd
LT
530}
531
9fc42d60
LT
532/*
533 * This is an inexact early pruning of any recursive directory
534 * reading - if the path cannot possibly be in the pathspec,
535 * return true, and we'll skip it early.
536 */
537static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
538{
539 if (simplify) {
540 for (;;) {
541 const char *match = simplify->path;
542 int len = simplify->len;
543
544 if (!match)
545 break;
546 if (len > pathlen)
547 len = pathlen;
548 if (!memcmp(path, match, len))
549 return 0;
550 simplify++;
551 }
552 return 1;
553 }
554 return 0;
555}
556
e96980ef
JK
557static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
558{
559 if (simplify) {
560 for (; simplify->path; simplify++) {
561 if (len == simplify->len
562 && !memcmp(path, simplify->path, len))
563 return 1;
564 }
565 }
566 return 0;
567}
568
443e061a
LT
569static int get_index_dtype(const char *path, int len)
570{
571 int pos;
572 struct cache_entry *ce;
573
574 ce = cache_name_exists(path, len, 0);
575 if (ce) {
576 if (!ce_uptodate(ce))
577 return DT_UNKNOWN;
578 if (S_ISGITLINK(ce->ce_mode))
579 return DT_DIR;
580 /*
581 * Nobody actually cares about the
582 * difference between DT_LNK and DT_REG
583 */
584 return DT_REG;
585 }
586
587 /* Try to look it up as a directory */
588 pos = cache_name_pos(path, len);
589 if (pos >= 0)
590 return DT_UNKNOWN;
591 pos = -pos-1;
592 while (pos < active_nr) {
593 ce = active_cache[pos++];
594 if (strncmp(ce->name, path, len))
595 break;
596 if (ce->name[len] > '/')
597 break;
598 if (ce->name[len] < '/')
599 continue;
600 if (!ce_uptodate(ce))
601 break; /* continue? */
602 return DT_DIR;
603 }
604 return DT_UNKNOWN;
605}
606
caa6b782 607static int get_dtype(struct dirent *de, const char *path, int len)
07134421 608{
6831a88a 609 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
07134421
LT
610 struct stat st;
611
612 if (dtype != DT_UNKNOWN)
613 return dtype;
443e061a
LT
614 dtype = get_index_dtype(path, len);
615 if (dtype != DT_UNKNOWN)
616 return dtype;
617 if (lstat(path, &st))
07134421
LT
618 return dtype;
619 if (S_ISREG(st.st_mode))
620 return DT_REG;
621 if (S_ISDIR(st.st_mode))
622 return DT_DIR;
623 if (S_ISLNK(st.st_mode))
624 return DT_LNK;
625 return dtype;
626}
627
53cc5356
JH
628enum path_treatment {
629 path_ignored,
630 path_handled,
631 path_recurse,
632};
633
16e2cfa9
JH
634static enum path_treatment treat_one_path(struct dir_struct *dir,
635 char *path, int *len,
636 const struct path_simplify *simplify,
637 int dtype, struct dirent *de)
53cc5356 638{
16e2cfa9 639 int exclude = excluded(dir, path, &dtype);
53cc5356
JH
640 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
641 && in_pathspec(path, *len, simplify))
642 dir_add_ignored(dir, path, *len);
643
644 /*
645 * Excluded? If we don't explicitly want to show
646 * ignored files, ignore it
647 */
648 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
649 return path_ignored;
650
651 if (dtype == DT_UNKNOWN)
652 dtype = get_dtype(de, path, *len);
653
654 /*
655 * Do we want to see just the ignored files?
656 * We still need to recurse into directories,
657 * even if we don't ignore them, since the
658 * directory may contain files that we do..
659 */
660 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
661 if (dtype != DT_DIR)
662 return path_ignored;
663 }
664
665 switch (dtype) {
666 default:
667 return path_ignored;
668 case DT_DIR:
669 memcpy(path + *len, "/", 2);
670 (*len)++;
671 switch (treat_directory(dir, path, *len, simplify)) {
672 case show_directory:
673 if (exclude != !!(dir->flags
674 & DIR_SHOW_IGNORED))
675 return path_ignored;
676 break;
677 case recurse_into_directory:
678 return path_recurse;
679 case ignore_directory:
680 return path_ignored;
681 }
682 break;
683 case DT_REG:
684 case DT_LNK:
685 break;
686 }
687 return path_handled;
688}
689
16e2cfa9
JH
690static enum path_treatment treat_path(struct dir_struct *dir,
691 struct dirent *de,
692 char *path, int path_max,
693 int baselen,
694 const struct path_simplify *simplify,
695 int *len)
696{
697 int dtype;
698
699 if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
700 return path_ignored;
701 *len = strlen(de->d_name);
702 /* Ignore overly long pathnames! */
703 if (*len + baselen + 8 > path_max)
704 return path_ignored;
705 memcpy(path + baselen, de->d_name, *len + 1);
706 *len += baselen;
707 if (simplify_away(path, *len, simplify))
708 return path_ignored;
709
710 dtype = DTYPE(de);
711 return treat_one_path(dir, path, len, simplify, dtype, de);
712}
713
453ec4bd
LT
714/*
715 * Read a directory tree. We currently ignore anything but
716 * directories, regular files and symlinks. That's because git
717 * doesn't handle them at all yet. Maybe that will change some
718 * day.
719 *
720 * Also, we ignore the name ".git" (even if it is not a directory).
721 * That likely will not change.
722 */
53cc5356
JH
723static int read_directory_recursive(struct dir_struct *dir,
724 const char *base, int baselen,
725 int check_only,
726 const struct path_simplify *simplify)
453ec4bd 727{
dba2e203 728 DIR *fdir = opendir(*base ? base : ".");
453ec4bd
LT
729 int contents = 0;
730
731 if (fdir) {
453ec4bd 732 struct dirent *de;
dba2e203
LT
733 char path[PATH_MAX + 1];
734 memcpy(path, base, baselen);
453ec4bd 735
453ec4bd 736 while ((de = readdir(fdir)) != NULL) {
53cc5356
JH
737 int len;
738 switch (treat_path(dir, de, path, sizeof(path),
739 baselen, simplify, &len)) {
740 case path_recurse:
741 contents += read_directory_recursive
742 (dir, path, len, 0, simplify);
5d5cea67 743 continue;
53cc5356 744 case path_ignored:
9fc42d60 745 continue;
53cc5356 746 case path_handled:
453ec4bd
LT
747 break;
748 }
453ec4bd 749 contents++;
07ccbff8
JS
750 if (check_only)
751 goto exit_early;
752 else
dba2e203 753 dir_add_name(dir, path, len);
453ec4bd 754 }
07ccbff8 755exit_early:
453ec4bd 756 closedir(fdir);
453ec4bd
LT
757 }
758
759 return contents;
760}
761
762static int cmp_name(const void *p1, const void *p2)
763{
764 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
765 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
766
767 return cache_name_compare(e1->name, e1->len,
768 e2->name, e2->len);
769}
770
9fc42d60
LT
771/*
772 * Return the length of the "simple" part of a path match limiter.
773 */
774static int simple_length(const char *match)
453ec4bd 775{
9fc42d60
LT
776 int len = -1;
777
778 for (;;) {
779 unsigned char c = *match++;
780 len++;
8cc32992 781 if (c == '\0' || is_glob_special(c))
9fc42d60
LT
782 return len;
783 }
784}
785
786static struct path_simplify *create_simplify(const char **pathspec)
787{
788 int nr, alloc = 0;
789 struct path_simplify *simplify = NULL;
790
791 if (!pathspec)
792 return NULL;
793
794 for (nr = 0 ; ; nr++) {
795 const char *match;
796 if (nr >= alloc) {
797 alloc = alloc_nr(alloc);
798 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
799 }
800 match = *pathspec++;
801 if (!match)
802 break;
803 simplify[nr].path = match;
804 simplify[nr].len = simple_length(match);
805 }
806 simplify[nr].path = NULL;
807 simplify[nr].len = 0;
808 return simplify;
809}
810
811static void free_simplify(struct path_simplify *simplify)
812{
8e0f7003 813 free(simplify);
9fc42d60
LT
814}
815
48ffef96
JH
816static int treat_leading_path(struct dir_struct *dir,
817 const char *path, int len,
818 const struct path_simplify *simplify)
819{
820 char pathbuf[PATH_MAX];
821 int baselen, blen;
822 const char *cp;
823
824 while (len && path[len - 1] == '/')
825 len--;
826 if (!len)
827 return 1;
828 baselen = 0;
829 while (1) {
830 cp = path + baselen + !!baselen;
831 cp = memchr(cp, '/', path + len - cp);
832 if (!cp)
833 baselen = len;
834 else
835 baselen = cp - path;
836 memcpy(pathbuf, path, baselen);
837 pathbuf[baselen] = '\0';
838 if (!is_directory(pathbuf))
839 return 0;
840 if (simplify_away(pathbuf, baselen, simplify))
841 return 0;
842 blen = baselen;
843 if (treat_one_path(dir, pathbuf, &blen, simplify,
844 DT_DIR, NULL) == path_ignored)
845 return 0; /* do not recurse into it */
846 if (len <= baselen)
847 return 1; /* finished checking */
848 }
849}
850
dba2e203 851int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
9fc42d60 852{
725b0605 853 struct path_simplify *simplify;
b4189aa8 854
dba2e203 855 if (has_symlink_leading_path(path, len))
725b0605
JH
856 return dir->nr;
857
858 simplify = create_simplify(pathspec);
48ffef96
JH
859 if (!len || treat_leading_path(dir, path, len, simplify))
860 read_directory_recursive(dir, path, len, 0, simplify);
9fc42d60 861 free_simplify(simplify);
453ec4bd 862 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 863 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
864 return dir->nr;
865}
c91f0d92 866
686a4a06 867int file_exists(const char *f)
c91f0d92 868{
686a4a06 869 struct stat sb;
a50f9fc5 870 return lstat(f, &sb) == 0;
c91f0d92 871}
e6636747
JS
872
873/*
874 * get_relative_cwd() gets the prefix of the current working directory
875 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
420acb31
JS
876 *
877 * As a convenience, it also returns NULL if 'dir' is already NULL. The
878 * reason for this behaviour is that it is natural for functions returning
879 * directory names to return NULL to say "this directory does not exist"
880 * or "this directory is invalid". These cases are usually handled the
881 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
882 * returns NULL for both of them.
883 *
884 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
885 * unifies the handling of "outside work tree" with "no work tree at all".
e6636747
JS
886 */
887char *get_relative_cwd(char *buffer, int size, const char *dir)
888{
889 char *cwd = buffer;
890
e6636747
JS
891 if (!dir)
892 return NULL;
893 if (!getcwd(buffer, size))
d824cbba 894 die_errno("can't find the current directory");
e6636747
JS
895
896 if (!is_absolute_path(dir))
897 dir = make_absolute_path(dir);
898
899 while (*dir && *dir == *cwd) {
900 dir++;
901 cwd++;
902 }
903 if (*dir)
904 return NULL;
905 if (*cwd == '/')
906 return cwd + 1;
907 return cwd;
908}
909
910int is_inside_dir(const char *dir)
911{
912 char buffer[PATH_MAX];
913 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
914}
7155b727 915
55892d23
AP
916int is_empty_dir(const char *path)
917{
918 DIR *dir = opendir(path);
919 struct dirent *e;
920 int ret = 1;
921
922 if (!dir)
923 return 0;
924
925 while ((e = readdir(dir)) != NULL)
926 if (!is_dot_or_dotdot(e->d_name)) {
927 ret = 0;
928 break;
929 }
930
931 closedir(dir);
932 return ret;
933}
934
a0f4afbe 935int remove_dir_recursively(struct strbuf *path, int flag)
7155b727 936{
a0f4afbe 937 DIR *dir;
7155b727
JS
938 struct dirent *e;
939 int ret = 0, original_len = path->len, len;
a0f4afbe
JH
940 int only_empty = (flag & REMOVE_DIR_EMPTY_ONLY);
941 unsigned char submodule_head[20];
7155b727 942
a0f4afbe
JH
943 if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) &&
944 !resolve_gitlink_ref(path->buf, "HEAD", submodule_head))
945 /* Do not descend and nuke a nested git work tree. */
946 return 0;
947
948 dir = opendir(path->buf);
7155b727
JS
949 if (!dir)
950 return -1;
951 if (path->buf[original_len - 1] != '/')
952 strbuf_addch(path, '/');
953
954 len = path->len;
955 while ((e = readdir(dir)) != NULL) {
956 struct stat st;
8ca12c0d
AP
957 if (is_dot_or_dotdot(e->d_name))
958 continue;
7155b727
JS
959
960 strbuf_setlen(path, len);
961 strbuf_addstr(path, e->d_name);
962 if (lstat(path->buf, &st))
963 ; /* fall thru */
964 else if (S_ISDIR(st.st_mode)) {
965 if (!remove_dir_recursively(path, only_empty))
966 continue; /* happy */
967 } else if (!only_empty && !unlink(path->buf))
968 continue; /* happy, too */
969
970 /* path too long, stat fails, or non-directory still exists */
971 ret = -1;
972 break;
973 }
974 closedir(dir);
975
976 strbuf_setlen(path, original_len);
977 if (!ret)
978 ret = rmdir(path->buf);
979 return ret;
980}
039bc64e
JH
981
982void setup_standard_excludes(struct dir_struct *dir)
983{
984 const char *path;
985
986 dir->exclude_per_dir = ".gitignore";
987 path = git_path("info/exclude");
988 if (!access(path, R_OK))
989 add_excludes_from_file(dir, path);
990 if (excludes_file && !access(excludes_file, R_OK))
991 add_excludes_from_file(dir, excludes_file);
992}
4a92d1bf
AR
993
994int remove_path(const char *name)
995{
996 char *slash;
997
998 if (unlink(name) && errno != ENOENT)
999 return -1;
1000
1001 slash = strrchr(name, '/');
1002 if (slash) {
1003 char *dirs = xstrdup(name);
1004 slash = dirs + (slash - name);
1005 do {
1006 *slash = '\0';
1007 } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
1008 free(dirs);
1009 }
1010 return 0;
1011}
1012