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