<p>Kevin Harwell <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/17963">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  Benjamin Keith Ford: Looks good to me, approved
  Kevin Harwell: Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_stir_shaken: refactor utility function<br><br>Refactors temp file utility function into file.c.<br><br>ASTERISK-29809 #close<br><br>Change-Id: Ife478708c8f2b127239cb73c1755ef18c0bf431b<br>---<br>M include/asterisk/file.h<br>M main/file.c<br>M res/res_stir_shaken/curl.c<br>3 files changed, 41 insertions(+), 34 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/file.h b/include/asterisk/file.h</span><br><span>index b2da2a2..4e7c505 100644</span><br><span>--- a/include/asterisk/file.h</span><br><span>+++ b/include/asterisk/file.h</span><br><span>@@ -147,6 +147,23 @@</span><br><span> FILE *ast_file_mkftemp(char *template, mode_t mode);</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Create a temporary file located at path</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note The directory containing path will be created if it does not exist</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note This function assumes path does not end with a '/'</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param path The directory path to create the file in</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param filename Function allocates memory and stores full filename (including path) here</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param template_name mkstemp template to use. Must end with XXXXXX.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note filename will need to be freed with ast_free if this function succeeds</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval -1 on failure</span><br><span style="color: hsl(120, 100%, 40%);">+ * \return file descriptor on success</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int ast_file_fdtemp(const char *path, char **filename, const char *template_name);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span>  * \brief Callback called for each file found when reading directories</span><br><span>  * \param dir_name the name of the directory</span><br><span>  * \param filename the name of the file</span><br><span>diff --git a/main/file.c b/main/file.c</span><br><span>index 0c1fdd4..a6cd300 100644</span><br><span>--- a/main/file.c</span><br><span>+++ b/main/file.c</span><br><span>@@ -199,6 +199,26 @@</span><br><span>      return p;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int ast_file_fdtemp(const char *path, char **filename, const char *template_name)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      int fd;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     if (ast_asprintf(filename, "%s/%s", path, template_name) < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+          ast_log(LOG_ERROR, "Failed to set up temporary file path\n");</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%);">+   ast_mkdir(path, 0644);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      if ((fd = mkstemp(*filename)) < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+               ast_log(LOG_NOTICE, "Failed to create temporary file\n");</span><br><span style="color: hsl(120, 100%, 40%);">+           ast_free(*filename);</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%);">+   return fd;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int ast_stopstream(struct ast_channel *tmp)</span><br><span> {</span><br><span>  ast_channel_lock(tmp);</span><br><span>diff --git a/res/res_stir_shaken/curl.c b/res/res_stir_shaken/curl.c</span><br><span>index ad3adbc..97c1098 100644</span><br><span>--- a/res/res_stir_shaken/curl.c</span><br><span>+++ b/res/res_stir_shaken/curl.c</span><br><span>@@ -20,6 +20,7 @@</span><br><span> </span><br><span> #include "asterisk/utils.h"</span><br><span> #include "asterisk/logger.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/file.h"</span><br><span> #include "curl.h"</span><br><span> #include "general.h"</span><br><span> #include "stir_shaken.h"</span><br><span>@@ -151,42 +152,11 @@</span><br><span>  return curl;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-/*!</span><br><span style="color: hsl(0, 100%, 40%);">- * \brief Create a temporary file located at path</span><br><span style="color: hsl(0, 100%, 40%);">- *</span><br><span style="color: hsl(0, 100%, 40%);">- * \note This function assumes path does not end with a '/'</span><br><span style="color: hsl(0, 100%, 40%);">- *</span><br><span style="color: hsl(0, 100%, 40%);">- * \param path The directory path to create the file in</span><br><span style="color: hsl(0, 100%, 40%);">- * \param filename Function allocates memory and stores full filename (including path) here</span><br><span style="color: hsl(0, 100%, 40%);">- *</span><br><span style="color: hsl(0, 100%, 40%);">- * \retval -1 on failure</span><br><span style="color: hsl(0, 100%, 40%);">- * \return file descriptor on success</span><br><span style="color: hsl(0, 100%, 40%);">- */</span><br><span style="color: hsl(0, 100%, 40%);">-static int create_temp_file(const char *path, char **filename)</span><br><span style="color: hsl(0, 100%, 40%);">-{</span><br><span style="color: hsl(0, 100%, 40%);">-     const char *template_name = "certXXXXXX";</span><br><span style="color: hsl(0, 100%, 40%);">-     int fd;</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- if (ast_asprintf(filename, "%s/%s", path, template_name) < 0) {</span><br><span style="color: hsl(0, 100%, 40%);">-            ast_log(LOG_ERROR, "Failed to set up temporary file path for CURL\n");</span><br><span style="color: hsl(0, 100%, 40%);">-                return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-      }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-       ast_mkdir(path, 0644);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-  if ((fd = mkstemp(*filename)) < 0) {</span><br><span style="color: hsl(0, 100%, 40%);">-         ast_log(LOG_NOTICE, "Failed to create temporary file for CURL\n");</span><br><span style="color: hsl(0, 100%, 40%);">-            ast_free(*filename);</span><br><span style="color: hsl(0, 100%, 40%);">-            return -1;</span><br><span style="color: hsl(0, 100%, 40%);">-      }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">-       return fd;</span><br><span style="color: hsl(0, 100%, 40%);">-}</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> char *curl_public_key(const char *public_cert_url, const char *path, struct curl_cb_data *data)</span><br><span> {</span><br><span>  FILE *public_key_file;</span><br><span>       RAII_VAR(char *, tmp_filename, NULL, ast_free);</span><br><span style="color: hsl(120, 100%, 40%);">+       const char *template_name = "certXXXXXX";</span><br><span>  char *filename;</span><br><span>      char *serial;</span><br><span>        int fd;</span><br><span>@@ -199,9 +169,9 @@</span><br><span>        /* For now, it's fine to pass in path as is - it shouldn't end with a '/'. However,</span><br><span>   * if we decide to change how certificates are stored in the future (configurable paths),</span><br><span>     * then we will need to check to see if path ends with '/', copy everything up to the '/',</span><br><span style="color: hsl(0, 100%, 40%);">-       * and use this new variable for create_temp_file as well as for ast_asprintf below.</span><br><span style="color: hsl(120, 100%, 40%);">+   * and use this new variable for ast_create_temp_file as well as for ast_asprintf below.</span><br><span>      */</span><br><span style="color: hsl(0, 100%, 40%);">-     fd = create_temp_file(path, &tmp_filename);</span><br><span style="color: hsl(120, 100%, 40%);">+       fd = ast_file_fdtemp(path, &tmp_filename, template_name);</span><br><span>        if (fd == -1) {</span><br><span>              ast_log(LOG_ERROR, "Failed to get temporary file descriptor for CURL\n");</span><br><span>          return NULL;</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/+/17963">change 17963</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/+/17963"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 19 </div>
<div style="display:none"> Gerrit-Change-Id: Ife478708c8f2b127239cb73c1755ef18c0bf431b </div>
<div style="display:none"> Gerrit-Change-Number: 17963 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: N A <mail@interlinked.x10host.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.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-Reviewer: Sean Bright <sean@seanbright.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>