]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/job.c
- Back port changes to 1.38.5
[bacula/bacula] / bacula / src / dird / job.c
index faef49445ae0c4371c159e54b0b61582fe8fd43e..f23976861c8e3b95803e459d1044d1182275db9c 100644 (file)
@@ -7,7 +7,7 @@
  *    Version $Id$
  */
 /*
-   Copyright (C) 2000-2005 Kern Sibbald
+   Copyright (C) 2000-2006 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
@@ -73,8 +73,22 @@ void term_job_server()
  */
 JobId_t run_job(JCR *jcr)
 {
-   int stat, errstat;
-   JobId_t JobId = 0;
+   int stat;
+   if (setup_job(jcr)) {
+      /* Queue the job to be run */
+      if ((stat = jobq_add(&job_queue, jcr)) != 0) {
+         berrno be;
+         Jmsg(jcr, M_FATAL, 0, _("Could not add job queue: ERR=%s\n"), be.strerror(stat));
+         return 0;
+      }
+      return jcr->JobId;
+   }
+   return 0;
+}            
+
+bool setup_job(JCR *jcr) 
+{
+   int errstat;
 
    P(jcr->mutex);
    sm_check(__FILE__, __LINE__, true);
@@ -117,7 +131,7 @@ JobId_t run_job(JCR *jcr)
       Jmsg(jcr, M_FATAL, 0, "%s", db_strerror(jcr->db));
       goto bail_out;
    }
-   JobId = jcr->JobId = jcr->jr.JobId;
+   jcr->JobId = jcr->jr.JobId;
    Dmsg4(100, "Created job record JobId=%d Name=%s Type=%c Level=%c\n",
        jcr->JobId, jcr->Job, jcr->jr.JobType, jcr->jr.JobLevel);
 
@@ -133,17 +147,9 @@ JobId_t run_job(JCR *jcr)
 
    Dmsg0(200, "Add jrc to work queue\n");
 
-   /* Queue the job to be run */
-   if ((stat = jobq_add(&job_queue, jcr)) != 0) {
-      berrno be;
-      Jmsg(jcr, M_FATAL, 0, _("Could not add job queue: ERR=%s\n"), be.strerror(stat));
-      JobId = 0;
-      goto bail_out;
-   }
-   Dmsg0(100, "Done run_job()\n");
 
    V(jcr->mutex);
-   return JobId;
+   return true;
 
 bail_out:
    if (jcr->fname) {
@@ -151,7 +157,7 @@ bail_out:
       jcr->fname = NULL;
    }
    V(jcr->mutex);
-   return JobId;
+   return false;
 }
 
 
@@ -218,7 +224,7 @@ static void *job_thread(void *arg)
          admin_cleanup(jcr, JS_ErrorTerminated);
       }
       break;
-   case JT_MIGRATION:
+   case JT_MIGRATE:
    case JT_COPY:
    case JT_ARCHIVE:
       if (!do_mac_init(jcr)) {             /* migration, archive, copy */
@@ -294,7 +300,7 @@ static void *job_thread(void *arg)
             admin_cleanup(jcr, JS_ErrorTerminated);
          }
          break;
-      case JT_MIGRATION:
+      case JT_MIGRATE:
       case JT_COPY:
       case JT_ARCHIVE:
          if (do_mac(jcr)) {              /* migration, archive, copy */
@@ -379,7 +385,6 @@ bool cancel_job(UAContext *ua, JCR *jcr)
       return true;
 
    default:
-
       /* Cancel File daemon */
       if (jcr->file_bsock) {
          ua->jcr->client = jcr->client;
@@ -401,7 +406,7 @@ bool cancel_job(UAContext *ua, JCR *jcr)
       /* Cancel Storage daemon */
       if (jcr->store_bsock) {
          if (!ua->jcr->storage) {
-            copy_storage(ua->jcr, jcr);
+            copy_storage(ua->jcr, jcr->storage);
          } else {
             set_storage(ua->jcr, jcr->store);
          }
@@ -427,7 +432,7 @@ bool cancel_job(UAContext *ua, JCR *jcr)
 
 static void job_monitor_destructor(watchdog_t *self)
 {
-   JCR *control_jcr = (JCR *) self->data;
+   JCR *control_jcr = (JCR *)self->data;
 
    free_jcr(control_jcr);
 }
@@ -446,8 +451,6 @@ static void job_monitor_watchdog(watchdog_t *self)
       if (jcr->JobId == 0) {
          Dmsg2(800, "Skipping JCR %p (%s) with JobId 0\n",
                jcr, jcr->Job);
-         /* Keep reference counts correct */
-         free_jcr(jcr);
          continue;
       }
 
@@ -466,12 +469,12 @@ static void job_monitor_watchdog(watchdog_t *self)
          cancel_job(ua, jcr);
          free_ua_context(ua);
 
-         Dmsg1(800, "Have cancelled JCR %p\n", jcr);
+         Dmsg2(800, "Have cancelled JCR %p Job=%d\n", jcr, jcr->JobId);
       }
 
       /* Keep reference counts correct */
-      free_jcr(jcr);
    }
+   endeach_jcr(jcr);
 }
 
 /*
@@ -634,32 +637,34 @@ bool get_or_create_client_record(JCR *jcr)
    return true;
 }
 
-bool get_or_create_fileset_record(JCR *jcr, FILESET_DBR *fsr)
+bool get_or_create_fileset_record(JCR *jcr)
 {
+   FILESET_DBR fsr;
    /*
     * Get or Create FileSet record
     */
