Merge pull request #72 from j-h-choi/tac_env
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / prefer_dotnet_aot_plugin.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "ni_common.h"
18 #include "log.h"
19 #include "utils.h"
20
21 #include <cstring>
22 #include <vector>
23 #include <sstream>
24 #include <glib.h>
25 #include <pkgmgr_installer_info.h>
26
27 #ifdef  LOG_TAG
28 #undef  LOG_TAG
29 #endif
30 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
31
32 typedef struct Metadata {
33         const char *key;
34         const char *value;
35 } Metadata;
36
37 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
38 {
39         GList *tag = NULL;
40         bool mdValue = false;
41         Metadata *mdInfo = NULL;
42         tag = g_list_first(list);
43         while (tag) {
44                 mdInfo = (Metadata*)tag->data;
45                 if (strcmp(mdInfo->key, AOT_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) {
46                         _DBG("Prefer dotnet application AOT set TRUE");
47                         mdValue = true;
48                 }
49                 tag = g_list_next(tag);
50         }
51
52         if (mdValue) {
53                 NiCommonOption option = {std::string(), std::string(), std::string()};
54                 if (initNICommon(&option) < 0) {
55                         _ERR("Fail to initialize NI Common");
56                         return -1;
57                 }
58
59                 if (createNiUnderPkgRoot(pkgId, false) != 0) {
60                         _ERR("Failed to get root path from [%s]", pkgId);
61                         return -1;
62                 } else {
63                         _INFO("Complete make application to native image");
64                 }
65
66                 std::string pkgRoot;
67                 if (getRootPath(pkgId, pkgRoot) < 0) {
68                         fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
69                         return -1;
70                 }
71
72                 std::string binDir = concatPath(pkgRoot, "bin");
73                 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
74                 if (bf::exists(tacDir)) {
75                         uid_t uid = 0;
76                         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
77                                 _ERR("Failed to get UID");
78                                 return -1;
79                         }
80                         for (auto& symlinkAssembly : bf::recursive_directory_iterator(bf::path(tacDir))) {
81                                 std::string symPath = symlinkAssembly.path().string();
82                                 if (!isNativeImage(symPath)) {
83                                         std::string originPath = bf::read_symlink(symPath).string();
84                                         std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
85                                         if (!bf::exists(originNiPath)) {
86                                                 if(createNiDll(originPath, false) != NI_ERROR_NONE) {
87                                                         _ERR("Failed to create NI file [%s]\n", originPath.c_str());
88                                                         return -1;
89                                                 }
90                                         }
91                                         std::string setNiPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
92                                         if (!bf::exists(setNiPath)) {
93                                                 bf::create_symlink(originNiPath, setNiPath);
94                                                 fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
95                                                 if (lchown(setNiPath.c_str(), uid, 0)) {
96                                                         _ERR("Failed to change owner of: %s", setNiPath.c_str());
97                                                         return -1;
98                                                 }
99                                         }
100                                 }
101                         }
102                 }
103         }
104         return 0;
105 }
106
107 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
108 {
109         return PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgId, appId, list);
110 }
111
112 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
113 {
114         return 0;
115 }
116
117 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
118 {
119         return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
120 }
121
122 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
123 {
124         return 0;
125 }
126
127 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
128 {
129         return 0;
130 }