]> git.sur5r.net Git - bacula/bacula/commitdiff
ebl cleanup File relocation to use RegexWhere instead of
authorEric Bollengier <eric@eb.homelinux.org>
Tue, 24 Apr 2007 20:05:52 +0000 (20:05 +0000)
committerEric Bollengier <eric@eb.homelinux.org>
Tue, 24 Apr 2007 20:05:52 +0000 (20:05 +0000)
     where char* + where_use_regexp boolean.

git-svn-id: https://bacula.svn.sourceforge.net/svnroot/bacula/trunk@4621 91ce42f0-d328-0410-95d8-f526ca767f89

bacula/src/dird/dird_conf.c
bacula/src/dird/restore.c
bacula/src/dird/ua.h
bacula/src/dird/ua_restore.c
bacula/src/dird/ua_run.c
bacula/src/filed/job.c
bacula/src/jcr.h
bacula/src/lib/attr.c
bacula/src/lib/jcr.c

index c1b81bca152d8901d651181f3c2e8e0bd1f967f0..c87d79d21e6c12a416dc18d37b22fbde01bcda25 100644 (file)
@@ -615,12 +615,11 @@ void dump_resource(int type, RES *reshdr, void sendit(void *sock, const char *fm
          sendit(sock, _("  --> "));
          dump_resource(-R_SCHEDULE, (RES *)res->res_job.schedule, sendit, sock);
       }
-      if (res->res_job.RestoreWhere) {
-        if (res->res_job.where_use_regexp) {
-           sendit(sock, _("  --> RegexWhere=%s\n"), NPRT(res->res_job.RestoreWhere));
-        } else {
-           sendit(sock, _("  --> Where=%s\n"), NPRT(res->res_job.RestoreWhere));
-        }
+      if (res->res_job.RestoreWhere && !res->res_job.RegexWhere) {
+          sendit(sock, _("  --> Where=%s\n"), NPRT(res->res_job.RestoreWhere));
+      }
+      if (res->res_job.RegexWhere) {
+          sendit(sock, _("  --> RegexWhere=%s\n"), NPRT(res->res_job.RegexWhere));
       }
       if (res->res_job.RestoreBootstrap) {
          sendit(sock, _("  --> Bootstrap=%s\n"), NPRT(res->res_job.RestoreBootstrap));
@@ -1323,32 +1322,26 @@ void save_resource(int type, RES_ITEM *items, int pass)
          res->res_job.run_cmds   = res_all.res_job.run_cmds;
          res->res_job.RunScripts = res_all.res_job.RunScripts;
 
-        if (res->res_job.strip_prefix ||
-            res->res_job.add_suffix   ||
-            res->res_job.add_prefix)
+        if (!res->res_job.RegexWhere 
+            &&
+            (res->res_job.strip_prefix ||
+             res->res_job.add_suffix   ||
+             res->res_job.add_prefix))
         {
-           if (res->res_job.RestoreWhere) {
-              free(res->res_job.RestoreWhere);
-           }
            int len = bregexp_get_build_where_size(res->res_job.strip_prefix,
                                                   res->res_job.add_prefix,
                                                   res->res_job.add_suffix);
-           res->res_job.RestoreWhere = (char *) bmalloc (len * sizeof(char));
-           bregexp_build_where(res->res_job.RestoreWhere, len,
+           res->res_job.RegexWhere = (char *) bmalloc (len * sizeof(char));
+           bregexp_build_where(res->res_job.RegexWhere, len,
                                res->res_job.strip_prefix,
                                res->res_job.add_prefix,
                                res->res_job.add_suffix);
-           res->res_job.where_use_regexp = true;
-
            /* TODO: test bregexp */
         }
 
-        if (res->res_job.RegexWhere) {
-           if (res->res_job.RestoreWhere) {
-              free(res->res_job.RestoreWhere);
-           }
-           res->res_job.RestoreWhere = bstrdup(res->res_job.RegexWhere);
-           res->res_job.where_use_regexp = true;
+        if (res->res_job.RegexWhere && res->res_job.RestoreWhere) {
+           free(res->res_job.RestoreWhere);
+           res->res_job.RestoreWhere = NULL;
         }
 
          break;
index e3c5b5029020143abeaeab908badddcee6ba728c..973c1ebb805740ae23e1ad7994ad37cf741b2a43 100644 (file)
@@ -173,7 +173,7 @@ bool do_restore(JCR *jcr)
    }
 
    /* Send restore command */
-   char replace, *where, *cmd;
+   char replace, *where, *cmd=NULL;
    char empty = '\0';
 
    if (jcr->replace != 0) {
@@ -183,22 +183,26 @@ bool do_restore(JCR *jcr)
    } else {
       replace = REPLACE_ALWAYS;       /* always replace */
    }
-   if (jcr->where) {
+   
+   where = &empty;                   /* default */
+
+   if (jcr->RegexWhere) {
+      where = jcr->RegexWhere;             /* override */
+      cmd = restorecmdR;
+   } else if (jcr->job->RegexWhere) {
+      where = jcr->job->RegexWhere; /* no override take from job */
+      cmd = restorecmdR;
+
+   } else if (jcr->where) {
       where = jcr->where;             /* override */
+      cmd = restorecmd;
    } else if (jcr->job->RestoreWhere) {
       where = jcr->job->RestoreWhere; /* no override take from job */
-   } else {
-      where = &empty;                 /* None */
-   }
+      cmd = restorecmd;
+   } 
    
    jcr->prefix_links = jcr->job->PrefixLinks;
 
-   if (jcr->where_use_regexp) {
-      cmd = restorecmdR;
-   } else {
-      cmd = restorecmd;
-   }
-
    bash_spaces(where);
    bnet_fsend(fd, cmd, replace, jcr->prefix_links, where);
    unbash_spaces(where);
index aabb2e1f6d0834fdf8793cfa46a86d7b3506c4e2..476bebc97124d64e491971518cb2cae25b3aadfb 100644 (file)
@@ -110,6 +110,7 @@ struct RESTORE_CTX {
    int restore_jobs;
    uint32_t selected_files;
    char *where;
+   char *RegexWhere;
    RBSR *bsr;
    POOLMEM *fname;                    /* filename only */
    POOLMEM *path;                     /* path only */
index 74154c3b6a34679a0419f0bf798f7e1d6f607b29..79d2466d181c357f6d250a8f6f3636bd74100048 100644 (file)
@@ -82,7 +82,6 @@ int restore_cmd(UAContext *ua, const char *cmd)
    JCR *jcr = ua->jcr;
    char *escaped_bsr_name = NULL;
    char *escaped_where_name = NULL;
-   bool where_use_regexp = false;
    char *strip_prefix, *add_prefix, *add_suffix, *regexp;
    strip_prefix = add_prefix = add_suffix = regexp = NULL;
 
@@ -113,15 +112,9 @@ int restore_cmd(UAContext *ua, const char *cmd)
       add_suffix = ua->argv[i];
    }
 
-   i = find_arg(ua, "where_use_regexp");
-   if (i >= 0) {
-      where_use_regexp = true;
-   }
-
    i = find_arg_with_value(ua, "regexwhere");
    if (i >= 0) {
-      where_use_regexp = true;
-      rx.where = ua->argv[i];
+      rx.RegexWhere = ua->argv[i];
    }
 
    if (strip_prefix || add_suffix || add_prefix) {
@@ -129,9 +122,16 @@ int restore_cmd(UAContext *ua, const char *cmd)
       regexp = (char *) bmalloc (len * sizeof(char));
 
       bregexp_build_where(regexp, len, strip_prefix, add_prefix, add_suffix);
-      where_use_regexp = true;
-      
-      rx.where = regexp;
+      rx.RegexWhere = regexp;
+   }
+
+   /* TODO: add acl for regexwhere ? */
+
+   if (rx.RegexWhere) {
+      if (!acl_access_ok(ua, Where_ACL, rx.RegexWhere)) {
+         ua->error_msg(_("\"RegexWhere\" specification not authorized.\n"));
+         goto bail_out;
+      }
    }
 
    if (rx.where) {
@@ -225,23 +225,28 @@ int restore_cmd(UAContext *ua, const char *cmd)
    }
 
    escaped_bsr_name = escape_filename(jcr->RestoreBootstrap);
-   escaped_where_name = escape_filename(rx.where);
 
    /* Build run command */
-   if (rx.where) {
-      if (!acl_access_ok(ua, Where_ACL, rx.where)) {
-         ua->error_msg(_("\"where\" specification not authorized.\n"));
-         goto bail_out;
-      }
+   if (rx.RegexWhere) {
+      escaped_where_name = escape_filename(rx.RegexWhere);
+      Mmsg(ua->cmd,
+          "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s\""
+          " regexwhere=\"%s\" files=%d catalog=\"%s\"",
+          job->name(), rx.ClientName, rx.store?rx.store->name():"",
+          escaped_bsr_name ? escaped_bsr_name : jcr->RestoreBootstrap,
+          escaped_where_name ? escaped_where_name : rx.RegexWhere,
+          rx.selected_files, ua->catalog->name());
 
+   } else if (rx.where) {
+      escaped_where_name = escape_filename(rx.where);
       Mmsg(ua->cmd,
           "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s\""
-          " %swhere=\"%s\" files=%d catalog=\"%s\"",
+          " where=\"%s\" files=%d catalog=\"%s\"",
           job->name(), rx.ClientName, rx.store?rx.store->name():"",
           escaped_bsr_name ? escaped_bsr_name : jcr->RestoreBootstrap,
-          where_use_regexp ? "regex" : "",
           escaped_where_name ? escaped_where_name : rx.where,
           rx.selected_files, ua->catalog->name());
+
    } else {
       Mmsg(ua->cmd,
           "run job=\"%s\" client=\"%s\" storage=\"%s\" bootstrap=\"%s\""
@@ -403,8 +408,7 @@ static int user_select_jobids_or_files(UAContext *ua, RESTORE_CTX *rx)
       "strip_prefix", /* 15 */
       "add_prefix",   /* 16 */
       "add_suffix",   /* 17 */
-      "where_use_regexp",/* 18 */
-      "regexwhere",   /* 19 like where + where_use_regexp */
+      "regexwhere",   /* 18 */
       NULL
    };
 
index eb776e428f1b9dad2d81bd5ab67017586f9fa00a..2171f4afe05ad30ba11b71e68d9c373c456399cc 100644 (file)
@@ -62,7 +62,7 @@ int run_cmd(UAContext *ua, const char *cmd)
 {
    JCR *jcr;
    char *job_name, *level_name, *jid, *store_name, *pool_name;
-   char *where, *fileset_name, *client_name, *bootstrap;
+   char *where, *fileset_name, *client_name, *bootstrap, *regexwhere;
    const char *replace;
    char *when, *verify_job_name, *catalog_name;
    char *previous_job_name;
@@ -72,7 +72,6 @@ int run_cmd(UAContext *ua, const char *cmd)
    int Priority = 0;
    int i, j, opt, files = 0;
    bool kw_ok;
-   bool where_use_regexp = false;
    JOB *job = NULL;
    JOB *verify_job = NULL;
    JOB *previous_job = NULL;
@@ -118,6 +117,7 @@ int run_cmd(UAContext *ua, const char *cmd)
    store_name = NULL;
    pool_name = NULL;
    where = NULL;
+   regexwhere = NULL;
    when = NULL;
    client_name = NULL;
    fileset_name = NULL;
@@ -192,12 +192,20 @@ int run_cmd(UAContext *ua, const char *cmd)
                kw_ok = true;
                break;
             case 8: /* regexwhere */
-            case 9: /* where */
-               /* TODO: this is ugly ... */
-               where_use_regexp = (j == 9)?false:true;/*regexwhere or where ?*/
-
-               if (where) {
-                  ua->send_msg(_("Where specified twice.\n"));
+                if (regexwhere || where) {
+                  ua->send_msg(_("RegexWhere or Where specified twice.\n"));
+                  return 0;
+               }
+              regexwhere = ua->argv[i];
+               if (!acl_access_ok(ua, Where_ACL, regexwhere)) {
+                  ua->send_msg(_("Forbidden \"regexwhere\" specified.\n"));
+                  return 0;
+               }
+               kw_ok = true;
+               break;
+           case 9: /* where */
+               if (where || regexwhere) {
+                  ua->send_msg(_("Where or RegexWhere specified twice.\n"));
                   return 0;
                }
                where = ua->argv[i];
@@ -484,7 +492,13 @@ int run_cmd(UAContext *ua, const char *cmd)
          free(jcr->where);
       }
       jcr->where = bstrdup(where);
-      jcr->where_use_regexp = where_use_regexp;
+   }
+
+   if (regexwhere) {
+      if (jcr->RegexWhere) {
+         free(jcr->RegexWhere);
+      }
+      jcr->RegexWhere = bstrdup(regexwhere);      
    }
 
    if (when) {
@@ -719,6 +733,10 @@ try_again:
          if (!get_cmd(ua, _("Please enter path prefix for restore (/ for none): "))) {
             break;
          }
+        if (jcr->RegexWhere) { /* cannot use regexwhere and where */
+           free(jcr->RegexWhere);
+           jcr->RegexWhere = NULL;
+        }
          if (jcr->where) {
             free(jcr->where);
             jcr->where = NULL;
@@ -727,7 +745,6 @@ try_again:
             ua->cmd[0] = 0;
          }
          jcr->where = bstrdup(ua->cmd);
-        jcr->where_use_regexp = false;
          goto try_again;
       case 9: 
         /* File relocation */
@@ -888,23 +905,28 @@ try_again_reg:
       jcr->where = NULL;
    }
 
+   /* replace the existing regexwhere */
+   if (jcr->RegexWhere) {
+      bfree(jcr->RegexWhere);
+      jcr->RegexWhere = NULL;
+   }
+
    if (rwhere) {
-      jcr->where = bstrdup(rwhere);
+      jcr->RegexWhere = bstrdup(rwhere);
    } else if (strip_prefix || add_prefix || add_suffix) {
       int len = bregexp_get_build_where_size(strip_prefix, add_prefix, add_suffix);
-      jcr->where = (char *) bmalloc(len*sizeof(char));
-      bregexp_build_where(jcr->where, len, strip_prefix, add_prefix, add_suffix);
+      jcr->RegexWhere = (char *) bmalloc(len*sizeof(char));
+      bregexp_build_where(jcr->RegexWhere, len, strip_prefix, add_prefix, add_suffix);
    }
 
-   regs = get_bregexps(jcr->where);
+   regs = get_bregexps(jcr->RegexWhere);
    if (regs) {
       free_bregexps(regs);
       delete regs;
-      jcr->where_use_regexp = true;
    } else {
-      if (jcr->where) {
-        bfree(jcr->where);
-        jcr->where = NULL;
+      if (jcr->RegexWhere) {
+        bfree(jcr->RegexWhere);
+        jcr->RegexWhere = NULL;
       }
       ua->send_msg(_("Cannot use your regexp.\n"));
    }
@@ -1078,19 +1100,26 @@ static bool display_job_parameters(UAContext *ua, JCR *jcr, JOB *job, char *veri
          if (ua->api) ua->signal(BNET_RUN_CMD);   
          ua->send_msg(_("Run Restore job\n"
                         "JobName:    %s\n"
-                        "Bootstrap:  %s\n"
-                        "%s %s\n"             /* Where or RegexWhere */
-                        "Replace:    %s\n"
+                        "Bootstrap:  %s\n"),
+                     job->name(),
+                     NPRT(jcr->RestoreBootstrap));
+
+        /* RegexWhere is take before RestoreWhere */
+        if (jcr->RegexWhere || (job->RegexWhere && !jcr->where)) {
+           ua->send_msg(_("RegexWhere: %s\n"),
+                        jcr->RegexWhere?jcr->RegexWhere:job->RegexWhere);
+        } else {
+           ua->send_msg(_("Where:      %s\n"),
+                        jcr->where?jcr->where:NPRT(job->RestoreWhere));
+        }
+
+        ua->send_msg(_("Replace:    %s\n"
                         "FileSet:    %s\n"
                         "Client:     %s\n"
                         "Storage:    %s\n"
                         "When:       %s\n"
                         "Catalog:    %s\n"
                         "Priority:   %d\n"),
-              job->name(),
-              NPRT(jcr->RestoreBootstrap),
-              jcr->where_use_regexp?"RegexWhere:":"Where:     ",
-              jcr->where?jcr->where:NPRT(job->RestoreWhere),
               replace,
               jcr->fileset->name(),
               jcr->client->name(),
@@ -1101,20 +1130,27 @@ static bool display_job_parameters(UAContext *ua, JCR *jcr, JOB *job, char *veri
       } else {
          if (ua->api) ua->signal(BNET_RUN_CMD);   
          ua->send_msg(_("Run Restore job\n"
-                       "JobName:    %s\n"
-                       "Bootstrap:  %s\n"
-                       "%s %s\n"             /* Where or RegexWhere */
-                       "Replace:    %s\n"
-                       "Client:     %s\n"
-                       "Storage:    %s\n"
-                       "JobId:      %s\n"
-                       "When:       %s\n"
-                       "Catalog:    %s\n"
-                       "Priority:   %d\n"),
-              job->name(),
-              NPRT(jcr->RestoreBootstrap),
-              jcr->where_use_regexp?"RegexWhere:":"Where:     ",
-              jcr->where?jcr->where:NPRT(job->RestoreWhere),
+                       "JobName:    %s\n"
+                       "Bootstrap:  %s\n"),
+                     job->name(),
+                     NPRT(jcr->RestoreBootstrap));
+                     
+        /* RegexWhere is take before RestoreWhere */
+        if (jcr->RegexWhere || (job->RegexWhere && !jcr->where)) {
+           ua->send_msg(_("RegexWhere: %s\n"),
+                        jcr->RegexWhere?jcr->RegexWhere:job->RegexWhere);
+        } else {
+           ua->send_msg(_("Where:      %s\n"),
+                        jcr->where?jcr->where:NPRT(job->RestoreWhere));
+        }
+
+        ua->send_msg(_("Replace:    %s\n"
+                       "Client:     %s\n"
+                       "Storage:    %s\n"
+                       "JobId:      %s\n"
+                       "When:       %s\n"
+                       "Catalog:    %s\n"
+                       "Priority:   %d\n"),
               replace,
               jcr->client->name(),
               jcr->rstore->name(),
index 096f587a1e47e3d2c2eedb11eea86000fe33f585..b75369d872abe109771f453fd1afa385dc2b3c2c 100644 (file)
@@ -1573,7 +1573,8 @@ static int restore_cmd(JCR *jcr)
 {
    BSOCK *dir = jcr->dir_bsock;
    BSOCK *sd = jcr->store_bsock;
-   POOLMEM *where;
+   POOLMEM *args;
+   bool use_regexwhere=false;
    int prefix_links;
    char replace;
    char ed1[50], ed2[50];
@@ -1583,38 +1584,40 @@ static int restore_cmd(JCR *jcr)
     */
    Dmsg0(150, "restore command\n");
    /* Pickup where string */
-   where = get_memory(dir->msglen+1);
-   *where = 0;
+   args = get_memory(dir->msglen+1);
+   *args = 0;
 
-   if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, where) != 3) {
-      if (sscanf(dir->msg, restorecmdR, &replace, &prefix_links, where) != 3){
+   if (sscanf(dir->msg, restorecmd, &replace, &prefix_links, args) != 3) {
+      if (sscanf(dir->msg, restorecmdR, &replace, &prefix_links, args) != 3){
          if (sscanf(dir->msg, restorecmd1, &replace, &prefix_links) != 2) {
             pm_strcpy(jcr->errmsg, dir->msg);
             Jmsg(jcr, M_FATAL, 0, _("Bad replace command. CMD=%s\n"), jcr->errmsg);
             return 0;
          }
-         *where = 0;
+         *args = 0;
       }
-      jcr->where_use_regexp = true;
+      use_regexwhere = true;
    }
    /* Turn / into nothing */
-   if (IsPathSeparator(where[0]) && where[1] == '\0') {
-      where[0] = '\0';
+   if (IsPathSeparator(args[0]) && args[1] == '\0') {
+      args[0] = '\0';
    }
 
-   Dmsg2(150, "Got replace %c, where=%s\n", replace, where);
-   unbash_spaces(where);
-   jcr->where = bstrdup(where);
+   Dmsg2(150, "Got replace %c, where=%s\n", replace, args);
+   unbash_spaces(args);
 
-   if (jcr->where_use_regexp) {
-      jcr->where_bregexp = get_bregexps(jcr->where);
+   if (use_regexwhere) {
+      jcr->where_bregexp = get_bregexps(args);
       if (!jcr->where_bregexp) {
-        Jmsg(jcr, M_FATAL, 0, _("Bad where regexp. where=%s\n"), jcr->where);
-        free_pool_memory(where);
+        Jmsg(jcr, M_FATAL, 0, _("Bad where regexp. where=%s\n"), args);
+        free_pool_memory(args);
         return 0;
       }
+   } else {
+      jcr->where = bstrdup(args);
    }
-   free_pool_memory(where);
+
+   free_pool_memory(args);
    jcr->replace = replace;
    jcr->prefix_links = prefix_links;
 
index f31ca4cccafa820bbac4dccddd992d3adede2cdf..6c984de2e58e132fb29212ecbcb9ca4e6e5dd303 100644 (file)
@@ -173,7 +173,7 @@ public:
    MSGS *jcr_msgs;                    /* Copy of message resource -- actually used */
    uint32_t ClientId;                 /* Client associated with Job */
    char *where;                       /* prefix to restore files to */
-   bool where_use_regexp;             /* True if where is a bregexp */
+   char *RegexWhere;                  /* file relocation in restore */
    alist *where_bregexp;              /* BREGEXP alist for path manipulation */
    int cached_pnl;                    /* cached path length */
    POOLMEM *cached_path;              /* cached path */
index 138dad8ad4b2b61288c5051f2bb3a53d610af472..0bfcb8ab54201e5ebc195578cff31d5ac89d7c53 100644 (file)
@@ -149,11 +149,7 @@ void build_attr_output_fnames(JCR *jcr, ATTR *attr)
     *
     */
 
-   if (jcr->where[0] == 0) {
-      pm_strcpy(attr->ofname, attr->fname);
-      pm_strcpy(attr->olname, attr->lname);
-
-   } else if (jcr->where_bregexp) { 
+   if (jcr->where_bregexp) { 
       char *ret;
       apply_bregexps(attr->fname, jcr->where_bregexp, &ret);
       pm_strcpy(attr->ofname, ret);
@@ -172,6 +168,10 @@ void build_attr_output_fnames(JCR *jcr, ATTR *attr)
          }
       }
       
+   } else if (jcr->where[0] == 0) {
+      pm_strcpy(attr->ofname, attr->fname);
+      pm_strcpy(attr->olname, attr->lname);
+
    } else {
       const char *fn;
       int wherelen = strlen(jcr->where);
index 76c164fa375072d2352cffbf130f0029bc5aef6c..925227a6b66ff45c621f1290da2c207ced19b792 100644 (file)
@@ -384,6 +384,10 @@ static void free_common_jcr(JCR *jcr)
       free(jcr->where);
       jcr->where = NULL;
    }
+   if (jcr->RegexWhere) {
+      free(jcr->RegexWhere);
+      jcr->RegexWhere = NULL;
+   }
    if (jcr->where_bregexp) {
       free_bregexps(jcr->where_bregexp);
       delete jcr->where_bregexp;