2 * Routines used to keep and match include and exclude
3 * filename/pathname patterns.
5 * Note, this file is used for the old style include and
6 * excludes, so is deprecated. The new style code is
9 * Kern E. Sibbald, December MMI
13 Copyright (C) 2001-2004 Kern Sibbald
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received a copy of the GNU General Public
26 License along with this program; if not, write to the Free
27 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
37 #include <sys/types.h>
39 #ifndef FNM_LEADING_DIR
40 #define FNM_LEADING_DIR 0
43 /* Fold case in fnmatch() on Win32 */
45 static const int fnmode = FNM_CASEFOLD;
47 static const int fnmode = 0;
52 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
54 extern const int win32_client;
57 * Initialize structures for filename matching
59 void init_include_exclude_files(FF_PKT *ff)
64 * Done doing filename matching, release all
67 void term_include_exclude_files(FF_PKT *ff)
69 struct s_included_file *inc, *next_inc;
70 struct s_excluded_file *exc, *next_exc;
72 for (inc=ff->included_files_list; inc; ) {
77 ff->included_files_list = NULL;
79 for (exc=ff->excluded_files_list; exc; ) {
84 ff->excluded_files_list = NULL;
86 for (exc=ff->excluded_paths_list; exc; ) {
91 ff->excluded_paths_list = NULL;
95 * Add a filename to list of included files
97 void add_fname_to_include_list(FF_PKT *ff, int prefixed, const char *fname)
100 struct s_included_file *inc;
106 inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
108 inc->VerifyOpts[0] = 'V';
109 inc->VerifyOpts[1] = ':';
110 inc->VerifyOpts[2] = 0;
112 /* prefixed = preceded with options */
114 for (rp=fname; *rp && *rp != ' '; rp++) {
116 case 'a': /* alway replace */
117 case '0': /* no option */
120 inc->options |= FO_MULTIFS;
122 case 'h': /* no recursion */
123 inc->options |= FO_NO_RECURSION;
126 inc->options |= FO_MD5;
129 inc->options |= FO_NOREPLACE;
131 case 'p': /* use portable data format */
132 inc->options |= FO_PORTABLE;
134 case 'r': /* read fifo */
135 inc->options |= FO_READFIFO;
138 inc->options |= FO_SHA1;
141 inc->options |= FO_SPARSE;
144 inc->options |= FO_MTIMEONLY;
147 inc->options |= FO_KEEPATIME;
149 case 'V': /* verify options */
150 /* Copy Verify Options */
151 for (j=0; *rp && *rp != ':'; rp++) {
152 inc->VerifyOpts[j] = *rp;
153 if (j < (int)sizeof(inc->VerifyOpts) - 1) {
157 inc->VerifyOpts[j] = 0;
160 inc->options |= FO_IF_NEWER;
163 inc->options |= FO_ACL;
165 case 'Z': /* gzip compression */
166 inc->options |= FO_GZIP;
167 inc->level = *++rp - '0';
168 Dmsg1(200, "Compression level=%d\n", inc->level);
171 Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *rp);
175 /* Skip past space(s) */
176 for ( ; *rp == ' '; rp++)
182 strcpy(inc->fname, rp);
185 /* Zap trailing slashes. */
187 while (p > inc->fname && *p == '/') {
192 /* Check for wild cards */
194 for (p=inc->fname; *p; p++) {
195 if (*p == '*' || *p == '[' || *p == '?') {
200 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
201 /* Convert any \'s into /'s */
202 for (p=inc->fname; *p; p++) {
209 /* Chain this one on the end of the list */
210 if (!ff->included_files_list) {
211 /* First one, so set head */
212 ff->included_files_list = inc;
214 struct s_included_file *next;
215 /* Walk to end of list */
216 for (next=ff->included_files_list; next->next; next=next->next)
220 Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
224 * We add an exclude name to either the exclude path
225 * list or the exclude filename list.
227 void add_fname_to_exclude_list(FF_PKT *ff, const char *fname)
230 struct s_excluded_file *exc, **list;
232 Dmsg1(20, "Add name to exclude: %s\n", fname);
234 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
235 if (strchr(fname, '/') || strchr(fname, '\\')) {
237 if (strchr(fname, '/')) {
239 list = &ff->excluded_paths_list;
241 list = &ff->excluded_files_list;
246 exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
249 strcpy(exc->fname, fname);
250 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
251 /* Convert any \'s into /'s */
252 for (char *p=exc->fname; *p; p++) {
263 * Get next included file
265 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
267 struct s_included_file *inc;
270 inc = ff->included_files_list;
275 * copy inc_options for this file into the ff packet
278 ff->flags = inc->options;
279 ff->GZIP_level = inc->level;
285 * Walk through the included list to see if this
286 * file is included possibly with wild-cards.
289 int file_is_included(FF_PKT *ff, const char *file)
291 struct s_included_file *inc = ff->included_files_list;
294 for ( ; inc; inc=inc->next ) {
296 if (fnmatch(inc->fname, file, fnmode|FNM_LEADING_DIR) == 0) {
302 * No wild cards. We accept a match to the
303 * end of any component.
305 Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
307 if (inc->len == len && strcmp(inc->fname, file) == 0) {
310 if (inc->len < len && file[inc->len] == '/' &&
311 strncmp(inc->fname, file, inc->len) == 0) {
314 if (inc->len == 1 && inc->fname[0] == '/') {
323 * This is the workhorse of excluded_file().
324 * Determine if the file is excluded or not.
327 file_in_excluded_list(struct s_excluded_file *exc, const char *file)
330 Dmsg0(900, "exc is NULL\n");
332 for ( ; exc; exc=exc->next ) {
333 if (fnmatch(exc->fname, file, fnmode|FNM_PATHNAME) == 0) {
334 Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
337 Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
344 * Walk through the excluded lists to see if this
345 * file is excluded, or if it matches a component
346 * of an excluded directory.
349 int file_is_excluded(FF_PKT *ff, const char *file)
354 * ***NB*** this removes the drive from the exclude
357 if (win32_client && file[1] == ':') {
361 if (file_in_excluded_list(ff->excluded_paths_list, file)) {
365 /* Try each component */
366 for (p = file; *p; p++) {
367 /* Match from the beginning of a component only */
368 if ((p == file || (*p != '/' && *(p-1) == '/'))
369 && file_in_excluded_list(ff->excluded_files_list, p)) {