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$");
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 return (process_lines(cpio, pathname, &include));
114 add_pattern(struct match **list, const char *pattern)
118 match = malloc(sizeof(*match) + strlen(pattern) + 1);
120 cpio_errc(1, errno, "Out of memory");
121 if (pattern[0] == '/')
123 strcpy(match->pattern, pattern);
124 /* Both "foo/" and "foo" should match "foo/bar". */
125 if (match->pattern[strlen(match->pattern)-1] == '/')
126 match->pattern[strlen(match->pattern)-1] = '\0';
134 excluded(struct cpio *cpio, const char *pathname)
136 struct matching *matching;
138 struct match *matched;
140 matching = cpio->matching;
141 if (matching == NULL)
144 /* Exclusions take priority */
145 for (match = matching->exclusions; match != NULL; match = match->next){
146 if (match_exclusion(match, pathname))
150 /* Then check for inclusions */
152 for (match = matching->inclusions; match != NULL; match = match->next){
153 if (match_inclusion(match, pathname)) {
155 * If this pattern has never been matched,
158 if (match->matches == 0) {
160 matching->inclusions_unmatched_count++;
164 * Otherwise, remember the match but keep checking
165 * in case we can tick off an unmatched pattern.
171 * We didn't find a pattern that had never been matched, but
172 * we did find a match, so count it and exit.
174 if (matched != NULL) {
179 /* If there were inclusions, default is to exclude. */
180 if (matching->inclusions != NULL)
183 /* No explicit inclusions, default is to match. */
188 * This is a little odd, but it matches the default behavior of
189 * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar'
193 match_exclusion(struct match *match, const char *pathname)
195 return (pathmatch(match->pattern,
197 PATHMATCH_NO_ANCHOR_START | PATHMATCH_NO_ANCHOR_END));
201 * Again, mimic gtar: inclusions are always anchored (have to match
202 * the beginning of the path) even though exclusions are not anchored.
205 match_inclusion(struct match *match, const char *pathname)
207 return (pathmatch(match->pattern, pathname, 0));
211 cleanup_exclusions(struct cpio *cpio)
215 if (cpio->matching) {
216 p = cpio->matching->inclusions;
222 p = cpio->matching->exclusions;
228 free(cpio->matching);
233 initialize_matching(struct cpio *cpio)
235 cpio->matching = malloc(sizeof(*cpio->matching));
236 if (cpio->matching == NULL)
237 cpio_errc(1, errno, "No memory");
238 memset(cpio->matching, 0, sizeof(*cpio->matching));
242 unmatched_inclusions(struct cpio *cpio)
244 struct matching *matching;
246 matching = cpio->matching;
247 if (matching == NULL)
249 return (matching->inclusions_unmatched_count);