]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/catreq.c
- Convert more atoi to str_to_int64() for DB.
[bacula/bacula] / bacula / src / dird / catreq.c
index 21d759e6e47aa1f7e945526e21f7da9d52af3198..17ff0a194657b8cf3e69e9dfdf64b1abb266e75f 100644 (file)
@@ -41,7 +41,7 @@
  */
 
 /* Requests from the Storage daemon */
-static char Find_media[] = "CatReq Job=%127s FindMedia=%d\n";
+static char Find_media[] = "CatReq Job=%127s FindMedia=%d pool_name=%127s media_type=%127s\n";
 static char Get_Vol_Info[] = "CatReq Job=%127s GetVolInfo VolName=%127s write=%d\n";
 
 static char Update_media[] = "CatReq Job=%127s UpdateMedia VolName=%s"
@@ -86,7 +86,7 @@ static int send_volume_info_to_storage_daemon(JCR *jcr, BSOCK *sd, MEDIA_DBR *mr
       mr->VolParts,
       mr->LabelType);
    unbash_spaces(mr->VolumeName);
-   Dmsg2(400, "Vol Info for %s: %s", jcr->Job, sd->msg);
+   Dmsg2(100, "Vol Info for %s: %s", jcr->Job, sd->msg);
    return stat;
 }
 
@@ -95,8 +95,10 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
    MEDIA_DBR mr, sdmr;
    JOBMEDIA_DBR jm;
    char Job[MAX_NAME_LENGTH];
+   char pool_name[MAX_NAME_LENGTH];
    int index, ok, label, writing;
    POOLMEM *omsg;
+   POOL_DBR pr;
 
    memset(&mr, 0, sizeof(mr));
    memset(&sdmr, 0, sizeof(sdmr));
@@ -105,9 +107,26 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
    /*
     * Request to find next appendable Volume for this Job
     */
