src/dird/ua_label.c
void update_slots(UAContext *ua)
{
   USTORE store;
   vol_list_t *vl, *vol_list = NULL;
   MEDIA_DBR mr;
   char *slot_list;
   bool scan;
   int max_slots;
   int drive;
   int Enabled = 1;
   bool have_enabled;
   int i;


   if (!open_client_db(ua)) {
      return;
   }
   store.store = get_storage_resource(ua, true/*arg is storage*/);
   if (!store.store) {
      return;
   }
   pm_strcpy(store.store_source, _("Command input"));
   set_wstorage(ua->jcr, &store);
   drive = get_storage_drive(ua, store.store);

   scan = find_arg(ua, NT_("scan")) >= 0;
   if ((i=find_arg_with_value(ua, NT_("Enabled"))) >= 0) {
      Enabled = get_enabled(ua, ua->argv[i]);
      if (Enabled < 0) {
         return;
      }
      have_enabled = true;
   } else {
      have_enabled = false;
   }

   max_slots = get_num_slots_from_SD(ua);
   Dmsg1(100, "max_slots=%d\n", max_slots);
   if (max_slots <= 0) {
      ua->warning_msg(_("No slots in changer to scan.\n"));
      return;
   }
   slot_list = (char *)malloc(max_slots+1);
   if (!get_user_slot_list(ua, slot_list, max_slots)) {
      free(slot_list);
      return;
   }

   vol_list = get_vol_list_from_SD(ua, scan);

   if (!vol_list) {
      ua->warning_msg(_("No Volumes found to label, or no barcodes.\n"));
      goto bail_out;
   }

   /* First zap out any InChanger with StorageId=0 */
   db_sql_query(ua->db, "UPDATE Media SET InChanger=0 WHERE StorageId=0", NULL, NULL);

   /* Walk through the list updating the media records */
   for (vl=vol_list; vl; vl=vl->next) {
      if (vl->Slot > max_slots) {
         ua->warning_msg(_("Slot %d greater than max %d ignored.\n"),
            vl->Slot, max_slots);
         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 */
      if (scan) {
         if (vl->VolName) {
            free(vl->VolName);
            vl->VolName = NULL;
         }
         vl->VolName = get_volume_name_from_SD(ua, vl->Slot, drive);
         Dmsg2(100, "Got Vol=%s from SD for Slot=%d\n", vl->VolName, vl->Slot);
      }
      slot_list[vl->Slot] = 0;        /* clear Slot */
      mr.Slot = vl->Slot;
      mr.InChanger = 1;
      mr.MediaId = 0;                 /* Force using VolumeName */
      if (vl->VolName) {
         bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName));
      } else {
         mr.VolumeName[0] = 0;
      }
      set_storageid_in_mr(store.store, &mr);
      Dmsg4(100, "Before make unique: Vol=%s slot=%d inchanger=%d sid=%d\n",
            mr.VolumeName, mr.Slot, mr.InChanger, mr.StorageId);
      db_lock(ua->db);
      /* Set InChanger to zero for this Slot */
      db_make_inchanger_unique(ua->jcr, ua->db, &mr);
      db_unlock(ua->db);
      Dmsg4(100, "After make unique: Vol=%s slot=%d inchanger=%d sid=%d\n",
            mr.VolumeName, mr.Slot, mr.InChanger, mr.StorageId);
      if (!vl->VolName) {
         Dmsg1(100, "No VolName for Slot=%d setting InChanger to zero.\n", vl->Slot);
         ua->info_msg(_("No VolName for Slot=%d InChanger set to zero.\n"), vl->Slot);
         continue;
      }
      db_lock(ua->db);
      Dmsg4(100, "Before get MR: Vol=%s slot=%d inchanger=%d sid=%d\n",
            mr.VolumeName, mr.Slot, mr.InChanger, mr.StorageId);
      if (db_get_media_record(ua->jcr, ua->db, &mr)) {
         Dmsg4(100, "After get MR: Vol=%s slot=%d inchanger=%d sid=%d\n",
            mr.VolumeName, mr.Slot, mr.InChanger, mr.StorageId);
         /* If Slot, Inchanger, and StorageId have changed, update the Media record */
         if (mr.Slot != vl->Slot || !mr.InChanger || mr.StorageId != store.store->StorageId) {
            mr.Slot = vl->Slot;
            mr.InChanger = 1;
            if (have_enabled) {
               mr.Enabled = Enabled;
            }
            set_storageid_in_mr(store.store, &mr);
            if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
               ua->error_msg("%s", db_strerror(ua->db));
            } else {
               ua->info_msg(_(
                 "Catalog record for Volume \"%s\" updated to reference slot %d.\n"),
                 mr.VolumeName, mr.Slot);
            }
         } else {
            ua->info_msg(_("Catalog record for Volume \"%s\" is up to date.\n"),
               mr.VolumeName);
         }
         db_unlock(ua->db);
         continue;
      } else {
         ua->warning_msg(_("Volume \"%s\" not found in catalog. Slot=%d InChanger set to zero.\n"),
             mr.VolumeName, vl->Slot);
      }
      db_unlock(ua->db);
   }
   mr.clear();
   mr.InChanger = 1;
   set_storageid_in_mr(store.store, &mr);
   db_lock(ua->db);
   for (int i=1; i <= max_slots; i++) {
      if (slot_list[i]) {
         mr.Slot = i;
         /* Set InChanger to zero for this Slot */
         db_make_inchanger_unique(ua->jcr, ua->db, &mr);
      }
   }
   db_unlock(ua->db);

