]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/match.c
318781ddb924c3f7d633439655a9cbc58af4bc57
[bacula/bacula] / bacula / src / findlib / match.c
1 /*
2  *  Routines used to keep and match include and exclude
3  *   filename/pathname patterns.
4  *
5  *   Kern E. Sibbald, December MMI
6  *
7  */
8 /*
9    Copyright (C) 2001, 2002 Kern Sibbald and John Walker
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License as
13    published by the Free Software Foundation; either version 2 of
14    the License, or (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19    General Public License for more details.
20
21    You should have received a copy of the GNU General Public
22    License along with this program; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
24    MA 02111-1307, USA.
25
26  */
27
28 #include "bacula.h"
29 #include "find.h"
30
31 #include <pwd.h>
32 #include <grp.h>
33 #include <sys/types.h>
34
35 #ifndef FNM_LEADING_DIR
36 #define FNM_LEADING_DIR 0
37 #endif
38
39 #undef bmalloc
40 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
41
42 #ifdef HAVE_CYGWIN
43 static int win32_client = 1;
44 #else
45 static int win32_client = 0;
46 #endif
47
48        
49 /*
50  * Initialize structures for filename matching
51  */
52 void init_include_exclude_files(FF_PKT *ff)
53 {
54 }
55
56 /*
57  * Done doing filename matching, release all 
58  *  resources used.
59  */
60 void term_include_exclude_files(FF_PKT *ff)
61 {
62    struct s_included_file *inc, *next_inc;
63    struct s_excluded_file *exc, *next_exc;
64
65    for (inc=ff->included_files_list; inc; ) {
66       next_inc = inc->next;
67       free(inc);
68       inc = next_inc;
69    }
70
71    for (exc=ff->excluded_files_list; exc; ) {
72       next_exc = exc->next;
73       free(exc);
74       exc = next_exc;
75    }
76
77    for (exc=ff->excluded_paths_list; exc; ) {
78       next_exc = exc->next;
79       free(exc);
80       exc = next_exc;
81    }
82    
83 }
84
85 /*
86  * Add a filename to list of included files
87  */
88 void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *fname)
89 {
90    int len, j;
91    struct s_included_file *inc;
92    char *p;
93
94    len = strlen(fname);
95
96    inc =(struct s_included_file *) bmalloc(sizeof(struct s_included_file) + len + 1);
97    inc->next = ff->included_files_list;
98    inc->options = 0;
99    inc->VerifyOpts[0] = 'V'; 
100    inc->VerifyOpts[1] = ':';
101    inc->VerifyOpts[2] = 0;
102
103    /* prefixed = preceded with options */
104    if (prefixed) {
105       for (p=fname; *p && *p != ' '; p++) {
106          switch (*p) {
107             case '0':                  /* no option */
108                break;
109             case 'M':                  /* MD5 */
110                inc->options |= OPT_compute_MD5;
111                break;
112             case 'Z':                  /* gzip compression */
113                inc->options |= OPT_GZIP_compression;
114                inc->level = *++p - '0';
115                Dmsg1(200, "Compression level=%d\n", inc->level);
116                break;
117             case 'h':                  /* no recursion */
118                inc->options |= OPT_no_recursion;
119                break;
120             case 'f':
121                inc->options |= OPT_multifs;
122                break;
123             case 's':
124                inc->options |= OPT_sparse;
125                break;
126             case 'V':                  /* verify options */
127                /* Copy Verify Options */
128                for (j=0; *p && *p != ':'; p++) {
129                   inc->VerifyOpts[j] = *p;
130                   if (j < (int)sizeof(inc->VerifyOpts) - 1) {
131                      j++;
132                   }
133                }
134                inc->VerifyOpts[j] = 0;
135                break;
136             default:
137                Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *p);
138                break;
139          }
140       }
141       /* Skip past space(s) */
142       for ( ; *p == ' '; p++)
143          {}
144    } else {
145       p = fname;
146    }
147
148    strcpy(inc->fname, p);                 
149    len = strlen(p);
150    /* Zap trailing slashes.  */
151    p += len - 1;
152    while (p > inc->fname && *p == '/') {
153       *p-- = 0;
154       len--;
155    }
156    inc->len = len;
157    /* Check for wild cards */
158    inc->pattern = 0;
159    for (p=inc->fname; *p; p++) {
160       if (*p == '*' || *p == '[' || *p == '?') {
161          inc->pattern = 1;
162          break;
163       }
164    }
165    ff->included_files_list = inc;
166    Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
167 }
168
169 /*
170  * We add an exclude name to either the exclude path
171  *  list or the exclude filename list.
172  */
173 void add_fname_to_exclude_list(FF_PKT *ff, char *fname)
174 {
175    int len;
176    struct s_excluded_file *exc, **list;
177
178    Dmsg1(20, "Add name to exclude: %s\n", fname);
179
180    if (strchr(fname, '/')) {
181       list = &ff->excluded_paths_list;
182    } else {
183       list = &ff->excluded_files_list;
184    }
185   
186    len = strlen(fname);
187
188    exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
189    exc->next = *list;
190    exc->len = len;
191    strcpy(exc->fname, fname);                 
192    *list = exc;
193 }
194
195
196 /*
197  * Get next included file
198  */
199 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
200 {
201    struct s_included_file *inc;
202
203    if (ainc == NULL) { 
204       inc = ff->included_files_list;
205    } else {
206       inc = ainc->next;
207    }
208    if (inc) {
209       ff->flags = inc->options;
210       if (inc->options & OPT_compute_MD5) {
211          ff->compute_MD5 = 1;
212       } else {
213          ff->compute_MD5 = 0;
214       }
215       if (inc->options & OPT_GZIP_compression) {
216          ff->GZIP_compression = 1;
217          ff->GZIP_level = inc->level;
218       } else {
219          ff->GZIP_compression = 0;
220       }
221       if (inc->options & OPT_no_recursion) {
222          ff->no_recursion = 1;
223       } else {
224          ff->no_recursion = 0;
225       }
226       if (inc->options & OPT_multifs) {
227          ff->one_file_system = 0;
228       } else {
229          ff->one_file_system = 1;
230       }
231    }
232    return inc;
233 }
234
235 /*
236  * Walk through the included list to see if this
237  *  file is included possibly with wild-cards.
238  */
239
240 int file_is_included(FF_PKT *ff, char *file)
241 {
242    struct s_included_file *inc = ff->included_files_list;
243    int len;
244
245    for ( ; inc; inc=inc->next ) {
246       if (inc->pattern) {
247          if (fnmatch(inc->fname, file, FNM_LEADING_DIR) == 0) {
248             return 1;
249          }
250          continue;
251       }                             
252       /*
253        * No wild cards. We accept a match to the
254        *  end of any component.
255        */
256       Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
257       len = strlen(file);
258       if (inc->len == len && strcmp(inc->fname, file) == 0) {
259          return 1;
260       }
261       if (inc->len < len && file[inc->len] == '/' && 
262           strncmp(inc->fname, file, inc->len) == 0) {
263          return 1;
264       }
265       if (inc->len == 1 && inc->fname[0] == '/') {
266          return 1;
267       }
268    }
269    return 0;
270 }
271
272
273 /*
274  * This is the workhorse of excluded_file().
275  * Determine if the file is excluded or not.
276  */
277 static int
278 file_in_excluded_list(struct s_excluded_file *exc, char *file)
279 {
280    if (exc == NULL) {
281       Dmsg0(900, "exc is NULL\n");
282    }
283    for ( ; exc; exc=exc->next ) {
284       if (fnmatch(exc->fname, file, FNM_PATHNAME) == 0) {
285          Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
286          return 1;
287       }
288       Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
289    }
290    return 0;
291 }
292
293
294 /*
295  * Walk through the excluded lists to see if this
296  *  file is excluded, or if it matches a component
297  *  of an excluded directory.
298  */
299
300 int file_is_excluded(FF_PKT *ff, char *file)
301 {
302    char *p;
303
304    if (win32_client && file[1] == ':') {
305       file += 2;
306    }
307
308    if (file_in_excluded_list(ff->excluded_paths_list, file)) {
309       return 1;
310    }
311
312    /* Try each component */
313    for (p = file; *p; p++) {
314       /* Match from the beginning of a component only */
315       if ((p == file || (*p != '/' && *(p-1) == '/'))
316            && file_in_excluded_list(ff->excluded_files_list, p)) {
317          return 1;
318       }
319    }
320    return 0;
321 }