]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/breg.c
Added fix for bug #1275 where acl or xattr data is saved for virtual filenames genera...
[bacula/bacula] / bacula / src / lib / breg.c
index 6c9a4ef560a04093f24c54c7043d3fee1ae31765..a0a907f75314df51862088c6de05d7e4b41558f0 100644 (file)
@@ -1,22 +1,14 @@
 /*
- * Manipulation routines for BREGEXP list
- *
- *  Eric Bollengier, March 2007
- *
- *  Version $Id$
- *
- */
-/*
-   Bacula\81Â\81® - The Network Backup Solution
+   Bacula® - The Network Backup Solution
 
-   Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
+   Copyright (C) 2006-2008 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
    modify it under the terms of version two of the GNU General Public
-   License as published by the Free Software Foundation plus additions
-   that are listed in the file LICENSE.
+   License as published by the Free Software Foundation and included
+   in the file LICENSE.
 
    This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Bacula\81Â\81® is a registered trademark of John Walker.
+   Bacula® is a registered trademark of Kern Sibbald.
    The licensor of Bacula is the Free Software Foundation Europe
-   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Z\81Ã\81¼rich,
+   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zuerich,
    Switzerland, email:ftf@fsfeurope.org.
 */
+/*
+ * Manipulation routines for BREGEXP list
+ *
+ *  Eric Bollengier, March 2007
+ *
+ *  Version $Id$
+ *
+ */
 
 
 #include "bacula.h"
@@ -55,15 +55,6 @@ BREGEXP *new_bregexp(const char *motif)
    self->result = get_pool_memory(PM_FNAME);
    self->result[0] = '\0';
 
-#ifdef HAVE_REGEX_H
-   /* TODO: que devient cette memoire... */
-   self->_regs_match = (int *) bmalloc (2*RE_NREGS * sizeof(int));
-
-   self->regs.num_regs = RE_NREGS;
-   self->regs.start = self->_regs_match;
-   self->regs.end   = self->_regs_match+(RE_NREGS * sizeof(int));
-#endif 
-
    return self;
 }
 
@@ -81,10 +72,6 @@ void free_bregexp(BREGEXP *self)
    if (self->result) {
       free_pool_memory(self->result);
    }
-   if (self->_regs_match) {
-      bfree(self->_regs_match);
-   }
-
    regfree(&self->preg);
    bfree(self);
 }
@@ -153,16 +140,16 @@ bool BREGEXP::extract_regexp(const char *motif)
    char sep = motif[0];
 
    if (!(sep == '!' || 
-        sep == ':' || 
-        sep == ';' || 
-        sep == '|' || 
-        sep == ',' || 
-        sep == '&' || 
-        sep == '%' || 
-        sep == '=' || 
-        sep == '~' ||
-        sep == '/' ||
-        sep == '#'   )) 
+         sep == ':' || 
+         sep == ';' || 
+         sep == '|' || 
+         sep == ',' || 
+         sep == '&' || 
+         sep == '%' || 
+         sep == '=' || 
+         sep == '~' ||
+         sep == '/' ||
+         sep == '#'   )) 
    {
       return false;
    }
@@ -176,27 +163,27 @@ bool BREGEXP::extract_regexp(const char *motif)
 
    while (*search && !ok) {
       if (search[0] == '\\' && search[1] == sep) {
-        *dest++ = *++search;       /* we skip separator */ 
+         *dest++ = *++search;       /* we skip separator */ 
 
       } else if (search[0] == '\\' && search[1] == '\\') {
-        *dest++ = *++search;       /* we skip the second \ */
+         *dest++ = *++search;       /* we skip the second \ */
 
       } else if (*search == sep) {  /* we found end of expression */
-        *dest++ = '\0';
+         *dest++ = '\0';
 
-        if (subst) {           /* already have found motif */
-           ok = true;
+         if (subst) {           /* already have found motif */
+            ok = true;
 
-        } else {
-           *dest++ = *++search; /* we skip separator */ 
-           subst = dest;        /* get replaced string */
-        }
+         } else {
+            *dest++ = *++search; /* we skip separator */ 
+            subst = dest;        /* get replaced string */
+         }
 
       } else {
-        *dest++ = *search++;
+         *dest++ = *search++;
       }
    }
-   *dest = '\0';               /* in case of */
+   *dest = '\0';                /* in case of */
    
    if (!ok || !subst) {
       /* bad regexp */
@@ -207,16 +194,16 @@ bool BREGEXP::extract_regexp(const char *motif)
    /* find options */
    while (*search && !ok) {
       if (*search == 'i') {
-        options |= REG_ICASE;
+         options |= REG_ICASE;
 
       } else if (*search == 'g') {
-             /* recherche multiple*/
+              /* recherche multiple*/
 
       } else if (*search == sep) {
-        /* skip separator */
+         /* skip separator */
 
-      } else {                 /* end of options */
-        ok = true;
+      } else {                  /* end of options */
+         ok = true;
       }
       search++;
    }
@@ -229,48 +216,32 @@ bool BREGEXP::extract_regexp(const char *motif)
       return false;
    }
 
