]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/cpio/matching.c
- Associate myself with ncurses
[FreeBSD/FreeBSD.git] / usr.bin / 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$");
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         return (process_lines(cpio, pathname, &include));
111 }
112
113 static void
114 add_pattern(struct match **list, const char *pattern)
115 {
116         struct match *match;
117
118         match = malloc(sizeof(*match) + strlen(pattern) + 1);
119         if (match == NULL)
120                 cpio_errc(1, errno, "Out of memory");
121         if (pattern[0] == '/')
122                 pattern++;
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';
127         match->next = *list;
128         *list = match;
129         match->matches = 0;
130 }
131
132
133 int
134 excluded(struct cpio *cpio, const char *pathname)
135 {
136         struct matching *matching;
137         struct match *match;
138         struct match *matched;
139
140         matching = cpio->matching;
141         if (matching == NULL)
142                 return (0);
143
144         /* Exclusions take priority */
145         for (match = matching->exclusions; match != NULL; match = match->next){
146                 if (match_exclusion(match, pathname))
147                         return (1);
148         }
149
150         /* Then check for inclusions */
151         matched = NULL;
152         for (match = matching->inclusions; match != NULL; match = match->next){
153                 if (match_inclusion(match, pathname)) {
154                         /*
155                          * If this pattern has never been matched,
156                          * then we're done.
157                          */
158                         if (match->matches == 0) {
159                                 match->matches++;
160                                 matching->inclusions_unmatched_count++;
161                                 return (0);
162                         }
163                         /*
164                          * Otherwise, remember the match but keep checking
165                          * in case we can tick off an unmatched pattern.
166                          */
167                         matched = match;
168                 }
169         }
170         /*
171          * We didn't find a pattern that had never been matched, but
172          * we did find a match, so count it and exit.
173          */
174         if (matched != NULL) {
175                 matched->matches++;
176                 return (0);
177         }
178
179         /* If there were inclusions, default is to exclude. */
180         if (matching->inclusions != NULL)
181             return (1);
182
183         /* No explicit inclusions, default is to match. */
184         return (0);
185 }
186
187 /*
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'
190  *
191  */
192 int
193 match_exclusion(struct match *match, const char *pathname)
194 {
195         return (pathmatch(match->pattern,
196                     pathname,
197                     PATHMATCH_NO_ANCHOR_START | PATHMATCH_NO_ANCHOR_END));
198 }
199
200 /*
201  * Again, mimic gtar:  inclusions are always anchored (have to match
202  * the beginning of the path) even though exclusions are not anchored.
203  */
204 int
205 match_inclusion(struct match *match, const char *pathname)
206 {
207         return (pathmatch(match->pattern, pathname, 0));
208 }
209
210 void
211 cleanup_exclusions(struct cpio *cpio)
212 {
213         struct match *p, *q;
214
215         if (cpio->matching) {
216                 p = cpio->matching->inclusions;
217                 while (p != NULL) {
218                         q = p;
219                         p = p->next;
220                         free(q);
221                 }
222                 p = cpio->matching->exclusions;
223                 while (p != NULL) {
224                         q = p;
225                         p = p->next;
226                         free(q);
227                 }
228                 free(cpio->matching);
229         }
230 }
231
232 static void
233 initialize_matching(struct cpio *cpio)
234 {
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));
239 }
240
241 int
242 unmatched_inclusions(struct cpio *cpio)
243 {
244         struct matching *matching;
245
246         matching = cpio->matching;
247         if (matching == NULL)
248                 return (0);
249         return (matching->inclusions_unmatched_count);
250 }