4 * Routines used to keep and match include and exclude
5 * filename/pathname patterns.
7 * Note, this file is used for the old style include and
8 * excludes, so is deprecated. The new style code is
10 * This code is still used for lists in testls and bextract.
12 * Kern E. Sibbald, December MMI
16 Copyright (C) 2001-2005 Kern Sibbald
18 This program is free software; you can redistribute it and/or
19 modify it under the terms of the GNU General Public License
20 version 2 as ammended with additional clauses defined in the
21 file LICENSE in the main source directory.
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 the file LICENSE for additional details.
35 #include <sys/types.h>
37 #ifndef FNM_LEADING_DIR
38 #define FNM_LEADING_DIR 0
41 /* Fold case in fnmatch() on Win32 */
43 static const int fnmode = FNM_CASEFOLD;
45 static const int fnmode = 0;
50 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
52 extern const int win32_client;
55 match_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt, bool), void *his_pkt)
57 ff->callback = callback;
59 struct s_included_file *inc = NULL;
61 /* This is the old deprecated way */
62 while (!job_canceled(jcr) && (inc = get_next_included_file(ff, inc))) {
63 /* Copy options for this file */
64 bstrncat(ff->VerifyOpts, inc->VerifyOpts, sizeof(ff->VerifyOpts));
65 Dmsg1(100, "find_files: file=%s\n", inc->fname);
66 if (!file_is_excluded(ff, inc->fname)) {
67 if (find_one_file(jcr, ff, callback, his_pkt, inc->fname, (dev_t)-1, 1) ==0) {
68 return 0; /* error return */
77 * Done doing filename matching, release all
80 void term_include_exclude_files(FF_PKT *ff)
82 struct s_included_file *inc, *next_inc;
83 struct s_excluded_file *exc, *next_exc;
85 for (inc=ff->included_files_list; inc; ) {
90 ff->included_files_list = NULL;
92 for (exc=ff->excluded_files_list; exc; ) {
97 ff->excluded_files_list = NULL;
99 for (exc=ff->excluded_paths_list; exc; ) {
100 next_exc = exc->next;
104 ff->excluded_paths_list = NULL;
108 * Add a filename to list of included files
110 void add_fname_to_include_list(FF_PKT *ff, int prefixed, const char *fname)
113 struct s_included_file *inc;
119 inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
121 inc->VerifyOpts[0] = 'V';
122 inc->VerifyOpts[1] = ':';
123 inc->VerifyOpts[2] = 0;
125 /* prefixed = preceded with options */
127 for (rp=fname; *rp && *rp != ' '; rp++) {
129 case 'a': /* alway replace */
130 case '0': /* no option */
133 inc->options |= FO_MULTIFS;
135 case 'h': /* no recursion */
136 inc->options |= FO_NO_RECURSION;
139 inc->options |= FO_MD5;
142 inc->options |= FO_NOREPLACE;
144 case 'p': /* use portable data format */
145 inc->options |= FO_PORTABLE;
147 case 'r': /* read fifo */
148 inc->options |= FO_READFIFO;
151 inc->options |= FO_SHA1;
154 inc->options |= FO_SPARSE;
157 inc->options |= FO_MTIMEONLY;
160 inc->options |= FO_KEEPATIME;
162 case 'V': /* verify options */
163 /* Copy Verify Options */
164 for (j=0; *rp && *rp != ':'; rp++) {
165 inc->VerifyOpts[j] = *rp;
166 if (j < (int)sizeof(inc->VerifyOpts) - 1) {
170 inc->VerifyOpts[j] = 0;
173 inc->options |= FO_IF_NEWER;
176 inc->options |= FO_ACL;
178 case 'Z': /* gzip compression */
179 inc->options |= FO_GZIP;
180 inc->level = *++rp - '0';
181 Dmsg1(200, "Compression level=%d\n", inc->level);
184 Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *rp);
188 /* Skip past space(s) */
189 for ( ; *rp == ' '; rp++)
195 strcpy(inc->fname, rp);
198 /* Zap trailing slashes. */
200 while (p > inc->fname && *p == '/') {
205 /* Check for wild cards */
207 for (p=inc->fname; *p; p++) {
208 if (*p == '*' || *p == '[' || *p == '?') {
213 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
214 /* Convert any \'s into /'s */
215 for (p=inc->fname; *p; p++) {
222 /* Chain this one on the end of the list */
223 if (!ff->included_files_list) {
224 /* First one, so set head */
225 ff->included_files_list = inc;
227 struct s_included_file *next;
228 /* Walk to end of list */
229 for (next=ff->included_files_list; next->next; next=next->next)
233 Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
237 * We add an exclude name to either the exclude path
238 * list or the exclude filename list.
240 void add_fname_to_exclude_list(FF_PKT *ff, const char *fname)
243 struct s_excluded_file *exc, **list;
245 Dmsg1(20, "Add name to exclude: %s\n", fname);
247 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
248 if (strchr(fname, '/') || strchr(fname, '\\')) {
250 if (strchr(fname, '/')) {
252 list = &ff->excluded_paths_list;
254 list = &ff->excluded_files_list;
259 exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
262 strcpy(exc->fname, fname);
263 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
264 /* Convert any \'s into /'s */
265 for (char *p=exc->fname; *p; p++) {
276 * Get next included file
278 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
280 struct s_included_file *inc;
283 inc = ff->included_files_list;
288 * copy inc_options for this file into the ff packet
291 ff->flags = inc->options;
292 ff->GZIP_level = inc->level;
298 * Walk through the included list to see if this
299 * file is included possibly with wild-cards.
302 int file_is_included(FF_PKT *ff, const char *file)
304 struct s_included_file *inc = ff->included_files_list;
307 for ( ; inc; inc=inc->next ) {
309 if (fnmatch(inc->fname, file, fnmode|FNM_LEADING_DIR) == 0) {
315 * No wild cards. We accept a match to the
316 * end of any component.
318 Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
320 if (inc->len == len && strcmp(inc->fname, file) == 0) {
323 if (inc->len < len && file[inc->len] == '/' &&
324 strncmp(inc->fname, file, inc->len) == 0) {
327 if (inc->len == 1 && inc->fname[0] == '/') {
336 * This is the workhorse of excluded_file().
337 * Determine if the file is excluded or not.
340 file_in_excluded_list(struct s_excluded_file *exc, const char *file)
343 Dmsg0(900, "exc is NULL\n");
345 for ( ; exc; exc=exc->next ) {
346 if (fnmatch(exc->fname, file, fnmode|FNM_PATHNAME) == 0) {
347 Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
350 Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
357 * Walk through the excluded lists to see if this
358 * file is excluded, or if it matches a component
359 * of an excluded directory.
362 int file_is_excluded(FF_PKT *ff, const char *file)
367 * ***NB*** this removes the drive from the exclude
370 if (win32_client && file[1] == ':') {
374 if (file_in_excluded_list(ff->excluded_paths_list, file)) {
378 /* Try each component */
379 for (p = file; *p; p++) {
380 /* Match from the beginning of a component only */
381 if ((p == file || (*p != '/' && *(p-1) == '/'))
382 && file_in_excluded_list(ff->excluded_files_list, p)) {