Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions
astdb.h File Reference

Persistant 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_deltree (const char *family, const char *keytree)
 Delete one or more entries in astdb. 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

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

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

Referenced by __expire_registry(), ast_privacy_set(), AST_TEST_DEFINE(), auth_exec(), cache_lookup_internal(), dahdi_cc_callback(), destroy_all_channels(), destroy_association(), dialgroup_refreshdb(), do_register_expire(), dump_queue_members(), function_db_delete(), handle_cli_database_del(), handle_dbdel(), manager_dbdel(), media_cache_item_del_from_astdb(), media_cache_remove_from_astdb(), mkintf(), presence_change_common(), process_clearcache(), reload_queue_members(), remove_public_key_from_astdb(), sorcery_astdb_delete(), stasis_app_device_state_delete(), and update_registry().

430 {
431  char fullkey[MAX_DB_FIELD];
432  size_t fullkey_len;
433  int res = 0;
434 
435  if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
436  ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
437  return -1;
438  }
439 
440  fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
441 
443  if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
444  ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
445  res = -1;
446  } else if (sqlite3_step(del_stmt) != SQLITE_DONE) {
447  ast_debug(1, "Unable to find key '%s' in family '%s'\n", key, family);
448  res = -1;
449  }
450  sqlite3_reset(del_stmt);
451  db_sync();
453 
454  return res;
455 }
#define LOG_WARNING
Definition: logger.h:274
#define ast_mutex_lock(a)
Definition: lock.h:187
static ast_mutex_t dblock
Definition: main/db.c:108
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
#define MAX_DB_FIELD
Definition: main/db.c:107
static sqlite3 * astdb
Definition: main/db.c:110
static void db_sync(void)
Definition: main/db.c:1009
#define ast_mutex_unlock(a)
Definition: lock.h:188

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

References ast_log, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero, astdb, db_sync(), dblock, LOG_WARNING, MAX_DB_FIELD, and prefix.

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

458 {
459  sqlite3_stmt *stmt = deltree_stmt;
460  char prefix[MAX_DB_FIELD];
461  int res = 0;
462 
463  if (!ast_strlen_zero(family)) {
464  if (!ast_strlen_zero(keytree)) {
465  /* Family and key tree */
466  snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
467  } else {
468  /* Family only */
469  snprintf(prefix, sizeof(prefix), "/%s", family);
470  }
471  } else {
472  prefix[0] = '\0';
473  stmt = deltree_all_stmt;
474  }
475 
477  if (!ast_strlen_zero(prefix) && (sqlite3_bind_text(stmt, 1, prefix, -1, SQLITE_STATIC) != SQLITE_OK)) {
478  ast_log(LOG_WARNING, "Could bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
479  res = -1;
480  } else if (sqlite3_step(stmt) != SQLITE_DONE) {
481  ast_log(LOG_WARNING, "Couldn't execute stmt: %s\n", sqlite3_errmsg(astdb));
482  res = -1;
483  }
484  res = sqlite3_changes(astdb);
485  sqlite3_reset(stmt);
486  db_sync();
488 
489  return res;
490 }
#define LOG_WARNING
Definition: logger.h:274
#define ast_mutex_lock(a)
Definition: lock.h:187
static ast_mutex_t dblock
Definition: main/db.c:108
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
#define MAX_DB_FIELD
Definition: main/db.c:107
static sqlite3 * astdb
Definition: main/db.c:110
static void db_sync(void)
Definition: main/db.c:1009
#define ast_mutex_unlock(a)
Definition: lock.h:188
static char prefix[MAX_PREFIX]
Definition: http.c:141

◆ 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 
)

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

References db_get_common(), and NULL.

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

423 {
424  *out = NULL;
425 
426  return db_get_common(family, key, out, -1);
427 }
static int db_get_common(const char *family, const char *key, char **buffer, int bufferlen)
Definition: main/db.c:373
#define NULL
Definition: resample.c:96
FILE * out
Definition: utils/frame.c:33
char * key
Definition: astdb.h:33

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

References ast_log, ast_mutex_lock, ast_mutex_unlock, ast_strlen_zero, astdb, db_gettree_common(), dblock, LOG_WARNING, MAX_DB_FIELD, NULL, and prefix.

Referenced by AST_TEST_DEFINE(), dundi_show_cache(), dundi_show_hints(), 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().

