src/stored/init_dev.c
DEVICE *init_dev(JCR *jcr, DEVRES *device, bool adata)
{
   struct stat statp;
   DEVICE *dev = NULL;
   uint32_t n_drivers;

   generate_global_plugin_event(bsdGlobalEventDeviceInit, device);
   Dmsg1(150, "init_dev dev_type=%d\n", device->dev_type);
   /* If no device type specified, try to guess */
   if (!device->dev_type) {
      /* Check that device is available */
      if (stat(device->device_name, &statp) < 0) {
         berrno be;
         Jmsg3(jcr, M_ERROR, 0, _("[SE0001] Unable to stat device %s at %s: ERR=%s\n"),
            device->hdr.name, device->device_name, be.bstrerror());
         return NULL;
      }
      if (S_ISDIR(statp.st_mode)) {
         device->dev_type = B_FILE_DEV;
      } else if (S_ISCHR(statp.st_mode)) {
         device->dev_type = B_TAPE_DEV;
      } else if (S_ISFIFO(statp.st_mode)) {
         device->dev_type = B_FIFO_DEV;
#ifdef USE_VTAPE
      /* must set DeviceType = Vtape
       * in normal mode, autodetection is disabled
       */
      } else if (S_ISREG(statp.st_mode)) {
         device->dev_type = B_VTAPE_DEV;
#endif
      } else if (!(device->cap_bits & CAP_REQMOUNT)) {
         Jmsg2(jcr, M_ERROR, 0, _("[SE0002] %s is an unknown device type. Must be tape or directory."
               " st_mode=%x\n"),
            device->device_name, statp.st_mode);
         return NULL;
      }
      if (strcmp(device->device_name, "/dev/null") == 0) {
         device->dev_type = B_NULL_DEV;
      }
   }

   /* Count drivers */
   for (n_drivers=0; driver_tab[n_drivers].name; n_drivers++) { };
   Dmsg1(100, "Num drivers=%d\n", n_drivers);

   /* If invalid dev_type get out */
   if (device->dev_type < 0 || device->dev_type > n_drivers) {
      Jmsg2(jcr, M_FATAL, 0, _("[SF0001] Invalid device type=%d name=\"%s\"\n"),
         device->dev_type, device->hdr.name);
      return NULL;
   }
   Dmsg5(100, "loadable=%d type=%d loaded=%d name=%s handle=%p\n",
      !driver_tab[device->dev_type-1].builtin,
      device->dev_type,
      driver_tab[device->dev_type-1].loaded,
      driver_tab[device->dev_type-1].name,
      driver_tab[device->dev_type-1].handle);
   if (driver_tab[device->dev_type-1].builtin) {
      /* Built-in driver */
      switch (device->dev_type) {
#ifdef HAVE_WIN32
      case B_TAPE_DEV:
//       dev = New(win_tape_dev);
         break;
      case B_ADATA_DEV:
      case B_ALIGNED_DEV:
      case B_FILE_DEV:
         dev = New(win_file_dev);
         dev->capabilities |= CAP_LSEEK;
         break;
      case B_NULL_DEV:
         dev = New(win_file_dev);
         break;
#else
      case B_VTAPE_DEV:
         dev = New(vtape);
         break;
      case B_TAPE_DEV:
         dev = New(tape_dev);
         break;
      case B_FILE_DEV:
         dev = New(file_dev);
         dev->capabilities |= CAP_LSEEK;
         break;
      case B_NULL_DEV:
         dev = New(null_dev);
         break;
      case B_FIFO_DEV:
         dev = New(fifo_dev);
         break;
#endif
      default:
         Jmsg2(jcr, M_FATAL, 0, _("[SF0002] Unknown device type=%d device=\"%s\"\n"),
            device->dev_type, device->hdr.name);
         return NULL;
      }
   } else {
      /* Loadable driver */
      dev = load_driver(jcr, device);
   }
   if (!dev) {
      return NULL;
   }

   dev->adata = adata;

   /* Keep the device ID in the DEVICE struct to identify the hardware */
   if (dev->is_file() && stat(dev->archive_name(), &statp) == 0) {
      dev->devno = statp.st_dev;
   }

   dev->device_generic_init(jcr, device);

   /* Do device specific initialization */
   dev->device_specific_init(jcr, device);

   /* ***FIXME*** move to fifo driver */
   if (dev->is_fifo()) {
      dev->capabilities |= CAP_STREAM; /* set stream device */
   }

   return dev;
}
/*
 * Allocate and initialize the DEVICE structure
 * Note, if dev is non-NULL, it is already allocated,
 * thus we neither allocate it nor free it. This allows
 * the caller to put the packet in shared memory.
 *
 *  Note, for a tape, the device->device_name is the device name
 *     (e.g. /dev/nst0), and for a file, the device name
 *     is the directory in which the file will be placed.
 *
 */
