]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/dird/ua_label.c
Add heap stats to Dir and SD -- eliminate #ifdefs
[bacula/bacula] / bacula / src / dird / ua_label.c
index f663fc7ce6d8848d2a5a67602940bd9365288f5a..972f5c6fa50e40202dcb5206211a957217b35c1e 100644 (file)
@@ -39,13 +39,13 @@ typedef struct s_vol_list {
 
 
 /* Forward referenced functions */
-static int do_label(UAContext *ua, char *cmd, int relabel);
+static int do_label(UAContext *ua, const char *cmd, int relabel);
 static void label_from_barcodes(UAContext *ua);
 static int send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr, 
               POOL_DBR *pr, int relabel, bool media_record_exits);
 static vol_list_t *get_vol_list_from_SD(UAContext *ua, bool scan);
 static void free_vol_list(vol_list_t *vol_list);
-static int is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr);
+static bool is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr);
 static BSOCK *open_sd_bsock(UAContext *ua);
 static void close_sd_bsock(UAContext *ua);
 static char *get_volume_name_from_SD(UAContext *ua, int Slot);
@@ -56,22 +56,22 @@ static char *get_volume_name_from_SD(UAContext *ua, int Slot);
  *  
  *   label storage=xxx volume=vvv
  */
-int label_cmd(UAContext *ua, char *cmd)
+int label_cmd(UAContext *ua, const char *cmd)
 {
    return do_label(ua, cmd, 0);       /* standard label */
 }
 
-int relabel_cmd(UAContext *ua, char *cmd)
+int relabel_cmd(UAContext *ua, const char *cmd)
 {
    return do_label(ua, cmd, 1);      /* relabel tape */
 }
 
-#define MAX_SLOTS 5000
+static int const max_slots = 5000;
 
 static bool get_user_slot_list(UAContext *ua, char *slot_list, int num_slots)
 {
    int i;
-   char *msg;
+   const char *msg;
 
    for (int i=0; i<num_slots; i++) {
       slot_list[i] = 0;
@@ -174,8 +174,8 @@ int update_slots(UAContext *ua)
 
    scan = find_arg(ua, _("scan")) >= 0;
 
-   slot_list = (char *)malloc(MAX_SLOTS);
-   if (!get_user_slot_list(ua, slot_list, MAX_SLOTS)) {
+   slot_list = (char *)malloc(max_slots);
+   if (!get_user_slot_list(ua, slot_list, max_slots)) {
       free(slot_list);
       return 1;
    }
@@ -189,8 +189,13 @@ int update_slots(UAContext *ua)
 
    /* Walk through the list updating the media records */
    for (vl=vol_list; vl; vl=vl->next) {
+      if (vl->Slot >= max_slots) {
+         bsendmsg(ua, _("Slot %d larger than max %d ignored.\n"));
+        continue;
+      }
       /* Check if user wants us to look at this slot */
       if (!slot_list[vl->Slot]) {
+         Dmsg1(100, "Skipping slot=%d\n", vl->Slot);
         continue;
       }
       /* If scanning, we read the label rather than the barcode */
@@ -200,8 +205,19 @@ int update_slots(UAContext *ua)
            vl->VolName = NULL;
         }
         vl->VolName = get_volume_name_from_SD(ua, vl->Slot);
+         Dmsg2(100, "Got Vol=%s from SD for Slot=%d\n", vl->VolName, vl->Slot);
       }
+      slot_list[vl->Slot] = 0;       /* clear Slot */
       if (!vl->VolName) {
+         Dmsg1(100, "No VolName for Slot=%d setting InChanger to zero.\n", vl->Slot);
+        memset(&mr, 0, sizeof(mr));
+        mr.Slot = vl->Slot;
+        mr.InChanger = 1;
+        /* Set InChanger to zero for this Slot */
+        db_lock(ua->db);
+        db_make_inchanger_unique(ua->jcr, ua->db, &mr);
+        db_unlock(ua->db);
+         bsendmsg(ua, _("No VolName for Slot=%d set InChanger to zero.\n"), vl->Slot);
         continue;
       }
       memset(&mr, 0, sizeof(mr));
@@ -230,8 +246,19 @@ int update_slots(UAContext *ua)
       }
       db_unlock(ua->db);
    }
-
-
+   memset(&mr, 0, sizeof(mr));
+   mr.InChanger = 1;
+   for (int i=1; i<max_slots; i++) {
+      if (slot_list[i]) {
+        mr.Slot = i;
+        /* Set InChanger to zero for this Slot */
+        db_lock(ua->db);
+        db_make_inchanger_unique(ua->jcr, ua->db, &mr);
+        db_unlock(ua->db);
+         bsendmsg(ua, _("No VolName for Slot=%d set InChanger to zero.\n"), i);
+      }
+   }
+      
 bail_out:
 
    free_vol_list(vol_list);
@@ -245,7 +272,7 @@ bail_out:
 /*
  * Common routine for both label and relabel
  */