bail_out:
   if (vol_list) {
      free_vol_list(vol_list);
   }
   free(slot_list);
   close_sd_bsock(ua);

   return;
}
/*
 * Update Slots corresponding to Volumes in autochanger
 */
Update Slots corresponding to Volumes in autochanger
Update Slots corresponding to Volumes in autochanger
void update_slots(UAContext *ua)
{
   USTORE store;
   vol_list_t *vl, *vol_list = NULL;
   MEDIA_DBR mr;
   char *slot_list;
   bool scan;
   int max_slots;
   int drive;
   int Enabled = 1;
   bool have_enabled;
   int i;
   if (!open_client_db(ua)) {
      return;
   }
   store.store = get_storage_resource(ua, true);
   if (!store.store) {
      return;
   }
   pm_strcpy(store.store_source, _("Command input"));
   set_wstorage(ua->jcr, &store);
   drive = get_storage_drive(ua, store.store);
   scan = find_arg(ua, NT_("scan")) >= 0;
   if ((i=find_arg_with_value(ua, NT_("Enabled"))) >= 0) {
      Enabled = get_enabled(ua, ua->argv[i]);
      if (Enabled < 0) {
         return;
      }
      have_enabled = true;
   } else {
      have_enabled = false;
   }
   max_slots = get_num_slots_from_SD(ua);
   Dmsg1(100, "max_slots=%d\n", max_slots);
   if (max_slots <= 0) {
      ua->warning_msg(_("No slots in changer to scan.\n"));
      return;
   }
   slot_list = (char *)malloc(max_slots+1);
   if (!get_user_slot_list(ua, slot_list, max_slots)) {
      free(slot_list);
      return;
   }
   vol_list = get_vol_list_from_SD(ua, scan);
   if (!vol_list) {
      ua->warning_msg(_("No Volumes found to label, or no barcodes.\n"));
      goto bail_out;
   }
   db_sql_query(ua->db, "UPDATE Media SET InChanger=0 WHERE StorageId=0", NULL, NULL);
   for (vl=vol_list; vl; vl=vl->next) {
      if (vl->Slot > max_slots) {
         ua->warning_msg(_("Slot %d greater than max %d ignored.\n"),
            vl->Slot, max_slots);
         continue;
      }
      if (!slot_list[vl->Slot]) {
         Dmsg1(100, "Skipping slot=%d\n", vl->Slot);
         continue;
      }
      if (scan) {
         if (vl->VolName) {
            free(vl->VolName);
            vl->VolName = NULL;
         }
         vl->VolName = get_volume_name_from_SD(ua, vl->Slot, drive);
         Dmsg2(100, "Got Vol=%s from SD for Slot=%d\n", vl->VolName, vl->Slot);
      }
      slot_list[vl->Slot] = 0;        
      mr.Slot = vl->Slot;
      mr.InChanger = 1;
      mr.MediaId = 0;                 
      if (vl->VolName) {
         bstrncpy(mr.VolumeName, vl->VolName, sizeof(mr.VolumeName));
      } else {
         mr.VolumeName[0] = 0;
      }
      set_storageid_in_mr(store.store, &mr);
      Dmsg4(100, "Before make unique: Vol=%s slot=%d inchanger=%d sid=%d\n",
            mr.VolumeName, mr.Slot, mr.InChanger, mr.StorageId);
      db_lock(ua->db);
      db_make_inchanger_unique(ua->jcr, ua->db, &mr);
      db_unlock(ua->db);
      Dmsg4(100, "After make unique: Vol=%s slot=%d inchanger=%d sid=%d\n",
            mr.VolumeName, mr.Slot, mr.InChanger, mr.StorageId);
      if (!vl->VolName) {
         Dmsg1(100, "No VolName for Slot=%d setting InChanger to zero.\n", vl->Slot);
         ua->info_msg(_("No VolName for Slot=%d InChanger set to zero.\n"), vl->Slot);
         continue;
      }
      db_lock(ua->db);
      Dmsg4(100, "Before get MR: Vol=%s slot=%d inchanger=%d sid=%d\n",
            mr.VolumeName, mr.Slot, mr.InChanger, mr.StorageId);
      if (db_get_media_record(ua->jcr, ua->db, &mr)) {
         Dmsg4(100, "After get MR: Vol=%s slot=%d inchanger=%d sid=%d\n",
            mr.VolumeName, mr.Slot, mr.InChanger, mr.StorageId);
         if (mr.Slot != vl->Slot || !mr.InChanger || mr.StorageId != store.store->StorageId) {
            mr.Slot = vl->Slot;
            mr.InChanger = 1;
            if (have_enabled) {
               mr.Enabled = Enabled;
            }
            set_storageid_in_mr(store.store, &mr);
            if (!db_update_media_record(ua->jcr, ua->db, &mr)) {
               ua->error_msg("%s", db_strerror(ua->db));
            } else {
               ua->info_msg(_(
                 "Catalog record for Volume \"%s\" updated to reference slot %d.\n"),
                 mr.VolumeName, mr.Slot);
            }
         } else {
            ua->info_msg(_("Catalog record for Volume \"%s\" is up to date.\n"),
               mr.VolumeName);
         }
         db_unlock(ua->db);
         continue;
      } else {
         ua->warning_msg(_("Volume \"%s\" not found in catalog. Slot=%d InChanger set to zero.\n"),
             mr.VolumeName, vl->Slot);
      }
      db_unlock(ua->db);
   }
   mr.clear();
   mr.InChanger = 1;
   set_storageid_in_mr(store.store, &mr);
   db_lock(ua->db);
   for (int i=1; i <= max_slots; i++) {
      if (slot_list[i]) {
         mr.Slot = i;
         db_make_inchanger_unique(ua->jcr, ua->db, &mr);
      }
   }
   db_unlock(ua->db);
bail_out:
   if (vol_list) {
      free_vol_list(vol_list);
   }
   free(slot_list);
   close_sd_bsock(ua);
   return;
}
void update_slots ( UAContext * ua ) { USTORE store ; vol_list_t * vl , * vol_list = NULL ; MEDIA_DBR mr ; char * slot_list ; bool scan ; int max_slots ; int drive ; int Enabled = 1 ; bool have_enabled ; int i ; if ( ! open_client_db ( ua ) ) { return ; } store . store = get_storage_resource ( ua , true ) ; if ( ! store . store ) { return ; } pm_strcpy ( store . store_source , _ ( " " ) ) ; set_wstorage ( ua -> jcr , & store ) ; drive = get_storage_drive ( ua , store . store ) ; scan = find_arg ( ua , NT_ ( " " ) ) >= 0 ; if ( ( i = find_arg_with_value ( ua , NT_ ( " " ) ) ) >= 0 ) { Enabled = get_enabled ( ua , ua -> argv [ i ] ) ; if ( Enabled < 0 ) { return ; } have_enabled = true ; } else { have_enabled = false ; } max_slots = get_num_slots_from_SD ( ua ) ; Dmsg1 ( 100 , " \n " , max_slots ) ; if ( max_slots <= 0 ) { ua -> warning_msg ( _ ( " \n " ) ) ; return ; } slot_list = ( char * ) malloc ( max_slots + 1 ) ; if ( ! get_user_slot_list ( ua , slot_list , max_slots ) ) { free ( slot_list ) ; return ; } vol_list = get_vol_list_from_SD ( ua , scan ) ; if ( ! vol_list ) { ua -> warning_msg ( _ ( " \n " ) ) ; goto bail_out ; } db_sql_query ( ua -> db , " " , NULL , NULL ) ; for ( vl = vol_list ; vl ; vl = vl -> next ) { if ( vl -> Slot > max_slots ) { ua -> warning_msg ( _ ( " \n " ) , vl -> Slot , max_slots ) ; continue ; } if ( ! slot_list [ vl -> Slot ] ) { Dmsg1 ( 100 , " \n " , vl -> Slot ) ; continue ; } if ( scan ) { if ( vl -> VolName ) { free ( vl -> VolName ) ; vl -> VolName = NULL ; } vl -> VolName = get_volume_name_from_SD ( ua , vl -> Slot , drive ) ; Dmsg2 ( 100 , " \n " , vl -> VolName , vl -> Slot ) ; } slot_list [ vl -> Slot ] = 0 ; mr . Slot = vl -> Slot ; mr . InChanger = 1 ; mr . MediaId = 0 ; if ( vl -> VolName ) { bstrncpy ( mr . VolumeName , vl -> VolName , sizeof ( mr . VolumeName ) ) ; } else { mr . VolumeName [ 0 ] = 0 ; } set_storageid_in_mr ( store . store , & mr ) ; Dmsg4 ( 100 , " \n " , mr . VolumeName , mr . Slot , mr . InChanger , mr . StorageId ) ; db_lock ( ua -> db ) ; db_make_inchanger_unique ( ua -> jcr , ua -> db , & mr ) ; db_unlock ( ua -> db ) ; Dmsg4 ( 100 , " \n " , mr . VolumeName , mr . Slot , mr . InChanger , mr . StorageId ) ; if ( ! vl -> VolName ) { Dmsg1 ( 100 , " \n " , vl -> Slot ) ; ua -> info_msg ( _ ( " \n " ) , vl -> Slot ) ; continue ; } db_lock ( ua -> db ) ; Dmsg4 ( 100 , " \n " , mr . VolumeName , mr . Slot , mr . InChanger , mr . StorageId ) ; if ( db_get_media_record ( ua -> jcr , ua -> db , & mr ) ) { Dmsg4 ( 100 , " \n " , mr . VolumeName , mr . Slot , mr . InChanger , mr . StorageId ) ; if ( mr . Slot != vl -> Slot || ! mr . InChanger || mr . StorageId != store . store -> StorageId ) { mr . Slot = vl -> Slot ; mr . InChanger = 1 ; if ( have_enabled ) { mr . Enabled = Enabled ; } set_storageid_in_mr ( store . store , & mr ) ; if ( ! db_update_media_record ( ua -> jcr , ua -> db , & mr ) ) { ua -> error_msg ( " " , db_strerror ( ua -> db ) ) ; } else { ua -> info_msg ( _ ( " \" \" \n " ) , mr . VolumeName , mr . Slot ) ; } } else { ua -> info_msg ( _ ( " \" \" \n " ) , mr . VolumeName ) ; } db_unlock ( ua -> db ) ; continue ; } else { ua -> warning_msg ( _ ( " \" \" \n " ) , mr . VolumeName , vl -> Slot ) ; } db_unlock ( ua -> db ) ; } mr . clear ( ) ; mr . InChanger = 1 ; set_storageid_in_mr ( store . store , & mr ) ; db_lock ( ua -> db ) ; for ( int i = 1 ; i <= max_slots ; i ++ ) { if ( slot_list [ i ] ) { mr . Slot = i ; db_make_inchanger_unique ( ua -> jcr , ua -> db , & mr ) ; } } db_unlock ( ua -> db ) ; bail_out : if ( vol_list ) { free_vol_list ( vol_list ) ; } free ( slot_list ) ; close_sd_bsock ( ua ) ; return ; }
Update Slots corresponding to Volumes in autochanger
Update Slots corresponding to Volumes in autochanger
/*arg is storage*/ /* First zap out any InChanger with StorageId=0 */ /* Walk through the list updating the media records */ /* Check if user wants us to look at this slot */ /* If scanning, we read the label rather than the barcode */ /* clear Slot */ /* Force using VolumeName */ /* Set InChanger to zero for this Slot */ /* If Slot, Inchanger, and StorageId have changed, update the Media record */ /* Set InChanger to zero for this Slot */