2 * Routines used to keep and match include and exclude
3 * filename/pathname patterns.
5 * Kern E. Sibbald, December MMI
9 Copyright (C) 2001-2003 Kern Sibbald and John Walker
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.
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.
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,
33 #include <sys/types.h>
35 #ifndef FNM_LEADING_DIR
36 #define FNM_LEADING_DIR 0
40 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
42 extern const int win32_client;
45 * Initialize structures for filename matching
47 void init_include_exclude_files(FF_PKT *ff)
52 * Done doing filename matching, release all
55 void term_include_exclude_files(FF_PKT *ff)
57 struct s_included_file *inc, *next_inc;
58 struct s_excluded_file *exc, *next_exc;
60 for (inc=ff->included_files_list; inc; ) {
66 for (exc=ff->excluded_files_list; exc; ) {
72 for (exc=ff->excluded_paths_list; exc; ) {
81 * Add a filename to list of included files
83 void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *fname)
86 struct s_included_file *inc;
91 inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
93 inc->VerifyOpts[0] = 'V';
94 inc->VerifyOpts[1] = ':';
95 inc->VerifyOpts[2] = 0;
97 /* prefixed = preceded with options */
99 for (p=fname; *p && *p != ' '; p++) {
101 case 'a': /* alway replace */
102 case '0': /* no option */
105 inc->options |= FO_MULTIFS;
107 case 'h': /* no recursion */
108 inc->options |= FO_NO_RECURSION;
111 inc->options |= FO_MD5;
114 inc->options |= FO_NOREPLACE;
116 case 'p': /* use portable data format */
117 inc->options |= FO_PORTABLE;
119 case 'r': /* read fifo */
120 inc->options |= FO_READFIFO;
123 inc->options |= FO_SHA1;
126 inc->options |= FO_SPARSE;
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) {
136 inc->VerifyOpts[j] = 0;
139 inc->options |= FO_IF_NEWER;
141 case 'Z': /* gzip compression */
142 inc->options |= FO_GZIP;
143 inc->level = *++p - '0';
144 Dmsg1(200, "Compression level=%d\n", inc->level);
147 Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *p);
151 /* Skip past space(s) */
152 for ( ; *p == ' '; p++)
158 strcpy(inc->fname, p);
161 /* Zap trailing slashes. */
163 while (p > inc->fname && *p == '/') {
168 /* Check for wild cards */
170 for (p=inc->fname; *p; p++) {
171 if (*p == '*' || *p == '[' || *p == '?') {
176 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
177 /* Convert any \'s into /'s */
178 for (p=inc->fname; *p; p++) {
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;
190 struct s_included_file *next;
191 /* Walk to end of list */
192 for (next=ff->included_files_list; next->next; next=next->next)
196 Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
200 * We add an exclude name to either the exclude path
201 * list or the exclude filename list.
203 void add_fname_to_exclude_list(FF_PKT *ff, char *fname)
206 struct s_excluded_file *exc, **list;
208 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
209 /* Convert any \'s into /'s */
210 for (char *p=fname; *p; p++) {
216 Dmsg1(20, "Add name to exclude: %s\n", fname);
218 if (strchr(fname, '/')) {
219 list = &ff->excluded_paths_list;
221 list = &ff->excluded_files_list;
226 exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
229 strcpy(exc->fname, fname);
235 * Get next included file
237 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
239 struct s_included_file *inc;
242 inc = ff->included_files_list;
247 * copy inc_options for this file into the ff packet
250 ff->flags = inc->options;
251 ff->GZIP_level = inc->level;
257 * Walk through the included list to see if this
258 * file is included possibly with wild-cards.
261 int file_is_included(FF_PKT *ff, char *file)
263 struct s_included_file *inc = ff->included_files_list;
266 for ( ; inc; inc=inc->next ) {
268 if (fnmatch(inc->fname, file, FNM_LEADING_DIR) == 0) {
274 * No wild cards. We accept a match to the
275 * end of any component.
277 Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
279 if (inc->len == len && strcmp(inc->fname, file) == 0) {
282 if (inc->len < len && file[inc->len] == '/' &&
283 strncmp(inc->fname, file, inc->len) == 0) {
286 if (inc->len == 1 && inc->fname[0] == '/') {
295 * This is the workhorse of excluded_file().
296 * Determine if the file is excluded or not.
299 file_in_excluded_list(struct s_excluded_file *exc, char *file)
302 Dmsg0(900, "exc is NULL\n");
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);
309 Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
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.
321 int file_is_excluded(FF_PKT *ff, char *file)
325 if (win32_client && file[1] == ':') {
329 if (file_in_excluded_list(ff->excluded_paths_list, file)) {
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)) {