Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / libarchive-2 / cpio / matching.c
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 #include "cpio_platform.h"
27 __FBSDID("$FreeBSD: src/usr.bin/cpio/matching.c,v 1.2 2008/06/21 02:20:20 kientzle Exp $");
28
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
38
39 #include "matching.h"
40 #include "pathmatch.h"
41
42 struct match {
43         struct match     *next;
44         int               matches;
45         char              pattern[1];
46 };
47
48 struct matching {
49         struct match     *exclusions;
50         int               exclusions_count;
51         struct match     *inclusions;
52         int               inclusions_count;
53         int               inclusions_unmatched_count;
54 };
55
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);
60
61 /*
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.
66  */
67
68 /*
69  * Utility functions to manage exclusion/inclusion patterns
70  */
71
72 int
73 exclude(struct cpio *cpio, const char *pattern)
74 {
75         struct matching *matching;
76
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);
83 }
84
85 #if 0
86 int
87 exclude_from_file(struct cpio *cpio, const char *pathname)
88 {
89         return (process_lines(cpio, pathname, &exclude));
90 }
91 #endif
92
93 int
94 include(struct cpio *cpio, const char *pattern)
95 {
96         struct matching *matching;
97
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);
105 }
106
107 int
108 include_from_file(struct cpio *cpio, const char *pathname)
109 {
110         struct line_reader *lr;
111         const char *p;
112         int ret = 0;
113
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);
120 }
121
122 static void
123 add_pattern(struct match **list, const char *pattern)
124 {
125         struct match *match;
126
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;
139 }
140
141
142 int
143 excluded(struct cpio *cpio, const char *pathname)
144 {
145         struct matching *matching;
146         struct match *match;
147         struct match *matched;
148
149         matching = cpio->matching;
150         if (matching == NULL)
151                 return (0);
152
153         /* Exclusions take priority */
154         for (match = matching->exclusions; match != NULL; match = match->next){
155                 if (match_exclusion(match, pathname))
156                         return (1);
157         }
158
159         /* Then check for inclusions */
160         matched = NULL;
161         for (match = matching->inclusions; match != NULL; match = match->next){
162                 if (match_inclusion(match, pathname)) {
163                         /*
164                          * If this pattern has never been matched,
165                          * then we're done.
166                          */
167                         if (match->matches == 0) {
168                                 match->matches++;
169                                 matching->inclusions_unmatched_count++;
170                                 return (0);
171                         }
172                         /*
173                          * Otherwise, remember the match but keep checking
174                          * in case we can tick off an unmatched pattern.
175                          */
176                         matched = match;
177                 }
178         }
179         /*
180          * We didn't find a pattern that had never been matched, but
181          * we did find a match, so count it and exit.
182          */
183         if (matched != NULL) {
184                 matched->matches++;
185                 return (0);
186         }
187
188         /* If there were inclusions, default is to exclude. */
189         if (matching->inclusions != NULL)
190             return (1);
191
192         /* No explicit inclusions, default is to match. */
193         return (0);
194 }
195
196 /*
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'
199  *
200  */
201 int
202 match_exclusion(struct match *match, const char *pathname)
203 {
204         return (pathmatch(match->pattern,
205                     pathname,
206                     PATHMATCH_NO_ANCHOR_START | PATHMATCH_NO_ANCHOR_END));
207 }
208
209 /*
210  * Again, mimic gtar:  inclusions are always anchored (have to match
211  * the beginning of the path) even though exclusions are not anchored.
212  */
213 int
214 match_inclusion(struct match *match, const char *pathname)
215 {
216         return (pathmatch(match->pattern, pathname, 0));
217 }
218
219 void
220 cleanup_exclusions(struct cpio *cpio)
221 {
222         struct match *p, *q;
223
224         if (cpio->matching) {
225                 p = cpio->matching->inclusions;
226                 while (p != NULL) {
227                         q = p;
228                         p = p->next;
229                         free(q);
230                 }
231                 p = cpio->matching->exclusions;
232                 while (p != NULL) {
233                         q = p;
234                         p = p->next;
235                         free(q);
236                 }
237                 free(cpio->matching);
238         }
239 }
240
241 static void
242 initialize_matching(struct cpio *cpio)
243 {
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));
248 }
249
250 int
251 unmatched_inclusions(struct cpio *cpio)
252 {
253         struct matching *matching;
254
255         matching = cpio->matching;
256         if (matching == NULL)
257                 return (0);
258         return (matching->inclusions_unmatched_count);
259 }