<commit_before>/*
 *
 *  Routines for handling the autochanger.
 *
 *   <NAME>, August MMII
 *			      
 *   Version $Id$
 */
/*
   Copyright (C) 2000-2003 <NAME> and <NAME>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   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 along with this program; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

 */

#include "bacula.h"                   /* pull in global headers */
#include "stored.h"                   /* pull in Storage Deamon headers */

/* Forward referenced functions */
static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
static int get_autochanger_loaded_slot(JCR *jcr);


/*
 * Called here to do an autoload using the autochanger, if
 *  configured, and if a Slot has been defined for this Volume.
 *  On success this routine loads the indicated tape, but the
 *  label is not read, so it must be verified.
 *
 *  Note if dir is not NULL, it is the console requesting the 
 *   autoload for labeling, so we respond directly to the
 *   dir bsock.
 *
 *  Returns: 1 on success
 *	     0 on failure (no changer available) 
 *	    -1 on error on autochanger
 */
int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
{
   int slot = jcr->VolCatInfo.Slot;
   int drive = jcr->device->drive_index;
   int rtn_stat = -1;		      /* error status */
     
   /*
    * Handle autoloaders here.	If we cannot autoload it, we
    *  will return FALSE to ask the sysop.
    */
   if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
      if (dir) {
	 return 0;		      /* For user, bail out right now */
      }
      if (dir_find_next_appendable_volume(jcr)) {
	 slot = jcr->VolCatInfo.Slot; 
      }
   }
   Dmsg1(400, "Want changer slot=%d\n", slot);

   if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
      uint32_t timeout = jcr->device->max_changer_wait;
      POOLMEM *changer, *results;
      int loaded, status;     

      results = get_pool_memory(PM_MESSAGE);
      changer = get_pool_memory(PM_FNAME);

      loaded = get_autochanger_loaded_slot(jcr);

      Dmsg1(400, "loaded=%s\n", results);

      /* If tape we want is not loaded, load it. */
      if (loaded != slot) { 
	 offline_or_rewind_dev(dev);
	 /* We are going to load a new tape, so close the device */
	 force_close_dev(dev);
	 if (loaded != 0) {	   /* must unload drive */
            Dmsg0(400, "Doing changer unload.\n");
	    Jmsg(jcr, M_INFO, 0, 
                 _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
		 loaded, drive);
	    jcr->VolCatInfo.Slot = loaded;   /* slot to be unloaded */
	    changer = edit_device_codes(jcr, changer, 
                        jcr->device->changer_command, "unload");
	    status = run_program(changer, timeout, NULL);
            Dmsg1(400, "unload status=%d\n", status);
	 }
	 /*
	  * Load the desired cassette	 
	  */
         Dmsg1(400, "Doing changer load slot %d\n", slot);
	 Jmsg(jcr, M_INFO, 0, 
              _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"), 
	      slot, drive);
	 jcr->VolCatInfo.Slot = slot;	 /* slot to be loaded */
	 changer = edit_device_codes(jcr, changer, 
                      jcr->device->changer_command, "load");
	 status = run_program(changer, timeout, NULL);
	 if (status == 0) {
            Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
		    slot, drive);
	 } else {
            Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\", status=%d.\n"),
		    slot, drive, status);
	 }
         Dmsg2(400, "load slot %d status=%d\n", slot, status);
      } else { 
	 status = 0;		      /* we got what we want */
      }
      free_pool_memory(changer);
      free_pool_memory(results);
      Dmsg1(400, "After changer, status=%d\n", status);
      if (status == 0) {	      /* did we succeed? */
	 rtn_stat = 1;		      /* tape loaded by changer */
      }
   } else {
      rtn_stat = 0;		      /* no changer found */
   }
   return rtn_stat;
}

static int get_autochanger_loaded_slot(JCR *jcr)
{
   POOLMEM *changer, *results;
   int status, loaded;
   uint32_t timeout = jcr->device->max_changer_wait;
   int drive = jcr->device->drive_index;

   results = get_pool_memory(PM_MESSAGE);
   changer = get_pool_memory(PM_FNAME);

   /* Find out what is loaded, zero means device is unloaded */
   Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
	drive);
   changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
                "loaded");
   status = run_program(changer, timeout, results);
   Dmsg3(000, "run_prog: %s stat=%d result=%s\n", changer, status, results);
   if (status == 0) {
      loaded = atoi(results);
      if (loaded > 0) {
         Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
	      drive, loaded);
      } else {
         Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
	      drive);
      }
   } else {
      Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command, status=%d.\n"), 
	   drive, status);
      loaded = -1;		/* force unload */
   }
   free_pool_memory(changer);
   free_pool_memory(results);
   return loaded;
}

/*<commit_msg>Turn off debug message<commit_after>/*
 *
 *  Routines for handling the autochanger.
 *
 *   <NAME>, <NAME>
 *			      
 *   Version $Id$
 */
/*
   Copyright (C) 2000-2003 <NAME> and <NAME>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   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 along with this program; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
   MA 02111-1307, USA.

 */

#include "bacula.h"                   /* pull in global headers */
#include "stored.h"                   /* pull in Storage Deamon headers */

/* Forward referenced functions */
static char *edit_device_codes(JCR *jcr, char *omsg, char *imsg, char *cmd);
static int get_autochanger_loaded_slot(JCR *jcr);


