Test and document --keep-directory-symlink
[tar.git] / src / exclist.c
blobf20b98e581ebda310377374a3152b1be4681bffd
1 /* Per-directory exclusion files for tar.
3 Copyright 2014, 2016-2017 Free Software Foundation, Inc.
5 This file is part of GNU tar.
7 GNU tar is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU tar is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <system.h>
21 #include <quotearg.h>
22 #include <fnmatch.h>
23 #include <wordsplit.h>
24 #include "common.h"
26 typedef void (*add_fn) (struct exclude *, char const *, int, void *);
28 struct vcs_ignore_file
30 char const *filename;
31 int flags;
32 add_fn addfn;
33 void *(*initfn) (void *);
34 void *data;
37 static struct vcs_ignore_file *get_vcs_ignore_file (const char *name);
39 struct excfile
41 struct excfile *next;
42 int flags;
43 char name[1];
46 static struct excfile *excfile_head, *excfile_tail;
48 void
49 excfile_add (const char *name, int flags)
51 struct excfile *p = xmalloc (sizeof (*p) + strlen (name));
52 p->next = NULL;
53 p->flags = flags;
54 strcpy (p->name, name);
55 if (excfile_tail)
56 excfile_tail->next = p;
57 else
58 excfile_head = p;
59 excfile_tail = p;
62 struct exclist
64 struct exclist *next, *prev;
65 int flags;
66 struct exclude *excluded;
69 void
70 info_attach_exclist (struct tar_stat_info *dir)
72 struct excfile *file;
73 struct exclist *head = NULL, *tail = NULL, *ent;
74 struct vcs_ignore_file *vcsfile;
76 if (dir->exclude_list)
77 return;
78 for (file = excfile_head; file; file = file->next)
80 if (faccessat (dir ? dir->fd : chdir_fd, file->name, F_OK, 0) == 0)
82 FILE *fp;
83 struct exclude *ex = NULL;
84 int fd = subfile_open (dir, file->name, O_RDONLY);
85 if (fd == -1)
87 open_error (file->name);
88 continue;
90 fp = fdopen (fd, "r");
91 if (!fp)
93 ERROR ((0, errno, _("%s: fdopen failed"), file->name));
94 close (fd);
95 continue;
98 if (!ex)
99 ex = new_exclude ();
101 vcsfile = get_vcs_ignore_file (file->name);
103 if (vcsfile->initfn)
104 vcsfile->data = vcsfile->initfn (vcsfile->data);
106 if (add_exclude_fp (vcsfile->addfn, ex, fp,
107 EXCLUDE_WILDCARDS|EXCLUDE_ANCHORED, '\n',
108 vcsfile->data))
110 int e = errno;
111 FATAL_ERROR ((0, e, "%s", quotearg_colon (file->name)));
113 fclose (fp);
115 ent = xmalloc (sizeof (*ent));
116 ent->excluded = ex;
117 ent->flags = file->flags == EXCL_DEFAULT
118 ? file->flags : vcsfile->flags;
119 ent->prev = tail;
120 ent->next = NULL;
122 if (tail)
123 tail->next = ent;
124 else
125 head = ent;
126 tail = ent;
129 dir->exclude_list = head;
132 void
133 info_free_exclist (struct tar_stat_info *dir)
135 struct exclist *ep = dir->exclude_list;
137 while (ep)
139 struct exclist *next = ep->next;
140 free_exclude (ep->excluded);
141 free (ep);
142 ep = next;
145 dir->exclude_list = NULL;
149 /* Return nonzero if file NAME is excluded. */
150 bool
151 excluded_name (char const *name, struct tar_stat_info *st)
153 struct exclist *ep;
154 const char *rname = NULL;
155 char *bname = NULL;
156 bool result;
157 int nr = 0;
159 name += FILE_SYSTEM_PREFIX_LEN (name);
161 /* Try global exclusion list first */
162 if (excluded_file_name (excluded, name))
163 return true;
165 if (!st)
166 return false;
168 for (result = false; st && !result; st = st->parent, nr = EXCL_NON_RECURSIVE)
170 for (ep = st->exclude_list; ep; ep = ep->next)
172 if (ep->flags & nr)
173 continue;
174 if ((result = excluded_file_name (ep->excluded, name)))
175 break;
177 if (!rname)
179 rname = name;
180 /* Skip leading ./ */
181 while (*rname == '.' && ISSLASH (rname[1]))
182 rname += 2;
184 if ((result = excluded_file_name (ep->excluded, rname)))
185 break;
187 if (!bname)
188 bname = base_name (name);
189 if ((result = excluded_file_name (ep->excluded, bname)))
190 break;
194 free (bname);
196 return result;
199 static void
200 cvs_addfn (struct exclude *ex, char const *pattern, int options, void *data)
202 struct wordsplit ws;
203 size_t i;
205 if (wordsplit (pattern, &ws,
206 WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_SQUEEZE_DELIMS))
207 return;
208 for (i = 0; i < ws.ws_wordc; i++)
209 add_exclude (ex, ws.ws_wordv[i], options);
210 wordsplit_free (&ws);
213 static void
214 git_addfn (struct exclude *ex, char const *pattern, int options, void *data)
216 while (isspace (*pattern))
217 ++pattern;
218 if (*pattern == 0 || *pattern == '#')
219 return;
220 if (*pattern == '\\' && pattern[1] == '#')
221 ++pattern;
222 add_exclude (ex, pattern, options);
225 static void
226 bzr_addfn (struct exclude *ex, char const *pattern, int options, void *data)
228 while (isspace (*pattern))
229 ++pattern;
230 if (*pattern == 0 || *pattern == '#')
231 return;
232 if (*pattern == '!')
234 if (*++pattern == '!')
235 ++pattern;
236 else
237 options |= EXCLUDE_INCLUDE;
239 /* FIXME: According to the docs, globbing patterns are rsync-style,
240 and regexps are perl-style. */
241 if (strncmp (pattern, "RE:", 3) == 0)
243 pattern += 3;
244 options &= ~EXCLUDE_WILDCARDS;
245 options |= EXCLUDE_REGEX;
247 add_exclude (ex, pattern, options);
250 static void *
251 hg_initfn (void *data)
253 static int hg_options;
254 int *hgopt = data ? data : &hg_options;
255 *hgopt = EXCLUDE_REGEX;
256 return hgopt;
259 static void
260 hg_addfn (struct exclude *ex, char const *pattern, int options, void *data)
262 int *hgopt = data;
263 size_t len;
265 while (isspace (*pattern))
266 ++pattern;
267 if (*pattern == 0 || *pattern == '#')
268 return;
269 if (strncmp (pattern, "syntax:", 7) == 0)
271 for (pattern += 7; isspace (*pattern); ++pattern)
273 if (strcmp (pattern, "regexp") == 0)
274 /* FIXME: Regexps must be perl-style */
275 *hgopt = EXCLUDE_REGEX;
276 else if (strcmp (pattern, "glob") == 0)
277 *hgopt = EXCLUDE_WILDCARDS;
278 /* Ignore unknown syntax */
279 return;
282 len = strlen(pattern);
283 if (pattern[len-1] == '/')
285 char *p;
287 --len;
288 p = xmalloc (len+1);
289 memcpy (p, pattern, len);
290 p[len] = 0;
291 pattern = p;
292 exclude_add_pattern_buffer (ex, p);
293 options |= FNM_LEADING_DIR|EXCLUDE_ALLOC;
296 add_exclude (ex, pattern,
297 ((*hgopt == EXCLUDE_REGEX)
298 ? (options & ~EXCLUDE_WILDCARDS)
299 : (options & ~EXCLUDE_REGEX)) | *hgopt);
302 static struct vcs_ignore_file vcs_ignore_files[] = {
303 { ".cvsignore", EXCL_NON_RECURSIVE, cvs_addfn, NULL, NULL },
304 { ".gitignore", 0, git_addfn, NULL, NULL },
305 { ".bzrignore", 0, bzr_addfn, NULL, NULL },
306 { ".hgignore", 0, hg_addfn, hg_initfn, NULL },
307 { NULL, 0, git_addfn, NULL, NULL }
310 static struct vcs_ignore_file *
311 get_vcs_ignore_file (const char *name)
313 struct vcs_ignore_file *p;
315 for (p = vcs_ignore_files; p->filename; p++)
316 if (strcmp (p->filename, name) == 0)
317 break;
319 return p;
322 void
323 exclude_vcs_ignores (void)
325 struct vcs_ignore_file *p;
327 for (p = vcs_ignore_files; p->filename; p++)
328 excfile_add (p->filename, EXCL_DEFAULT);