-   Dmsg1(400, "catreq %s", bs->msg);
-   if (sscanf(bs->msg, Find_media, &Job, &index) == 2) {
-      ok = find_next_volume_for_append(jcr, &mr, true /*permit create new vol*/);
+   Dmsg1(100, "catreq %s", bs->msg);
+   if (!jcr->db) {
+      omsg = get_memory(bs->msglen+1);
+      pm_strcpy(omsg, bs->msg);
+      bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);    
+      Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request; DB not open: %s"), omsg);
+      free_memory(omsg);
+      return;
+   }
+   /*
+    * Find next appendable medium for SD
+    */
+   if (sscanf(bs->msg, Find_media, &Job, &index, &pool_name, &mr.MediaType) == 4) {
+      memset(&pr, 0, sizeof(pr));
+      bstrncpy(pr.Name, pool_name, sizeof(pr.Name));
+      ok = db_get_pool_record(jcr, jcr->db, &pr);
+      if (ok) {
+        mr.PoolId = pr.PoolId;
+        ok = find_next_volume_for_append(jcr, &mr, true /*permit create new vol*/);
+      }
       /*
        * Send Find Media response to Storage daemon
        */
@@ -115,13 +134,14 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
         send_volume_info_to_storage_daemon(jcr, bs, &mr);
       } else {
          bnet_fsend(bs, "1901 No Media.\n");
+         Dmsg0(500, "1901 No Media.\n");
       }
 
    /*
     * Request to find specific Volume information
     */
    } else if (sscanf(bs->msg, Get_Vol_Info, &Job, &mr.VolumeName, &writing) == 3) {
-      Dmsg1(400, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
+      Dmsg1(500, "CatReq GetVolInfo Vol=%s\n", mr.VolumeName);
       /*
        * Find the Volume
        */
@@ -175,9 +195,9 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
 
       } else {
          bnet_fsend(bs, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
+         Dmsg1(400, "1997 Volume \"%s\" not in catalog.\n", mr.VolumeName);
       }
 
-
    /*
     * Request to update Media record. Comes typically at the end
     *  of a Storage daemon Job Session, when labeling/relabeling a
@@ -203,20 +223,26 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
         return;
       }
       /* Set first written time if this is first job */
-      if (mr.VolJobs == 0 || sdmr.VolJobs == 1) {
+      if (mr.FirstWritten == 0) {
         mr.FirstWritten = jcr->start_time;   /* use Job start time as first write */
+        mr.set_first_written = true;
       }
       /* If we just labeled the tape set time */
-      Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
       if (label || mr.LabelDate == 0) {
-        mr.LabelDate = time(NULL);
+        mr.LabelDate = jcr->start_time;
+        mr.set_label_date = true;
+         Dmsg2(400, "label=%d labeldate=%d\n", label, mr.LabelDate);
       } else {
         /*
          * Insanity check for VolFiles get set to a smaller value
          */
         if (sdmr.VolFiles < mr.VolFiles) {
-            Jmsg(jcr, M_ERROR, 0, _("ERROR!! Volume Files at %u being set to %u. This is probably wrong.\n"),
-              mr.VolFiles, sdmr.VolFiles);
+            Jmsg(jcr, M_FATAL, 0, _("Volume Files at %u being set to %u"
+                 " for Volume \"%s\". This is incorrect.\n"),
+              mr.VolFiles, sdmr.VolFiles, mr.VolumeName);
+            bnet_fsend(bs, "1992 Update Media error\n");
+           db_unlock(jcr->db);
+           return;
         }
       }
       Dmsg2(400, "Update media: BefVolJobs=%u After=%u\n", mr.VolJobs, sdmr.VolJobs);
@@ -241,12 +267,10 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
        * Check if it has expired, and if not update the DB. Note, if
        *   Volume has expired, has_volume_expired() will update the DB.
        */
-      if (has_volume_expired(jcr, &mr)) {
-        send_volume_info_to_storage_daemon(jcr, bs, &mr);
-      } else if (db_update_media_record(jcr, jcr->db, &mr)) {
+      if (has_volume_expired(jcr, &mr) || db_update_media_record(jcr, jcr->db, &mr)) {
         send_volume_info_to_storage_daemon(jcr, bs, &mr);
       } else {
-         Jmsg(jcr, M_ERROR, 0, _("Catalog error updating Media record. %s"),
+         Jmsg(jcr, M_FATAL, 0, _("Catalog error updating Media record. %s"),
            db_strerror(jcr->db));
          bnet_fsend(bs, "1992 Update Media error\n");
          Dmsg0(400, "send error\n");
@@ -265,7 +289,7 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
       Dmsg6(400, "create_jobmedia JobId=%d MediaId=%d SF=%d EF=%d FI=%d LI=%d\n",
         jm.JobId, jm.MediaId, jm.StartFile, jm.EndFile, jm.FirstIndex, jm.LastIndex);
       if (!db_create_jobmedia_record(jcr, jcr->db, &jm)) {
-         Jmsg(jcr, M_ERROR, 0, _("Catalog error creating JobMedia record. %s"),
+         Jmsg(jcr, M_FATAL, 0, _("Catalog error creating JobMedia record. %s"),
            db_strerror(jcr->db));
          bnet_fsend(bs, "1991 Update JobMedia error\n");
       } else {
@@ -277,7 +301,7 @@ void catalog_request(JCR *jcr, BSOCK *bs, char *msg)
       omsg = get_memory(bs->msglen+1);
       pm_strcpy(omsg, bs->msg);
       bnet_fsend(bs, "1990 Invalid Catalog Request: %s", omsg);
-      Jmsg1(jcr, M_ERROR, 0, _("Invalid Catalog request: %s"), omsg);
+      Jmsg1(jcr, M_FATAL, 0, _("Invalid Catalog request: %s"), omsg);
       free_memory(omsg);
    }
    Dmsg1(400, ">CatReq response: %s", bs->msg);