re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / src / exclist.c
1 /* Per-directory exclusion files for tar.
2
3    Copyright 2014, 2016 Free Software Foundation, Inc.
4
5    This file is part of GNU tar.
6
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.
11
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.
16
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/>.
19 */
20 #include <system.h>
21 #include <quotearg.h>
22 #include <fnmatch.h>
23 #include <wordsplit.h>
24 #include "common.h"
25
26 typedef void (*add_fn) (struct exclude *, char const *, int, void *);
27
28 struct vcs_ignore_file
29 {
30   char const *filename;
31   int flags;
32   add_fn addfn;
33   void *(*initfn) (void *);
34   void *data;
35 };
36
37 static struct vcs_ignore_file *get_vcs_ignore_file (const char *name);
38 \f
39 struct excfile
40 {
41   struct excfile *next;
42   int flags;
43   char name[1];
44 };
45
46 static struct excfile *excfile_head, *excfile_tail;
47
48 void
49 excfile_add (const char *name, int flags)
50 {
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;
60 }
61
62 struct exclist
63 {
64   struct exclist *next, *prev;
65   int flags;
66   struct exclude *excluded;
67 };
68
69 void
70 info_attach_exclist (struct tar_stat_info *dir)
71 {
72   struct excfile *file;
73   struct exclist *head = NULL, *tail = NULL, *ent;
74   struct vcs_ignore_file *vcsfile;
75
76   if (dir->exclude_list)
77     return;
78   for (file = excfile_head; file; file = file->next)
79     {
80       if (faccessat (dir ? dir->fd : chdir_fd, file->name, F_OK, 0) == 0)
81         {
82           FILE *fp;
83           struct exclude *ex = NULL;
84           int fd = subfile_open (dir, file->name, O_RDONLY);
85           if (fd == -1)
86             {
87               open_error (file->name);
88               continue;
89             }
90           fp = fdopen (fd, "r");
91           if (!fp)
92             {
93               ERROR ((0, errno, _("%s: fdopen failed"), file->name));
94               close (fd);
95               continue;
96             }
97
98           if (!ex)
99             ex = new_exclude ();
100
101           vcsfile = get_vcs_ignore_file (file->name);
102
103           if (vcsfile->initfn)
104             vcsfile->data = vcsfile->initfn (vcsfile->data);
105
106           if (add_exclude_fp (vcsfile->addfn, ex, fp,
107                               EXCLUDE_WILDCARDS|EXCLUDE_ANCHORED, '\n',
108                               vcsfile->data))
109             {
110               int e = errno;
111               FATAL_ERROR ((0, e, "%s", quotearg_colon (file->name)));
112             }
113           fclose (fp);
114
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;
121
122           if (tail)
123             tail->next = ent;
124           else
125             head = ent;
126           tail = ent;
127         }
128     }
129   dir->exclude_list = head;
130 }
131
132 void
133 info_free_exclist (struct tar_stat_info *dir)
134 {
135   struct exclist *ep = dir->exclude_list;
136
137   while (ep)
138     {
139       struct exclist *next = ep->next;
140       free_exclude (ep->excluded);
141       free (ep);
142       ep = next;
143     }
144
145   dir->exclude_list = NULL;
146 }
147
148
149 /* Return nonzero if file NAME is excluded.  */
150 bool
151 excluded_name (char const *name, struct tar_stat_info *st)
152 {
153   struct exclist *ep;
154   const char *rname = NULL;
155   char *bname = NULL;
156   bool result;
157   int nr = 0;
158
159   name += FILE_SYSTEM_PREFIX_LEN (name);
160
161   /* Try global exclusion list first */
162   if (excluded_file_name (excluded, name))
163     return true;
164
165   if (!st)
166     return false;
167
168   for (result = false; st && !result; st = st->parent, nr = EXCL_NON_RECURSIVE)
169     {
170       for (ep = st->exclude_list; ep; ep = ep->next)
171         {
172           if (ep->flags & nr)
173             continue;
174           if ((result = excluded_file_name (ep->excluded, name)))
175             break;
176
177           if (!rname)
178             {
179               rname = name;
180               /* Skip leading ./ */
181               while (*rname == '.' && ISSLASH (rname[1]))
182                 rname += 2;
183             }
184           if ((result = excluded_file_name (ep->excluded, rname)))
185             break;
186
187           if (!bname)
188             bname = base_name (name);
189           if ((result = excluded_file_name (ep->excluded, bname)))
190             break;
191         }
192     }
193
194   free (bname);
195
196   return result;
197 }
198 \f
199 static void
200 cvs_addfn (struct exclude *ex, char const *pattern, int options, void *data)
201 {
202   struct wordsplit ws;
203   size_t i;
204
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);
211 }
212
213 static void
214 git_addfn (struct exclude *ex, char const *pattern, int options, void *data)
215 {
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);
223 }
224
225 static void
226 bzr_addfn (struct exclude *ex, char const *pattern, int options, void *data)
227 {
228   while (isspace (*pattern))
229     ++pattern;
230   if (*pattern == 0 || *pattern == '#')
231     return;
232   if (*pattern == '!')
233     {
234       if (*++pattern == '!')
235         ++pattern;
236       else
237         options |= EXCLUDE_INCLUDE;
238     }
239   /* FIXME: According to the docs, globbing patterns are rsync-style,
240             and regexps are perl-style. */
241   if (strncmp (pattern, "RE:", 3) == 0)
242     {
243       pattern += 3;
244       options &= ~EXCLUDE_WILDCARDS;
245       options |= EXCLUDE_REGEX;
246     }
247   add_exclude (ex, pattern, options);
248 }
249
250 static void *
251 hg_initfn (void *data)
252 {
253   static int hg_options;
254   int *hgopt = data ? data : &hg_options;
255   *hgopt = EXCLUDE_REGEX;
256   return hgopt;
257 }
258
259 static void
260 hg_addfn (struct exclude *ex, char const *pattern, int options, void *data)
261 {
262   int *hgopt = data;
263   size_t len;
264
265   while (isspace (*pattern))
266     ++pattern;
267   if (*pattern == 0 || *pattern == '#')
268     return;
269   if (strncmp (pattern, "syntax:", 7) == 0)
270     {
271       for (pattern += 7; isspace (*pattern); ++pattern)
272         ;
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;
280     }
281
282   len = strlen(pattern);
283   if (pattern[len-1] == '/')
284     {
285       char *p;
286
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;
294     }
295
296   add_exclude (ex, pattern,
297                ((*hgopt == EXCLUDE_REGEX)
298                 ? (options & ~EXCLUDE_WILDCARDS)
299                 : (options & ~EXCLUDE_REGEX)) | *hgopt);
300 }
301 \f
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 }
308 };
309
310 static struct vcs_ignore_file *
311 get_vcs_ignore_file (const char *name)
312 {
313   struct vcs_ignore_file *p;
314
315   for (p = vcs_ignore_files; p->filename; p++)
316     if (strcmp (p->filename, name) == 0)
317       break;
318
319   return p;
320 }
321 \f
322 void
323 exclude_vcs_ignores (void)
324 {
325   struct vcs_ignore_file *p;
326
327   for (p = vcs_ignore_files; p->filename; p++)
328     excfile_add (p->filename, EXCL_DEFAULT);
329 }