StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
sfs_index_daq.cxx
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include "sfs_index.h"
7 #include <sys/uio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <rtsLog.h>
13 
14 #include <rts.h>
15 // #include <byteswap.h>
16 // #define swap16(x) bswap_16(x)
17 // #define swap32(x) bswap_32(x)
18 
19 #include <daqFormats.h>
20 
21 // Ugly Ugly hack.
22 // searches the future for an event number
23 int SFS_ittr::findEventNumber()
24 {
25  int ret;
26 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
27  long long int orig_pos = wfile->lseek(0, SEEK_CUR);
28  LOG(DBG, "findEventNumber: pos=%lld",orig_pos);
29 #else
30  int orig_pos = wfile->lseek(0, SEEK_CUR);
31  LOG(DBG, "findEventNumber: pos=%d",orig_pos);
32 #endif
33 
34  char buff[12];
35  for(;;) {
36  ret = wfile->read(buff, 8);
37  if(ret == 0) { ret = 0; break; }
38  if(ret != 8) { ret = -1; break; }
39 
40 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
41  wfile->lseek(-((long long int)8), SEEK_CUR);
42 #else
43  wfile->lseek(-8, SEEK_CUR);
44 #endif
45 
46  if(memcmp(buff, "SFS V", 5) == 0) {
47  wfile->lseek(12, SEEK_CUR);
48  continue;
49  }
50  else if(memcmp(buff, "LRHD", 4) == 0) {
51  wfile->lseek(60, SEEK_CUR);
52  continue;
53  }
54  else if(memcmp(buff, "HEAD", 4) == 0) {
55  wfile->lseek(12, SEEK_CUR);
56  continue;
57  }
58  else if(memcmp(buff, "DATAP", 5) == 0) {
59  DATAP datap;
60  ret = wfile->read(&datap, sizeof(datap));
61  if(ret != sizeof(datap)) {
62  LOG(ERR, "Error reading datap: %d",ret);
63  ret = -1;
64  break;
65  }
66 
67  int seq = datap.seq;
68  if(datap.bh.byte_order != 0x04030201) {
69  seq = swap32(seq);
70  }
71 
72  LOG(DBG, "Got event #%d from DATAP",seq);
73  ret = seq;
74  break;
75  }
76  else if(memcmp(buff, "FILE", 4) == 0) { // finally!
77  char fbuff[120];
78  SFS_File *f = (SFS_File *)fbuff;
79  ret = wfile->read(fbuff, sizeof(SFS_File));
80  if(ret != sizeof(SFS_File)) {
81  LOG(ERR, "Error reading FILE: %d",ret);
82  ret = -1;
83  break;
84  }
85  int sz = f->head_sz;
86  if(f->byte_order != 0x04030201) {
87  sz = swap32(sz);
88  }
89 
90 
91  if((unsigned int)sz > sizeof(SFS_File)) {
92  int sztogo = sz - sizeof(SFS_File);
93  char *btogo = fbuff;
94  btogo += sizeof(SFS_File);
95 
96  ret = wfile->read(btogo, sztogo);
97  if(ret != sztogo) {
98  LOG(ERR, "Error reading FILE: %d",ret);
99  ret = -1;
100  break;
101  }
102  }
103 
104  ret = atoi(&f->name[2]); // skip "/#"
105 
106  LOG(DBG, "Set event number to #%d from next FILE rec",ret);
107  break;
108  }
109  else {
110  LOG(DBG, "Else?");
111  break;
112  }
113  }
114 
115  wfile->lseek(orig_pos, SEEK_SET);
116  LOG(DBG, "Returning %d",ret);
117  return ret;
118 }
119 
120 // VERY VERY ugly hack
121 // jumps back from datap to LRHD and gets total event length
122 // then subtracts LRHD length and returns....
123 int SFS_ittr::findFullLength()
124 {
125  LOGREC lrhd;
126  // int orig_pos = wfile->lseek(0, SEEK_CUR);
127 
128 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
129  wfile->lseek(-((long long int)sizeof(lrhd)),SEEK_CUR);
130 #else
131  wfile->lseek(-(sizeof(lrhd)),SEEK_CUR);
132 #endif
133 
134  int ret = wfile->read(&lrhd, sizeof(lrhd));
135 
136  if(ret != sizeof(lrhd)) {
137  LOG(ERR, "Error reading lrhd %d", ret);
138  return -1;
139  }
140 
141  int sz = lrhd.length;
142  if(lrhd.lh.byte_order != 0x04030201) {
143  sz = swap32(sz);
144  }
145  sz *= 4;
146  sz -= sizeof(lrhd);
147 
148  return sz;
149 }
150 
151 int SFS_ittr::nextLRHD()
152 {
153  // First check if valid?
154  LOGREC lrhd;
155  int ret = wfile->read(&lrhd, sizeof(lrhd));
156  if(ret != sizeof(lrhd)) {
157  LOG(ERR, "Error reading lrhd %d", ret);
158  return -1;
159  }
160 
161 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
162  long long int xxx = wfile->lseek(-((long long int)sizeof(lrhd)),SEEK_CUR); // go back to start...
163  LOG(DBG, "nextLRHD(): (%c%c%c%c) off=%lld",
164  lrhd.lh.bank_type[0],lrhd.lh.bank_type[1],
165  lrhd.lh.bank_type[2],lrhd.lh.bank_type[3], xxx);
166 #else
167  int xxx = wfile->lseek(-(sizeof(lrhd)),SEEK_CUR); // go back to start...
168  LOG(DBG, "nextLRHD(): (%c%c%c%c) off=%d",
169  lrhd.lh.bank_type[0],lrhd.lh.bank_type[1],
170  lrhd.lh.bank_type[2],lrhd.lh.bank_type[3], xxx);
171 #endif
172 
173 
174 
175  if(memcmp(lrhd.lh.bank_type, "LRHD", 4) != 0) {
176  LOG(ERR, "nextLRHD() not LRHD %c%c%c%c",
177  lrhd.lh.bank_type[0],lrhd.lh.bank_type[1],
178  lrhd.lh.bank_type[2],lrhd.lh.bank_type[3]);
179  //wfile->lseek(-sizeof(lrhd), SEEK_CUR); // put it back...
180  return -1;
181  }
182 
183  if(memcmp(lrhd.record_type, "DATA", 4) != 0) {
184  LOG(DBG, "nextLRHD() not DATA %c%c%c%c",
185  lrhd.record_type[0],lrhd.record_type[1],
186  lrhd.record_type[2],lrhd.record_type[3]);
187  //wfile->lseek(-sizeof(lrhd), SEEK_CUR); // put it back...
188  return -1;
189  }
190 
191  // Okay. Now we know there is a LRHD record
192  int n = findEventNumber();
193 
194 
195  memcpy(entry.type,"FILE",4);
196  entry.byte_order = 0x04030201;
197  entry.sz = sizeof(LOGREC);
198  LOG(DBG, "LRHD: entry.sz = %d",entry.sz);
199  sprintf(entry.name, "/#%d/lrhd",n);
200  entry.head_sz = 0;
201  entry.attr = 0;
202 
203  strcpy(fullpath, entry.name);
204  strcpy(ppath, entry.name);
205  striptofile(entry.name);
206  stripfile(ppath);
207 
208  int ccc = wfile->lseek(0,SEEK_CUR);
209 
210  filepos = 1;
211 
212 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
213  LOG(DBG,"fullpath %s, entry.name: %s, fileoffset %lld (%d)/%d, sz %d head_sz %d",
214  fullpath, entry.name, fileoffset, ccc, filepos, entry.sz, entry.head_sz);
215 #else
216  LOG(DBG,"fullpath %s, entry.name: %s, fileoffset %d (%d)/%d, sz %d head_sz %d",
217  fullpath, entry.name, fileoffset, ccc, filepos, entry.sz, entry.head_sz);
218 #endif
219 
220  return 0;
221 }
222 
223 int SFS_ittr::nextDatap()
224 {
225  int sz = findFullLength();
226 
227  DATAP datap;
228  int ret = wfile->read(&datap, sizeof(datap));
229  if(ret != sizeof(datap)) {
230  LOG(ERR, "Bad read of datap %d",ret);
231  return -1;
232  }
233 
234  LOG(DBG, "Nextdatap: (%c%c%c%c)",
235  datap.bh.bank_type[0],
236  datap.bh.bank_type[1],
237  datap.bh.bank_type[2],
238  datap.bh.bank_type[3]);
239 
240  char buff[8];
241  ret = wfile->read(buff, 8);
242  if(ret != 8) {
243  LOG(ERR, "Bad read of datap %d",ret);
244  return -1;
245  }
246 
247 #if defined(__USE_LARGEFILE64) || defined(_LARGEFILE64_SOURCE)
248  wfile->lseek(-((long long int)8),SEEK_CUR);
249  wfile->lseek(-((long long int)sizeof(DATAP)), SEEK_CUR);
250 #else
251  wfile->lseek(-8,SEEK_CUR);
252  wfile->lseek(-sizeof(DATAP), SEEK_CUR);
253 #endif
254 
255  if(memcmp(buff, "FILE", 4) == 0) { // SFS follows, so no legacy...
256  LOG(DBG, "No legacy because SFS follows");
257  return -1;
258  }
259  else {
260  LOG(DBG, "Making legacy entry");
261  }
262 
263  int seq = datap.seq;
264  if(datap.bh.byte_order != 0x04030201) seq = swap32(seq);
265 
266  memcpy(entry.type,"FILE",4);
267  entry.byte_order = 0x04030201;
268  LOG(DBG, "sz = %d");
269  entry.sz = sz;
270  sprintf(entry.name, "/#%d/legacy",seq);
271  entry.head_sz = 0;
272  entry.attr = 0;
273 
274  strcpy(fullpath, entry.name);
275  strcpy(ppath, entry.name);
276  striptofile(entry.name);
277  stripfile(ppath);
278 
279  int ccc = wfile->lseek(0,SEEK_CUR);
280 
281  LOG(DBG,"fullpath %s, entry.name: %s, fileoffset %d (%d)/%d, sz %d head_sz %d",
282  fullpath, entry.name, fileoffset, ccc, filepos, entry.sz, entry.head_sz);
283 
284  filepos = 1;
285  return 0;
286 }