2 * Copyright (c) 2003-2004 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 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "bsdtar_platform.h"
28 __FBSDID("$FreeBSD$");
44 struct match *exclusions;
46 struct match *inclusions;
48 int inclusions_unmatched_count;
52 static void add_pattern(struct bsdtar *, struct match **list,
54 static void initialize_matching(struct bsdtar *);
55 static int match_exclusion(struct match *, const char *pathname);
56 static int match_inclusion(struct match *, const char *pathname);
59 * The matching logic here needs to be re-thought. I started out to
60 * try to mimic gtar's matching logic, but it's not entirely
61 * consistent. In particular 'tar -t' and 'tar -x' interpret patterns
62 * on the command line as anchored, but --exclude doesn't.
66 * Utility functions to manage exclusion/inclusion patterns
70 exclude(struct bsdtar *bsdtar, const char *pattern)
72 struct matching *matching;
74 if (bsdtar->matching == NULL)
75 initialize_matching(bsdtar);
76 matching = bsdtar->matching;
77 add_pattern(bsdtar, &(matching->exclusions), pattern);
78 matching->exclusions_count++;
83 exclude_from_file(struct bsdtar *bsdtar, const char *pathname)
85 return (process_lines(bsdtar, pathname, &exclude));
89 include(struct bsdtar *bsdtar, const char *pattern)
91 struct matching *matching;
93 if (bsdtar->matching == NULL)
94 initialize_matching(bsdtar);
95 matching = bsdtar->matching;
96 add_pattern(bsdtar, &(matching->inclusions), pattern);
97 matching->inclusions_count++;
98 matching->inclusions_unmatched_count++;
103 include_from_file(struct bsdtar *bsdtar, const char *pathname)
105 return (process_lines(bsdtar, pathname, &include));
109 add_pattern(struct bsdtar *bsdtar, struct match **list, const char *pattern)
113 match = malloc(sizeof(*match) + strlen(pattern) + 1);
115 bsdtar_errc(bsdtar, 1, errno, "Out of memory");
116 if (pattern[0] == '/')
118 strcpy(match->pattern, pattern);
119 /* Both "foo/" and "foo" should match "foo/bar". */
120 if (match->pattern[strlen(match->pattern)-1] == '/')
121 match->pattern[strlen(match->pattern)-1] = '\0';
129 excluded(struct bsdtar *bsdtar, const char *pathname)
131 struct matching *matching;
133 struct match *matched;
135 matching = bsdtar->matching;
136 if (matching == NULL)
139 /* Exclusions take priority */
140 for (match = matching->exclusions; match != NULL; match = match->next){
141 if (match_exclusion(match, pathname))
145 /* Then check for inclusions */
147 for (match = matching->inclusions; match != NULL; match = match->next){
148 if (match_inclusion(match, pathname)) {
150 * If this pattern has never been matched,
153 if (match->matches == 0) {
155 matching->inclusions_unmatched_count++;
159 * Otherwise, remember the match but keep checking
160 * in case we can tick off an unmatched pattern.
166 * We didn't find a pattern that had never been matched, but
167 * we did find a match, so count it and exit.
169 if (matched != NULL) {
174 /* If there were inclusions, default is to exclude. */
175 if (matching->inclusions != NULL)
178 /* No explicit inclusions, default is to match. */
183 * This is a little odd, but it matches the default behavior of
184 * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar'
186 * XXX TODO: fnmatch isn't the most portable thing around, and even
187 * worse, FNM_LEADING_DIR is a non-POSIX extension. <sigh> Thus, the
188 * following two functions need to eventually be replaced with code
189 * that does not rely on fnmatch().
192 match_exclusion(struct match *match, const char *pathname)
196 if (*match->pattern == '*' || *match->pattern == '/')
197 return (fnmatch(match->pattern, pathname, FNM_LEADING_DIR) == 0);
199 for (p = pathname; p != NULL; p = strchr(p, '/')) {
202 if (fnmatch(match->pattern, p, FNM_LEADING_DIR) == 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 (fnmatch(match->pattern, pathname, FNM_LEADING_DIR) == 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);