]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/findlib/match.c
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / findlib / match.c
index 8ca68db1a40bfd28c4fb94a7ea4ebde10bf7ed75..b1214f3ebf6163797fcefc1083df55286741e7cb 100644 (file)
@@ -1,60 +1,82 @@
 /*
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2018 Kern Sibbald
+
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
+
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
+
+   This notice must be preserved when any source code is
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
+*/
+/*
+ *     Old style
+ *
  *  Routines used to keep and match include and exclude
  *   filename/pathname patterns.
  *
+ *  Note, this file is used for the old style include and
+ *   excludes, so is deprecated. The new style code is
+ *   found in find.c.
+ *  This code is still used for lists in testls and bextract.
+ *
  *   Kern E. Sibbald, December MMI
  *
  */
-/*
-   Copyright (C) 2001-2003 Kern Sibbald and John Walker
-
-   This program is free software; you can redistribute it and/or
-   modify it under the terms of the GNU General Public License as
-   published by the Free Software Foundation; either version 2 of
-   the License, or (at your option) any later version.
-
-   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public
-   License along with this program; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
-   MA 02111-1307, USA.
-
- */
 
 #include "bacula.h"
 #include "find.h"
+#include "ch.h"
 
-#include <pwd.h>
-#include <grp.h>
 #include <sys/types.h>
 
 #ifndef FNM_LEADING_DIR
 #define FNM_LEADING_DIR 0
 #endif
 
+/* Fold case in fnmatch() on Win32 */
+#ifdef HAVE_WIN32
+static const int fnmode = FNM_CASEFOLD;
+#else
+static const int fnmode = 0;
+#endif
+
+
 #undef bmalloc
 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
 
-#ifdef HAVE_CYGWIN
-static int win32_client = 1;
-#else
-static int win32_client = 0;
-#endif
 
-       
-/*
- * Initialize structures for filename matching
- */
-void init_include_exclude_files(FF_PKT *ff)
+int
+match_files(JCR *jcr, FF_PKT *ff, int file_save(JCR *, FF_PKT *ff_pkt, bool))
 {
+   ff->file_save = file_save;
+
+   struct s_included_file *inc = NULL;
+
+   /* This is the old deprecated way */
+   while (!job_canceled(jcr) && (inc = get_next_included_file(ff, inc))) {
+      /* Copy options for this file */
+      bstrncat(ff->VerifyOpts, inc->VerifyOpts, sizeof(ff->VerifyOpts));
+      Dmsg1(100, "find_files: file=%s\n", inc->fname);
+      if (!file_is_excluded(ff, inc->fname)) {
+         if (find_one_file(jcr, ff, file_save, inc->fname, (dev_t)-1, 1) ==0) {
+            return 0;                  /* error return */
+         }
+      }
+   }
+   return 1;
 }
 
+
 /*
- * Done doing filename matching, release all 
+ * Done doing filename matching, release all
  *  resources used.
  */
 void term_include_exclude_files(FF_PKT *ff)
@@ -67,102 +89,132 @@ void term_include_exclude_files(FF_PKT *ff)
       free(inc);
       inc = next_inc;
    }
+   ff->included_files_list = NULL;
 
    for (exc=ff->excluded_files_list; exc; ) {
       next_exc = exc->next;
       free(exc);
       exc = next_exc;
    }
+   ff->excluded_files_list = NULL;
 
    for (exc=ff->excluded_paths_list; exc; ) {
       next_exc = exc->next;
       free(exc);
       exc = next_exc;
    }
-   
+   ff->excluded_paths_list = NULL;
 }
 
 /*
  * Add a filename to list of included files
  */
