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 as
20 published by the Free Software Foundation; either version 2 of
21 the License, or (at your option) any later version.
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 GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public
29 License along with this program; if not, write to the Free
30 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
40 #include <sys/types.h>
42 #ifndef FNM_LEADING_DIR
43 #define FNM_LEADING_DIR 0
46 /* Fold case in fnmatch() on Win32 */
48 static const int fnmode = FNM_CASEFOLD;
50 static const int fnmode = 0;
55 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
57 extern const int win32_client;
60 match_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt, bool), void *his_pkt)
62 ff->callback = callback;
64 struct s_included_file *inc = NULL;
66 /* This is the old deprecated way */
67 while (!job_canceled(jcr) && (inc = get_next_included_file(ff, inc))) {
68 /* Copy options for this file */
69 bstrncat(ff->VerifyOpts, inc->VerifyOpts, sizeof(ff->VerifyOpts));
70 Dmsg1(100, "find_files: file=%s\n", inc->fname);
71 if (!file_is_excluded(ff, inc->fname)) {
72 if (find_one_file(jcr, ff, callback, his_pkt, inc->fname, (dev_t)-1, 1) ==0) {
73 return 0; /* error return */
82 * Done doing filename matching, release all
85 void term_include_exclude_files(FF_PKT *ff)
87 struct s_included_file *inc, *next_inc;
88 struct s_excluded_file *exc, *next_exc;
90 for (inc=ff->included_files_list; inc; ) {
95 ff->included_files_list = NULL;
97 for (exc=ff->excluded_files_list; exc; ) {
102 ff->excluded_files_list = NULL;
104 for (exc=ff->excluded_paths_list; exc; ) {
105 next_exc = exc->next;
109 ff->excluded_paths_list = NULL;
113 * Add a filename to list of included files
115 void add_fname_to_include_list(FF_PKT *ff, int prefixed, const char *fname)
118 struct s_included_file *inc;
124 inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
126 inc->VerifyOpts[0] = 'V';
127 inc->VerifyOpts[1] = ':';
128 inc->VerifyOpts[2] = 0;
130 /* prefixed = preceded with options */
132 for (rp=fname; *rp && *rp != ' '; rp++) {
134 case 'a': /* alway replace */
135 case '0': /* no option */
138 inc->options |= FO_MULTIFS;
140 case 'h': /* no recursion */
141 inc->options |= FO_NO_RECURSION;
144 inc->options |= FO_MD5;
147 inc->options |= FO_NOREPLACE;
149 case 'p': /* use portable data format */
150 inc->options |= FO_PORTABLE;
152 case 'r': /* read fifo */
153 inc->options |= FO_READFIFO;
156 inc->options |= FO_SHA1;
159 inc->options |= FO_SPARSE;
162 inc->options |= FO_MTIMEONLY;
165 inc->options |= FO_KEEPATIME;
167 case 'V': /* verify options */
168 /* Copy Verify Options */
169 for (j=0; *rp && *rp != ':'; rp++) {
170 inc->VerifyOpts[j] = *rp;
171 if (j < (int)sizeof(inc->VerifyOpts) - 1) {
175 inc->VerifyOpts[j] = 0;
178 inc->options |= FO_IF_NEWER;
181 inc->options |= FO_ACL;
183 case 'Z': /* gzip compression */
184 inc->options |= FO_GZIP;
185 inc->level = *++rp - '0';
186 Dmsg1(200, "Compression level=%d\n", inc->level);
189 Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *rp);
193 /* Skip past space(s) */
194 for ( ; *rp == ' '; rp++)
200 strcpy(inc->fname, rp);
203 /* Zap trailing slashes. */
205 while (p > inc->fname && *p == '/') {
210 /* Check for wild cards */
212 for (p=inc->fname; *p; p++) {
213 if (*p == '*' || *p == '[' || *p == '?') {
218 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
219 /* Convert any \'s into /'s */
220 for (p=inc->fname; *p; p++) {
227 /* Chain this one on the end of the list */
228 if (!ff->included_files_list) {
229 /* First one, so set head */
230 ff->included_files_list = inc;
232 struct s_included_file *next;
233 /* Walk to end of list */
234 for (next=ff->included_files_list; next->next; next=next->next)
238 Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
242 * We add an exclude name to either the exclude path
243 * list or the exclude filename list.
245 void add_fname_to_exclude_list(FF_PKT *ff, const char *fname)
248 struct s_excluded_file *exc, **list;
250 Dmsg1(20, "Add name to exclude: %s\n", fname);
252 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
253 if (strchr(fname, '/') || strchr(fname, '\\')) {
255 if (strchr(fname, '/')) {
257 list = &ff->excluded_paths_list;
259 list = &ff->excluded_files_list;
264 exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
267 strcpy(exc->fname, fname);
268 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
269 /* Convert any \'s into /'s */
270 for (char *p=exc->fname; *p; p++) {
281 * Get next included file
283 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
285 struct s_included_file *inc;
288 inc = ff->included_files_list;
293 * copy inc_options for this file into the ff packet
296 ff->flags = inc->options;
297 ff->GZIP_level = inc->level;
303 * Walk through the included list to see if this
304 * file is included possibly with wild-cards.
307 int file_is_included(FF_PKT *ff, const char *file)
309 struct s_included_file *inc = ff->included_files_list;
312 for ( ; inc; inc=inc->next ) {
314 if (fnmatch(inc->fname, file, fnmode|FNM_LEADING_DIR) == 0) {
320 * No wild cards. We accept a match to the
321 * end of any component.
323 Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
325 if (inc->len == len && strcmp(inc->fname, file) == 0) {
328 if (inc->len < len && file[inc->len] == '/' &&
329 strncmp(inc->fname, file, inc->len) == 0) {
332 if (inc->len == 1 && inc->fname[0] == '/') {
341 * This is the workhorse of excluded_file().
342 * Determine if the file is excluded or not.
345 file_in_excluded_list(struct s_excluded_file *exc, const char *file)
348 Dmsg0(900, "exc is NULL\n");
350 for ( ; exc; exc=exc->next ) {
351 if (fnmatch(exc->fname, file, fnmode|FNM_PATHNAME) == 0) {
352 Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
355 Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
362 * Walk through the excluded lists to see if this
363 * file is excluded, or if it matches a component
364 * of an excluded directory.
367 int file_is_excluded(FF_PKT *ff, const char *file)
372 * ***NB*** this removes the drive from the exclude
375 if (win32_client && file[1] == ':') {
379 if (file_in_excluded_list(ff->excluded_paths_list, file)) {
383 /* Try each component */
384 for (p = file; *p; p++) {
385 /* Match from the beginning of a component only */
386 if ((p == file || (*p != '/' && *(p-1) == '/'))
387 && file_in_excluded_list(ff->excluded_files_list, p)) {