From 0fdb02fb51c41ddc57ec83950dd847557820aae1 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Andre Date: Tue, 1 Apr 2014 16:50:46 +0900 Subject: [PATCH] Eina: Add support for file extensions in eina_mkstemp Using mkstemps --- src/lib/eina/eina_file.h | 10 +++++++--- src/lib/eina/eina_file_common.c | 13 ++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/lib/eina/eina_file.h b/src/lib/eina/eina_file.h index 546a1e0863..66c05948cc 100644 --- a/src/lib/eina/eina_file.h +++ b/src/lib/eina/eina_file.h @@ -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 diff --git a/src/lib/eina/eina_file_common.c b/src/lib/eina/eina_file_common.c index 2c733ad322..a62a2ec1fc 100644 --- a/src/lib/eina/eina_file_common.c +++ b/src/lib/eina/eina_file_common.c @@ -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);