]> git.ipfire.org Git - thirdparty/git.git/blame - dir.c
Avoid writing to buffer in add_excludes_from_file_1()
[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 232 entry = buf;
b5041c5f
NTND
233 for (i = 0; i <= size; i++) {
234 if (i == size || buf[i] == '\n') {
453ec4bd
LT
235 if (entry != buf + i && entry[0] != '#') {
236 buf[i - (i && buf[i-1] == '\r')] = 0;
237 add_exclude(entry, base, baselen, which);
238 }
239 entry = buf + i + 1;
240 }
241 }
242 return 0;
243
244 err:
245 if (0 <= fd)
246 close(fd);
247 return -1;
248}
249
250void add_excludes_from_file(struct dir_struct *dir, const char *fname)
251{
63d285c8 252 if (add_excludes_from_file_1(fname, "", 0, NULL,
453ec4bd
LT
253 &dir->exclude_list[EXC_FILE]) < 0)
254 die("cannot use %s as an exclude file", fname);
255}
256
63d285c8 257static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
453ec4bd 258{
63d285c8
JH
259 struct exclude_list *el;
260 struct exclude_stack *stk = NULL;
261 int current;
262
263 if ((!dir->exclude_per_dir) ||
264 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
265 return; /* too long a path -- ignore */
266
267 /* Pop the ones that are not the prefix of the path being checked. */
268 el = &dir->exclude_list[EXC_DIRS];
269 while ((stk = dir->exclude_stack) != NULL) {
270 if (stk->baselen <= baselen &&
271 !strncmp(dir->basebuf, base, stk->baselen))
272 break;
273 dir->exclude_stack = stk->prev;
274 while (stk->exclude_ix < el->nr)
275 free(el->excludes[--el->nr]);
276 free(stk->filebuf);
277 free(stk);
453ec4bd 278 }
453ec4bd 279
63d285c8
JH
280 /* Read from the parent directories and push them down. */
281 current = stk ? stk->baselen : -1;
282 while (current < baselen) {
283 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
284 const char *cp;
453ec4bd 285
63d285c8
JH
286 if (current < 0) {
287 cp = base;
288 current = 0;
289 }
290 else {
291 cp = strchr(base + current + 1, '/');
292 if (!cp)
293 die("oops in prep_exclude");
294 cp++;
295 }
296 stk->prev = dir->exclude_stack;
297 stk->baselen = cp - base;
298 stk->exclude_ix = el->nr;
299 memcpy(dir->basebuf + current, base + current,
300 stk->baselen - current);
301 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
302 add_excludes_from_file_1(dir->basebuf,
303 dir->basebuf, stk->baselen,
304 &stk->filebuf, el);
305 dir->exclude_stack = stk;
306 current = stk->baselen;
307 }
308 dir->basebuf[baselen] = '\0';
453ec4bd
LT
309}
310
2c5b0115 311/* Scan the list and let the last match determine the fate.
453ec4bd
LT
312 * Return 1 for exclude, 0 for include and -1 for undecided.
313 */
314static int excluded_1(const char *pathname,
6831a88a 315 int pathlen, const char *basename, int *dtype,
453ec4bd
LT
316 struct exclude_list *el)
317{
318 int i;
319
320 if (el->nr) {
321 for (i = el->nr - 1; 0 <= i; i--) {
322 struct exclude *x = el->excludes[i];
323 const char *exclude = x->pattern;
68492fc7 324 int to_exclude = x->to_exclude;
453ec4bd 325
6831a88a
JH
326 if (x->flags & EXC_FLAG_MUSTBEDIR) {
327 if (*dtype == DT_UNKNOWN)
caa6b782 328 *dtype = get_dtype(NULL, pathname, pathlen);
6831a88a
JH
329 if (*dtype != DT_DIR)
330 continue;
331 }
d6b8fc30 332
68492fc7 333 if (x->flags & EXC_FLAG_NODIR) {
453ec4bd 334 /* match basename */
68492fc7
LK
335 if (x->flags & EXC_FLAG_NOWILDCARD) {
336 if (!strcmp(exclude, basename))
337 return to_exclude;
338 } else if (x->flags & EXC_FLAG_ENDSWITH) {
339 if (x->patternlen - 1 <= pathlen &&
340 !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
341 return to_exclude;
342 } else {
343 if (fnmatch(exclude, basename, 0) == 0)
344 return to_exclude;
345 }
453ec4bd
LT
346 }
347 else {
348 /* match with FNM_PATHNAME:
349 * exclude has base (baselen long) implicitly
350 * in front of it.
351 */
352 int baselen = x->baselen;
353 if (*exclude == '/')
354 exclude++;
355
356 if (pathlen < baselen ||
357 (baselen && pathname[baselen-1] != '/') ||
358 strncmp(pathname, x->base, baselen))
359 continue;
360
68492fc7
LK
361 if (x->flags & EXC_FLAG_NOWILDCARD) {
362 if (!strcmp(exclude, pathname + baselen))
363 return to_exclude;
364 } else {
365 if (fnmatch(exclude, pathname+baselen,
366 FNM_PATHNAME) == 0)
367 return to_exclude;
368 }
453ec4bd
LT
369 }
370 }
371 }
372 return -1; /* undecided */
373}
374
6831a88a 375int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
453ec4bd
LT
376{
377 int pathlen = strlen(pathname);
378 int st;
68492fc7
LK
379 const char *basename = strrchr(pathname, '/');
380 basename = (basename) ? basename+1 : pathname;
453ec4bd 381
63d285c8 382 prep_exclude(dir, pathname, basename-pathname);
453ec4bd 383 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
d6b8fc30 384 switch (excluded_1(pathname, pathlen, basename,
6831a88a 385 dtype_p, &dir->exclude_list[st])) {
453ec4bd
LT
386 case 0:
387 return 0;
388 case 1:
389 return 1;
390 }
391 }
392 return 0;
393}
394
f3fa1838
JH
395static struct dir_entry *dir_entry_new(const char *pathname, int len)
396{
453ec4bd
LT
397 struct dir_entry *ent;
398
453ec4bd
LT
399 ent = xmalloc(sizeof(*ent) + len + 1);
400 ent->len = len;
401 memcpy(ent->name, pathname, len);
402 ent->name[len] = 0;
4d06f8ac 403 return ent;
453ec4bd
LT
404}
405
159b3212 406static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
6815e569 407{
0a9b88b7 408 if (cache_name_exists(pathname, len, ignore_case))
6815e569
JK
409 return NULL;
410
25fd2f7a 411 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
6815e569
JK
412 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
413}
414
159b3212 415static struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
2abd31b0 416{
6e4f981f 417 if (!cache_name_is_other(pathname, len))
2abd31b0
JK
418 return NULL;
419
25fd2f7a 420 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
2abd31b0
JK
421 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
422}
423
09595258
LT
424enum exist_status {
425 index_nonexistent = 0,
426 index_directory,
427 index_gitdir,
428};
429
430/*
431 * The index sorts alphabetically by entry name, which
432 * means that a gitlink sorts as '\0' at the end, while
433 * a directory (which is defined not as an entry, but as
434 * the files it contains) will sort with the '/' at the
435 * end.
436 */
437static enum exist_status directory_exists_in_index(const char *dirname, int len)
453ec4bd
LT
438{
439 int pos = cache_name_pos(dirname, len);
09595258
LT
440 if (pos < 0)
441 pos = -pos-1;
442 while (pos < active_nr) {
443 struct cache_entry *ce = active_cache[pos++];
444 unsigned char endchar;
445
446 if (strncmp(ce->name, dirname, len))
447 break;
448 endchar = ce->name[len];
449 if (endchar > '/')
450 break;
451 if (endchar == '/')
452 return index_directory;
7a51ed66 453 if (!endchar && S_ISGITLINK(ce->ce_mode))
09595258
LT
454 return index_gitdir;
455 }
456 return index_nonexistent;
457}
458
459/*
460 * When we find a directory when traversing the filesystem, we
461 * have three distinct cases:
462 *
463 * - ignore it
464 * - see it as a directory
465 * - recurse into it
466 *
467 * and which one we choose depends on a combination of existing
468 * git index contents and the flags passed into the directory
469 * traversal routine.
470 *
471 * Case 1: If we *already* have entries in the index under that
472 * directory name, we always recurse into the directory to see
473 * all the files.
474 *
475 * Case 2: If we *already* have that directory name as a gitlink,
476 * we always continue to see it as a gitlink, regardless of whether
477 * there is an actual git directory there or not (it might not
478 * be checked out as a subproject!)
479 *
480 * Case 3: if we didn't have it in the index previously, we
481 * have a few sub-cases:
482 *
483 * (a) if "show_other_directories" is true, we show it as
484 * just a directory, unless "hide_empty_directories" is
485 * also true and the directory is empty, in which case
486 * we just ignore it entirely.
487 * (b) if it looks like a git directory, and we don't have
302b9282 488 * 'no_gitlinks' set we treat it as a gitlink, and show it
09595258
LT
489 * as a directory.
490 * (c) otherwise, we recurse into it.
491 */
492enum directory_treatment {
493 show_directory,
494 ignore_directory,
495 recurse_into_directory,
496};
497
498static enum directory_treatment treat_directory(struct dir_struct *dir,
499 const char *dirname, int len,
500 const struct path_simplify *simplify)
501{
502 /* The "len-1" is to strip the final '/' */
503 switch (directory_exists_in_index(dirname, len-1)) {
504 case index_directory:
505 return recurse_into_directory;
506
507 case index_gitdir:
7c4c97c0 508 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
ab22aed3 509 return ignore_directory;
09595258
LT
510 return show_directory;
511
512 case index_nonexistent:
7c4c97c0 513 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
09595258 514 break;
7c4c97c0 515 if (!(dir->flags & DIR_NO_GITLINKS)) {
09595258
LT
516 unsigned char sha1[20];
517 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
518 return show_directory;
519 }
520 return recurse_into_directory;
521 }
522
523 /* This is the "show_other_directories" case */
7c4c97c0 524 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
09595258 525 return show_directory;
dba2e203 526 if (!read_directory_recursive(dir, dirname, len, 1, simplify))
09595258
LT
527 return ignore_directory;
528 return show_directory;
453ec4bd
LT
529}
530
9fc42d60
LT
531/*
532 * This is an inexact early pruning of any recursive directory
533 * reading - if the path cannot possibly be in the pathspec,
534 * return true, and we'll skip it early.
535 */
536static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
537{
538 if (simplify) {
539 for (;;) {
540 const char *match = simplify->path;
541 int len = simplify->len;
542
543 if (!match)
544 break;
545 if (len > pathlen)
546 len = pathlen;
547 if (!memcmp(path, match, len))
548 return 0;
549 simplify++;
550 }
551 return 1;
552 }
553 return 0;
554}
555
e96980ef
JK
556static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
557{
558 if (simplify) {
559 for (; simplify->path; simplify++) {
560 if (len == simplify->len
561 && !memcmp(path, simplify->path, len))
562 return 1;
563 }
564 }
565 return 0;
566}
567
443e061a
LT
568static int get_index_dtype(const char *path, int len)
569{
570 int pos;
571 struct cache_entry *ce;
572
573 ce = cache_name_exists(path, len, 0);
574 if (ce) {
575 if (!ce_uptodate(ce))
576 return DT_UNKNOWN;
577 if (S_ISGITLINK(ce->ce_mode))
578 return DT_DIR;
579 /*
580 * Nobody actually cares about the
581 * difference between DT_LNK and DT_REG
582 */
583 return DT_REG;
584 }
585
586 /* Try to look it up as a directory */
587 pos = cache_name_pos(path, len);
588 if (pos >= 0)
589 return DT_UNKNOWN;
590 pos = -pos-1;
591 while (pos < active_nr) {
592 ce = active_cache[pos++];
593 if (strncmp(ce->name, path, len))
594 break;
595 if (ce->name[len] > '/')
596 break;
597 if (ce->name[len] < '/')
598 continue;
599 if (!ce_uptodate(ce))
600 break; /* continue? */
601 return DT_DIR;
602 }
603 return DT_UNKNOWN;
604}
605
caa6b782 606static int get_dtype(struct dirent *de, const char *path, int len)
07134421 607{
6831a88a 608 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
07134421
LT
609 struct stat st;
610
611 if (dtype != DT_UNKNOWN)
612 return dtype;
443e061a
LT
613 dtype = get_index_dtype(path, len);
614 if (dtype != DT_UNKNOWN)
615 return dtype;
616 if (lstat(path, &st))
07134421
LT
617 return dtype;
618 if (S_ISREG(st.st_mode))
619 return DT_REG;
620 if (S_ISDIR(st.st_mode))
621 return DT_DIR;
622 if (S_ISLNK(st.st_mode))
623 return DT_LNK;
624 return dtype;
625}
626
453ec4bd
LT
627/*
628 * Read a directory tree. We currently ignore anything but
629 * directories, regular files and symlinks. That's because git
630 * doesn't handle them at all yet. Maybe that will change some
631 * day.
632 *
633 * Also, we ignore the name ".git" (even if it is not a directory).
634 * That likely will not change.
635 */
dba2e203 636static int read_directory_recursive(struct dir_struct *dir, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
453ec4bd 637{
dba2e203 638 DIR *fdir = opendir(*base ? base : ".");
453ec4bd
LT
639 int contents = 0;
640
641 if (fdir) {
453ec4bd 642 struct dirent *de;
dba2e203
LT
643 char path[PATH_MAX + 1];
644 memcpy(path, base, baselen);
453ec4bd 645
453ec4bd 646 while ((de = readdir(fdir)) != NULL) {
07134421 647 int len, dtype;
b9916256 648 int exclude;
453ec4bd 649
8ca12c0d
AP
650 if (is_dot_or_dotdot(de->d_name) ||
651 !strcmp(de->d_name, ".git"))
453ec4bd
LT
652 continue;
653 len = strlen(de->d_name);
5d5cea67 654 /* Ignore overly long pathnames! */
dba2e203 655 if (len + baselen + 8 > sizeof(path))
5d5cea67 656 continue;
dba2e203
LT
657 memcpy(path + baselen, de->d_name, len+1);
658 len = baselen + len;
659 if (simplify_away(path, len, simplify))
9fc42d60 660 continue;
b9916256 661
6831a88a 662 dtype = DTYPE(de);
dba2e203 663 exclude = excluded(dir, path, &dtype);
7c4c97c0 664 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
dba2e203
LT
665 && in_pathspec(path, len, simplify))
666 dir_add_ignored(dir, path,len);
07134421
LT
667
668 /*
669 * Excluded? If we don't explicitly want to show
670 * ignored files, ignore it
671 */
7c4c97c0 672 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
07134421
LT
673 continue;
674
6831a88a 675 if (dtype == DT_UNKNOWN)
caa6b782 676 dtype = get_dtype(de, path, len);
07134421
LT
677
678 /*
679 * Do we want to see just the ignored files?
680 * We still need to recurse into directories,
681 * even if we don't ignore them, since the
682 * directory may contain files that we do..
683 */
7c4c97c0 684 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
07134421 685 if (dtype != DT_DIR)
c889763b 686 continue;
c889763b 687 }
453ec4bd 688
07134421 689 switch (dtype) {
453ec4bd
LT
690 default:
691 continue;
453ec4bd 692 case DT_DIR:
dba2e203 693 memcpy(path + len, "/", 2);
453ec4bd 694 len++;
dba2e203 695 switch (treat_directory(dir, path, len, simplify)) {
09595258 696 case show_directory:
7c4c97c0
JS
697 if (exclude != !!(dir->flags
698 & DIR_SHOW_IGNORED))
b9916256 699 continue;
453ec4bd 700 break;
09595258
LT
701 case recurse_into_directory:
702 contents += read_directory_recursive(dir,
dba2e203 703 path, len, 0, simplify);
09595258
LT
704 continue;
705 case ignore_directory:
706 continue;
453ec4bd 707 }
09595258 708 break;
453ec4bd
LT
709 case DT_REG:
710 case DT_LNK:
711 break;
712 }
453ec4bd 713 contents++;
07ccbff8
JS
714 if (check_only)
715 goto exit_early;
716 else
dba2e203 717 dir_add_name(dir, path, len);
453ec4bd 718 }
07ccbff8 719exit_early:
453ec4bd 720 closedir(fdir);
453ec4bd
LT
721 }
722
723 return contents;
724}
725
726static int cmp_name(const void *p1, const void *p2)
727{
728 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
729 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
730
731 return cache_name_compare(e1->name, e1->len,
732 e2->name, e2->len);
733}
734
9fc42d60
LT
735/*
736 * Return the length of the "simple" part of a path match limiter.
737 */
738static int simple_length(const char *match)
453ec4bd 739{
9fc42d60
LT
740 int len = -1;
741
742 for (;;) {
743 unsigned char c = *match++;
744 len++;
8cc32992 745 if (c == '\0' || is_glob_special(c))
9fc42d60
LT
746 return len;
747 }
748}
749
750static struct path_simplify *create_simplify(const char **pathspec)
751{
752 int nr, alloc = 0;
753 struct path_simplify *simplify = NULL;
754
755 if (!pathspec)
756 return NULL;
757
758 for (nr = 0 ; ; nr++) {
759 const char *match;
760 if (nr >= alloc) {
761 alloc = alloc_nr(alloc);
762 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
763 }
764 match = *pathspec++;
765 if (!match)
766 break;
767 simplify[nr].path = match;
768 simplify[nr].len = simple_length(match);
769 }
770 simplify[nr].path = NULL;
771 simplify[nr].len = 0;
772 return simplify;
773}
774
775static void free_simplify(struct path_simplify *simplify)
776{
8e0f7003 777 free(simplify);
9fc42d60
LT
778}
779
dba2e203 780int read_directory(struct dir_struct *dir, const char *path, int len, const char **pathspec)
9fc42d60 781{
725b0605 782 struct path_simplify *simplify;
b4189aa8 783
dba2e203 784 if (has_symlink_leading_path(path, len))
725b0605
JH
785 return dir->nr;
786
787 simplify = create_simplify(pathspec);
dba2e203 788 read_directory_recursive(dir, path, len, 0, simplify);
9fc42d60 789 free_simplify(simplify);
453ec4bd 790 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2abd31b0 791 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
453ec4bd
LT
792 return dir->nr;
793}
c91f0d92 794
686a4a06 795int file_exists(const char *f)
c91f0d92 796{
686a4a06 797 struct stat sb;
a50f9fc5 798 return lstat(f, &sb) == 0;
c91f0d92 799}
e6636747
JS
800
801/*
802 * get_relative_cwd() gets the prefix of the current working directory
803 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
420acb31
JS
804 *
805 * As a convenience, it also returns NULL if 'dir' is already NULL. The
806 * reason for this behaviour is that it is natural for functions returning
807 * directory names to return NULL to say "this directory does not exist"
808 * or "this directory is invalid". These cases are usually handled the
809 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
810 * returns NULL for both of them.
811 *
812 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
813 * unifies the handling of "outside work tree" with "no work tree at all".
e6636747
JS
814 */
815char *get_relative_cwd(char *buffer, int size, const char *dir)
816{
817 char *cwd = buffer;
818
e6636747
JS
819 if (!dir)
820 return NULL;
821 if (!getcwd(buffer, size))
d824cbba 822 die_errno("can't find the current directory");
e6636747
JS
823
824 if (!is_absolute_path(dir))
825 dir = make_absolute_path(dir);
826
827 while (*dir && *dir == *cwd) {
828 dir++;
829 cwd++;
830 }
831 if (*dir)
832 return NULL;
833 if (*cwd == '/')
834 return cwd + 1;
835 return cwd;
836}
837
838int is_inside_dir(const char *dir)
839{
840 char buffer[PATH_MAX];
841 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
842}
7155b727 843
55892d23
AP
844int is_empty_dir(const char *path)
845{
846 DIR *dir = opendir(path);
847 struct dirent *e;
848 int ret = 1;
849
850 if (!dir)
851 return 0;
852
853 while ((e = readdir(dir)) != NULL)
854 if (!is_dot_or_dotdot(e->d_name)) {
855 ret = 0;
856 break;
857 }
858
859 closedir(dir);
860 return ret;
861}
862
7155b727
JS
863int remove_dir_recursively(struct strbuf *path, int only_empty)
864{
865 DIR *dir = opendir(path->buf);
866 struct dirent *e;
867 int ret = 0, original_len = path->len, len;
868
869 if (!dir)
870 return -1;
871 if (path->buf[original_len - 1] != '/')
872 strbuf_addch(path, '/');
873
874 len = path->len;
875 while ((e = readdir(dir)) != NULL) {
876 struct stat st;
8ca12c0d
AP
877 if (is_dot_or_dotdot(e->d_name))
878 continue;
7155b727
JS
879
880 strbuf_setlen(path, len);
881 strbuf_addstr(path, e->d_name);
882 if (lstat(path->buf, &st))
883 ; /* fall thru */
884 else if (S_ISDIR(st.st_mode)) {
885 if (!remove_dir_recursively(path, only_empty))
886 continue; /* happy */
887 } else if (!only_empty && !unlink(path->buf))
888 continue; /* happy, too */
889
890 /* path too long, stat fails, or non-directory still exists */
891 ret = -1;
892 break;
893 }
894 closedir(dir);
895
896 strbuf_setlen(path, original_len);
897 if (!ret)
898 ret = rmdir(path->buf);
899 return ret;
900}
039bc64e
JH
901
902void setup_standard_excludes(struct dir_struct *dir)
903{
904 const char *path;
905
906 dir->exclude_per_dir = ".gitignore";
907 path = git_path("info/exclude");
908 if (!access(path, R_OK))
909 add_excludes_from_file(dir, path);
910 if (excludes_file && !access(excludes_file, R_OK))
911 add_excludes_from_file(dir, excludes_file);
912}
4a92d1bf
AR
913
914int remove_path(const char *name)
915{
916 char *slash;
917
918 if (unlink(name) && errno != ENOENT)
919 return -1;
920
921 slash = strrchr(name, '/');
922 if (slash) {
923 char *dirs = xstrdup(name);
924 slash = dirs + (slash - name);
925 do {
926 *slash = '\0';
927 } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
928 free(dirs);
929 }
930 return 0;
931}
932