Fixed svace (376818, 386676, 386690)
[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)
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, false) != 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()
286 {
287         const std::string tacDir[] = {__TAC_DIR};
288         removeNiUnderDirs(tacDir, 1);
289
290         auto convert = [](const std::string& path, std::string name) {
291                 if (strstr(path.c_str(), TAC_APP_LIST_DB) != NULL ||
292                         strstr(path.c_str(), TAC_APP_LIST_RESTORE_DB) != NULL ||
293                         strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
294                         return;
295                 if(createNiDll(path, false) != NI_ERROR_NONE) {
296                         fprintf(stderr, "Failed to create NI file [%s]\n", path.c_str());
297                         return;
298                 }
299         };
300         scanFilesInDir(tacDir[0], convert, -1);
301         return TAC_ERROR_NONE;
302 }
303
304 tac_error_e disableTACPackage(const std::string& pkgId)
305 {
306         std::string pkgRoot;
307         if (getRootPath(pkgId, pkgRoot) < 0) {
308                 return TAC_ERROR_INVALID_PACKAGE;
309         }
310
311         std::string binDir = concatPath(pkgRoot, "bin");
312         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
313         std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
314         if (bf::exists(tacDir)) {
315                 try {
316                         for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
317                                 std::string symPath = symlinkAssembly.path().string();
318                                 std::string fileName = symlinkAssembly.path().filename().string();
319                                 if (bf::is_symlink(symPath)) {
320                                         std::string originPath = bf::read_symlink(symPath).string();
321                                         if (!isNativeImage(symPath)) {
322                                                 std::string dllPath = concatPath(binDir, fileName);
323                                                 if (!copyFile(originPath, dllPath)) {
324                                                         fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
325                                                         return TAC_ERROR_UNKNOWN;
326                                                 }
327                                                 updateAssemblyInfo(binDir.c_str(), concatPath(binDir, fileName).c_str());
328                                         } else {
329                                                 std::string niPath = concatPath(binNIDir, fileName);
330                                                 if (!copyFile(originPath, niPath)) {
331                                                         fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
332                                                         return TAC_ERROR_UNKNOWN;
333                                                 }
334                                                 updateAssemblyInfo(binDir.c_str(), niPath.c_str());
335                                         }
336                                 }
337                         }
338                         if (!removeAll(tacDir)) {
339                                 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
340                                 return TAC_ERROR_UNKNOWN;
341                         }
342                 } catch (const bf::filesystem_error& error) {
343                         _ERR("Failed to recursive directory: %s", error.what());
344                         return TAC_ERROR_UNKNOWN;
345                 }
346         }
347         return TAC_ERROR_NONE;
348 }
349
350 tac_error_e enableTACPackage(const std::string& pkgId)
351 {
352         std::string rootPath;
353         if (getRootPath(pkgId, rootPath) < 0) {
354                 return TAC_ERROR_INVALID_PACKAGE;
355         }
356
357         std::string execName;
358         if (getExecName(pkgId, execName) < 0) {
359                 return TAC_ERROR_INVALID_PACKAGE;
360         }
361
362         std::string metaValue;
363         if (getMetadataValue(pkgId, TAC_METADATA_KEY, metaValue) < 0) {
364                 return TAC_ERROR_INVALID_PACKAGE;
365         }
366
367         if (initializePathManager(std::string(), std::string(), std::string())) {
368                 fprintf(stderr, "Fail to initialize PathManger");
369                 return TAC_ERROR_UNKNOWN;
370         }
371
372         if (!strcmp(metaValue.c_str(), "true")) {
373                 std::string binDir = concatPath(rootPath, "bin");
374                 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
375                 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
376                 if (!bf::exists(tacDir)) {
377                         if (!createDir(tacDir)) {
378                                 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
379                                 return TAC_ERROR_UNKNOWN;
380                         }
381                         updateAssemblyInfo(binDir.c_str(), tacDir.c_str());
382
383                         std::vector<std::string> enableNuget;
384                         for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
385                                 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
386                                 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
387                                 std::string nugetPath = concatPath(__TAC_DIR, nugetPackage);
388                                 if (bf::exists(nugetPath)) {
389                                         std::string originPath = concatPath(nugetPath, assemblyName);
390                                         if (bf::exists(originPath)) {
391                                                 enableNuget.push_back(originPath);
392                                         }
393                                 }
394                         }
395
396                         for (auto& originPath : enableNuget) {
397                                 if (bf::exists(originPath)) {
398                                         std::string fileName = originPath.substr(originPath.rfind('/') + 1);
399                                         std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
400                                         if (bf::exists(binNIDir)) {
401                                                 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
402                                                 if (bf::exists(originNIPath)) {
403                                                         bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
404                                                         fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
405                                                         updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
406
407                                                         if (!removeFile(concatPath(binNIDir, NIFileName))) {
408                                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
409                                                                 return TAC_ERROR_UNKNOWN;
410                                                         }
411                                                 }
412                                         }
413                                         bf::create_symlink(originPath, concatPath(tacDir, fileName));
414                                         fprintf(stderr, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
415                                         updateAssemblyInfo(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
416
417                                         if (!removeFile(concatPath(binDir, fileName))) {
418                                                 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
419                                                 return TAC_ERROR_UNKNOWN;
420                                         }
421                                 }
422                         }
423                         if (enableNuget.empty()) {
424                                 if (!removeAll(tacDir)) {
425                                         _ERR("Failed to remove of %s", tacDir.c_str());
426                                 }
427                         }
428                         enableNuget.clear();
429                 }
430         } else {
431                 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
432         }
433         return TAC_ERROR_NONE;
434 }