Import libarchive-2.5.5.
[dragonfly.git] / contrib / libarchive-2 / cpio / matching.c
blob3e4ddc3bfdb522bb728974b0060fac7658ee6715
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
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.
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 $");
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #ifdef HAVE_STDLIB_H
33 #include <stdlib.h>
34 #endif
35 #ifdef HAVE_STRING_H
36 #include <string.h>
37 #endif
39 #include "matching.h"
40 #include "pathmatch.h"
42 struct match {
43 struct match *next;
44 int matches;
45 char pattern[1];
48 struct matching {
49 struct match *exclusions;
50 int exclusions_count;
51 struct match *inclusions;
52 int inclusions_count;
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
72 int
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++;
82 return (0);
85 #if 0
86 int
87 exclude_from_file(struct cpio *cpio, const char *pathname)
89 return (process_lines(cpio, pathname, &exclude));
91 #endif
93 int
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++;
104 return (0);
108 include_from_file(struct cpio *cpio, const char *pathname)
110 struct line_reader *lr;
111 const char *p;
112 int ret = 0;
114 lr = process_lines_init(pathname, '\n');
115 while ((p = process_lines_next(lr)) != NULL)
116 if (include(cpio, p) != 0)
117 ret = -1;
118 process_lines_free(lr);
119 return (ret);
122 static void
123 add_pattern(struct match **list, const char *pattern)
125 struct match *match;
127 match = malloc(sizeof(*match) + strlen(pattern) + 1);
128 if (match == NULL)
129 cpio_errc(1, errno, "Out of memory");
130 if (pattern[0] == '/')
131 pattern++;
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';
136 match->next = *list;
137 *list = match;
138 match->matches = 0;
143 excluded(struct cpio *cpio, const char *pathname)
145 struct matching *matching;
146 struct match *match;
147 struct match *matched;
149 matching = cpio->matching;
150 if (matching == NULL)
151 return (0);
153 /* Exclusions take priority */
154 for (match = matching->exclusions; match != NULL; match = match->next){
155 if (match_exclusion(match, pathname))
156 return (1);
159 /* Then check for inclusions */
160 matched = NULL;
161 for (match = matching->inclusions; match != NULL; match = match->next){
162 if (match_inclusion(match, pathname)) {
164 * If this pattern has never been matched,
165 * then we're done.
167 if (match->matches == 0) {
168 match->matches++;
169 matching->inclusions_unmatched_count++;
170 return (0);
173 * Otherwise, remember the match but keep checking
174 * in case we can tick off an unmatched pattern.
176 matched = match;
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) {
184 matched->matches++;
185 return (0);
188 /* If there were inclusions, default is to exclude. */
189 if (matching->inclusions != NULL)
190 return (1);
192 /* No explicit inclusions, default is to match. */
193 return (0);
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,
205 pathname,
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));
219 void
220 cleanup_exclusions(struct cpio *cpio)
222 struct match *p, *q;
224 if (cpio->matching) {
225 p = cpio->matching->inclusions;
226 while (p != NULL) {
227 q = p;
228 p = p->next;
229 free(q);
231 p = cpio->matching->exclusions;
232 while (p != NULL) {
233 q = p;
234 p = p->next;
235 free(q);
237 free(cpio->matching);
241 static void
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)
257 return (0);
258 return (matching->inclusions_unmatched_count);