532 {
533  char prefix[MAX_DB_FIELD];
534  sqlite3_stmt *stmt = gettree_stmt;
535  size_t res = 0;
536  struct ast_db_entry *ret;
537 
538  if (!ast_strlen_zero(family)) {
539  if (!ast_strlen_zero(keytree)) {
540  /* Family and key tree */
541  res = snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
542  } else {
543  /* Family only */
544  res = snprintf(prefix, sizeof(prefix), "/%s", family);
545  }
546 
547  if (res >= sizeof(prefix)) {
548  ast_log(LOG_WARNING, "Requested prefix is too long: %s\n", keytree);
549  return NULL;
550  }
551  } else {
552  prefix[0] = '\0';
553  stmt = gettree_all_stmt;
554  }
555 
557  if (res && (sqlite3_bind_text(stmt, 1, prefix, res, SQLITE_STATIC) != SQLITE_OK)) {
558  ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
559  sqlite3_reset(stmt);
561  return NULL;
562  }
563 
564  ret = db_gettree_common(stmt);
565  sqlite3_reset(stmt);
567 
568  return ret;
569 }
#define LOG_WARNING
Definition: logger.h:274
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
static ast_mutex_t dblock
Definition: main/db.c:108
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
static struct ast_db_entry * db_gettree_common(sqlite3_stmt *stmt)
Definition: main/db.c:492
#define MAX_DB_FIELD
Definition: main/db.c:107
Definition: astdb.h:31
static sqlite3 * astdb
Definition: main/db.c:110
#define ast_mutex_unlock(a)
Definition: lock.h:188
static char prefix[MAX_PREFIX]
Definition: http.c:141

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

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

Referenced by sorcery_astdb_retrieve_prefix().

572 {
573  char prefix[MAX_DB_FIELD];
574  size_t res;
575  struct ast_db_entry *ret;
576 
577  res = snprintf(prefix, sizeof(prefix), "/%s/%s", family, key_prefix);
578  if (res >= sizeof(prefix)) {
579  ast_log(LOG_WARNING, "Requested key prefix is too long: %s\n", key_prefix);
580  return NULL;
581  }
582 
584  if (sqlite3_bind_text(gettree_prefix_stmt, 1, prefix, res, SQLITE_STATIC) != SQLITE_OK) {
585  ast_log(LOG_WARNING, "Could not bind %s to stmt: %s\n", prefix, sqlite3_errmsg(astdb));
586  sqlite3_reset(gettree_prefix_stmt);
588  return NULL;
589  }
590 
591  ret = db_gettree_common(gettree_prefix_stmt);
592  sqlite3_reset(gettree_prefix_stmt);
594 
595  return ret;
596 }
#define LOG_WARNING
Definition: logger.h:274
#define ast_mutex_lock(a)
Definition: lock.h:187
#define NULL
Definition: resample.c:96
static ast_mutex_t dblock
Definition: main/db.c:108
#define ast_log
Definition: astobj2.c:42
static struct ast_db_entry * db_gettree_common(sqlite3_stmt *stmt)
Definition: main/db.c:492
#define MAX_DB_FIELD
Definition: main/db.c:107
Definition: astdb.h:31
static sqlite3 * astdb
Definition: main/db.c:110
#define ast_mutex_unlock(a)
Definition: lock.h:188
static char prefix[MAX_PREFIX]
Definition: http.c:141

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

References ast_log, ast_mutex_lock, ast_mutex_unlock, astdb, db_sync(), dblock, LOG_WARNING, and MAX_DB_FIELD.

Referenced by __analog_ss_thread(), add_public_key_to_astdb(), ast_privacy_set(), AST_TEST_DEFINE(), asterisk_daemon(), cache_save(), cache_save_hint(), dahdi_cc_callback(), 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(), mgcp_ss(), parse_register_contact(), presence_write(), save_secret(), set_public_key_expiration(), sorcery_astdb_create(), stasis_app_device_state_update(), test_stir_shaken_add_fake_astdb_entry(), and update_registry().

328 {
329  char fullkey[MAX_DB_FIELD];
330  size_t fullkey_len;
331  int res = 0;
332 
333  if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
334  ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
335  return -1;
336  }
337 
338  fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
339 
341  if (sqlite3_bind_text(put_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
342  ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
343  res = -1;
344  } else if (sqlite3_bind_text(put_stmt, 2, value, -1, SQLITE_STATIC) != SQLITE_OK) {
345  ast_log(LOG_WARNING, "Couldn't bind value to stmt: %s\n", sqlite3_errmsg(astdb));
346  res = -1;
347  } else if (sqlite3_step(put_stmt) != SQLITE_DONE) {
348  ast_log(LOG_WARNING, "Couldn't execute statement: %s\n", sqlite3_errmsg(astdb));
349  res = -1;
350  }
351 
352  sqlite3_reset(put_stmt);
353  db_sync();
355 
356  return res;
357 }
#define LOG_WARNING
Definition: logger.h:274
#define ast_mutex_lock(a)
Definition: lock.h:187
int value
Definition: syslog.c:37
static ast_mutex_t dblock
Definition: main/db.c:108
#define ast_log
Definition: astobj2.c:42
#define MAX_DB_FIELD
Definition: main/db.c:107
static sqlite3 * astdb
Definition: main/db.c:110
static void db_sync(void)
Definition: main/db.c:1009
#define ast_mutex_unlock(a)
Definition: lock.h:188
char * key
Definition: astdb.h:33