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