Fermi Tape Tools (ftt) Library User's Guide
Appendix D

Valery Sergeev
Computation and Communications Fabric Department

Abstract

SWIG (Simplified Wrapper and Interface Generator) is a freely available tool that integrates Perl, Python, Tcl, and other scripting languages with programs written in C, C++, and Objective-C. While SWIG is similar to other Perl extension building tools such as xsubpp and h2xs, SWIG has a number of unique features that help simplify the task of creating Perl extension modules. To interface Perl with FTT code written in C , it is necessary to write wrappers that serve as a glue layer between the Perl interpreter and the underlying C code. These wrappers are responsible for converting data between Perl and C, reporting errors, and other tasks.This paper describes FTT functions, accessible from Perl scripts.

FTT functions were selected for Perl extension building with SWIG:

  • ftt_descriptor _open (char*, int)
  • int _close (ftt_descriptor)
  • int _rewind (ftt_descriptor)
  • int _get_position (ftt_descriptor)
  • int _skip_rec (ftt_descriptor, int)
  • int _skip_fm (ftt_descriptor, int)
  • int _skip_to_double_fm (ftt_descriptor)
  • int _retension (ftt_descriptor)
  • int _erase (ftt_descriptor)
  • int _unload (ftt_descriptor)
  • int _status (ftt_descriptor)
  • int _get_stats (ftt_descriptor)
  • int _extract_stats (char*)
  • int _writefm (ftt_descriptor)
  • int _write2fm (ftt_descriptor)
  • int _write (ftt_descriptor, cons char*, int)
  • int _read (ftt_stat_buf, cons char*, int)
  • int _get_basename (ftt_descriptor)
  • int _set_mode (ftt_descriptor, int, int, int)
  • int _get_mode (ftt_descriptor)
  • int _describe_dev (ftt_descriptor, char*, char*)
  • By building a Perl interface to FTT, our goal is to write similar code in Perl. Thus, the functionality of the FTT library must to be exposed to the Perl interpreter. To do this, a SWIG interface file ftt.i and few C files were written (e.g. ftt_s_open.c, ftt_s_position.c, ftt_s_rdwr.c etc). Also, some Global variables were created, that allow to transfer variable values from FTT to Perl. This paper contains examples of some functions that requared some explanations. All Other functions that not mentioned in this examples executes in the same way as in FTT.

    ftt_descriptor _open(char*), int _close(ftt_descriptor)

    All tape activities in FTT and Perl start with call to _open(char*, int) and issue an _close($fd) before exiting.

    Example:

    #!/usr/bin/perl
    use ftt;
    $fd = ftt::_open ("/dev/rmt/my_tape", $FTT_FLAG); /*FTT_FLAG = RDONLY/RDWR */
    /*
    do some operations
    */
    ftt::_close($fd);
    

    int _get_stats(ftt_descriptor), int _extract_stats(char *)

    _get_stats will extract current statistics for tape usage.Note: _get_stats always put filemarks on your tape if you have been writing. _extract_stats extract a string describing a named piece of tape data.

    Example:

    #!/usr/bin/perl
    use ftt;
    $fd = ftt::_open("/dev/rmt/my_tape", FTT_FLAG);
    ftt::_get_stats($fd);
    ftt::_extract_stats("FTT_REMAIN_TAPE");
    print "Remain tape is ",$ftt::Stat_value,"\n";
    ftt::_close($fd);
    

    int _status(ftt_descriptor, int)

    _status indicates the current tape position with bloking up to int seconds waiting for the tape (if it in the drive) to come online or not busy. _status will write filemark if the last operation was _writefm, _write. _get_position returns the current file and block position on the tape.

    Example:

    #!/usr/bin/perl
    use ftt;
    $fd = ftt::_open("/dev/rmt/my_tape", FTT_FLAG);
    $status = ftt::_status($fd, $Time_out);
    ftt::_get_position($fd);
    print "Status of tape is ",$status," File no. ",$ftt::File_no," Block no. ",$ftt::Block_no,"\n";
    ftt::_close($fd);
    

    int _write(ftt_descriptor, char* file_name, int bsize) int _read(ftt_descriptor, char *file_name,int bsize)

    _write and _read calls will write data in and read data from caracter pointer to the data file_name by blocks bsize length.

    Example:

    #!/usr/bin/perl
    use ftt;
    $fd = ftt::_open(/dev/rmt/my_tape", FTT_FLAG);
    $Bsize = 123456;
    $File_name = "my_filename";
    $status = ftt::_write($fd, $File_name, $Bsize);
    print "Status write ",$status,"\n";
    $status = ftt::_read($fd, $File_name, $Bsize);
    print  "Status read ",$status,"\n";
    ftt::_close($fd);
    

    int _get_basename(ftt_descriptor)

    returns the basename for the device, wich can be found after doing _open.

    Example:

    #!/usr/bin/perl
    use ftt;
    $fd = ftt::_open("/dev/rmt/my_tape", FTT_FLAG);
    /*
        doing some operations
    */
    ftt::_get_basename($fd);
    print "Base name of dev is ",$ftt::Stat_value,"\n";
    ftt::_close($fd);
    

    int _set_mode(ftt_descriptor, int density, int comp, int bsize) int _get_mode(ftt_descriptor)

    _set_mode sets a cpecific density number, turns compression on or off, and sets variable blocking (bsize = 0), or a given fixed blocksize. The function returns the name of device it will use for operation at that density. _get_mode returns device basename, and density, compression, and blocksize were last set by _set_mode in assinged integers.

    Example:

    #!/usr/bin/perl
    use ftt;
    $fd = ftt::_open("/dev/rmt/my_tape", FTT_FLAG);
    $Density = ;
    $Compression = ;
    $Blocksize = ;
    ftt::_set_mode($fd, $Density, $Compression, $Blocksize);
    /*
         doing some operations
    */
    ftt::_get_mode($fd);
    print "Device name ",$ftt::Stat_value,"Density ",$ftt::Density,"Compression ",
           $ftt::Comp,"Blocksize ",$ftt::Blocksize,"\n";  
    

    int _describe_dev(ftt_descriptor, char *my_dev, char *file_name)

    prints a description of the device given in my_dev, listing the density, compression options, rewind modes, etc. on file file_name. It retuns the number of bytes written on the file_name.

    Example:

    #!/usr/bin/perl
    use ftt;
    $fd = ::_open("/dev/rmt/my_tape", FTT_FLAG);
    ftt::_describe_dev($fd, "My_device", "My_file");
    /*
         doing some operation
         and print device description
    */
    open (out,"My_file");
    while () {
       print $_;
    }
    close (out);
    ftt::_close($fd);
    

    Other functions.

    This function executes in the same way as in FTT (see FTT Doc's for explanations)

    Running SWIG.

    To build a Perl5 module, run swig as follows:

    swig -perl5 -I/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE $INC ftt.i

    where INC looks like:

    -I/$FTT_DIR/include -I/$FTT_DIR/ftt_lib -I/usr/src/linux/include/scsi

    This procedure will produce 2 files. The first file ftt_wrap.c contains all of the C code needed to build a Perl5 module. The second file ftt.pm contains supporting Perl code needed to properly load the module into Perl. To finish building a module, you will need to compile the ftt_wrap.c and link it with the C programs that call FTT routines.

    gcc -fpic -Dbool=char -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBUGGING\
    -I/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE -I. $INC $LIB -c ftt_wrap.c


    where LIB looks like:

    -L/$FTT_DIR/lib -L/$FTT_DIR/ftt_lib -L/$FTT_DIR/ftt_lib/local/libs

    And last operation:

    ld -G ftt_wrap.o ftt_s_position.o ftt_s_open.o ftt_s_stats.o ftt_s_rdwr.o ftt_s_mode.o -o ftt.so $LIB -lc -lftt -lc -lftt