2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "bsdtar_platform.h"
27 __FBSDID("$FreeBSD$");
42 struct match *exclusions;
44 struct match *inclusions;
46 int inclusions_unmatched_count;
50 static void add_pattern(struct bsdtar *, struct match **list,
52 static int bsdtar_fnmatch(const char *p, const char *s);
53 static void initialize_matching(struct bsdtar *);
54 static int match_exclusion(struct match *, const char *pathname);
55 static int match_inclusion(struct match *, const char *pathname);
58 * The matching logic here needs to be re-thought. I started out to
59 * try to mimic gtar's matching logic, but it's not entirely
60 * consistent. In particular 'tar -t' and 'tar -x' interpret patterns
61 * on the command line as anchored, but --exclude doesn't.
65 * Utility functions to manage exclusion/inclusion patterns
69 exclude(struct bsdtar *bsdtar, const char *pattern)
71 struct matching *matching;
73 if (bsdtar->matching == NULL)
74 initialize_matching(bsdtar);
75 matching = bsdtar->matching;
76 add_pattern(bsdtar, &(matching->exclusions), pattern);
77 matching->exclusions_count++;
82 exclude_from_file(struct bsdtar *bsdtar, const char *pathname)
84 return (process_lines(bsdtar, pathname, &exclude));
88 include(struct bsdtar *bsdtar, const char *pattern)
90 struct matching *matching;
92 if (bsdtar->matching == NULL)
93 initialize_matching(bsdtar);
94 matching = bsdtar->matching;
95 add_pattern(bsdtar, &(matching->inclusions), pattern);
96 matching->inclusions_count++;
97 matching->inclusions_unmatched_count++;
102 include_from_file(struct bsdtar *bsdtar, const char *pathname)
104 return (process_lines(bsdtar, pathname, &include));
108 add_pattern(struct bsdtar *bsdtar, struct match **list, const char *pattern)
112 match = malloc(sizeof(*match) + strlen(pattern) + 1);
114 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
115 if (pattern[0] == '/')
117 strcpy(match->pattern, pattern);
118 /* Both "foo/" and "foo" should match "foo/bar". */
119 if (match->pattern[strlen(match->pattern)-1] == '/')
120 match->pattern[strlen(match->pattern)-1] = '\0';
128 excluded(struct bsdtar *bsdtar, const char *pathname)
130 struct matching *matching;
132 struct match *matched;
134 matching = bsdtar->matching;
135 if (matching == NULL)
138 /* Exclusions take priority */
139 for (match = matching->exclusions; match != NULL; match = match->next){
140 if (match_exclusion(match, pathname))
144 /* Then check for inclusions */
146 for (match = matching->inclusions; match != NULL; match = match->next){
147 if (match_inclusion(match, pathname)) {
149 * If this pattern has never been matched,
152 if (match->matches == 0) {
154 matching->inclusions_unmatched_count++;
158 * Otherwise, remember the match but keep checking
159 * in case we can tick off an unmatched pattern.
165 * We didn't find a pattern that had never been matched, but
166 * we did find a match, so count it and exit.
168 if (matched != NULL) {
173 /* If there were inclusions, default is to exclude. */
174 if (matching->inclusions != NULL)
177 /* No explicit inclusions, default is to match. */
182 * This is a little odd, but it matches the default behavior of
183 * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar'
187 match_exclusion(struct match *match, const char *pathname)
191 if (*match->pattern == '*' || *match->pattern == '/')
192 return (bsdtar_fnmatch(match->pattern, pathname) == 0);
194 for (p = pathname; p != NULL; p = strchr(p, '/')) {
197 if (bsdtar_fnmatch(match->pattern, p) == 0)
204 * Again, mimic gtar: inclusions are always anchored (have to match
205 * the beginning of the path) even though exclusions are not anchored.
208 match_inclusion(struct match *match, const char *pathname)
210 return (bsdtar_fnmatch(match->pattern, pathname) == 0);
214 cleanup_exclusions(struct bsdtar *bsdtar)
218 if (bsdtar->matching) {
219 p = bsdtar->matching->inclusions;
225 p = bsdtar->matching->exclusions;
231 free(bsdtar->matching);
236 initialize_matching(struct bsdtar *bsdtar)
238 bsdtar->matching = malloc(sizeof(*bsdtar->matching));
239 if (bsdtar->matching == NULL)
240 bsdtar_errc(bsdtar, 1, errno, "No memory");
241 memset(bsdtar->matching, 0, sizeof(*bsdtar->matching));
245 unmatched_inclusions(struct bsdtar *bsdtar)
247 struct matching *matching;
249 matching = bsdtar->matching;
250 if (matching == NULL)
252 return (matching->inclusions_unmatched_count);
257 #if defined(HAVE_FNMATCH) && defined(HAVE_FNM_LEADING_DIR)
259 /* Use system fnmatch() if it suits our needs. */
260 /* On Linux, _GNU_SOURCE must be defined to get FNM_LEADING_DIR. */
264 bsdtar_fnmatch(const char *pattern, const char *string)
266 return (fnmatch(pattern, string, FNM_LEADING_DIR));
271 * The following was hacked from BSD C library
272 * code: src/lib/libc/gen/fnmatch.c,v 1.15 2002/02/01
274 * In particular, most of the flags were ripped out: this always
275 * behaves like FNM_LEADING_DIR is set and other flags specified
276 * by POSIX are unset.
278 * Normally, I would not conditionally compile something like this: If
279 * I have to support it anyway, everyone may as well use it. ;-)
280 * However, the full POSIX spec for fnmatch() includes a lot of
281 * advanced character handling that I'm not ready to put in here, so
282 * it's probably best if people use a local version when it's available.
286 * Copyright (c) 1989, 1993, 1994
287 * The Regents of the University of California. All rights reserved.
289 * This code is derived from software contributed to Berkeley by
292 * Redistribution and use in source and binary forms, with or without
293 * modification, are permitted provided that the following conditions
295 * 1. Redistributions of source code must retain the above copyright
296 * notice, this list of conditions and the following disclaimer.
297 * 2. Redistributions in binary form must reproduce the above copyright
298 * notice, this list of conditions and the following disclaimer in the
299 * documentation and/or other materials provided with the distribution.
300 * 4. Neither the name of the University nor the names of its contributors
301 * may be used to endorse or promote products derived from this software
302 * without specific prior written permission.
304 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
305 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
306 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
307 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
308 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
309 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
310 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
312 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
318 bsdtar_fnmatch(const char *pattern, const char *string)
320 const char *saved_pattern;
325 switch (c = *pattern++) {
327 if (*string == '/' || *string == '\0')
337 /* Collapse multiple stars. */
341 /* Optimize for pattern with * at end. */
345 /* General case, use recursion. */
346 while (*string != '\0') {
347 if (!bsdtar_fnmatch(pattern, string))
355 saved_pattern = pattern;
356 if (*pattern == '!' || *pattern == '^') {
367 pattern = saved_pattern;
371 if (*pattern == '-') {
372 char c2 = *(pattern + 1);
374 pattern = saved_pattern;
379 /* [a-] is not a range. */
390 } else if (c == *string)
394 if (matched == negate)
399 if ((c = *pattern++) == '\0') {