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