From: hades Date: Wed, 6 Jun 2001 15:49:02 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=083258d53782009ef3aab2fc6f7a7d32db28487f;p=daqdata.git *** empty log message *** --- diff --git a/hadaq/ansiTape.c b/hadaq/ansiTape.c index b34d814..7a95ae5 100644 --- a/hadaq/ansiTape.c +++ b/hadaq/ansiTape.c @@ -1,48 +1,39 @@ -static char *rcsId = "$Header: /misc/hadesprojects/daq/cvsroot/eventbuilder/hadaq/ansiTape.c,v 1.9 2001-03-07 16:02:43 hades Exp $"; - -/* For allocMem(), freeMem() */ -#include - -/* For msglog() */ -#include -#include - -/* For sscanf() */ +#include #include - -/* For open() */ +#include #include #include -#include - -/* For read(), write(), close() */ +#include #include /* For st */ #include #include -#include "ansiTapeLabel.h" #include "ansiTape.h" +#include "ansiTapeLabel.h" -AnsiTape *openAnsiTape(const char* filename) { +AnsiTape *openAnsiTape(const char* filename, const char *dev) { #if defined(MTIOCTOP) int tape; int fileSeqNum = 0; int stat; int i = 0; - char *vol; - char *label; + char vol[LABELSIZE]; + char label[LABELSIZE]; AnsiTape *thisAnsiTape; struct mtop mtoperS, *mtoper = &mtoperS; - thisAnsiTape = allocMem(sizeof(AnsiTape)); + if (NULL == (thisAnsiTape = malloc(sizeof(AnsiTape)))) { + syslog(LOG_ERR, "Could not allocate memory for ansiTape."); + return NULL; + } - tape = open("/dev/tape", O_RDWR); + tape = open(dev, O_RDWR); if(tape == -1) { - msglog(LOG_ERR, "Could not open tape!\n"); - exit(-2); + syslog(LOG_ERR, "Could not open tape."); + return NULL; } #if defined(MTEOM) @@ -55,35 +46,36 @@ AnsiTape *openAnsiTape(const char* filename) { mtoper->mt_count = 1; stat = ioctl(tape, MTIOCTOP, mtoper); if (stat == -1) { - msglog(LOG_ERR, "Could not go to the eom of the tape!\n"); - exit(-2); + syslog(LOG_ERR, "Could not go to the eom of the tape."); + return NULL; } mtoper->mt_op = MTBSF; - mtoper->mt_count = 2; + mtoper->mt_count = 3; stat = ioctl(tape, MTIOCTOP, mtoper); if (stat == -1) { + int isNewTape; mtoper->mt_op = MTREW; mtoper->mt_count = 1; stat = ioctl(tape, MTIOCTOP, mtoper); if (stat == -1) { - msglog(LOG_ERR, "Could not rewind tape!\n"); - exit(-2); + syslog(LOG_ERR, "Could not rewind tape."); + return NULL; } - vol = readVolumeLabel(tape); + isNewTape = readVolumeLabel(tape, vol); stat = ioctl(tape, MTIOCTOP, mtoper); if (stat == -1) { - msglog(LOG_ERR, "Could not rewind tape!\n"); - exit(-2); + syslog(LOG_ERR, "Could not rewind tape."); + return NULL; } - if (vol == 0) { - writeNewVolumeLabel("HADAQ ", " ", tape); + if (isNewTape == 0) { + writeVolumeLabel(tape, vol); } else { - writeVolumeLabel(vol, tape); + writeNewVolumeLabel(tape, "HADAQ ", " "); } fileSeqNum = 1; @@ -92,21 +84,17 @@ AnsiTape *openAnsiTape(const char* filename) { mtoper->mt_count = 1; stat = ioctl(tape, MTIOCTOP, mtoper); if (stat == -1) { - msglog(LOG_ERR, "Could not jump over the filemark!\n"); - exit(-2); + syslog(LOG_ERR, "Could not jump over the filemark."); + return NULL; } - label = allocMem(81*sizeof(char)); do { - stat = read(tape, label, 80); - label[80] = '\0'; - msglog(LOG_INFO, "EOF Label read te get fileSeqNum:\n%s\n", label); + stat = read(tape, label, LABELSIZE); if (i == 0) { sscanf(label, "%*31c%4d", &fileSeqNum); fileSeqNum++; } i++; - } while (stat == 80); - freeMem(label); + } while (stat == LABELSIZE); } writeHeader(tape, fileSeqNum, filename); @@ -115,8 +103,8 @@ AnsiTape *openAnsiTape(const char* filename) { mtoper->mt_count = 1; stat = ioctl(tape, MTIOCTOP, mtoper); if (stat == -1) { - msglog(LOG_ERR, "Could not write filemark!\n"); - exit(-2); + syslog(LOG_ERR, "Could not write filemark."); + return NULL; } thisAnsiTape->fd = tape; @@ -136,12 +124,12 @@ AnsiTape *openAnsiTape(const char* filename) { int writeAnsiTape(AnsiTape *openTape, const char *src, size_t size) { #if defined(MTIOCTOP) - int returnValue = 0; + int retVal = 0; while(size >= BLOCKSIZE - openTape->bufferFull) { if (openTape->bufferFull < BLOCKSIZE) { memcpy(openTape->buf + openTape->bufferFull, src, BLOCKSIZE - openTape->bufferFull); src += (BLOCKSIZE - openTape->bufferFull); - returnValue += (BLOCKSIZE - openTape->bufferFull); + retVal += (BLOCKSIZE - openTape->bufferFull); openTape->bytesWritten += (BLOCKSIZE - openTape->bufferFull); size -= (BLOCKSIZE - openTape->bufferFull); } @@ -152,37 +140,38 @@ int writeAnsiTape(AnsiTape *openTape, const char *src, size_t size) memcpy(openTape->buf + openTape->bufferFull, src, size); openTape->bytesWritten += size; openTape->bufferFull += size; - returnValue += size; + retVal += size; } - return returnValue; + return retVal; #else return -1; #endif } +/* BUGBUG the last write may have less than BLOCKSIZE bytes */ int closeAnsiTape(AnsiTape *openTape) { #if defined(MTIOCTOP) int stat; struct mtop mtoperS, *mtoper = &mtoperS; if(openTape->bufferFull != 0) { - write(openTape->fd, openTape->buf, BLOCKSIZE); + write(openTape->fd, openTape->buf, openTape->bufferFull); } mtoper->mt_op = MTWEOF; mtoper->mt_count = 1; stat = ioctl(openTape->fd, MTIOCTOP, mtoper); if (stat == -1) { - msglog(LOG_ERR, "Could not write filemark!\n"); - exit(-2); + syslog(LOG_ERR, "Could not write filemark."); + return -1; } writeTrailer(openTape->fd, openTape->fileSeqNum, openTape->bytesWritten, openTape->filename); stat = close(openTape->fd); if (stat == -1) { - msglog(LOG_ERR, "Could not close file on tape!\n"); - exit(-2); - freeMem(openTape); + syslog(LOG_ERR, "Could not close file on tape."); + return -1; + free(openTape); } return 0;