db.c
author Mahlon E. Smith <mahlon@martini.nu>
Mon, 07 Nov 2011 10:43:09 -0800
changeset 17 bd746609ba46
parent 16 e6a640ad2cc2
child 18 d4ce82194b64
permissions -rw-r--r--
Retain the CDB struct between lookups, only freeing when reopening the db. Big, big speed boost.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     1
/* vim: set noet nosta sw=4 ts=4 ft=c : */
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     2
/*
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     3
Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu>
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     4
All rights reserved.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     5
Redistribution and use in source and binary forms, with or without
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     6
modification, are permitted provided that the following conditions are met:
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     7
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     8
    * Redistributions of source code must retain the above copyright
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     9
      notice, this list of conditions and the following disclaimer.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    10
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    11
    * Redistributions in binary form must reproduce the above copyright
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    12
      notice, this list of conditions and the following disclaimer in the
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    13
      documentation and/or other materials provided with the distribution.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    14
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    15
    * Neither the name of Mahlon E. Smith nor the names of his
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    16
      contributors may be used to endorse or promote products derived
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    17
      from this software without specific prior written permission.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    18
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    19
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    20
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    21
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    22
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    23
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    24
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    25
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    26
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    27
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    28
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    29
*/
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    30
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    31
#include "volta.h"
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    32
#include "db.h"
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    33
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    34
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    35
/*
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    36
 * Open the database specified in the 'v' global struct,
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    37
 * setting the file descriptor.  Returns 0 on success.
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    38
 *
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    39
 */
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    40
short int
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    41
db_attach( void )
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    42
{
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    43
	/* only re-open the db at most every 10 seconds */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    44
	time_t now = time( NULL );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    45
	if ( v.timer.db_lastcheck > 0 ) {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    46
		if ( now - v.timer.db_lastcheck >= 10 ) {
17
bd746609ba46 Retain the CDB struct between lookups, only freeing when reopening the
Mahlon E. Smith <mahlon@martini.nu>
parents: 16
diff changeset
    47
			cdb_free( &v.db );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    48
			close( v.db_fd );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    49
		}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    50
		else {
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    51
			return( 0 );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    52
		}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    53
	}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    54
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    55
	debug( 2, LOC, "(Re)attaching to database '%s'\n", v.dbname );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    56
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    57
	/* db filename not set? */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    58
	if ( strlen(v.dbname) == 0 ) {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    59
		debug( 1, LOC, "Error when attaching to database: DB filename unset?\n" );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    60
		return( -1 );
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    61
	}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    62
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    63
	if ( (v.db_fd = open( v.dbname, O_RDONLY )) == -1 ) {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    64
		debug( 1, LOC, "Error when attaching to database: %s\n", strerror(errno) );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    65
		return( -1 );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    66
	}
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    67
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    68
	v.timer.db_lastcheck = now;
17
bd746609ba46 Retain the CDB struct between lookups, only freeing when reopening the
Mahlon E. Smith <mahlon@martini.nu>
parents: 16
diff changeset
    69
	cdb_init( &v.db, v.db_fd );
bd746609ba46 Retain the CDB struct between lookups, only freeing when reopening the
Mahlon E. Smith <mahlon@martini.nu>
parents: 16
diff changeset
    70
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    71
	return( 0 );
4
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    72
}
5701b7859a31 Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    73
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    74
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    75
/*
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    76
 * Given a rule file in ascii specified by the -c command line arg,
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    77
 * convert it to a cdb after checking rules for validity.
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    78
 *
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    79
 */
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    80
unsigned short int
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    81
db_create_new( char *txt )
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    82
{
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    83
	struct cdb_make cdbm;
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    84
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    85
	char buf[ LINE_BUFSIZE*10 ];
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    86
	char tmpfile[25];
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    87
	int  tmp_fd;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    88
	FILE *txt_f = NULL;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    89
	int  linenum = 0, parsed = 0;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    90
	struct db_input *dbline;
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
    91
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    92
	/* open temporary file */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    93
	debug( 0, LOC, "Creating/updating database (%s) using rules in \"%s\"\n", v.dbname, txt );
16
e6a640ad2cc2 Ensure that all output is flushed immediately.
Mahlon E. Smith <mahlon@martini.nu>
parents: 15
diff changeset
    94
	sprintf( tmpfile, "volta-db-%d.tmp", getpid() );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    95
	if ( (tmp_fd = open( tmpfile,
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    96
						 O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH )) == -1 ) {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    97
		debug( 0, LOC, "Error writing temporary file: %s\n", strerror(errno) );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    98
		return( 1 );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
    99
	}
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   100
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   101
	/* open rules file */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   102
	if ( (txt_f = fopen( txt, "r" )) == NULL ) {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   103
		debug( 0, LOC, "Error reading rules file: %s\n", strerror(errno) );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   104
		return( 1 );
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   105
	}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   106
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   107
	/* init struct and start parsing lines */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   108
	cdb_make_start( &cdbm, tmp_fd );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   109
	while ( fgets( buf, LINE_BUFSIZE*10, txt_f ) != NULL ) {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   110
		linenum++;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   111
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   112
		/* skip blank lines and comments */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   113
		if ( strlen(buf) == 1 || buf[0] == '#' ) continue;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   114
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   115
		/* validate and add! */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   116
		dbline = parse_dbinput( buf );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   117
		if ( dbline == NULL ) {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   118
			debug( 0, LOC, "Invalid rule (line %d), skipping: %s", linenum, buf );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   119
			continue;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   120
		}
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   121
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   122
		cdb_make_add( &cdbm, dbline->key, dbline->klen, dbline->val, dbline->vlen );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   123
		parsed++;
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   124
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   125
		free( dbline->key );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   126
		free( dbline->val );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   127
		free( dbline );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   128
	}
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   129
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   130
	/* write indexes */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   131
	fclose( txt_f );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   132
	cdb_make_finish( &cdbm );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   133
	close( tmp_fd );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   134
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   135
	/* move cdb into place */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   136
	if ( (rename( tmpfile, v.dbname )) == -1 ) {
16
e6a640ad2cc2 Ensure that all output is flushed immediately.
Mahlon E. Smith <mahlon@martini.nu>
parents: 15
diff changeset
   137
		debug( 0, LOC, "Unable to move temp cdb into place: %s", strerror(errno) );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   138
		return( 1 );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   139
	}
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   140
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   141
	debug( 0, LOC, "Added %d rules to %s.\n", parsed, v.dbname );
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   142
	return( 0 );
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   143
}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   144
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   145
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   146
/* 
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   147
 * Search the CDB for all occurances of the given +key+, checking
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   148
 * each one against the +p_request+.  First match wins and is
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   149
 * returned.  NULL on no match.
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   150
 *
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   151
 */
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   152
parsed *
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   153
find_rule( char *key, parsed *p_request )
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   154
{
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   155
	if ( key == NULL ) return( NULL );
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   156
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   157
	struct cdb_find cdbf; /* structure to hold current find position */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   158
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   159
	parsed *rule = NULL;
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   160
	char *val    = NULL;
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   161
	unsigned int vlen, vpos;
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   162
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   163
	/* initialize search structs */
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   164
	if ( db_attach() == -1 ) return( NULL );
17
bd746609ba46 Retain the CDB struct between lookups, only freeing when reopening the
Mahlon E. Smith <mahlon@martini.nu>
parents: 16
diff changeset
   165
	cdb_findinit( &cdbf, &v.db, key, (int)strlen(key) );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   166
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   167
	while ( cdb_findnext( &cdbf ) > 0 ) {
17
bd746609ba46 Retain the CDB struct between lookups, only freeing when reopening the
Mahlon E. Smith <mahlon@martini.nu>
parents: 16
diff changeset
   168
		vpos = cdb_datapos( &v.db );
bd746609ba46 Retain the CDB struct between lookups, only freeing when reopening the
Mahlon E. Smith <mahlon@martini.nu>
parents: 16
diff changeset
   169
		vlen = cdb_datalen( &v.db );
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   170
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   171
		/* pull the value from the db */
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   172
		if ( (val = calloc( vlen, sizeof(char) )) == NULL ) {
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   173
			debug( 5, LOC, "Unable to allocate memory for DB value storage: %s\n",
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   174
					strerror(errno) );
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   175
			return( NULL );
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   176
		}
17
bd746609ba46 Retain the CDB struct between lookups, only freeing when reopening the
Mahlon E. Smith <mahlon@martini.nu>
parents: 16
diff changeset
   177
		cdb_read( &v.db, val, vlen, vpos );
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   178
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   179
		/* check it against the request */
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   180
		debug( 4, LOC, "DB match for key '%s': %s\n", key, val );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   181
		rule = parse_rule( val );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   182
		free( val );
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   183
		if ( rule != NULL ) {
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   184
			if ( check_rule( rule, p_request ) == 0 ) {
16
e6a640ad2cc2 Ensure that all output is flushed immediately.
Mahlon E. Smith <mahlon@martini.nu>
parents: 15
diff changeset
   185
				finish_parsed( rule ), rule = NULL;
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   186
			}
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   187
			else {
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   188
				break;
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   189
			}
14
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   190
		}
51eb85ae4de4 There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents: 13
diff changeset
   191
	}
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   192
15
2706fc514dea Add whitelisting rules, to negate other matches if they come first in
Mahlon E. Smith <mahlon@martini.nu>
parents: 14
diff changeset
   193
	return( rule );
13
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   194
}
23a242d7b7fa 1st iteration of volta actually doing something. Process the request,
Mahlon E. Smith <mahlon@laika.com>
parents: 10
diff changeset
   195