Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

db.c File Reference

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include <dirent.h>
#include <asterisk/channel.h>
#include <asterisk/file.h>
#include <asterisk/app.h>
#include <asterisk/dsp.h>
#include <asterisk/logger.h>
#include <asterisk/options.h>
#include <asterisk/astdb.h>
#include <asterisk/cli.h>
#include "db1-ast/include/db.h"
#include "asterisk.h"
#include "astconf.h"

Go to the source code of this file.

Functions

int ast_db_deltree (const char *family, const char *keytree)
int ast_db_put (const char *family, const char *keys, char *value)
int ast_db_get (const char *family, const char *keys, char *value, int valuelen)
int ast_db_del (const char *family, const char *keys)
ast_db_entryast_db_gettree (const char *family, const char *keytree)
void ast_db_freetree (struct ast_db_entry *dbe)
int astdb_init (void)

Variables

ast_cli_entry cli_database_show
ast_cli_entry cli_database_get
ast_cli_entry cli_database_put
ast_cli_entry cli_database_del
ast_cli_entry cli_database_deltree


Function Documentation

int ast_db_del const char *  family,
const char *  keys
 

Definition at line 177 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, and LOG_DEBUG.

Referenced by ast_privacy_set().

00178 {
00179    char fullkey[256];
00180    DBT key;
00181    int res;
00182 
00183    ast_mutex_lock(&dblock);
00184    if (dbinit()) {
00185       ast_mutex_unlock(&dblock);
00186       return -1;
00187    }
00188    
00189    snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00190    memset(&key, 0, sizeof(key));
00191    key.data = fullkey;
00192    key.size = strlen(fullkey) + 1;
00193    
00194    res = astdb->del(astdb, &key, 0);
00195    astdb->sync(astdb, 0);
00196    
00197    ast_mutex_unlock(&dblock);
00198 
00199    if (res) 
00200       ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
00201    return res;
00202 }

int ast_db_deltree const char *  family,
const char *  keytree
 

Definition at line 69 of file db.c.

References ast_mutex_lock, and ast_mutex_unlock.

Referenced by ast_privacy_reset().

00070 {
00071    char prefix[256];
00072    DBT key, data;
00073    char *keys;
00074    int res;
00075    int pass;
00076    
00077    if (family) {
00078       if (keytree)
00079          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00080       else
00081          snprintf(prefix, sizeof(prefix), "/%s", family);
00082    } else if (keytree)
00083       return -1;
00084    else
00085       strcpy(prefix, "");
00086    
00087    ast_mutex_lock(&dblock);
00088    if (dbinit()) 
00089       return -1;
00090    
00091    memset(&key, 0, sizeof(key));
00092    memset(&data, 0, sizeof(data));
00093    pass = 0;
00094    while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00095       if (key.size) {
00096          keys = key.data;
00097          keys[key.size - 1] = '\0';
00098       } else
00099          keys = "<bad key>";
00100       if (keymatch(keys, prefix)) {
00101          astdb->del(astdb, &key, 0);
00102       }
00103    }
00104    astdb->sync(astdb, 0);
00105    ast_mutex_unlock(&dblock);
00106    return 0;
00107 }

void ast_db_freetree struct ast_db_entry dbe  ) 
 

Definition at line 365 of file db.c.

References free, and ast_db_entry::next.

00366 {
00367    struct ast_db_entry *last;
00368    while(dbe) {
00369       last = dbe;
00370       dbe = dbe->next;
00371       free(last);
00372    }
00373 }

int ast_db_get const char *  family,
const char *  keys,
char *  value,
int  valuelen
 

Definition at line 136 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, LOG_DEBUG, and LOG_NOTICE.

Referenced by ast_privacy_check().

00137 {
00138    char fullkey[256]="";
00139    DBT key, data;
00140    int res;
00141 
00142    ast_mutex_lock(&dblock);
00143    if (dbinit()) {
00144       ast_mutex_unlock(&dblock);
00145       return -1;
00146    }
00147 
00148    snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00149    memset(&key, 0, sizeof(key));
00150    memset(&data, 0, sizeof(data));
00151    memset(value, 0, valuelen);
00152    key.data = fullkey;
00153    key.size = strlen(fullkey) + 1;
00154    
00155    res = astdb->get(astdb, &key, &data, 0);
00156    
00157    ast_mutex_unlock(&dblock);
00158 
00159    /* Be sure to NULL terminate our data either way */
00160    if (res) {
00161       ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
00162    } else {
00163 #if 0
00164       printf("Got value of size %d\n", data.size);
00165 #endif
00166       if (data.size) {
00167          ((char *)data.data)[data.size - 1] = '\0';
00168          /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
00169          strncpy(value, data.data, (valuelen > data.size) ? data.size : valuelen);
00170       } else {
00171          ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
00172       }
00173    }
00174    return res;
00175 }

