2011-09-27 06:58:49 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-09-27 06:58:49 +04:00
|
|
|
* File Utils
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-31 02:47:55 +04:00
|
|
|
#include <string.h>
|
2011-09-27 06:58:49 +04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2012-01-31 02:47:55 +04:00
|
|
|
#include <freerdp/types.h>
|
|
|
|
#include <freerdp/settings.h>
|
|
|
|
#include <freerdp/utils/memory.h>
|
|
|
|
#include <freerdp/utils/string.h>
|
|
|
|
|
2012-02-09 06:56:50 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#else
|
2012-02-09 06:16:37 +04:00
|
|
|
#include <direct.h>
|
|
|
|
#define getcwd _getcwd
|
|
|
|
#endif
|
|
|
|
|
2012-05-05 03:36:35 +04:00
|
|
|
#include <winpr/windows.h>
|
2011-09-27 06:58:49 +04:00
|
|
|
#include <freerdp/utils/file.h>
|
|
|
|
|
2012-01-31 02:47:55 +04:00
|
|
|
#ifndef _WIN32
|
|
|
|
#define PATH_SEPARATOR_STR "/"
|
|
|
|
#define PATH_SEPARATOR_CHR '/'
|
|
|
|
#define HOME_ENV_VARIABLE "HOME"
|
|
|
|
#else
|
|
|
|
#define PATH_SEPARATOR_STR "\\"
|
|
|
|
#define PATH_SEPARATOR_CHR '\\'
|
2012-02-29 02:10:06 +04:00
|
|
|
#define HOME_ENV_VARIABLE "HOME"
|
2011-09-27 06:58:49 +04:00
|
|
|
#endif
|
|
|
|
|
2012-02-10 02:48:30 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define SHARED_LIB_SUFFIX ".dll"
|
|
|
|
#elif __APPLE__
|
|
|
|
#define SHARED_LIB_SUFFIX ".dylib"
|
|
|
|
#else
|
|
|
|
#define SHARED_LIB_SUFFIX ".so"
|
|
|
|
#endif
|
|
|
|
|
2012-01-31 02:47:55 +04:00
|
|
|
#define FREERDP_CONFIG_DIR ".freerdp"
|
|
|
|
|
|
|
|
#define PARENT_PATH ".." PATH_SEPARATOR_STR
|
|
|
|
|
2011-09-27 06:58:49 +04:00
|
|
|
void freerdp_mkdir(char* path)
|
|
|
|
{
|
|
|
|
#ifndef _WIN32
|
|
|
|
mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
|
|
|
|
#else
|
|
|
|
CreateDirectoryA(path, 0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:47:55 +04:00
|
|
|
boolean freerdp_check_file_exists(char* file)
|
|
|
|
{
|
|
|
|
struct stat stat_info;
|
|
|
|
|
|
|
|
if (stat(file, &stat_info) != 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-01-31 02:47:55 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-01-31 02:47:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char* freerdp_get_home_path(rdpSettings* settings)
|
|
|
|
{
|
|
|
|
if (settings->home_path == NULL)
|
|
|
|
settings->home_path = getenv(HOME_ENV_VARIABLE);
|
2012-02-17 20:38:02 +04:00
|
|
|
if (settings->home_path == NULL)
|
2012-10-09 07:42:01 +04:00
|
|
|
settings->home_path = _strdup("/");
|
2012-01-31 02:47:55 +04:00
|
|
|
|
|
|
|
return settings->home_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* freerdp_get_config_path(rdpSettings* settings)
|
|
|
|
{
|
2012-02-27 19:55:49 +04:00
|
|
|
if (settings->config_path != NULL)
|
|
|
|
return settings->config_path;
|
2012-01-31 02:47:55 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
settings->config_path = (char*) malloc(strlen(settings->home_path) + sizeof(FREERDP_CONFIG_DIR) + 2);
|
2012-02-29 02:10:06 +04:00
|
|
|
sprintf(settings->config_path, "%s" PATH_SEPARATOR_STR "%s", settings->home_path, FREERDP_CONFIG_DIR);
|
2012-01-31 02:47:55 +04:00
|
|
|
|
2012-02-27 19:55:49 +04:00
|
|
|
if (!freerdp_check_file_exists(settings->config_path))
|
|
|
|
freerdp_mkdir(settings->config_path);
|
2012-01-31 02:47:55 +04:00
|
|
|
|
2012-02-27 19:55:49 +04:00
|
|
|
return settings->config_path;
|
2012-01-31 02:47:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
char* freerdp_get_current_path(rdpSettings* settings)
|
|
|
|
{
|
|
|
|
if (settings->current_path == NULL)
|
|
|
|
settings->current_path = getcwd(NULL, 0);
|
|
|
|
|
|
|
|
return settings->current_path;
|
|
|
|
}
|
|
|
|
|
2012-01-31 05:35:33 +04:00
|
|
|
char* freerdp_construct_path(char* base_path, char* relative_path)
|
|
|
|
{
|
|
|
|
char* path;
|
|
|
|
int length;
|
|
|
|
int base_path_length;
|
|
|
|
int relative_path_length;
|
|
|
|
|
|
|
|
base_path_length = strlen(base_path);
|
|
|
|
relative_path_length = strlen(relative_path);
|
|
|
|
length = base_path_length + relative_path_length + 1;
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
path = malloc(length + 1);
|
2012-01-31 05:35:33 +04:00
|
|
|
sprintf(path, "%s" PATH_SEPARATOR_STR "%s", base_path, relative_path);
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2012-02-10 02:48:30 +04:00
|
|
|
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)
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
path = malloc(file_path_length + shared_lib_suffix_length + 1);
|
2012-02-10 02:48:30 +04:00
|
|
|
sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 07:42:01 +04:00
|
|
|
path = _strdup(file_path);
|
2012-02-10 02:48:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
path = malloc(file_path_length + shared_lib_suffix_length + 1);
|
2012-03-27 23:54:10 +04:00
|
|
|
sprintf(path, "%s%s", file_path, SHARED_LIB_SUFFIX);
|
2012-02-10 02:48:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2012-01-31 02:47:55 +04:00
|
|
|
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)
|
2012-10-09 07:42:01 +04:00
|
|
|
return _strdup(base_path);
|
2012-01-31 02:47:55 +04:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
path = (char*) malloc(length + 1);
|
2012-01-31 02:47:55 +04:00
|
|
|
memcpy(path, base_path, length);
|
|
|
|
path[length] = '\0';
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2012-02-10 02:48:30 +04:00
|
|
|
boolean freerdp_path_contains_separator(char* path)
|
|
|
|
{
|
|
|
|
if (path == NULL)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-02-10 02:48:30 +04:00
|
|
|
|
|
|
|
if (strchr(path, PATH_SEPARATOR_CHR) == NULL)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-02-10 02:48:30 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-02-10 02:48:30 +04:00
|
|
|
}
|
|
|
|
|
2012-01-31 02:47:55 +04:00
|
|
|
/* detects if we are running from the source tree */
|
|
|
|
|
|
|
|
boolean freerdp_detect_development_mode(rdpSettings* settings)
|
|
|
|
{
|
|
|
|
int depth = 0;
|
|
|
|
char* current_path;
|
|
|
|
char* development_path = NULL;
|
2012-10-09 10:31:28 +04:00
|
|
|
boolean development_mode = FALSE;
|
2012-01-31 02:47:55 +04:00
|
|
|
|
|
|
|
if (freerdp_check_file_exists(".git"))
|
|
|
|
{
|
|
|
|
depth = 0;
|
2012-10-09 10:31:28 +04:00
|
|
|
development_mode = TRUE;
|
2012-01-31 02:47:55 +04:00
|
|
|
}
|
|
|
|
else if (freerdp_check_file_exists(PARENT_PATH ".git"))
|
|
|
|
{
|
|
|
|
depth = 1;
|
2012-10-09 10:31:28 +04:00
|
|
|
development_mode = TRUE;
|
2012-01-31 02:47:55 +04:00
|
|
|
}
|
|
|
|
else if (freerdp_check_file_exists(PARENT_PATH PARENT_PATH ".git"))
|
|
|
|
{
|
|
|
|
depth = 2;
|
2012-10-09 10:31:28 +04:00
|
|
|
development_mode = TRUE;
|
2012-01-31 02:47:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
current_path = freerdp_get_current_path(settings);
|
|
|
|
|
|
|
|
if (development_mode)
|
|
|
|
development_path = freerdp_get_parent_path(current_path, depth);
|
|
|
|
|
|
|
|
settings->development_mode = development_mode;
|
|
|
|
settings->development_path = development_path;
|
|
|
|
|
|
|
|
return settings->development_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_detect_paths(rdpSettings* settings)
|
|
|
|
{
|
|
|
|
freerdp_get_home_path(settings);
|
|
|
|
freerdp_get_config_path(settings);
|
|
|
|
freerdp_detect_development_mode(settings);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
printf("home path: %s\n", settings->home_path);
|
|
|
|
printf("config path: %s\n", settings->config_path);
|
|
|
|
printf("current path: %s\n", settings->current_path);
|
|
|
|
|
|
|
|
if (settings->development_mode)
|
|
|
|
printf("development path: %s\n", settings->development_path);
|
|
|
|
#endif
|
|
|
|
}
|