[core,settings] unify configuration paths

* Add new function freerdp_settings_get_config_path to get current
  user configuration directory
* Add CMake option WITH_FULL_CONFIG_PATH (default OFF): If defined, use
  <appdata>/Vendor/Product as config directory, otherwise use
  <appdata>/vendor (lowercase, only if vendor equal to product)
This commit is contained in:
akallabeth 2024-06-06 11:17:55 +02:00
parent ad83ba2088
commit 7aa72a7507
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5
7 changed files with 51 additions and 94 deletions

View File

@ -23,7 +23,7 @@ add_library(sdl-common-prefs STATIC
sdl_prefs.hpp
sdl_prefs.cpp
)
target_link_libraries(sdl-common-prefs winpr)
target_link_libraries(sdl-common-prefs winpr freerdp)
if(BUILD_TESTING)
add_subdirectory(test)

View File

@ -34,6 +34,7 @@ namespace fs = std::experimental::filesystem;
#include <winpr/config.h>
#include <freerdp/version.h>
#include <winpr/json.h>
#include <freerdp/settings.h>
SdlPref::WINPR_JSONPtr SdlPref::get()
{
@ -108,13 +109,11 @@ SdlPref::SdlPref(const std::string& file) : _name(file), _config(get())
std::string SdlPref::get_pref_dir()
{
using CStringPtr = std::unique_ptr<char, decltype(&free)>;
CStringPtr path(GetKnownPath(KNOWN_PATH_XDG_CONFIG_HOME), free);
CStringPtr path(freerdp_settings_get_config_path(), free);
if (!path)
return {};
fs::path config{ path.get() };
config /= FREERDP_VENDOR;
config /= FREERDP_PRODUCT;
return config.string();
}

View File

@ -237,3 +237,5 @@ if (BUILD_FUZZERS)
>
)
endif()
option(WITH_FULL_CONFIG_PATH "Use <appdata>/Vendor/Product instead of <appdata>/product (lowercase, only if vendor equals product) as config directory" OFF)

View File

@ -713,6 +713,10 @@ typedef struct rdp_settings rdpSettings;
FREERDP_API const char* freerdp_supported_color_depths_string(UINT16 mask, char* buffer,
size_t size);
/** \brief return the configuration directory for the library
*/
FREERDP_API char* freerdp_settings_get_config_path(void);
#ifdef __cplusplus
}
#endif

View File

