[Asterisk-code-review] db: Notify user if deleted DB entry didn't exist. (asterisk[master])

N A asteriskteam at digium.com
Fri Apr 1 14:55:30 CDT 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/18361 )


Change subject: db: Notify user if deleted DB entry didn't exist.
......................................................................

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.

This fixes this issue by first attempting to retrieve
the entry to be deleted, so we can inform the user
if it does not exist.

ASTERISK-30001 #close

Change-Id: Ic84e3eddcd66c7a6ed7fea91cdfd402568378b18
---
M main/db.c
1 file changed, 16 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/61/18361/1

diff --git a/main/db.c b/main/db.c
index d4479f4..54834b0 100644
--- a/main/db.c
+++ b/main/db.c
@@ -663,6 +663,7 @@
 static char *handle_cli_database_del(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	int res;
+	char buf[1];
 
 	switch (cmd) {
 	case CLI_INIT:
@@ -678,9 +679,16 @@
 
 	if (a->argc != 4)
 		return CLI_SHOWUSAGE;
+
+	/* ast_db_del doesn't care if something didn't exist, so check first */
+	if (ast_db_get(a->argv[2], a->argv[3], buf, 1)) {
+		ast_cli(a->fd, "Database entry does not exist.\n");
+		return CLI_SUCCESS;
+	}
+
 	res = ast_db_del(a->argv[2], a->argv[3]);
 	if (res) {
-		ast_cli(a->fd, "Database entry does not exist.\n");
+		ast_cli(a->fd, "Database error occured.\n");
 	} else {
 		ast_cli(a->fd, "Database entry removed.\n");
 	}
@@ -952,6 +960,7 @@
 	const char *family = astman_get_header(m, "Family");
 	const char *key = astman_get_header(m, "Key");
 	int res;
+	char buf[1];
 
 	if (ast_strlen_zero(family)) {
 		astman_send_error(s, m, "No family specified.");
@@ -963,9 +972,14 @@
 		return 0;
 	}
 
+	if (ast_db_get(family, key, buf, 1)) {
+		astman_send_error(s, m, "Database entry not found");
+		return 0;
+	}
+
 	res = ast_db_del(family, key);
 	if (res)
-		astman_send_error(s, m, "Database entry not found");
+		astman_send_error(s, m, "Database error occured");
 	else
 		astman_send_ack(s, m, "Key deleted successfully");
 

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/18361
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Ic84e3eddcd66c7a6ed7fea91cdfd402568378b18
Gerrit-Change-Number: 18361
Gerrit-PatchSet: 1
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220401/a8a8829e/attachment.html>


More information about the asterisk-code-review mailing list