How to check if a Tape is write protected

How to check if a Tape is write protected

Post by Basti » Thu, 30 Aug 2001 16:39:27



Hello,

how can I check if a tape (/dev/streamer) is write protected in "C"?

Thanks for your help.

Greetinx

Bastain Trompetter

 
 
 

How to check if a Tape is write protected

Post by Maurizio Loret » Thu, 30 Aug 2001 16:47:35



> how can I check if a tape (/dev/streamer) is write protected in "C"?

You may stat the file associated to the device, and check the st_mode
field of the returned struct stat - man stat(2) for more.  There are
predefined constants like S_IWUSR, S_IWGRP and S_IWOTH that you may
use.

--
Maurizio Loreti                         http://www.pd.infn.it/~loreti/mlo.html


 
 
 

How to check if a Tape is write protected

Post by Igmar Palsenber » Sat, 01 Sep 2001 04:56:17




> > how can I check if a tape (/dev/streamer) is write protected in "C"?

> You may stat the file associated to the device, and check the st_mode
> field of the returned struct stat - man stat(2) for more.  There are
> predefined constants like S_IWUSR, S_IWGRP and S_IWOTH that you may
> use.

Isn't this just basic file permissions ?

/dev/tape can be :

crw-rw-rw-    1 root root  xx, y

which allows all access, but the device itself can be write protected.
In that case, the stat() doesn't work I think. Opening the device for
read-write might fail, if it does, that is the answer.

        Igmar

 
 
 

How to check if a Tape is write protected

Post by Martin Jos » Thu, 06 Sep 2001 23:23:53





> > > how can I check if a tape (/dev/streamer) is write protected in "C"?
> which allows all access, but the device itself can be write protected.
> In that case, the stat() doesn't work I think. Opening the device for
> read-write might fail, if it does, that is the answer.

No, this will also fail, e.g. if no tape is present.
Maybe looking at errno could tell this apart.

I don't have a solution for tape-drives. But I have one for SCSI-disks
on HPUX 10.20
(In my case these have been MO-disks, which can be removed or
write-protected)

This uses ioctl(), but a quick check of the man-pages didn't who a
solution for tapes....

#include <stdio.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <sys/diskio.h>
#include <sys/scsi.h>

/* #define VERBOSE 1 */

void wp(int dfd, char *dev)
{
  int ret;
  struct capacity cap;
  disk_describe_type descr_type;

  if ((ret = ioctl (dfd, SIOC_CAPACITY, &cap)) == -1)
  {
    printf("ioctl %s SIOC_CAPACITY failed\n", dev);
    perror("");
    exit(1);
  }

#ifdef VERBOSE  
  printf ("\nSuccessful ioctl SIOC_CAPACITY \n");
  printf ("lba:     %d  \n", cap.lba);
  printf ("blksize: %d  \n", cap.blksz);
#endif
  if (cap.lba == 0)
  {
    /* Capacity == 0 means no disk inserted */  
    exit(55);
  }

  if ((ret = ioctl (dfd, DIOC_DESCRIBE, &descr_type)) == -1)
  {
    printf("ioctl %s DIOC_DESCRIBE failed\n", dev);
    perror("");
    exit(1);
  }

#ifdef VERBOSE  
  printf ("\nSuccessful ioctl DIOC_DESCRIBE \n");
  printf ("  model number: %s\n", descr_type.model_num);
  printf ("  interface:    %d  <20=scsi>\n", descr_type.intf_type);
  printf ("  type:    %d  \n", descr_type.dev_type);
  printf ("  flags:   %d  \n", descr_type.flags);

  puts("WP-Switch:");
  if ((descr_type.flags & WRITE_PROTECT_FLAG) != 0)
  {
    puts("protected\n");
    exit(77);
  }
  else
  {
    puts("unprotected\n");
    exit(66);
  }
#else
  if ((descr_type.flags & WRITE_PROTECT_FLAG) != 0)
  {
    exit(77);
  }
  else
  {
    exit(66);
  }
#endif

Quote:}

main (int argc, char *argv[])
{
  int
    ret,
    fd;

  if (argc != 2)
  {
    printf("Usage: %s <disk3_device, raw> \n", argv[0]);
    printf("E.g. %s /dev/__r__dsk/c0t0d0\n", argv[0]);
    exit(1);
  }

  if ((fd = open (argv[1], O_RDWR)) < 0)
  {
    puts("open failed");
    perror("");
    exit (1);
  }
  wp (fd, argv[1]);

Quote:}

Maybe you can find something similar for tapes....

Martin

 
 
 

How to check if a Tape is write protected

Post by Donald McLachl » Fri, 07 Sep 2001 00:05:10


/dev/streamer is not familiar to me.  On solaris I'd check man mtio, or man st
Maybe the See Also section of "man mt" or "man scsi" on your system will
point you in the right direction for finding the tape specific ioctls.

Don

--

Communications Research Centre / RNS    Tel     (613) 998-2845
3701 Carling Ave.,                      Fax     (613) 998-9648
Ottawa, Ontario
K2H 8S2
Canada

 
 
 

How to check if a Tape is write protected

Post by Joe Seig » Fri, 07 Sep 2001 19:29:21



> Hello,

> how can I check if a tape (/dev/streamer) is write protected in "C"?

> Thanks for your help.

> Greetinx

> Bastain Trompetter

A scsi request sense will tell you if a tape is write protected.  This
involves doing your own scsi programming unless you can find a utility
and drivers that will do it for you.

Joe Seigh

 
 
 

1. Checking Tape for Write Protect

Does anyone know of a way to check if the write protect tab is
set on a tape? (Oh yea, this is under Solaris)

I have a shell script that starts another process that writes to a tape,
but if the tape is write protected, then the whole process just hangs.

The only way that I've been able to figure it out is by first trying to
write something small to the tape, and check for error messages.

Any suggestions greatly appreciated.

Thanks
Paul Suchomel

2. linux/if.h and sys/socket.h conflict

3. sh script : How to check if tape is write protected?

4. FastTrack Admin: No route to host

5. Checking Tape for Write Protect

6. ssh cant leave homedir

7. Checking tape write-protect status

8. Visualize gprof?

9. Check if tape is write protected

10. Checking for write protect on 8mm tapes.

11. How to detect whether a tape is write-protected

12. 4mm tape drive is falsely write protected!

13. Tape Write protect Status