Merge pull request #2675 from akallabeth/path_make_path

Added PathMakePath function.
This commit is contained in:
Vic Lee 2015-06-18 02:43:47 +00:00
commit 2e11eac79a
13 changed files with 155 additions and 17 deletions

View File

@ -487,9 +487,6 @@ rdpSettings* freerdp_settings_new(DWORD flags)
settings->ConfigPath = GetCombinedPath(
base,
FREERDP_PRODUCT_STRING);
if (!PathFileExistsA(base))
if (!CreateDirectoryA(base, NULL))
settings->ConfigPath = NULL;
}
free (base);
} else {

View File

@ -57,7 +57,7 @@ BOOL certificate_store_init(rdpCertificateStore* certificate_store)
if (!PathFileExistsA(settings->ConfigPath))
{
if (!CreateDirectoryA(settings->ConfigPath, 0))
if (!PathMakePathA(settings->ConfigPath, 0))
{
WLog_ERR(TAG, "error creating directory '%s'", settings->ConfigPath);
goto fail;
@ -70,7 +70,7 @@ BOOL certificate_store_init(rdpCertificateStore* certificate_store)
if (!PathFileExistsA(certificate_store->path))
{
if (!CreateDirectoryA(certificate_store->path, 0))
if (!PathMakePathA(certificate_store->path, 0))
{
WLog_ERR(TAG, "error creating directory [%s]", certificate_store->path);
goto fail;
@ -83,7 +83,7 @@ BOOL certificate_store_init(rdpCertificateStore* certificate_store)
if (!PathFileExistsA(server_path))
{
if (!CreateDirectoryA(server_path, 0))
if (!PathMakePathA(server_path, 0))
{
WLog_ERR(TAG, "error creating directory [%s]", server_path);
goto fail;

View File

@ -445,7 +445,7 @@ int shadow_server_init_config_path(rdpShadowServer* server)
if (userLibraryPath)
{
if (!PathFileExistsA(userLibraryPath) &&
!CreateDirectoryA(userLibraryPath, 0))
!PathMakePathA(userLibraryPath, 0))
{
WLog_ERR(TAG, "Failed to create directory '%s'", userLibraryPath);
free(userLibraryPath);
@ -457,7 +457,7 @@ int shadow_server_init_config_path(rdpShadowServer* server)
if (userApplicationSupportPath)
{
if (!PathFileExistsA(userApplicationSupportPath) &&
!CreateDirectoryA(userApplicationSupportPath, 0))
!PathMakePathA(userApplicationSupportPath, 0))
{
WLog_ERR(TAG, "Failed to create directory '%s'", userApplicationSupportPath);
free(userLibraryPath);
@ -482,7 +482,7 @@ int shadow_server_init_config_path(rdpShadowServer* server)
if (configHome)
{
if (!PathFileExistsA(configHome) &&
!CreateDirectoryA(configHome, 0))
!PathMakePathA(configHome, 0))
{
WLog_ERR(TAG, "Failed to create directory '%s'", configHome);
free(configHome);
@ -516,7 +516,7 @@ int shadow_server_init_certificate(rdpShadowServer* server)
int makecert_argc = (sizeof(makecert_argv) / sizeof(char*));
if (!PathFileExistsA(server->ConfigPath) &&
!CreateDirectoryA(server->ConfigPath, 0))
!PathMakePathA(server->ConfigPath, 0))
{
WLog_ERR(TAG, "Failed to create directory '%s'", server->ConfigPath);
return -1;
@ -526,7 +526,7 @@ int shadow_server_init_certificate(rdpShadowServer* server)
return -1;
if (!PathFileExistsA(filepath) &&
!CreateDirectoryA(filepath, 0))
!PathMakePathA(filepath, 0))
{
WLog_ERR(TAG, "Failed to create directory '%s'", filepath);
free(filepath);

View File

@ -291,6 +291,9 @@ WINPR_API BOOL FindClose(HANDLE hFindFile);
WINPR_API BOOL CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes);
WINPR_API BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes);
WINPR_API BOOL RemoveDirectoryA(LPCSTR lpPathName);
WINPR_API BOOL RemoveDirectoryW(LPCWSTR lpPathName);
#ifdef __cplusplus
}
#endif
@ -302,6 +305,7 @@ WINPR_API BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecu
#define FindFirstFileEx FindFirstFileExW
#define FindNextFile FindNextFileW
#define CreateDirectory CreateDirectoryW
#define RemoveDirectory RemoveDirectoryW
#else
#define CreateFile CreateFileA
#define DeleteFile DeleteFileA
@ -309,6 +313,7 @@ WINPR_API BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecu
#define FindFirstFileEx FindFirstFileExA
#define FindNextFile FindNextFileA
#define CreateDirectory CreateDirectoryA
#define RemoveDirectory RemoveDirectoryA
#endif

View File

@ -285,6 +285,8 @@ WINPR_API char* GetEnvironmentPath(char* name);
WINPR_API char* GetEnvironmentSubPath(char* name, const char* path);
WINPR_API char* GetCombinedPath(const char* basePath, const char* subPath);
WINPR_API BOOL PathMakePathA(LPCSTR path, LPSECURITY_ATTRIBUTES lpAttributes);
WINPR_API BOOL PathFileExistsA(LPCSTR pszPath);
WINPR_API BOOL PathFileExistsW(LPCWSTR pszPath);

View File

@ -788,7 +788,20 @@ BOOL CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttribu
BOOL CreateDirectoryW(LPCWSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
return TRUE;
return FALSE;
}
BOOL RemoveDirectoryA(LPCSTR lpPathName)
{
if (!rmdir(lpPathName))
return TRUE;
return FALSE;
}
BOOL RemoveDirectoryW(LPCWSTR lpPathName)
{
return FALSE;
}
#endif

View File

@ -28,6 +28,7 @@
#include <winpr/crt.h>
#include <winpr/heap.h>
#include <winpr/file.h>
#include <winpr/tchar.h>
#include <winpr/environment.h>
@ -420,6 +421,50 @@ char* GetCombinedPath(const char* basePath, const char* subPath)
return path;
}
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;
if (!copy_org)
return FALSE;
length = strlen(copy_org);
/* Find first path element that exists. */
while (copy)
{
if (!PathFileExistsA(copy))
{
cur = strrchr(copy, delim);
if (cur)
*cur = '\0';
}
else
break;
}
/* Create directories. */
while(copy)
{
if (!PathFileExistsA(copy))
{
if (!CreateDirectoryA(copy, NULL))
break;
}
if (strlen(copy) < length)
copy[strlen(copy)] = delim;
else
break;
}
free (copy_org);
return PathFileExistsA(path);
}
BOOL PathFileExistsA(LPCSTR pszPath)
{
struct stat stat_info;

View File

@ -27,7 +27,8 @@ set(${MODULE_PREFIX}_TESTS
TestPathCchStripToRoot.c
TestPathCchStripPrefix.c
TestPathCchRemoveFileSpec.c
TestPathShell.c)
TestPathShell.c
TestPathMakePath.c)
create_test_sourcelist(${MODULE_PREFIX}_SRCS
${${MODULE_PREFIX}_DRIVER}

View File

@ -0,0 +1,75 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <winpr/crt.h>
#include <winpr/file.h>
#include <winpr/path.h>
int TestPathMakePath(int argc, char* argv[])
{
int x;
size_t baseLen;
BOOL success;
char tmp[64];
char* path;
char* cur;
char delim = PathGetSeparatorA(0);
char* base = GetKnownPath(KNOWN_PATH_TEMP);
if (!base)
{
fprintf(stderr, "Failed to get temporary directory!\n");
return -1;
}
baseLen = strlen(base);
srand(time(NULL));
for (x=0; x<5; x++)
{
sprintf(tmp, "%08X", rand());
path = GetCombinedPath(base, tmp);
free(base);
if (!path)
{
fprintf(stderr, "GetCombinedPath failed!\n");
return -1;
}
base = path;
}
printf("Creating path %s\n", path);
success = PathMakePathA(path, NULL);
if (!success)
{
fprintf(stderr, "MakePath failed!\n");
free (path);
return -1;
}
success = PathFileExistsA(path);
if (!success)
{
fprintf(stderr, "MakePath lied about success!\n");
free (path);
return -1;
}
while (strlen(path) > baseLen)
{
if (!RemoveDirectoryA(path))
{
fprintf(stderr, "RemoveDirectoryA %s failed!\n", path);
free (path);
return -1;
}
cur = strrchr(path, delim);
if (cur)
*cur = '\0';
}
free (path);
printf("%s success!\n", __FUNCTION__);
return 0;
}

View File

@ -1276,7 +1276,7 @@ void Inspect_InitLog()
return;
if (!PathFileExistsA(filepath))
if (!CreateDirectoryA(filepath, NULL))
if (!PathMakePathA(filepath, NULL))
return;
if (!(g_Log = WLog_Get("WinSCard")))

View File

@ -98,7 +98,7 @@ int WLog_BinaryAppender_Open(wLog* log, wLogBinaryAppender* appender)
if (!PathFileExistsA(appender->FilePath))
{
if (!CreateDirectoryA(appender->FilePath, 0))
if (!PathMakePathA(appender->FilePath, 0))
return -1;
UnixChangeFileMode(appender->FilePath, 0xFFFF);
}

View File

@ -95,7 +95,7 @@ int WLog_FileAppender_Open(wLog* log, wLogFileAppender* appender)
if (!PathFileExistsA(appender->FilePath))
{
if (!CreateDirectoryA(appender->FilePath, 0))
if (!PathMakePathA(appender->FilePath, 0))
return -1;
UnixChangeFileMode(appender->FilePath, 0xFFFF);
}

View File

@ -44,7 +44,7 @@ char* WLog_Message_GetOutputFileName(int id, const char* ext)
if (!PathFileExistsA(FilePath))
{
if (!CreateDirectoryA(FilePath, NULL))
if (!PathMakePathA(FilePath, NULL))
{
free(FileName);
free(FilePath);