Skip to content

Commit 1633410

Browse files
InterLinked1kharwell
authored andcommitted
res_stir_shaken: refactor utility function
Refactors temp file utility function into file.c. ASTERISK-29809 #close Change-Id: Ife478708c8f2b127239cb73c1755ef18c0bf431b
1 parent 39820e3 commit 1633410

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

include/asterisk/file.h

+17
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ int ast_filecopy(const char *oldname, const char *newname, const char *fmt);
146146
*/
147147
FILE *ast_file_mkftemp(char *template, mode_t mode);
148148

149+
/*!
150+
* \brief Create a temporary file located at path
151+
*
152+
* \note The directory containing path will be created if it does not exist
153+
* \note This function assumes path does not end with a '/'
154+
*
155+
* \param path The directory path to create the file in
156+
* \param filename Function allocates memory and stores full filename (including path) here
157+
* \param template_name mkstemp template to use. Must end with XXXXXX.
158+
*
159+
* \note filename will need to be freed with ast_free if this function succeeds
160+
*
161+
* \retval -1 on failure
162+
* \return file descriptor on success
163+
*/
164+
int ast_file_fdtemp(const char *path, char **filename, const char *template_name);
165+
149166
/*!
150167
* \brief Callback called for each file found when reading directories
151168
* \param dir_name the name of the directory

main/file.c

+20
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,26 @@ FILE *ast_file_mkftemp(char *template, mode_t mode)
199199
return p;
200200
}
201201

202+
int ast_file_fdtemp(const char *path, char **filename, const char *template_name)
203+
{
204+
int fd;
205+
206+
if (ast_asprintf(filename, "%s/%s", path, template_name) < 0) {
207+
ast_log(LOG_ERROR, "Failed to set up temporary file path\n");
208+
return -1;
209+
}
210+
211+
ast_mkdir(path, 0644);
212+
213+
if ((fd = mkstemp(*filename)) < 0) {
214+
ast_log(LOG_NOTICE, "Failed to create temporary file\n");
215+
ast_free(*filename);
216+
return -1;
217+
}
218+
219+
return fd;
220+
}
221+
202222
int ast_stopstream(struct ast_channel *tmp)
203223
{
204224
ast_channel_lock(tmp);

res/res_stir_shaken/curl.c

+4-34
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "asterisk/utils.h"
2222
#include "asterisk/logger.h"
23+
#include "asterisk/file.h"
2324
#include "curl.h"
2425
#include "general.h"
2526
#include "stir_shaken.h"
@@ -151,42 +152,11 @@ static CURL *get_curl_instance(struct curl_cb_data *data)
151152
return curl;
152153
}
153154

154-
/*!
155-
* \brief Create a temporary file located at path
156-
*
157-
* \note This function assumes path does not end with a '/'
158-
*
159-
* \param path The directory path to create the file in
160-
* \param filename Function allocates memory and stores full filename (including path) here
161-
*
162-
* \retval -1 on failure
163-
* \return file descriptor on success
164-
*/
165-
static int create_temp_file(const char *path, char **filename)
166-
{
167-
const char *template_name = "certXXXXXX";
168-
int fd;
169-
170-
if (ast_asprintf(filename, "%s/%s", path, template_name) < 0) {
171-
ast_log(LOG_ERROR, "Failed to set up temporary file path for CURL\n");
172-
return -1;
173-
}
174-
175-
ast_mkdir(path, 0644);
176-
177-
if ((fd = mkstemp(*filename)) < 0) {
178-
ast_log(LOG_NOTICE, "Failed to create temporary file for CURL\n");
179-
ast_free(*filename);
180-
return -1;
181-
}
182-
183-
return fd;
184-
}
185-
186155
char *curl_public_key(const char *public_cert_url, const char *path, struct curl_cb_data *data)
187156
{
188157
FILE *public_key_file;
189158
RAII_VAR(char *, tmp_filename, NULL, ast_free);
159+
const char *template_name = "certXXXXXX";
190160
char *filename;
191161
char *serial;
192162
int fd;
@@ -199,9 +169,9 @@ char *curl_public_key(const char *public_cert_url, const char *path, struct curl
199169
/* For now, it's fine to pass in path as is - it shouldn't end with a '/'. However,
200170
* if we decide to change how certificates are stored in the future (configurable paths),
201171
* then we will need to check to see if path ends with '/', copy everything up to the '/',
202-
* and use this new variable for create_temp_file as well as for ast_asprintf below.
172+
* and use this new variable for ast_create_temp_file as well as for ast_asprintf below.
203173
*/
204-
fd = create_temp_file(path, &tmp_filename);
174+
fd = ast_file_fdtemp(path, &tmp_filename, template_name);
205175
if (fd == -1) {
206176
ast_log(LOG_ERROR, "Failed to get temporary file descriptor for CURL\n");
207177
return NULL;

0 commit comments

Comments
 (0)