Allocate and initialize the DEVICE structure
Note, if dev is non-NULL, it is already allocated,
thus we neither allocate it nor free it. This allows
the caller to put the packet in shared memory.

Note, for a tape, the device->device_name is the device name
, and for a file, the device name
is the directory in which the file will be placed.
Allocate and initialize the DEVICE structure Note if dev is non - NULL it is already allocated thus we neither allocate it nor free it . This allows the caller to put the packet in shared memory . Note for a tape the device - > device_name is the device name and for a file the device name is the directory in which the file will be placed .
DEVICE *init_dev(JCR *jcr, DEVRES *device, bool adata)
{
   struct stat statp;
   DEVICE *dev = NULL;
   uint32_t n_drivers;
   generate_global_plugin_event(bsdGlobalEventDeviceInit, device);
   Dmsg1(150, "init_dev dev_type=%d\n", device->dev_type);
   if (!device->dev_type) {
      if (stat(device->device_name, &statp) < 0) {
         berrno be;
         Jmsg3(jcr, M_ERROR, 0, _("[SE0001] Unable to stat device %s at %s: ERR=%s\n"),
            device->hdr.name, device->device_name, be.bstrerror());
         return NULL;
      }
      if (S_ISDIR(statp.st_mode)) {
         device->dev_type = B_FILE_DEV;
      } else if (S_ISCHR(statp.st_mode)) {
         device->dev_type = B_TAPE_DEV;
      } else if (S_ISFIFO(statp.st_mode)) {
         device->dev_type = B_FIFO_DEV;
#ifdef USE_VTAPE
      } else if (S_ISREG(statp.st_mode)) {
         device->dev_type = B_VTAPE_DEV;
#endif
      } else if (!(device->cap_bits & CAP_REQMOUNT)) {
         Jmsg2(jcr, M_ERROR, 0, _("[SE0002] %s is an unknown device type. Must be tape or directory."
               " st_mode=%x\n"),
            device->device_name, statp.st_mode);
         return NULL;
      }
      if (strcmp(device->device_name, "/dev/null") == 0) {
         device->dev_type = B_NULL_DEV;
      }
   }
   for (n_drivers=0; driver_tab[n_drivers].name; n_drivers++) { };
   Dmsg1(100, "Num drivers=%d\n", n_drivers);
   if (device->dev_type < 0 || device->dev_type > n_drivers) {
      Jmsg2(jcr, M_FATAL, 0, _("[SF0001] Invalid device type=%d name=\"%s\"\n"),
         device->dev_type, device->hdr.name);
      return NULL;
   }
   Dmsg5(100, "loadable=%d type=%d loaded=%d name=%s handle=%p\n",
      !driver_tab[device->dev_type-1].builtin,
      device->dev_type,
      driver_tab[device->dev_type-1].loaded,
      driver_tab[device->dev_type-1].name,
      driver_tab[device->dev_type-1].handle);
   if (driver_tab[device->dev_type-1].builtin) {
      switch (device->dev_type) {
#ifdef HAVE_WIN32
      case B_TAPE_DEV:
         break;
      case B_ADATA_DEV:
      case B_ALIGNED_DEV:
      case B_FILE_DEV:
         dev = New(win_file_dev);
         dev->capabilities |= CAP_LSEEK;
         break;
      case B_NULL_DEV:
         dev = New(win_file_dev);
         break;
#else
      case B_VTAPE_DEV:
         dev = New(vtape);
         break;
      case B_TAPE_DEV:
         dev = New(tape_dev);
         break;
      case B_FILE_DEV:
         dev = New(file_dev);
         dev->capabilities |= CAP_LSEEK;
         break;
      case B_NULL_DEV:
         dev = New(null_dev);
         break;
      case B_FIFO_DEV:
         dev = New(fifo_dev);
         break;
#endif
      default:
         Jmsg2(jcr, M_FATAL, 0, _("[SF0002] Unknown device type=%d device=\"%s\"\n"),
            device->dev_type, device->hdr.name);
         return NULL;
      }
   } else {
      dev = load_driver(jcr, device);
   }
   if (!dev) {
      return NULL;
   }
   dev->adata = adata;
   if (dev->is_file() && stat(dev->archive_name(), &statp) == 0) {
      dev->devno = statp.st_dev;
   }
   dev->device_generic_init(jcr, device);
   dev->device_specific_init(jcr, device);
   if (dev->is_fifo()) {
      dev->capabilities |= CAP_STREAM; 
   }
   return dev;
}
DEVICE * init_dev ( JCR * jcr , DEVRES * device , bool adata ) { struct stat statp ; DEVICE * dev = NULL ; uint32_t n_drivers ; generate_global_plugin_event ( bsdGlobalEventDeviceInit , device ) ; Dmsg1 ( 150 , " \n " , device -> dev_type ) ; if ( ! device -> dev_type ) { if ( stat ( device -> device_name , & statp ) < 0 ) { berrno be ; Jmsg3 ( jcr , M_ERROR , 0 , _ ( " \n " ) , device -> hdr . name , device -> device_name , be . bstrerror ( ) ) ; return NULL ; } if ( S_ISDIR ( statp . st_mode ) ) { device -> dev_type = B_FILE_DEV ; } else if ( S_ISCHR ( statp . st_mode ) ) { device -> dev_type = B_TAPE_DEV ; } else if ( S_ISFIFO ( statp . st_mode ) ) { device -> dev_type = B_FIFO_DEV ; #ifdef USE_VTAPE } else if ( S_ISREG ( statp . st_mode ) ) { device -> dev_type = B_VTAPE_DEV ; #endif 
      } else if (!(device->cap_bits & CAP_REQMOUNT)) { Jmsg2 ( jcr , M_ERROR , 0 , _ ( " " " \n " ) , device -> device_name , statp . st_mode ) ; return NULL ; } if ( strcmp ( device -> device_name , " " ) == 0 ) { device -> dev_type = B_NULL_DEV ; } } for ( n_drivers = 0 ; driver_tab [ n_drivers ] . name ; n_drivers ++ ) { } ; Dmsg1 ( 100 , " \n " , n_drivers ) ; if ( device -> dev_type < 0 || device -> dev_type > n_drivers ) { Jmsg2 ( jcr , M_FATAL , 0 , _ ( " \" \" \n " ) , device -> dev_type , device -> hdr . name ) ; return NULL ; } Dmsg5 ( 100 , " \n " , ! driver_tab [ device -> dev_type - 1 ] . builtin , device -> dev_type , driver_tab [ device -> dev_type - 1 ] . loaded , driver_tab [ device -> dev_type - 1 ] . name , driver_tab [ device -> dev_type - 1 ] . handle ) ; if ( driver_tab [ device -> dev_type - 1 ] . builtin ) { switch ( device -> dev_type ) { #ifdef HAVE_WIN32 case B_TAPE_DEV : break ; case B_ADATA_DEV : case B_ALIGNED_DEV : case B_FILE_DEV : dev = New ( win_file_dev ) ; dev -> capabilities |= CAP_LSEEK ; break ; case B_NULL_DEV : dev = New ( win_file_dev ) ; break ; #else case B_VTAPE_DEV : dev = New ( vtape ) ; break ; case B_TAPE_DEV : dev = New ( tape_dev ) ; break ; case B_FILE_DEV : dev = New ( file_dev ) ; dev -> capabilities |= CAP_LSEEK ; break ; case B_NULL_DEV : dev = New ( null_dev ) ; break ; case B_FIFO_DEV : dev = New ( fifo_dev ) ; break ; #endif default : Jmsg2 ( jcr , M_FATAL , 0 , _ ( " \" \" \n " ) , device -> dev_type , device -> hdr . name ) ; return NULL ; } } else { dev = load_driver ( jcr , device ) ; } if ( ! dev ) { return NULL ; } dev -> adata = adata ; if ( dev -> is_file ( ) && stat ( dev -> archive_name ( ) , & statp ) == 0 ) { dev -> devno = statp . st_dev ; } dev -> device_generic_init ( jcr , device ) ; dev -> device_specific_init ( jcr , device ) ; if ( dev -> is_fifo ( ) ) { dev -> capabilities |= CAP_STREAM ; } return dev ; }
Allocate and initialize the DEVICE structure
 Note, if dev is non-NULL, it is already allocated,
 thus we neither allocate it nor free it.
Allocate and initialize the DEVICE structure Note if dev is non - NULL it is already allocated thus we neither allocate it nor free it .
/* If no device type specified, try to guess */ /* Check that device is available */ /* must set DeviceType = Vtape
       * in normal mode, autodetection is disabled
       */ /* Count drivers */ /* If invalid dev_type get out */ /* Built-in driver */ //       dev = New(win_tape_dev); /* Loadable driver */ /* Keep the device ID in the DEVICE struct to identify the hardware */ /* Do device specific initialization */ /* ***FIXME*** move to fifo driver */ /* set stream device */