Asterisk - The Open Source Telephony Project GIT-master-754dea3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
Data Structures | Functions
astdb.h File Reference

Persistent data storage (akin to *doze registry) More...

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)
 Delete entry in astdb. More...
 
int ast_db_del2 (const char *family, const char *key)
 Same as ast_db_del, but with more stringent error checking. More...
 
int ast_db_deltree (const char *family, const char *keytree)
 Delete one or more entries in astdb. More...
 
int ast_db_exists (const char *family, const char *key)
 Check if family/key exitsts. More...
 
void ast_db_freetree (struct ast_db_entry *entry)
 Free structure created by ast_db_gettree() More...
 
int ast_db_get (const char *family, const char *key, char *value, int valuelen)
 Get key value specified by family/key. More...
 
int ast_db_get_allocated (const char *family, const char *key, char **out)
 Get key value specified by family/key as a heap allocated string. More...
 
struct ast_db_entryast_db_gettree (const char *family, const char *keytree)
 Get a list of values within the astdb tree. More...
 
struct ast_db_entryast_db_gettree_by_prefix (const char *family, const char *key_prefix)
 Get a list of values with the given key prefix. More...
 
int ast_db_put (const char *family, const char *key, const char *value)
 Store value addressed by family/key. More...
 

Detailed Description

Persistent data storage (akin to *doze registry)

Definition in file astdb.h.

Function Documentation

◆ ast_db_del()

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

Delete entry in astdb.

Definition at line 472 of file db.c.

473{
474 char *fullkey;
475 int fullkey_len;
476 int res = 0;
477
478 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
479 if (fullkey_len < 0) {
480 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
481 family, key);
482 return -1;
483 }
484
486 if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
487 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
488 res = -1;
489 } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
490 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
491 res = -1;
492 }
493 sqlite3_reset(del_stmt);
494 db_sync();
496 ast_free(fullkey);
497
498 return res;
499}
#define ast_free(a)
Definition: astmm.h:180
#define ast_asprintf(ret, fmt,...)
A wrapper for asprintf()
Definition: astmm.h:267
#define ast_log
Definition: astobj2.c:42
static void db_sync(void)
Definition: db.c:1176
static sqlite3 * astdb
Definition: db.c:138
static ast_mutex_t dblock
Definition: db.c:136
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
#define ast_mutex_unlock(a)
Definition: lock.h:194
#define ast_mutex_lock(a)
Definition: lock.h:193

References ast_asprintf, ast_debug, ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, and LOG_WARNING.

Referenced by __expire_registry(), add_cert_key_to_astdb(), ast_privacy_set(), AST_TEST_DEFINE(), auth_exec(), cache_lookup_internal(), cleanup_cert_from_astdb_and_fs(), destroy_all_channels(), dialgroup_refreshdb(), do_register_expire(), dump_queue_members(), function_db_delete(), handle_dbdel(), media_cache_item_del_from_astdb(), media_cache_remove_from_astdb(), mkintf(), process_clearcache(), reload_queue_members(), sorcery_astdb_delete(), stasis_app_device_state_delete(), and update_registry().

◆ ast_db_del2()

int ast_db_del2 ( const char *  family,
const char *  key 
)

Same as ast_db_del, but with more stringent error checking.

Unlike ast_db_del, if the key does not exist in the first place, an error is emitted and -1 is returned.

Return values
-1An error occured (including key not found to begin with)
0Successfully deleted

Definition at line 501 of file db.c.

502{
503 char *fullkey;
504 char tmp[1];
505 int fullkey_len;
506 int mres, res = 0;
507
508 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
509 if (fullkey_len < 0) {
510 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
511 family, key);
512 return -1;
513 }
514
516 if (ast_db_get(family, key, tmp, sizeof(tmp))) {
517 ast_log(LOG_WARNING, "AstDB key %s does not exist\n", fullkey);
518 res = -1;
519 } else if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
520 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
521 res = -1;
522 } else if ((mres = sqlite3_step(del_stmt) != SQLITE_DONE)) {
523 ast_log(LOG_WARNING, "AstDB error (%s): %s\n", fullkey, sqlite3_errstr(mres));
524 res = -1;
525 }
526 sqlite3_reset(del_stmt);
527 db_sync();
529 ast_free(fullkey);
530
531 return res;
532}
int ast_db_get(const char *family, const char *key, char *value, int valuelen)
Get key value specified by family/key.
Definition: db.c:421

References ast_asprintf, ast_db_get(), ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, and LOG_WARNING.

Referenced by handle_cli_database_del(), and manager_dbdel().

◆ ast_db_deltree()

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

Delete one or more entries in astdb.

If both parameters are NULL, the entire database will be purged. If only keytree is NULL, all entries within the family will be purged. It is an error for keytree to have a value when family is NULL.

Return values
-1An error occurred
>=0 Number of records deleted

Definition at line 559 of file db.c.

560{
561 sqlite3_stmt *stmt = deltree_stmt;
562 char *prefix = NULL;
563 int prefix_len = 0;
564 int res = 0;
565
566 if (ast_strlen_zero(family) && !ast_strlen_zero(keytree)) {
567 ast_log(LOG_WARNING, "Key tree '%s' specified without family\n", keytree);
568 return -1;
569 }
570
571 prefix = create_prefix(family, keytree, &prefix_len);
572 if (!prefix) {
573 return -1;
574 }
575 if (prefix_len == 0) {
576 stmt = deltree_all_stmt;
577 }
578
580 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
581 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
582 res = -1;
583 } else if (sqlite3_step(stmt) != SQLITE_DONE) {
584 ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
585 res = -1;
586 }
587 res = sqlite3_changes(astdb);
588 sqlite3_reset(stmt);
589 db_sync();
592
593 return res;
594}
static char * create_prefix(const char *family, const char *keytree, int *prefix_len)
Definition: db.c:534
static char prefix[MAX_PREFIX]
Definition: http.c:144
#define NULL
Definition: resample.c:96
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65

References ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero(), astdb, create_prefix(), db_sync(), dblock, LOG_WARNING, NULL, and prefix.

Referenced by ast_privacy_reset(), AST_TEST_DEFINE(), cleanup_cert_from_astdb_and_fs(), deinitialize_sorcery(), deltree_exec(), dundi_flush(), handle_cli_database_deltree(), handle_dbdeltree(), iax_provision_reload(), manager_dbdeltree(), media_cache_item_del_from_astdb(), and media_cache_remove_from_astdb().

◆ ast_db_exists()

int ast_db_exists ( const char *  family,
const char *  key 
)

Check if family/key exitsts.

Parameters
family
key
Return values
1if family/key exists
0if family/key does not exist or an error occurred

Definition at line 438 of file db.c.

439{
440 int result;
441 char *fullkey;
442 int fullkey_len;
443 int res = 0;
444
445 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
446 if (fullkey_len < 0) {
447 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
448 family, key);
449 return -1;
450 }
451
453 res = sqlite3_bind_text(exists_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC);
454 if (res != SQLITE_OK) {
455 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %d:%s\n", res, sqlite3_errmsg(astdb));
456 res = 0;
457 } else if (sqlite3_step(exists_stmt) != SQLITE_ROW) {
458 res = 0;
459 } else if (!(result = sqlite3_column_int(exists_stmt, 0))) {
460 res = 0;
461 } else {
462 res = result;
463 }
464 sqlite3_reset(exists_stmt);
466 ast_free(fullkey);
467
468 return res;
469}
static PGresult * result
Definition: cel_pgsql.c:84

References ast_asprintf, ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, dblock, LOG_WARNING, and result.

Referenced by cleanup_cert_from_astdb_and_fs(), and retrieve_cert_from_cache().

◆ ast_db_freetree()

void ast_db_freetree ( struct ast_db_entry entry)

◆ ast_db_get()

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

Get key value specified by family/key.

Definition at line 421 of file db.c.

422{
424
425 /* Make sure we initialize */
426 value[0] = 0;
427
428 return db_get_common(family, key, &value, valuelen);
429}
static int db_get_common(const char *family, const char *key, char **buffer, int bufferlen)
Definition: db.c:381
int value
Definition: syslog.c:37
#define ast_assert(a)
Definition: utils.h:739

References ast_assert, db_get_common(), NULL, and value.

Referenced by ast_db_del2(), ast_pbx_uuid_get(), ast_privacy_check(), AST_TEST_DEFINE(), auth_exec(), blacklist_read(), cache_lookup_internal(), check_access(), create_addr(), custom_devstate_callback(), custom_presence_callback(), database_increment(), destroy_all_channels(), function_db_delete(), function_db_exists(), function_db_read(), handle_dbget(), iax_provision_version(), load_password(), mkintf(), populate_addr(), refresh_all_favorite(), reg_source_db(), retrieve_cert_from_cache(), sorcery_astdb_delete(), sorcery_astdb_update(), and stasis_device_state_cb().

◆ ast_db_get_allocated()

int ast_db_get_allocated ( const char *  family,
const char *  key,
char **  out 
)

Get key value specified by family/key as a heap allocated string.

Given a family and key, sets out to a pointer to a heap allocated string. In the event of an error, out will be set to NULL. The string must be freed by calling ast_free().

Return values
-1An error occurred
0Success

Definition at line 431 of file db.c.