-static int do_label(UAContext *ua, char *cmd, int relabel)
+static int do_label(UAContext *ua, const char *cmd, int relabel)
 {
    STORE *store;
    BSOCK *sd;
@@ -256,7 +283,7 @@ static int do_label(UAContext *ua, char *cmd, int relabel)
    int ok = FALSE;
    int i;
    bool media_record_exists = false;
-   static char *barcode_keyword[] = {
+   static const char *barcode_keyword[] = {
       "barcode",
       "barcodes",
       NULL};
@@ -414,10 +441,9 @@ static void label_from_barcodes(UAContext *ua)
    bool media_record_exists;
    char *slot_list;
 
-   slot_list = (char *)malloc(MAX_SLOTS);
-   if (!get_user_slot_list(ua, slot_list, MAX_SLOTS)) {
-      free(slot_list);
-      return;
+   slot_list = (char *)malloc(max_slots);
+   if (!get_user_slot_list(ua, slot_list, max_slots)) {
+      goto bail_out;
    }
 
    vol_list = get_vol_list_from_SD(ua, false /*no scan*/);
@@ -495,13 +521,12 @@ static void label_from_barcodes(UAContext *ua)
       bstrncpy(mr.MediaType, store->media_type, sizeof(mr.MediaType));
 
       mr.Slot = vl->Slot;
-      if (!send_label_request(ua, &mr, &omr, &pr, 0, media_record_exists)) {
-        goto bail_out;
-      }
+      send_label_request(ua, &mr, &omr, &pr, 0, media_record_exists);
    }
 
 
 bail_out:
+   free(slot_list);
    free_vol_list(vol_list);
    close_sd_bsock(ua);
 
@@ -512,11 +537,11 @@ bail_out:
  * Check if the Volume name has legal characters
  * If ua is non-NULL send the message
  */
-int is_volume_name_legal(UAContext *ua, char *name)
+bool is_volume_name_legal(UAContext *ua, const char *name)
 {
    int len;
-   char *p;
-   char *accept = ":.-_";
+   const char *p;
+   const char *accept = ":.-_";
 
    /* Restrict the characters permitted in the Volume name */
    for (p=name; *p; p++) {
@@ -544,6 +569,9 @@ int is_volume_name_legal(UAContext *ua, char *name)
    return 1;
 }
 
+/*
+ * NOTE! This routine opens the SD socket but leaves it open
+ */
 static int send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr, 
                              POOL_DBR *pr, int relabel, bool media_record_exists)
 {
@@ -605,7 +633,7 @@ static int send_label_request(UAContext *ua, MEDIA_DBR *mr, MEDIA_DBR *omr,
         }
       }
    } else {
-      bsendmsg(ua, _("Label command failed.\n"));
+      bsendmsg(ua, _("Label command failed for Volume %s.\n"), mr->VolumeName);
    }
    return ok;
 }
@@ -643,6 +671,7 @@ static char *get_volume_name_from_SD(UAContext *ua, int Slot)
    int rtn_slot;
 
    if (!(sd=open_sd_bsock(ua))) {
+      bsendmsg(ua, _("Could not open SD socket.\n"));
       return NULL;
    }
    bstrncpy(dev_name, store->dev_name, sizeof(dev_name));
@@ -654,6 +683,7 @@ static char *get_volume_name_from_SD(UAContext *ua, int Slot)
    /* Get Volume name in this Slot */
    while (bnet_recv(sd) >= 0) {
       bsendmsg(ua, "%s", sd->msg);
+      Dmsg1(100, "Got: %s", sd->msg);
       if (strncmp(sd->msg, "3001 Volume=", 12) == 0) {
         VolName = (char *)malloc(sd->msglen);
          if (sscanf(sd->msg, "3001 Volume=%s Slot=%d", VolName, &rtn_slot) == 2) {
@@ -663,6 +693,7 @@ static char *get_volume_name_from_SD(UAContext *ua, int Slot)
         VolName = NULL;
       }
    }
+   close_sd_bsock(ua);
    Dmsg1(200, "get_vol_name=%s\n", NPRT(VolName));
    return VolName;
 }
@@ -740,10 +771,14 @@ static vol_list_t *get_vol_list_from_SD(UAContext *ua, bool scan)
       vl = (vol_list_t *)malloc(sizeof(vol_list_t));
       vl->Slot = Slot;
       if (p) {
+         if (*p == ':') {
+           p++;                      /* skip separator */
+        }
         vl->VolName = bstrdup(p);
       } else {
         vl->VolName = NULL;
       }
+      Dmsg2(100, "Add slot=%d Vol=%s to SD list.\n", vl->Slot, NPRT(vl->VolName));
       if (!vol_list) {
         vl->next = vol_list;
         vol_list = vl;
@@ -765,6 +800,7 @@ static vol_list_t *get_vol_list_from_SD(UAContext *ua, bool scan)
 static void free_vol_list(vol_list_t *vol_list)
 {
    vol_list_t *vl;
+
    /* Free list */
    for (vl=vol_list; vl; ) {
       vol_list_t *ovl;
@@ -783,20 +819,18 @@ static void free_vol_list(vol_list_t *vol_list)
  *  with the Cleaning Prefix. If they match, this is a cleaning 
  *  tape.
  */
-static int is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr)
+static bool is_cleaning_tape(UAContext *ua, MEDIA_DBR *mr, POOL_DBR *pr)
 {
+   /* Find Pool resource */
+   ua->jcr->pool = (POOL *)GetResWithName(R_POOL, pr->Name);
    if (!ua->jcr->pool) {
-      /* Find Pool resource */
-      ua->jcr->pool = (POOL *)GetResWithName(R_POOL, pr->Name);
-      if (!ua->jcr->pool) {
-         bsendmsg(ua, _("Pool %s resource not found!\n"), pr->Name);
-        return 1;
-      }
+      bsendmsg(ua, _("Pool \"%s\" resource not found!\n"), pr->Name);
+      return true;
    }
    if (ua->jcr->pool->cleaning_prefix == NULL) {
-      return 0;
+      return false;
    }
-   Dmsg4(200, "CLNprefix=%s: Vol=%s: len=%d strncmp=%d\n",
+   Dmsg4(100, "CLNprefix=%s: Vol=%s: len=%d strncmp=%d\n",
       ua->jcr->pool->cleaning_prefix, mr->VolumeName,
       strlen(ua->jcr->pool->cleaning_prefix), 
       strncmp(mr->VolumeName, ua->jcr->pool->cleaning_prefix,