Eina: Add support for file extensions in eina_mkstemp

Using mkstemps
This commit is contained in:
Jean-Philippe Andre 2014-04-01 16:50:46 +09:00
parent 2c4ae3fe6f
commit 0fdb02fb51
2 changed files with 17 additions and 6 deletions

View File

@ -334,15 +334,19 @@ EAPI int eina_file_statat(void *container, Eina_File_Direct_Info *info, Eina_Sta
* @brief Generate and create a uniquely named temporary file from template.
* Generated file is opened with the open(2) O_EXCL flag.
*
* @param [in] templatename is a string. The last six characters of templatename must be XXXXXX.
* @param [in] templatename is a string. It must contain the six characters 'XXXXXX'
* at the end or directly followed by an extension as in 'prefixXXXXXX.ext'.
* @param [out] path Where to put the name of the created file. If not NULL
* should be released by eina_tmpstr_del.
* @return On success @c file descriptor of the temporary file is returned,
* On error @c -1 is returned, in which case errno is set appropriately.
*
* This function calls mkstemp, generates a unique temporary filename
* This function calls mkstemp[s], generates a unique temporary filename
* from template, creates and opens the file, and returns an open file
* descriptor for the file.
* descriptor for the file.
*
* If a filename extension was specified in @a templatename, then the new @a path
* will also contain this extension (since 1.10).
*
* @see eina_file_mkdtemp()
* @since 1.8

View File

@ -898,7 +898,8 @@ eina_file_mkstemp(const char *templatename, Eina_Tmpstr **path)
{
char buffer[PATH_MAX];
const char *tmpdir = NULL;
int fd;
const char *XXXXXX = NULL;
int fd, len;
mode_t old_umask;
#ifndef HAVE_EVIL
@ -911,14 +912,20 @@ eina_file_mkstemp(const char *templatename, Eina_Tmpstr **path)
tmpdir = (char *)evil_tmpdir_get();
#endif /* ! HAVE_EVIL */
snprintf(buffer, PATH_MAX, "%s/%s", tmpdir, templatename);
len = snprintf(buffer, PATH_MAX, "%s/%s", tmpdir, templatename);
/*
* Make sure temp file is created with secure permissions,
* http://man7.org/linux/man-pages/man3/mkstemp.3.html#NOTES
*/
old_umask = umask(S_IRWXG|S_IRWXO);
fd = mkstemp(buffer);
if ((XXXXXX = strstr(templatename, "XXXXXX.")) != NULL)
{
int suffixlen = templatename + len - XXXXXX - 6;
fd = mkstemps(buffer, suffixlen);
}
else
fd = mkstemp(buffer);
umask(old_umask);
if (path) *path = eina_tmpstr_add(buffer);