432{
433 *out = NULL;
434
435 return db_get_common(family, key, out, -1);
436}
FILE * out
Definition: utils/frame.c:33

References db_get_common(), NULL, and out.

Referenced by AST_TEST_DEFINE(), handle_cli_database_get(), manager_dbget(), media_cache_item_del_from_astdb(), reload_queue_members(), and sorcery_astdb_retrieve_id().

◆ ast_db_gettree()

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

Get a list of values within the astdb tree.

If family is specified, only those keys will be returned. If keytree is specified, subkeys are expected to exist (separated from the key with a slash). If subkeys do not exist and keytree is specified, the tree will consist of either a single entry or NULL will be returned.

Resulting tree should be freed by passing the return value to ast_db_freetree() when usage is concluded.

Definition at line 635 of file db.c.

636{
637 char *prefix = NULL;
638 int prefix_len = 0;
639 sqlite3_stmt *stmt = gettree_stmt;
640 struct ast_db_entry *ret;
641
642 prefix = create_prefix(family, keytree, &prefix_len);
643 if (!prefix) {
644 return NULL;
645 }
646 if (prefix_len == 0) {
647 stmt = gettree_all_stmt;
648 }
649
651 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
652 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
653 sqlite3_reset(stmt);
656 return NULL;
657 }
658
659 ret = db_gettree_common(stmt);
660 sqlite3_reset(stmt);
663
664 return ret;
665}
static struct ast_db_entry * db_gettree_common(sqlite3_stmt *stmt)
Definition: db.c:596

References ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, create_prefix(), db_gettree_common(), dblock, LOG_WARNING, NULL, and prefix.

Referenced by AST_TEST_DEFINE(), dundi_show_cache(), dundi_show_hints(), function_db_keycount(), function_db_keys(), handle_cli_devstate_list(), handle_cli_presencestate_list(), load_module(), media_cache_item_populate_from_astdb(), media_cache_populate_from_astdb(), populate_cache(), process_clearcache(), reload_queue_members(), sorcery_astdb_retrieve_fields_common(), sorcery_astdb_retrieve_regex(), and stasis_app_device_states_to_json().

◆ ast_db_gettree_by_prefix()

struct ast_db_entry * ast_db_gettree_by_prefix ( const char *  family,
const char *  key_prefix 
)

Get a list of values with the given key prefix.

Parameters
familyThe family to search under
key_prefixThe key prefix to search under
Return values
NULLAn error occurred

Definition at line 667 of file db.c.

668{
669 char *prefix = NULL;
670 int prefix_len = 0;
671 struct ast_db_entry *ret;
672
673 prefix = create_prefix(family, key_prefix, &prefix_len);
674 if (!prefix) {
675 return NULL;
676 }
677
679 if (sqlite3_bind_text(gettree_prefix_stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK) {
680 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
681 sqlite3_reset(gettree_prefix_stmt);
684 return NULL;
685 }
686
687 ret = db_gettree_common(gettree_prefix_stmt);
688 sqlite3_reset(gettree_prefix_stmt);
691
692 return ret;
693}

References ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, create_prefix(), db_gettree_common(), dblock, LOG_WARNING, NULL, and prefix.

Referenced by sorcery_astdb_retrieve_prefix().

◆ ast_db_put()

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

Store value addressed by family/key.

Definition at line 335 of file db.c.

336{
337 char *fullkey;
338 int fullkey_len;
339 int res = 0;
340
341 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
342 if (fullkey_len < 0) {
343 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
344 family, key);
345 return -1;
346 }
347
349 if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
350 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
351 res = -1;
352 } else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
353 ast_log(LOG_WARNING, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
354 res = -1;
355 } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
356 ast_log(LOG_WARNING, "Couldn't execute statement: %s\n", sqlite3_errmsg(astdb));
357 res = -1;
358 }
359 sqlite3_reset(put_stmt);
360 db_sync();
362 ast_free(fullkey);
363
364 return res;
365}

References ast_asprintf, ast_free, ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, LOG_WARNING, and value.

Referenced by __analog_ss_thread(), add_cert_expiration_to_astdb(), add_cert_key_to_astdb(), ast_privacy_set(), AST_TEST_DEFINE(), asterisk_daemon(), cache_save(), cache_save_hint(), database_increment(), devstate_write(), dialgroup_refreshdb(), dump_queue_members(), function_db_write(), handle_cli_database_put(), handle_cli_devstate_change(), handle_cli_presencestate_change(), handle_command_response(), handle_dbput(), iax_provision_build(), manager_dbput(), media_cache_item_sync_to_astdb(), metadata_sync_to_astdb(), presence_write(), save_secret(), sorcery_astdb_create(), stasis_app_device_state_update(), and update_registry().