2 * Routines used to keep and match include and exclude
3 * filename/pathname patterns.
5 * Kern E. Sibbald, December MMI
9 Copyright (C) 2001-2004 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, const char *fname)
86 struct s_included_file *inc;
92 inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
94 inc->VerifyOpts[0] = 'V';
95 inc->VerifyOpts[1] = ':';
96 inc->VerifyOpts[2] = 0;
98 /* prefixed = preceded with options */
100 for (rp=fname; *rp && *rp != ' '; rp++) {
102 case 'a': /* alway replace */
103 case '0': /* no option */
106 inc->options |= FO_MULTIFS;
108 case 'h': /* no recursion */
109 inc->options |= FO_NO_RECURSION;
112 inc->options |= FO_MD5;
115 inc->options |= FO_NOREPLACE;
117 case 'p': /* use portable data format */
118 inc->options |= FO_PORTABLE;
120 case 'r': /* read fifo */
121 inc->options |= FO_READFIFO;
124 inc->options |= FO_SHA1;
127 inc->options |= FO_SPARSE;
130 inc->options |= FO_MTIMEONLY;
133 inc->options |= FO_KEEPATIME;
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) {
143 inc->VerifyOpts[j] = 0;
146 inc->options |= FO_IF_NEWER;
149 inc->options |= FO_ACL;
151 case 'Z': /* gzip compression */
152 inc->options |= FO_GZIP;
153 inc->level = *++rp - '0';
154 Dmsg1(200, "Compression level=%d\n", inc->level);
157 Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *rp);
161 /* Skip past space(s) */
162 for ( ; *rp == ' '; rp++)
168 strcpy(inc->fname, rp);
171 /* Zap trailing slashes. */
173 while (p > inc->fname && *p == '/') {
178 /* Check for wild cards */
180 for (p=inc->fname; *p; p++) {
181 if (*p == '*' || *p == '[' || *p == '?') {
186 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
187 /* Convert any \'s into /'s */
188 for (p=inc->fname; *p; p++) {
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;
200 struct s_included_file *next;
201 /* Walk to end of list */
202 for (next=ff->included_files_list; next->next; next=next->next)
206 Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
210 * We add an exclude name to either the exclude path
211 * list or the exclude filename list.
213 void add_fname_to_exclude_list(FF_PKT *ff, const char *fname)
216 struct s_excluded_file *exc, **list;
218 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
219 /* Convert any \'s into /'s */
220 for (char *p=fname; *p; p++) {
226 Dmsg1(20, "Add name to exclude: %s\n", fname);
228 if (strchr(fname, '/')) {
229 list = &ff->excluded_paths_list;
231 list = &ff->excluded_files_list;
236 exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
239 strcpy(exc->fname, fname);
245 * Get next included file
247 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
249 struct s_included_file *inc;
252 inc = ff->included_files_list;
257 * copy inc_options for this file into the ff packet
260 ff->flags = inc->options;
261 ff->GZIP_level = inc->level;
267 * Walk through the included list to see if this
268 * file is included possibly with wild-cards.
271 int file_is_included(FF_PKT *ff, const char *file)
273 struct s_included_file *inc = ff->included_files_list;
276 for ( ; inc; inc=inc->next ) {
278 if (fnmatch(inc->fname, file, FNM_LEADING_DIR) == 0) {
284 * No wild cards. We accept a match to the
285 * end of any component.
287 Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
289 if (inc->len == len && strcmp(inc->fname, file) == 0) {
292 if (inc->len < len && file[inc->len] == '/' &&
293 strncmp(inc->fname, file, inc->len) == 0) {
296 if (inc->len == 1 && inc->fname[0] == '/') {
305 * This is the workhorse of excluded_file().
306 * Determine if the file is excluded or not.
309 file_in_excluded_list(struct s_excluded_file *exc, const char *file)
312 Dmsg0(900, "exc is NULL\n");
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);
319 Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
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.
331 int file_is_excluded(FF_PKT *ff, const char *file)
335 if (win32_client && file[1] == ':') {
339 if (file_in_excluded_list(ff->excluded_paths_list, file)) {
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)) {