Fixed Svace issue
[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         int skipOpt = false;
40         if (!pkgmgr_installer_info_get_skip_optimization(&skipOpt)) {
41                 if (skipOpt) {
42                         _DBG("Skip dotnet AOT");
43                         return 0;
44                 }
45         }
46
47         GList *tag = NULL;
48         bool mdValue = false;
49         Metadata *mdInfo = NULL;
50         tag = g_list_first(list);
51         while (tag) {
52                 mdInfo = (Metadata*)tag->data;
53                 if (strcmp(mdInfo->key, AOT_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) {
54                         _DBG("Prefer dotnet application AOT set TRUE");
55                         mdValue = true;
56                 }
57                 tag = g_list_next(tag);
58         }
59
60         if (mdValue) {
61                 NiCommonOption option = {std::string(), std::string(), std::string()};
62                 if (initNICommon(&option) < 0) {
63                         _ERR("Fail to initialize NI Common");
64                         return -1;
65                 }
66
67                 if (createNiUnderPkgRoot(pkgId, 0) != NI_ERROR_NONE) {
68                         _ERR("Failed to get root path from [%s]", pkgId);
69                         return -1;
70                 } else {
71                         _INFO("Complete make application to native image");
72                 }
73
74                 std::string pkgRoot;
75                 if (getRootPath(pkgId, pkgRoot) < 0) {
76                         _ERR("Failed to get root path from [%s]", pkgId);
77                         return 0;
78                 }
79
80                 std::string binDir = concatPath(pkgRoot, "bin");
81                 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
82                 if (bf::exists(tacDir)) {
83                         uid_t g_uid = 0;
84                         gid_t g_gid = 0;
85                         if (pkgmgr_installer_info_get_target_uid(&g_uid) < 0) {
86                                 _ERR("Failed to get UID");
87                                 return 0;
88                         }
89                         try {
90                                 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
91                                         std::string symPath = symlinkAssembly.path().string();
92                                         if (!isNativeImage(symPath)) {
93                                                 std::string originPath = bf::read_symlink(symPath).string();
94                                                 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
95                                                 if (!bf::exists(originNiPath)) {
96                                                         if(createNiDllUnderPkgRoot(pkgId, originPath, 0) != NI_ERROR_NONE) {
97                                                                 _ERR("Failed to create NI file [%s]", originPath.c_str());
98                                                                 return -1;
99                                                         }
100                                                 }
101                                                 std::string setNiPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
102                                                 if (!bf::exists(setNiPath)) {
103                                                         bf::create_symlink(originNiPath, setNiPath);
104                                                         _INFO("%s symbolic link file generated successfully.", setNiPath.c_str());
105                                                         if (lchown(setNiPath.c_str(), g_uid, g_gid)) {
106                                                                 _ERR("Failed to change owner of: %s", setNiPath.c_str());
107                                                                 return -1;
108                                                         }
109                                                 }
110                                         }
111                                 }
112                         } catch (const bf::filesystem_error& error) {
113                                 _ERR("File system error while interating files: %s", error.what());
114                                 return -1;
115                         }
116                 }
117         }
118         return 0;
119 }
120
121 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
122 {
123         return PKGMGR_MDPARSER_PLUGIN_INSTALL(pkgId, appId, list);
124 }
125
126 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
127 {
128         return 0;
129 }
130
131 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
132 {
133         return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
134 }
135
136 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
137 {
138         return 0;
139 }
140
141 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
142 {
143         return 0;
144 }