<commit_before>/*
   Bacula® - The Network Backup Solution

   Copyright (C) 2001-2012 Free Software Foundation Europe e.V.

   The main author of Bacula is <NAME>, 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 three of the GNU Affero General Public
   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
   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 Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.

   Bacula® is a registered trademark of <NAME>.
   The licensor of Bacula is the Free Software Foundation Europe
   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
   Switzerland, email:<EMAIL>.
*/
/*
 *
 *   Bacula Director -- Run Command
 *
 *     <NAME>, December MMI
 *
 */

#include "bacula.h"
#include "dird.h"

class run_ctx {
public:
   char *job_name, *level_name, *jid, *store_name, *pool_name;
   char *where, *fileset_name, *client_name, *bootstrap, *regexwhere;
   char *restore_client_name, *comment;
   const char *replace;
   char *when, *verify_job_name, *catalog_name;
   char *previous_job_name;
   char *since;
   char *plugin_options;
   const char *verify_list;
   JOB *job;
   JOB *verify_job;
   JOB *previous_job;
   USTORE *store;
   CLIENT *client;
   FILESET *fileset;
   POOL *pool;
   CAT *catalog;
   int Priority;
   int files;
   bool cloned;
   bool mod;
   int spool_data;
   bool spool_data_set;
   int accurate;
   bool accurate_set;
   int ignoreduplicatecheck;
   bool ignoreduplicatecheck_set;

   /* Methods */
   run_ctx() { memset(this, 0, sizeof(run_ctx)); 
               store = new USTORE; };
   ~run_ctx() { delete store; };
};

/* Forward referenced subroutines */
static void select_job_level(UAContext *ua, JCR *jcr);
static bool display_job_parameters(UAContext *ua, JCR *jcr, JOB *job, 
                const char *verify_list, char *jid, const char *replace,
                char *client_name);
static void select_where_regexp(UAContext *ua, JCR *jcr);
static bool scan_command_line_arguments(UAContext *ua, run_ctx &rc);
static bool reset_restore_context(UAContext *ua, JCR *jcr, run_ctx &rc);
static int modify_job_parameters(UAContext *ua, JCR *jcr, run_ctx &rc);

/* Imported variables */
extern struct s_kw ReplaceOptions[];

/*
 * For Backup and Verify Jobs
 *     run [job=]<job-name> level=<level-name>
 *
 * For Restore Jobs
 *     run <job-name> 
 *
 *  Returns: 0 on error
 *           JobId if OK
 *
 */
int run_cmd(UAContext *ua, const char *cmd)
{
   JCR *jcr = NULL;
   run_ctx rc;
   int status;

   if (!open_client_db(ua)) {
      return 1;
   }

   if (!scan_command_line_arguments(ua, rc)) {
      return 0;
   }

   if (find_arg(ua, NT_("fdcalled")) > 0) {
      jcr->file_bsock = dup_bsock(ua->UA_sock);
      ua->quit = true;
   }

   /*
    * Create JCR to run job.  NOTE!!! after this point, free_jcr()
    *  before returning.
    */
   if (!jcr) {
      jcr = new_jcr(sizeof(JCR), dird_free_jcr);
      set_jcr_defaults(jcr, rc.job);
      jcr->unlink_bsr = ua->jcr->unlink_bsr;    /* copy unlink flag from caller */
      ua->jcr->unlink_bsr = false;
   }
   /* Transfer JobIds to new restore Job */
   if (ua->jcr->JobIds) {
      jcr->JobIds = ua->jcr->JobIds;
      ua->jcr->JobIds = NULL;
   }

try_again:
   if (!reset_restore_context(ua, jcr, rc)) {
      goto bail_out;
   }


   /* Run without prompting? */
   if (ua->batch || find_arg(ua, NT_("yes")) > 0) {
      goto start_job;
   }

   /*
    * Prompt User to see if all run job parameters are correct, and
    *   allow him to modify them.
    */
   if (!display_job_parameters(ua, jcr, rc.job, rc.verify_list, rc.jid, rc.replace,
        rc.client_name)) {
      goto bail_out;
   }

   if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
      goto bail_out;
   }

   if (strncasecmp(ua->cmd, ".mod ", 5) == 0 || 
       (strncasecmp(ua->cmd, "mod ", 4) == 0 && strlen(ua->cmd) > 6)) {
      parse_ua_args(ua);
      rc.mod = true;
      if (!scan_command_line_arguments(ua, rc)) {
         return 0;
      }
      goto try_again;
   }

   /* Allow the user to modify the settings */
   status = modify_job_parameters(ua, jcr, rc);
   switch (status) {
   case 0:
      goto try_again;
   case 1:
      break;
   case -1:
      goto bail_out;
   }


   if (ua->cmd[0] == 0 || strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
      JobId_t JobId;
      Dmsg1(800, "Calling run_job job=%x\n", jcr->job);

start_job:
      Dmsg3(100, "JobId=%u using pool %s priority=%d\n", (int)jcr->JobId, 
            jcr->pool->name(), jcr->JobPriority);
      Dmsg1(900, "Running a job; its spool_data = %d\n", jcr->spool_data);
      JobId = run_job(jcr);
      Dmsg4(100, "JobId=%u NewJobId=%d using pool %s priority=%d\n", (int)jcr->JobId, 
            JobId, jcr->pool->name(), jcr->JobPriority);
      free_jcr(jcr);                  /* release jcr */
      if (JobId == 0) {
         ua->error_msg(_("Job failed.\n"));
      } else {
         char ed1[50];
         ua->send_msg(_("Job queued. JobId=%s\n"), edit_int64(JobId, ed1));
      }
      return JobId;
   }

