winpr: fix PathMakePathA and TestWLog

PathMakePathA:
- This function had an endless loop if no native delimiter was in the string
- Use SHCreateDirectoryExA on Windows
- Replaced old code with a new implementation

TestWLog:
- Windows has no "/tmp" by default
- Use GetKnownPath(KNOWN_PATH_TEMP) for the WLog "outputfilepath"
This commit is contained in:
Norbert Federa 2016-06-13 19:19:28 +02:00
parent ee4d118beb
commit 62d73dcb75
2 changed files with 38 additions and 32 deletions

View File

@ -36,6 +36,8 @@
#if defined(WIN32) #if defined(WIN32)
#include <Shlobj.h> #include <Shlobj.h>
#else
#include <errno.h>
#endif #endif
static char* GetPath_XDG_CONFIG_HOME(void); static char* GetPath_XDG_CONFIG_HOME(void);
@ -438,46 +440,35 @@ char* GetCombinedPath(const char* basePath, const char* subPath)
BOOL PathMakePathA(LPCSTR path, LPSECURITY_ATTRIBUTES lpAttributes) BOOL PathMakePathA(LPCSTR path, LPSECURITY_ATTRIBUTES lpAttributes)
{ {
size_t length; #ifdef _WIN32
const char delim = PathGetSeparatorA(0); return (SHCreateDirectoryExA(NULL, path, lpAttributes) == ERROR_SUCCESS);
char* cur; #else
char* copy_org = _strdup(path); const char delim = PathGetSeparatorA(PATH_STYLE_NATIVE);
char* copy = copy_org; char* dup;
char* p;
if (!copy_org) /* we only operate on a non-null, absolute path */
if (!path || *path != delim)
return FALSE; return FALSE;
length = strlen(copy_org); if (!(dup = _strdup(path)))
return FALSE;
/* Find first path element that exists. */ for (p = dup; p;)
while (copy)
{ {
if (!PathFileExistsA(copy)) if ((p = strchr(p + 1, delim)))
{ *p = '\0';
cur = strrchr(copy, delim);
if (cur)
*cur = '\0';
}
else
break;
}
/* Create directories. */ if (mkdir(dup, 0777) != 0)
while(copy) if (errno != EEXIST)
{
if (!PathFileExistsA(copy))
{
if (!CreateDirectoryA(copy, NULL))
break; break;
} if (p)
if (strlen(copy) < length) *p = delim;
copy[strlen(copy)] = delim;
else
break;
} }
free (copy_org);
return PathFileExistsA(path); free(dup);
return (p == NULL);
#endif
} }
#if !defined(_WIN32) || defined(_UWP) #if !defined(_WIN32) || defined(_UWP)

View File

@ -2,6 +2,7 @@
#include <winpr/crt.h> #include <winpr/crt.h>
#include <winpr/tchar.h> #include <winpr/tchar.h>
#include <winpr/path.h> #include <winpr/path.h>
#include <winpr/file.h>
#include <winpr/wlog.h> #include <winpr/wlog.h>
int TestWLog(int argc, char* argv[]) int TestWLog(int argc, char* argv[])
@ -11,6 +12,14 @@ int TestWLog(int argc, char* argv[])
wLog* logB; wLog* logB;
wLogLayout* layout; wLogLayout* layout;
wLogAppender* appender; wLogAppender* appender;
char* tmp_path;
char* wlog_file;
if (!(tmp_path = GetKnownPath(KNOWN_PATH_TEMP)))
{
fprintf(stderr, "Failed to get temporary directory!\n");
return -1;
}
WLog_Init(); WLog_Init();
@ -21,7 +30,7 @@ int TestWLog(int argc, char* argv[])
appender = WLog_GetLogAppender(root); appender = WLog_GetLogAppender(root);
if(!WLog_ConfigureAppender(appender, "outputfilename", "test_w.log")) if(!WLog_ConfigureAppender(appender, "outputfilename", "test_w.log"))
return 1; return 1;
if(!WLog_ConfigureAppender(appender, "outputfilepath", "/tmp/")) if(!WLog_ConfigureAppender(appender, "outputfilepath", tmp_path))
return 1; return 1;
layout = WLog_GetLogLayout(root); layout = WLog_GetLogLayout(root);
@ -49,5 +58,11 @@ int TestWLog(int argc, char* argv[])
WLog_Uninit(); WLog_Uninit();
if ((wlog_file = GetCombinedPath(tmp_path, "test_w.log")))
{
DeleteFileA(wlog_file);
free(wlog_file);
}
return 0; return 0;
} }