]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/match.c
Massive SD calling sequence reorganization
[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    Dmsg1(20, "Add name to exclude: %s\n", fname);
219
220 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
221    if (strchr(fname, '/') || strchr(fname, '\\')) {
222 #else
223    if (strchr(fname, '/')) {
224 #endif
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 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
237    /* Convert any \'s into /'s */
238    for (char *p=exc->fname; *p; p++) {
239       if (*p == '\\') {
240          *p = '/';
241       }
242    }
243 #endif
244    *list = exc;
245 }
246
247
248 /*
249  * Get next included file
250  */
251 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
252 {
253    struct s_included_file *inc;
254
255    if (ainc == NULL) { 
256       inc = ff->included_files_list;
257    } else {
258       inc = ainc->next;
259    }
260    /*
261     * copy inc_options for this file into the ff packet
262     */
263    if (inc) {
264       ff->flags = inc->options;
265       ff->GZIP_level = inc->level;
266    }
267    return inc;
268 }
269
270 /*
271  * Walk through the included list to see if this
272  *  file is included possibly with wild-cards.
273  */
274
275 int file_is_included(FF_PKT *ff, const char *file)
276 {
277    struct s_included_file *inc = ff->included_files_list;
278    int len;
279
280    for ( ; inc; inc=inc->next ) {
281       if (inc->pattern) {
282          if (fnmatch(inc->fname, file, FNM_LEADING_DIR) == 0) {
283             return 1;
284          }
285          continue;
286       }                             
287       /*
288        * No wild cards. We accept a match to the
289        *  end of any component.
290        */
291       Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
292       len = strlen(file);
293       if (inc->len == len && strcmp(inc->fname, file) == 0) {
294          return 1;
295       }
296       if (inc->len < len && file[inc->len] == '/' && 
297           strncmp(inc->fname, file, inc->len) == 0) {
298          return 1;
299       }
300       if (inc->len == 1 && inc->fname[0] == '/') {
301          return 1;
302       }
303    }
304    return 0;
305 }
306
307
308 /*
309  * This is the workhorse of excluded_file().
310  * Determine if the file is excluded or not.
311  */
312 static int
313 file_in_excluded_list(struct s_excluded_file *exc, const char *file)
314 {
315    if (exc == NULL) {
316       Dmsg0(900, "exc is NULL\n");
317    }
318    for ( ; exc; exc=exc->next ) {
319       if (fnmatch(exc->fname, file, FNM_PATHNAME) == 0) {
320          Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
321          return 1;
322       }
323       Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
324    }
325    return 0;
326 }
327
328
329 /*
330  * Walk through the excluded lists to see if this
331  *  file is excluded, or if it matches a component
332  *  of an excluded directory.
333  */
334
335 int file_is_excluded(FF_PKT *ff, const char *file)
336 {
337    const char *p;
338
339    /* 
340     *  ***NB*** this removes the drive from the exclude
341     *  rule.  Why?????
342     */
343    if (win32_client && file[1] == ':') {
344       file += 2;
345    }
346
347    if (file_in_excluded_list(ff->excluded_paths_list, file)) {
348       return 1;
349    }
350
351    /* Try each component */
352    for (p = file; *p; p++) {
353       /* Match from the beginning of a component only */
354       if ((p == file || (*p != '/' && *(p-1) == '/'))
355            && file_in_excluded_list(ff->excluded_files_list, p)) {
356          return 1;   
357       }
358    }
359    return 0;
360 }