-   memset(fsr, 0, sizeof(FILESET_DBR));
-   bstrncpy(fsr->FileSet, jcr->fileset->hdr.name, sizeof(fsr->FileSet));
+   memset(&fsr, 0, sizeof(FILESET_DBR));
+   bstrncpy(fsr.FileSet, jcr->fileset->hdr.name, sizeof(fsr.FileSet));
    if (jcr->fileset->have_MD5) {
       struct MD5Context md5c;
-      unsigned char signature[16];
+      unsigned char digest[MD5HashSize];
       memcpy(&md5c, &jcr->fileset->md5c, sizeof(md5c));
-      MD5Final(signature, &md5c);
-      bin_to_base64(fsr->MD5, (char *)signature, 16); /* encode 16 bytes */
-      bstrncpy(jcr->fileset->MD5, fsr->MD5, sizeof(jcr->fileset->MD5));
+      MD5Final(digest, &md5c);
+      bin_to_base64(fsr.MD5, (char *)digest, MD5HashSize);
+      bstrncpy(jcr->fileset->MD5, fsr.MD5, sizeof(jcr->fileset->MD5));
    } else {
-      Jmsg(jcr, M_WARNING, 0, _("FileSet MD5 signature not found.\n"));
+      Jmsg(jcr, M_WARNING, 0, _("FileSet MD5 digest not found.\n"));
    }
    if (!jcr->fileset->ignore_fs_changes ||
-       !db_get_fileset_record(jcr, jcr->db, fsr)) {
-      if (!db_create_fileset_record(jcr, jcr->db, fsr)) {
+       !db_get_fileset_record(jcr, jcr->db, &fsr)) {
+      if (!db_create_fileset_record(jcr, jcr->db, &fsr)) {
          Jmsg(jcr, M_ERROR, 0, _("Could not create FileSet \"%s\" record. ERR=%s\n"),
-            fsr->FileSet, db_strerror(jcr->db));
+            fsr.FileSet, db_strerror(jcr->db));
          return false;
       }
    }
-   jcr->jr.FileSetId = fsr->FileSetId;
+   jcr->jr.FileSetId = fsr.FileSetId;
+   bstrncpy(jcr->FSCreateTime, fsr.cCreateTime, sizeof(jcr->FSCreateTime));
    Dmsg2(119, "Created FileSet %s record %u\n", jcr->fileset->hdr.name,
       jcr->jr.FileSetId);
    return true;
@@ -823,7 +828,6 @@ void dird_free_jcr(JCR *jcr)
  */
 void set_jcr_defaults(JCR *jcr, JOB *job)
 {
-   STORE *st;
    jcr->job = job;
    jcr->JobType = job->JobType;
    switch (jcr->JobType) {
@@ -837,18 +841,7 @@ void set_jcr_defaults(JCR *jcr, JOB *job)
    }
    jcr->JobPriority = job->Priority;
    /* Copy storage definitions -- deleted in dir_free_jcr above */
-   if (job->storage) {
-      if (jcr->storage) {
-         delete jcr->storage;
-      }
-      jcr->storage = New(alist(10, not_owned_by_alist));
-      foreach_alist(st, job->storage) {
-         jcr->storage->append(st);
-      }
-   }
-   if (jcr->storage) {
-      jcr->store = (STORE *)jcr->storage->first();
-   }
+   copy_storage(jcr, job->storage);
    jcr->client = job->client;
    if (!jcr->client_name) {
       jcr->client_name = get_pool_memory(PM_NAME);
@@ -892,28 +885,28 @@ void set_jcr_defaults(JCR *jcr, JOB *job)
    }
 }
 
