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 and John Walker
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; ) {
78 for (exc=ff->excluded_files_list; exc; ) {
84 for (exc=ff->excluded_paths_list; exc; ) {
93 * Add a filename to list of included files
95 void add_fname_to_include_list(FF_PKT *ff, int prefixed, const char *fname)
98 struct s_included_file *inc;
104 inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
106 inc->VerifyOpts[0] = 'V';
107 inc->VerifyOpts[1] = ':';
108 inc->VerifyOpts[2] = 0;
110 /* prefixed = preceded with options */
112 for (rp=fname; *rp && *rp != ' '; rp++) {
114 case 'a': /* alway replace */
115 case '0': /* no option */
118 inc->options |= FO_MULTIFS;
120 case 'h': /* no recursion */
121 inc->options |= FO_NO_RECURSION;
124 inc->options |= FO_MD5;
127 inc->options |= FO_NOREPLACE;
129 case 'p': /* use portable data format */
130 inc->options |= FO_PORTABLE;
132 case 'r': /* read fifo */
133 inc->options |= FO_READFIFO;
136 inc->options |= FO_SHA1;
139 inc->options |= FO_SPARSE;
142 inc->options |= FO_MTIMEONLY;
145 inc->options |= FO_KEEPATIME;
147 case 'V': /* verify options */
148 /* Copy Verify Options */
149 for (j=0; *rp && *rp != ':'; rp++) {
150 inc->VerifyOpts[j] = *rp;
151 if (j < (int)sizeof(inc->VerifyOpts) - 1) {
155 inc->VerifyOpts[j] = 0;
158 inc->options |= FO_IF_NEWER;
161 inc->options |= FO_ACL;
163 case 'Z': /* gzip compression */
164 inc->options |= FO_GZIP;
165 inc->level = *++rp - '0';
166 Dmsg1(200, "Compression level=%d\n", inc->level);
169 Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *rp);
173 /* Skip past space(s) */
174 for ( ; *rp == ' '; rp++)
180 strcpy(inc->fname, rp);
183 /* Zap trailing slashes. */
185 while (p > inc->fname && *p == '/') {
190 /* Check for wild cards */
192 for (p=inc->fname; *p; p++) {
193 if (*p == '*' || *p == '[' || *p == '?') {
198 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
199 /* Convert any \'s into /'s */
200 for (p=inc->fname; *p; p++) {
207 /* Chain this one on the end of the list */
208 if (!ff->included_files_list) {
209 /* First one, so set head */
210 ff->included_files_list = inc;
212 struct s_included_file *next;
213 /* Walk to end of list */
214 for (next=ff->included_files_list; next->next; next=next->next)
218 Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
222 * We add an exclude name to either the exclude path
223 * list or the exclude filename list.
225 void add_fname_to_exclude_list(FF_PKT *ff, const char *fname)
228 struct s_excluded_file *exc, **list;
230 Dmsg1(20, "Add name to exclude: %s\n", fname);
232 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
233 if (strchr(fname, '/') || strchr(fname, '\\')) {
235 if (strchr(fname, '/')) {
237 list = &ff->excluded_paths_list;
239 list = &ff->excluded_files_list;
244 exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
247 strcpy(exc->fname, fname);
248 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
249 /* Convert any \'s into /'s */
250 for (char *p=exc->fname; *p; p++) {
261 * Get next included file
263 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
265 struct s_included_file *inc;
268 inc = ff->included_files_list;
273 * copy inc_options for this file into the ff packet
276 ff->flags = inc->options;
277 ff->GZIP_level = inc->level;
283 * Walk through the included list to see if this
284 * file is included possibly with wild-cards.
287 int file_is_included(FF_PKT *ff, const char *file)
289 struct s_included_file *inc = ff->included_files_list;
292 for ( ; inc; inc=inc->next ) {
294 if (fnmatch(inc->fname, file, fnmode|FNM_LEADING_DIR) == 0) {
300 * No wild cards. We accept a match to the
301 * end of any component.
303 Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
305 if (inc->len == len && strcmp(inc->fname, file) == 0) {
308 if (inc->len < len && file[inc->len] == '/' &&
309 strncmp(inc->fname, file, inc->len) == 0) {
312 if (inc->len == 1 && inc->fname[0] == '/') {
321 * This is the workhorse of excluded_file().
322 * Determine if the file is excluded or not.
325 file_in_excluded_list(struct s_excluded_file *exc, const char *file)
328 Dmsg0(900, "exc is NULL\n");
330 for ( ; exc; exc=exc->next ) {
331 if (fnmatch(exc->fname, file, fnmode|FNM_PATHNAME) == 0) {
332 Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
335 Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
342 * Walk through the excluded lists to see if this
343 * file is excluded, or if it matches a component
344 * of an excluded directory.
347 int file_is_excluded(FF_PKT *ff, const char *file)
352 * ***NB*** this removes the drive from the exclude
355 if (win32_client && file[1] == ':') {
359 if (file_in_excluded_list(ff->excluded_paths_list, file)) {
363 /* Try each component */
364 for (p = file; *p; p++) {
365 /* Match from the beginning of a component only */
366 if ((p == file || (*p != '/' && *(p-1) == '/'))
367 && file_in_excluded_list(ff->excluded_files_list, p)) {