Skip to content
Snippets Groups Projects
Commit 14ee4059 authored by Lyndon Brown's avatar Lyndon Brown
Browse files

core: add vlc_MakeTmpFile() helper

to avoid messy and duplicated code creating temporary files.
parent c31315c0
No related branches found
No related tags found
No related merge requests found
......@@ -99,6 +99,21 @@ VLC_API int vlc_openat(int fd, const char *filename, int flags, ...) VLC_USED;
VLC_API int vlc_mkstemp( char * );
/**
* Temporary file creation helper
*
* Creates a unique temporary file, using config_GetTempPath() to determine the
* directory in which to create it, and using vlc_mkstemp() for the creation.
*
* Note, the returned `path` pointer must be free'd with free(), except when
* the returned file descriptor is -1 indicating error, where is it undefined.
*
* @param path [OUT] full path of the created file.
* @param filetemplate filename template, which must end with a sequence of 'XXXXXX'.
* @return file descriptor, or -1 on error.
*/
VLC_API int vlc_MakeTmpFile( char **path, const char *filetemplate ) VLC_USED;
/**
* Duplicates a file descriptor. The new file descriptor has the close-on-exec
* descriptor flag preset.
......
......@@ -432,6 +432,7 @@ vlc_loaddir
vlc_lstat
vlc_mkdir
vlc_mkstemp
vlc_MakeTmpFile
vlc_open
vlc_openat
vlc_memfd
......
......@@ -30,6 +30,7 @@
#include <vlc_common.h>
#include <vlc_fs.h>
#include <vlc_configuration.h>
#include <vlc_sort.h>
#include <assert.h>
......@@ -242,3 +243,21 @@ VLC_WEAK int vlc_mkstemp(char *template)
errno = EEXIST;
return -1;
}
int vlc_MakeTmpFile( char **path, const char *filetemplate )
{
char *tmpdir = config_GetTempPath();
if (tmpdir == NULL)
return -1;
if (asprintf(path, "%s"DIR_SEP"%s", tmpdir, filetemplate) < 0) {
free(tmpdir);
return -1;
}
free(tmpdir);
int fd = vlc_mkstemp(*path);
if (fd == -1)
free(*path);
return fd;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment