2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <json/json.h>
19 #include <pkgmgr-info.h>
20 #include <pkgmgr_installer_info.h>
24 #include "tac_common.h"
25 #include "db_manager.h"
30 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
33 #define __STR(x) __XSTR(x)
34 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
38 static sqlite3 *tac_db = NULL;
39 static sqlite3 *tlc_db = NULL;
40 std::vector<std::string> restore_nuget;
41 std::vector<std::string> restore_library;
43 static void cleanupDirectory()
45 std::vector<std::string> removeNuget;
47 for (auto& nuget : bf::recursive_directory_iterator(__DOTNET_DIR)) {
49 std::string nugetPath = nuget.path().string();
50 for (auto& restore : restore_nuget) {
51 if (!bf::is_directory(nugetPath) || nugetPath.find(TLC_LIBRARIES_DIR) != std::string::npos) {
55 if (!strcmp(nugetPath.c_str(), restore.c_str()) ||
56 !strcmp(nugetPath.c_str(), restore.substr(0, restore.rfind('/')).c_str())) {
62 removeNuget.push_back(nugetPath);
66 for (auto& rm : removeNuget) {
68 fprintf(stderr, "Failed to remove of %s\n", rm.c_str());
72 } catch (const bf::filesystem_error& error) {
73 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
78 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
79 static int tac_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
87 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
88 if (ret != PMINFO_R_OK) {
89 fprintf(stderr, "Failed to get pkgid\n");
93 enableTACPackage(std::string(pkgId));
95 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
96 if (ret != PMINFO_R_OK) {
97 fprintf(stderr, "Failed to get root path\n");
100 rootPath = std::string(root);
102 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
103 if (ret != PMINFO_R_OK) {
104 fprintf(stderr, "Failed to get exec name\n");
107 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
109 std::vector<std::string> parserData;
110 std::string binDir = concatPath(rootPath, "bin");
111 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
112 for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
113 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
114 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
115 if (exist(tacDir) && exist(concatPath(tacDir, assemblyName))) {
116 parserData.push_back(nugetPackage);
119 std::sort(parserData.begin(), parserData.end());
120 parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
122 for (auto& nuget : parserData) {
124 std::string name = nuget.substr(0, nuget.find('/'));
125 std::string version = nuget.substr(nuget.rfind('/') + 1);
126 char *sql = sqlite3_mprintf(
127 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
128 "VALUES (%Q, %Q, %Q, %Q);", pkgId, nuget.c_str(), name.c_str(), version.c_str());
129 insertDB(tac_db, sql);
130 restore_nuget.push_back(concatPath(__DOTNET_DIR, nuget));
138 tac_error_e tac_restoreDB()
140 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
141 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
142 return TAC_ERROR_UNKNOWN;
145 std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
146 if (!removeFile(dbRestoreJournal)) {
147 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
148 return TAC_ERROR_UNKNOWN;
151 tac_db = createDB(TAC_APP_LIST_RESTORE_DB, CREATE_TAC_DB_TABLE);
153 fprintf(stderr, "Sqlite create error\n");
154 return TAC_ERROR_UNKNOWN;
156 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
158 pkgmgrinfo_appinfo_metadata_filter_h handle;
159 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
160 if (ret != PMINFO_R_OK) {
161 return TAC_ERROR_UNKNOWN;
164 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
165 if (ret != PMINFO_R_OK) {
166 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
167 return TAC_ERROR_UNKNOWN;
170 ret = pkgmgrMDFilterForeach(handle, tac_restoreDBCb, NULL);
172 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
173 return TAC_ERROR_UNKNOWN;
176 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
183 if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
184 fprintf(stderr, "Failed to copy of %s\n", TAC_APP_LIST_DB);
185 return TAC_ERROR_UNKNOWN;
187 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
188 fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
189 return TAC_ERROR_UNKNOWN;
192 std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
193 if (!copyFile(dbRestoreJournal, dbJournal)) {
194 fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
195 return TAC_ERROR_UNKNOWN;
197 if (!removeFile(dbRestoreJournal)) {
198 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
199 return TAC_ERROR_UNKNOWN;
204 return TAC_ERROR_NONE;
207 tac_error_e disableTACPackage(const std::string& pkgId)
209 std::string rootPath = getRootPath(pkgId);
210 if (rootPath.empty()) {
211 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
212 return TAC_ERROR_INVALID_PACKAGE;
215 std::string binDir = concatPath(rootPath, "bin");
216 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
217 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
220 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
221 std::string symPath = symlinkAssembly.path().string();
222 std::string fileName = symlinkAssembly.path().filename().string();
223 if (bf::is_symlink(symPath)) {
224 std::string originPath = bf::read_symlink(symPath).string();
225 if (!isNativeImage(symPath)) {
226 std::string dllPath = concatPath(binDir, fileName);
227 if (!copyFile(originPath, dllPath)) {
228 fprintf(stderr, "Failed to copy of %s\n", dllPath.c_str());
229 return TAC_ERROR_UNKNOWN;
231 copySmackAndOwnership(binDir.c_str(), concatPath(binDir, fileName).c_str());
233 std::string niPath = concatPath(binNIDir, fileName);
234 if (!copyFile(originPath, niPath)) {
235 fprintf(stderr, "Failed to copy of %s\n", niPath.c_str());
236 return TAC_ERROR_UNKNOWN;
238 copySmackAndOwnership(binDir.c_str(), niPath.c_str());
242 if (!removeAll(tacDir)) {
243 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
244 return TAC_ERROR_UNKNOWN;
246 } catch (const bf::filesystem_error& error) {
247 fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
248 return TAC_ERROR_UNKNOWN;
251 return TAC_ERROR_NONE;
254 tac_error_e enableTACPackage(const std::string& pkgId)
256 std::string rootPath = getRootPath(pkgId);
257 if (rootPath.empty()) {
258 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
259 return TAC_ERROR_INVALID_PACKAGE;
262 std::string execName = getExecName(pkgId);
263 if (execName.empty()) {
264 fprintf(stderr, "Failed to get exec name from [%s]\n", pkgId.c_str());
265 return TAC_ERROR_INVALID_PACKAGE;
268 std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
269 if (metaValue.empty()) {
270 fprintf(stderr, "Failed to get metadata from [%s]\n", pkgId.c_str());
271 return TAC_ERROR_INVALID_PACKAGE;
274 if (!strcmp(metaValue.c_str(), "true")) {
275 std::string binDir = concatPath(rootPath, "bin");
276 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
277 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
278 if (!exist(tacDir)) {
279 if (!createDir(tacDir)) {
280 fprintf(stderr, "Cannot create directory: %s\n", tacDir.c_str());
281 return TAC_ERROR_UNKNOWN;
283 copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
285 std::vector<std::string> enableNuget;
286 for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
287 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
288 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
289 std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
290 if (exist(nugetPath)) {
291 std::string originPath = concatPath(nugetPath, assemblyName);
292 if (exist(originPath)) {
293 enableNuget.push_back(originPath);
298 for (auto& originPath : enableNuget) {
299 if (exist(originPath)) {
300 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
301 std::string NIFileName = fileName.substr(0, fileName.rfind(".dll")) + ".ni.dll";
302 if (exist(binNIDir)) {
303 std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
304 if (exist(originNIPath)) {
305 bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName));
306 fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, NIFileName).c_str());
307 copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
309 if (!removeFile(concatPath(binNIDir, NIFileName))) {
310 fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
311 return TAC_ERROR_UNKNOWN;
315 bf::create_symlink(originPath, concatPath(tacDir, fileName));
316 fprintf(stdout, "%s symbolic link file generated successfully.\n", concatPath(tacDir, fileName).c_str());
317 copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
319 if (!removeFile(concatPath(binDir, fileName))) {
320 fprintf(stderr, "Failed to remove of %s\n", concatPath(binDir, fileName).c_str());
321 return TAC_ERROR_UNKNOWN;
325 if (enableNuget.empty()) {
326 if (!removeAll(tacDir)) {
327 fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
333 fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str());
335 return TAC_ERROR_NONE;
338 //Parser the .deps.json file to get nuget information.
339 std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::string& execName)
341 std::vector<std::string> parserData;
342 std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
343 std::string depsJsonPath = concatPath(rootPath, depsJsonName);
345 if (exist(depsJsonPath)) {
346 std::ifstream ifs(depsJsonPath);
347 Json::CharReaderBuilder reader;
351 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
352 _ERR("Failed to parse of deps.json");
356 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
357 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
358 std::vector<std::string> appDependencies;
359 for (auto& nuget : nugetPackages.getMemberNames()) {
360 if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
361 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
362 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
363 if (assemblies != Json::nullValue) {
364 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
365 for (auto& dependency : dependencies.getMemberNames()) {
366 appDependencies.push_back(dependency);
371 for (auto& nuget : nugetPackages.getMemberNames()) {
372 //Skip the nuget package related to Tizen
373 if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
374 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
375 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
376 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
377 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
378 if (assemblies != Json::nullValue) {
379 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
380 bool hasDependency = false;
381 for (auto& dependency : dependencies.getMemberNames()) {
382 //Skip the nugget package that is dependent on another nuget package
383 if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
384 strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
385 hasDependency = true;
386 for (auto& ad : appDependencies) {
387 if (!strcmp(ad.c_str(), dependency.c_str())) {
388 hasDependency = true;
391 hasDependency = false;
394 if (hasDependency) break;
397 if (!hasDependency) {
398 // handle assembly even though that is included in the TPA.
399 for (auto& assembly : assemblies.getMemberNames()) {
400 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
401 parserData.push_back(nuget + ":" + assemblyName);
402 _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
408 appDependencies.clear();
412 } catch (const Json::LogicError& error) {
413 _ERR("Failed to parse Json: %s", error.what());
418 std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
420 std::vector<std::string> LibrariesInfo;
421 std::string binDir = concatPath(rootPath, "bin");
423 return LibrariesInfo;
425 auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
426 if (filepath.rfind(".so") != std::string::npos) {
427 std::string buffer = SHA256(filepath);
428 LibrariesInfo.push_back(filepath + ":" + buffer);
429 _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
432 scanFilesInDirectory(binDir, convert, -1);
434 return LibrariesInfo;
437 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
438 static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
442 std::string rootPath;
444 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
445 if (ret != PMINFO_R_OK) {
446 fprintf(stderr, "Failed to get pkgid\n");
450 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
451 if (ret != PMINFO_R_OK) {
452 fprintf(stderr, "Failed to get root path\n");
455 rootPath = std::string(root);
457 for (auto& librarySha : getLibrariesInfo(rootPath)) {
458 std::string library = librarySha.substr(0, librarySha.find(':'));
459 if (exist(library)) {
460 std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
461 char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
462 insertDB(tlc_db, sql);
463 restore_library.push_back(fileSha);
470 tac_error_e tlc_restoreDB()
472 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
473 fprintf(stderr, "Failed to remove of %s\n", TLC_APP_LIST_RESTORE_DB);
474 return TAC_ERROR_UNKNOWN;
477 std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
478 if (!removeFile(dbRestoreJournal)) {
479 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
480 return TAC_ERROR_UNKNOWN;
483 tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
485 fprintf(stderr, "Sqlite create error\n");
486 return TAC_ERROR_UNKNOWN;
488 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
490 pkgmgrinfo_appinfo_metadata_filter_h handle;
491 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
492 if (ret != PMINFO_R_OK) {
493 return TAC_ERROR_UNKNOWN;
496 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
497 if (ret != PMINFO_R_OK) {
498 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
499 return TAC_ERROR_UNKNOWN;
502 ret = pkgmgrMDFilterForeach(handle, tlc_restoreDBCb, NULL);
504 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
505 return TAC_ERROR_UNKNOWN;
508 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
515 if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
516 fprintf(stderr, "Failed to copy of %s\n", TLC_APP_LIST_DB);
517 return TAC_ERROR_UNKNOWN;
519 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
520 fprintf(stderr, "Failed to remove of %s\n", TLC_APP_LIST_RESTORE_DB);
521 return TAC_ERROR_UNKNOWN;
524 std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
525 if (!copyFile(dbRestoreJournal, dbJournal)) {
526 fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
527 return TAC_ERROR_UNKNOWN;
529 if (!removeFile(dbRestoreJournal)) {
530 fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
531 return TAC_ERROR_UNKNOWN;
534 auto convert = [](const std::string& path, const std::string& filename) {
535 bool isExist = false;
536 for (auto& library : restore_library) {
537 if (!strcmp(filename.c_str(), library.c_str())) {
543 if (!removeFile(path)) {
544 _ERR("Failed to remove of %s", path.c_str());
549 scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
551 return TAC_ERROR_NONE;