示例#1
0
文件: db.c 项目: TobyGoodwin/bfilter
// open the database, read-only or read/write as specified by the flag. if the
// database is missing: create in write mode, return 0 in read mode.
static _Bool db_open(_Bool write) {
    char *name;
    int err, flags;

    name = dbfilename(NULL);

    if (write)
        flags = SQLITE_OPEN_READWRITE;
    else
        flags = SQLITE_OPEN_READONLY;

    err = sqlite3_open_v2(name, &db, flags, 0);

    if (err != SQLITE_OK && errno == ENOENT) {
        if (!write) return 0;
        flags |= SQLITE_OPEN_CREATE;
        err = sqlite3_open_v2(name, &db, flags, 0);
        if (err == SQLITE_OK)
            db_init();
    }

    if (err != SQLITE_OK) {
        fatal4("cannot open database `", name, "': ", sqlite3_errmsg(db));
        sqlite3_close(db);
    }

    sqlite3_extended_result_codes(db, 1);

    db_check_version();
    return 1;
}
示例#2
0
int
main( int argc, char **argv )
{
	int	c;
	int	errflag = 0;
	int	orb;
	int	stop = 0;
	long	nrecs;
	char	*match = ".*/pf/st";
	char	*from = 0;
	char	*statefile = 0;
	char	*pfname = "orb2rrdc";
	char	*orbname;
	char	*dbcache;
	char	*rrdtool;
	char	command[STRSZ];
	char	net[STRSZ];
	char	sta[STRSZ];
	char	rrdvar[STRSZ];
	char	key[STRSZ];
	char	path[FILENAME_MAX];
	Dbptr	db;
	Dbptr	dbt;
	Pf	*pf;
	char	*Default_network;
	Tbl	*dlslines;
	Arr	*Dls_vars_dsparams;
	Arr	*Dls_vars_rras;
	Tbl	*Dls_vars_keys;
	char	*line;
	char	*dls_var;
	char	*dsparams;
	Tbl	*rras;
	int	i;
	int	j;
	OrbreapThr *ort;
	int	pktid;
	char	srcname[ORBSRCNAME_SIZE];
	double	time = 0;
	char	*packet = 0;
	int	nbytes = 0;
	int	bufsize = 0;
	Packet	*pkt = 0;
	int	rc;
	char	*s;
	Pf	*dlspf;
	Tbl	*dlspfkeys;
	char	*element;
	Tbl	*parts;
	double	val;
	Pf	*pfval = 0;

	elog_init( argc, argv );

	while( ( c = getopt( argc, argv, "vVd:s:p:m:f:" ) ) != -1 ) {

		switch( c ) {

		case 'd':
			CacheDaemon = optarg;
			break;

		case 'f':
			from = optarg;
			break;

		case 'm':
			match = optarg;
			break;

		case 'p': 
			pfname = optarg;
			break;

		case 's':
			statefile = optarg;
			break;
			
		case 'v':
			Verbose++;
			break;

		case 'V':
			VeryVerbose++;
			Verbose++;
			break;
		
		default:
			elog_complain( 0, "Unknown option '%c'\n", c );
			errflag++;
			break;
		}
	}

	if( errflag || argc - optind != 2 ) {

		usage();
	}

	if( Verbose ) {

		elog_notify( 0, "Starting at %s (%s $Revision$ $Date$)\n", 
				zepoch2str( str2epoch( "now" ), "%D %T %Z", "" ),
				Program_Name );
	}

	orbname = argv[optind++];
	dbcache = argv[optind++];

	pfread( pfname, &pf );

	rrdtool = pfget_string( pf, "rrdtool" );

	if( rrdtool == NULL || ! strcmp( rrdtool, "" ) ) {

		elog_die( 0, "Error: no rrdtool executable name specified in parameter file\n" );

	} else if( ( rrdtool[0] == '/' && ! is_present( rrdtool ) ) || ( rrdtool[0] != '/' && ! datafile( "PATH", rrdtool ) ) ) {

		elog_die( 0, "Error: can't find rrdtool executable by name of '%s' (check PATH environment " 
			"variable, or absolute path name if given)\n", rrdtool );

	} else if( rrdtool[0] == '/' ) {

		sprintf( command, "%s -", rrdtool );

	} else {

		sprintf( command, "rrdtool -" );
	}

	Suppress_egrep = pfget_string( pf, "suppress_egrep" );

	if( Suppress_egrep != NULL && strcmp( Suppress_egrep, "" ) ) {
		
		if( ! datafile( "PATH", "egrep" ) ) {

			elog_complain( 0, "Ignoring suppress_egrep parameter: can't find egrep on path\n" ); 

		} else {

			sprintf( command, "%s 2>&1 | egrep -v '%s'", command, Suppress_egrep );
		}
	}

	if( VeryVerbose ) {

		elog_notify( 0, "Executing command: %s\n", command );
	}

	Rrdfp = popen( command, "w" );

	if( Rrdfp == (FILE *) NULL ) {

		elog_die( 0, "Failed to open socket to rrdtool command\n" );
	}

	orb = orbopen( orbname, "r&" );

	if( orb < 0 ) {

		elog_die( 0, "Failed to open orb '%s' for reading. Bye.\n", orbname );
	}

	orbselect( orb, match );

	if( from != NULL && statefile == NULL ) {

		pktid = orbposition( orb, from );

		if( Verbose ) {

			elog_notify( 0, "Positioned to packet %d\n", pktid );
		}

	} else if( from != NULL ) {

		elog_complain( 0, "Ignoring -f in favor of existing state file\n" );
	}

	if( statefile != NULL ) {

		stop = 0;

		exhume( statefile, &stop, 15, 0 );

		orbresurrect( orb, &pktid, &time );

		if( Verbose ) {

			elog_notify( 0, "Resurrecting state to pktid %d, time %s\n",
				pktid, s = strtime( time ) );

			free( s );
		}

		orbseek( orb, pktid );
	}

	dbopen( dbcache, "r+", &db );

	if( db.database < 0 ) {
		
		elog_die( 0, "Failed to open cache database '%s'. Bye.\n", dbcache );

	} else {
		
		db = dblookup( db, "", "rrdcache", "", "" );

		if( db.table < 0 ) {

			elog_die( 0, "Failed to lookup 'rrdcache' table in '%s'. Bye.\n", dbcache );
		}
	}

	dbcrunch( db );

	dbt = dbsubset( db, "endtime == NULL", NULL );

	Rrd_files = newarr( 0 );

	dbquery( dbt, dbRECORD_COUNT, &nrecs );

	for( dbt.record = 0; dbt.record < nrecs; dbt.record++ ) {

		dbgetv( dbt, 0, "net", &net, "sta", &sta, "rrdvar", &rrdvar, NULL );

		dbfilename( dbt, (char *) &path );

		sprintf( key, "%s:%s:%s", net, sta, rrdvar );

		if( ! is_present( path ) ) {
			
			elog_complain( 0, "WARNING: rrd file '%s', listed in database, does not exist. "
				"Removing database entry.\n", path );

			dbmark( dbt );

		} else {

			setarr( Rrd_files, key, strdup( path ) );

			if( VeryVerbose ) {

				elog_notify( 0, "Re-using rrd file '%s' for '%s'\n", path, key );
			}
		}
	}

	Rrdfile_pattern = pfget_string( pf, "rrdfile_pattern" );
	Status_stepsize_sec = pfget_double( pf, "status_stepsize_sec" );
	Default_network = pfget_string( pf, "default_network" );
	dlslines = pfget_tbl( pf, "dls_vars" );

	Dls_vars_dsparams = newarr( 0 );
	Dls_vars_rras = newarr( 0 );

	for( i = 0; i < maxtbl( dlslines ); i++ ) {
		
		line = gettbl( dlslines, i );
		
		strtr( line, "\t", " " );
		rras = split( line, ' ' );

		dls_var = shifttbl( rras );
		dsparams = shifttbl( rras );

		setarr( Dls_vars_dsparams, dls_var, dsparams );
		setarr( Dls_vars_rras, dls_var, rras );
	}

	ort = orbreapthr_new( orb, -1., 0 );

	for( ; stop == 0; ) {

		orbreapthr_get( ort, &pktid, srcname, &time, &packet, &nbytes, &bufsize );

		if( statefile ) {

			rc = bury();

			if( rc < 0 ) {

				elog_complain( 0, "Unexpected failure of bury command! " 
					"(are there two orb2rrdc's running with the same state" 
					"file?)\n" );

				clear_register( 1 );
			}
		}

		rc = unstuffPkt( srcname, time, packet, nbytes, &pkt );

		if( rc == Pkt_pf ) {

			if( VeryVerbose ) {

				/* Parameter files generally too big for elog */

				fprintf( stderr, "Received a parameter-file '%s' at %s\n%s\n\n", 
						srcname, 
						s = strtime( time ), 
						pf2string( pkt->pf ) );

				free( s );

			} else if( Verbose ) {

				elog_notify( 0, "Received a parameter-file '%s' at %s\n", 
						srcname, s = strtime( time ) );

				free( s );
			}

			pfmorph( pkt->pf );

			if( VeryVerbose ) {

				fprintf( stderr, "Morphed parameter-file '%s' to interpret 'opt':\n%s\n\n", 
						srcname, 
						pf2string( pkt->pf ) );
			}

			pfget( pkt->pf, "dls", (void **) &dlspf );

			dlspfkeys = pfkeys( dlspf );
			Dls_vars_keys = keysarr( Dls_vars_dsparams );

			for( i = 0; i < maxtbl( dlspfkeys ); i++ ) {
			   
			   element = gettbl( dlspfkeys, i );

			   if( strcontains( element, "_", 0, 0, 0 ) ) {

				parts = split( (s = strdup( element )), '_' );

				sprintf( net, "%s", (char *) gettbl( parts, 0 ) );
				sprintf( sta, "%s", (char *) gettbl( parts, 1 ) );

				free( s );
				freetbl( parts, 0 );

			   } else {

				sprintf( net, "%s", Default_network );

				sprintf( sta, "%s", element );
			   }

			   for( j = 0; j < maxtbl( Dls_vars_keys ); j++ ) {

			   	dls_var = gettbl( Dls_vars_keys, j );

				sprintf( key, "%s{%s}", element, dls_var );

				if( pfresolve( dlspf, key, 0, &pfval ) < 0 ) {

					elog_complain( 0, "Unable to extract variable '%s' "
						"(not present or wrong type) from element '%s' "
						"in packet from '%s', timestamped '%s'; Skipping\n",
						key, element, srcname, s = strtime( time ) );

					free( s );

					pfval = 0;

					continue;

				} else if( pfval != (Pf *) NULL &&
					   pfval->value.s != (char *) NULL &&
					   ! strcmp( pfval->value.s, "-" ) ) {

					if( VeryVerbose ) {

						elog_notify( 0, "Non-floating point value '-' in variable '%s', "
							"in packet from '%s', timestamped '%s'; Skipping data point\n",
							key, srcname, s = strtime( time ) );

						free( s );
					}

					continue;

				} else {

					val = pfget_double( dlspf, key );
				}

				archive_dlsvar( db, net, sta, dls_var, 
						(char *) getarr( Dls_vars_dsparams, dls_var ),
						(Tbl *) getarr( Dls_vars_rras, dls_var ),
						time, val );
			   }

			}

			freetbl( dlspfkeys, 0 );
			freetbl( Dls_vars_keys, 0 );

		} else if( rc == Pkt_stash ) {

			; /* Do nothing */

		} else {

			if( Verbose ) {

				elog_notify( 0, "Received a packet that's not a parameter file " 
					"(type '%d' from unstuffPkt); skipping\n", rc );
			}
		}
	}
}
long writePolygonData(Dbptr db, Point *poly, long npoints, char *pname, int closed, int level, char *ptype, char *auth, char *dir, char *dfile, int pcode) {
	/* writes poygon data into file
   		if dir and o dfile are NULL, use defaults
		returns record number if successful,dbINVALID else
	 */		
	long i;
	char datafilename[PATH_MAX];
	FILE *dfh;
	int foff;
	char *ftype;
	int int1,int2;
	float float1,float2;
	double north=-360.0,east=-360.0,west=360.0,south=360.0;
	double lat,lon;
	int pid;
	int free_dir=0;
	int free_dfile=0;
	char closed_flag[2];

	if (closed) {
		strcpy(closed_flag,"y");
	} else {
		strcpy(closed_flag,"n");
	}

	pid=dbnextid(db,"pid");
	
	db=dblookup(db, 0, "polygon", 0, 0);
	if ( (db.record=dbaddnull(db) ) ==dbINVALID) {
		elog_log(0,"writePolygonData: problem adding row...");
		return dbINVALID;
	}
	
	if (dir == NULL) {
		dir = strdup(default_dir);
		free_dir=1;
	}
	if (dfile == NULL) {
		dfile = strdup(default_dfile);
		free_dfile=0;
	}

	if (dbputv(db,0,"pid",pid,"dir",dir,"dfile",dfile,NULL ) == dbINVALID) {
		elog_log(0,"writePolygonData: error putting dir %s & dfile %s",dir,dfile);
		if (free_dir) free(dir);
		if (free_dfile) free(dfile);
		return dbINVALID;
	}
	/* some fix needed here when in "relative" directories...*/
	switch (dbfilename(db,datafilename)) {
		case -1:
			if (makedir(dir)<0) {
				elog_log(0,"writePolygonData: dir %s is NOT writable!",dir);
				if (free_dir) free(dir);
				if (free_dfile) free(dfile);
				return dbINVALID;
			}
			if ((dfh= fopen(datafilename,"w"))==NULL) {
				elog_log(0,"writePolygondata: error crating %s/%s",dir,dfile);
				if (free_dir) free(dir);
				if (free_dfile) free(dfile);
				return dbINVALID;
				}
			foff= 0;
			break;
		case 0:
			/*make file*/
			if (makedir(dir)<0) {
				elog_log(0,"problem creating directory %s",dir);
				if (free_dir) free(dir);
				if (free_dfile) free(dfile);
				return dbINVALID;
			}
			if ((dfh= fopen(datafilename,"w"))==NULL) {
				elog_log(0,"problem creating file %s",dir);
				if (free_dir) free(dir);
				if (free_dfile) free(dfile);
				return dbINVALID;
			}
			foff= 0;
			break;
		case 1:
			/*append*/	
			dfh= fopen(datafilename,"a");
			fseek(dfh,0,2);
			foff= ftell(dfh);
			break;
		case 2:
			/*.Z exists....*/
			elog_log(0,"writePolygonData: compression not yet supported...");
			if (free_dir) free(dir);
			if (free_dfile) free(dfile);
			return dbINVALID;
			break;
		default:
			elog_log(0,"writePolygondata: unknown return value from dbfilename, giving up...");
			if (free_dir) free(dir);
			if (free_dfile) free(dfile);
			return dbINVALID;	
			
	}
	ftype = polytype(pcode);
	
	switch (pcode) {
		case polyINT:
			for (i=0;i < npoints;i++) {
				lon=poly[i].lon;
				west = (lon<west) ? lon : west;
				east = (lon>east) ? lon : east;
				int1=lon * 1e6;
				H2N4((char *)(&int2),(char *)(&int1),1);
				fwrite(&int2,sizeof(int),1,dfh);

				lat=poly[i].lat;
				south = (lat < south) ? lat : south;
				north = (lat > north) ? lat : north;
				int1=lat * 1e6;
				H2N4((char *)(&int2),(char *)(&int1),1);
				fwrite(&int2,sizeof(int),1,dfh);
			}
			fclose(dfh);
			break;
		case polyINTELINT:
			for (i=0;i < npoints;i++) {
				lon=poly[i].lon;
				west = (lon<west) ? lon : west;
				east = (lon>east) ? lon : east;
				int1=lon * 1e6;
				#ifdef WORDS_BIGENDIAN
					swap4(&int1,&int2,1);
				#else
					int2=int1;
				#endif
				fwrite(&int2,sizeof(int),1,dfh);

				lat=poly[i].lat;
				south = (lat < south) ? lat : south;
				north = (lat > north) ? lat : north;
				int1=lat * 1e6;
				#ifdef WORDS_BIGENDIAN
					swap4(&int1,&int2,1);
				#else
					int2=int1;
				#endif
				fwrite(&int2,sizeof(int),1,dfh);
			}
			fclose(dfh);
			break;
		case polyFLOAT:
			for (i=0;i < npoints;i++) {
				lon=poly[i].lon;
				west = (lon<west) ? lon : west;
				east = (lon>east) ? lon : east;
				float1=lon;
				H2N4((char *)(&float2),(char *)(&float1),1);
				fwrite(&float2,sizeof(float),1,dfh);

				lat=poly[i].lat;
				south = (lat < south) ? lat : south;
				north = (lat > north) ? lat : north;
				float1=lat;
				H2N4((char *)(&float2),(char *)(&float1),1);
				fwrite(&float2,sizeof(float),1,dfh);
			}
			fclose(dfh);
			break;	
		case polyINTELFLOAT:
			for (i=0;i < npoints;i++) {
				lon= poly[i].lon;
				west= (lon<west) ? lon : west;
				east= (lon>east) ? lon : east;
				float1=lon;
				#ifdef WORDS_BIGENDIAN
					swap4(&float1,&float2,1);
				#else	
					float2= float1;
				#endif
				fwrite(&float2,sizeof(float),1,dfh);
				lat= poly[i].lat;
				south= (lat < south) ? lat : south;
				north= (lat > north) ? lat : north;
				float1=lat;
				#ifdef WORDS_BIGENDIAN
					swap4(&float1,&float2,1);
				#else	
					float2= float1;
				#endif
				fwrite(&float2,sizeof(float),1,dfh);
			}
			fclose(dfh);
			break;	
		case polyGSHHS:
			elog_log(0,"writePolygonData: storage type %s not yet implemented... ",ftype);
			if (free_dir) free(dir);
			if (free_dfile) free(dfile);
			return dbINVALID;
			break;
		default:
			elog_log(0,"writePolygonData: unknown storage type pcode, giving up");
			if (free_dir) free(dir);
			if (free_dfile) free(dfile);
			return dbINVALID;
	}	
	if (dbputv(db,0,"north",north, "east",east, "south",south, "west",west,
			"pname",pname,"closed",closed_flag,"level",level,"ptype",ptype, "auth",auth,"npoints",npoints, "ftype",ftype,"foff",foff,NULL ) == dbINVALID) {
		elog_log(0,"writePolygonData: error putting values!");
		//free (ftype);
		if (free_dir) free(dir);
		if (free_dfile) free(dfile);
		return dbINVALID;
	} else {
		//free (ftype);
		if (free_dir) free(dir);
		if (free_dfile) free(dfile);
		return db.record;
	}
}
示例#4
0
Trace *
off_read_trace (Dbptr db, double tstart, double tend)

