00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <unistd.h>
00005 #include <time.h>
00006 #include "sfs_index.h"
00007 #include <sys/uio.h>
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 #include <fcntl.h>
00011 #include <errno.h>
00012 #include <rtsLog.h>
00013
00014 #include <byteswap.h>
00015 #define swap16(x) bswap_16(x)
00016 #define swap32(x) bswap_32(x)
00017
00018 #include <daqFormats.h>
00019
00020
00021
00022 int SFS_ittr::findEventNumber()
00023 {
00024 int ret;
00025 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00026 long long int orig_pos = wfile->lseek(0, SEEK_CUR);
00027 LOG(DBG, "findEventNumber: pos=%lld",orig_pos);
00028 #else
00029 int orig_pos = wfile->lseek(0, SEEK_CUR);
00030 LOG(DBG, "findEventNumber: pos=%d",orig_pos);
00031 #endif
00032
00033 char buff[12];
00034 for(;;) {
00035 ret = wfile->read(buff, 8);
00036 if(ret == 0) { ret = 0; break; }
00037 if(ret != 8) { ret = -1; break; }
00038
00039 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00040 wfile->lseek(-((long long int)8), SEEK_CUR);
00041 #else
00042 wfile->lseek(-8, SEEK_CUR);
00043 #endif
00044
00045 if(memcmp(buff, "SFS V", 5) == 0) {
00046 wfile->lseek(12, SEEK_CUR);
00047 continue;
00048 }
00049 else if(memcmp(buff, "LRHD", 4) == 0) {
00050 wfile->lseek(60, SEEK_CUR);
00051 continue;
00052 }
00053 else if(memcmp(buff, "HEAD", 4) == 0) {
00054 wfile->lseek(12, SEEK_CUR);
00055 continue;
00056 }
00057 else if(memcmp(buff, "DATAP", 5) == 0) {
00058 DATAP datap;
00059 ret = wfile->read(&datap, sizeof(datap));
00060 if(ret != sizeof(datap)) {
00061 LOG(ERR, "Error reading datap: %d",ret);
00062 ret = -1;
00063 break;
00064 }
00065
00066 int seq = datap.seq;
00067 if(datap.bh.byte_order != 0x04030201) {
00068 seq = swap32(seq);
00069 }
00070
00071 LOG(DBG, "Got event #%d from DATAP",seq);
00072 ret = seq;
00073 break;
00074 }
00075 else if(memcmp(buff, "FILE", 4) == 0) {
00076 char fbuff[120];
00077 SFS_File *f = (SFS_File *)fbuff;
00078 ret = wfile->read(fbuff, sizeof(SFS_File));
00079 if(ret != sizeof(SFS_File)) {
00080 LOG(ERR, "Error reading FILE: %d",ret);
00081 ret = -1;
00082 break;
00083 }
00084 int sz = f->head_sz;
00085 if(f->byte_order != 0x04030201) {
00086 sz = swap32(sz);
00087 }
00088
00089
00090 if((unsigned int)sz > sizeof(SFS_File)) {
00091 int sztogo = sz - sizeof(SFS_File);
00092 char *btogo = fbuff;
00093 btogo += sizeof(SFS_File);
00094
00095 ret = wfile->read(btogo, sztogo);
00096 if(ret != sztogo) {
00097 LOG(ERR, "Error reading FILE: %d",ret);
00098 ret = -1;
00099 break;
00100 }
00101 }
00102
00103 ret = atoi(&f->name[2]);
00104
00105 LOG(DBG, "Set event number to #%d from next FILE rec",ret);
00106 break;
00107 }
00108 else {
00109 LOG(DBG, "Else?");
00110 break;
00111 }
00112 }
00113
00114 wfile->lseek(orig_pos, SEEK_SET);
00115 LOG(DBG, "Returning %d",ret);
00116 return ret;
00117 }
00118
00119
00120
00121
00122 int SFS_ittr::findFullLength()
00123 {
00124 LOGREC lrhd;
00125
00126
00127 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00128 wfile->lseek(-((long long int)sizeof(lrhd)),SEEK_CUR);
00129 #else
00130 wfile->lseek(-(sizeof(lrhd)),SEEK_CUR);
00131 #endif
00132
00133 int ret = wfile->read(&lrhd, sizeof(lrhd));
00134
00135 if(ret != sizeof(lrhd)) {
00136 LOG(ERR, "Error reading lrhd %d", ret);
00137 return -1;
00138 }
00139
00140 int sz = lrhd.length;
00141 if(lrhd.lh.byte_order != 0x04030201) {
00142 sz = swap32(sz);
00143 }
00144 sz *= 4;
00145 sz -= sizeof(lrhd);
00146
00147 return sz;
00148 }
00149
00150 int SFS_ittr::nextLRHD()
00151 {
00152
00153 LOGREC lrhd;
00154 int ret = wfile->read(&lrhd, sizeof(lrhd));
00155 if(ret != sizeof(lrhd)) {
00156 LOG(ERR, "Error reading lrhd %d", ret);
00157 return -1;
00158 }
00159
00160 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00161 long long int xxx = wfile->lseek(-((long long int)sizeof(lrhd)),SEEK_CUR);
00162 LOG(DBG, "nextLRHD(): (%c%c%c%c) off=%lld",
00163 lrhd.lh.bank_type[0],lrhd.lh.bank_type[1],
00164 lrhd.lh.bank_type[2],lrhd.lh.bank_type[3], xxx);
00165 #else
00166 int xxx = wfile->lseek(-(sizeof(lrhd)),SEEK_CUR);
00167 LOG(DBG, "nextLRHD(): (%c%c%c%c) off=%d",
00168 lrhd.lh.bank_type[0],lrhd.lh.bank_type[1],
00169 lrhd.lh.bank_type[2],lrhd.lh.bank_type[3], xxx);
00170 #endif
00171
00172
00173
00174 if(memcmp(lrhd.lh.bank_type, "LRHD", 4) != 0) {
00175 LOG(ERR, "nextLRHD() not LRHD %c%c%c%c",
00176 lrhd.lh.bank_type[0],lrhd.lh.bank_type[1],
00177 lrhd.lh.bank_type[2],lrhd.lh.bank_type[3]);
00178
00179 return -1;
00180 }
00181
00182 if(memcmp(lrhd.record_type, "DATA", 4) != 0) {
00183 LOG(DBG, "nextLRHD() not DATA %c%c%c%c",
00184 lrhd.record_type[0],lrhd.record_type[1],
00185 lrhd.record_type[2],lrhd.record_type[3]);
00186
00187 return -1;
00188 }
00189
00190
00191 int n = findEventNumber();
00192
00193
00194 memcpy(entry.type,"FILE",4);
00195 entry.byte_order = 0x04030201;
00196 entry.sz = sizeof(LOGREC);
00197 LOG(DBG, "LRHD: entry.sz = %d",entry.sz);
00198 sprintf(entry.name, "/#%d/lrhd",n);
00199 entry.head_sz = 0;
00200 entry.attr = 0;
00201
00202 strcpy(fullpath, entry.name);
00203 strcpy(ppath, entry.name);
00204 striptofile(entry.name);
00205 stripfile(ppath);
00206
00207 int ccc = wfile->lseek(0,SEEK_CUR);
00208
00209 filepos = 1;
00210
00211 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00212 LOG(DBG,"fullpath %s, entry.name: %s, fileoffset %lld (%d)/%d, sz %d head_sz %d",
00213 fullpath, entry.name, fileoffset, ccc, filepos, entry.sz, entry.head_sz);
00214 #else
00215 LOG(DBG,"fullpath %s, entry.name: %s, fileoffset %d (%d)/%d, sz %d head_sz %d",
00216 fullpath, entry.name, fileoffset, ccc, filepos, entry.sz, entry.head_sz);
00217 #endif
00218
00219 return 0;
00220 }
00221
00222 int SFS_ittr::nextDatap()
00223 {
00224 int sz = findFullLength();
00225
00226 DATAP datap;
00227 int ret = wfile->read(&datap, sizeof(datap));
00228 if(ret != sizeof(datap)) {
00229 LOG(ERR, "Bad read of datap %d",ret);
00230 return -1;
00231 }
00232
00233 LOG(DBG, "Nextdatap: (%c%c%c%c)",
00234 datap.bh.bank_type[0],
00235 datap.bh.bank_type[1],
00236 datap.bh.bank_type[2],
00237 datap.bh.bank_type[3]);
00238
00239 char buff[8];
00240 ret = wfile->read(buff, 8);
00241 if(ret != 8) {
00242 LOG(ERR, "Bad read of datap %d",ret);
00243 return -1;
00244 }
00245
00246 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
00247 wfile->lseek(-((long long int)8),SEEK_CUR);
00248 wfile->lseek(-((long long int)sizeof(DATAP)), SEEK_CUR);
00249 #else
00250 wfile->lseek(-8,SEEK_CUR);
00251 wfile->lseek(-sizeof(DATAP), SEEK_CUR);
00252 #endif
00253
00254 if(memcmp(buff, "FILE", 4) == 0) {
00255 LOG(DBG, "No legacy because SFS follows");
00256 return -1;
00257 }
00258 else {
00259 LOG(DBG, "Making legacy entry");
00260 }
00261
00262 int seq = datap.seq;
00263 if(datap.bh.byte_order != 0x04030201) seq = swap32(seq);
00264
00265 memcpy(entry.type,"FILE",4);
00266 entry.byte_order = 0x04030201;
00267 LOG(DBG, "sz = %d");
00268 entry.sz = sz;
00269 sprintf(entry.name, "/#%d/legacy",seq);
00270 entry.head_sz = 0;
00271 entry.attr = 0;
00272
00273 strcpy(fullpath, entry.name);
00274 strcpy(ppath, entry.name);
00275 striptofile(entry.name);
00276 stripfile(ppath);
00277
00278 int ccc = wfile->lseek(0,SEEK_CUR);
00279
00280 LOG(DBG,"fullpath %s, entry.name: %s, fileoffset %d (%d)/%d, sz %d head_sz %d",
00281 fullpath, entry.name, fileoffset, ccc, filepos, entry.sz, entry.head_sz);
00282
00283 filepos = 1;
00284 return 0;
00285 }