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 "cpio_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/cpio/matching.c,v 1.2 2008/06/21 02:20:20 kientzle Exp $");
40 #include "pathmatch.h"
49 struct match
*exclusions
;
51 struct match
*inclusions
;
53 int inclusions_unmatched_count
;
56 static void add_pattern(struct match
**list
, const char *pattern
);
57 static void initialize_matching(struct cpio
*);
58 static int match_exclusion(struct match
*, const char *pathname
);
59 static int match_inclusion(struct match
*, const char *pathname
);
62 * The matching logic here needs to be re-thought. I started out to
63 * try to mimic gtar's matching logic, but it's not entirely
64 * consistent. In particular 'tar -t' and 'tar -x' interpret patterns
65 * on the command line as anchored, but --exclude doesn't.
69 * Utility functions to manage exclusion/inclusion patterns
73 exclude(struct cpio
*cpio
, const char *pattern
)
75 struct matching
*matching
;
77 if (cpio
->matching
== NULL
)
78 initialize_matching(cpio
);
79 matching
= cpio
->matching
;
80 add_pattern(&(matching
->exclusions
), pattern
);
81 matching
->exclusions_count
++;
87 exclude_from_file(struct cpio
*cpio
, const char *pathname
)
89 return (process_lines(cpio
, pathname
, &exclude
));
94 include(struct cpio
*cpio
, const char *pattern
)
96 struct matching
*matching
;
98 if (cpio
->matching
== NULL
)
99 initialize_matching(cpio
);
100 matching
= cpio
->matching
;
101 add_pattern(&(matching
->inclusions
), pattern
);
102 matching
->inclusions_count
++;
103 matching
->inclusions_unmatched_count
++;
108 include_from_file(struct cpio
*cpio
, const char *pathname
)
110 struct line_reader
*lr
;
114 lr
= process_lines_init(pathname
, '\n');
115 while ((p
= process_lines_next(lr
)) != NULL
)
116 if (include(cpio
, p
) != 0)
118 process_lines_free(lr
);
123 add_pattern(struct match
**list
, const char *pattern
)
127 match
= malloc(sizeof(*match
) + strlen(pattern
) + 1);
129 cpio_errc(1, errno
, "Out of memory");
130 if (pattern
[0] == '/')
132 strcpy(match
->pattern
, pattern
);
133 /* Both "foo/" and "foo" should match "foo/bar". */
134 if (match
->pattern
[strlen(match
->pattern
)-1] == '/')
135 match
->pattern
[strlen(match
->pattern
)-1] = '\0';
143 excluded(struct cpio
*cpio
, const char *pathname
)
145 struct matching
*matching
;
147 struct match
*matched
;
149 matching
= cpio
->matching
;
150 if (matching
== NULL
)
153 /* Exclusions take priority */
154 for (match
= matching
->exclusions
; match
!= NULL
; match
= match
->next
){
155 if (match_exclusion(match
, pathname
))
159 /* Then check for inclusions */
161 for (match
= matching
->inclusions
; match
!= NULL
; match
= match
->next
){
162 if (match_inclusion(match
, pathname
)) {
164 * If this pattern has never been matched,
167 if (match
->matches
== 0) {
169 matching
->inclusions_unmatched_count
++;
173 * Otherwise, remember the match but keep checking
174 * in case we can tick off an unmatched pattern.
180 * We didn't find a pattern that had never been matched, but
181 * we did find a match, so count it and exit.
183 if (matched
!= NULL
) {
188 /* If there were inclusions, default is to exclude. */
189 if (matching
->inclusions
!= NULL
)
192 /* No explicit inclusions, default is to match. */
197 * This is a little odd, but it matches the default behavior of
198 * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar'
202 match_exclusion(struct match
*match
, const char *pathname
)
204 return (pathmatch(match
->pattern
,
206 PATHMATCH_NO_ANCHOR_START
| PATHMATCH_NO_ANCHOR_END
));
210 * Again, mimic gtar: inclusions are always anchored (have to match
211 * the beginning of the path) even though exclusions are not anchored.
214 match_inclusion(struct match
*match
, const char *pathname
)
216 return (pathmatch(match
->pattern
, pathname
, 0));
220 cleanup_exclusions(struct cpio
*cpio
)
224 if (cpio
->matching
) {
225 p
= cpio
->matching
->inclusions
;
231 p
= cpio
->matching
->exclusions
;
237 free(cpio
->matching
);
242 initialize_matching(struct cpio
*cpio
)
244 cpio
->matching
= malloc(sizeof(*cpio
->matching
));
245 if (cpio
->matching
== NULL
)
246 cpio_errc(1, errno
, "No memory");
247 memset(cpio
->matching
, 0, sizeof(*cpio
->matching
));
251 unmatched_inclusions(struct cpio
*cpio
)
253 struct matching
*matching
;
255 matching
= cpio
->matching
;
256 if (matching
== NULL
)
258 return (matching
->inclusions_unmatched_count
);