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