]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/match.c
Important protocol change -- see kes29Oct02
[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 'a':                 /* alway replace */
108             case '0':                 /* no option */
109                break;
110             case 'w':
111                inc->options |= OPT_replace_if_newer;
112                break;
113             case 'n':
114                inc->options |= OPT_never_replace;
115                break;
116             case 'M':                 /* MD5 */
117                inc->options |= OPT_compute_MD5;
118                break;
119             case 'Z':                 /* gzip compression */
120                inc->options |= OPT_GZIP_compression;
121                inc->level = *++p - '0';
122                Dmsg1(200, "Compression level=%d\n", inc->level);
123                break;
124             case 'h':                 /* no recursion */
125                inc->options |= OPT_no_recursion;
126                break;
127             case 'f':
128                inc->options |= OPT_multifs;
129                break;
130             case 's':
131                inc->options |= OPT_sparse;
132                break;
133             case 'V':                  /* verify options */
134                /* Copy Verify Options */
135                for (j=0; *p && *p != ':'; p++) {
136                   inc->VerifyOpts[j] = *p;
137                   if (j < (int)sizeof(inc->VerifyOpts) - 1) {
138                      j++;
139                   }
140                }
141                inc->VerifyOpts[j] = 0;
142                break;
143             default:
144                Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *p);
145                break;
146          }
147       }
148       /* Skip past space(s) */
149       for ( ; *p == ' '; p++)
150          {}
151    } else {
152       p = fname;
153    }
154
155    strcpy(inc->fname, p);                 
156    len = strlen(p);
157    /* Zap trailing slashes.  */
158    p += len - 1;
159    while (p > inc->fname && *p == '/') {
160       *p-- = 0;
161       len--;
162    }
163    inc->len = len;
164    /* Check for wild cards */
165    inc->pattern = 0;
166    for (p=inc->fname; *p; p++) {
167       if (*p == '*' || *p == '[' || *p == '?') {
168          inc->pattern = 1;
169          break;
170       }
171    }
172    ff->included_files_list = inc;
173    Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
174 }
175
176 /*
177  * We add an exclude name to either the exclude path
178  *  list or the exclude filename list.
179  */
180 void add_fname_to_exclude_list(FF_PKT *ff, char *fname)
181 {
182    int len;
183    struct s_excluded_file *exc, **list;
184
185    Dmsg1(20, "Add name to exclude: %s\n", fname);
186
187    if (strchr(fname, '/')) {
188       list = &ff->excluded_paths_list;
189    } else {
190       list = &ff->excluded_files_list;
191    }
192   
193    len = strlen(fname);
194
195    exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
196    exc->next = *list;
197    exc->len = len;
198    strcpy(exc->fname, fname);                 
199    *list = exc;
200 }
201
202
203 /*
204  * Get next included file
205  */
206 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
207 {
208    struct s_included_file *inc;
209
210    if (ainc == NULL) { 
211       inc = ff->included_files_list;
212    } else {
213       inc = ainc->next;
214    }
215    if (inc) {
216       ff->flags = inc->options;
217       if (inc->options & OPT_compute_MD5) {
218          ff->compute_MD5 = 1;
219       } else {
220          ff->compute_MD5 = 0;
221       }
222       if (inc->options & OPT_GZIP_compression) {
223          ff->GZIP_compression = 1;
224          ff->GZIP_level = inc->level;
225       } else {
226          ff->GZIP_compression = 0;
227       }
228       if (inc->options & OPT_no_recursion) {
229          ff->no_recursion = 1;
230       } else {
231          ff->no_recursion = 0;
232       }
233       if (inc->options & OPT_multifs) {
234          ff->one_file_system = 0;
235       } else {
236          ff->one_file_system = 1;
237       }
238    }
239    return inc;
240 }
241
242 /*
243  * Walk through the included list to see if this
244  *  file is included possibly with wild-cards.
245  */
246
247 int file_is_included(FF_PKT *ff, char *file)
248 {
249    struct s_included_file *inc = ff->included_files_list;
250    int len;
251
252    for ( ; inc; inc=inc->next ) {
253       if (inc->pattern) {
254          if (fnmatch(inc->fname, file, FNM_LEADING_DIR) == 0) {
255             return 1;
256          }
257          continue;
258       }                             
259       /*
260        * No wild cards. We accept a match to the
261        *  end of any component.
262        */
263       Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
264       len = strlen(file);
265       if (inc->len == len && strcmp(inc->fname, file) == 0) {
266          return 1;
267       }
268       if (inc->len < len && file[inc->len] == '/' && 
269           strncmp(inc->fname, file, inc->len) == 0) {
270          return 1;
271       }
272       if (inc->len == 1 && inc->fname[0] == '/') {
273          return 1;
274       }
275    }
276    return 0;
277 }
278
279
280 /*
281  * This is the workhorse of excluded_file().
282  * Determine if the file is excluded or not.
283  */
284 static int
285 file_in_excluded_list(struct s_excluded_file *exc, char *file)
286 {
287    if (exc == NULL) {
288       Dmsg0(900, "exc is NULL\n");
289    }
290    for ( ; exc; exc=exc->next ) {
291       if (fnmatch(exc->fname, file, FNM_PATHNAME) == 0) {
292          Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
293          return 1;
294       }
295       Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
296    }
297    return 0;
298 }
299
300
301 /*
302  * Walk through the excluded lists to see if this
303  *  file is excluded, or if it matches a component
304  *  of an excluded directory.
305  */
306
307 int file_is_excluded(FF_PKT *ff, char *file)
308 {
309    char *p;
310
311    if (win32_client && file[1] == ':') {
312       file += 2;
313    }
314
315    if (file_in_excluded_list(ff->excluded_paths_list, file)) {
316       return 1;
317    }
318
319    /* Try each component */
320    for (p = file; *p; p++) {
321       /* Match from the beginning of a component only */
322       if ((p == file || (*p != '/' && *(p-1) == '/'))
323            && file_in_excluded_list(ff->excluded_files_list, p)) {
324          return 1;
325       }
326    }
327    return 0;
328 }