Skip to content

Commit

Permalink
libwinpr-path: add path detection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
awakecoding committed Mar 22, 2013
1 parent 93a752b commit edf6e72
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 140 deletions.
5 changes: 0 additions & 5 deletions include/freerdp/utils/file.h
Expand Up @@ -29,14 +29,9 @@ extern "C" {
#endif

FREERDP_API void freerdp_mkdir(char* path);
FREERDP_API BOOL freerdp_check_file_exists(char* file);
FREERDP_API char* freerdp_get_home_path(rdpSettings* settings);
FREERDP_API char* freerdp_get_config_path(rdpSettings* settings);
FREERDP_API char* freerdp_get_current_path(rdpSettings* settings);
FREERDP_API char* freerdp_construct_path(char* base_path, char* relative_path);
FREERDP_API char* freerdp_append_shared_library_suffix(char* file_path);
FREERDP_API char* freerdp_get_parent_path(char* base_path, int depth);
FREERDP_API BOOL freerdp_path_contains_separator(char* path);
FREERDP_API void freerdp_detect_paths(rdpSettings* settings);

#ifdef __cplusplus
Expand Down
4 changes: 2 additions & 2 deletions libfreerdp/crypto/certificate.c
Expand Up @@ -48,15 +48,15 @@ void certificate_store_init(rdpCertificateStore* certificate_store)
config_path = freerdp_get_config_path(settings);
certificate_store->path = freerdp_construct_path(config_path, (char*) certificate_store_dir);

if (freerdp_check_file_exists(certificate_store->path) == FALSE)
if (PathFileExistsA(certificate_store->path) == FALSE)
{
freerdp_mkdir(certificate_store->path);
printf("creating directory %s\n", certificate_store->path);
}

certificate_store->file = freerdp_construct_path(config_path, (char*) certificate_known_hosts_file);

if (freerdp_check_file_exists(certificate_store->file) == FALSE)
if (PathFileExistsA(certificate_store->file) == FALSE)
{
certificate_store->fp = fopen((char*) certificate_store->file, "w+");

Expand Down
2 changes: 1 addition & 1 deletion libfreerdp/utils/CMakeLists.txt
Expand Up @@ -59,7 +59,7 @@ endif()
set_complex_link_libraries(VARIABLE ${MODULE_PREFIX}_LIBS
MONOLITHIC ${MONOLITHIC_BUILD}
MODULE winpr
MODULES winpr-crt winpr-synch winpr-thread winpr-utils)
MODULES winpr-crt winpr-synch winpr-thread winpr-utils winpr-path)

if(MONOLITHIC_BUILD)
set(FREERDP_LIBS ${FREERDP_LIBS} ${${MODULE_PREFIX}_LIBS} PARENT_SCOPE)
Expand Down
130 changes: 3 additions & 127 deletions libfreerdp/utils/file.c
Expand Up @@ -27,6 +27,7 @@
#include <sys/stat.h>

#include <winpr/crt.h>
#include <winpr/path.h>

#include <freerdp/types.h>
#include <freerdp/settings.h>
Expand All @@ -44,25 +45,13 @@
#ifndef _WIN32
#define PATH_SEPARATOR_STR "/"
#define PATH_SEPARATOR_CHR '/'
#define HOME_ENV_VARIABLE "HOME"
#else
#define PATH_SEPARATOR_STR "\\"
#define PATH_SEPARATOR_CHR '\\'
#define HOME_ENV_VARIABLE "HOME"
#endif

#ifdef _WIN32
#define SHARED_LIB_SUFFIX ".dll"
#elif defined(__APPLE__)
#define SHARED_LIB_SUFFIX ".dylib"
#else
#define SHARED_LIB_SUFFIX ".so"
#endif

#define FREERDP_CONFIG_DIR ".freerdp"

#define PARENT_PATH ".." PATH_SEPARATOR_STR

void freerdp_mkdir(char* path)
{
#ifndef _WIN32
Expand All @@ -72,31 +61,9 @@ void freerdp_mkdir(char* path)
#endif
}

BOOL freerdp_check_file_exists(char* file)
{
struct stat stat_info;

if (stat(file, &stat_info) != 0)
return FALSE;

return TRUE;
}

char* freerdp_get_home_path(rdpSettings* settings)
{
char* home_env = NULL;

if (settings->HomePath == NULL)
{
home_env = getenv(HOME_ENV_VARIABLE);

if (home_env)
settings->HomePath = _strdup(home_env);
}

if (settings->HomePath == NULL)
settings->HomePath = _strdup("/");

settings->HomePath = GetKnownPath(KNOWN_PATH_HOME);
return settings->HomePath;
}

Expand All @@ -108,20 +75,12 @@ char* freerdp_get_config_path(rdpSettings* settings)
settings->ConfigPath = (char*) malloc(strlen(settings->HomePath) + sizeof(FREERDP_CONFIG_DIR) + 2);
sprintf(settings->ConfigPath, "%s" PATH_SEPARATOR_STR "%s", settings->HomePath, FREERDP_CONFIG_DIR);

if (!freerdp_check_file_exists(settings->ConfigPath))
if (!PathFileExistsA(settings->ConfigPath))
freerdp_mkdir(settings->ConfigPath);

return settings->ConfigPath;
}

