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)
43 static int win32_client = 1;
45 static int win32_client = 0;
50 * Initialize structures for filename matching
52 void init_include_exclude_files(FF_PKT *ff)
57 * Done doing filename matching, release all
60 void term_include_exclude_files(FF_PKT *ff)
62 struct s_included_file *inc, *next_inc;
63 struct s_excluded_file *exc, *next_exc;
65 for (inc=ff->included_files_list; inc; ) {
71 for (exc=ff->excluded_files_list; exc; ) {
77 for (exc=ff->excluded_paths_list; exc; ) {
86 * Add a filename to list of included files
88 void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *fname)
91 struct s_included_file *inc;
96 inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
98 inc->VerifyOpts[0] = 'V';
99 inc->VerifyOpts[1] = ':';
100 inc->VerifyOpts[2] = 0;
102 /* prefixed = preceded with options */
104 for (p=fname; *p && *p != ' '; p++) {
106 case 'a': /* alway replace */
107 case '0': /* no option */
110 inc->options |= FO_MULTIFS;
112 case 'h': /* no recursion */
113 inc->options |= FO_NO_RECURSION;
116 inc->options |= FO_MD5;
119 inc->options |= FO_NOREPLACE;
121 case 'p': /* use portable data format */
122 inc->options |= FO_PORTABLE;
124 case 'r': /* read fifo */
125 inc->options |= FO_READFIFO;
128 inc->options |= FO_SHA1;
131 inc->options |= FO_SPARSE;
133 case 'V': /* verify options */
134 /* Copy Verify Options */
135 for (j=0; *p && *p != ':'; p++) {
136 inc->VerifyOpts[j] = *p;
137 if (j < (int)sizeof(inc->VerifyOpts) - 1) {
141 inc->VerifyOpts[j] = 0;
144 inc->options |= FO_IF_NEWER;
146 case 'Z': /* gzip compression */
147 inc->options |= FO_GZIP;
148 inc->level = *++p - '0';
149 Dmsg1(200, "Compression level=%d\n", inc->level);
152 Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *p);
156 /* Skip past space(s) */
157 for ( ; *p == ' '; p++)
163 strcpy(inc->fname, p);
166 /* Zap trailing slashes. */
168 while (p > inc->fname && *p == '/') {
173 /* Check for wild cards */
175 for (p=inc->fname; *p; p++) {
176 if (*p == '*' || *p == '[' || *p == '?') {
182 /* Convert any \'s into /'s */
183 for (p=inc->fname; *p; p++) {
190 /* Chain this one on the end of the list */
191 if (!ff->included_files_list) {
192 /* First one, so set head */
193 ff->included_files_list = inc;
195 struct s_included_file *next;
196 /* Walk to end of list */
197 for (next=ff->included_files_list; next->next; next=next->next)
201 Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
205 * We add an exclude name to either the exclude path
206 * list or the exclude filename list.
208 void add_fname_to_exclude_list(FF_PKT *ff, char *fname)
211 struct s_excluded_file *exc, **list;
214 /* Convert any \'s into /'s */
215 for (char *p=fname; *p; p++) {
221 Dmsg1(20, "Add name to exclude: %s\n", fname);
223 if (strchr(fname, '/')) {
224 list = &ff->excluded_paths_list;
226 list = &ff->excluded_files_list;
231 exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
234 strcpy(exc->fname, fname);
240 * Get next included file
242 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
244 struct s_included_file *inc;
247 inc = ff->included_files_list;
252 * copy inc_options for this file into the ff packet
255 ff->flags = inc->options;
256 ff->GZIP_level = inc->level;
262 * Walk through the included list to see if this
263 * file is included possibly with wild-cards.
266 int file_is_included(FF_PKT *ff, char *file)
268 struct s_included_file *inc = ff->included_files_list;
271 for ( ; inc; inc=inc->next ) {
273 if (fnmatch(inc->fname, file, FNM_LEADING_DIR) == 0) {
279 * No wild cards. We accept a match to the
280 * end of any component.
282 Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
284 if (inc->len == len && strcmp(inc->fname, file) == 0) {
287 if (inc->len < len && file[inc->len] == '/' &&
288 strncmp(inc->fname, file, inc->len) == 0) {
291 if (inc->len == 1 && inc->fname[0] == '/') {
300 * This is the workhorse of excluded_file().
301 * Determine if the file is excluded or not.
304 file_in_excluded_list(struct s_excluded_file *exc, char *file)
307 Dmsg0(900, "exc is NULL\n");
309 for ( ; exc; exc=exc->next ) {
310 if (fnmatch(exc->fname, file, FNM_PATHNAME) == 0) {
311 Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
314 Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
321 * Walk through the excluded lists to see if this
322 * file is excluded, or if it matches a component
323 * of an excluded directory.
326 int file_is_excluded(FF_PKT *ff, char *file)
330 if (win32_client && file[1] == ':') {
334 if (file_in_excluded_list(ff->excluded_paths_list, file)) {
338 /* Try each component */
339 for (p = file; *p; p++) {
340 /* Match from the beginning of a component only */
341 if ((p == file || (*p != '/' && *(p-1) == '/'))
342 && file_in_excluded_list(ff->excluded_files_list, p)) {