Codename Pineapple

Home page | Mailing list | Docs

Last updated: Sat Feb 3 05:01:01 2007

Asterisk developer's documentation :: Codename Pineapple


astdb.h File Reference


Detailed Description

Persistant data storage (akin to *doze registry).

Definition in file astdb.h.

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_db_entry

Functions

int ast_db_del (const char *family, const char *key)
int ast_db_deltree (const char *family, const char *keytree)
void ast_db_freetree (struct ast_db_entry *entry)
int ast_db_get (const char *family, const char *key, char *out, int outlen)
ast_db_entryast_db_gettree (const char *family, const char *keytree)
int ast_db_put (const char *family, const char *key, const char *value)


Function Documentation

int ast_db_del const char *  family,
const char *  key
 

Definition at line 214 of file db.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), LOG_DEBUG, and option_debug.

Referenced by __expire_registry(), ast_privacy_set(), cache_lookup_internal(), database_del(), destroy_association(), dump_agents(), handle_dbdel(), manager_dbdel(), reload_agents(), and update_registry().

00215 {
00216    char fullkey[256];
00217    DBT key;
00218    int res, fullkeylen;
00219 
00220    ast_mutex_lock(&dblock);
00221    if (dbinit()) {
00222       ast_mutex_unlock(&dblock);
00223       return -1;
00224    }
00225    
00226    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00227    memset(&key, 0, sizeof(key));
00228    key.data = fullkey;
00229    key.size = fullkeylen + 1;
00230    
00231    res = astdb->del(astdb, &key, 0);
00232    astdb->sync(astdb, 0);
00233    
00234    ast_mutex_unlock(&dblock);
00235 
00236    if (res) {
00237       if (option_debug)
00238          ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
00239    }
00240    return res;
00241 }

int ast_db_deltree const char *  family,
const char *  keytree
 

Definition at line 100 of file db.c.

References ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), keymatch(), keys, and prefix.

Referenced by ast_privacy_reset(), database_deltree(), handle_dbdeltree(), iax_provision_reload(), and manager_dbdeltree().

00101 {
00102    char prefix[256];
00103    DBT key, data;
00104    char *keys;
00105    int res;
00106    int pass;
00107    
00108    if (family) {
00109       if (keytree) {
00110          snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
00111       } else {
00112          snprintf(prefix, sizeof(prefix), "/%s", family);
00113       }
00114    } else if (keytree) {
00115       return -1;
00116    } else {
00117       prefix[0] = '\0';
00118    }
00119    
00120    ast_mutex_lock(&dblock);
00121    if (dbinit()) {
00122       ast_mutex_unlock(&dblock);
00123       return -1;
00124    }
00125    
00126    memset(&key, 0, sizeof(key));
00127    memset(&data, 0, sizeof(data));
00128    pass = 0;
00129    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00130       if (key.size) {
00131          keys = key.data;
00132          keys[key.size - 1] = '\0';
00133       } else {
00134          keys = "<bad key>";
00135       }
00136       if (keymatch(keys, prefix)) {
00137          astdb->del(astdb, &key, 0);
00138       }
00139    }
00140    astdb->sync(astdb, 0);
00141    ast_mutex_unlock(&dblock);
00142    return 0;
00143 }

void ast_db_freetree struct ast_db_entry entry  ) 
 

Definition at line 460 of file db.c.

References free, last, and ast_db_entry::next.

00461 {
00462    struct ast_db_entry *last;
00463    while (dbe) {
00464       last = dbe;
00465       dbe = dbe->next;
00466       free(last);
00467    }
00468 }

int ast_db_get const char *  family,
const char *  key,
char *  out,
int  outlen
 

Definition at line 172 of file db.c.

References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), LOG_DEBUG, LOG_NOTICE, and option_debug.

Referenced by ast_privacy_check(), cache_lookup_internal(), check_access(), create_addr(), database_get(), handle_dbget(), iax_provision_version(), load_password(), manager_dbget(), populate_addr(), reg_source_db(), and reload_agents().

