static char *RCSid="$Id: write_tape.c,v 1.1 2000/01/17 15:51:34 jwm Exp $"; /*********************************************************************** write_tape.c ********************************************************************** Description: Handles the writing of patient data files in AAPM format to a tape. It assumes that the files are named "aapm####" where the #### is a zero padded file number. These files are written to tape in numeric order with 0000 first (as it is the directory file). The files are expected to already be written to disk in the appropriate size buffers. ************************ Revision History: $Log: write_tape.c,v $ * Revision 1.1 2000/01/17 15:51:34 jwm * Remove comments originating in other development trees; Clean up some * compiler gripes found on castor * * Revision 1.0 2000/01/17 15:37:27 jwm * Initial revision **********************************************************************/ /* Include Files */ #include #include #include #include #include #include #include #include #include #include "sitedata.h" #include "exchkeys.h" #include "patexchange.h" /* GLOBAL FILE STREAM POINTER FOR LOG FILE */ FILE *aapm_log_file=NULL; char aapm_log_string[180]; /***************************************************************************** main() **************************************************************************** Functional Description: Handles the writing of fixed block length records to magnetic tape being used for patient data exchange. It writes all records of all files specified where the files are named "aapmXXXX" where XXXX is the number of the file (0000 for the directory file). The command line is as follows: write_tape where: read_tape is the name of the executable is the identifier of the device. On our SGI machines, for instance, for our DAT tape we use /dev/tps0d3nrnsv for the no-rewind, no byte swap, variable record length device (variable as the tape drive will use its own size if you don's specify variable). Your tape device identifier will probably be different. is the number of bytes in a tape block (2048 per protocol, but may be otherwise). is the path to the directory in which the disk files are to be read from. These files should already be in the Patient Data Exchange format. I only claim to know UNIX (System V, in particular). Your mileage may vary with conditions. is the number of the last file to be copied to tape. For instance, if there are 30 files total, the last number BETTER be 29 as the first (the directory) is zero. ***************************************************************************/ void main ( int argc, /* COMMAND LINE ARGUMENT COUNT */ char *argv[] /* COMMAND LINE ARGUMENT LIST */ ) { int block_size; /* TAPE RECORD SIZE IN BYTES */ int block_number; /* NUMBER OF BLOCK BEING PROCESSED */ int num_written; /* NUMBER OF BYTES WRITTEN TO DISK */ int file_number; /* FILE NUMBER BEING PROCESSED */ char *device; /* TAPE DEVICE STRING */ char *path; /* DESTINATION PATH FOR FILES */ char filename[PATH_MAX]; /* OUTPUT DISK FILE NAME */ char *data_block; /* BUFFER FOR DATA TO BE READ INTO */ int filedes; /* FILE DESCRIPTOR FOR TAPE DRIVE */ FILE *fp; /* FILE POINTER FOR OUTPUT FILE */ int num_files; /* # OF FILES TO WRITE */ /* CHECK THE ARGUMENTS PASSED IN */ if ( argc != 5 ) { /* LET USER KNOW THE ACTUAL USAGE */ fprintf( stderr, "\nUsage:" ); fprintf( stderr, "\nwrite_tape \n"); fprintf( stderr, "\n\nThe usage is for a SGI system. You may need a different device" ); fprintf( stderr, "\nFor an SGI system a no-rewind, no-swap, variable block size device is req'd\n\n" ); exit( 1 ); } /* PROCESS THE COMMAND LINE ARGUMENTS */ block_size = atoi( argv[2] ); device = dup_string( argv[1] ); path = dup_string( argv[3] ); num_files = atoi( argv[4] ); /* ALLOCATE THE DATA BLOCK (BUFFER )*/ if ( (data_block = (char *)malloc( block_size )) == NULL ) { fprintf( stderr, "\nError allocating block data memory\n" ); exit( 1 ); } file_number = 0; for ( file_number=0; file_number<=num_files; file_number++ ) { block_number = 0; /* OPEN THE TAPE DEVICE */ if ( (filedes = open( device, O_WRONLY, 0 )) < 0 ) { /* LET USER KNOW HE HAS A PROBLEM */ fprintf( stderr, "\nError opening device: %s\n", device ); exit( 1 ); } /* OPEN THE INPUT DISK FILE */ sprintf( filename, "%s/aapm%04d", path, file_number ); if ( (fp = fopen( filename, "r" )) == NULL ) { /* LET USER KNOW HE HAS A PROBLEM */ fprintf( stderr, "\nError opening disk input file: %s\n", filename ); close( filedes ); exit( 1 ); } while( !feof( fp ) ) { /* READ IN A BLOCK AT A TIME UNTIL AN EOF IS FOUND */ if ( (fread( (void *)data_block, (size_t)1, (size_t)block_size, fp )) != block_size ) { /* MAKE SURE THAT THIS IS A TRUE EOF */ if ( feof( fp ) == 0 ) { /* A BIGGER ERROR HAS OCCURRED */ fprintf( stderr, "\nError occurred reading file: %s\n", filename ); fclose( fp ); close( filedes ); exit( 1 ); } } else { /* WRITE THE DATA BLOCK TO TAPE */ if ( (num_written = write( filedes, (void *)data_block, (unsigned)block_size )) != block_size ) { fprintf( stderr, "\nError writing block #%d to tape\n", block_number ); fprintf( stderr, "\nShould have been %d bytes, was %d bytes\n", block_size, num_written ); fclose( fp ); close( filedes ); exit( 1 ); } block_number++; } /* end of else */ } /* END OF FEOF */ /* CLOSE THE TAPE DRIVE */ close( filedes ); /* CLOSE THE DISK FILE */ fclose( fp ); fprintf( stderr, "\nDone with file #: %d", file_number ); } /* OPEN THE TAPE DEVICE */ if ( (filedes = open( device, O_WRONLY, 0 )) < 0 ) { /* LET USER KNOW HE HAS A PROBLEM */ fprintf( stderr, "\nError opening device: %s to write terminal EOF\n", device ); exit( 1 ); } /* CLOSE THE TAPE DRIVE */ close( filedes ); exit( 0 ); } /************************** End of main() *********************************/ /************************* End of write_tape.c ****************************/