2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version two of the GNU General Public
10 License as published by the Free Software Foundation plus additions
11 that are listed in the file LICENSE.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of John Walker.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
31 * Routines used to keep and match include and exclude
32 * filename/pathname patterns.
34 * Note, this file is used for the old style include and
35 * excludes, so is deprecated. The new style code is
37 * This code is still used for lists in testls and bextract.
39 * Kern E. Sibbald, December MMI
48 #include <sys/types.h>
50 #ifndef FNM_LEADING_DIR
51 #define FNM_LEADING_DIR 0
54 /* Fold case in fnmatch() on Win32 */
56 static const int fnmode = FNM_CASEFOLD;
58 static const int fnmode = 0;
63 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
67 match_files(JCR *jcr, FF_PKT *ff, int callback(FF_PKT *ff_pkt, void *hpkt, bool), void *his_pkt)
69 ff->callback = callback;
71 struct s_included_file *inc = NULL;
73 /* This is the old deprecated way */
74 while (!job_canceled(jcr) && (inc = get_next_included_file(ff, inc))) {
75 /* Copy options for this file */
76 bstrncat(ff->VerifyOpts, inc->VerifyOpts, sizeof(ff->VerifyOpts));
77 Dmsg1(100, "find_files: file=%s\n", inc->fname);
78 if (!file_is_excluded(ff, inc->fname)) {
79 if (find_one_file(jcr, ff, callback, his_pkt, inc->fname, (dev_t)-1, 1) ==0) {
80 return 0; /* error return */
89 * Done doing filename matching, release all
92 void term_include_exclude_files(FF_PKT *ff)
94 struct s_included_file *inc, *next_inc;
95 struct s_excluded_file *exc, *next_exc;
97 for (inc=ff->included_files_list; inc; ) {
102 ff->included_files_list = NULL;
104 for (exc=ff->excluded_files_list; exc; ) {
105 next_exc = exc->next;
109 ff->excluded_files_list = NULL;
111 for (exc=ff->excluded_paths_list; exc; ) {
112 next_exc = exc->next;
116 ff->excluded_paths_list = NULL;
120 * Add a filename to list of included files
122 void add_fname_to_include_list(FF_PKT *ff, int prefixed, const char *fname)
125 struct s_included_file *inc;
131 inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
133 inc->VerifyOpts[0] = 'V';
134 inc->VerifyOpts[1] = ':';
135 inc->VerifyOpts[2] = 0;
137 /* prefixed = preceded with options */
139 for (rp=fname; *rp && *rp != ' '; rp++) {
141 case 'a': /* alway replace */
142 case '0': /* no option */
145 inc->options |= FO_MULTIFS;
147 case 'h': /* no recursion */
148 inc->options |= FO_NO_RECURSION;
151 inc->options |= FO_MD5;
154 inc->options |= FO_NOREPLACE;
156 case 'p': /* use portable data format */
157 inc->options |= FO_PORTABLE;
159 case 'r': /* read fifo */
160 inc->options |= FO_READFIFO;
163 inc->options |= FO_SHA1;
166 inc->options |= FO_SPARSE;
169 inc->options |= FO_MTIMEONLY;
172 inc->options |= FO_KEEPATIME;
174 case 'V': /* verify options */
175 /* Copy Verify Options */
176 for (j=0; *rp && *rp != ':'; rp++) {
177 inc->VerifyOpts[j] = *rp;
178 if (j < (int)sizeof(inc->VerifyOpts) - 1) {
182 inc->VerifyOpts[j] = 0;
185 inc->options |= FO_IF_NEWER;
188 inc->options |= FO_ACL;
190 case 'Z': /* gzip compression */
191 inc->options |= FO_GZIP;
192 inc->level = *++rp - '0';
193 Dmsg1(200, "Compression level=%d\n", inc->level);
196 inc->options |= FO_NOATIME;
199 Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *rp);
203 /* Skip past space(s) */
204 for ( ; *rp == ' '; rp++)
210 strcpy(inc->fname, rp);
213 /* Zap trailing slashes. */
215 while (p > inc->fname && IsPathSeparator(*p)) {
220 /* Check for wild cards */
222 for (p=inc->fname; *p; p++) {
223 if (*p == '*' || *p == '[' || *p == '?') {
228 #if defined(HAVE_WIN32)
229 /* Convert any \'s into /'s */
230 for (p=inc->fname; *p; p++) {
237 /* Chain this one on the end of the list */
238 if (!ff->included_files_list) {
239 /* First one, so set head */
240 ff->included_files_list = inc;
242 struct s_included_file *next;
243 /* Walk to end of list */
244 for (next=ff->included_files_list; next->next; next=next->next)
248 Dmsg3(00, "add_fname_to_include prefix=%d gzip=%d fname=%s\n",
249 prefixed, !!(inc->options & FO_GZIP), inc->fname);
253 * We add an exclude name to either the exclude path
254 * list or the exclude filename list.
256 void add_fname_to_exclude_list(FF_PKT *ff, const char *fname)
259 struct s_excluded_file *exc, **list;
261 Dmsg1(20, "Add name to exclude: %s\n", fname);
263 if (first_path_separator(fname) != NULL) {
264 list = &ff->excluded_paths_list;
266 list = &ff->excluded_files_list;
271 exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
274 strcpy(exc->fname, fname);
275 #if defined(HAVE_WIN32)
276 /* Convert any \'s into /'s */
277 for (char *p=exc->fname; *p; p++) {
288 * Get next included file
290 struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_file *ainc)
292 struct s_included_file *inc;
295 inc = ff->included_files_list;
300 * copy inc_options for this file into the ff packet
303 ff->flags = inc->options;
304 ff->GZIP_level = inc->level;
310 * Walk through the included list to see if this
311 * file is included possibly with wild-cards.
314 int file_is_included(FF_PKT *ff, const char *file)
316 struct s_included_file *inc = ff->included_files_list;
319 for ( ; inc; inc=inc->next ) {
321 if (fnmatch(inc->fname, file, fnmode|FNM_LEADING_DIR) == 0) {
327 * No wild cards. We accept a match to the
328 * end of any component.
330 Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
332 if (inc->len == len && strcmp(inc->fname, file) == 0) {
335 if (inc->len < len && IsPathSeparator(file[inc->len]) &&
336 strncmp(inc->fname, file, inc->len) == 0) {
339 if (inc->len == 1 && IsPathSeparator(inc->fname[0])) {
348 * This is the workhorse of excluded_file().
349 * Determine if the file is excluded or not.
352 file_in_excluded_list(struct s_excluded_file *exc, const char *file)
355 Dmsg0(900, "exc is NULL\n");
357 for ( ; exc; exc=exc->next ) {
358 if (fnmatch(exc->fname, file, fnmode|FNM_PATHNAME) == 0) {
359 Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
362 Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
369 * Walk through the excluded lists to see if this
370 * file is excluded, or if it matches a component
371 * of an excluded directory.
374 int file_is_excluded(FF_PKT *ff, const char *file)
378 #if defined(HAVE_WIN32)
380 * ***NB*** this removes the drive from the exclude
383 if (file[1] == ':') {
388 if (file_in_excluded_list(ff->excluded_paths_list, file)) {
392 /* Try each component */
393 for (p = file; *p; p++) {
394 /* Match from the beginning of a component only */
395 if ((p == file || (!IsPathSeparator(*p) && IsPathSeparator(p[-1])))
396 && file_in_excluded_list(ff->excluded_files_list, p)) {