00173 {
00174    char fullkey[256] = "";
00175    DBT key, data;
00176    int res, fullkeylen;
00177 
00178    ast_mutex_lock(&dblock);
00179    if (dbinit()) {
00180       ast_mutex_unlock(&dblock);
00181       return -1;
00182    }
00183 
00184    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00185    memset(&key, 0, sizeof(key));
00186    memset(&data, 0, sizeof(data));
00187    memset(value, 0, valuelen);
00188    key.data = fullkey;
00189    key.size = fullkeylen + 1;
00190    
00191    res = astdb->get(astdb, &key, &data, 0);
00192    
00193    ast_mutex_unlock(&dblock);
00194 
00195    /* Be sure to NULL terminate our data either way */
00196    if (res) {
00197       if (option_debug)
00198          ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family);
00199    } else {
00200 #if 0
00201       printf("Got value of size %d\n", data.size);
00202 #endif
00203       if (data.size) {
00204          ((char *)data.data)[data.size - 1] = '\0';
00205          /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */
00206          ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen);
00207       } else {
00208          ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys);
00209       }
00210    }
00211    return res;
00212 }

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

Definition at line 398 of file db.c.

References ast_log(), ast_malloc, ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), astdb, ast_db_entry::data, dbinit(), key(), keymatch(), keys, last, LOG_WARNING, and prefix.

Referenced by reload_agents().

00399 {
00400    char prefix[256];
00401    DBT key, data;
00402    char *keys, *values;
00403    int values_len;
00404    int res;
00405    int pass;
00406    struct ast_db_entry *last = NULL;
00407    struct ast_db_entry *cur, *ret=NULL;
00408 
00409    if (!ast_strlen_zero(family)) {
00410       if (!ast_strlen_zero(keytree)) {
00411          /* Family and key tree */
00412          snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
00413       } else {
00414          /* Family only */
00415          snprintf(prefix, sizeof(prefix), "/%s", family);
00416       }
00417    } else {
00418       prefix[0] = '\0';
00419    }
00420    ast_mutex_lock(&dblock);
00421    if (dbinit()) {
00422       ast_mutex_unlock(&dblock);
00423       ast_log(LOG_WARNING, "Database unavailable\n");
00424       return NULL;   
00425    }
00426    memset(&key, 0, sizeof(key));
00427    memset(&data, 0, sizeof(data));
00428    pass = 0;
00429    while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
00430       if (key.size) {
00431          keys = key.data;
00432          keys[key.size - 1] = '\0';
00433       } else {
00434          keys = "<bad key>";
00435       }
00436       if (data.size) {
00437          values = data.data;
00438          values[data.size - 1] = '\0';
00439       } else {
00440          values = "<bad value>";
00441       }
00442       values_len = strlen(values) + 1;
00443       if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) {
00444          cur->next = NULL;
00445          cur->key = cur->data + values_len;
00446          strcpy(cur->data, values);
00447          strcpy(cur->key, keys);
00448          if (last) {
00449             last->next = cur;
00450          } else {
00451             ret = cur;
00452          }
00453          last = cur;
00454       }
00455    }
00456    ast_mutex_unlock(&dblock);
00457    return ret; 
00458 }

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

Definition at line 145 of file db.c.

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

Referenced by ast_privacy_set(), cache_save_hint(), database_put(), dump_agents(), handle_command_response(), handle_dbput(), iax_provision_build(), manager_dbput(), mgcp_ss(), parse_register_contact(), save_secret(), ss_thread(), and update_registry().

00146 {
00147    char fullkey[256];
00148    DBT key, data;
00149    int res, fullkeylen;
00150 
00151    ast_mutex_lock(&dblock);
00152    if (dbinit()) {
00153       ast_mutex_unlock(&dblock);
00154       return -1;
00155    }
00156 
00157    fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys);
00158    memset(&key, 0, sizeof(key));
00159    memset(&data, 0, sizeof(data));
00160    key.data = fullkey;
00161    key.size = fullkeylen + 1;
00162    data.data = (char *) value;
00163    data.size = strlen(value) + 1;
00164    res = astdb->put(astdb, &key, &data, 0);
00165    astdb->sync(astdb, 0);
00166    ast_mutex_unlock(&dblock);
00167    if (res)
00168       ast_log(LOG_WARNING, "Unable to put value '%s' for key '%s' in family '%s'\n", value, keys, family);
00169    return res;
00170 }


Asterisk is a trademark for Digium, inc.. | Edvina.net | Asterisk.org | This documentation was generated with Doxygen