{
    char fname[1024];
    char dtype[8];
    char segtype[8];
    long foff, nsamp;
    void *data;
    Trace *trace;
    double time, endtime, samprate;
    double calib, calper;
    int isamp, jsamp, size, nbytec;
    int ret;

    dbgetv (db, 0, "time", &time, "endtime", &endtime,
            "samprate", &samprate,
            "nsamp", &nsamp, "datatype", dtype,
            "segtype", segtype, "foff", &foff,
            "calib", &calib, "calper", &calper, NULL);
    switch (dtype[0]) {
    default:
        break;
    case 'c':
        nbytec = nsamp;
        nsamp = (endtime - time)*samprate + 1.5;
        break;
    }
    isamp = (tstart - time)*samprate - 1.5;
    jsamp = (tend - time)*samprate + 1.5;
    if (isamp < 0) isamp = 0;
    if (jsamp > nsamp-1) jsamp = nsamp-1;
    nsamp = jsamp+1;
    size = atoi(&dtype[strlen(dtype)-1]);
    nsamp -= isamp;
    time += isamp/samprate;
    if (nsamp < 0) nsamp = 0;
    data = NULL;
    if (nsamp > 0) {
        if (dbfilename (db, fname) < 1) {
            fprintf (stderr, "read_trace: Unable to find input file '%s'\n",
                     fname);
            nsamp = 0;
            data = NULL;
        } else {
            switch (dtype[0]) {
            default:
                foff += isamp*size;
                if (!read_file (fname, foff, dtype, &nsamp, &data)) {
                    fprintf (stderr, "read_trace: Unable to read input file '%s'\n",
                             fname);
                    return (NULL);
                }
                break;
            case 'c':
                data = malloc (nsamp*size);
                if (data == NULL) {
                    fprintf (stderr, "read_trace: malloc() error.\n");
                    return (NULL);
                }
                dtype[0] = 's';
                ret = wf_read_idacompress (fname, foff, nbytec, isamp, nsamp, size, data);
                if (ret < 0) {
                    fprintf (stderr, "read_trace: wf_read_idacompress() error.\n");
                    free (data);
                    return (NULL);
                }
                if (ret < nsamp) nsamp = ret;
                break;
            }
        }
    }
    trace = (Trace *) malloc (sizeof(Trace));
    if (trace == NULL) {
        fprintf (stderr, "read_trace: Malloc error on Trace structure.\n");
        free (data);
        return (NULL);
    }
    trace->tstart = time;
    trace->dt = 1.0/samprate;
    trace->nsamps = nsamp;
    trace->calib = calib;
    trace->calper = calper;
    strcpy (trace->rawdata_format, dtype);
    strcpy (trace->rawdata_type, segtype);
    trace->data = NULL;
    trace->data_free = NULL;
    trace->data_malloc = 0;
    trace->raw_data = data;
    trace->rawdata_free = data;
    if (data) trace->rawdata_malloc = nsamp*size;
    else trace->rawdata_malloc = 0;
    trace->prev = NULL;
    trace->next = NULL;
    if (data) trace = (Trace *) SCV_trace_fixgaps (trace, "segment");
    return (trace);
}