[vlc-commits] Win32: reimplement tmpfile()

Jean-Baptiste Kempf git at videolan.org
Tue Feb 17 10:08:24 CET 2015


vlc/vlc-2.2 | branch: master | Jean-Baptiste Kempf <jb at videolan.org> | Mon Feb 16 19:17:10 2015 +0100| [99ba0127a9b3552b0e5cce3356c92e44e07cb2f1] | committer: Jean-Baptiste Kempf

Win32: reimplement tmpfile()

Because tmpfile() cannot be used if not admin on the machine on
Windows...

Close #13642

(cherry picked from commit 3f08049ef7f3a963774590c0570d4b41cd777c65)
Signed-off-by: Jean-Baptiste Kempf <jb at videolan.org>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-2.2.git/?a=commit;h=99ba0127a9b3552b0e5cce3356c92e44e07cb2f1
---

 include/vlc_fixups.h   |    4 ++++
 src/text/strings.c     |    2 ++
 src/win32/filesystem.c |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 39 insertions(+)

diff --git a/include/vlc_fixups.h b/include/vlc_fixups.h
index b448d83..997f600 100644
--- a/include/vlc_fixups.h
+++ b/include/vlc_fixups.h
@@ -361,4 +361,8 @@ struct addrinfo
 #define nanf(tagp) NAN
 #endif
 
+#ifdef _WIN32
+FILE *vlc_win32_tmpfile(void);
+#endif
+
 #endif /* !LIBVLC_FIXUPS_H */
diff --git a/src/text/strings.c b/src/text/strings.c
index 5da1b7a..a9464f8 100644
--- a/src/text/strings.c
+++ b/src/text/strings.c
@@ -534,6 +534,8 @@ char *str_format_meta(input_thread_t *input, const char *s)
     size_t len;
 #ifdef HAVE_OPEN_MEMSTREAM
     FILE *stream = open_memstream(&str, &len);
+#elif defined( _WIN32 )
+    FILE *stream = vlc_win32_tmpfile();
 #else
     FILE *stream = tmpfile();
 #endif
diff --git a/src/win32/filesystem.c b/src/win32/filesystem.c
index f938147..7f29c8f 100644
--- a/src/win32/filesystem.c
+++ b/src/win32/filesystem.c
@@ -300,3 +300,36 @@ int vlc_accept (int lfd, struct sockaddr *addr, socklen_t *alen, bool nonblock)
         ioctlsocket (fd, FIONBIO, &(unsigned long){ 1 });
     return fd;
 }
+
+FILE *vlc_win32_tmpfile(void)
+{
+    TCHAR tmp_path[MAX_PATH-14];
+    int i_ret = GetTempPath (MAX_PATH-14, tmp_path);
+    if (i_ret == 0)
+        return NULL;
+
+    TCHAR tmp_name[MAX_PATH];
+    i_ret = GetTempFileName(tmp_path, TEXT("VLC"), 0, tmp_name);
+    if (i_ret == 0)
+        return NULL;
+
+    HANDLE hFile = CreateFile(tmp_name,
+            GENERIC_READ | GENERIC_WRITE | DELETE, 0, NULL, CREATE_ALWAYS,
+            FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL);
+    if (hFile == INVALID_HANDLE_VALUE)
+        return NULL;
+
+    int fd = _open_osfhandle((intptr_t)hFile, 0);
+    if (fd == -1) {
+        CloseHandle(hFile);
+        return NULL;
+    }
+
+    FILE *stream = _fdopen(fd, "w+b");
+    if (stream == NULL) {
+        _close(fd);
+        return NULL;
+    }
+    return stream;
+}
+



More information about the vlc-commits mailing list