src/stored/ansi_label.c
bool write_ansi_ibm_labels(DCR *dcr, int type, const char *VolName)
{
   DEVICE *dev = dcr->dev;
   JCR *jcr = dcr->jcr;
   char ansi_volname[7];              /* 6 char + \0 */
   char label[80];                    /* tape label */
   char date[20];                     /* ansi date buffer */
   time_t now;
   int len, stat, label_type;

   /*
    * If the Device requires a specific label type use it,
    * otherwise, use the type requested by the Director
    */
   if (dcr->device->label_type != B_BACULA_LABEL) {
      label_type = dcr->device->label_type;   /* force label type */
   } else {
      label_type = dcr->VolCatInfo.LabelType; /* accept Dir type */
   }

   switch (label_type) {
   case B_BACULA_LABEL:
      return true;
   case B_ANSI_LABEL:
   case B_IBM_LABEL:
      ser_declare;
      Dmsg1(100, "Write ANSI label type=%d\n", label_type);
      len = strlen(VolName);
      if (len > 6) {
         Jmsg1(jcr, M_FATAL, 0, _("ANSI Volume label name \"%s\" longer than 6 chars.\n"),
            VolName);
         return false;
      }
      /* ANSI labels have 6 characters, and are padded with spaces
       * 'vol1\0' => 'vol1   \0'
       */
      strcpy(ansi_volname, VolName);
      for(int i=len; i < 6; i++) {
         ansi_volname[i]=' ';
      }
      ansi_volname[6]='\0';     /* only for debug */

      if (type == ANSI_VOL_LABEL) {
         ser_begin(label, sizeof(label));
         ser_bytes("VOL1", 4);
         ser_bytes(ansi_volname, 6);
         /* Write VOL1 label */
         if (label_type == B_IBM_LABEL) {
            ascii_to_ebcdic(label, label, sizeof(label));
         } else {
            label[79] = '3';                /* ANSI label flag */
         }
         stat = dev->write(label, sizeof(label));
         if (stat != sizeof(label)) {
            berrno be;
            Jmsg3(jcr, M_FATAL, 0,  _("Could not write ANSI VOL1 label. Wanted size=%d got=%d ERR=%s\n"),
               sizeof(label), stat, be.bstrerror());
            return false;
         }
      }

      /* Now construct HDR1 label */
      memset(label, ' ', sizeof(label));
      ser_begin(label, sizeof(label));
      ser_bytes(labels[type], 3);
      ser_bytes("1", 1);
      ser_bytes("BACULA.DATA", 11);            /* Filename field */
      ser_begin(&label[21], sizeof(label)-21); /* fileset field */
      ser_bytes(ansi_volname, 6);              /* write Vol Ser No. */
      ser_begin(&label[27], sizeof(label)-27);
      ser_bytes("00010001000100", 14);  /* File section, File seq no, Generation no */
      now = time(NULL);
      ser_bytes(ansi_date(now, date), 6); /* current date */
      ser_bytes(ansi_date(now - 24 * 3600, date), 6); /* created yesterday */
      ser_bytes(" 000000Bacula              ", 27);
      /* Write HDR1 label */
      if (label_type == B_IBM_LABEL) {
         ascii_to_ebcdic(label, label, sizeof(label));
      }

      /*
       * This could come at the end of a tape, ignore
       *  EOT errors.
       */
      stat = dev->write(label, sizeof(label));
      if (stat != sizeof(label)) {
         berrno be;
         if (stat == -1) {
            dev->clrerror(-1);
            if (dev->dev_errno == 0) {
               dev->dev_errno = ENOSPC; /* out of space */
            }
            if (dev->dev_errno != ENOSPC) {
               Jmsg1(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label. ERR=%s\n"),
               be.bstrerror());
               return false;
            }
         } else {
            Jmsg(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label.\n"));
            return false;
         }
      }

      /* Now construct HDR2 label */
      memset(label, ' ', sizeof(label));
      ser_begin(label, sizeof(label));
      ser_bytes(labels[type], 3);
      ser_bytes("2D3200032000", 12);
      /* Write HDR2 label */
      if (label_type == B_IBM_LABEL) {
         label[4] = 'V';
         ascii_to_ebcdic(label, label, sizeof(label));
      }
      stat = dev->write(label, sizeof(label));
      if (stat != sizeof(label)) {
         berrno be;
         if (stat == -1) {
            dev->clrerror(-1);
            if (dev->dev_errno == 0) {
               dev->dev_errno = ENOSPC; /* out of space */
            }
            if (dev->dev_errno != ENOSPC) {
               Jmsg1(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label. ERR=%s\n"),
               be.bstrerror());
               return false;
            }
            dev->weof(NULL, 1);
            return true;
         } else {
            Jmsg(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label.\n"));
            return false;
         }
      }
      if (!dev->weof(NULL, 1)) {
         Jmsg(jcr, M_FATAL, 0, _("Error writing EOF to tape. ERR=%s"), dev->errmsg);
         return false;
      }
      return true;
   default:
      Jmsg0(jcr, M_ABORT, 0, _("write_ansi_ibm_label called for non-ANSI/IBM type\n"));
      return false; /* should not get here */
   }
}
/*
 * Write an ANSI or IBM 80 character tape label
 *   Type determines whether we are writing HDR, EOF, or EOV labels
 *   Assume we are positioned to write the labels
 *   Returns:  true of OK
 *             false if error
 */
Write an ANSI or IBM 80 character tape label
Type determines whether we are writing HDR, EOF, or EOV labels
Assume we are positioned to write the labels
Returns:  true of OK
false if error
Write an ANSI or IBM 80 character tape label Type determines whether we are writing HDR EOF or EOV labels Assume we are positioned to write the labels Returns : true of OK false if error
bool write_ansi_ibm_labels(DCR *dcr, int type, const char *VolName)
{
   DEVICE *dev = dcr->dev;
   JCR *jcr = dcr->jcr;
   char ansi_volname[7];              
   char label[80];                    
   char date[20];                     
   time_t now;
   int len, stat, label_type;
   if (dcr->device->label_type != B_BACULA_LABEL) {
      label_type = dcr->device->label_type;   
   } else {
      label_type = dcr->VolCatInfo.LabelType; 
   }
   switch (label_type) {
   case B_BACULA_LABEL:
      return true;
   case B_ANSI_LABEL:
   case B_IBM_LABEL:
      ser_declare;
      Dmsg1(100, "Write ANSI label type=%d\n", label_type);
      len = strlen(VolName);
      if (len > 6) {
         Jmsg1(jcr, M_FATAL, 0, _("ANSI Volume label name \"%s\" longer than 6 chars.\n"),
            VolName);
         return false;
      }
      strcpy(ansi_volname, VolName);
      for(int i=len; i < 6; i++) {
         ansi_volname[i]=' ';
      }
      ansi_volname[6]='\0';     
      if (type == ANSI_VOL_LABEL) {
         ser_begin(label, sizeof(label));
         ser_bytes("VOL1", 4);
         ser_bytes(ansi_volname, 6);
         if (label_type == B_IBM_LABEL) {
            ascii_to_ebcdic(label, label, sizeof(label));
         } else {
            label[79] = '3';                
         }
         stat = dev->write(label, sizeof(label));
         if (stat != sizeof(label)) {
            berrno be;
            Jmsg3(jcr, M_FATAL, 0,  _("Could not write ANSI VOL1 label. Wanted size=%d got=%d ERR=%s\n"),
               sizeof(label), stat, be.bstrerror());
            return false;
         }
      }
      memset(label, ' ', sizeof(label));
      ser_begin(label, sizeof(label));
      ser_bytes(labels[type], 3);
      ser_bytes("1", 1);
      ser_bytes("BACULA.DATA", 11);            
      ser_begin(&label[21], sizeof(label)-21); 
      ser_bytes(ansi_volname, 6);              
      ser_begin(&label[27], sizeof(label)-27);
      ser_bytes("00010001000100", 14);  
      now = time(NULL);
      ser_bytes(ansi_date(now, date), 6); 
      ser_bytes(ansi_date(now - 24 * 3600, date), 6); 
      ser_bytes(" 000000Bacula              ", 27);
      if (label_type == B_IBM_LABEL) {
         ascii_to_ebcdic(label, label, sizeof(label));
      }
      stat = dev->write(label, sizeof(label));
      if (stat != sizeof(label)) {
         berrno be;
         if (stat == -1) {
            dev->clrerror(-1);
            if (dev->dev_errno == 0) {
               dev->dev_errno = ENOSPC; 
            }
            if (dev->dev_errno != ENOSPC) {
               Jmsg1(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label. ERR=%s\n"),
               be.bstrerror());
               return false;
            }
         } else {
            Jmsg(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label.\n"));
            return false;
         }
      }
      memset(label, ' ', sizeof(label));
      ser_begin(label, sizeof(label));
      ser_bytes(labels[type], 3);
      ser_bytes("2D3200032000", 12);
      if (label_type == B_IBM_LABEL) {
         label[4] = 'V';
         ascii_to_ebcdic(label, label, sizeof(label));
      }
      stat = dev->write(label, sizeof(label));
      if (stat != sizeof(label)) {
         berrno be;
         if (stat == -1) {
            dev->clrerror(-1);
            if (dev->dev_errno == 0) {
               dev->dev_errno = ENOSPC; 
            }
            if (dev->dev_errno != ENOSPC) {
               Jmsg1(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label. ERR=%s\n"),
               be.bstrerror());
               return false;
            }
            dev->weof(NULL, 1);
            return true;
         } else {
            Jmsg(jcr, M_FATAL, 0, _("Could not write ANSI HDR1 label.\n"));
            return false;
         }
      }
      if (!dev->weof(NULL, 1)) {
         Jmsg(jcr, M_FATAL, 0, _("Error writing EOF to tape. ERR=%s"), dev->errmsg);
         return false;
      }
      return true;
   default:
      Jmsg0(jcr, M_ABORT, 0, _("write_ansi_ibm_label called for non-ANSI/IBM type\n"));
      return false; 
   }
}
bool write_ansi_ibm_labels ( DCR * dcr , int type , const char * VolName ) { DEVICE * dev = dcr -> dev ; JCR * jcr = dcr -> jcr ; char ansi_volname [ 7 ] ; char label [ 80 ] ; char date [ 20 ] ; time_t now ; int len , stat , label_type ; if ( dcr -> device -> label_type != B_BACULA_LABEL ) { label_type = dcr -> device -> label_type ; } else { label_type = dcr -> VolCatInfo . LabelType ; } switch ( label_type ) { case B_BACULA_LABEL : return true ; case B_ANSI_LABEL : case B_IBM_LABEL : ser_declare ; Dmsg1 ( 100 , " \n " , label_type ) ; len = strlen ( VolName ) ; if ( len > 6 ) { Jmsg1 ( jcr , M_FATAL , 0 , _ ( " \" \" \n " ) , VolName ) ; return false ; } strcpy ( ansi_volname , VolName ) ; for ( int i = len ; i < 6 ; i ++ ) { ansi_volname [ i ] = ' ' ; } ansi_volname [ 6 ] = ' \0 ' ; if ( type == ANSI_VOL_LABEL ) { ser_begin ( label , sizeof ( label ) ) ; ser_bytes ( " " , 4 ) ; ser_bytes ( ansi_volname , 6 ) ; if ( label_type == B_IBM_LABEL ) { ascii_to_ebcdic ( label , label , sizeof ( label ) ) ; } else { label [ 79 ] = ' ' ; } stat = dev -> write ( label , sizeof ( label ) ) ; if ( stat != sizeof ( label ) ) { berrno be ; Jmsg3 ( jcr , M_FATAL , 0 , _ ( " \n " ) , sizeof ( label ) , stat , be . bstrerror ( ) ) ; return false ; } } memset ( label , ' ' , sizeof ( label ) ) ; ser_begin ( label , sizeof ( label ) ) ; ser_bytes ( labels [ type ] , 3 ) ; ser_bytes ( " " , 1 ) ; ser_bytes ( " " , 11 ) ; ser_begin ( & label [ 21 ] , sizeof ( label ) - 21 ) ; ser_bytes ( ansi_volname , 6 ) ; ser_begin ( & label [ 27 ] , sizeof ( label ) - 27 ) ; ser_bytes ( " " , 14 ) ; now = time ( NULL ) ; ser_bytes ( ansi_date ( now , date ) , 6 ) ; ser_bytes ( ansi_date ( now - 24 * 3600 , date ) , 6 ) ; ser_bytes ( " " , 27 ) ; if ( label_type == B_IBM_LABEL ) { ascii_to_ebcdic ( label , label , sizeof ( label ) ) ; } stat = dev -> write ( label , sizeof ( label ) ) ; if ( stat != sizeof ( label ) ) { berrno be ; if ( stat == -1 ) { dev -> clrerror ( -1 ) ; if ( dev -> dev_errno == 0 ) { dev -> dev_errno = ENOSPC ; } if ( dev -> dev_errno != ENOSPC ) { Jmsg1 ( jcr , M_FATAL , 0 , _ ( " \n " ) , be . bstrerror ( ) ) ; return false ; } } else { Jmsg ( jcr , M_FATAL , 0 , _ ( " \n " ) ) ; return false ; } } memset ( label , ' ' , sizeof ( label ) ) ; ser_begin ( label , sizeof ( label ) ) ; ser_bytes ( labels [ type ] , 3 ) ; ser_bytes ( " " , 12 ) ; if ( label_type == B_IBM_LABEL ) { label [ 4 ] = ' ' ; ascii_to_ebcdic ( label , label , sizeof ( label ) ) ; } stat = dev -> write ( label , sizeof ( label ) ) ; if ( stat != sizeof ( label ) ) { berrno be ; if ( stat == -1 ) { dev -> clrerror ( -1 ) ; if ( dev -> dev_errno == 0 ) { dev -> dev_errno = ENOSPC ; } if ( dev -> dev_errno != ENOSPC ) { Jmsg1 ( jcr , M_FATAL , 0 , _ ( " \n " ) , be . bstrerror ( ) ) ; return false ; } dev -> weof ( NULL , 1 ) ; return true ; } else { Jmsg ( jcr , M_FATAL , 0 , _ ( " \n " ) ) ; return false ; } } if ( ! dev -> weof ( NULL , 1 ) ) { Jmsg ( jcr , M_FATAL , 0 , _ ( " " ) , dev -> errmsg ) ; return false ; } return true ; default : Jmsg0 ( jcr , M_ABORT , 0 , _ ( " \n " ) ) ; return false ; } }
Write an ANSI or IBM 80 character tape label
   Type determines whether we are writing HDR, EOF, or EOV labels
   Assume we are positioned to write the labels
   Returns:  true of OK
             false if error
Write an ANSI or IBM 80 character tape label Type determines whether we are writing HDR EOF or EOV labels Assume we are positioned to write the labels Returns : true of OK false if error
/* 6 char + \0 */ /* tape label */ /* ansi date buffer */ /*
    * If the Device requires a specific label type use it,
    * otherwise, use the type requested by the Director
    */ /* force label type */ /* accept Dir type */ /* ANSI labels have 6 characters, and are padded with spaces
       * 'vol1\0' => 'vol1   \0'
       */ /* only for debug */ /* Write VOL1 label */ /* ANSI label flag */ /* Now construct HDR1 label */ /* Filename field */ /* fileset field */ /* write Vol Ser No. */ /* File section, File seq no, Generation no */ /* current date */ /* created yesterday */ /* Write HDR1 label */ /*
       * This could come at the end of a tape, ignore
       *  EOT errors.
       */ /* out of space */ /* Now construct HDR2 label */ /* Write HDR2 label */ /* out of space */ /* should not get here */