-void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *fname)
+void add_fname_to_include_list(FF_PKT *ff, int prefixed, const char *fname)
 {
    int len, j;
    struct s_included_file *inc;
    char *p;
+   const char *rp;
 
    len = strlen(fname);
 
    inc =(struct s_included_file *)bmalloc(sizeof(struct s_included_file) + len + 1);
    inc->options = 0;
-   inc->VerifyOpts[0] = 'V'; 
+   inc->VerifyOpts[0] = 'V';
    inc->VerifyOpts[1] = ':';
    inc->VerifyOpts[2] = 0;
 
    /* prefixed = preceded with options */
    if (prefixed) {
-      for (p=fname; *p && *p != ' '; p++) {
-        switch (*p) {
-            case 'a':                 /* alway replace */
-            case '0':                 /* no option */
-              break;
-            case 'f':
-              inc->options |= OPT_multifs;
-              break;
-            case 'h':                 /* no recursion */
-              inc->options |= OPT_no_recursion;
-              break;
-            case 'M':                 /* MD5 */
-              inc->options |= OPT_compute_MD5;
-              break;
-            case 'S':
-              inc->options |= OPT_compute_SHA1;
-              break;
-            case 'n':
-              inc->options |= OPT_never_replace;
-              break;
-            case 'r':                 /* read fifo */
-              inc->options |= OPT_read_fifo;
-              break;
-            case 's':
-              inc->options |= OPT_sparse;
-              break;
-            case 'V':                  /* verify options */
-              /* Copy Verify Options */
-               for (j=0; *p && *p != ':'; p++) {
-                 inc->VerifyOpts[j] = *p;
-                 if (j < (int)sizeof(inc->VerifyOpts) - 1) {
-                    j++;
-                 }
-              }
-              inc->VerifyOpts[j] = 0;
-              break;
-            case 'w':
-              inc->options |= OPT_replace_if_newer;
-              break;
-            case 'Z':                 /* gzip compression */
-              inc->options |= OPT_GZIP_compression;
-               inc->level = *++p - '0';
-               Dmsg1(200, "Compression level=%d\n", inc->level);
-              break;
-           default:
-               Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *p);
-              break;
-        }
+      for (rp=fname; *rp && *rp != ' '; rp++) {
+         switch (*rp) {
+         case 'a':                 /* alway replace */
+         case '0':                 /* no option */
+            break;
+         case 'f':
+            inc->options |= FO_MULTIFS;
+            break;
+         case 'h':                 /* no recursion */
+            inc->options |= FO_NO_RECURSION;
+            break;
+         case 'M':                 /* MD5 */
+            inc->options |= FO_MD5;
+            break;
+         case 'n':
+            inc->options |= FO_NOREPLACE;
+            break;
+         case 'p':                 /* use portable data format */
+            inc->options |= FO_PORTABLE;
+            break;
+         case 'r':                 /* read fifo */
+            inc->options |= FO_READFIFO;
+            break;
+         case 'S':
+            inc->options |= FO_SHA1;
+            break;
+         case 's':
+            inc->options |= FO_SPARSE;
+            break;
+         case 'm':
+            inc->options |= FO_MTIMEONLY;
+            break;
+         case 'k':
+            inc->options |= FO_KEEPATIME;
+            break;
+         case 'V':                  /* verify options */
+            /* Copy Verify Options */
+            for (j=0; *rp && *rp != ':'; rp++) {
+               inc->VerifyOpts[j] = *rp;
+               if (j < (int)sizeof(inc->VerifyOpts) - 1) {
+                  j++;
+               }
+            }
+            inc->VerifyOpts[j] = 0;
+            break;
+         case 'w':
+            inc->options |= FO_IF_NEWER;
+            break;
+         case 'A':
+            inc->options |= FO_ACL;
+            break;
+         case 'Z':                 /* compression */
+            rp++;                   /* skip Z */
+            if (*rp >= '0' && *rp <= '9') {
+               inc->options |= FO_COMPRESS;
+               inc->algo = COMPRESS_GZIP;
+               inc->Compress_level = *rp - '0';
+            }
+               else if (*rp == 'o') {
+               inc->options |= FO_COMPRESS;
+               inc->algo = COMPRESS_LZO1X;
+               inc->Compress_level = 1; /* not used with LZO */
+            }
+            Dmsg2(200, "Compression alg=%d level=%d\n", inc->algo, inc->Compress_level);
+            break;
+         case 'K':
+            inc->options |= FO_NOATIME;
+            break;
+         case 'X':
+            inc->options |= FO_XATTR;
+            break;
+         default:
+            Emsg1(M_ERROR, 0, _("Unknown include/exclude option: %c\n"), *rp);
+            break;
+         }
       }
       /* Skip past space(s) */
-      for ( ; *p == ' '; p++)
-        {}
+      for ( ; *rp == ' '; rp++)
+         {}
    } else {
-      p = fname;
+      rp = fname;
    }
 
-   strcpy(inc->fname, p);                
+   strcpy(inc->fname, rp);
    p = inc->fname;
    len = strlen(p);
    /* Zap trailing slashes.  */
    p += len - 1;
-   while (p > inc->fname && *p == '/') {
+   while (p > inc->fname && IsPathSeparator(*p)) {
       *p-- = 0;
       len--;
    }
@@ -171,11 +223,11 @@ void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *fname)
    inc->pattern = 0;
    for (p=inc->fname; *p; p++) {
       if (*p == '*' || *p == '[' || *p == '?') {
-        inc->pattern = 1;
-        break;
+         inc->pattern = 1;
+         break;
       }
    }
-#ifdef HAVE_CYGWIN
+#if defined(HAVE_WIN32)
    /* Convert any \'s into /'s */
    for (p=inc->fname; *p; p++) {
       if (*p == '\\') {
@@ -192,43 +244,44 @@ void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *fname)
       struct s_included_file *next;
       /* Walk to end of list */
       for (next=ff->included_files_list; next->next; next=next->next)
-        { }
+         { }
       next->next = inc;
-   }  
-   Dmsg1(50, "add_fname_to_include fname=%s\n", inc->fname);
+   }
+   Dmsg4(100, "add_fname_to_include prefix=%d compres=%d alg= %d fname=%s\n",
+         prefixed, !!(inc->options & FO_COMPRESS), inc->algo, inc->fname);
 }
 
 /*
  * We add an exclude name to either the exclude path
  *  list or the exclude filename list.
  */
