[vlc-devel] [PATCH] api: add vlc_gettmpdir

Francois Cartegnie fcvlcdev at free.fr
Wed Feb 12 15:49:17 CET 2014


We have no function for implementation defined temporary dir, and tempnam
is unsafe.
That's existing code from es_out_timeshift.


---
 include/vlc_fs.h             |  1 +
 src/input/es_out_timeshift.c | 39 +--------------------------------------
 src/libvlccore.sym           |  1 +
 src/text/filesystem.c        | 40 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 43 insertions(+), 38 deletions(-)

diff --git a/include/vlc_fs.h b/include/vlc_fs.h
index 533aa9b..7a98f0d 100644
--- a/include/vlc_fs.h
+++ b/include/vlc_fs.h
@@ -101,6 +101,7 @@ VLC_API int vlc_stat( const char *filename, struct stat *buf );
 VLC_API int vlc_lstat( const char *filename, struct stat *buf );
 
 VLC_API int vlc_mkstemp( char * );
+VLC_API char *vlc_gettmpdir( void );
 
 VLC_API int vlc_dup( int );
 VLC_API int vlc_pipe( int[2] );
diff --git a/src/input/es_out_timeshift.c b/src/input/es_out_timeshift.c
index c12d73a..9c8fe59 100644
--- a/src/input/es_out_timeshift.c
+++ b/src/input/es_out_timeshift.c
@@ -31,17 +31,11 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <assert.h>
-#if defined (_WIN32)
-#  include <direct.h>
-#endif
 #include <sys/stat.h>
 #include <unistd.h>
 
 #include <vlc_common.h>
 #include <vlc_fs.h>
-#ifdef _WIN32
-#  include <vlc_charset.h>
-#endif
 #include <vlc_input.h>
 #include <vlc_es_out.h>
 #include <vlc_block.h>
@@ -1550,38 +1544,7 @@ static char *GetTmpPath( char *psz_path )
     }
     free( psz_path );
 
-    /* Create a suitable path */
-#if defined (_WIN32) && !VLC_WINSTORE_APP
-    const DWORD dwCount = GetTempPathW( 0, NULL );
-    wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
-    if( psw_path )
-    {
-        if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
-        {
-            free( psw_path );
-
-            psw_path = _wgetcwd( NULL, 0 );
-        }
-    }
-
-    psz_path = NULL;
-    if( psw_path )
-    {
-        psz_path = FromWide( psw_path );
-        while( psz_path && *psz_path && psz_path[strlen( psz_path ) - 1] == '\\' )
-            psz_path[strlen( psz_path ) - 1] = '\0';
-
-        free( psw_path );
-    }
-
-    if( !psz_path || *psz_path == '\0' )
-    {
-        free( psz_path );
-        return strdup( "C:" );
-    }
-#else
-    psz_path = strdup( DIR_SEP"tmp" );
-#endif
+    psz_path = vlc_gettmpdir();
 
     return psz_path;
 }
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 1ed906a..430feba 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -430,6 +430,7 @@ vlc_loaddir
 vlc_lstat
 vlc_mkdir
 vlc_mkstemp
+vlc_gettmpdir
 vlc_open
 vlc_openat
 vlc_opendir
diff --git a/src/text/filesystem.c b/src/text/filesystem.c
index 31d4dc6..24af770 100644
--- a/src/text/filesystem.c
+++ b/src/text/filesystem.c
@@ -31,6 +31,10 @@
 #include <vlc_common.h>
 #include <vlc_fs.h>
 #include <vlc_rand.h>
+#ifdef _WIN32
+#  include <direct.h>
+#  include <vlc_charset.h>
+#endif
 
 #include <assert.h>
 
@@ -231,3 +235,39 @@ int vlc_mkstemp( char *template )
     errno = EEXIST;
     return -1;
 }
+
+char *vlc_gettmpdir( void )
+{
+    char *psz_dir = NULL;
+#if defined (_WIN32) && !VLC_WINSTORE_APP
+    const DWORD dwCount = GetTempPathW( 0, NULL );
+    wchar_t *psw_path = calloc( dwCount + 1, sizeof(wchar_t) );
+    if( psw_path )
+    {
+        if( GetTempPathW( dwCount + 1, psw_path ) <= 0 )
+        {
+            free( psw_path );
+
+            psw_path = _wgetcwd( NULL, 0 );
+        }
+    }
+
+    if( psw_path )
+    {
+        psz_dir = FromWide( psw_path );
+        while( psz_dir && *psz_dir && psz_dir[strlen( psz_dir ) - 1] == '\\' )
+            psz_dir[strlen( psz_dir ) - 1] = '\0';
+
+        free( psw_path );
+    }
+
+    if( !psz_dir || *psz_dir == '\0' )
+    {
+        free( psz_dir );
+        return strdup( "C:" );
+    }
+#else
+    psz_dir = strdup( DIR_SEP"tmp" );
+#endif
+    return psz_dir;
+}
-- 
1.8.5.3




More information about the vlc-devel mailing list