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