00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 #include <time.h>
00027 #include <ctype.h>
00028 #include <math.h>
00029 #include <errno.h>
00030 #include <unistd.h>
00031 #include <string.h>
00032 #include <sys/types.h>
00033 #include <sys/stat.h>
00034 #include <fcntl.h>
00035 #include <dirent.h>
00036
00037 #include "common.h"
00038
00039 int nFiles=0;
00040
00041 int trySeek=-1;
00042 struct dirent **titanList=NULL;
00043 int titanListLen=0;
00044 int titanListIndex=0;
00045 struct dirent **dayDir=NULL;
00046 int dayDirLen=0;
00047 int dayDirIndex=0;
00048 char fullName[256];
00049
00050 FILE *eventList=NULL;
00051 int segmentLength=0;
00052
00054 void CloseInput(void)
00055 {
00056 if (titan2File!=0)
00057 {
00058 fclose(titan2File);
00059 titan2File=NULL;
00060 }
00061 }
00062
00067 FILE * InitEventList(const char *fn)
00068 {
00069 if (eventList==NULL)
00070 {
00071 eventList=fopen(fn,"rt");
00072 if (eventList==NULL)
00073 {
00074 PrintError("%s: %m\n",fn);
00075 }
00076 }
00077 return(eventList);
00078 }
00079
00080
00086 time_t NextWindow(void)
00087 {
00088 char line[255];
00089 double t;
00090 int len;
00091
00092 trySeek=-1;
00093
00094
00095 while (eventList!=NULL)
00096 {
00097
00098 line[0]=0;
00099 fgets(line,254,eventList);
00100
00101 if (line[0]==0)
00102 {
00103
00104 PrintLog("end of timelist\n");
00105 fclose(eventList);
00106 eventList=NULL;
00107 return(-1);
00108 }
00109
00110
00111 if (line[0]=='#')
00112 {
00113
00114 continue;
00115 }
00116
00117
00118
00119 if (str2time(line,&t)<0)
00120 {
00121 beginWindow=-1;
00122 }
00123 else
00124 {
00125 beginWindow=(time_t)t;
00126 }
00127 if ((beginWindow<0)||(sscanf(line,"%*s %d",&len)!=1))
00128 {
00129 PrintError("timelist format error: %s",line);
00130 return(-1);
00131 }
00132 endWindow=beginWindow+len;
00133
00134 PrintLog("############ next window: %s, len=%d\n",
00135 time2str((double)beginWindow),len);
00136
00137 if (preEvent!=0xefffffff)
00138 {
00139
00140 beginWindow-=preEvent;
00141
00142 }
00143
00144 if (postEvent!=0xefffffff)
00145 {
00146
00147 endWindow=beginWindow+postEvent;
00148
00149 }
00150 return(beginWindow);
00151
00152 }
00153
00154 if (cmdLineWindow>0)
00155 {
00156 cmdLineWindow=-1;
00157 return(beginWindow);
00158 }
00159
00160
00161
00162 if ((beginWindow==endWindow)&&(beginWindow==0))
00163 {
00164
00165 return(-1);
00166 }
00167
00168 else
00169 {
00170
00171 }
00172
00173 beginWindow=0;
00174 endWindow=0;
00175 if (timeSegment>0)
00176 {
00177 return(-1);
00178 }
00179 else
00180 {
00181 return(beginWindow);
00182 }
00183 }
00184
00186 int SequentialSeek(time_t t)
00187 {
00188 double tf;
00189 fastSeek=1;
00190
00191
00192
00193 seeking=1;
00194
00195 correctedTime.usecond=0;
00196
00197 while ((correctedTime.usecond>>32)+1800<t)
00198
00199 {
00200 int len;
00201
00202 printTrigger=-1;
00203
00204 correctedTime.usecond=0;
00205
00206 titan2File=NextFile();
00207
00208 len=strlen(currentFile);
00209
00210 if (titan2File==NULL)
00211 {
00212 break;
00213 }
00214
00215 if (strcmp(currentFile+len-4,".ftp")==0)
00216 {
00217 if (str2time(currentFile+len-23,&tf)<0)
00218 {
00219 PrintError("internal error (%s %s %d)\n",
00220 currentFile,__FILE__,__LINE__);
00221 PrintError("make sure that the day-dir is present in the path\n");
00222 }
00223 }
00224 else
00225 {
00226 if (str2time(currentFile+len-19,&tf)<0)
00227 {
00228 PrintError("internal error (%s %s %d)\n",
00229 currentFile,__FILE__,__LINE__);
00230 PrintError("make sure that the day-dir is present in the path\n");
00231 }
00232 }
00233 if (tf+3600<t)
00234 {
00235 continue;
00236 }
00237 PrintDebug("trying %s\n",currentFile);
00238
00239 while (correctedTime.usecond<=0)
00240 {
00241
00242 GetFrame();
00243
00244 }
00245
00246 }
00247
00248 seeking=0;
00249 fastSeek=0;
00250
00251
00252 if ((correctedTime.usecond>>32)+600>=t)
00253 {
00254 return(1);
00255 }
00256 else
00257 {
00258 return(0);
00259 }
00260 }
00261
00268 int SeekTime(time_t t)
00269 {
00270 char line[128];
00271 char fn[128],theFile[128];
00272 int r;
00273 time_t at=0,lt=-1;
00274 int64_t offset=0,lastOffset=0;
00275
00276
00277 if (trySeek==0)
00278
00279 {
00280
00281 return(0);
00282
00283 }
00284
00285
00286 if ((inputMode==DAY_MODE)||(inputMode==DISK_MODE))
00287
00288 {
00289 trySeek=0;
00290
00291 return(SequentialSeek(t));
00292
00293 }
00294
00295 if (strcmp(HWConfig.serialNumber,DEFAULT_SERIAL)==0)
00296 {
00297 return(0);
00298 }
00299
00300
00301 if ((beginWindow<=0)||(t<=0))
00302 {
00303 trySeek=0;
00304 return(0);
00305 }
00306 PrintLog("trying to seek %s\n",time2str((double)t));
00307
00308 r=OpenCacheFileRead(HWConfig.serialNumber,t);
00309 if (r==1)
00310 {
00311
00312 trySeek=0;
00313 return(0);
00314 }
00315
00316 if (r==-1)
00317 {
00318
00319 trySeek=0;
00320 return(0);
00321 }
00322
00323 RewindCache();
00324
00326
00327 if ((correctedTime.usecond>>32)>beginWindow)
00328 {
00329
00330 trySeek=0;
00331 return(0);
00332 }
00333
00334
00335 seeking=1;
00336
00337 fn[0]=theFile[0]=0;
00338
00339
00340 while (CacheRead(line)!=NULL)
00341 {
00342 int64_t o;
00343 PrintDebug("'%s'\n",line);
00344
00345
00346 if (memcmp(line,"#TIME",5))
00347 {
00348
00349 continue;
00350
00351 }
00352
00353 if (fn[0]!=0)
00354 {
00355 strcpy(theFile,fn);
00356 offset=lastOffset;
00357 lt=at;
00358 }
00359
00360
00361 if (sscanf(line,"%*s %s %lld %ld",fn,&o,&at)!=3)
00362 {
00363 fn[0]=0;
00364 continue;
00365 }
00366 if (at<=lt)
00367 {
00368 fn[0]=0;
00369 continue;
00370 }
00371
00372 if ((at>0)&&(at>t))
00373 {
00374 PrintDebug("at=%d %s\n",at,time2str(at));
00375 PrintDebug("lt=%d %s\n",lt,time2str(lt));
00376 PrintDebug(" t=%d\n",t);
00377 PrintDebug("file=%s, offset=%d\n",theFile,offset);
00378
00379 break;
00380
00381 }
00382 lastOffset=o;
00383 }
00384
00385 trySeek=0;
00386
00387
00388 while (strcmp(theFile,currentFile))
00389 {
00390
00391 titan2File=NextFile();
00392 if (titan2File==NULL)
00393 {
00394 break;
00395 }
00396 PrintDebug("%s vs %s\n", theFile,currentFile);
00397
00398 }
00399
00400 if (titan2File)
00401 {
00402 PrintDebug("found: %s %d\n",currentFile,offset);
00403 fseek(titan2File,offset,SEEK_SET);
00404 }
00405 else
00406 {
00407 PrintDebug("not found\n");
00408 }
00409
00410 seeking=0;
00411
00412
00413 printTrigger=0;
00414
00415 return(0);
00416 }
00417
00421 int TitanFileSelect(const struct dirent* t)
00422 {
00423 int i;
00424 for (i=0; i<8; i++)
00425 {
00426 if (!isdigit(t->d_name[i]))
00427 {
00428 if (t->d_name[i]!='.')
00429 {
00430 return(0);
00431 }
00432 }
00433 }
00434 if (strlen(t->d_name)<8)
00435 {
00436 return(0);
00437 }
00438 if (strlen(t->d_name)>8)
00439 {
00440 if (strcmp(t->d_name+8,".ftp"))
00441 {
00442 return(0);
00443 }
00444 }
00445 return(1);
00446 }
00447
00451 int DayDirSelect(const struct dirent* t)
00452 {
00453 int i;
00454 if (strlen(t->d_name)!=10)
00455 {
00456 return(0);
00457 }
00458 if ((t->d_name[4]!='.')||(t->d_name[7]!='.'))
00459 {
00460 return(0);
00461 }
00462 for (i=0; i<2; i++)
00463 {
00464 if (
00465 (!isdigit(t->d_name[i]))||
00466 (!isdigit(t->d_name[i+2]))||
00467 (!isdigit(t->d_name[i+5]))||
00468 (!isdigit(t->d_name[i+8]))
00469 )
00470 {
00471 return(0);
00472 }
00473 }
00474 return(1);
00475 }
00476
00478 void FreeDiskDir(void)
00479 {
00480 int i;
00481 if (dayDirLen>0)
00482 {
00483 for (i=0; i<dayDirLen; i++)
00484 {
00485 free(dayDir[i]);
00486 }
00487 free(dayDir);
00488 dayDir=NULL;
00489 }
00490 dayDirLen=0;
00491 }
00492
00497 int OpenDiskDir(const char * path)
00498 {
00499
00500 if (inputMode==DISK_MODE)
00501 {
00502 baseDir=(char*)path;
00503 while (baseDir[strlen(baseDir)-1]=='/')
00504 {
00505 baseDir[strlen(baseDir)-1]=0;
00506 }
00507 }
00508
00509 FreeDiskDir();
00510
00511 dayDirLen=__scandir(path,&dayDir,DayDirSelect,__alphasort);
00512 dayDirIndex=0;
00513
00514 return(dayDirLen);
00515 }
00516
00518 void FreeDayDir(void)
00519 {
00520 int i;
00521 if (titanListLen>0)
00522 {
00523 for (i=0; i<titanListLen; i++)
00524 {
00525 free(titanList[i]);
00526 }
00527 free(titanList);
00528 titanList=NULL;
00529 }
00530 titanListLen=0;
00531 }
00532
00537 int OpenDayDir(const char * path)
00538 {
00539
00540 if (inputMode==DAY_MODE)
00541 {
00542 baseDir=(char*)path;
00543 }
00544
00545 FreeDayDir();
00546
00547 titanListLen=__scandir(path,&titanList,TitanFileSelect,__alphasort);
00548 titanListIndex=0;
00549
00550 return(titanListLen);
00551 }
00552
00563 FILE * NextFile(void)
00564 {
00565 FILE *f=NULL;
00566
00567
00568 CloseInput();
00569
00570
00571 if ((scanAll==0)&&(endWindow>0)&&((correctedTime.usecond>>32)>endWindow))
00572 {
00573 FlushDataFiles();
00574 return(NULL);
00575 }
00576
00577 while (f==NULL)
00578 {
00579
00580 if (inputMode!=FILE_MODE)
00581 {
00582 strcpy(fullName,baseDir);
00583 }
00584 switch (inputMode)
00585 {
00586 case DISK_MODE:
00587
00588 if (titanListIndex>=titanListLen)
00589 {
00590
00591 if (dayDirIndex>=dayDirLen)
00592
00593 {
00594
00595 return(NULL);
00596 }
00597
00598 else
00599 {
00600
00601 sprintf(fullName,"%s/%s",
00602 baseDir,
00603 dayDir[dayDirIndex]->d_name);
00604 PrintDebug("reading %s\n",fullName);
00605 OpenDayDir(fullName);
00606 dayDirIndex++;
00607
00608 }
00609 }
00610
00611 if (titanListIndex>=titanListLen)
00612 {
00613
00614 break;
00615
00616 }
00617
00618
00619 sprintf(fullName,"%s/%s",baseDir,dayDir[dayDirIndex-1]->d_name);
00620
00621 case DAY_MODE:
00622
00623 if (titanListIndex<titanListLen)
00624 {
00625 if (fullName[strlen(fullName)-1]!='/')
00626 {
00627 strcat(fullName,"/");
00628 }
00629 strcat(fullName,titanList[titanListIndex]->d_name);
00630
00631
00632 currentFile=fullName;
00633
00634 titanListIndex++;
00635 if ((f=fopen(currentFile,"rb"))==NULL)
00636 {
00637 strcat(currentFile,".ftp");
00638 if ((f=fopen(currentFile,"rb"))==NULL)
00639 {
00640 break;
00641 }
00642 else
00643 {
00644 PrintError("%s: %m (n=%d)\n",currentFile,nFiles);
00645 }
00646 }
00647 nFiles++;
00648 setvbuf(f,NULL,_IOFBF,FILE_BUFFER_SIZE);
00649
00650
00651 inputOffset=0;
00652 }
00653
00654 else
00655 {
00656
00657 return(NULL);
00658 }
00659 break;
00660 case FILE_MODE:
00661
00662 if (iFileInput>=ARGC)
00663 {
00664
00665 return(NULL);
00666 }
00667
00668 else
00669 {
00670
00671 currentFile=ARGV[iFileInput];
00672 inputOffset=0;
00673 printTrigger=totalFrameProcessed+INIT_PRINT_TRIGGER;
00674 iFileInput++;
00675 if ((f=fopen(currentFile,"rb"))==NULL)
00676 {
00677 PrintError("%s: %m\n",currentFile);
00678 break;
00679 }
00680 setvbuf(f,NULL,_IOFBF,FILE_BUFFER_SIZE);
00681 if (beginOfFile>0)
00682 {
00683 fseek(f,(long)beginOfFile,SEEK_SET);
00684 inputOffset=SafeFtell(f);
00685 }
00686 if (baseDir==NULL)
00687 {
00688 baseDir=currentFile;
00689 }
00690
00691 }
00692 break;
00693 }
00694 }
00695 return(f);
00696 }
00697
00701 int OpenTempFile(char * template)
00702 {
00703 char *path;
00704 static char templ[255];
00705 path=getenv("RTITAN2TMP");
00706 if (path)
00707 {
00708 sprintf(templ,"%s/%s",path,template);
00709 strcpy(template,templ);
00710 }
00711 else
00712 {
00713 }
00714 return(mkstemp(template));
00715 }
00716
00721 int Rename(const char *oldpath, const char *newpath)
00722 {
00723 int in, out, ret;
00724 ret=-1;
00725
00726 if (0==rename(oldpath,newpath))
00727 {
00728 return(0);
00729 }
00730 if (errno!=EXDEV)
00731 {
00732 PrintError("rename %s %s: %m\n",oldpath,newpath);
00733 return(-1);
00734 }
00735
00736 in=open(oldpath,O_RDONLY);
00737 if (in>=0)
00738 {
00739 struct stat st;
00740 fstat(in, &st);
00741 out=open(newpath,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR);
00742 if (out>=0)
00743 {
00744 if (SendFile(out, in, st.st_size)!=st.st_size)
00745 {
00746 PrintError("rename %s %s: %m\n",oldpath,newpath);
00747 ret=-1;
00748 }
00749 close(out);
00750 close(in);
00751 }
00752 else
00753 {
00754 close(in);
00755 }
00756 }
00757 else
00758 {
00759 close(in);
00760 }
00761 unlink(oldpath);
00762 return(ret);
00763 }