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