struct ast_db_entry* ast_db_gettree const char *  family,
const char *  keytree
 

Definition at line 307 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, LOG_WARNING, and malloc.

00308 {
00309    char prefix[256];
00310    DBT key, data;
00311    char *keys, *values;
00312    int res;
00313    int pass;
00314    struct ast_db_entry *last = NULL;
00315    struct ast_db_entry *cur, *ret=NULL;
00316 
00317    if (family && strlen(family)) {
00318       if (keytree && strlen(keytree))
00319          /* Family and key tree */
00320          snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
00321       else
00322          /* Family only */
00323          snprintf(prefix, sizeof(prefix), "/%s", family);
00324    } else
00325       strcpy(prefix, "");
00326    ast_mutex_lock(&dblock);
00327    if (dbinit()) {
00328       ast_mutex_unlock(&dblock);
00329       ast_log(LOG_WARNING, "Database unavailable\n");
00330       return NULL;   
00331    }
00332    memset(&key, 0, sizeof(key));
00333    memset(&data, 0, sizeof(data));
00334    pass = 0;
00335    while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00336       if (key.size) {
00337          keys = key.data;
00338          keys[key.size - 1] = '\0';
00339       } else
00340          keys = "<bad key>";
00341       if (data.size) {
00342          values = data.data;
00343          values[data.size - 1]='\0';
00344       } else
00345          values = "<bad value>";
00346       if (keymatch(keys, prefix)) {
00347             cur = malloc(sizeof(struct ast_db_entry) + strlen(keys) + strlen(values) + 2);
00348             if (cur) {
00349                cur->next = NULL;
00350                cur->key = cur->data + strlen(values) + 1;
00351                strcpy(cur->data, values);
00352                strcpy(cur->key, keys);
00353                if (last)
00354                   last->next = cur;
00355                else
00356                   ret = cur;
00357                last = cur;
00358             }
00359       }
00360    }
00361    ast_mutex_unlock(&dblock);
00362    return ret; 
00363 }

int ast_db_put const char *  family,
const char *  keys,
char *  value
 

Definition at line 109 of file db.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, and LOG_WARNING.

Referenced by ast_privacy_set().

00110 {
00111    char fullkey[256];
00112    DBT key, data;
00113    int res;
00114 
00115    ast_mutex_lock(&dblock);
00116    if (dbinit()) {
00117       ast_mutex_unlock(&dblock);
00118       return -1;
00119    }
00120 
00121    snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00122    memset(&key, 0, sizeof(key));
00123    memset(&data, 0, sizeof(data));
00124    key.data = fullkey;
00125    key.size = strlen(fullkey) + 1;
00126    data.data = value;
00127    data.size = strlen(value) + 1;
00128    res = astdb->put(astdb, &key, &data, 0);
00129    astdb->sync(astdb, 0);
00130    ast_mutex_unlock(&dblock);
00131    if (res)
00132       ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
00133    return res;
00134 }

int astdb_init void   ) 
 

Definition at line 415 of file db.c.

References ast_cli_register(), cli_database_del, cli_database_deltree, cli_database_get, cli_database_put, and cli_database_show.

Referenced by main().

00416 {
00417    dbinit();
00418    ast_cli_register(&cli_database_show);
00419    ast_cli_register(&cli_database_get);
00420    ast_cli_register(&cli_database_put);
00421    ast_cli_register(&cli_database_del);
00422    ast_cli_register(&cli_database_deltree);
00423    return 0;
00424 }


Variable Documentation

struct ast_cli_entry cli_database_del
 

Initial value:

{ { "database", "del", NULL }, database_del, "Removes database key/value", database_del_usage }

Definition at line 409 of file db.c.

Referenced by astdb_init().

struct ast_cli_entry cli_database_deltree
 

Initial value:

{ { "database", "deltree", NULL }, database_deltree, "Removes database keytree/values", database_deltree_usage }

Definition at line 412 of file db.c.

Referenced by astdb_init().

struct ast_cli_entry cli_database_get
 

Initial value:

{ { "database", "get", NULL }, database_get, "Gets database value", database_get_usage }

Definition at line 403 of file db.c.

Referenced by astdb_init().

struct ast_cli_entry cli_database_put
 

Initial value:

{ { "database", "put", NULL }, database_put, "Adds/updates database value", database_put_usage }

Definition at line 406 of file db.c.

Referenced by astdb_init().

struct ast_cli_entry cli_database_show
 

Initial value:

{ { "database", "show", NULL }, database_show, "Shows database contents", database_show_usage }

Definition at line 400 of file db.c.

Referenced by astdb_init().


Generated on Sun Apr 18 23:34:03 2004 for Asterisk by doxygen 1.3.6-20040222