[Asterisk-code-review] res_stir_shaken: refactor utility function (asterisk[master])

Kevin Harwell asteriskteam at digium.com
Wed Feb 23 17:04:53 CST 2022


Kevin Harwell has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/17708 )

Change subject: res_stir_shaken: refactor utility function
......................................................................

res_stir_shaken: refactor utility function

Refactors temp file utility function into file.c.

ASTERISK-29809 #close

Change-Id: Ife478708c8f2b127239cb73c1755ef18c0bf431b
---
M include/asterisk/file.h
M main/file.c
M res/res_stir_shaken/curl.c
3 files changed, 41 insertions(+), 34 deletions(-)

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



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

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Ife478708c8f2b127239cb73c1755ef18c0bf431b
Gerrit-Change-Number: 17708
Gerrit-PatchSet: 10
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-Reviewer: Benjamin Keith Ford <bford at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Sean Bright <sean at seanbright.com>
Gerrit-CC: George Joseph <gjoseph at digium.com>
Gerrit-CC: Mark Murawski <markm at intellasoft.net>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20220223/ece7bd78/attachment-0001.html>


More information about the asterisk-code-review mailing list