<p>Kevin Harwell <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/18716">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Joshua Colp: Looks good to me, but someone else must approve
Kevin Harwell: Looks good to me, approved; Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">db: Notify user if deleted DB entry didn't exist.<br><br>Currently, if using the CLI to delete a DB entry,<br>"Database entry removed" is always returned,<br>regardless of whether or not the entry actually<br>existed in the first place. This meant that users<br>were never told if entries did not exist.<br><br>The same issue occurs if trying to delete a DB key<br>using AMI.<br><br>To address this, new API is added that is more stringent<br>in deleting values from AstDB, which will not return<br>success if the value did not exist in the first place,<br>and will print out specific error details if available.<br><br>ASTERISK-30001 #close<br><br>Change-Id: Ic84e3eddcd66c7a6ed7fea91cdfd402568378b18<br>---<br>M include/asterisk/astdb.h<br>M main/db.c<br>2 files changed, 47 insertions(+), 4 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/astdb.h b/include/asterisk/astdb.h</span><br><span>index c511be8..216b8d3 100644</span><br><span>--- a/include/asterisk/astdb.h</span><br><span>+++ b/include/asterisk/astdb.h</span><br><span>@@ -56,6 +56,17 @@</span><br><span> /*! \brief Delete entry in astdb */</span><br><span> int ast_db_del(const char *family, const char *key);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*! \brief Same as ast_db_del, but with more stringent error checking</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \details</span><br><span style="color: hsl(120, 100%, 40%);">+ * Unlike ast_db_del, if the key does not exist in the first place,</span><br><span style="color: hsl(120, 100%, 40%);">+ * an error is emitted and -1 is returned.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval -1 An error occured (including key not found to begin with)</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 0 Successfully deleted</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int ast_db_del2(const char *family, const char *key);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*!</span><br><span> * \brief Delete one or more entries in astdb</span><br><span> *</span><br><span>diff --git a/main/db.c b/main/db.c</span><br><span>index d4479f4..8965014 100644</span><br><span>--- a/main/db.c</span><br><span>+++ b/main/db.c</span><br><span>@@ -454,6 +454,38 @@</span><br><span> return res;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int ast_db_del2(const char *family, const char *key)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ char fullkey[MAX_DB_FIELD];</span><br><span style="color: hsl(120, 100%, 40%);">+ char tmp[1];</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t fullkey_len;</span><br><span style="color: hsl(120, 100%, 40%);">+ int mres, res = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (strlen(family) + strlen(key) + 2 > sizeof(fullkey) - 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Family and key length must be less than %zu bytes\n", sizeof(fullkey) - 3);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ fullkey_len = snprintf(fullkey, sizeof(fullkey), "/%s/%s", family, key);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&dblock);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (ast_db_get(family, key, tmp, sizeof(tmp))) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "AstDB key %s does not exist\n", fullkey);</span><br><span style="color: hsl(120, 100%, 40%);">+ res = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (sqlite3_bind_text(del_stmt, 1, fullkey, fullkey_len, SQLITE_STATIC) != SQLITE_OK) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "Couldn't bind key to stmt: %s\n", sqlite3_errmsg(astdb));</span><br><span style="color: hsl(120, 100%, 40%);">+ res = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if ((mres = sqlite3_step(del_stmt) != SQLITE_DONE)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_WARNING, "AstDB error (%s): %s\n", fullkey, sqlite3_errstr(mres));</span><br><span style="color: hsl(120, 100%, 40%);">+ res = -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ sqlite3_reset(del_stmt);</span><br><span style="color: hsl(120, 100%, 40%);">+ db_sync();</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&dblock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return res;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int ast_db_deltree(const char *family, const char *keytree)</span><br><span> {</span><br><span> sqlite3_stmt *stmt = deltree_stmt;</span><br><span>@@ -678,9 +710,9 @@</span><br><span> </span><br><span> if (a->argc != 4)</span><br><span> return CLI_SHOWUSAGE;</span><br><span style="color: hsl(0, 100%, 40%);">- res = ast_db_del(a->argv[2], a->argv[3]);</span><br><span style="color: hsl(120, 100%, 40%);">+ res = ast_db_del2(a->argv[2], a->argv[3]);</span><br><span> if (res) {</span><br><span style="color: hsl(0, 100%, 40%);">- ast_cli(a->fd, "Database entry does not exist.\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "Database entry could not be removed.\n");</span><br><span> } else {</span><br><span> ast_cli(a->fd, "Database entry removed.\n");</span><br><span> }</span><br><span>@@ -963,9 +995,9 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- res = ast_db_del(family, key);</span><br><span style="color: hsl(120, 100%, 40%);">+ res = ast_db_del2(family, key);</span><br><span> if (res)</span><br><span style="color: hsl(0, 100%, 40%);">- astman_send_error(s, m, "Database entry not found");</span><br><span style="color: hsl(120, 100%, 40%);">+ astman_send_error(s, m, "Database entry could not be deleted");</span><br><span> else</span><br><span> astman_send_ack(s, m, "Key deleted successfully");</span><br><span> </span><br><span></span><br></pre><div style="white-space:pre-wrap"></div><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/18716">change 18716</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/18716"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: Ic84e3eddcd66c7a6ed7fea91cdfd402568378b18 </div>
<div style="display:none"> Gerrit-Change-Number: 18716 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-Reviewer: Kevin Harwell <kharwell@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>