[vlc-devel] [PATCH v2 9/13] fs: add optional dir param to vlc_MakeTmpFile()

Lyndon Brown jnqnfe at gmail.com
Tue Oct 6 08:00:25 CEST 2020


From: Lyndon Brown <jnqnfe at gmail.com>
Date: Tue, 6 Oct 2020 01:27:52 +0100
Subject: fs: add optional dir param to vlc_MakeTmpFile()

to avoid code duplication in cases where caller wants to possibly control
the directory chosen rather than just rely on default temporary directory.

diff --git a/include/vlc_fs.h b/include/vlc_fs.h
index 50ec52c299..07bf682c66 100644
--- a/include/vlc_fs.h
+++ b/include/vlc_fs.h
@@ -103,16 +103,19 @@ 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.
+ * directory in which to create it (unless the caller specifies an alternaive
+ * location), 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'.
+ * @param directory an optional caller chosen directory to use instead of the one
+ *                  returned by config_GetTempPath().
  * @return file descriptor, or -1 on error.
  */
-VLC_API int vlc_MakeTmpFile( char **path, const char *filetemplate ) VLC_USED;
+VLC_API int vlc_MakeTmpFile( char **path, const char *filetemplate, const char *directory ) VLC_USED;
 
 /**
  * Duplicates a file descriptor. The new file descriptor has the close-on-exec
diff --git a/src/linux/filesystem.c b/src/linux/filesystem.c
index c2ec9c3a5a..d08e7a59ca 100644
--- a/src/linux/filesystem.c
+++ b/src/linux/filesystem.c
@@ -59,7 +59,7 @@ int vlc_memfd(void)
      * EISDIR, or EOPNOTSUPP, cf. man open(2). */
     const char *filetemplate = PACKAGE_NAME"XXXXXX";
     char *bufpath;
-    fd = vlc_MakeTmpFile(&bufpath, filetemplate);
+    fd = vlc_MakeTmpFile(&bufpath, filetemplate, NULL);
 
     if (fd != -1)
         unlink(bufpath);
diff --git a/src/posix/filesystem.c b/src/posix/filesystem.c
index 01d4aa1ab0..6e8332ebc2 100644
--- a/src/posix/filesystem.c
+++ b/src/posix/filesystem.c
@@ -92,7 +92,7 @@ VLC_WEAK int vlc_memfd(void)
 {
     const char *filetemplate = PACKAGE_NAME"XXXXXX";
     char *bufpath;
-    int fd = vlc_MakeTmpFile(&bufpath, filetemplate);
+    int fd = vlc_MakeTmpFile(&bufpath, filetemplate, NULL);
 
     if (fd != -1)
         unlink (bufpath);
diff --git a/src/text/filesystem.c b/src/text/filesystem.c
index fa518f6b6e..f73f16bac0 100644
--- a/src/text/filesystem.c
+++ b/src/text/filesystem.c
@@ -244,20 +244,25 @@ VLC_WEAK int vlc_mkstemp(char *template)
     return -1;
 }
 
-int vlc_MakeTmpFile( char **path, const char *filetemplate )
+int vlc_MakeTmpFile( char **path, const char *filetemplate, const char *directory )
 {
-    char *tmpdir = config_GetTempPath();
-    if (tmpdir == NULL)
-        return -1;
+    char *dir_actual;
+    if (directory != NULL)
+        dir_actual = (char *)directory;
+    else {
+        dir_actual = config_GetTempPath();
+        if (dir_actual == NULL)
+            return -1;
+    }
 
-    if (asprintf(path, "%s"DIR_SEP"%s", tmpdir, filetemplate) < 0) {
-        free(tmpdir);
-        return -1;
+    int fd = -1;
+    if (asprintf(path, "%s"DIR_SEP"%s", dir_actual, filetemplate) >= 0) {
+        fd = vlc_mkstemp(*path);
+        if (fd == -1)
+            free(*path);
     }
-    free(tmpdir);
 
-    int fd = vlc_mkstemp(*path);
-    if (fd == -1)
-        free(*path);
+    if (directory == NULL)
+        free(dir_actual);
     return fd;
 }



More information about the vlc-devel mailing list