-   eor = search;               /* useful to find the next regexp in where */
+   eor = search;                /* useful to find the next regexp in where */
 
    return true;
 }
 
-#ifndef HAVE_REGEX_H
- #define BREGEX_CAST unsigned
-#else
- #define BREGEX_CAST const
-#endif
-
 /* return regexp->result */
 char *BREGEXP::replace(const char *fname)
 {
-   success = false;            /* use this.success to known if it's ok */
+   success = false;             /* use this.success to known if it's ok */
    int flen = strlen(fname);
-#ifndef HAVE_REGEX_H
-   int i;
-   check_pool_memory_size(lcase, flen);
-   for(i=0; fname[i] ; i++) {
-      lcase[i] = tolower(fname[i]);
-   }
-   lcase[i] = '\0';
-   int rc = re_search(&preg, (BREGEX_CAST char*) lcase, flen, 0, flen, &regs);
-#else
-   int rc = re_search(&preg, (BREGEX_CAST char*) fname, flen, 0, flen, &regs);
-#endif
+   int rc = regexec(&preg, fname, BREG_NREGS, regs, 0);
 
-   if (rc < 0) {
+   if (rc == REG_NOMATCH) {
       Dmsg0(500, "bregexp: regex mismatch\n");
       return return_fname(fname, flen);
    }
 
-   int len = compute_dest_len(fname, &regs);
+   int len = compute_dest_len(fname, regs);
 
    if (len) {
       result = check_pool_memory_size(result, len);
-      edit_subst(fname, &regs);
+      edit_subst(fname, regs);
       success = true;
       Dmsg2(500, "bregexp: len = %i, result_len = %i\n", len, strlen(result));
 
-   } else {                    /* error in substitution */
+   } else {                     /* error in substitution */
       Dmsg0(100, "bregexp: error in substitution\n");
       return return_fname(fname, flen);
    }
@@ -285,7 +256,7 @@ char *BREGEXP::return_fname(const char *fname, int len)
    return result;
 }
 
-int BREGEXP::compute_dest_len(const char *fname, struct re_registers *regs)
+int BREGEXP::compute_dest_len(const char *fname, regmatch_t regs[])
 {
    int len=0;
    char *p;
@@ -297,35 +268,35 @@ int BREGEXP::compute_dest_len(const char *fname, struct re_registers *regs)
    }
 
    /* match failed ? */
-   if (regs->start[0] < 0) {
+   if (regs[0].rm_so < 0) {
       return 0;
    }
 
    for (p = psubst++; *p ; p = psubst++) {
       /* match $1 \1 back references */
       if ((*p == '$' || *p == '\\') && ('0' <= *psubst && *psubst <= '9')) {
-        no = *psubst++ - '0';
+         no = *psubst++ - '0';
 
-        /* we check if the back reference exists */
-        /* references can not match if we are using (..)? */
+         /* we check if the back reference exists */
+         /* references can not match if we are using (..)? */
 
-        if (regs->start[no] >= 0 && regs->end[no] >= 0) { 
-           len += regs->end[no] - regs->start[no];
-        }
-        
+         if (regs[no].rm_so >= 0 && regs[no].rm_eo >= 0) { 
+            len += regs[no].rm_eo - regs[no].rm_so;
+         }
+         
       } else {
-        len++;
+         len++;
       }
    }
 
    /* $0 is replaced by subst */
-   len -= regs->end[0] - regs->start[0];
+   len -= regs[0].rm_eo - regs[0].rm_so;
    len += strlen(fname) + 1;
 
    return len;
 }
 
-char *BREGEXP::edit_subst(const char *fname, struct re_registers *regs)
+char *BREGEXP::edit_subst(const char *fname, regmatch_t regs[])
 {
    int i;
    char *p;
@@ -337,7 +308,7 @@ char *BREGEXP::edit_subst(const char *fname, struct re_registers *regs)
     *  on recopie le debut fname -> regs->start[0]
     */
    
-   for (i = 0; i < regs->start[0] ; i++) {
+   for (i = 0; i < regs[0].rm_so ; i++) {
       result[i] = fname[i];
    }
 
@@ -346,22 +317,22 @@ char *BREGEXP::edit_subst(const char *fname, struct re_registers *regs)
    for (p = psubst++; *p ; p = psubst++) {
       /* match $1 \1 back references */
       if ((*p == '$' || *p == '\\') && ('0' <= *psubst && *psubst <= '9')) {
-        no = *psubst++ - '0';
+         no = *psubst++ - '0';
 
-        /* have a back reference ? */
-        if (regs->start[no] >= 0 && regs->end[no] >= 0) {
-           len = regs->end[no] - regs->start[no];
-           bstrncpy(result + i, fname + regs->start[no], len + 1);
-           i += len ;
-        }
+         /* have a back reference ? */
+         if (regs[no].rm_so >= 0 && regs[no].rm_eo >= 0) {
+            len = regs[no].rm_eo - regs[no].rm_so;
+            bstrncpy(result + i, fname + regs[no].rm_so, len + 1);
+            i += len ;
+         }
 
       } else {
-        result[i++] = *p;
+         result[i++] = *p;
       }
    }
 
    /* we copy what is out of the match */
-   strcpy(result + i, fname + regs->end[0]);
+   strcpy(result + i, fname + regs[0].rm_eo);
 
    return result;
 }
@@ -369,15 +340,15 @@ char *BREGEXP::edit_subst(const char *fname, struct re_registers *regs)
 /* escape sep char and \
  * dest must be long enough (src*2+1)
  * return end of the string */
-char *bregexp_escape_string(char *dest, char *src, char sep)
+char *bregexp_escape_string(char *dest, const char *src, const char sep)
 {
    char *ret = dest;
    while (*src)
    {
       if (*src == sep) {
-        *dest++ = '\\';
+         *dest++ = '\\';
       } else if (*src == '\\') {
-        *dest++ = '\\';
+         *dest++ = '\\';
       }
       *dest++ = *src++;
    }
@@ -386,22 +357,22 @@ char *bregexp_escape_string(char *dest, char *src, char sep)
    return ret; 
 }
 
-static char regexp_sep = '!';
-static char *str_strip_prefix = "!%s!!i";
-static char *str_add_prefix   = "!^!%s!";
-static char *str_add_suffix   = "!([^/])$!$1%s!";
+static const char regexp_sep = '!';
+static const char *str_strip_prefix = "!%s!!i";
+static const char *str_add_prefix   = "!^!%s!";
+static const char *str_add_suffix   = "!([^/])$!$1%s!";
 
 int bregexp_get_build_where_size(char *strip_prefix, 
-                                char *add_prefix, 
-                                char *add_suffix)
+                                 char *add_prefix, 
+                                 char *add_suffix)
 {
    int str_size = ((strip_prefix?strlen(strip_prefix)+strlen(str_strip_prefix):0) +
-                  (add_prefix?strlen(add_prefix)+strlen(str_add_prefix)      :0) +  
-                  (add_suffix?strlen(add_suffix)+strlen(str_add_suffix)      :0) )
+                   (add_prefix?strlen(add_prefix)+strlen(str_add_prefix)      :0) +  
+                   (add_suffix?strlen(add_suffix)+strlen(str_add_suffix)      :0) )
          /* escape + 3*, + \0 */
-           * 2    + 3   + 1;
+            * 2    + 3   + 1;
 
-   Dmsg1(1, "bregexp_get_build_where_size = %i\n", str_size);
+   Dmsg1(200, "bregexp_get_build_where_size = %i\n", str_size);
    return str_size;
 }
 
