[moodns-cvs] CVS: moodns/src database.h,1.1,1.2 database.c,1.1,1.2 database-ht.c,1.1,1.2 database-gd
Status: Alpha
Brought to you by:
mawolf
From: Michael W. <ma...@us...> - 2002-01-27 10:19:20
|
Update of /cvsroot/moodns/moodns/src In directory usw-pr-cvs1:/tmp/cvs-serv7108 Modified Files: database.h database.c database-ht.c database-gdbm.c Log Message: Slightly less toyish. Index: database.h =================================================================== RCS file: /cvsroot/moodns/moodns/src/database.h,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** database.h 2002/01/17 13:07:01 1.1 --- database.h 2002/01/27 10:19:17 1.2 *************** *** 33,54 **** enum database_type type; lt_dlhandle dl_handle; ! /* Open the file containing the database. */ int (*open_file)(database *, const char * /* path */); ! /* Do what it takes so that a record can be found w/ get_record. */ int (*load)(database *); ! /* FIXME: This should probably return a record_chain instead. */ ! struct record *(*get_record)(database *, const char *); ! /* Put a record into the database. */ ! /* FIXME: This might need to be adapted to use a record_chain instead. */ int (*put_record)(database *, record *); ! /* Initially for debugging purposes; might be useful for other stuff too, eg when a database exists but the text file master is gone. */ void (*database_dump)(database *); ! /* eg database handle, ht ptr */ void *database_handle; --- 33,56 ---- enum database_type type; lt_dlhandle dl_handle; ! /* Open the file containing the database. */ int (*open_file)(database *, const char * /* path */); ! /* Do what it takes so that a record can be found w/ get_record. */ int (*load)(database *); ! /* FIXME: This should probably return a record_chain instead. */ ! struct record_chain *(*get_records)(database *, const char *); ! /* Put a record into the database. */ ! /* FIXME: This might need to be adapted to use a record_chain instead. ! Nope, it won't. We grab the record chain, grow it, and put it back. ! We only need a record to do that. */ int (*put_record)(database *, record *); ! /* Initially for debugging purposes; might be useful for other stuff too, eg when a database exists but the text file master is gone. */ void (*database_dump)(database *); ! /* eg database handle, ht ptr */ void *database_handle; Index: database.c =================================================================== RCS file: /cvsroot/moodns/moodns/src/database.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** database.c 2002/01/17 13:07:01 1.1 --- database.c 2002/01/27 10:19:17 1.2 *************** *** 58,63 **** database /*_generic*/ *retval; lt_ptr_t ifhtp; /* This stands for "I fucking hate typedeffed pointers". */ ! retval = (database /*_generic*/ *) xcalloc(1, sizeof(database_generic)); ! wpthread_mutex_lock(<dl_mutex); if (globals.general.modules_path != NULL) --- 58,64 ---- database /*_generic*/ *retval; lt_ptr_t ifhtp; /* This stands for "I fucking hate typedeffed pointers". */ ! // retval = (database /*_generic*/ *) xcalloc(1, sizeof(database_generic)); ! retval = (database *) xcalloc(1, sizeof(database)); ! // memset(retval, 0, sizeof(*retval)); // wtf? wpthread_mutex_lock(<dl_mutex); if (globals.general.modules_path != NULL) *************** *** 145,149 **** printf("ret is %d\n", ret); ! d->open_file(d, "../data/doc"); } --- 146,150 ---- printf("ret is %d\n", ret); ! d->open_file(d, "../doc/data"); } Index: database-ht.c =================================================================== RCS file: /cvsroot/moodns/moodns/src/database-ht.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** database-ht.c 2002/01/17 13:07:01 1.1 --- database-ht.c 2002/01/27 10:19:17 1.2 *************** *** 20,28 **** /* $Header$ */ #include <stdio.h> #include "ht.h" - #include "record.h" #include "database.h" --- 20,41 ---- /* $Header$ */ + /* When we're using a ht as the database, we don't longer have funky + marshalling requirements like we do with db4 and gdbm. The table instead + store pointers directly to `struct record_chain's. */ + + + #ifdef HAVE_CONFIG_H + # include "../config.h" + #endif + #include <stdio.h> + #include <assert.h> + #include <string.h> + #include <stdlib.h> #include "ht.h" #include "record.h" + #include "auth_data.h" + #include "database.h" *************** *** 31,34 **** --- 44,49 ---- # include <dmalloc.h> #endif + + #define CAST(x) ((struct ht_handle *) x) struct database_ht { *************** *** 36,46 **** }; static enum database_type _this_type(void); static int _ht_open_file(database *, const char *); static int _ht_load(database *); ! static record *_ht_get_record(database *, const char *); static int _ht_put_record(database *, record *); /* static */ void hello_module(void); int database_module_init(database *d) --- 51,73 ---- }; + struct ht_handle { + FILE *f; + ht *table; + + }; + static enum database_type _this_type(void); static int _ht_open_file(database *, const char *); static int _ht_load(database *); ! static struct record_chain *_ht_get_records(database *, const char *); static int _ht_put_record(database *, record *); /* static */ void hello_module(void); + int _ht_slap(struct record *, void *); + + int _strcmp_local(void *, void *, void *); + void _free_local(void *, void *); + void strprint(void *); + int database_module_init(database *d) *************** *** 50,54 **** d->open_file = _ht_open_file; d->load = _ht_load; ! d->get_record = _ht_get_record; d->put_record = _ht_put_record; return retval; --- 77,81 ---- d->open_file = _ht_open_file; d->load = _ht_load; ! d->get_records = _ht_get_records; d->put_record = _ht_put_record; return retval; *************** *** 56,60 **** ! enum database_type _this_type() { --- 83,87 ---- ! enum database_type _this_type() { *************** *** 65,72 **** _ht_open_file(database *d, const char *path) { printf("About to open %s\n", path); /* See parsing routines from auth_data.c. Probably should break that down a bit. Hmm. */ ! return 0; } --- 92,114 ---- _ht_open_file(database *d, const char *path) { + int retval; + FILE *ff; + printf("About to open %s\n", path); /* See parsing routines from auth_data.c. Probably should break that down a bit. Hmm. */ ! assert(d->database_handle == NULL); ! d->database_handle = (struct ht_handle *) xcalloc(1, sizeof(struct ht_handle)); ! CAST(d->database_handle)->table = ht_init(_strcmp_local, _free_local); ! ff = fopen(path, "r"); ! if (ff == NULL) { ! perror("fopen failed"); ! abort(); ! } else { ! retval = 0; ! } ! CAST(d->database_handle)->f = ff; ! ! return retval; } *************** *** 74,84 **** _ht_load(database *d) { ! return 0; } ! record * ! _ht_get_record(database *d, const char *key) { ! return NULL; } --- 116,139 ---- _ht_load(database *d) { ! int retval; ! int ret; ! ! ret = load_auth_data_f(CAST(d->database_handle)->f, _ht_slap, ! CAST(d->database_handle)->table); ! retval = ret; ! return retval; } ! struct record_chain * ! _ht_get_records(database *d, const char *key) { ! /* FIXME: Uhm, yeah. */ ! struct record_chain *retval; ! struct record_chain *ch; ! ! ch = ht_get(CAST(d->database_handle)->table, key); ! retval = ch; ! ! return retval; } *************** *** 86,90 **** _ht_put_record(database *d, record *rec) { ! return 0; } --- 141,151 ---- _ht_put_record(database *d, record *rec) { ! int retval; ! struct record_chain *ch; ! ! ch = ht_get(CAST(d->database_handle)->table, rec->name); ! retval = record_chain_add(ch, rec); ! ! return retval; } *************** *** 95,96 **** --- 156,207 ---- printf("Yo! I'm the libdatabase-ht.la module\n"); } + + + /* FIXME: this function uses a bastardised combination of malloc and alloca. + Eeet's reeeel faaaachted aaaap! (I think the calls to alloca need to + go.) */ + int + _ht_slap(struct record *rec, void *_abc) + { + struct record_chain *ch; + ht *abc = (ht *) _abc; + + RECORD_CHAIN_INIT_TMP(ch, rec->name); + + printf("rec->name is %s\n", rec->name); + + ch = ht_get(abc, rec->name); + if (ch != NULL) { + printf("it's there already.\n"); + record_chain_add(ch, rec); + /* Thanks to some shallow copying, there's no need to put this back in + the table. */ + } else { + printf("it's not found\n"); + record_chain_add(ch, rec); + ht_put(abc, rec->name, ch, NULL); + } + return 0; + } + + + int + _strcmp_local(void *_a, void *_b, void *_c __attribute__((unused))) + { + return (strcmp((char *) _a, (char *) _b)); + } + + void + _free_local(void *_a, void *_b __attribute__((unused))) + { + xfree(_a); + } + + void + strprint(void *_arg) + { + char *arg; + arg = (char *) _arg; + printf("%s", arg); + } + Index: database-gdbm.c =================================================================== RCS file: /cvsroot/moodns/moodns/src/database-gdbm.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** database-gdbm.c 2002/01/17 13:07:01 1.1 --- database-gdbm.c 2002/01/27 10:19:17 1.2 *************** *** 24,29 **** #endif - #if defined(HAVE_GDBM) - #include <gdbm.h> #include <stdio.h> --- 24,27 ---- *************** *** 40,46 **** #endif ! struct database_gdbm { ! ! }; --- 38,66 ---- #endif ! ! ! #ifndef HAVE_GDBM ! ! ! /* If you do not check the return value, then you will DIE! */ ! int ! database_module_init(database *d) ! { ! int retval = 1; ! xfree(d); ! return (retval); ! } ! ! #endif ! ! /* Limbo. */ ! ! #if defined(HAVE_GDBM) ! ! #define CAST(x) ((struct ht_handle *) x) ! ! struct gdbm_handle { ! FILE *f; ! GDBM_FILE *table; }; *************** *** 53,60 **** static int _gdbm_open_file(database *, const char *); static int _gdbm_load(database *); ! static record *_gdbm_get_record(database *, const char *); static int _gdbm_put_record(database *, record *); /* static */ void hello_module(void); int --- 73,81 ---- static int _gdbm_open_file(database *, const char *); static int _gdbm_load(database *); ! static struct record_chain *_gdbm_get_records(database *, const char *); static int _gdbm_put_record(database *, record *); /* static */ void hello_module(void); + int _gdbm_slap(struct record *, void *); int *************** *** 65,69 **** d->open_file = _gdbm_open_file; d->load = _gdbm_load; ! d->get_record = _gdbm_get_record; d->put_record = _gdbm_put_record; --- 86,90 ---- d->open_file = _gdbm_open_file; d->load = _gdbm_load; ! d->get_record = _gdbm_get_records; d->put_record = _gdbm_put_record; *************** *** 80,85 **** _gdbm_open_file(database *d, const char *path) { ! printf("unimplemented function\n"); ! return 0; } --- 101,122 ---- _gdbm_open_file(database *d, const char *path) { ! int retval; ! FILE *ff; ! ! printf("About to open %s\n", path); ! assert(d->database_handle == NULL); ! d->database_handle = (struct gdbm_handle) xcalloc(1, sizeof(struct ht_handle)); ! CAST(d->database_handle)->table = gdbm_open("data.gdbm", 0, GDBM_WCREAT, ! S_IRUSR | S_IWUSR, NULL); ! ff = fopen(path, "r"); ! if (ff == NULL) { ! perror("fopen failed"); ! abort(); ! } else { ! retval = 0; ! } ! CAST(d->database_handle)->f = ff; ! ! return retval; } *************** *** 87,99 **** _gdbm_load(database *d) { ! printf("unimplemented function\n"); ! return 0; } ! record * ! _gdbm_get_record(database *d, const char *key) { ! printf("unimplemented function\n"); ! return NULL; } --- 124,163 ---- _gdbm_load(database *d) { ! int retval; ! int ret; ! ! ret = load_auth_data_f(CAST(d->database_handle)->f, _gdbm_slap, ! CAST(d->database_handle)->table); ! retval = ret; ! ! return retval; } ! ! ! ! struct record_chain * ! _gdbm_get_records(database *d, const char *name) { ! struct record_chain *retval; ! struct record_chain *ch; ! ! datum key, data; ! ! key.dptr = name; ! key.dsize = strlen(name); ! ! if (gdbm_exists(CAST(d->database_handle)->table, key)) { ! ch = record_chain_init(name); ! ! data = gdbm_fetch(CAST(d->database_handle)->table, key); ! ch->records = data.dptr; ! ch->records_len = data.dsize / sizeof(struct record); ! ! retval = ch; ! } else { ! retval = NULL; ! } ! return retval; } *************** *** 110,113 **** --- 174,216 ---- printf("Hello, I am the libdatabase-gdbm.so module\n"); } + + + + int + _gdbm_slap(struct record *rec, void *_abc) + { + struct record *tmp; + struct record_chain *ch; + GDBM_FILE *abc = (GDBM_FILE *) _abc; + datum key; + datum data, data2; + + key.dptr = rec->name; + key.dsize = strlen(rec->name); + + + RECORD_CHAIN_INIT_TMP(ch, rec->name); + + if (gdbm_exists(*abc, key) != 0) { + printf("It's already there\n"); + data = gdbm_fetch(*abc, key); + printf("We got a record chain of size %d\n", data.dsize); + ch->records = (struct record *) data.dptr; + ch->records_len = data.dsize / sizeof(struct record); + record_chain_add(ch, rec); + data2.dptr = (char *) ch->records; + data2.dsize = ch->records_len * sizeof(struct record); + ret = gdbm_store(*abc, key, data2, GDBM_REPLACE); + } else { + printf("it's not there, so let's add it\n"); + record_chain_add(ch, rec); + data.dptr = (char *) ch->records; + data.dsize = ch->records_len * sizeof(struct record); + ret = gdbm_store(*abc, key, data, GDBM_INSERT); + } + return 0; + } + + #endif |