Add switches for crossgen (#91)
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / tac_common.cc
1 /*
2  * Copyright (c) 2019 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 <fstream>
18 #include <pkgmgr-info.h>
19 #include <pkgmgr_installer_info.h>
20
21 #include "log.h"
22 #include "utils.h"
23 #include "ni_common.h"
24 #include "tac_common.h"
25 #include "db_manager.h"
26 #include "path_manager.h"
27
28 #ifdef  LOG_TAG
29 #undef  LOG_TAG
30 #endif
31 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
32
33 #define __XSTR(x) #x
34 #define __STR(x) __XSTR(x)
35 static const char* __TAC_DIR = __STR(TAC_DIR);
36 #undef __STR
37 #undef __XSTR
38
39 static sqlite3 *tac_db = NULL;
40 std::vector<std::string> restoreNuget;
41
42 void cleanupDirectory()
43 {
44         std::vector<std::string> removeNuget;
45         try {
46                 for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
47                         bool isExist = false;
48                         std::string nugetPath = nuget.path().string();
49                         for (auto& restore : restoreNuget) {
50                                 if (!bf::is_directory(nugetPath)) {
51                                         isExist = true;
52                                 }
53                                 if (strstr(nugetPath.c_str(), restore.c_str()) != NULL) {
54                                         isExist = true;
55                                         break;
56                                 }
57                         }
58                         if (!isExist) {
59                                 removeNuget.push_back(nugetPath);
60                         }
61                 }
62
63                 for (auto& rm : removeNuget) {
64                         if (!removeAll(rm)) {
65                                 _ERR("Failed to remove of %s", rm.c_str());
66                         }
67                 }
68                 removeNuget.clear();
69         } catch (const bf::filesystem_error& error) {
70                 _ERR("Failed to recursive directory: %s", error.what());
71                 return;
72         }
73 }
74
75 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
76 static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
77 {
78         char *pkgId = NULL;
79         char *root = NULL;
80         char *exec = NULL;
81         std::string rootPath;
82         std::string execName;
83
84         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
85         if (ret != PMINFO_R_OK) {
86                 fprintf(stderr, "Failed to get pkgid\n");
87                 return -1;
88         }
89
90         ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
91         if (ret != PMINFO_R_OK) {
92                 fprintf(stderr, "Failed to get root path\n");
93                 return -1;
94         }
95         rootPath = root;
96
97         ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
98         if (ret != PMINFO_R_OK) {
99                 fprintf(stderr, "Failed to get exec name\n");
100                 return -1;
101         }
102         execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
103
104         std::vector<std::string> parserData;
105         for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
106                 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
107                 parserData.push_back(nugetPackage);
108         }
109         std::sort(parserData.begin(), parserData.end());
110         parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
111
112         for (auto& nuget : parserData) {
113                 if (tac_db) {
114                         std::string name = nuget.substr(0, nuget.find('/'));
115                         std::string version = nuget.substr(nuget.rfind('/') + 1);
116                         std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
117                                                 "VALUES ('" + std::string(pkgId) + "', '" + nuget + "', '" + name + "', '" + version + "');";
118                         dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql);
119                         restoreNuget.push_back(concatPath(__TAC_DIR, name));
120                 }
121         }
122         parserData.clear();
123         return 0;
124 }
125
126 tac_error_e restoreTACDB()
127 {
128         if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
129                 fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
130                 return TAC_ERROR_UNKNOWN;
131         }
132
133         std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
134         if (!removeFile(dbRestoreJournal)) {
135                 fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
136                 return TAC_ERROR_UNKNOWN;
137         }
138
139         if (initializePathManager(std::string(), std::string(), std::string())) {
140                 fprintf(stderr, "Fail to initialize PathManger");
141                 return TAC_ERROR_UNKNOWN;
142         }
143
144         tac_db = dbCreate(TAC_APP_LIST_RESTORE_DB);
145         if (tac_db) {
146                 if (!dbOpen(tac_db, TAC_APP_LIST_RESTORE_DB)) {
147                         return TAC_ERROR_UNKNOWN;
148                 }
149         } else {
150                 return TAC_ERROR_UNKNOWN;
151         }
152
153         pkgmgrinfo_appinfo_metadata_filter_h handle;
154         int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
155         if (ret != PMINFO_R_OK) {
156                 return TAC_ERROR_UNKNOWN;
157         }
158
159         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
160         if (ret != PMINFO_R_OK) {
161                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
162                 return TAC_ERROR_UNKNOWN;
163         }
164
165         ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, restoreDBCb, NULL);
166         if (ret != PMINFO_R_OK) {
167                 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
168                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
169                 return TAC_ERROR_UNKNOWN;
170         }
171         fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
172
173         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
174
175         if (tac_db) {
176                 dbClose(tac_db);
177                 tac_db = NULL;
178         }
179
180         if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
181                 fprintf(stderr, "Failed to copy of %s", TAC_APP_LIST_DB);
182                 return TAC_ERROR_UNKNOWN;
183         }
184         if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
185                 fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
186                 return TAC_ERROR_UNKNOWN;
187         }
188
189         std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
190         if (!copyFile(dbRestoreJournal, dbJournal)) {
191                 fprintf(stderr, "Failed to copy of %s", dbJournal.c_str());
192                 return TAC_ERROR_UNKNOWN;
193         }
194         if (!removeFile(dbRestoreJournal)) {
195                 fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
196                 return TAC_ERROR_UNKNOWN;
197         }
198
199         cleanupDirectory();
200         return TAC_ERROR_NONE;
201 }
202
203 tac_error_e resetTACPackage(const std::string& pkgId)
204 {
205         std::string pkgRoot;
206         if (getRootPath(pkgId, pkgRoot) < 0) {
207                 return TAC_ERROR_INVALID_PACKAGE;
208         }
209
210         std::vector<std::string> tacNativeImage;
211         std::string binDir = concatPath(pkgRoot, "bin");
212         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
213         if (bf::exists(tacDir)) {
214                 try {
215                         for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
216                                 std::string symPath = symlinkAssembly.path().string();
217                                 if (bf::is_symlink(symPath)) {
218                                         if (isNativeImage(symPath)) {
219                                                 tacNativeImage.push_back(symPath);
220                                         }
221                                 }
222                         }
223                         for (auto& path : tacNativeImage) {
224                                 if (!removeFile(path)) {
225                                         fprintf(stderr, "Failed to remove of %s", path.c_str());
226                                         return TAC_ERROR_UNKNOWN;
227                                 }
228                         }
229                         tacNativeImage.clear();
230                 } catch (const bf::filesystem_error& error) {
231                         _ERR("Failed to recursive directory: %s", error.what());
232                         return TAC_ERROR_UNKNOWN;
233                 }
234         }
235         return TAC_ERROR_NONE;
236 }
237
238 tac_error_e createTACPackage(const std::string& pkgId, DWORD flags)
239 {
240         std::string pkgRoot;
241         if (getRootPath(pkgId, pkgRoot) < 0) {
242                 return TAC_ERROR_INVALID_PACKAGE;
243         }
244
245         std::string binDir = concatPath(pkgRoot, "bin");
246         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
247         std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
248         if (bf::exists(tacDir)) {
249                 try {
250                         for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
251                                 std::string symPath = symlinkAssembly.path().string();
252                                 if (bf::is_symlink(symPath)) {
253                                         if (!isNativeImage(symPath)) {
254                                                 std::string originPath = bf::read_symlink(symPath).string();
255                                                 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
256                                                 if (!bf::exists(originNiPath)) {
257                                                         if(createNiDll(originPath, flags) != NI_ERROR_NONE) {
258                                                                 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
259                                                                 return TAC_ERROR_UNKNOWN;
260                                                         }
261                                                 }
262                                                 std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
263                                                 if (!bf::exists(symNIPath)) {
264                                                         bf::create_symlink(originNiPath, symNIPath);
265                                                         fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
266                                                         updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
267
268                                                         std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
269                                                         if (!removeFile(concatPath(binNIDir, NIFileName))) {
270                                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
271                                                                 return TAC_ERROR_UNKNOWN;
272                                                         }
273                                                 }
274                                         }
275                                 }
276                         }
277                 } catch (const bf::filesystem_error& error) {
278                         _ERR("Failed to recursive directory: %s", error.what());
279                         return TAC_ERROR_UNKNOWN;
280                 }
281         }
282         return TAC_ERROR_NONE;
283 }
284
285 tac_error_e regenerateTAC(DWORD flags)
286 {
287         const std::string tacDir[] = {__TAC_DIR};
288         removeNiUnderDirs(tacDir, 1);
289         createNiUnderTAC(tacDir, 1, flags);
290         return TAC_ERROR_NONE;
291 }
292
293 tac_error_e disableTACPackage(const std::string& pkgId)
294 {
295         std::string pkgRoot;
296         if (getRootPath(pkgId, pkgRoot) < 0) {
297                 return TAC_ERROR_INVALID_PACKAGE;
298         }
299
300         std::string binDir = concatPath(pkgRoot, "bin");
301         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
302         std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
303         if (bf::exists(tacDir)) {
304                 try {
305                         for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
306                                 std::string symPath = symlinkAssembly.path().string();
307                                 std::string fileName = symlinkAssembly.path().filename().string();
308                                 if (bf::is_symlink(symPath)) {
309                                         std::string originPath = bf::read_symlink(symPath).string();
310                                         if (!isNativeImage(symPath)) {
311                                                 std::string dllPath = concatPath(binDir, fileName);
312                                                 if (!copyFile(originPath, dllPath)) {
313                                                         fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
314                                                         return TAC_ERROR_UNKNOWN;
315                                                 }
316                                                 updateAssemblyInfo(binDir.c_str(), concatPath(binDir, fileName).c_str());
317                                         } else {
318                                                 std::string niPath = concatPath(binNIDir, fileName);
319                                                 if (!copyFile(originPath, niPath)) {
320                                                         fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
321                                                         return TAC_ERROR_UNKNOWN;
322                                                 }
323                                                 updateAssemblyInfo(binDir.c_str(), niPath.c_str());
324                                         }
325                                 }
326                         }
327                         if (!removeAll(tacDir)) {
328                                 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
329                                 return TAC_ERROR_UNKNOWN;
330                         }
331                 } catch (const bf::filesystem_error& error) {
332                         _ERR("Failed to recursive directory: %s", error.what());
333                         return TAC_ERROR_UNKNOWN;
334                 }
335         }
336         return TAC_ERROR_NONE;
337 }
338
339 tac_error_e enableTACPackage(const std::string& pkgId)
340 {
341         std::string rootPath;
342         if (getRootPath(pkgId, rootPath) < 0) {
343                 return TAC_ERROR_INVALID_PACKAGE;
344         }
345
346         std::string execName;
347         if (getExecName(pkgId, execName) < 0) {
348                 return TAC_ERROR_INVALID_PACKAGE;
349         }
350
351         std::string metaValue;
352         if (getMetadataValue(pkgId, TAC_METADATA_KEY, metaValue) < 0) {
353                 return TAC_ERROR_INVALID_PACKAGE;
354         }
355
356         if (initializePathManager(std::string(), std::string(), std::string())) {
357                 fprintf(stderr, "Fail to initialize PathManger");
358                 return TAC_ERROR_UNKNOWN;
359         }
360
361         if (!strcmp(metaValue.c_str(), "true")) {
362                 std::string binDir = concatPath(rootPath, "bin");
363                 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
364                 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
365                 if (!bf::exists(tacDir)) {
366                         if (!createDir(tacDir)) {
367                                 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
368                                 return TAC_ERROR_UNKNOWN;
369                         }
370                         updateAssemblyInfo(binDir.c_str(), tacDir.c_str());
371
372                         std::vector<std::string> enableNuget;
373                         for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
374                                 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
375                                 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
376                                 std::string nugetPath = concatPath(__TAC_DIR, nugetPackage);
377                                 if (bf::exists(nugetPath)) {
378                                         std::string originPath = concatPath(nugetPath, assemblyName);
379                                         if (bf::exists(originPath)) {
380                                                 enableNuget.push_back(originPath);
381                                         }
382                                 }
383                         }
384
385                         for (auto& originPath : enableNuget) {
386                                 if (bf::exists(originPath)) {
387                                         std::string fileName = originPath.substr(originPath.rfind('/') + 1);
388                                         std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
389                                         if (bf::exists(binNIDir)) {
390                                                 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
391                                                 if (bf::exists(originNIPath)) {
392                                                         bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
393                                                         fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
394                                                         updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
395
396                                                         if (!removeFile(concatPath(binNIDir, NIFileName))) {
397                                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
398                                                                 return TAC_ERROR_UNKNOWN;
399                                                         }
400                                                 }
401                                         }
402                                         bf::create_symlink(originPath, concatPath(tacDir, fileName));
403                                         fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
404                                         updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
405
406                                         if (!removeFile(concatPath(binDir, fileName))) {
407                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
408                                                 return TAC_ERROR_UNKNOWN;
409                                         }
410                                 }
411                         }
412                         if (enableNuget.empty()) {
413                                 if (!removeAll(tacDir)) {
414                                         _ERR("Failed to remove of %s", tacDir.c_str());
415                                 }
416                         }
417                         enableNuget.clear();
418                 }
419         } else {
420                 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
421         }
422         return TAC_ERROR_NONE;
423 }