]> git.sesse.net Git - vlc/commitdiff
Win32: reimplement tmpfile()
authorJean-Baptiste Kempf <jb@videolan.org>
Mon, 16 Feb 2015 18:17:10 +0000 (19:17 +0100)
committerJean-Baptiste Kempf <jb@videolan.org>
Mon, 16 Feb 2015 19:26:27 +0000 (20:26 +0100)
Because tmpfile() cannot be used if not admin on the machine on
Windows...

Close #13642

include/vlc_fixups.h
src/text/strings.c
src/win32/filesystem.c

index cee12546ba2b21111951f5c6a8af2d25fd5af6e1..b949d24629133a5c1471a60ba27b49fee1ddc42e 100644 (file)
@@ -369,4 +369,8 @@ struct addrinfo
 #define nanf(tagp) NAN
 #endif
 
+#ifdef _WIN32
+FILE *vlc_win32_tmpfile(void);
+#endif
+
 #endif /* !LIBVLC_FIXUPS_H */
index e0fd28c2cdcd7bcdeec6d9b162b83c593cdcdc90..5063e78dad72f69222a432ace1f8844cc829afda 100644 (file)
@@ -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
index f938147021ce83e479ccda46c50c2c43ea7a1fe9..7f29c8f88a8f1df78a1d0cb50c76994f5896d3e6 100644 (file)
@@ -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;
+}
+