@@ -415,7 +386,7 @@ int bregexp_get_build_where_size(char *strip_prefix,
  * 
  */
 char *bregexp_build_where(char *dest, int str_size,
-                         char *strip_prefix, 
+                          char *strip_prefix, 
                           char *add_prefix, 
                           char *add_suffix)
 {
@@ -427,21 +398,21 @@ char *bregexp_build_where(char *dest, int str_size,
    
    if (strip_prefix) {
       len += bsnprintf(dest, str_size - len, str_strip_prefix,
-                      bregexp_escape_string(str_tmp, strip_prefix, regexp_sep));
+                       bregexp_escape_string(str_tmp, strip_prefix, regexp_sep));
    }
 
    if (add_suffix) {
       if (len) dest[len++] = ',';
 
       len += bsnprintf(dest + len,  str_size - len, str_add_suffix,
-                      bregexp_escape_string(str_tmp, add_suffix, regexp_sep));
+                       bregexp_escape_string(str_tmp, add_suffix, regexp_sep));
    }
 
    if (add_prefix) {
       if (len) dest[len++] = ',';
 
       len += bsnprintf(dest + len, str_size - len, str_add_prefix, 
-                      bregexp_escape_string(str_tmp, add_prefix, regexp_sep));
+                       bregexp_escape_string(str_tmp, add_prefix, regexp_sep));
    }
 
    free_pool_memory(str_tmp);