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