char* freerdp_get_current_path(rdpSettings* settings)
{
if (settings->CurrentPath == NULL)
settings->CurrentPath = getcwd(NULL, 0);

return settings->CurrentPath;
}

char* freerdp_construct_path(char* base_path, char* relative_path)
{
char* path;
Expand All @@ -139,89 +98,6 @@ char* freerdp_construct_path(char* base_path, char* relative_path)
return path;
}

char* freerdp_append_shared_library_suffix(char* file_path)
{
char* p;
char* path = NULL;
int file_path_length;
int shared_lib_suffix_length;

if (file_path == NULL)
return NULL;

file_path_length = strlen(file_path);
shared_lib_suffix_length = strlen(SHARED_LIB_SUFFIX);

if (file_path_length >= shared_lib_suffix_length)
{
p = &file_path[file_path_length - shared_lib_suffix_length];

if (strcmp(p, SHARED_LIB_SUFFIX) != 0)
{
path = malloc(file_path_length + shared_lib_suffix_length + 1);
sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
}
else
{
path = _strdup(file_path);
}
}
else
{
path = malloc(file_path_length + shared_lib_suffix_length + 1);
sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
}

return path;
}

char* freerdp_get_parent_path(char* base_path, int depth)
{
int i;
char* p;
char* path;
int length;
int base_length;

if (base_path == NULL)
return NULL;

if (depth <= 0)
return _strdup(base_path);

base_length = strlen(base_path);

p = &base_path[base_length];

for (i = base_length - 1; ((i >= 0) && (depth > 0)); i--)
{
if (base_path[i] == PATH_SEPARATOR_CHR)
{
p = &base_path[i];
depth--;
}
}

length = (p - base_path);

path = (char*) malloc(length + 1);
memcpy(path, base_path, length);
path[length] = '\0';

return path;
}

BOOL freerdp_path_contains_separator(char* path)
{
if (path == NULL)
return FALSE;

if (strchr(path, PATH_SEPARATOR_CHR) == NULL)
return FALSE;

return TRUE;
}

void freerdp_detect_paths(rdpSettings* settings)
{
freerdp_get_home_path(settings);
Expand Down
2 changes: 1 addition & 1 deletion server/X11/xf_peer.c
Expand Up @@ -546,7 +546,7 @@ void* xf_peer_main_loop(void* arg)

server_file_path = freerdp_construct_path(settings->ConfigPath, "server");

if (!freerdp_check_file_exists(server_file_path))
if (!PathFileExistsA(server_file_path))
freerdp_mkdir(server_file_path);

settings->CertificateFile = freerdp_construct_path(server_file_path, "server.crt");
Expand Down
30 changes: 30 additions & 0 deletions winpr/include/winpr/path.h
Expand Up @@ -250,4 +250,34 @@ WINPR_API PCWSTR PathGetSharedLibraryExtensionW(unsigned long dwFlags);

#endif

/**
* Shell Path Functions
*/

#ifdef _WIN32
#include <shlwapi.h>
#endif

#define KNOWN_PATH_HOME 1
#define KNOWN_PATH_TEMP 2
#define KNOWN_PATH_DATA 3
#define KNOWN_PATH_CONFIG 4
#define KNOWN_PATH_CACHE 5
#define KNOWN_PATH_RUNTIME 6

WINPR_API char* GetKnownPath(int id);

#ifndef _WIN32

WINPR_API BOOL PathFileExistsA(LPCSTR pszPath);
WINPR_API BOOL PathFileExistsW(LPCWSTR pszPath);

#ifdef UNICODE
#define PathFileExists PathFileExistsW
#else
#define PathFileExists PathFileExistsA
#endif

#endif

#endif /* WINPR_PATH_H */
17 changes: 16 additions & 1 deletion winpr/libwinpr/environment/environment.c
Expand Up @@ -132,7 +132,22 @@ BOOL NeedCurrentDirectoryForExePathW(LPCWSTR ExeName)

DWORD GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer, DWORD nSize)
{
return 0;
int length;
char* env = NULL;

env = getenv(lpName);

if (!env)
return 0;

length = strlen(env);

if ((length + 1 > nSize) || (!lpBuffer))
return length + 1;

CopyMemory(lpBuffer, env, length + 1);

return length;
}

DWORD GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer, DWORD nSize)
Expand Down
5 changes: 3 additions & 2 deletions winpr/libwinpr/path/CMakeLists.txt
Expand Up @@ -19,7 +19,8 @@ set(MODULE_NAME "winpr-path")
set(MODULE_PREFIX "WINPR_PATH")

set(${MODULE_PREFIX}_SRCS
path.c)
path.c
shell.c)

add_complex_library(MODULE ${MODULE_NAME} TYPE "OBJECT"
MONOLITHIC ${MONOLITHIC_BUILD}
Expand All @@ -30,7 +31,7 @@ set_target_properties(${MODULE_NAME} PROPERTIES VERSION ${WINPR_VERSION_FULL} SO
if(MONOLITHIC_BUILD)

else()
target_link_libraries(${MODULE_NAME} winpr-crt winpr-heap)
target_link_libraries(${MODULE_NAME} winpr-crt winpr-heap winpr-environment)
install(TARGETS ${MODULE_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

Expand Down

0 comments on commit edf6e72

Please sign in to comment.