]> git.ipfire.org Git - thirdparty/bacula.git/commitdiff
Fix org#2627 About DIR crashing for copy jobs and resource rename
authorMichal Rakowski <michal.rakowski@baculasystems.com>
Tue, 10 Aug 2021 15:58:45 +0000 (17:58 +0200)
committerEric Bollengier <eric@baculasystems.com>
Wed, 11 Aug 2021 09:04:34 +0000 (11:04 +0200)
bacula/src/dird/mac.c

index 072b12a7287a6b311d645ca60ccd1e2a28a02f8d..8314a2c913b247acb20dcd000ab2d992ff260274 100644 (file)
@@ -83,10 +83,11 @@ bool do_mac_init(JCR *jcr)
 {
    POOL *pool = NULL;
    JOB *job, *prev_job;
-   JCR *wjcr;                     /* jcr of writing job */
+   JCR *wjcr = NULL;    /* jcr of writing job */
    int count;
    CLIENT_DBR cr;
    FILESET_DBR fr;
+   bool ret = false;
 
    apply_pool_overrides(jcr);
 
@@ -94,7 +95,7 @@ bool do_mac_init(JCR *jcr)
    if (jcr->jr.PoolId == 0) {
       Dmsg1(dbglevel, "JobId=%d no PoolId\n", (int)jcr->JobId);
       Jmsg(jcr, M_FATAL, 0, _("Could not get or create a Pool record.\n"));
-      return false;
+      goto bail_out;
    }
    /*
     * Note, at this point, pool is the pool for this job.  We
@@ -109,21 +110,22 @@ bool do_mac_init(JCR *jcr)
    if (!get_or_create_fileset_record(jcr)) {
       Dmsg1(dbglevel, "JobId=%d no FileSet\n", (int)jcr->JobId);
       Jmsg(jcr, M_FATAL, 0, _("Could not get or create the FileSet record.\n"));
-      return false;
+      goto bail_out;
    }
 
    if (!allow_duplicate_job(jcr)) {
-      return false;
+      goto bail_out;
    }
 
    /* If we find a job or jobs to migrate it is previous_jr.JobId */
    count = getJob_to_migrate(jcr);
    if (count < 0) {
-      return false;
+      goto bail_out;
    }
    if (count == 0) {
       set_mac_next_pool(jcr, &pool);
-      return true;                    /* no work */
+      ret = true;                    /* no work */
+      goto bail_out;
    }
 
    Dmsg1(dbglevel, "Back from getJob_to_migrate JobId=%d\n", (int)jcr->JobId);
@@ -132,12 +134,13 @@ bool do_mac_init(JCR *jcr)
       Dmsg1(dbglevel, "JobId=%d no previous JobId\n", (int)jcr->JobId);
       Jmsg(jcr, M_INFO, 0, _("No previous Job found to %s.\n"), jcr->get_ActionName(0));
       set_mac_next_pool(jcr, &pool);
-      return true;                    /* no work */
+      ret = true;                    /* no work */
+      goto bail_out;
    }
 
    if (create_restore_bootstrap_file(jcr) < 0) {
       Jmsg(jcr, M_FATAL, 0, _("Create bootstrap file failed.\n"));
-      return false;
+      goto bail_out;
    }
 
    if (jcr->previous_jr.JobId == 0 || jcr->ExpectedFiles == 0) {
@@ -149,7 +152,8 @@ bool do_mac_init(JCR *jcr)
          Jmsg(jcr, M_INFO, 0, _("Previous Job has no data to %s.\n"), jcr->get_ActionName(0));
       }
       set_mac_next_pool(jcr, &pool);
-      return true;                    /* no work */
+      ret = true;                    /* no work */
+      goto bail_out;
    }
 
 
@@ -164,17 +168,16 @@ bool do_mac_init(JCR *jcr)
    UnlockRes();
    if (!job) {
       Jmsg(jcr, M_FATAL, 0, _("Job resource not found for \"%s\".\n"), jcr->jr.Name);
-      return false;
+      goto bail_out;
    }
    if (!prev_job) {
       Jmsg(jcr, M_FATAL, 0, _("Previous Job resource not found for \"%s\".\n"),
            jcr->previous_jr.Name);
-      return false;
+      goto bail_out;
    }
 
-
    /* Create a write jcr */
-   wjcr = jcr->wjcr = new_jcr(sizeof(JCR), dird_free_jcr);
+   wjcr = new_jcr(sizeof(JCR), dird_free_jcr);
    memcpy(&wjcr->previous_jr, &jcr->previous_jr, sizeof(wjcr->previous_jr));
 
    /*
@@ -197,7 +200,7 @@ bool do_mac_init(JCR *jcr)
    if (!db_get_client_record(jcr, jcr->db, &cr)) {
       Jmsg(jcr, M_FATAL, 0, _("Could not get the client record for clientId %d.\n"),
             (int)wjcr->previous_jr.ClientId);
-      return false;
+      goto bail_out;
    }
    if (!wjcr->client_name) {
       wjcr->client_name = get_pool_memory(PM_NAME);
@@ -206,7 +209,7 @@ bool do_mac_init(JCR *jcr)
    wjcr->client = GetClientResWithName(cr.Name);
    if (!wjcr->client) {
       Jmsg(jcr, M_FATAL, 0, _("Client resource not found for \"%s\".\n"), cr.Name);
-      return false;
+      goto bail_out;
    }
    // Look for the FileSet from the Job Record
    bmemset(&fr, 0, sizeof(fr));
@@ -214,12 +217,12 @@ bool do_mac_init(JCR *jcr)
    if (!db_get_fileset_record(jcr, jcr->db, &fr)) {
       Jmsg(jcr, M_FATAL, 0, _("Could not get the FileSet record for FileSetId %d.\n"),
             (int)wjcr->previous_jr.FileSetId);
-      return false;
+      goto bail_out;
    }
    wjcr->fileset = GetFileSetResWithName(fr.FileSet);
    if (!wjcr->fileset) {
       Jmsg(jcr, M_FATAL, 0, _("FileSet resource not found for \"%s\".\n"), fr.FileSet);
-      return false;
+      goto bail_out;
    }
 
    /* fix MA 987 cannot copy/migrate jobs with a Level=VF in the job resource
@@ -237,7 +240,7 @@ bool do_mac_init(JCR *jcr)
 
    if (!setup_job(wjcr)) {
       Jmsg(jcr, M_FATAL, 0, _("setup job failed.\n"));
-      return false;
+      goto bail_out;
    }
 
    /* Now reset the job record from the previous job */
@@ -266,7 +269,20 @@ bool do_mac_init(JCR *jcr)
       wjcr->jr.PoolId = jcr->jr.PoolId;
    }
 
-   return true;
+   /* wjcr is all set right now, can be assigned to the original jcr so that it can be used
+    * later (even in the error handlig/cleanup) */
+   jcr->wjcr = wjcr;
+
+   ret = true;
+
+bail_out:
+   if (!jcr->wjcr && wjcr) {
+      /* We get here if wjcr was allocated, but not fully prepared to be assinged to the parent jcr,
+       * it has to be freed now so that it is not used during cleanup reporting */
+      free_jcr(wjcr);
+   }
+
+   return ret;
 }
 
 /*