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);
35 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
39 static sqlite3 *tac_db = NULL;
40 static sqlite3 *tlc_db = NULL;
41 std::vector<std::string> restore_nuget;
42 std::vector<std::string> restore_library;
44 static void cleanupDirectory()
46 std::vector<std::string> removeNuget;
48 for (auto& nuget : bf::recursive_directory_iterator(__DOTNET_DIR)) {
49 std::string nugetPath = nuget.path().string();
50 if (!bf::is_directory(nugetPath) ||
51 nugetPath.find(TLC_LIBRARIES_DIR) != std::string::npos ||
52 nugetPath.find(__READ_ONLY_APP_UPDATE_DIR) != std::string::npos) {
57 for (auto& restore : restore_nuget) {
58 if (nugetPath == restore || nugetPath == getBaseName(restore)) {
64 removeNuget.push_back(nugetPath);
68 for (auto& rm : removeNuget) {
70 _SERR("Failed to remove of %s", rm.c_str());
74 } catch (const bf::filesystem_error& error) {
75 _SERR("Failed to recursive directory: %s", error.what());
80 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
81 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 _SERR("Failed to get pkgid");
93 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
94 if (ret != PMINFO_R_OK) {
95 _SERR("Failed to get root path");
98 std::string rootPath = std::string(root);
100 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
101 if (ret != PMINFO_R_OK) {
102 _SERR("Failed to get exec name");
105 std::string execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
107 enableTACPackage(std::string(pkgId));
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));
139 tac_error_e tac_restoreDB()
141 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
142 _SERR("Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
143 return TAC_ERROR_UNKNOWN;
146 std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
147 if (!removeFile(dbRestoreJournal)) {
148 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
149 return TAC_ERROR_UNKNOWN;
152 tac_db = createDB(TAC_APP_LIST_RESTORE_DB, CREATE_TAC_DB_TABLE);
154 _SERR("Sqlite create error");
155 return TAC_ERROR_UNKNOWN;
157 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
159 pkgmgrinfo_appinfo_metadata_filter_h handle;
160 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
161 if (ret != PMINFO_R_OK) {
162 return TAC_ERROR_UNKNOWN;
165 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
166 if (ret != PMINFO_R_OK) {
167 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
168 return TAC_ERROR_UNKNOWN;
171 ret = pkgmgrMDFilterForeach(handle, tac_restoreDBCb, NULL);
173 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
174 return TAC_ERROR_UNKNOWN;
177 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
184 if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
185 _SERR("Failed to copy of %s", TAC_APP_LIST_DB);
186 return TAC_ERROR_UNKNOWN;
188 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
189 _SERR("Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
190 return TAC_ERROR_UNKNOWN;
193 std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
194 if (!copyFile(dbRestoreJournal, dbJournal)) {
195 _SERR("Failed to copy of %s", dbJournal.c_str());
196 return TAC_ERROR_UNKNOWN;
198 if (!removeFile(dbRestoreJournal)) {
199 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
200 return TAC_ERROR_UNKNOWN;
205 return TAC_ERROR_NONE;
208 tac_error_e disableTACPackage(const std::string& pkgId)
210 std::string rootPath = getRootPath(pkgId);
211 if (rootPath.empty()) {
212 _SERR("Failed to get root path from [%s]", pkgId.c_str());
213 return TAC_ERROR_INVALID_PACKAGE;
216 std::string binDir = concatPath(rootPath, "bin");
217 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
218 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
221 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
222 std::string symPath = symlinkAssembly.path().string();
223 std::string fileName = symlinkAssembly.path().filename().string();
224 if (isSymlinkFile(symPath)) {
225 std::string originPath = bf::read_symlink(symPath).string();
226 if (!isNativeImage(symPath)) {
227 std::string dllPath = concatPath(binDir, fileName);
228 if (!copyFile(originPath, dllPath)) {
229 _SERR("Failed to copy of %s", dllPath.c_str());
230 return TAC_ERROR_UNKNOWN;
232 copySmackAndOwnership(binDir.c_str(), concatPath(binDir, fileName).c_str());
234 std::string niPath = concatPath(binNIDir, fileName);
235 if (!copyFile(originPath, niPath)) {
236 _SERR("Failed to copy of %s", niPath.c_str());
237 return TAC_ERROR_UNKNOWN;
239 copySmackAndOwnership(binDir.c_str(), niPath.c_str());
243 if (!removeAll(tacDir)) {
244 _SERR("Failed to remove of %s", tacDir.c_str());
245 return TAC_ERROR_UNKNOWN;
247 } catch (const bf::filesystem_error& error) {
248 _SERR("Failed to recursive directory: %s", error.what());
249 return TAC_ERROR_UNKNOWN;
252 return TAC_ERROR_NONE;
255 tac_error_e enableTACPackage(const std::string& pkgId)
257 std::string rootPath = getRootPath(pkgId);
258 if (rootPath.empty()) {
259 _SERR("Failed to get root path from [%s]", pkgId.c_str());
260 return TAC_ERROR_INVALID_PACKAGE;
263 std::string binDir = concatPath(rootPath, "bin");
264 if (exist(concatPath(binDir, PRE_COMPILED_PACKAGE_FILE))) {
265 _INFO("The %s is a Pre-Compiled package. So, skip the TAC", pkgId.c_str());
266 return TAC_ERROR_NONE;
269 std::string execName = getExecName(pkgId);
270 if (execName.empty()) {
271 _SERR("Failed to get exec name from [%s]", pkgId.c_str());
272 return TAC_ERROR_INVALID_PACKAGE;
275 std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
276 if (metaValue.empty()) {
277 _SERR("Failed to get metadata from [%s]", pkgId.c_str());
278 return TAC_ERROR_INVALID_PACKAGE;
281 if (!strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) {
282 std::string binDir = concatPath(rootPath, "bin");
283 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
284 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
285 if (!exist(tacDir)) {
286 if (!createDir(tacDir)) {
287 _SERR("Cannot create directory: %s", tacDir.c_str());
288 return TAC_ERROR_UNKNOWN;
290 copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
292 std::vector<std::string> enableNuget;
293 for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
294 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
295 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
296 std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
297 if (exist(nugetPath)) {
298 std::string originPath = concatPath(nugetPath, assemblyName);
299 if (exist(originPath)) {
300 enableNuget.push_back(originPath);
305 bs::error_code error;
306 for (auto& originPath : enableNuget) {
307 if (exist(originPath)) {
308 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
309 std::string NIFileName = changeExtension(fileName, "dll", "ni.dll");
310 if (exist(binNIDir)) {
311 std::string originNIPath = changeExtension(originPath, "dll", "ni.dll");
312 if (exist(originNIPath)) {
313 bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName), error);
315 _SERR("Failed to create symlink %s file", concatPath(tacDir, NIFileName).c_str());
316 return TAC_ERROR_UNKNOWN;
318 _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, NIFileName).c_str());
319 copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
321 if (!removeFile(concatPath(binNIDir, NIFileName))) {
322 _SERR("Failed to remove of %s", concatPath(binNIDir, NIFileName).c_str());
323 return TAC_ERROR_UNKNOWN;
327 bf::create_symlink(originPath, concatPath(tacDir, fileName), error);
329 _SERR("Failed to create symlink %s file", concatPath(tacDir, fileName).c_str());
330 return TAC_ERROR_UNKNOWN;
332 _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, fileName).c_str());
333 copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
335 if (!removeFile(concatPath(binDir, fileName))) {
336 _SERR("Failed to remove of %s", concatPath(binDir, fileName).c_str());
337 return TAC_ERROR_UNKNOWN;
341 if (enableNuget.empty()) {
342 if (!removeAll(tacDir)) {
343 _SERR("Failed to remove of %s", tacDir.c_str());
349 _SERR("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str());
351 return TAC_ERROR_NONE;
354 //Parser the .deps.json file to get nuget information.
355 std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::string& execName)
357 std::vector<std::string> parserData;
358 std::string depsJsonName = changeExtension(execName, "dll", "deps.json");
359 std::string depsJsonPath = concatPath(rootPath, depsJsonName);
361 if (exist(depsJsonPath)) {
362 std::ifstream ifs(depsJsonPath);
363 Json::CharReaderBuilder reader;
367 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
368 _ERR("Failed to parse of deps.json");
372 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
373 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
374 std::vector<std::string> appDependencies;
375 for (auto& nuget : nugetPackages.getMemberNames()) {
376 if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
377 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
378 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
379 if (assemblies != Json::nullValue) {
380 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
381 for (auto& dependency : dependencies.getMemberNames()) {
382 appDependencies.push_back(dependency);
387 for (auto& nuget : nugetPackages.getMemberNames()) {
388 //Skip the nuget package related to Tizen
389 if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
390 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
391 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
392 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
393 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
394 if (assemblies != Json::nullValue) {
395 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
396 bool hasDependency = false;
397 for (auto& dependency : dependencies.getMemberNames()) {
398 //Skip the nugget package that is dependent on another nuget package
399 if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
400 strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
401 hasDependency = true;
402 for (auto& ad : appDependencies) {
403 if (!strcmp(ad.c_str(), dependency.c_str())) {
404 hasDependency = true;
407 hasDependency = false;
410 if (hasDependency) break;
413 if (!hasDependency) {
414 // handle assembly even though that is included in the TPA.
415 for (auto& assembly : assemblies.getMemberNames()) {
416 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
417 parserData.push_back(nuget + ":" + assemblyName);
418 _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
424 appDependencies.clear();
428 } catch (const Json::Exception& error) {
429 _ERR("Failed to parse Json: %s", error.what());
434 std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
436 std::vector<std::string> LibrariesInfo;
437 std::string binDir = concatPath(rootPath, "bin");
439 return LibrariesInfo;
441 auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
442 if (filepath.rfind(".so") != std::string::npos) {
443 std::string buffer = SHA256(filepath);
444 LibrariesInfo.push_back(filepath + ":" + buffer);
445 _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
448 scanFilesInDirectory(binDir, convert, -1);
450 return LibrariesInfo;
453 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
454 static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
458 std::string rootPath;
460 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
461 if (ret != PMINFO_R_OK) {
462 _SERR("Failed to get pkgid");
466 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
467 if (ret != PMINFO_R_OK) {
468 _SERR("Failed to get root path");
471 rootPath = std::string(root);
473 for (auto& librarySha : getLibrariesInfo(rootPath)) {
474 std::string library = librarySha.substr(0, librarySha.find(':'));
475 if (exist(library)) {
476 std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
477 char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
478 insertDB(tlc_db, sql);
479 restore_library.push_back(fileSha);
486 tac_error_e tlc_restoreDB()
488 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
489 _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
490 return TAC_ERROR_UNKNOWN;
493 std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
494 if (!removeFile(dbRestoreJournal)) {
495 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
496 return TAC_ERROR_UNKNOWN;
499 tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
501 _SERR("Sqlite create error");
502 return TAC_ERROR_UNKNOWN;
504 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
506 pkgmgrinfo_appinfo_metadata_filter_h handle;
507 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
508 if (ret != PMINFO_R_OK) {
509 return TAC_ERROR_UNKNOWN;
512 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
513 if (ret != PMINFO_R_OK) {
514 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
515 return TAC_ERROR_UNKNOWN;
518 ret = pkgmgrMDFilterForeach(handle, tlc_restoreDBCb, NULL);
520 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
521 return TAC_ERROR_UNKNOWN;
524 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
531 if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
532 _SERR("Failed to copy of %s", TLC_APP_LIST_DB);
533 return TAC_ERROR_UNKNOWN;
535 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
536 _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
537 return TAC_ERROR_UNKNOWN;
540 std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
541 if (!copyFile(dbRestoreJournal, dbJournal)) {
542 _SERR("Failed to copy of %s", dbJournal.c_str());
543 return TAC_ERROR_UNKNOWN;
545 if (!removeFile(dbRestoreJournal)) {
546 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
547 return TAC_ERROR_UNKNOWN;
550 auto convert = [](const std::string& path, const std::string& filename) {
551 bool isExist = false;
552 for (auto& library : restore_library) {
553 if (!strcmp(filename.c_str(), library.c_str())) {
559 if (!removeFile(path)) {
560 _ERR("Failed to remove of %s", path.c_str());
565 scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
567 return TAC_ERROR_NONE;