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: src/usr.bin/tar/matching.c,v 1.16 2008/08/18 18:13:40 kientzle Exp $");
48 struct match *exclusions;
50 struct match *inclusions;
52 int inclusions_unmatched_count;
56 static void add_pattern(struct bsdtar *, struct match **list,
58 static int bsdtar_fnmatch(const char *p, const char *s);
59 static void initialize_matching(struct bsdtar *);
60 static int match_exclusion(struct match *, const char *pathname);
61 static int match_inclusion(struct match *, const char *pathname);
62 static int pathmatch(const char *p, const char *s);
65 * The matching logic here needs to be re-thought. I started out to
66 * try to mimic gtar's matching logic, but it's not entirely
67 * consistent. In particular 'tar -t' and 'tar -x' interpret patterns
68 * on the command line as anchored, but --exclude doesn't.
72 * Utility functions to manage exclusion/inclusion patterns
76 exclude(struct bsdtar *bsdtar, const char *pattern)
78 struct matching *matching;
80 if (bsdtar->matching == NULL)
81 initialize_matching(bsdtar);
82 matching = bsdtar->matching;
83 add_pattern(bsdtar, &(matching->exclusions), pattern);
84 matching->exclusions_count++;
89 exclude_from_file(struct bsdtar *bsdtar, const char *pathname)
91 return (process_lines(bsdtar, pathname, &exclude));
95 include(struct bsdtar *bsdtar, const char *pattern)
97 struct matching *matching;
99 if (bsdtar->matching == NULL)
100 initialize_matching(bsdtar);
101 matching = bsdtar->matching;
102 add_pattern(bsdtar, &(matching->inclusions), pattern);
103 matching->inclusions_count++;
104 matching->inclusions_unmatched_count++;
109 include_from_file(struct bsdtar *bsdtar, const char *pathname)
111 return (process_lines(bsdtar, pathname, &include));
115 add_pattern(struct bsdtar *bsdtar, struct match **list, const char *pattern)
119 match = malloc(sizeof(*match) + strlen(pattern) + 1);
121 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
122 strcpy(match->pattern, pattern);
123 /* Both "foo/" and "foo" should match "foo/bar". */
124 if (match->pattern[strlen(match->pattern)-1] == '/')
125 match->pattern[strlen(match->pattern)-1] = '\0';
133 excluded(struct bsdtar *bsdtar, const char *pathname)
135 struct matching *matching;
137 struct match *matched;
139 matching = bsdtar->matching;
140 if (matching == NULL)
143 /* Exclusions take priority */
144 for (match = matching->exclusions; match != NULL; match = match->next){
145 if (match_exclusion(match, pathname))
149 /* Then check for inclusions */
151 for (match = matching->inclusions; match != NULL; match = match->next){
152 if (match_inclusion(match, pathname)) {
154 * If this pattern has never been matched,
157 if (match->matches == 0) {
159 matching->inclusions_unmatched_count--;
163 * Otherwise, remember the match but keep checking
164 * in case we can tick off an unmatched pattern.
170 * We didn't find a pattern that had never been matched, but
171 * we did find a match, so count it and exit.
173 if (matched != NULL) {
178 /* If there were inclusions, default is to exclude. */
179 if (matching->inclusions != NULL)
182 /* No explicit inclusions, default is to match. */
187 * This is a little odd, but it matches the default behavior of
188 * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar'
192 match_exclusion(struct match *match, const char *pathname)
196 if (*match->pattern == '*' || *match->pattern == '/')
197 return (pathmatch(match->pattern, pathname) == 0);
199 for (p = pathname; p != NULL; p = strchr(p, '/')) {
202 if (pathmatch(match->pattern, p) == 0)
209 * Again, mimic gtar: inclusions are always anchored (have to match
210 * the beginning of the path) even though exclusions are not anchored.
213 match_inclusion(struct match *match, const char *pathname)
215 return (pathmatch(match->pattern, pathname) == 0);
219 cleanup_exclusions(struct bsdtar *bsdtar)
223 if (bsdtar->matching) {
224 p = bsdtar->matching->inclusions;
230 p = bsdtar->matching->exclusions;
236 free(bsdtar->matching);
241 initialize_matching(struct bsdtar *bsdtar)
243 bsdtar->matching = malloc(sizeof(*bsdtar->matching));
244 if (bsdtar->matching == NULL)
245 bsdtar_errc(bsdtar, 1, errno, "No memory");
246 memset(bsdtar->matching, 0, sizeof(*bsdtar->matching));
250 unmatched_inclusions(struct bsdtar *bsdtar)
252 struct matching *matching;
254 matching = bsdtar->matching;
255 if (matching == NULL)
257 return (matching->inclusions_unmatched_count);
262 unmatched_inclusions_warn(struct bsdtar *bsdtar, const char *msg)
264 struct matching *matching;
267 matching = bsdtar->matching;
268 if (matching == NULL)
271 p = matching->inclusions;
273 if (p->matches == 0) {
274 bsdtar->return_value = 1;
275 bsdtar_warnc(bsdtar, 0, "%s: %s",
280 return (matching->inclusions_unmatched_count);
284 * TODO: Extend this so that the following matches work:
285 * "foo//bar" == "foo/bar"
286 * "foo/./bar" == "foo/bar"
289 * The POSIX fnmatch() function doesn't handle any of these, but
290 * all are common situations that arise when paths are generated within
291 * large scripts. E.g., the following is quite common:
292 * MYPATH=foo/ TARGET=$MYPATH/bar
293 * It may be worthwhile to edit such paths at write time as well,
294 * especially when such editing may avoid the need for long pathname
298 pathmatch(const char *pattern, const char *string)
301 * Strip leading "./" or ".//" so that, e.g.,
302 * "foo" matches "./foo". In particular, this
303 * opens up an optimization for the writer to
304 * elide leading "./".
306 if (pattern[0] == '.' && pattern[1] == '/') {
308 while (pattern[0] == '/')
311 if (string[0] == '.' && string[1] == '/') {
313 while (string[0] == '/')
316 return (bsdtar_fnmatch(pattern, string));
320 #if defined(HAVE_FNMATCH) && defined(HAVE_FNM_LEADING_DIR)
322 /* Use system fnmatch() if it suits our needs. */
323 /* On Linux, _GNU_SOURCE must be defined to get FNM_LEADING_DIR. */
327 bsdtar_fnmatch(const char *pattern, const char *string)
329 return (fnmatch(pattern, string, FNM_LEADING_DIR));
334 * The following was hacked from BSD C library
335 * code: src/lib/libc/gen/fnmatch.c,v 1.15 2002/02/01
337 * In particular, most of the flags were ripped out: this always
338 * behaves like FNM_LEADING_DIR is set and other flags specified
339 * by POSIX are unset.
341 * Normally, I would not conditionally compile something like this: If
342 * I have to support it anyway, everyone may as well use it. ;-)
343 * However, the full POSIX spec for fnmatch() includes a lot of
344 * advanced character handling that I'm not ready to put in here, so
345 * it's probably best if people use a local version when it's available.
349 * Copyright (c) 1989, 1993, 1994
350 * The Regents of the University of California. All rights reserved.
352 * This code is derived from software contributed to Berkeley by
355 * Redistribution and use in source and binary forms, with or without
356 * modification, are permitted provided that the following conditions
358 * 1. Redistributions of source code must retain the above copyright
359 * notice, this list of conditions and the following disclaimer.
360 * 2. Redistributions in binary form must reproduce the above copyright
361 * notice, this list of conditions and the following disclaimer in the
362 * documentation and/or other materials provided with the distribution.
363 * 4. Neither the name of the University nor the names of its contributors
364 * may be used to endorse or promote products derived from this software
365 * without specific prior written permission.
367 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
368 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
369 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
370 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
371 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
372 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
373 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
374 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
375 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
376 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381 bsdtar_fnmatch(const char *pattern, const char *string)
383 const char *saved_pattern;
388 switch (c = *pattern++) {
390 if (*string == '/' || *string == '\0')
400 /* Collapse multiple stars. */
404 /* Optimize for pattern with * at end. */
408 /* General case, use recursion. */
409 while (*string != '\0') {
410 if (!bsdtar_fnmatch(pattern, string))
418 saved_pattern = pattern;
419 if (*pattern == '!' || *pattern == '^') {
430 pattern = saved_pattern;
434 if (*pattern == '-') {
435 char c2 = *(pattern + 1);
437 pattern = saved_pattern;
442 /* [a-] is not a range. */
453 } else if (c == *string)
457 if (matched == negate)
462 if ((c = *pattern++) == '\0') {