Merge pull request #3404 from nfedera/fix-pathmakepath

winpr: fix PathMakePathA and TestWLog
This commit is contained in:
akallabeth 2016-06-14 10:20:52 +02:00 committed by GitHub
commit f0bdd62e9c
2 changed files with 38 additions and 32 deletions

View File

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

View File

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