@ -398,6 +398,10 @@ AddTargetWithResourceFile(${MODULE_NAME} FALSE "${FREERDP_VERSION}" LIBFREERDP_S
add_definitions(${LIBFREERDP_DEFINITIONS})
if (WITH_FULL_CONFIG_PATH)
add_definitions(-DWITH_FULL_CONFIG_PATH)
endif()
target_include_directories(${MODULE_NAME} INTERFACE $<INSTALL_INTERFACE:include>)
target_link_libraries(${MODULE_NAME} PRIVATE ${LIBFREERDP_LIBS})
target_link_libraries(${MODULE_NAME} PUBLIC ${LIBFREERDP_PUB_LIBS})

View File

@ -350,9 +350,39 @@ BOOL freerdp_capability_buffer_allocate(rdpSettings* settings, UINT32 count)
settings->ReceivedCapabilityDataSizes);
}
#if !defined(WITH_FULL_CONFIG_PATH)
static char* freerdp_settings_get_legacy_config_path(void)
{
char product[sizeof(FREERDP_PRODUCT_STRING)] = { 0 };
for (size_t i = 0; i < sizeof(product); i++)
product[i] = tolower(FREERDP_PRODUCT_STRING[i]);
return GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, product);
}
#endif
char* freerdp_settings_get_config_path(void)
{
char* path = NULL;
/* For default FreeRDP continue using same config directory
* as in old releases.
* Custom builds use <Vendor>/<Product> as config folder. */
#if !defined(WITH_FULL_CONFIG_PATH)
if (_stricmp(FREERDP_VENDOR_STRING, FREERDP_PRODUCT_STRING) == 0)
return freerdp_settings_get_legacy_config_path();
#endif
char* base = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, FREERDP_VENDOR_STRING);
if (base)
path = GetCombinedPath(base, FREERDP_PRODUCT_STRING);
free(base);
return path;
}
rdpSettings* freerdp_settings_new(DWORD flags)
{
char* base = NULL;
char* issuers[] = { "FreeRDP", "FreeRDP-licenser" };
const BOOL server = (flags & FREERDP_SETTINGS_SERVER_MODE) != 0 ? TRUE : FALSE;
const BOOL remote = (flags & FREERDP_SETTINGS_REMOTE_MODE) != 0 ? TRUE : FALSE;
@ -728,7 +758,7 @@ rdpSettings* freerdp_settings_new(DWORD flags)
if (!freerdp_settings_get_bool(settings, FreeRDP_ServerMode))
{
BOOL rc = 0;
BOOL rc = FALSE;
char* path = NULL;
if (!freerdp_settings_set_bool(settings, FreeRDP_RedirectClipboard, TRUE))
goto out_fail;
@ -740,42 +770,10 @@ rdpSettings* freerdp_settings_new(DWORD flags)
if (!rc || !freerdp_settings_get_string(settings, FreeRDP_HomePath))
goto out_fail;
/* For default FreeRDP continue using same config directory
* as in old releases.
* Custom builds use <Vendor>/<Product> as config folder. */
if (_stricmp(FREERDP_VENDOR_STRING, FREERDP_PRODUCT_STRING))
{
BOOL res = TRUE;
base = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, FREERDP_VENDOR_STRING);
if (base)
{
char* combined = GetCombinedPath(base, FREERDP_PRODUCT_STRING);
res = freerdp_settings_set_string(settings, FreeRDP_ConfigPath, combined);
free(combined);
}
free(base);
if (!res)
goto out_fail;
}
else
{
BOOL res = 0;
char* cpath = NULL;
char product[sizeof(FREERDP_PRODUCT_STRING)] = { 0 };
for (size_t i = 0; i < sizeof(product); i++)
product[i] = tolower(FREERDP_PRODUCT_STRING[i]);
cpath = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, product);
res = freerdp_settings_set_string(settings, FreeRDP_ConfigPath, cpath);
free(cpath);
if (!res)
goto out_fail;
}
if (!freerdp_settings_get_string(settings, FreeRDP_ConfigPath))
char* config = freerdp_settings_get_config_path();
rc = freerdp_settings_set_string(settings, FreeRDP_ConfigPath, config);
free(config);
if (!rc)
goto out_fail;
}

View File

@ -734,58 +734,9 @@ int shadow_server_stop(rdpShadowServer* server)
static int shadow_server_init_config_path(rdpShadowServer* server)
{
#ifdef _WIN32
if (!server->ConfigPath)
{
server->ConfigPath = GetEnvironmentSubPath("LOCALAPPDATA", "freerdp");
}
#endif
#ifdef __APPLE__
if (!server->ConfigPath)
{
char* userLibraryPath;
char* userApplicationSupportPath;
userLibraryPath = GetKnownSubPath(KNOWN_PATH_HOME, "Library");
if (userLibraryPath)
{
if (!winpr_PathFileExists(userLibraryPath) && !winpr_PathMakePath(userLibraryPath, 0))
{
WLog_ERR(TAG, "Failed to create directory '%s'", userLibraryPath);
free(userLibraryPath);
return -1;
}
userApplicationSupportPath = GetCombinedPath(userLibraryPath, "Application Support");
if (userApplicationSupportPath)
{
if (!winpr_PathFileExists(userApplicationSupportPath) &&
!winpr_PathMakePath(userApplicationSupportPath, 0))
{
WLog_ERR(TAG, "Failed to create directory '%s'", userApplicationSupportPath);
free(userLibraryPath);
free(userApplicationSupportPath);
return -1;
}
server->ConfigPath = GetCombinedPath(userApplicationSupportPath, "freerdp");
}
free(userLibraryPath);
free(userApplicationSupportPath);
}
}
#endif
if (!server->ConfigPath)
{
char* configHome = NULL;
configHome = GetKnownPath(KNOWN_PATH_XDG_CONFIG_HOME);
char* configHome = freerdp_settings_get_config_path();
if (configHome)
{
@ -796,8 +747,7 @@ static int shadow_server_init_config_path(rdpShadowServer* server)
return -1;
}
server->ConfigPath = GetKnownSubPath(KNOWN_PATH_XDG_CONFIG_HOME, "freerdp");
free(configHome);
server->ConfigPath = configHome;
}
}