[vlc-devel] [PATCH v2 3/13] platform: avoid hard coded temp dir path in vlc_memfd()

Lyndon Brown jnqnfe at gmail.com
Tue Oct 6 07:57:32 CEST 2020


From: Lyndon Brown <jnqnfe at gmail.com>
Date: Mon, 5 Oct 2020 21:07:14 +0100
Subject: platform: avoid hard coded temp dir path in vlc_memfd()

on posix/linux the $TMPDIR path should be preferred, using "/tmp" as a
fallback. here we use our new config_GetTempPath() helper.

diff --git a/src/linux/filesystem.c b/src/linux/filesystem.c
index e94a60ec45..efdff12580 100644
--- a/src/linux/filesystem.c
+++ b/src/linux/filesystem.c
@@ -33,6 +33,7 @@
 
 #include <vlc_common.h>
 #include <vlc_fs.h>
+#include <vlc_configuration.h>
 
 int vlc_memfd(void)
 {
@@ -43,16 +44,30 @@ int vlc_memfd(void)
         return fd;
 #endif
 
+    char *tempdir = config_GetTempPath();
+    if (tempdir == NULL)
+        return -1;
+
     /* Fallback to open with O_TMPFILE, */
-    fd = open("/tmp", O_RDWR | O_CLOEXEC | O_TMPFILE, S_IRUSR | S_IWUSR);
-    if (fd != -1 || (errno != EISDIR && errno != EOPNOTSUPP))
+    fd = open(tempdir, O_RDWR | O_CLOEXEC | O_TMPFILE, S_IRUSR | S_IWUSR);
+    if (fd != -1 || (errno != EISDIR && errno != EOPNOTSUPP)) {
+        free(tempdir);
         return fd;
+    }
 
     /* Fallback to POSIX implementation if O_TMPFILE is not supported (errno is
      * EISDIR, or EOPNOTSUPP, cf. man open(2). */
-    char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
+    const char *filetemplate = PACKAGE_NAME"XXXXXX";
+    char *bufpath;
+    if (asprintf(&bufpath, "%s/%s", tempdir, filetemplate) == -1) {
+        free(tempdir);
+        return -1;
+    }
+    free(tempdir);
+
     fd = vlc_mkstemp(bufpath);
     if (fd != -1)
         unlink(bufpath);
+    free(bufpath);
     return fd;
 }
diff --git a/src/posix/filesystem.c b/src/posix/filesystem.c
index 60a997d9e0..1cf5df224d 100644
--- a/src/posix/filesystem.c
+++ b/src/posix/filesystem.c
@@ -44,6 +44,7 @@
 
 #include <vlc_common.h>
 #include <vlc_fs.h>
+#include <vlc_configuration.h>
 
 #if !defined(HAVE_ACCEPT4)
 static inline void vlc_cloexec(int fd)
@@ -90,12 +91,22 @@ int vlc_mkstemp (char *template)
 
 VLC_WEAK int vlc_memfd(void)
 {
-    char bufpath[] = "/tmp/"PACKAGE_NAME"XXXXXX";
-    int fd;
+    char *tempdir = config_GetTempPath();
+    if (tempdir == NULL)
+        return -1;
+
+    const char *filetemplate = PACKAGE_NAME"XXXXXX";
+    char *bufpath;
+    if (asprintf(&bufpath, "%s/%s", tempdir, filetemplate) == -1) {
+        free(tempdir);
+        return -1;
+    }
+    free(tempdir);
 
-    fd = vlc_mkstemp (bufpath);
+    int fd = vlc_mkstemp(bufpath);
     if (fd != -1)
         unlink (bufpath);
+    free(bufpath);
     return fd;
 }
 



More information about the vlc-devel mailing list