[asterisk-commits] dlee: branch 1.8 r374426 - in /branches/1.8: main/db.c res/res_agi.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Thu Oct 4 10:25:55 CDT 2012


Author: dlee
Date: Thu Oct  4 10:25:50 2012
New Revision: 374426

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=374426
Log:
Fix DBDelTree error codes for AMI, CLI and AGI

The AMI DBDelTree command will return Success/Key tree deleted successfully even
if the given key does not exist. The CLI command 'database deltree' had a
similar problem, but was saved because it actually responded with '0 database
entries removed'. AGI had a slightly different error, where it would return
success if the database was unavailable.

This came from confusion about the ast_db_deltree retval, which is -1 in the
event of a database error, or number of entries deleted (including 0 for
deleting nothing).

* Adds a Doxygen comment to process_db_keys explaining its retval
* Changed some poorly named res variables to num_deleted
* Specified specific errors when calling ast_db_deltree (database unavailable
  vs. entry not found vs. success)
* Fixed similar bug in AGI database deltree, where 'Database unavailable'
  results in successful result

(closes issue AST-967)
Reported by: John Bigelow
Review: https://reviewboard.asterisk.org/r/2138/

Modified:
    branches/1.8/main/db.c
    branches/1.8/res/res_agi.c

Modified: branches/1.8/main/db.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.8/main/db.c?view=diff&rev=374426&r1=374425&r2=374426
==============================================================================
--- branches/1.8/main/db.c (original)
+++ branches/1.8/main/db.c Thu Oct  4 10:25:50 2012
@@ -167,6 +167,16 @@
 	return S_OR(dbt_data2str(dbt), def);
 }
 
+/*!
+ * \internal
+ * \brief Invoke a callback function on all keys, using given data and filter.
+ *
+ * \param cb      Callback function to invoke (itself returns number of keys it affected).
+ * \param data    Value to pass to cb's data param.
+ * \param filter  Value to pass to cb's filter param.
+ * \param sync    If non-zero, call db_sync() when done.
+ * \return Number of keys affected by the callback, or -1 if database is unavailable.
+ */
 static int process_db_keys(process_keys_cb cb, void *data, const char *filter, int sync)
 {
 	DBT key = { 0, }, value = { 0, }, last_key = { 0, };
@@ -460,7 +470,7 @@
 
 static char *handle_cli_database_deltree(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-	int res;
+	int num_deleted;
 
 	switch (cmd) {
 	case CLI_INIT:
@@ -479,14 +489,16 @@
 	if ((a->argc < 3) || (a->argc > 4))
 		return CLI_SHOWUSAGE;
 	if (a->argc == 4) {
-		res = ast_db_deltree(a->argv[2], a->argv[3]);
-	} else {
-		res = ast_db_deltree(a->argv[2], NULL);
-	}
-	if (res < 0) {
+		num_deleted = ast_db_deltree(a->argv[2], a->argv[3]);
+	} else {
+		num_deleted = ast_db_deltree(a->argv[2], NULL);
+	}
+	if (num_deleted < 0) {
+		ast_cli(a->fd, "Database unavailable.\n");
+	} else if (num_deleted == 0) {
 		ast_cli(a->fd, "Database entries do not exist.\n");
 	} else {
-		ast_cli(a->fd, "%d database entries removed.\n",res);
+		ast_cli(a->fd, "%d database entries removed.\n",num_deleted);
 	}
 	return CLI_SUCCESS;
 }
@@ -751,23 +763,27 @@
 {
 	const char *family = astman_get_header(m, "Family");
 	const char *key = astman_get_header(m, "Key");
-	int res;
+	int num_deleted;
 
 	if (ast_strlen_zero(family)) {
 		astman_send_error(s, m, "No family specified.");
 		return 0;
 	}
 
-	if (!ast_strlen_zero(key))
-		res = ast_db_deltree(family, key);
-	else
-		res = ast_db_deltree(family, NULL);
-
-	if (res < 0)
+	if (!ast_strlen_zero(key)) {
+		num_deleted = ast_db_deltree(family, key);
+	} else {
+		num_deleted = ast_db_deltree(family, NULL);
+	}
+
+	if (num_deleted < 0) {
+		astman_send_error(s, m, "Database unavailable");
+	} else if (num_deleted == 0) {
 		astman_send_error(s, m, "Database entry not found");
-	else
+	} else {
 		astman_send_ack(s, m, "Key tree deleted successfully");
-	
+	}
+
 	return 0;
 }
 

Modified: branches/1.8/res/res_agi.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.8/res/res_agi.c?view=diff&rev=374426&r1=374425&r2=374426
==============================================================================
--- branches/1.8/res/res_agi.c (original)
+++ branches/1.8/res/res_agi.c Thu Oct  4 10:25:50 2012
@@ -2692,16 +2692,18 @@
 
 static int handle_dbdeltree(struct ast_channel *chan, AGI *agi, int argc, const char * const argv[])
 {
-	int res;
-
-	if ((argc < 3) || (argc > 4))
-		return RESULT_SHOWUSAGE;
-	if (argc == 4)
-		res = ast_db_deltree(argv[2], argv[3]);
-	else
-		res = ast_db_deltree(argv[2], NULL);
-
-	ast_agi_send(agi->fd, chan, "200 result=%c\n", res ? '0' : '1');
+	int num_deleted;
+
+	if ((argc < 3) || (argc > 4)) {
+		return RESULT_SHOWUSAGE;
+	}
+	if (argc == 4) {
+		num_deleted = ast_db_deltree(argv[2], argv[3]);
+	} else {
+		num_deleted = ast_db_deltree(argv[2], NULL);
+	}
+
+	ast_agi_send(agi->fd, chan, "200 result=%c\n", num_deleted > 0 ? '0' : '1');
 	return RESULT_SUCCESS;
 }
 




More information about the asterisk-commits mailing list