From e110c812930c0c19963a711dd782eca405ac379c Mon Sep 17 00:00:00 2001 From: Woongsuk Cho Date: Wed, 8 Aug 2018 14:54:45 +0900 Subject: [PATCH] set environment for corefx special folder Change-Id: I80e0dafb32f1b716142c50371a1bd5779ca01559 --- NativeLauncher/CMakeLists.txt | 2 +- NativeLauncher/inc/coreclr_host.h | 11 ++++ NativeLauncher/launcher/dotnet/dotnet_launcher.cc | 71 +++++++++++++++++++++++ NativeLauncher/launcher/dotnet/dotnet_launcher.h | 2 + packaging/dotnet-launcher.spec | 2 + 5 files changed, 87 insertions(+), 1 deletion(-) diff --git a/NativeLauncher/CMakeLists.txt b/NativeLauncher/CMakeLists.txt index 42a59d1..93cd551 100644 --- a/NativeLauncher/CMakeLists.txt +++ b/NativeLauncher/CMakeLists.txt @@ -4,7 +4,7 @@ PROJECT("dotnet-tools") MESSAGE("CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") INCLUDE(FindPkgConfig) -PKG_CHECK_MODULES(${PROJECT_NAME} REQUIRED aul pkgmgr-info pkgmgr-installer dlog ecore bundle dlog launchpad elementary glib-2.0 libsmack) +PKG_CHECK_MODULES(${PROJECT_NAME} REQUIRED aul pkgmgr-info pkgmgr-installer dlog ecore bundle dlog launchpad elementary glib-2.0 libsmack capi-appfw-app-common storage) FOREACH(flag ${${PROJECT_NAME}_CFLAGS}) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag}") diff --git a/NativeLauncher/inc/coreclr_host.h b/NativeLauncher/inc/coreclr_host.h index 7dbd137..3b96d6c 100644 --- a/NativeLauncher/inc/coreclr_host.h +++ b/NativeLauncher/inc/coreclr_host.h @@ -47,6 +47,17 @@ extern "C" const char* entryPointTypeName, const char* entryPointMethodName, void** delegate); + + typedef bool (*set_environment_variable_ptr)(const char16_t* name, + const char16_t* value); + + typedef int (*multi_byte_to_wide_char_ptr)( + unsigned int CodePage, + unsigned int dwFlags, + const char * lpMultiByteStr, + int cbMultiByte, + char16_t * lpWideCharStr, + int cchWideChar); } #endif /* __CORECLR_HOST_H__ */ diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc index 18a5c3e..d33c0d7 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.cc +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.cc @@ -28,6 +28,10 @@ #include #include #include +#include + +#include +#include #include "utils.h" #include "log.h" @@ -158,11 +162,58 @@ static void registerSigHandler() } } +static bool storage_cb(int id, storage_type_e type, storage_state_e state, const char *path, void *user_data) +{ + int* tmp = (int*)user_data; + if (type == STORAGE_TYPE_INTERNAL) + { + *tmp = id; + return false; + } + + return true; +} + +static void initEnvForSpecialFolder() +{ + int storageId; + int error; + char *path = NULL; + + error = storage_foreach_device_supported(storage_cb, &storageId); + if (error != STORAGE_ERROR_NONE) { + return; + } + + error = storage_get_directory(storageId, STORAGE_DIRECTORY_IMAGES, &path); + if (error == STORAGE_ERROR_NONE && path != NULL) { + setenv("XDG_PICTURES_DIR", const_cast(path), 1); + free(path); + path = NULL; + } + + error = storage_get_directory(storageId, STORAGE_DIRECTORY_MUSIC, &path); + if (error == STORAGE_ERROR_NONE && path != NULL) { + setenv("XDG_MUSIC_DIR", const_cast(path), 1); + free(path); + path = NULL; + } + + error = storage_get_directory(storageId, STORAGE_DIRECTORY_VIDEOS, &path); + if (error == STORAGE_ERROR_NONE && path != NULL) { + setenv("XDG_VIDEOS_DIR", const_cast(path), 1); + free(path); + path = NULL; + } +} + CoreRuntime::CoreRuntime(const char* mode) : initializeClr(nullptr), executeAssembly(nullptr), shutdown(nullptr), createDelegate(nullptr), + setEnvironmentVariable(nullptr), + multiByteToWideChar(nullptr), __coreclrLib(nullptr), __hostHandle(nullptr), __domainId(-1), @@ -220,6 +271,9 @@ int CoreRuntime::initialize(bool standalone) // read string from external file and set them to environment value. setEnvFromFile(); + // Set environment for System.Environment.SpecialFolder + initEnvForSpecialFolder(); + if (initializePathManager(std::string(), std::string(), std::string()) < 0) { _ERR("Failed to initialize PathManager"); return -1; @@ -264,6 +318,8 @@ int CoreRuntime::initialize(bool standalone) CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly"); CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown"); CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate"); + CORELIB_RETURN_IF_NOSYM(set_environment_variable_ptr, setEnvironmentVariable, "SetEnvironmentVariableW"); + CORELIB_RETURN_IF_NOSYM(multi_byte_to_wide_char_ptr, multiByteToWideChar, "MultiByteToWideChar"); #undef CORELIB_RETURN_IF_NOSYM @@ -395,6 +451,21 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i if (fd2 >= 0) close(fd2); + // set application data path to coreclr environment. + // application data path can be changed by owner. So, we have to set data path just before launching. + char* localDataPath = app_get_data_path(); + if (localDataPath != nullptr) { + char16_t envval[PATH_MAX] = {0}; + int copied = multiByteToWideChar(0 /* CP_ACP */, 0, localDataPath, -1, envval, PATH_MAX); + if (copied >= PATH_MAX) { + _ERR("Data Path is bigger than PATH_MAX"); + } + + if (!setEnvironmentVariable(u"XDG_DATA_HOME", envval)) { + _ERR("Failed to set XDG_DATA_HOME"); + } + } + pluginBeforeExecute(); _INFO("execute assembly : %s", path); diff --git a/NativeLauncher/launcher/dotnet/dotnet_launcher.h b/NativeLauncher/launcher/dotnet/dotnet_launcher.h index 8be7617..a00b32e 100644 --- a/NativeLauncher/launcher/dotnet/dotnet_launcher.h +++ b/NativeLauncher/launcher/dotnet/dotnet_launcher.h @@ -40,6 +40,8 @@ class CoreRuntime coreclr_execute_assembly_ptr executeAssembly; coreclr_shutdown_ptr shutdown; coreclr_create_delegate_ptr createDelegate; + set_environment_variable_ptr setEnvironmentVariable; + multi_byte_to_wide_char_ptr multiByteToWideChar; std::string __nativeLibDirectory; void* __coreclrLib; void* __hostHandle; diff --git a/packaging/dotnet-launcher.spec b/packaging/dotnet-launcher.spec index 572ffb8..d7d100f 100644 --- a/packaging/dotnet-launcher.spec +++ b/packaging/dotnet-launcher.spec @@ -18,6 +18,8 @@ BuildRequires: pkgconfig(pkgmgr-installer) BuildRequires: pkgconfig(elementary) BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(libsmack) +BuildRequires: pkgconfig(capi-appfw-app-common) +BuildRequires: pkgconfig(storage) BuildRequires: aul-devel BuildRequires: dotnet-build-tools -- 2.7.4