Eina: Add support for file extensions in eina_mkstemp
authorJean-Philippe Andre <jp.andre@samsung.com>
Tue, 1 Apr 2014 07:50:46 +0000 (16:50 +0900)
committerJean-Philippe Andre <jp.andre@samsung.com>
Wed, 2 Apr 2014 02:54:44 +0000 (11:54 +0900)
Using mkstemps

src/lib/eina/eina_file.h
src/lib/eina/eina_file_common.c

index 546a1e0..66c0594 100644 (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
index 2c733ad..a62a2ec 100644 (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);