buildcache.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 Julien Sylvestre
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #include <unistd.h>
00027 
00028 #include <burstdso.h>
00029 
00030 
00031 int getFrameCache(char *fQuery, 
00032                   char *dataserver) {
00033 
00034   char *p0, *p1;
00035   char *buf;
00036 
00037   /* try to delete file */
00038   unlink(CACHEFILENAME);
00039 
00040 
00041   buf = (char *)calloc(1+strlen(fQuery),sizeof(char));
00042 
00043   /* process line by line */
00044 
00045   p0 = fQuery;
00046   p1 = strchr(fQuery,'\n');
00047   if(p1) {
00048     memcpy(buf,p0,p1-p0+1);
00049   } else {
00050     strcpy(buf,p0);
00051   }
00052 
00053   while(strlen(buf)) {
00054 
00055     char type[256], IFO[256], channel[256], Times[256];
00056     char File[1024];
00057     char *alias = (char *)calloc(256, sizeof(char));
00058     char *q, *T0, *T1;
00059 
00060     if(buf[0]!='\n') {
00061 
00062 #ifdef DEBUGBURST
00063       fprintf(stderr,"Processing: %s\n", buf);
00064 #endif
00065 
00066       /* Examine line
00067          Format: type IFO times channel alias */
00068       if(sscanf(buf,"%s\t%s\t%s\t%s\t%s\t%s",type,IFO,File,Times,channel,alias) == 6) {
00069 
00070         /* explicit filename provided */       
00071         FILE *out;
00072         char tname[] = CACHEFILENAME;
00073         char *p1, *p2;
00074         char tFile[1024];
00075 
00076         strcpy(tFile,File);
00077 
00078         p2 = strrchr(File,'.');
00079         if(!p2) {
00080           fprintf(stderr,"Invalid filename: %s\n",File);
00081           return 1;
00082         }
00083         *p2 = 0;
00084         p1 = strrchr(File,'-');
00085         if(!p1) {
00086           fprintf(stderr,"2-Invalid filename: %s\n",File);
00087           return 1;
00088         }
00089         p1++;
00090 
00091         q = strchr(Times,'-');
00092         if(!q) {
00093           fprintf(stderr,"Times: T0-T1\n");
00094           return 1;
00095         } 
00096         *q=0;
00097         T0 = Times;
00098         T1 = q+1;
00099 
00100         if((out = fopen(tname,"a"))==NULL) {
00101           fprintf(stderr,"Can't open %s\n",tname);
00102           return 1;
00103         }
00104         
00105         fprintf(out,"%s %s %s %s file://localhost/%s\n",IFO,type,T0,p1,tFile);
00106 
00107         fclose(out);
00108 
00109       } else {
00110 
00111         if(sscanf(buf,"%s\t%s\t%s\t%s\t%s",type,IFO,Times,channel,alias) != 5) {
00112           fprintf(stderr,"Malformed framequery\n");
00113           return 1;
00114         }
00115 
00116 
00117         q = strchr(Times,'-');
00118         if(!q) {
00119           fprintf(stderr,"Times: T0-T1\n");
00120           return 1;
00121         } 
00122         *q=0;
00123         T0 = Times;
00124         T1 = q+1;
00125 
00126         { /* get data */
00127           char tname[] = CACHEFILENAME;
00128           char cmd[1024];
00129           char *path;
00130           char *p;
00131 
00132           /* find the data */
00133           path = getenv("LSC_DATAGRID_CLIENT_LOCATION");
00134           if(!path) {
00135             fprintf(stderr,"Environment variable LSC_DATAGRID_CLIENT_LOCATION not set\n");
00136             return 1;
00137           }
00138       
00139           sprintf(cmd,"source %s/setup.sh; %s/ldg-client/bin/LSCdataFind --server %s --observatory %s --type %s --gps-start-time %s --gps-end-time %s --url-type file --lal-cache >> %s", path, path, dataserver, IFO, type, T0, T1, tname);
00140 
00141           if(system(cmd) == -1) {
00142             fprintf(stderr,"system call failed\n");
00143             perror("Error");
00144             return 1;
00145           }
00146 
00147         }
00148 
00149       }
00150     }
00151 
00152     /* update line being processed */
00153     if(p1) {
00154       p0 = p1+1;
00155       p1 = strchr(p0,'\n');
00156       bzero(buf,strlen(buf));
00157       if(p1) {
00158         memcpy(buf,p0,p1-p0+1);
00159       } else {
00160         strcpy(buf,p0);
00161       }
00162     } else {
00163       break;
00164     }
00165 
00166   }
00167 
00168   return 0;
00169 }
00170 
00171 int main(int argc, char *argv[]) {
00172 
00173   char *fQuery; /* frame data */
00174 
00175   char *times, *dataserver;
00176 
00177   if(argc<3) {
00178     fprintf(stderr,"buildcache dataserver framesQuery\n");
00179     return 1;
00180   }
00181 
00182   dataserver = argv[1];
00183   fQuery = argv[2];
00184 
00185 
00186   /*****************************************/
00187   /* parse parameters */
00188   /*****************************************/
00189   {
00190     /* frame query */
00191     struct stat buf;
00192 
00193     if(stat(fQuery,&buf)) {
00194       times = (char *)calloc(1 + strlen(fQuery), sizeof(char));
00195       strcpy(times, fQuery);
00196     } else {
00197       /* fQuery is a filename */
00198       FILE *in;
00199       char *p;
00200 
00201       if((in = fopen(fQuery,"r"))==NULL) {
00202         fprintf(stderr,"Can't open %s\n",fQuery);
00203         return 1;
00204       }
00205 
00206       p = times = (char *)calloc(1 + buf.st_size, sizeof(char));
00207 
00208       while((p=fgets(p,buf.st_size,in))) {
00209         p += strlen(p);
00210       }
00211 
00212       fclose(in);
00213     }
00214   }
00215 
00216 
00217   if(getFrameCache(times, dataserver)) {
00218     fprintf(stderr,"ERROR in getFrameCache\n");
00219     return 1;
00220   }
00221 
00222   return 0;
00223 }

Generated on Tue Sep 2 02:40:52 2008 for LAL by  doxygen 1.5.2