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: src/usr.bin/tar/matching.c,v 1.9 2005/03/14 00:30:35 kientzle Exp $");
43 struct match
*exclusions
;
45 struct match
*inclusions
;
47 int inclusions_unmatched_count
;
51 static void add_pattern(struct bsdtar
*, struct match
**list
,
53 static int bsdtar_fnmatch(const char *p
, const char *s
);
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'
188 match_exclusion(struct match
*match
, const char *pathname
)
192 if (*match
->pattern
== '*' || *match
->pattern
== '/')
193 return (bsdtar_fnmatch(match
->pattern
, pathname
) == 0);
195 for (p
= pathname
; p
!= NULL
; p
= strchr(p
, '/')) {
198 if (bsdtar_fnmatch(match
->pattern
, p
) == 0)
205 * Again, mimic gtar: inclusions are always anchored (have to match
206 * the beginning of the path) even though exclusions are not anchored.
209 match_inclusion(struct match
*match
, const char *pathname
)
211 return (bsdtar_fnmatch(match
->pattern
, pathname
) == 0);
215 cleanup_exclusions(struct bsdtar
*bsdtar
)
219 if (bsdtar
->matching
) {
220 p
= bsdtar
->matching
->inclusions
;
226 p
= bsdtar
->matching
->exclusions
;
232 free(bsdtar
->matching
);
237 initialize_matching(struct bsdtar
*bsdtar
)
239 bsdtar
->matching
= malloc(sizeof(*bsdtar
->matching
));
240 if (bsdtar
->matching
== NULL
)
241 bsdtar_errc(bsdtar
, 1, errno
, "No memory");
242 memset(bsdtar
->matching
, 0, sizeof(*bsdtar
->matching
));
246 unmatched_inclusions(struct bsdtar
*bsdtar
)
248 struct matching
*matching
;
250 matching
= bsdtar
->matching
;
251 if (matching
== NULL
)
253 return (matching
->inclusions_unmatched_count
);
258 #if defined(HAVE_FNMATCH) && defined(HAVE_FNM_LEADING_DIR)
260 /* Use system fnmatch() if it suits our needs. */
261 /* On Linux, _GNU_SOURCE must be defined to get FNM_LEADING_DIR. */
265 bsdtar_fnmatch(const char *pattern
, const char *string
)
267 return (fnmatch(pattern
, string
, FNM_LEADING_DIR
));
272 * The following was hacked from BSD C library
273 * code: src/lib/libc/gen/fnmatch.c,v 1.15 2002/02/01
275 * In particular, most of the flags were ripped out: this always
276 * behaves like FNM_LEADING_DIR is set and other flags specified
277 * by POSIX are unset.
279 * Normally, I would not conditionally compile something like this: If
280 * I have to support it anyway, everyone may as well use it. ;-)
281 * However, the full POSIX spec for fnmatch() includes a lot of
282 * advanced character handling that I'm not ready to put in here, so
283 * it's probably best if people use a local version when it's available.
287 * Copyright (c) 1989, 1993, 1994
288 * The Regents of the University of California. All rights reserved.
290 * This code is derived from software contributed to Berkeley by
293 * Redistribution and use in source and binary forms, with or without
294 * modification, are permitted provided that the following conditions
296 * 1. Redistributions of source code must retain the above copyright
297 * notice, this list of conditions and the following disclaimer.
298 * 2. Redistributions in binary form must reproduce the above copyright
299 * notice, this list of conditions and the following disclaimer in the
300 * documentation and/or other materials provided with the distribution.
301 * 4. Neither the name of the University nor the names of its contributors
302 * may be used to endorse or promote products derived from this software
303 * without specific prior written permission.
305 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
306 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
307 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
308 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
309 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
310 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
312 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
313 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319 bsdtar_fnmatch(const char *pattern
, const char *string
)
321 const char *saved_pattern
;
326 switch (c
= *pattern
++) {
328 if (*string
== '/' || *string
== '\0')
338 /* Collapse multiple stars. */
342 /* Optimize for pattern with * at end. */
346 /* General case, use recursion. */
347 while (*string
!= '\0') {
348 if (!bsdtar_fnmatch(pattern
, string
))
356 saved_pattern
= pattern
;
357 if (*pattern
== '!' || *pattern
== '^') {
368 pattern
= saved_pattern
;
372 if (*pattern
== '-') {
373 char c2
= *(pattern
+ 1);
375 pattern
= saved_pattern
;
380 /* [a-] is not a range. */
391 } else if (c
== *string
)
395 if (matched
== negate
)
400 if ((c
= *pattern
++) == '\0') {