db.c File Reference

#include <stdio.h>
#include <stdlib.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 <asterisk/utils.h>
#include <asterisk/lock.h>
#include "db1-ast/include/db.h"
#include "asterisk.h"
#include "astconf.h"

Include dependency graph for db.c:

Go to the source code of this file.

Functions

 AST_MUTEX_DEFINE_STATIC (dblock)
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)
struct 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

struct ast_cli_entry cli_database_show
struct ast_cli_entry cli_database_get
struct ast_cli_entry cli_database_put
struct ast_cli_entry cli_database_del
struct ast_cli_entry cli_database_deltree


Function Documentation

int ast_db_del ( const char *  family,
const char *  keys 
)

Definition at line 180 of file db.c.

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

int ast_db_deltree ( const char *  family,
const char *  keytree 
)

Definition at line 71 of file db.c.

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

void ast_db_freetree ( struct ast_db_entry dbe  ) 

Definition at line 368 of file db.c.

00369 {
00370    struct ast_db_entry *last;
00371    while(dbe) {
00372       last = dbe;
00373       dbe = dbe->next;
00374       free(last);
00375    }
00376 }

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

Definition at line 138 of file db.c.

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

struct ast_db_entry* ast_db_gettree ( const char *  family,
const char *  keytree 
) [read]

Definition at line 310 of file db.c.

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

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

Definition at line 111 of file db.c.

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

AST_MUTEX_DEFINE_STATIC ( dblock   ) 

int astdb_init ( void   ) 

Definition at line 418 of file db.c.


Variable Documentation

Initial value:

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

Definition at line 412 of file db.c.

Initial value:

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

Definition at line 415 of file db.c.

Initial value:

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

Definition at line 406 of file db.c.

Initial value:

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

Definition at line 409 of file db.c.

Initial value:

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

Definition at line 403 of file db.c.


Generated on Wed Oct 28 17:00:53 2009 for Asterisk by  doxygen 1.5.6