Asterisk - The Open Source Telephony Project GIT-master-97770a9
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 478 of file main/db.c.

479{
480 char *fullkey;
481 int fullkey_len;
482 int res = 0;
483
484 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
485 if (fullkey_len < 0) {
486 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
487 family, key);
488 return -1;
489 }
490
492 if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
493 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
494 res = -1;
495 } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
496 ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
497 res = -1;
498 }
499 sqlite3_reset(del_stmt);
500 db_sync();
502 ast_free(fullkey);
503
504 return res;
505}
#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
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
#define ast_mutex_unlock(a)
Definition: lock.h:190
#define ast_mutex_lock(a)
Definition: lock.h:189
static void db_sync(void)
Definition: main/db.c:1182
static sqlite3 * astdb
Definition: main/db.c:121
static ast_mutex_t dblock
Definition: main/db.c:119

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 507 of file main/db.c.

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

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

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 565 of file main/db.c.

566{
567 sqlite3_stmt *stmt = deltree_stmt;
568 char *prefix = NULL;
569 int prefix_len = 0;
570 int res = 0;
571
572 if (ast_strlen_zero(family) && !ast_strlen_zero(keytree)) {
573 ast_log(LOG_WARNING, "Key tree '%s' specified without family\n", keytree);
574 return -1;
575 }
576
577 prefix = create_prefix(family, keytree, &prefix_len);
578 if (!prefix) {
579 return -1;
580 }
581 if (prefix_len == 0) {
582 stmt = deltree_all_stmt;
583 }
584
586 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
587 ast_log(LOG_WARNING, "Couldn't bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
588 res = -1;
589 } else if (sqlite3_step(stmt) != SQLITE_DONE) {
590 ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
591 res = -1;
592 }
593 res = sqlite3_changes(astdb);
594 sqlite3_reset(stmt);
595 db_sync();
598
599 return res;
600}
static char prefix[MAX_PREFIX]
Definition: http.c:144
static char * create_prefix(const char *family, const char *keytree, int *prefix_len)
Definition: main/db.c:540
#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 444 of file main/db.c.

445{
446 int result;
447 char *fullkey;
448 int fullkey_len;
449 int res = 0;
450
451 fullkey_len = ast_asprintf(&fullkey, "/%s/%s", family, key);
452 if (fullkey_len < 0) {
453 ast_log(LOG_WARNING, "Unable to allocate memory for family/key '/%s/%s'\n",
454 family, key);
455 return -1;
456 }
457
459 res = sqlite3_bind_text(exists_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC);
460 if (res != SQLITE_OK) {
461 ast_log(LOG_WARNING, "Couldn't bind key to stmt: %d:%s\n", res, sqlite3_errmsg(astdb));
462 res = 0;
463 } else if (sqlite3_step(exists_stmt) != SQLITE_ROW) {
464 res = 0;
465 } else if (!(result = sqlite3_column_int(exists_stmt, 0))) {
466 res = 0;
467 } else {
468 res = result;
469 }
470 sqlite3_reset(exists_stmt);
472 ast_free(fullkey);
473
474 return res;
475}
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 427 of file main/db.c.

428{
430
431 /* Make sure we initialize */
432 value[0] = 0;
433
434 return db_get_common(family, key, &value, valuelen);
435}
static int db_get_common(const char *family, const char *key, char **buffer, int bufferlen)
Definition: main/db.c:387
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 437 of file main/db.c.

438{
439 *out = NULL;
440
441 return db_get_common(family, key, out, -1);
442}
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 641 of file main/db.c.

642{
643 char *prefix = NULL;
644 int prefix_len = 0;
645 sqlite3_stmt *stmt = gettree_stmt;
646 struct ast_db_entry *ret;
647
648 prefix = create_prefix(family, keytree, &prefix_len);
649 if (!prefix) {
650 return NULL;
651 }
652 if (prefix_len == 0) {
653 stmt = gettree_all_stmt;
654 }
655
657 if (prefix_len && (sqlite3_bind_text(stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK)) {
658 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
659 sqlite3_reset(stmt);
662 return NULL;
663 }
664
665 ret = db_gettree_common(stmt);
666 sqlite3_reset(stmt);
669
670 return ret;
671}
static struct ast_db_entry * db_gettree_common(sqlite3_stmt *stmt)
Definition: main/db.c:602

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 673 of file main/db.c.

674{
675 char *prefix = NULL;
676 int prefix_len = 0;
677 struct ast_db_entry *ret;
678
679 prefix = create_prefix(family, key_prefix, &prefix_len);
680 if (!prefix) {
681 return NULL;
682 }
683
685 if (sqlite3_bind_text(gettree_prefix_stmt, 1, prefix, prefix_len, SQLITE_STATIC) != SQLITE_OK) {
686 ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
687 sqlite3_reset(gettree_prefix_stmt);
690 return NULL;
691 }
692
693 ret = db_gettree_common(gettree_prefix_stmt);
694 sqlite3_reset(gettree_prefix_stmt);
697
698 return ret;
699}

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 341 of file main/db.c.

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

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().