Skip to content

Commit 350ffcb

Browse files
InterLinked1kharwell
authored andcommitted
db: Notify user if deleted DB entry didn't exist.
Currently, if using the CLI to delete a DB entry, "Database entry removed" is always returned, regardless of whether or not the entry actually existed in the first place. This meant that users were never told if entries did not exist. The same issue occurs if trying to delete a DB key using AMI. To address this, new API is added that is more stringent in deleting values from AstDB, which will not return success if the value did not exist in the first place, and will print out specific error details if available. ASTERISK-30001 #close Change-Id: Ic84e3eddcd66c7a6ed7fea91cdfd402568378b18
1 parent b841845 commit 350ffcb

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

Diff for: include/asterisk/astdb.h

+11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ int ast_db_put(const char *family, const char *key, const char *value);
5656
/*! \brief Delete entry in astdb */
5757
int ast_db_del(const char *family, const char *key);
5858

59+
/*! \brief Same as ast_db_del, but with more stringent error checking
60+
*
61+
* \details
62+
* Unlike ast_db_del, if the key does not exist in the first place,
63+
* an error is emitted and -1 is returned.
64+
*
65+
* \retval -1 An error occured (including key not found to begin with)
66+
* \retval 0 Successfully deleted
67+
*/
68+
int ast_db_del2(const char *family, const char *key);
69+
5970
/*!
6071
* \brief Delete one or more entries in astdb
6172
*

Diff for: main/db.c

+36-4
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,38 @@ int ast_db_del(const char *family, const char *key)
454454
return res;
455455
}
456456

457+
int ast_db_del2(const char *family, const char *key)
458+
{
459+
char fullkey[MAX_DB_FIELD];
460+
char tmp[1];
461+
size_t fullkey_len;
462+
int mres, res = 0;
463+
464+
if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {
465+
ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);
466+
return -1;
467+
}
468+
469+
fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);
470+
471+
ast_mutex_lock(&dblock);
472+
if (ast_db_get(family, key, tmp, sizeof(tmp))) {
473+
ast_log(LOG_WARNING, "AstDB key %s does not exist\n", fullkey);
474+
res = -1;
475+
} else if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {
476+
ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));
477+
res = -1;
478+
} else if ((mres = sqlite3_step(del_stmt) != SQLITE_DONE)) {
479+
ast_log(LOG_WARNING, "AstDB error (%s): %s\n", fullkey, sqlite3_errstr(mres));
480+
res = -1;
481+
}
482+
sqlite3_reset(del_stmt);
483+
db_sync();
484+
ast_mutex_unlock(&dblock);
485+
486+
return res;
487+
}
488+
457489
int ast_db_deltree(const char *family, const char *keytree)
458490
{
459491
sqlite3_stmt *stmt = deltree_stmt;
@@ -678,9 +710,9 @@ static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct as
678710

679711
if (a->argc != 4)
680712
return CLI_SHOWUSAGE;
681-
res = ast_db_del(a->argv[2], a->argv[3]);
713+
res = ast_db_del2(a->argv[2], a->argv[3]);
682714
if (res) {
683-
ast_cli(a->fd, "Database entry does not exist.\n");
715+
ast_cli(a->fd, "Database entry could not be removed.\n");
684716
} else {
685717
ast_cli(a->fd, "Database entry removed.\n");
686718
}
@@ -963,9 +995,9 @@ static int manager_dbdel(struct mansession *s, const struct message *m)
963995
return 0;
964996
}
965997

966-
res = ast_db_del(family, key);
998+
res = ast_db_del2(family, key);
967999
if (res)
968-
astman_send_error(s, m, "Database entry not found");
1000+
astman_send_error(s, m, "Database entry could not be deleted");
9691001
else
9701002
astman_send_ack(s, m, "Key deleted successfully");
9711003

0 commit comments

Comments
 (0)