Find the answer to your Linux question:
Results 1 to 9 of 9
I've been playing around with C & C++ for a little while now and am getting fairly comfy with 'em so far. Anyway, I want to step it up a ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2004
    Posts
    4

    App to access /dev/st0


    I've been playing around with C & C++ for a little while now and am getting fairly comfy with 'em so far. Anyway, I want to step it up a notch.
    I'ld like to write an app for linux which will be able to write/read directly to a tape drive (/dev/[n]st[0,1]). I dont want to simply call tar or mt. Can anyone point me in the right direction to read() and write() to a /dev/ scsi device?
    Thanks

  2. #2
    Linux Guru
    Join Date
    Apr 2003
    Location
    London, UK
    Posts
    3,284
    Once the drive is mounted, should it not be the same as normal reading & writing files on your HD, the only difference is that your files are stored under /dev/<whatever> ?

    I may be wrong as i dont have a tape drive, but would have thought once mounted it would act like any hard drive..

    eg, a simple write:

    Code:
    FILE *fp;
    char *buf="some text to write";
    
    fp = fopen&#40;"/dev/nst0/filename", "r+"&#41;;
    
    if&#40; fp == NULL &#41;
    &#123;
         puts&#40;strerror&#40;errno&#41;&#41;;
         return 0;
    &#125;
    
    while&#40; fwrite&#40;buf, sizeof&#40;char&#41;, strlen&#40;buf&#41;, fp&#41; != EOF &#41;
         continue;
    
    fclose&#40;fp&#41;;

  3. #3
    Just Joined!
    Join Date
    Apr 2004
    Posts
    4
    This type of device never actually gets mounted to anything. Its a raw device.
    The mt command allows me to physically access (check status, forward, rewind, etc) the drive and tar lets me read/write to it no prob. I just want to get me hands on it without these commands.

  4. #4
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    even if you are writing to the raw device, it is still treated as a file.. so you should be able to assign an fd to it and write to it no problem...

    Code:
    FILE *fp=NULL;
    fopen&#40;"/dev/<device>",fp,"r+"&#41;;
    
    <other code here>
    and my syntax may be wrong on the fopen() call.. it's late, and i'm too lazy too look it up... but that's the gist of it.
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  5. #5
    Linux Engineer Giro's Avatar
    Join Date
    Jul 2003
    Location
    England
    Posts
    1,219
    Everything in Linux is just a file.

  6. #6
    Just Joined!
    Join Date
    Apr 2004
    Posts
    4
    I've got the "linux is a file" thing. I can write or read across various drives wiht no problem. Its just this "/dev/st0" that I can not get to accept a read/write.
    I've tried to write to the device "/dev/st0" and by placing a file name at the end "/dev/st0/<filename>". Either way, no luck.

    Thanks anyways.

  7. #7
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    i know this may seem a dumb question, but, do you have permission to write to the device?
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  8. #8
    Just Joined!
    Join Date
    Apr 2004
    Posts
    4
    Update:
    Evidently when I try to access it via "nst0", I'm not properly rewinding (bsf) or fastforwarding (fsf). However, "st0" will write and read now but the filesize is larger once I read it off of the tape drive.

    Can anyone see anything wrong with this block...
    Code:
    in = open&#40;"/dev/st0", O_RDONLY&#41;;
    out = open&#40;"test/test_img.img", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR&#41;;
    
    while&#40;&#40;nread = read&#40;in,block,sizeof&#40;block&#41;&#41;&#41; > 0&#41;
          write&#40;out,block,nread&#41;;

  9. #9
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    Code:
    while&#40;&#40;nread = read&#40;in,block,sizeof&#40;block&#41;&#41;&#41; > 0&#41;
          write&#40;out,block,nread&#41;;
    this statement is an infinite loop... the reason is that whenever you assign a value, it always resolves to true (1)... so, this:
    Code:
    &#40;nread=read&#40;in,block,sizeof&#40;block&#41;&#41;&#41; > 0&#41;
    should be this:
    Code:
    &#40;nread==read&#40;in,block,sizeof&#40;block&#41;&#41;&#41; > 0&#41;
    that will check to see if the var nread is equal to the return of read().. if so, it will return 1 and the loop will continue.. if not, it will return 0 and the loop will not be executed...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •