@@ -885,7 +885,15 @@
#ifdef _WIN32
/* On Windows, find temp dir via environment variables */
o->tmp_dir = win_get_tempdir();
-#else
+
+ if (!o->tmp_dir)
+ {
+ /* Error out if we can't find a valid temporary directory, which should
+ * be very unlikely. */
+ msg(M_USAGE, "Could not find a suitable temporary directory."
+ " (GetTempPath() failed). Consider using --tmp-dir");
+ }
+#else /* ifdef _WIN32 */
/* Non-windows platforms use $TMPDIR, and if not set, default to '/tmp' */
o->tmp_dir = getenv("TMPDIR");
if (!o->tmp_dir)
@@ -147,4 +147,26 @@
}
return true;
}
+
+const char *
+win_get_tempdir(void)
+{
+ static char tmpdir[MAX_PATH];
+ WCHAR wtmpdir[MAX_PATH];
+
+ if (!GetTempPathW(_countof(wtmpdir), wtmpdir))
+ {
+ return NULL;
+ }
+
+ if (WideCharToMultiByte(CP_UTF8, 0, wtmpdir, -1, NULL, 0, NULL, NULL) > sizeof(tmpdir))
+ {
+ msg(M_WARN, "Could not get temporary directory. Path is too long."
+ " Consider using --tmp-dir");
+ return NULL;
+ }
+
+ WideCharToMultiByte(CP_UTF8, 0, wtmpdir, -1, tmpdir, sizeof(tmpdir), NULL, NULL);
+ return tmpdir;
+}
#endif /* _WIN32 */
@@ -40,5 +40,8 @@
/* return true if filename is safe to be used on Windows */
bool win_safe_filename(const char *fn);
+/* Find temporary directory */
+const char *win_get_tempdir(void);
+
#endif /* OPENVPN_WIN32_UTIL_H */
#endif /* ifdef _WIN32 */
@@ -1137,34 +1137,6 @@
set_win_sys_path(buf, es);
}
-
-const char *
-win_get_tempdir(void)
-{
- static char tmpdir[MAX_PATH];
- WCHAR wtmpdir[MAX_PATH];
-
- if (!GetTempPathW(_countof(wtmpdir), wtmpdir))
- {
- /* Warn if we can't find a valid temporary directory, which should
- * be unlikely.
- */
- msg(M_WARN, "Could not find a suitable temporary directory."
- " (GetTempPath() failed). Consider using --tmp-dir");
- return NULL;
- }
-
- if (WideCharToMultiByte(CP_UTF8, 0, wtmpdir, -1, NULL, 0, NULL, NULL) > sizeof(tmpdir))
- {
- msg(M_WARN, "Could not get temporary directory. Path is too long."
- " Consider using --tmp-dir");
- return NULL;
- }
-
- WideCharToMultiByte(CP_UTF8, 0, wtmpdir, -1, tmpdir, sizeof(tmpdir), NULL, NULL);
- return tmpdir;
-}
-
static bool
win_block_dns_service(bool add, int index, const HANDLE pipe)
{
@@ -286,9 +286,6 @@
/* call self in a subprocess */
void fork_to_self(const char *cmdline);
-/* Find temporary directory */
-const char *win_get_tempdir(void);
-
bool win_wfp_block_dns(const NET_IFINDEX index, const HANDLE msg_channel);
bool win_wfp_uninit(const NET_IFINDEX index, const HANDLE msg_channel);