]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/restore.c
Fix bat seg fault
[bacula/bacula] / bacula / src / dird / restore.c
index b75b82935de1e75109a8ec7cc34b9884dc57801b..6940ef6a5ccf9388b1cda6921f86aca5e1bfa2b4 100644 (file)
@@ -6,7 +6,7 @@
    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
+   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.
 
@@ -15,7 +15,7 @@
    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 General Public License
+   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.
@@ -111,15 +111,15 @@ struct bootstrap_info
 
 #define UA_CMD_SIZE 1000
 
-/*
- * Open the bootstrap file and find the first Storage= 
+/**
+ * Open the bootstrap file and find the first Storage=
  * Returns ok if able to open
  * It fills the storage name (should be the first line) 
  * and the file descriptor to the bootstrap file, 
  * it should be used for next operations, and need to be closed
  * at the end.
  */
-static bool open_bootstrap_file(JCR *jcr, struct bootstrap_info &info)
+static bool open_bootstrap_file(JCR *jcr, bootstrap_info &info)
 {
    FILE *bs;
    UAContext *ua;
@@ -158,7 +158,7 @@ static bool open_bootstrap_file(JCR *jcr, struct bootstrap_info &info)
    return true;
 }
 
-/** 
+/**
  * This function compare the given storage name with the
  * the current one. We compare the name and the address:port.
  * Returns true if we use the same storage.
@@ -197,7 +197,7 @@ static bool is_on_same_storage(JCR *jcr, char *new_one)
    return true;
 }
 
-/** 
+/**
  * Check if the current line contains Storage="xxx", and compare the
  * result to the current storage. We use UAContext to analyse the bsr 
  * string.
@@ -205,7 +205,7 @@ static bool is_on_same_storage(JCR *jcr, char *new_one)
  * Returns true if we need to change the storage, and it set the new
  * Storage resource name in "storage" arg. 
  */
-static bool check_for_new_storage(JCR *jcr, struct bootstrap_info &info)
+static bool check_for_new_storage(JCR *jcr, bootstrap_info &info)
 {
    UAContext *ua = info.ua;
    parse_ua_args(ua);
@@ -229,7 +229,7 @@ static bool check_for_new_storage(JCR *jcr, struct bootstrap_info &info)
  * Send bootstrap file to Storage daemon section by section.
  */
 static bool send_bootstrap_file(JCR *jcr, BSOCK *sock,
-                                struct bootstrap_info &info)
+                                bootstrap_info &info)
 {
    boffset_t pos;
    const char *bootstrap = "bootstrap\n";
@@ -257,37 +257,69 @@ static bool send_bootstrap_file(JCR *jcr, BSOCK *sock,
    return true;
 }
 
-/** 
+#define MAX_TRIES 6 * 360   /* 6 hours */
+
+/**
  * Change the read storage resource for the current job.
  */
-static void select_rstore(JCR *jcr, struct bootstrap_info &info)
+static bool select_rstore(JCR *jcr, bootstrap_info &info)
 {
    USTORE ustore;
+   int i;
+
 
    if (!strcmp(jcr->rstore->name(), info.storage)) {
-      return;
+      return true;                 /* same SD nothing to change */
    }
 
    if (!(ustore.store = (STORE *)GetResWithName(R_STORAGE,info.storage))) {
       Jmsg(jcr, M_FATAL, 0,
            _("Could not get storage resource '%s'.\n"), info.storage);
       set_jcr_job_status(jcr, JS_ErrorTerminated);
-      return;
+      return false;
    }
    
+   /*
+    * What does this do???????????  KES
+    */
    if (jcr->store_bsock) {
       jcr->store_bsock->destroy();
       jcr->store_bsock = NULL;
    }
    
+   /*
+    * release current read storage and get a new one 
+    */
+   dec_read_store(jcr);
    free_rstorage(jcr);
    set_rstorage(jcr, &ustore);
+   set_jcr_job_status(jcr, JS_WaitSD);
+   /*
+    * Wait for up to 6 hours to increment read stoage counter 
+    */
+   for (i=0; i < MAX_TRIES; i++) {
+      /* try to get read storage counter incremented */
+      if (inc_read_store(jcr)) {
+         set_jcr_job_status(jcr, JS_Running);
+         return true;
+      }
+      bmicrosleep(10, 0);       /* sleep 10 secs */
+      if (job_canceled(jcr)) {
+         free_rstorage(jcr);
+         return false;
+      }
+   }
+   /* Failed to inc_read_store() */
+   free_rstorage(jcr);
+   Jmsg(jcr, M_FATAL, 0, 
+      _("Could not acquire read storage lock for \"%s\""), info.storage);
+   return false;
 }
 
 /* 
- * Clean the struct bootstrap_info struct
+ * Clean the bootstrap_info struct
  */
-static void close_bootstrap_file(struct bootstrap_info &info)
+static void close_bootstrap_file(bootstrap_info &info)
 {
    if (info.bs) {
       fclose(info.bs);
@@ -297,7 +329,7 @@ static void close_bootstrap_file(struct bootstrap_info &info)
    }
 }
 
-/** 
+/**
  * The bootstrap is stored in a file, so open the file, and loop
  *   through it processing each storage device in turn. If the
  *   storage is different from the prior one, we open a new connection
@@ -308,12 +340,12 @@ static void close_bootstrap_file(struct bootstrap_info &info)
  */
 bool restore_bootstrap(JCR *jcr)
 {
-   BSOCK *fd=NULL, *sd;
-   bool end_loop=false;
-   bool first_time=true;
-   struct bootstrap_info info;
+   BSOCK *fd = NULL;
+   BSOCK *sd;
+   bool first_time = true;
+   bootstrap_info info;
    POOL_MEM restore_cmd(PM_MESSAGE);
-   bool ret=false;
+   bool ret = false;
 
    /* this command is used for each part */
    build_restore_command(jcr, restore_cmd);
@@ -323,9 +355,11 @@ bool restore_bootstrap(JCR *jcr)
       goto bail_out;
    }
    /* Read the bootstrap file */
-   while (!end_loop && !feof(info.bs)) {
+   while (!feof(info.bs)) {
       
-      select_rstore(jcr, info);
+      if (!select_rstore(jcr, info)) {
+         goto bail_out;
+      }
 
       /**
        * Open a message channel connection with the Storage
@@ -399,15 +433,16 @@ bool restore_bootstrap(JCR *jcr)
          goto bail_out;
       }
 
+      /* Only pass "global" commands to the FD once */
       if (first_time) {
+         first_time = false;
          if (!send_runscripts_commands(jcr)) {
             goto bail_out;
          }
-         first_time=false;
-      }
-
-      if (!send_restore_objects(jcr)) {
-         goto bail_out;
+         if (!send_restore_objects(jcr)) {
+            Dmsg0(000, "FAIL: Send restore objects\n");
+            goto bail_out;
+         }
       }
 
       fd->fsend("%s", restore_cmd.c_str());
@@ -417,8 +452,7 @@ bool restore_bootstrap(JCR *jcr)
       }
 
       if (jcr->FDVersion < 2) { /* Old FD */
-         end_loop=true;         /* we do only one loop */
-
+         break;                 /* we do only one loop */
       } else {
          if (!response(jcr, fd, OKstoreend, "Store end", DISPLAY_ERROR)) {
             goto bail_out;