check if "log_dir" exists or create it

pull/1/head
Heiko Hund 2012-07-26 09:27:59 +02:00
parent 56654663f5
commit 22ff456888
6 changed files with 50 additions and 0 deletions

7
main.c
View File

@ -41,6 +41,7 @@
#include "openvpn-gui-res.h"
#include "localization.h"
#include "manage.h"
#include "misc.h"
#ifndef DISABLE_CHANGE_PASSWORD
#include <openssl/evp.h>
@ -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);

38
misc.c
View File

@ -24,6 +24,8 @@
#endif
#include <windows.h>
#include <tchar.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
@ -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
*/

2
misc.h
View File

@ -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);

View File

@ -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

View File

@ -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."

View File

@ -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."