]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/findlib/match.c
Apply Preben 'Peppe' Guldberg <peppe@wielders.org>
[bacula/bacula] / bacula / src / findlib / match.c
index 61b926b1fa00cf6dcae98ac246d2cb8c8e2c027b..fe84276b3cb74da018fe2d828a0dc9b0bb2b8aba 100644 (file)
@@ -2,6 +2,10 @@
  *  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
+ *
  *   Kern E. Sibbald, December MMI
  *
  */
 #define FNM_LEADING_DIR 0
 #endif
 
+/* Fold case in fnmatch() on Win32 */
+#ifdef WIN32
+static const int fnmode = FNM_CASEFOLD;
+#else
+static const int fnmode = 0;
+#endif
+
+
 #undef bmalloc
 #define bmalloc(x) sm_malloc(__FILE__, __LINE__, x)
 
 extern const int win32_client;
-       
+
 /*
  * Initialize structures for filename matching
  */
@@ -49,7 +61,7 @@ void init_include_exclude_files(FF_PKT *ff)
 }
 
 /*
- * Done doing filename matching, release all 
+ * Done doing filename matching, release all
  *  resources used.
  */
 void term_include_exclude_files(FF_PKT *ff)
@@ -74,97 +86,98 @@ void term_include_exclude_files(FF_PKT *ff)
       free(exc);
       exc = next_exc;
    }
-   
+
 }
 
 /*
  * 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 */
+      for (rp=fname; *rp && *rp != ' '; rp++) {
+        switch (*rp) {
+        case 'a':                 /* alway replace */
+        case '0':                 /* no option */
            break;
-         case 'f':
+        case 'f':
            inc->options |= FO_MULTIFS;
            break;
-         case 'h':                 /* no recursion */
+        case 'h':                 /* no recursion */
            inc->options |= FO_NO_RECURSION;
            break;
-         case 'M':                 /* MD5 */
+        case 'M':                 /* MD5 */
            inc->options |= FO_MD5;
            break;
-         case 'n':
+        case 'n':
            inc->options |= FO_NOREPLACE;
            break;
-         case 'p':                 /* use portable data format */
+        case 'p':                 /* use portable data format */
            inc->options |= FO_PORTABLE;
            break;
-         case 'r':                 /* read fifo */
+        case 'r':                 /* read fifo */
            inc->options |= FO_READFIFO;
            break;
-         case 'S':
+        case 'S':
            inc->options |= FO_SHA1;
            break;
-         case 's':
+        case 's':
            inc->options |= FO_SPARSE;
            break;
-         case 'm':
+        case 'm':
            inc->options |= FO_MTIMEONLY;
            break;
-         case 'k':
+        case 'k':
            inc->options |= FO_KEEPATIME;
            break;
-         case 'V':                  /* verify options */
+        case 'V':                  /* verify options */
            /* Copy Verify Options */
-            for (j=0; *p && *p != ':'; p++) {
-              inc->VerifyOpts[j] = *p;
+           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':
+        case 'w':
            inc->options |= FO_IF_NEWER;
            break;
-         case 'A':
+        case 'A':
            inc->options |= FO_ACL;
            break;
         case 'Z':                 /* gzip compression */
            inc->options |= FO_GZIP;
-            inc->level = *++p - '0';
-            Dmsg1(200, "Compression level=%d\n", inc->level);
+           inc->level = *++rp - '0';
+           Dmsg1(200, "Compression level=%d\n", inc->level);
            break;
         default:
-            Emsg1(M_ERROR, 0, "Unknown include/exclude option: %c\n", *p);
+           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.  */
@@ -186,7 +199,7 @@ void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *fname)
    /* Convert any \'s into /'s */
    for (p=inc->fname; *p; p++) {
       if (*p == '\\') {
-         *p = '/';
+        *p = '/';
       }
    }
 #endif
@@ -201,7 +214,7 @@ void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *fname)
       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);
 }
 
@@ -209,33 +222,37 @@ void add_fname_to_include_list(FF_PKT *ff, int prefixed, char *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;
 
-#if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
-   /* 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 defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
+   if (strchr(fname, '/') || strchr(fname, '\\')) {
+#else
    if (strchr(fname, '/')) {
+#endif
       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_CYGWIN) || defined(HAVE_WIN32)
+   /* Convert any \'s into /'s */
+   for (char *p=exc->fname; *p; p++) {
+      if (*p == '\\') {
+        *p = '/';
+      }
+   }
+#endif
    *list = exc;
 }
 
@@ -247,7 +264,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;
@@ -267,18 +284,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) {
+        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.
@@ -288,7 +305,7 @@ int file_is_included(FF_PKT *ff, char *file)
       if (inc->len == len && strcmp(inc->fname, file) == 0) {
         return 1;
       }
-      if (inc->len < len && file[inc->len] == '/' && 
+      if (inc->len < len && file[inc->len] == '/' &&
          strncmp(inc->fname, file, inc->len) == 0) {
         return 1;
       }
@@ -305,14 +322,14 @@ 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) {
-         Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
+      if (fnmatch(exc->fname, file, fnmode|FNM_PATHNAME) == 0) {
+        Dmsg2(900, "Match exc pat=%s: file=%s:\n", exc->fname, file);
         return 1;
       }
       Dmsg2(900, "No match exc pat=%s: file=%s:\n", exc->fname, file);
@@ -327,10 +344,14 @@ 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;
 
+   /*
+    *  ***NB*** this removes the drive from the exclude
+    *  rule.  Why?????
+    */
    if (win32_client && file[1] == ':') {
       file += 2;
    }
@@ -344,7 +365,7 @@ int file_is_excluded(FF_PKT *ff, char *file)
       /* 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;   
+        return 1;
       }
    }
    return 0;