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) |
struct ast_db_entry * | ast_db_gettree (const char *family, const char *keytree) |
int | ast_db_put (const char *family, const char *key, char *value) |
Definition in file astdb.h.
int ast_db_del | ( | const char * | family, | |
const char * | key | |||
) |
Definition at line 213 of file db.c.
References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), and LOG_DEBUG.
00214 { 00215 char fullkey[256]; 00216 DBT key; 00217 int res, fullkeylen; 00218 00219 ast_mutex_lock(&dblock); 00220 if (dbinit()) { 00221 ast_mutex_unlock(&dblock); 00222 return -1; 00223 } 00224 00225 fullkeylen = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, keys); 00226 memset(&key, 0, sizeof(key)); 00227 key.data = fullkey; 00228 key.size = fullkeylen + 1; 00229 00230 res = astdb->del(astdb, &key, 0); 00231 astdb->sync(astdb, 0); 00232 00233 ast_mutex_unlock(&dblock); 00234 00235 if (res) 00236 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family); 00237 return res; 00238 }
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.
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 457 of file db.c.
References free, last, and ast_db_entry::next.
00458 { 00459 struct ast_db_entry *last; 00460 while (dbe) { 00461 last = dbe; 00462 dbe = dbe->next; 00463 free(last); 00464 } 00465 }
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, and LOG_NOTICE.
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 ast_log(LOG_DEBUG, "Unable to find key '%s' in family '%s'\n", keys, family); 00198 } else { 00199 #if 0 00200 printf("Got value of size %d\n", data.size); 00201 #endif 00202 if (data.size) { 00203 ((char *)data.data)[data.size - 1] = '\0'; 00204 /* Make sure that we don't write too much to the dst pointer or we don't read too much from the source pointer */ 00205 ast_copy_string(value, data.data, (valuelen > data.size) ? data.size : valuelen); 00206 } else { 00207 ast_log(LOG_NOTICE, "Strange, empty value for /%s/%s\n", family, keys); 00208 } 00209 } 00210 return res; 00211 }
struct ast_db_entry* ast_db_gettree | ( | const char * | family, | |
const char * | keytree | |||
) | [read] |
Definition at line 395 of file db.c.
References ast_log(), ast_malloc, ast_mutex_lock(), ast_mutex_unlock(), ast_strlen_zero(), ast_db_entry::data, dbinit(), ast_db_entry::key, key(), keymatch(), keys, last, LOG_WARNING, ast_db_entry::next, and prefix.
00396 { 00397 char prefix[256]; 00398 DBT key, data; 00399 char *keys, *values; 00400 int values_len; 00401 int res; 00402 int pass; 00403 struct ast_db_entry *last = NULL; 00404 struct ast_db_entry *cur, *ret=NULL; 00405 00406 if (!ast_strlen_zero(family)) { 00407 if (!ast_strlen_zero(keytree)) { 00408 /* Family and key tree */ 00409 snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix); 00410 } else { 00411 /* Family only */ 00412 snprintf(prefix, sizeof(prefix), "/%s", family); 00413 } 00414 } else { 00415 prefix[0] = '\0'; 00416 } 00417 ast_mutex_lock(&dblock); 00418 if (dbinit()) { 00419 ast_mutex_unlock(&dblock); 00420 ast_log(LOG_WARNING, "Database unavailable\n"); 00421 return NULL; 00422 } 00423 memset(&key, 0, sizeof(key)); 00424 memset(&data, 0, sizeof(data)); 00425 pass = 0; 00426 while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) { 00427 if (key.size) { 00428 keys = key.data; 00429 keys[key.size - 1] = '\0'; 00430 } else { 00431 keys = "<bad key>"; 00432 } 00433 if (data.size) { 00434 values = data.data; 00435 values[data.size - 1] = '\0'; 00436 } else { 00437 values = "<bad value>"; 00438 } 00439 values_len = strlen(values) + 1; 00440 if (keymatch(keys, prefix) && (cur = ast_malloc(sizeof(*cur) + strlen(keys) + 1 + values_len))) { 00441 cur->next = NULL; 00442 cur->key = cur->data + values_len; 00443 strcpy(cur->data, values); 00444 strcpy(cur->key, keys); 00445 if (last) { 00446 last->next = cur; 00447 } else { 00448 ret = cur; 00449 } 00450 last = cur; 00451 } 00452 } 00453 ast_mutex_unlock(&dblock); 00454 return ret; 00455 }
int ast_db_put | ( | const char * | family, | |
const char * | key, | |||
char * | value | |||
) |
Definition at line 145 of file db.c.
References ast_log(), ast_mutex_lock(), ast_mutex_unlock(), dbinit(), key(), and LOG_WARNING.
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 = 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 }