/*
 * Called here to do an autoload using the autochanger, if
 *  configured, and if a Slot has been defined for this Volume.
 *  On success this routine loads the indicated tape, but the
 *  label is not read, so it must be verified.
 *
 *  Note if dir is not NULL, it is the console requesting the 
 *   autoload for labeling, so we respond directly to the
 *   dir bsock.
 *
 *  Returns: 1 on success
 *	     0 on failure (no changer available) 
 *	    -1 on error on autochanger
 */
int autoload_device(JCR *jcr, DEVICE *dev, int writing, BSOCK *dir)
{
   int slot = jcr->VolCatInfo.Slot;
   int drive = jcr->device->drive_index;
   int rtn_stat = -1;		      /* error status */
     
   /*
    * Handle autoloaders here.	If we cannot autoload it, we
    *  will return FALSE to ask the sysop.
    */
   if (writing && dev_cap(dev, CAP_AUTOCHANGER) && slot <= 0) {
      if (dir) {
	 return 0;		      /* For user, bail out right now */
      }
      if (dir_find_next_appendable_volume(jcr)) {
	 slot = jcr->VolCatInfo.Slot; 
      }
   }
   Dmsg1(400, "Want changer slot=%d\n", slot);

   if (slot > 0 && jcr->device->changer_name && jcr->device->changer_command) {
      uint32_t timeout = jcr->device->max_changer_wait;
      POOLMEM *changer, *results;
      int loaded, status;     

      results = get_pool_memory(PM_MESSAGE);
      changer = get_pool_memory(PM_FNAME);

      loaded = get_autochanger_loaded_slot(jcr);

      Dmsg1(400, "loaded=%s\n", results);

      /* If tape we want is not loaded, load it. */
      if (loaded != slot) { 
	 offline_or_rewind_dev(dev);
	 /* We are going to load a new tape, so close the device */
	 force_close_dev(dev);
	 if (loaded != 0) {	   /* must unload drive */
            Dmsg0(400, "Doing changer unload.\n");
	    Jmsg(jcr, M_INFO, 0, 
                 _("3303 Issuing autochanger \"unload slot %d, drive %d\" command.\n"),
		 loaded, drive);
	    jcr->VolCatInfo.Slot = loaded;   /* slot to be unloaded */
	    changer = edit_device_codes(jcr, changer, 
                        jcr->device->changer_command, "unload");
	    status = run_program(changer, timeout, NULL);
            Dmsg1(400, "unload status=%d\n", status);
	 }
	 /*
	  * Load the desired cassette	 
	  */
         Dmsg1(400, "Doing changer load slot %d\n", slot);
	 Jmsg(jcr, M_INFO, 0, 
              _("3304 Issuing autochanger \"load slot %d, drive %d\" command.\n"), 
	      slot, drive);
	 jcr->VolCatInfo.Slot = slot;	 /* slot to be loaded */
	 changer = edit_device_codes(jcr, changer, 
                      jcr->device->changer_command, "load");
	 status = run_program(changer, timeout, NULL);
	 if (status == 0) {
            Jmsg(jcr, M_INFO, 0, _("3305 Autochanger \"load slot %d, drive %d\", status is OK.\n"),
		    slot, drive);
	 } else {
            Jmsg(jcr, M_INFO, 0, _("3992 Bad autochanger \"load slot %d, drive %d\", status=%d.\n"),
		    slot, drive, status);
	 }
         Dmsg2(400, "load slot %d status=%d\n", slot, status);
      } else { 
	 status = 0;		      /* we got what we want */
      }
      free_pool_memory(changer);
      free_pool_memory(results);
      Dmsg1(400, "After changer, status=%d\n", status);
      if (status == 0) {	      /* did we succeed? */
	 rtn_stat = 1;		      /* tape loaded by changer */
      }
   } else {
      rtn_stat = 0;		      /* no changer found */
   }
   return rtn_stat;
}

static int get_autochanger_loaded_slot(JCR *jcr)
{
   POOLMEM *changer, *results;
   int status, loaded;
   uint32_t timeout = jcr->device->max_changer_wait;
   int drive = jcr->device->drive_index;

   results = get_pool_memory(PM_MESSAGE);
   changer = get_pool_memory(PM_FNAME);

   /* Find out what is loaded, zero means device is unloaded */
   Jmsg(jcr, M_INFO, 0, _("3301 Issuing autochanger \"loaded drive %d\" command.\n"),
	drive);
   changer = edit_device_codes(jcr, changer, jcr->device->changer_command, 
                "loaded");
   status = run_program(changer, timeout, results);
   Dmsg3(100, "run_prog: %s stat=%d result=%s\n", changer, status, results);
   if (status == 0) {
      loaded = atoi(results);
      if (loaded > 0) {
         Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result is Slot %d.\n"),
	      drive, loaded);
      } else {
         Jmsg(jcr, M_INFO, 0, _("3302 Autochanger \"loaded drive %d\", result: nothing loaded.\n"),
	      drive);
      }
   } else {
      Jmsg(jcr, M_INFO, 0, _("3991 Bad autochanger \"loaded drive %d\" command, status=%d.\n"), 
	   drive, status);
      loaded = -1;		/* force unload */
   }
   free_pool_memory(changer);
   free_pool_memory(results);
   return loaded;
}

/*
{'fraction_non_alphanumeric': 0.12254315501623654, 'fraction_numerical': 0.019654759870107674, 'mean_word_length': 2.5603894128384543, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4308973', 'n_tokens_mistral': 4113, 'n_tokens_neox': 3809, 'n_words': 1248}