bail_out:
   ua->send_msg(_("Job not run.\n"));
   free_jcr(jcr);
   return 0;                       /* do not run */
}

int modify_job_parameters(UAContext *ua, JCR *jcr, run_ctx &rc)
{
   int i, opt;
   
   /*
    * At user request modify parameters of job to be run.
    */
   if (ua->cmd[0] != 0 && strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
      FILE *fd;

      start_prompt(ua, _("Parameters to modify:\n"));
      add_prompt(ua, _("Level"));            /* 0 */
      add_prompt(ua, _("Storage"));          /* 1 */
      add_prompt(ua, _("Job"));              /* 2 */
      add_prompt(ua, _("FileSet"));          /* 3 */
      if (jcr->getJobType() == JT_RESTORE) {
         add_prompt(ua, _("Restore Client"));   /* 4 */<commit_msg>Fix bug #1955 that OK to run? does not retry on bad response<commit_after>/*
   Bacula® - The Network Backup Solution

   Copyright (C) 2001-2012 Free Software Foundation Europe e.V.

   The main author of Bacula is <NAME>, 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 three of the GNU Affero General Public
   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
   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 Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.

   Bacula® is a registered trademark of <NAME>.
   The licensor of Bacula is the Free Software Foundation Europe
   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
   Switzerland, email:<EMAIL>.
*/
/*
 *
 *   Bacula Director -- Run Command
 *
 *     <NAME>, December MMI
 *
 */

#include "bacula.h"
#include "dird.h"

class run_ctx {
public:
   char *job_name, *level_name, *jid, *store_name, *pool_name;
   char *where, *fileset_name, *client_name, *bootstrap, *regexwhere;
   char *restore_client_name, *comment;
   const char *replace;
   char *when, *verify_job_name, *catalog_name;
   char *previous_job_name;
   char *since;
   char *plugin_options;
   const char *verify_list;
   JOB *job;
   JOB *verify_job;
   JOB *previous_job;
   USTORE *store;
   CLIENT *client;
   FILESET *fileset;
   POOL *pool;
   CAT *catalog;
   int Priority;
   int files;
   bool cloned;
   bool mod;
   int spool_data;
   bool spool_data_set;
   int accurate;
   bool accurate_set;
   int ignoreduplicatecheck;
   bool ignoreduplicatecheck_set;

   /* Methods */
   run_ctx() { memset(this, 0, sizeof(run_ctx)); 
               store = new USTORE; };
   ~run_ctx() { delete store; };
};

/* Forward referenced subroutines */
static void select_job_level(UAContext *ua, JCR *jcr);
static bool display_job_parameters(UAContext *ua, JCR *jcr, JOB *job, 
                const char *verify_list, char *jid, const char *replace,
                char *client_name);
static void select_where_regexp(UAContext *ua, JCR *jcr);
static bool scan_command_line_arguments(UAContext *ua, run_ctx &rc);
static bool reset_restore_context(UAContext *ua, JCR *jcr, run_ctx &rc);
static int modify_job_parameters(UAContext *ua, JCR *jcr, run_ctx &rc);

/* Imported variables */
extern struct s_kw ReplaceOptions[];

/*
 * For Backup and Verify Jobs
 *     run [job=]<job-name> level=<level-name>
 *
 * For Restore Jobs
 *     run <job-name> 
 *
 *  Returns: 0 on error
 *           JobId if OK
 *
 */
int run_cmd(UAContext *ua, const char *cmd)
{
   JCR *jcr = NULL;
   run_ctx rc;
   int status;

   if (!open_client_db(ua)) {
      return 1;
   }

   if (!scan_command_line_arguments(ua, rc)) {
      return 0;
   }

   if (find_arg(ua, NT_("fdcalled")) > 0) {
      jcr->file_bsock = dup_bsock(ua->UA_sock);
      ua->quit = true;
   }

   /*
    * Create JCR to run job.  NOTE!!! after this point, free_jcr()
    *  before returning.
    */
   if (!jcr) {
      jcr = new_jcr(sizeof(JCR), dird_free_jcr);
      set_jcr_defaults(jcr, rc.job);
      jcr->unlink_bsr = ua->jcr->unlink_bsr;    /* copy unlink flag from caller */
      ua->jcr->unlink_bsr = false;
   }
   /* Transfer JobIds to new restore Job */
   if (ua->jcr->JobIds) {
      jcr->JobIds = ua->jcr->JobIds;
      ua->jcr->JobIds = NULL;
   }

try_again:
   if (!reset_restore_context(ua, jcr, rc)) {
      goto bail_out;
   }


   /* Run without prompting? */
   if (ua->batch || find_arg(ua, NT_("yes")) > 0) {
      goto start_job;
   }

   /*
    * Prompt User to see if all run job parameters are correct, and
    *   allow him to modify them.
    */
   if (!display_job_parameters(ua, jcr, rc.job, rc.verify_list, rc.jid, rc.replace,
        rc.client_name)) {
      goto bail_out;
   }

   if (!get_cmd(ua, _("OK to run? (yes/mod/no): "))) {
      goto bail_out;
   }

   if (strncasecmp(ua->cmd, ".mod ", 5) == 0 || 
       (strncasecmp(ua->cmd, "mod ", 4) == 0 && strlen(ua->cmd) > 6)) {
      parse_ua_args(ua);
      rc.mod = true;
      if (!scan_command_line_arguments(ua, rc)) {
         return 0;
      }
      goto try_again;
   }

   /* Allow the user to modify the settings */
   status = modify_job_parameters(ua, jcr, rc);
   switch (status) {
   case 0:
      goto try_again;
   case 1:
      break;
   case -1:
      goto bail_out;
   }


   if (ua->cmd[0] == 0 || strncasecmp(ua->cmd, _("yes"), strlen(ua->cmd)) == 0) {
      JobId_t JobId;
      Dmsg1(800, "Calling run_job job=%x\n", jcr->job);

start_job:
      Dmsg3(100, "JobId=%u using pool %s priority=%d\n", (int)jcr->JobId, 
            jcr->pool->name(), jcr->JobPriority);
      Dmsg1(900, "Running a job; its spool_data = %d\n", jcr->spool_data);
      JobId = run_job(jcr);
      Dmsg4(100, "JobId=%u NewJobId=%d using pool %s priority=%d\n", (int)jcr->JobId, 
            JobId, jcr->pool->name(), jcr->JobPriority);
      free_jcr(jcr);                  /* release jcr */
      if (JobId == 0) {
         ua->error_msg(_("Job failed.\n"));
      } else {
         char ed1[50];
         ua->send_msg(_("Job queued. JobId=%s\n"), edit_int64(JobId, ed1));
      }
      return JobId;
   }
   if (strncasecmp(ua->cmd, _("no"), strlen(ua->cmd)) == 0) {
      goto bail_out;
   }
   ua->send_msg(_("\nBad response: %s. You must answer yes, mod, or no.\n\n"), ua->cmd);
   goto try_again;

bail_out:
   ua->send_msg(_("Job not run.\n"));
   free_jcr(jcr);
   return 0;                       /* do not run */
}

int modify_job_parameters(UAContext *ua, JCR *jcr, run_ctx &rc)
{
   int i, opt;
   
   /*
    * At user request modify parameters of job to be run.
    */
   if (ua->cmd[0] != 0 && strncasecmp(ua->cmd, _("mod"), strlen(ua->cmd)) == 0) {
      FILE *fd;

      start_prompt(ua, _("Parameters to modify:\n"));
      add_prompt(ua, _("Level"));            /* 0 */
      add_prompt(ua, _("Storage"));          /* 1 */
      add_prompt(ua, _("Job"));              /* 2 */
      add_prompt(ua, _("FileSet"));          /* 3 */
      if (jcr->getJobType() == JT_RESTORE) {
         add_prompt(ua, _("Restore Client"));   /* 4 */
{'fraction_non_alphanumeric': 0.13406049354065136, 'fraction_numerical': 0.011990407673860911, 'mean_word_length': 2.6406646015206983, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1745607', 'n_tokens_mistral': 4709, 'n_tokens_neox': 4481, 'n_words': 1308}