-/*
- * copy the storage definitions from an old JCR to a new one
+
+/* 
+ * Copy the storage definitions from an alist to the JCR
  */
-void copy_storage(JCR *new_jcr, JCR *old_jcr)
+void copy_storage(JCR *jcr, alist *storage)
 {
-   if (old_jcr->storage) {
+   if (storage) {
       STORE *st;
-      if (old_jcr->storage) {
-         delete old_jcr->storage;
+      if (jcr->storage) {
+         delete jcr->storage;
       }
-      new_jcr->storage = New(alist(10, not_owned_by_alist));
-      foreach_alist(st, old_jcr->storage) {
-         new_jcr->storage->append(st);
+      jcr->storage = New(alist(10, not_owned_by_alist));
+      foreach_alist(st, storage) {
+         jcr->storage->append(st);
       }
-   }
-   if (old_jcr->store) {
-      new_jcr->store = old_jcr->store;
-   } else if (new_jcr->storage) {
-      new_jcr->store = (STORE *)new_jcr->storage->first();
+   }               
+   if (jcr->storage) {
+      jcr->store = (STORE *)jcr->storage->first();
    }
 }
 
+
 /* Set storage override */
 void set_storage(JCR *jcr, STORE *store)
 {
@@ -928,3 +921,64 @@ void set_storage(JCR *jcr, STORE *store)
    /* Store not in list, so add it */
    jcr->storage->prepend(store);
 }
+
+void create_clones(JCR *jcr)
+{
+   /*
+    * Fire off any clone jobs (run directives)
+    */
+   Dmsg2(900, "cloned=%d run_cmds=%p\n", jcr->cloned, jcr->job->run_cmds);
+   if (!jcr->cloned && jcr->job->run_cmds) {
+      char *runcmd;
+      JOB *job = jcr->job;
+      POOLMEM *cmd = get_pool_memory(PM_FNAME);
+      UAContext *ua = new_ua_context(jcr);
+      ua->batch = true;
+      foreach_alist(runcmd, job->run_cmds) {
+         cmd = edit_job_codes(jcr, cmd, runcmd, "");              
+         Mmsg(ua->cmd, "run %s cloned=yes", cmd);
+         Dmsg1(900, "=============== Clone cmd=%s\n", ua->cmd);
+         parse_ua_args(ua);                 /* parse command */
+         int stat = run_cmd(ua, ua->cmd);
+         if (stat == 0) {
+            Jmsg(jcr, M_ERROR, 0, _("Could not start clone job.\n"));
+         } else {
+            Jmsg(jcr, M_INFO, 0, _("Clone JobId %d started.\n"), stat);
+         }
+      }
+      free_ua_context(ua);
+      free_pool_memory(cmd);
+   }
+}
+
+bool create_restore_bootstrap_file(JCR *jcr)
+{
+   RESTORE_CTX rx;
+   UAContext *ua;
+   memset(&rx, 0, sizeof(rx));
+   rx.bsr = new_bsr();
+   rx.JobIds = "";                       
+   rx.bsr->JobId = jcr->target_jr.JobId;
+   ua = new_ua_context(jcr);
+   complete_bsr(ua, rx.bsr);
+   rx.bsr->fi = new_findex();
+   rx.bsr->fi->findex = 1;
+   rx.bsr->fi->findex2 = jcr->target_jr.JobFiles;
+   jcr->ExpectedFiles = write_bsr_file(ua, rx);
+   if (jcr->ExpectedFiles == 0) {
+      free_ua_context(ua);
+      free_bsr(rx.bsr);
+      return false;
+   }
+   if (jcr->RestoreBootstrap) {
+      free(jcr->RestoreBootstrap);
+   }
+   POOLMEM *fname = get_pool_memory(PM_MESSAGE);
+   make_unique_restore_filename(ua, &fname);
+   jcr->RestoreBootstrap = bstrdup(fname);
+   free_ua_context(ua);
+   free_bsr(rx.bsr);
+   free_pool_memory(fname);
+   jcr->needs_sd = true;
+   return true;
+}