From 22ff4568887de244dc97c8e91273ba1cadf9368a Mon Sep 17 00:00:00 2001 From: Heiko Hund Date: Thu, 26 Jul 2012 09:27:59 +0200 Subject: [PATCH] check if "log_dir" exists or create it --- main.c | 7 +++++++ misc.c | 38 ++++++++++++++++++++++++++++++++++++++ misc.h | 2 ++ openvpn-gui-res.h | 1 + res/openvpn-gui-res-de.rc | 1 + res/openvpn-gui-res-en.rc | 1 + 6 files changed, 50 insertions(+) diff --git a/main.c b/main.c index 07375b6..74632fb 100644 --- a/main.c +++ b/main.c @@ -41,6 +41,7 @@ #include "openvpn-gui-res.h" #include "localization.h" #include "manage.h" +#include "misc.h" #ifndef DISABLE_CHANGE_PASSWORD #include @@ -170,6 +171,12 @@ int WINAPI _tWinMain (HINSTANCE hThisInstance, exit(1); } + if (!EnsureDirExists(o.log_dir)) + { + ShowLocalizedMsg(IDS_ERR_CREATE_PATH, _T("log_dir"), o.log_dir); + exit(1); + } + BuildFileList(); if (!VerifyAutoConnections()) { exit(1); diff --git a/misc.c b/misc.c index 6d9140b..9ca2617 100644 --- a/misc.c +++ b/misc.c @@ -24,6 +24,8 @@ #endif #include +#include +#include #include #include @@ -106,6 +108,42 @@ ManagementCommandFromInput(connection_t *c, LPCSTR fmt, HWND hDlg, int id) } +/* + * Ensures the given directory exists, by checking for and + * creating missing parts of the path. + * If the path does not exist and cannot be created return FALSE. + */ +BOOL +EnsureDirExists(LPTSTR dir) +{ + DWORD attr = GetFileAttributes(dir); + if (attr == INVALID_FILE_ATTRIBUTES) + { + DWORD error = GetLastError(); + if (error == ERROR_PATH_NOT_FOUND) + { + LPTSTR pos = _tcsrchr(dir, '\\'); + if (pos == NULL) + return FALSE; + + *pos = '\0'; + BOOL ret = EnsureDirExists(dir); + *pos = '\\'; + if (ret == FALSE) + return FALSE; + } + else if (error != ERROR_FILE_NOT_FOUND) + return FALSE; + + /* No error if directory already exists */ + return (CreateDirectory(dir, NULL) == TRUE + || GetLastError() == ERROR_ALREADY_EXISTS); + } + + return (attr & FILE_ATTRIBUTE_DIRECTORY ? TRUE : FALSE); +} + + /* * Various string helper functions */ diff --git a/misc.h b/misc.h index f0ed32b..b67c1a7 100644 --- a/misc.h +++ b/misc.h @@ -24,6 +24,8 @@ BOOL ManagementCommandFromInput(connection_t *, LPCSTR, HWND, int); +BOOL EnsureDirExists(LPTSTR); + BOOL streq(LPCSTR, LPCSTR); BOOL wcsbegins(LPCWSTR, LPCWSTR); BOOL wcseq(LPCWSTR, LPCWSTR); diff --git a/openvpn-gui-res.h b/openvpn-gui-res.h index 2c828f3..86057bb 100644 --- a/openvpn-gui-res.h +++ b/openvpn-gui-res.h @@ -166,6 +166,7 @@ #define IDS_NFO_SERVICE_STOPPED 1306 #define IDS_NFO_ACTIVE_CONN_EXIT 1307 #define IDS_NFO_SERVICE_ACTIVE_EXIT 1308 +#define IDS_ERR_CREATE_PATH 1309 /* Program Options Related */ #define IDS_NFO_USAGE 1401 diff --git a/res/openvpn-gui-res-de.rc b/res/openvpn-gui-res-de.rc index f78b9ab..c744137 100644 --- a/res/openvpn-gui-res-de.rc +++ b/res/openvpn-gui-res-de.rc @@ -230,6 +230,7 @@ BEGIN /* main - Resources */ IDS_ERR_OPEN_DEBUG_FILE "Fehler beim öffnen des Debugfiles (%s)." + IDS_ERR_CREATE_PATH "Fehler beim Erstellen des %s Pfads:\n%s" IDS_ERR_LOAD_RICHED20 "Kann RICHED20.DLL nicht laden." IDS_ERR_SHELL_DLL_VERSION "Die shell32.dll Versionsnummer ist zu niedrieg (0x%lx). Es müssen mindestens Version 5.0 installiert sein." IDS_ERR_GUI_ALREADY_RUNNING "Die OpenVPN GUI wurde bereits gestartet." diff --git a/res/openvpn-gui-res-en.rc b/res/openvpn-gui-res-en.rc index 4747759..490efcb 100644 --- a/res/openvpn-gui-res-en.rc +++ b/res/openvpn-gui-res-en.rc @@ -228,6 +228,7 @@ BEGIN "they reside in diffrent folders." /* main - Resources */ IDS_ERR_OPEN_DEBUG_FILE "Error opening debug file (%s) for output." + IDS_ERR_CREATE_PATH "Could not create %s path:\n%s" IDS_ERR_LOAD_RICHED20 "Could not load RICHED20.DLL." IDS_ERR_SHELL_DLL_VERSION "Your shell32.dll version is to low (0x%lx). You need at least version 5.0." IDS_ERR_GUI_ALREADY_RUNNING "OpenVPN GUI is already running."