-void add_fname_to_exclude_list(FF_PKT *ff, char *fname)
+void add_fname_to_exclude_list(FF_PKT *ff, const char *fname)
 {
    int len;
    struct s_excluded_file *exc, **list;
 
-#ifdef HAVE_CYGWIN
-   /* Convert any \'s into /'s */
-   for (char *p=fname; *p; p++) {
-      if (*p == '\\') {
-         *p = '/';
-      }
-   }
-#endif
    Dmsg1(20, "Add name to exclude: %s\n", fname);
 
-   if (strchr(fname, '/')) {
+   if (first_path_separator(fname) != NULL) {
       list = &ff->excluded_paths_list;
    } else {
       list = &ff->excluded_files_list;
    }
-  
+
    len = strlen(fname);
 
    exc = (struct s_excluded_file *)bmalloc(sizeof(struct s_excluded_file) + len + 1);
    exc->next = *list;
    exc->len = len;
-   strcpy(exc->fname, fname);                
+   strcpy(exc->fname, fname);
+#if defined(HAVE_WIN32)
+   /* Convert any \'s into /'s */
+   for (char *p=exc->fname; *p; p++) {
+      if (*p == '\\') {
+         *p = '/';
+      }
+   }
+#endif
    *list = exc;
 }
 
@@ -240,7 +293,7 @@ struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_fil
 {
    struct s_included_file *inc;
 
-   if (ainc == NULL) { 
+   if (ainc == NULL) {
       inc = ff->included_files_list;
    } else {
       inc = ainc->next;
@@ -250,7 +303,8 @@ struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_fil
     */
    if (inc) {
       ff->flags = inc->options;
-      ff->GZIP_level = inc->level;
+      ff->Compress_algo = inc->algo;
+      ff->Compress_level = inc->Compress_level;
    }
    return inc;
 }
@@ -260,18 +314,18 @@ struct s_included_file *get_next_included_file(FF_PKT *ff, struct s_included_fil
  *  file is included possibly with wild-cards.
  */
 
-int file_is_included(FF_PKT *ff, char *file)
+int file_is_included(FF_PKT *ff, const char *file)
 {
    struct s_included_file *inc = ff->included_files_list;
    int len;
 
    for ( ; inc; inc=inc->next ) {
       if (inc->pattern) {
-        if (fnmatch(inc->fname, file, FNM_LEADING_DIR) == 0) {
-           return 1;
-        }
-        continue;
-      }                            
+         if (fnmatch(inc->fname, file, fnmode|FNM_LEADING_DIR) == 0) {
+            return 1;
+         }
+         continue;
+      }
       /*
        * No wild cards. We accept a match to the
        *  end of any component.
@@ -279,14 +333,14 @@ int file_is_included(FF_PKT *ff, char *file)
       Dmsg2(900, "pat=%s file=%s\n", inc->fname, file);
       len = strlen(file);
       if (inc->len == len && strcmp(inc->fname, file) == 0) {
-        return 1;
+         return 1;
       }
-      if (inc->len < len && file[inc->len] == '/' && 
-         strncmp(inc->fname, file, inc->len) == 0) {
-        return 1;
+      if (inc->len < len && IsPathSeparator(file[inc->len]) &&
+          strncmp(inc->fname, file, inc->len) == 0) {
+         return 1;
       }
-      if (inc->len == 1 && inc->fname[0] == '/') {
-        return 1;
+      if (inc->len == 1 && IsPathSeparator(inc->fname[0])) {
+         return 1;
       }
    }
    return 0;
@@ -298,15 +352,15 @@ int file_is_included(FF_PKT *ff, char *file)
  * Determine if the file is excluded or not.
  */
 static int
-file_in_excluded_list(struct s_excluded_file *exc, char *file)
+file_in_excluded_list(struct s_excluded_file *exc, const char *file)
 {
    if (exc == NULL) {
       Dmsg0(900, "exc is NULL\n");
    }
    for ( ; exc; exc=exc->next ) {
-      if (fnmatch(exc->fname, file, FNM_PATHNAME) == 0) {
+      if (fnmatch(exc->fname, file, fnmode|FNM_PATHNAME) == 0) {
          Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
-        return 1;
+         return 1;
       }
       Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
    }
@@ -320,13 +374,19 @@ file_in_excluded_list(struct s_excluded_file *exc, char *file)
  *  of an excluded directory.
  */
 
-int file_is_excluded(FF_PKT *ff, char *file)
+int file_is_excluded(FF_PKT *ff, const char *file)
 {
-   char *p;
+   const char *p;
 
-   if (win32_client && file[1] == ':') {
+#if defined(HAVE_WIN32)
+   /*
+    *  ***NB*** this removes the drive from the exclude
+    *  rule.  Why?????
+    */
+   if (file[1] == ':') {
       file += 2;
    }
+#endif
 
    if (file_in_excluded_list(ff->excluded_paths_list, file)) {
       return 1;
@@ -335,9 +395,9 @@ int file_is_excluded(FF_PKT *ff, char *file)
    /* Try each component */
    for (p = file; *p; p++) {
       /* Match from the beginning of a component only */
-      if ((p == file || (*p != '/' && *(p-1) == '/'))
-          && file_in_excluded_list(ff->excluded_files_list, p)) {
-        return 1;
+      if ((p == file || (!IsPathSeparator(*p) && IsPathSeparator(p[-1])))
+           && file_in_excluded_list(ff->excluded_files_list, p)) {
+         return 1;
       }
    }
    return 0;