set environment for corefx special folder
authorWoongsuk Cho <ws77.cho@samsung.com>
Wed, 8 Aug 2018 05:54:45 +0000 (14:54 +0900)
committer조웅석/Tizen Platform Lab(SR)/Staff Engineer/삼성전자 <ws77.cho@samsung.com>
Thu, 6 Sep 2018 01:22:00 +0000 (10:22 +0900)
Change-Id: I80e0dafb32f1b716142c50371a1bd5779ca01559

NativeLauncher/CMakeLists.txt
NativeLauncher/inc/coreclr_host.h
NativeLauncher/launcher/dotnet/dotnet_launcher.cc
NativeLauncher/launcher/dotnet/dotnet_launcher.h
packaging/dotnet-launcher.spec

index 42a59d1..93cd551 100644 (file)
@@ -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}")
index 7dbd137..3b96d6c 100644 (file)
@@ -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__ */
index 18a5c3e..d33c0d7 100644 (file)
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
+#include <linux/limits.h>
+
+#include <storage.h>
+#include <app_common.h>
 
 #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<char *>(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<char *>(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<char *>(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);
index 8be7617..a00b32e 100644 (file)
@@ -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;
index 572ffb8..d7d100f 100644 (file)
@@ -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