File Coverage

src/match_engine.c
Criterion Covered Total %
statement 81 95 85.2
branch 48 62 77.4
condition n/a
subroutine n/a
pod n/a
total 129 157 82.1


line stmt bran cond sub pod time code
1             /*-
2             * Copyright (c) 2003-2007 Tim Kientzle
3             * All rights reserved.
4             *
5             * Redistribution and use in source and binary forms, with or without
6             * modification, are permitted provided that the following conditions
7             * are met:
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.
13             *
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.
24             */
25              
26             /*
27             * Copyright (c) 2012, cPanel, Inc.
28             * All rights reserved.
29             * http://cpanel.net/
30             *
31             * This is free software; you can redistribute it and/or modify it under the
32             * same terms as Perl itself. See the Perl manual section 'perlartistic' for
33             * further information.
34             *
35             * Modified for use in Archive::Tar::Builder.
36             */
37              
38             #include
39             #include
40             #include
41              
42             #include "match_line_reader.h"
43             #include "match_engine.h"
44             #include "match_path.h"
45              
46             struct match {
47             struct match * next;
48             int matches;
49             char pattern[1];
50             };
51              
52             struct lafe_matching {
53             struct match * exclusions;
54             int exclusions_count;
55             struct match * inclusions;
56             int inclusions_count;
57             int inclusions_unmatched_count;
58             };
59              
60             static int add_pattern(struct match **list, const char *pattern);
61             static struct lafe_matching ** initialize_matching(struct lafe_matching **);
62             static int match_exclusion(struct match *, const char *pathname);
63             static int match_inclusion(struct match *, const char *pathname);
64              
65             /*
66             * The matching logic here needs to be re-thought. I started out to
67             * try to mimic gtar's matching logic, but it's not entirely
68             * consistent. In particular 'tar -t' and 'tar -x' interpret patterns
69             * on the command line as anchored, but --exclude doesn't.
70             */
71              
72             /*
73             * Utility functions to manage exclusion/inclusion patterns
74             */
75              
76             int
77 49           lafe_exclude(struct lafe_matching **matching, const char *pattern)
78             {
79              
80 49 100         if (*matching == NULL) {
81 14           initialize_matching(matching);
82             }
83              
84 49 50         if (add_pattern(&((*matching)->exclusions), pattern) < 0) {
85 0           return -1;
86             }
87              
88 49           (*matching)->exclusions_count++;
89              
90 49           return 0;
91             }
92              
93             int
94 21           lafe_exclude_from_file(struct lafe_matching **matching, const char *pathname)
95             {
96             struct lafe_line_reader *lr;
97             const char *p;
98 21           int ret = 0;
99              
100 21 100         if ((lr = lafe_line_reader(pathname, 0)) == NULL) {
101 7           return -1;
102             }
103              
104 49 50         while (lafe_line_reader_next(lr, &p) == 0) {
105 49 100         if (p == NULL) {
106 14           break;
107             }
108              
109 35 50         if (lafe_exclude(matching, p) != 0) {
110 0           ret = -1;
111             }
112             }
113              
114 14           lafe_line_reader_free(lr);
115              
116 21           return ret;
117             }
118              
119             int
120 49           lafe_include(struct lafe_matching **matching, const char *pattern)
121             {
122              
123 49 100         if (*matching == NULL) {
124 14           initialize_matching(matching);
125             }
126              
127 49 50         if (add_pattern(&((*matching)->inclusions), pattern) < 0) {
128 0           return -1;
129             }
130              
131 49           (*matching)->inclusions_count++;
132 49           (*matching)->inclusions_unmatched_count++;
133              
134 49           return 0;
135             }
136              
137             int
138 21           lafe_include_from_file(struct lafe_matching **matching, const char *pathname,
139             int nullSeparator)
140             {
141             struct lafe_line_reader *lr;
142             const char *p;
143 21           int ret = 0;
144              
145 21 100         if ((lr = lafe_line_reader(pathname, nullSeparator)) == NULL) {
146 7           return -1;
147             }
148              
149 49 50         while (lafe_line_reader_next(lr, &p) == 0) {
150 49 100         if (p == NULL) {
151 14           break;
152             }
153              
154 35 50         if (lafe_include(matching, p) != 0) {
155 0           ret = -1;
156             }
157             }
158              
159 14           lafe_line_reader_free(lr);
160              
161 21           return ret;
162             }
163              
164             static int
165 98           add_pattern(struct match **list, const char *pattern)
166             {
167             struct match *match;
168             size_t len;
169              
170 98           len = strlen(pattern);
171 98           match = malloc(sizeof(*match) + len + 1);
172              
173 98 50         if (match == NULL) {
174 0           return -1;
175             }
176              
177 98           strcpy(match->pattern, pattern);
178              
179             /* Both "foo/" and "foo" should match "foo/bar". */
180 98 50         if (len && match->pattern[len - 1] == '/') {
    50          
181 0           match->pattern[len - 1] = '\0';
182             }
183              
184 98           match->next = *list;
185 98           *list = match;
186 98           match->matches = 0;
187              
188 98           return 0;
189             }
190              
191             int
192 322           lafe_excluded(struct lafe_matching *matching, const char *pathname)
193             {
194             struct match *match;
195             struct match *matched;
196              
197 322 50         if (matching == NULL) {
198 0           return 0;
199             }
200              
201             /* Mark off any unmatched inclusions. */
202             /* In particular, if a filename does appear in the archive and
203             * is explicitly included and excluded, then we don't report
204             * it as missing even though we don't extract it.
205             */
206 322           matched = NULL;
207              
208 665 100         for (match = matching->inclusions; match != NULL; match = match->next) {
209 343 100         if (match->matches == 0 && match_inclusion(match, pathname)) {
    100          
210 49           matching->inclusions_unmatched_count--;
211 49           match->matches++;
212 49           matched = match;
213             }
214             }
215              
216             /* Exclusions take priority */
217 770 100         for (match = matching->exclusions; match != NULL; match = match->next){
218 567 100         if (match_exclusion(match, pathname)) {
219 119           return 1;
220             }
221             }
222              
223             /* It's not excluded and we found an inclusion above, so it's included. */
224 203 100         if (matched != NULL) {
225 49           return 0;
226             }
227              
228              
229             /* We didn't find an unmatched inclusion, check the remaining ones. */
230 322 100         for (match = matching->inclusions; match != NULL; match = match->next){
231             /* We looked at previously-unmatched inclusions already. */
232 168 100         if (match->matches > 0 && match_inclusion(match, pathname)) {
    50          
233 0           match->matches++;
234              
235 0           return 0;
236             }
237             }
238              
239             /* If there were inclusions, default is to exclude. */
240 154 100         if (matching->inclusions != NULL) {
241 49           return 1;
242             }
243              
244             /* No explicit inclusions, default is to match. */
245 105           return 0;
246             }
247              
248             /*
249             * This is a little odd, but it matches the default behavior of
250             * gtar. In particular, 'a*b' will match 'foo/a1111/222b/bar'
251             *
252             */
253             static int
254 567           match_exclusion(struct match *match, const char *pathname)
255             {
256 567           return lafe_pathmatch(match->pattern, pathname,
257             PATHMATCH_NO_ANCHOR_START | PATHMATCH_NO_ANCHOR_END
258             );
259             }
260              
261             /*
262             * Again, mimic gtar: inclusions are always anchored (have to match
263             * the beginning of the path) even though exclusions are not anchored.
264             */
265             static int
266 280           match_inclusion(struct match *match, const char *pathname)
267             {
268 280           return lafe_pathmatch(match->pattern, pathname, PATHMATCH_NO_ANCHOR_END);
269             }
270              
271             void
272 92           lafe_cleanup_exclusions(struct lafe_matching **matching)
273             {
274             struct match *p, *q;
275              
276 92 100         if (*matching == NULL) {
277 64           return;
278             }
279              
280 77 100         for (p = (*matching)->inclusions; p != NULL; ) {
281 49           q = p;
282 49           p = p->next;
283              
284 49           free(q);
285             }
286              
287 77 100         for (p = (*matching)->exclusions; p != NULL; ) {
288 49           q = p;
289 49           p = p->next;
290              
291 49           free(q);
292             }
293              
294 28           free(*matching);
295 28           *matching = NULL;
296             }
297              
298             static struct lafe_matching **
299 28           initialize_matching(struct lafe_matching **matching)
300             {
301 28 50         if ((*matching = calloc(sizeof(**matching), 1)) == NULL) {
302 0           return NULL;
303             }
304              
305 28           return matching;
306             }
307              
308             int
309 0           lafe_unmatched_inclusions(struct lafe_matching *matching)
310             {
311              
312 0 0         if (matching == NULL) {
313 0           return 0;
314             }
315              
316 0           return matching->inclusions_unmatched_count;
317             }