added index caching
[git/mingw/4msysgit/wingit-dll.git] / dir.c
blob6d16bd01b92ee07a44ddeb03287e2c33c903fa68
1 /*
2 * This handles recursive filename detection with exclude
3 * files, index knowledge etc..
5 * Copyright (C) Linus Torvalds, 2005-2006
6 * Junio Hamano, 2005-2006
7 */
8 #include "cache.h"
9 #include "dir.h"
10 #include "refs.h"
12 struct path_simplify {
13 int len;
14 const char *path;
17 static int read_directory_recursive(struct dir_struct *dir,
18 const char *path, const char *base, int baselen,
19 int check_only, const struct path_simplify *simplify);
20 static int get_dtype(struct dirent *de, const char *path);
22 int common_prefix(const char **pathspec)
24 const char *path, *slash, *next;
25 int prefix;
27 if (!pathspec)
28 return 0;
30 path = *pathspec;
31 slash = strrchr(path, '/');
32 if (!slash)
33 return 0;
35 prefix = slash - path + 1;
36 while ((next = *++pathspec) != NULL) {
37 int len = strlen(next);
38 if (len >= prefix && !memcmp(path, next, prefix))
39 continue;
40 len = prefix - 1;
41 for (;;) {
42 if (!len)
43 return 0;
44 if (next[--len] != '/')
45 continue;
46 if (memcmp(path, next, len+1))
47 continue;
48 prefix = len + 1;
49 break;
52 return prefix;
56 * Does 'match' match the given name?
57 * A match is found if
59 * (1) the 'match' string is leading directory of 'name', or
60 * (2) the 'match' string is a wildcard and matches 'name', or
61 * (3) the 'match' string is exactly the same as 'name'.
63 * and the return value tells which case it was.
65 * It returns 0 when there is no match.
67 static int match_one(const char *match, const char *name, int namelen)
69 int matchlen;
71 /* If the match was just the prefix, we matched */
72 if (!*match)
73 return MATCHED_RECURSIVELY;
75 for (;;) {
76 unsigned char c1 = *match;
77 unsigned char c2 = *name;
78 if (c1 == '\0' || is_glob_special(c1))
79 break;
80 if (c1 != c2)
81 return 0;
82 match++;
83 name++;
84 namelen--;
89 * If we don't match the matchstring exactly,
90 * we need to match by fnmatch
92 matchlen = strlen(match);
93 if (strncmp(match, name, matchlen))
94 return !fnmatch(match, name, 0) ? MATCHED_FNMATCH : 0;
96 if (namelen == matchlen)
97 return MATCHED_EXACTLY;
98 if (match[matchlen-1] == '/' || name[matchlen] == '/')
99 return MATCHED_RECURSIVELY;
100 return 0;
104 * Given a name and a list of pathspecs, see if the name matches
105 * any of the pathspecs. The caller is also interested in seeing
106 * all pathspec matches some names it calls this function with
107 * (otherwise the user could have mistyped the unmatched pathspec),
108 * and a mark is left in seen[] array for pathspec element that
109 * actually matched anything.
111 int match_pathspec(const char **pathspec, const char *name, int namelen,
112 int prefix, char *seen)
114 int i, retval = 0;
116 if (!pathspec)
117 return 1;
119 name += prefix;
120 namelen -= prefix;
122 for (i = 0; pathspec[i] != NULL; i++) {
123 int how;
124 const char *match = pathspec[i] + prefix;
125 if (seen && seen[i] == MATCHED_EXACTLY)
126 continue;
127 how = match_one(match, name, namelen);
128 if (how) {
129 if (retval < how)
130 retval = how;
131 if (seen && seen[i] < how)
132 seen[i] = how;
135 return retval;
138 static int no_wildcard(const char *string)
140 return string[strcspn(string, "*?[{\\")] == '\0';
143 void add_exclude(const char *string, const char *base,
144 int baselen, struct exclude_list *which)
146 struct exclude *x;
147 size_t len;
148 int to_exclude = 1;
149 int flags = 0;
151 if (*string == '!') {
152 to_exclude = 0;
153 string++;
155 len = strlen(string);
156 if (len && string[len - 1] == '/') {
157 char *s;
158 x = xmalloc(sizeof(*x) + len);
159 s = (char *)(x+1);
160 memcpy(s, string, len - 1);
161 s[len - 1] = '\0';
162 string = s;
163 x->pattern = s;
164 flags = EXC_FLAG_MUSTBEDIR;
165 } else {
166 x = xmalloc(sizeof(*x));
167 x->pattern = string;
169 x->to_exclude = to_exclude;
170 x->patternlen = strlen(string);
171 x->base = base;
172 x->baselen = baselen;
173 x->flags = flags;
174 if (!strchr(string, '/'))
175 x->flags |= EXC_FLAG_NODIR;
176 if (no_wildcard(string))
177 x->flags |= EXC_FLAG_NOWILDCARD;
178 if (*string == '*' && no_wildcard(string+1))
179 x->flags |= EXC_FLAG_ENDSWITH;
180 ALLOC_GROW(which->excludes, which->nr + 1, which->alloc);
181 which->excludes[which->nr++] = x;
184 static int add_excludes_from_file_1(const char *fname,
185 const char *base,
186 int baselen,
187 char **buf_p,
188 struct exclude_list *which)
190 struct stat st;
191 int fd, i;
192 size_t size;
193 char *buf, *entry;
195 fd = open(fname, O_RDONLY);
196 if (fd < 0 || fstat(fd, &st) < 0)
197 goto err;
198 size = xsize_t(st.st_size);
199 if (size == 0) {
200 close(fd);
201 return 0;
203 buf = xmalloc(size+1);
204 if (read_in_full(fd, buf, size) != size)
206 free(buf);
207 goto err;
209 close(fd);
211 if (buf_p)
212 *buf_p = buf;
213 buf[size++] = '\n';
214 entry = buf;
215 for (i = 0; i < size; i++) {
216 if (buf[i] == '\n') {
217 if (entry != buf + i && entry[0] != '#') {
218 buf[i - (i && buf[i-1] == '\r')] = 0;
219 add_exclude(entry, base, baselen, which);
221 entry = buf + i + 1;
224 return 0;
226 err:
227 if (0 <= fd)
228 close(fd);
229 return -1;
232 void add_excludes_from_file(struct dir_struct *dir, const char *fname)
234 if (add_excludes_from_file_1(fname, "", 0, NULL,
235 &dir->exclude_list[EXC_FILE]) < 0)
236 die("cannot use %s as an exclude file", fname);
239 static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
241 struct exclude_list *el;
242 struct exclude_stack *stk = NULL;
243 int current;
245 if ((!dir->exclude_per_dir) ||
246 (baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
247 return; /* too long a path -- ignore */
249 /* Pop the ones that are not the prefix of the path being checked. */
250 el = &dir->exclude_list[EXC_DIRS];
251 while ((stk = dir->exclude_stack) != NULL) {
252 if (stk->baselen <= baselen &&
253 !strncmp(dir->basebuf, base, stk->baselen))
254 break;
255 dir->exclude_stack = stk->prev;
256 while (stk->exclude_ix < el->nr)
257 free(el->excludes[--el->nr]);
258 free(stk->filebuf);
259 free(stk);
262 /* Read from the parent directories and push them down. */
263 current = stk ? stk->baselen : -1;
264 while (current < baselen) {
265 struct exclude_stack *stk = xcalloc(1, sizeof(*stk));
266 const char *cp;
268 if (current < 0) {
269 cp = base;
270 current = 0;
272 else {
273 cp = strchr(base + current + 1, '/');
274 if (!cp)
275 die("oops in prep_exclude");
276 cp++;
278 stk->prev = dir->exclude_stack;
279 stk->baselen = cp - base;
280 stk->exclude_ix = el->nr;
281 memcpy(dir->basebuf + current, base + current,
282 stk->baselen - current);
283 strcpy(dir->basebuf + stk->baselen, dir->exclude_per_dir);
284 add_excludes_from_file_1(dir->basebuf,
285 dir->basebuf, stk->baselen,
286 &stk->filebuf, el);
287 dir->exclude_stack = stk;
288 current = stk->baselen;
290 dir->basebuf[baselen] = '\0';
293 /* Scan the list and let the last match determine the fate.
294 * Return 1 for exclude, 0 for include and -1 for undecided.
296 static int excluded_1(const char *pathname,
297 int pathlen, const char *basename, int *dtype,
298 struct exclude_list *el)
300 int i;
302 if (el->nr) {
303 for (i = el->nr - 1; 0 <= i; i--) {
304 struct exclude *x = el->excludes[i];
305 const char *exclude = x->pattern;
306 int to_exclude = x->to_exclude;
308 if (x->flags & EXC_FLAG_MUSTBEDIR) {
309 if (*dtype == DT_UNKNOWN)
310 *dtype = get_dtype(NULL, pathname);
311 if (*dtype != DT_DIR)
312 continue;
315 if (x->flags & EXC_FLAG_NODIR) {
316 /* match basename */
317 if (x->flags & EXC_FLAG_NOWILDCARD) {
318 if (!strcmp(exclude, basename))
319 return to_exclude;
320 } else if (x->flags & EXC_FLAG_ENDSWITH) {
321 if (x->patternlen - 1 <= pathlen &&
322 !strcmp(exclude + 1, pathname + pathlen - x->patternlen + 1))
323 return to_exclude;
324 } else {
325 if (fnmatch(exclude, basename, 0) == 0)
326 return to_exclude;
329 else {
330 /* match with FNM_PATHNAME:
331 * exclude has base (baselen long) implicitly
332 * in front of it.
334 int baselen = x->baselen;
335 if (*exclude == '/')
336 exclude++;
338 if (pathlen < baselen ||
339 (baselen && pathname[baselen-1] != '/') ||
340 strncmp(pathname, x->base, baselen))
341 continue;
343 if (x->flags & EXC_FLAG_NOWILDCARD) {
344 if (!strcmp(exclude, pathname + baselen))
345 return to_exclude;
346 } else {
347 if (fnmatch(exclude, pathname+baselen,
348 FNM_PATHNAME) == 0)
349 return to_exclude;
354 return -1; /* undecided */
357 int excluded(struct dir_struct *dir, const char *pathname, int *dtype_p)
359 int pathlen = strlen(pathname);
360 int st;
361 const char *basename = strrchr(pathname, '/');
362 basename = (basename) ? basename+1 : pathname;
364 prep_exclude(dir, pathname, basename-pathname);
365 for (st = EXC_CMDL; st <= EXC_FILE; st++) {
366 switch (excluded_1(pathname, pathlen, basename,
367 dtype_p, &dir->exclude_list[st])) {
368 case 0:
369 return 0;
370 case 1:
371 return 1;
374 return 0;
377 static struct dir_entry *dir_entry_new(const char *pathname, int len)
379 struct dir_entry *ent;
381 ent = xmalloc(sizeof(*ent) + len + 1);
382 ent->len = len;
383 memcpy(ent->name, pathname, len);
384 ent->name[len] = 0;
385 return ent;
388 static struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
390 if (cache_name_exists(pathname, len, ignore_case))
391 return NULL;
393 ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
394 return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
397 static struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
399 if (cache_name_pos(pathname, len) >= 0)
400 return NULL;
402 ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
403 return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
406 enum exist_status {
407 index_nonexistent = 0,
408 index_directory,
409 index_gitdir,
413 * The index sorts alphabetically by entry name, which
414 * means that a gitlink sorts as '\0' at the end, while
415 * a directory (which is defined not as an entry, but as
416 * the files it contains) will sort with the '/' at the
417 * end.
419 static enum exist_status directory_exists_in_index(const char *dirname, int len)
421 int pos = cache_name_pos(dirname, len);
422 if (pos < 0)
423 pos = -pos-1;
424 while (pos < active_nr) {
425 struct cache_entry *ce = active_cache[pos++];
426 unsigned char endchar;
428 if (strncmp(ce->name, dirname, len))
429 break;
430 endchar = ce->name[len];
431 if (endchar > '/')
432 break;
433 if (endchar == '/')
434 return index_directory;
435 if (!endchar && S_ISGITLINK(ce->ce_mode))
436 return index_gitdir;
438 return index_nonexistent;
442 * When we find a directory when traversing the filesystem, we
443 * have three distinct cases:
445 * - ignore it
446 * - see it as a directory
447 * - recurse into it
449 * and which one we choose depends on a combination of existing
450 * git index contents and the flags passed into the directory
451 * traversal routine.
453 * Case 1: If we *already* have entries in the index under that
454 * directory name, we always recurse into the directory to see
455 * all the files.
457 * Case 2: If we *already* have that directory name as a gitlink,
458 * we always continue to see it as a gitlink, regardless of whether
459 * there is an actual git directory there or not (it might not
460 * be checked out as a subproject!)
462 * Case 3: if we didn't have it in the index previously, we
463 * have a few sub-cases:
465 * (a) if "show_other_directories" is true, we show it as
466 * just a directory, unless "hide_empty_directories" is
467 * also true and the directory is empty, in which case
468 * we just ignore it entirely.
469 * (b) if it looks like a git directory, and we don't have
470 * 'no_gitlinks' set we treat it as a gitlink, and show it
471 * as a directory.
472 * (c) otherwise, we recurse into it.
474 enum directory_treatment {
475 show_directory,
476 ignore_directory,
477 recurse_into_directory,
480 static enum directory_treatment treat_directory(struct dir_struct *dir,
481 const char *dirname, int len,
482 const struct path_simplify *simplify)
484 /* The "len-1" is to strip the final '/' */
485 switch (directory_exists_in_index(dirname, len-1)) {
486 case index_directory:
487 return recurse_into_directory;
489 case index_gitdir:
490 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
491 return ignore_directory;
492 return show_directory;
494 case index_nonexistent:
495 if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES)
496 break;
497 if (!(dir->flags & DIR_NO_GITLINKS)) {
498 unsigned char sha1[20];
499 if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
500 return show_directory;
502 return recurse_into_directory;
505 /* This is the "show_other_directories" case */
506 if (!(dir->flags & DIR_HIDE_EMPTY_DIRECTORIES))
507 return show_directory;
508 if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
509 return ignore_directory;
510 return show_directory;
514 * This is an inexact early pruning of any recursive directory
515 * reading - if the path cannot possibly be in the pathspec,
516 * return true, and we'll skip it early.
518 static int simplify_away(const char *path, int pathlen, const struct path_simplify *simplify)
520 if (simplify) {
521 for (;;) {
522 const char *match = simplify->path;
523 int len = simplify->len;
525 if (!match)
526 break;
527 if (len > pathlen)
528 len = pathlen;
529 if (!memcmp(path, match, len))
530 return 0;
531 simplify++;
533 return 1;
535 return 0;
538 static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
540 if (simplify) {
541 for (; simplify->path; simplify++) {
542 if (len == simplify->len
543 && !memcmp(path, simplify->path, len))
544 return 1;
547 return 0;
550 static int get_dtype(struct dirent *de, const char *path)
552 int dtype = de ? DTYPE(de) : DT_UNKNOWN;
553 struct stat st;
555 if (dtype != DT_UNKNOWN)
556 return dtype;
557 if (lstat(path, &st))
558 return dtype;
559 if (S_ISREG(st.st_mode))
560 return DT_REG;
561 if (S_ISDIR(st.st_mode))
562 return DT_DIR;
563 if (S_ISLNK(st.st_mode))
564 return DT_LNK;
565 return dtype;
569 * Read a directory tree. We currently ignore anything but
570 * directories, regular files and symlinks. That's because git
571 * doesn't handle them at all yet. Maybe that will change some
572 * day.
574 * Also, we ignore the name ".git" (even if it is not a directory).
575 * That likely will not change.
577 static int read_directory_recursive(struct dir_struct *dir, const char *path, const char *base, int baselen, int check_only, const struct path_simplify *simplify)
579 DIR *fdir = opendir(*path ? path : ".");
580 int contents = 0;
582 if (fdir) {
583 struct dirent *de;
584 char fullname[PATH_MAX + 1];
585 memcpy(fullname, base, baselen);
587 while ((de = readdir(fdir)) != NULL) {
588 int len, dtype;
589 int exclude;
591 if (is_dot_or_dotdot(de->d_name) ||
592 !strcmp(de->d_name, ".git"))
593 continue;
594 len = strlen(de->d_name);
595 /* Ignore overly long pathnames! */
596 if (len + baselen + 8 > sizeof(fullname))
597 continue;
598 memcpy(fullname + baselen, de->d_name, len+1);
599 if (simplify_away(fullname, baselen + len, simplify))
600 continue;
602 dtype = DTYPE(de);
603 exclude = excluded(dir, fullname, &dtype);
604 if (exclude && (dir->flags & DIR_COLLECT_IGNORED)
605 && ((dir->flags & DIR_COLLECT_ALL_IGNORED) || in_pathspec(fullname, baselen + len, simplify)))
607 if (dir->flags & DIR_COLLECT_ALL_IGNORED)
609 // collect dirs too, but end their names with a /
610 if (dtype == DT_UNKNOWN)
611 dtype = get_dtype(de, fullname);
613 if (dtype == DT_DIR)
615 memcpy(fullname + baselen + len, "/", 2);
616 dir_add_ignored(dir, fullname, baselen + len+1);
617 fullname[baselen + len] = 0;
619 else
621 dir_add_ignored(dir, fullname, baselen + len);
624 else
626 dir_add_ignored(dir, fullname, baselen + len);
631 * Excluded? If we don't explicitly want to show
632 * ignored files, ignore it
634 if (exclude && !(dir->flags & DIR_SHOW_IGNORED))
635 continue;
637 if (dtype == DT_UNKNOWN)
638 dtype = get_dtype(de, fullname);
641 * Do we want to see just the ignored files?
642 * We still need to recurse into directories,
643 * even if we don't ignore them, since the
644 * directory may contain files that we do..
646 if (!exclude && (dir->flags & DIR_SHOW_IGNORED)) {
647 if (dtype != DT_DIR)
648 continue;
651 switch (dtype) {
652 default:
653 continue;
654 case DT_DIR:
655 memcpy(fullname + baselen + len, "/", 2);
656 len++;
657 switch (treat_directory(dir, fullname, baselen + len, simplify)) {
658 case show_directory:
659 if (exclude != !!(dir->flags
660 & DIR_SHOW_IGNORED))
661 continue;
662 break;
663 case recurse_into_directory:
664 if (dir->flags & DIR_COLLECT_DIRECTORIES)
665 dir_add_name(dir, fullname, baselen + len);
666 if (dir->flags & DIR_NO_RECURSE_READDIR)
667 continue;
668 contents += read_directory_recursive(dir,
669 fullname, fullname, baselen + len, 0, simplify);
670 continue;
671 case ignore_directory:
672 if (dir->flags & DIR_COLLECT_DIRECTORIES)
673 dir_add_name(dir, fullname, baselen + len);
674 continue;
676 break;
677 case DT_REG:
678 case DT_LNK:
679 break;
681 contents++;
682 if (check_only)
683 goto exit_early;
684 else
685 dir_add_name(dir, fullname, baselen + len);
687 exit_early:
688 closedir(fdir);
691 return contents;
694 static int cmp_name(const void *p1, const void *p2)
696 const struct dir_entry *e1 = *(const struct dir_entry **)p1;
697 const struct dir_entry *e2 = *(const struct dir_entry **)p2;
699 return cache_name_compare(e1->name, e1->len,
700 e2->name, e2->len);
704 * Return the length of the "simple" part of a path match limiter.
706 static int simple_length(const char *match)
708 int len = -1;
710 for (;;) {
711 unsigned char c = *match++;
712 len++;
713 if (c == '\0' || is_glob_special(c))
714 return len;
718 static struct path_simplify *create_simplify(const char **pathspec)
720 int nr, alloc = 0;
721 struct path_simplify *simplify = NULL;
723 if (!pathspec)
724 return NULL;
726 for (nr = 0 ; ; nr++) {
727 const char *match;
728 if (nr >= alloc) {
729 alloc = alloc_nr(alloc);
730 simplify = xrealloc(simplify, alloc * sizeof(*simplify));
732 match = *pathspec++;
733 if (!match)
734 break;
735 simplify[nr].path = match;
736 simplify[nr].len = simple_length(match);
738 simplify[nr].path = NULL;
739 simplify[nr].len = 0;
740 return simplify;
743 static void free_simplify(struct path_simplify *simplify)
745 free(simplify);
748 int read_directory(struct dir_struct *dir, const char *path, const char *base, int baselen, const char **pathspec)
750 struct path_simplify *simplify;
752 if (has_symlink_leading_path(path, strlen(path)))
753 return dir->nr;
755 simplify = create_simplify(pathspec);
756 read_directory_recursive(dir, path, base, baselen, 0, simplify);
757 free_simplify(simplify);
758 qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
759 qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
760 return dir->nr;
763 int file_exists(const char *f)
765 struct stat sb;
766 return lstat(f, &sb) == 0;
770 * get_relative_cwd() gets the prefix of the current working directory
771 * relative to 'dir'. If we are not inside 'dir', it returns NULL.
773 * As a convenience, it also returns NULL if 'dir' is already NULL. The
774 * reason for this behaviour is that it is natural for functions returning
775 * directory names to return NULL to say "this directory does not exist"
776 * or "this directory is invalid". These cases are usually handled the
777 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
778 * returns NULL for both of them.
780 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
781 * unifies the handling of "outside work tree" with "no work tree at all".
783 char *get_relative_cwd(char *buffer, int size, const char *dir)
785 char *cwd = buffer;
787 if (!dir)
788 return NULL;
789 if (!getcwd(buffer, size))
790 die("can't find the current directory: %s", strerror(errno));
792 if (!is_absolute_path(dir))
793 dir = make_absolute_path(dir);
795 while (*dir && *dir == *cwd) {
796 dir++;
797 cwd++;
799 if (*dir)
800 return NULL;
801 if (*cwd == '/')
802 return cwd + 1;
803 return cwd;
806 int is_inside_dir(const char *dir)
808 char buffer[PATH_MAX];
809 return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
812 int is_empty_dir(const char *path)
814 DIR *dir = opendir(path);
815 struct dirent *e;
816 int ret = 1;
818 if (!dir)
819 return 0;
821 while ((e = readdir(dir)) != NULL)
822 if (!is_dot_or_dotdot(e->d_name)) {
823 ret = 0;
824 break;
827 closedir(dir);
828 return ret;
831 int remove_dir_recursively(struct strbuf *path, int only_empty)
833 DIR *dir = opendir(path->buf);
834 struct dirent *e;
835 int ret = 0, original_len = path->len, len;
837 if (!dir)
838 return -1;
839 if (path->buf[original_len - 1] != '/')
840 strbuf_addch(path, '/');
842 len = path->len;
843 while ((e = readdir(dir)) != NULL) {
844 struct stat st;
845 if (is_dot_or_dotdot(e->d_name))
846 continue;
848 strbuf_setlen(path, len);
849 strbuf_addstr(path, e->d_name);
850 if (lstat(path->buf, &st))
851 ; /* fall thru */
852 else if (S_ISDIR(st.st_mode)) {
853 if (!remove_dir_recursively(path, only_empty))
854 continue; /* happy */
855 } else if (!only_empty && !unlink(path->buf))
856 continue; /* happy, too */
858 /* path too long, stat fails, or non-directory still exists */
859 ret = -1;
860 break;
862 closedir(dir);
864 strbuf_setlen(path, original_len);
865 if (!ret)
866 ret = rmdir(path->buf);
867 return ret;
870 void setup_standard_excludes(struct dir_struct *dir)
872 const char *path;
874 dir->exclude_per_dir = ".gitignore";
875 path = git_path("info/exclude");
876 if (!access(path, R_OK))
877 add_excludes_from_file(dir, path);
878 if (excludes_file && !access(excludes_file, R_OK))
879 add_excludes_from_file(dir, excludes_file);
882 int remove_path(const char *name)
884 char *slash;
886 if (unlink(name) && errno != ENOENT)
887 return -1;
889 slash = strrchr(name, '/');
890 if (slash) {
891 char *dirs = xstrdup(name);
892 slash = dirs + (slash - name);
893 do {
894 *slash = '\0';
895 } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
896 free(dirs);
898 return 0;