-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 <hadesstd.h>
-
-/* For msglog() */
-#include <hadesstd.h>
-#include <errno.h>
-
-/* For sscanf() */
+#include <fcntl.h>
#include <stdio.h>
-
-/* For open() */
+#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
-#include <fcntl.h>
-
-/* For read(), write(), close() */
+#include <syslog.h>
#include <unistd.h>
/* For st */
#include <sys/ioctl.h>
#include <sys/mtio.h>
-#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)
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;
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);
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;
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);
}
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;