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"
26 #include "r2r_checker.h"
31 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
34 #define __STR(x) __XSTR(x)
35 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
36 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
40 static sqlite3 *tac_db = NULL;
41 static sqlite3 *tlc_db = NULL;
42 static std::vector<std::string> restore_nuget;
43 static std::vector<std::string> restore_library;
45 static void cleanupDirectory()
47 std::vector<std::string> removeNuget;
49 for (auto& nuget : bf::recursive_directory_iterator(__DOTNET_DIR)) {
50 std::string nugetPath = nuget.path().string();
51 if (!bf::is_directory(nugetPath) ||
52 nugetPath.find(TLC_LIBRARIES_DIR) != std::string::npos ||
53 nugetPath.find(__READ_ONLY_APP_UPDATE_DIR) != std::string::npos) {
58 for (auto& restore : restore_nuget) {
59 if (nugetPath == restore || nugetPath == getBaseName(restore)) {
65 removeNuget.push_back(nugetPath);
69 for (auto& rm : removeNuget) {
71 _SERR("Failed to remove of %s", rm.c_str());
75 } catch (const bf::filesystem_error& error) {
76 _SERR("Failed to recursive directory: %s", error.what());
81 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
82 static int tac_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
88 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
89 if (ret != PMINFO_R_OK) {
90 _SERR("Failed to get pkgid");
94 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
95 if (ret != PMINFO_R_OK) {
96 _SERR("Failed to get root path");
99 std::string rootPath = std::string(root);
101 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
102 if (ret != PMINFO_R_OK) {
103 _SERR("Failed to get exec name");
106 std::string execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
108 enableTACPackage(std::string(pkgId));
110 std::vector<std::string> parserData;
111 std::string binDir = concatPath(rootPath, "bin");
112 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
113 for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
114 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
115 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
116 if (exist(tacDir) && exist(concatPath(tacDir, assemblyName))) {
117 parserData.push_back(nugetPackage);
120 std::sort(parserData.begin(), parserData.end());
121 parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
123 for (auto& nuget : parserData) {
125 std::string name = nuget.substr(0, nuget.find('/'));
126 std::string version = nuget.substr(nuget.rfind('/') + 1);
127 char *sql = sqlite3_mprintf(
128 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
129 "VALUES (%Q, %Q, %Q, %Q);", pkgId, nuget.c_str(), name.c_str(), version.c_str());
130 insertDB(tac_db, sql);
131 restore_nuget.push_back(concatPath(__DOTNET_DIR, nuget));
140 tac_error_e tac_restoreDB()
142 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
143 _SERR("Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
144 return TAC_ERROR_UNKNOWN;
147 std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
148 if (!removeFile(dbRestoreJournal)) {
149 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
150 return TAC_ERROR_UNKNOWN;
153 tac_db = createDB(TAC_APP_LIST_RESTORE_DB, CREATE_TAC_DB_TABLE);
155 _SERR("Sqlite create error");
156 return TAC_ERROR_UNKNOWN;
158 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
160 pkgmgrinfo_appinfo_metadata_filter_h handle;
161 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
162 if (ret != PMINFO_R_OK) {
163 return TAC_ERROR_UNKNOWN;
166 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
167 if (ret != PMINFO_R_OK) {
168 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
169 return TAC_ERROR_UNKNOWN;
172 ret = pkgmgrAppMDFilterForeach(handle, tac_restoreDBCb, NULL);
174 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
175 return TAC_ERROR_UNKNOWN;
178 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
185 if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
186 _SERR("Failed to copy of %s", TAC_APP_LIST_DB);
187 return TAC_ERROR_UNKNOWN;
189 if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
190 _SERR("Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
191 return TAC_ERROR_UNKNOWN;
194 std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
195 if (!copyFile(dbRestoreJournal, dbJournal)) {
196 _SERR("Failed to copy of %s", dbJournal.c_str());
197 return TAC_ERROR_UNKNOWN;
199 if (!removeFile(dbRestoreJournal)) {
200 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
201 return TAC_ERROR_UNKNOWN;
206 return TAC_ERROR_NONE;
209 static tac_error_e createSymlinkFile(const std::string& tacDir, const std::string& binDir, const std::string& from, const std::string& to)
211 bs::error_code error;
212 std::string symlinkFile = concatPath(tacDir, to);
213 bf::create_symlink(from, symlinkFile, error);
215 _SERR("Failed to create symlink %s file", symlinkFile.c_str());
216 return TAC_ERROR_UNKNOWN;
218 _SOUT("%s symbolic link file generated successfully.", symlinkFile.c_str());
219 copySmackAndOwnership(tacDir.c_str(), symlinkFile.c_str(), true);
221 if (!removeFile(concatPath(binDir, to))) {
222 _SERR("Failed to remove of %s", concatPath(binDir, to).c_str());
223 return TAC_ERROR_UNKNOWN;
225 return TAC_ERROR_NONE;
228 tac_error_e disableTACPackage(const std::string& pkgId)
230 std::string rootPath = getRootPath(pkgId);
231 if (rootPath.empty()) {
232 _SERR("Failed to get root path from [%s]", pkgId.c_str());
233 return TAC_ERROR_INVALID_PACKAGE;
236 std::string binDir = concatPath(rootPath, "bin");
237 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
238 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
241 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
242 std::string symPath = symlinkAssembly.path().string();
243 std::string fileName = symlinkAssembly.path().filename().string();
244 if (isSymlinkFile(symPath)) {
245 std::string originPath = bf::read_symlink(symPath).string();
246 if (!isR2RImage(symPath)) {
247 std::string dllPath = concatPath(binDir, fileName);
248 if (!copyFile(originPath, dllPath)) {
249 _SERR("Failed to copy of %s", dllPath.c_str());
250 return TAC_ERROR_UNKNOWN;
252 copySmackAndOwnership(binDir.c_str(), concatPath(binDir, fileName).c_str());
254 std::string niPath = concatPath(binNIDir, fileName);
255 if (!copyFile(originPath, niPath)) {
256 _SERR("Failed to copy of %s", niPath.c_str());
257 return TAC_ERROR_UNKNOWN;
259 copySmackAndOwnership(binDir.c_str(), niPath.c_str());
263 if (!removeAll(tacDir)) {
264 _SERR("Failed to remove of %s", tacDir.c_str());
265 return TAC_ERROR_UNKNOWN;
267 } catch (const bf::filesystem_error& error) {
268 _SERR("Failed to recursive directory: %s", error.what());
269 return TAC_ERROR_UNKNOWN;
272 return TAC_ERROR_NONE;
275 tac_error_e enableTACPackage(const std::string& pkgId)
277 std::string rootPath = getRootPath(pkgId);
278 if (rootPath.empty()) {
279 _SERR("Failed to get root path from [%s]", pkgId.c_str());
280 return TAC_ERROR_INVALID_PACKAGE;
283 std::string binDir = concatPath(rootPath, "bin");
284 if (exist(concatPath(binDir, PRE_COMPILED_PACKAGE_FILE))) {
285 _INFO("The %s is a Pre-Compiled package. So, skip the TAC", pkgId.c_str());
286 return TAC_ERROR_NONE;
289 std::string execName = getExecName(pkgId);
290 if (execName.empty()) {
291 _SERR("Failed to get exec name from [%s]", pkgId.c_str());
292 return TAC_ERROR_INVALID_PACKAGE;
295 std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
296 if (metaValue.empty()) {
297 _SERR("Failed to get metadata from [%s]", pkgId.c_str());
298 return TAC_ERROR_INVALID_PACKAGE;
301 if (strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) {
302 _INFO("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str());
303 return TAC_ERROR_NONE;
306 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
307 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
309 return TAC_ERROR_NONE;
312 if (!createDir(tacDir)) {
313 _SERR("Cannot create directory: %s", tacDir.c_str());
314 return TAC_ERROR_UNKNOWN;
316 copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
318 std::vector<std::string> enableNuget;
319 for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
320 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
321 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
322 std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
323 if (exist(nugetPath)) {
324 std::string originPath = concatPath(nugetPath, assemblyName);
325 if (exist(originPath)) {
326 enableNuget.push_back(originPath);
331 if (enableNuget.empty()) {
332 if (!removeAll(tacDir)) {
333 _SERR("Failed to remove of %s", tacDir.c_str());
335 return TAC_ERROR_NONE;
338 for (auto& originPath : enableNuget) {
339 if (exist(originPath)) {
340 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
341 if (exist(binNIDir)) {
342 std::string originNIPath = changeExtension(originPath, "dll", "ni.dll");
343 if (exist(originNIPath)) {
344 if (createSymlinkFile(tacDir, binNIDir, originNIPath, changeExtension(fileName, "dll", "ni.dll")) != TAC_ERROR_NONE) {
345 return TAC_ERROR_UNKNOWN;
349 if (createSymlinkFile(tacDir, binDir, originPath, fileName) != TAC_ERROR_NONE) {
350 return TAC_ERROR_UNKNOWN;
356 return TAC_ERROR_NONE;
359 //Parser the .deps.json file to get nuget information.
360 std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::string& execName)
362 std::vector<std::string> parserData;
363 std::string depsJsonName = changeExtension(execName, "dll", "deps.json");
364 std::string depsJsonPath = concatPath(rootPath, depsJsonName);
366 if (exist(depsJsonPath)) {
367 std::ifstream ifs(depsJsonPath);
368 Json::CharReaderBuilder reader;
372 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
373 _ERR("Failed to parse of deps.json");
377 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
378 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
379 for (auto& nuget : nugetPackages.getMemberNames()) {
380 //Skip the nuget package related to Tizen
381 if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
382 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
383 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
384 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
385 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
386 if (assemblies != Json::nullValue) {
387 // handle assembly even though that is included in the TPA.
388 for (auto& assembly : assemblies.getMemberNames()) {
389 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
390 parserData.push_back(nuget + ":" + assemblyName);
391 _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
399 } catch (const Json::Exception& error) {
400 _ERR("Failed to parse Json: %s", error.what());
405 std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
407 std::vector<std::string> LibrariesInfo;
408 std::string binDir = concatPath(rootPath, "bin");
410 return LibrariesInfo;
412 auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
413 if (filename.find(".so", filename.size() - 3) != std::string::npos || filepath.rfind(".so.") != std::string::npos) {
414 std::string buffer = SHA256(filepath);
415 LibrariesInfo.push_back(filepath + ":" + buffer);
416 _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
419 scanFilesInDirectory(binDir, convert, -1);
421 return LibrariesInfo;
424 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
425 static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
429 std::string rootPath;
431 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
432 if (ret != PMINFO_R_OK) {
433 _SERR("Failed to get pkgid");
437 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
438 if (ret != PMINFO_R_OK) {
439 _SERR("Failed to get root path");
442 rootPath = std::string(root);
444 for (auto& librarySha : getLibrariesInfo(rootPath)) {
445 std::string library = librarySha.substr(0, librarySha.find(':'));
446 if (exist(library)) {
447 std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
448 char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
449 insertDB(tlc_db, sql);
450 restore_library.push_back(fileSha);
457 tac_error_e tlc_restoreDB()
459 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
460 _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
461 return TAC_ERROR_UNKNOWN;
464 std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
465 if (!removeFile(dbRestoreJournal)) {
466 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
467 return TAC_ERROR_UNKNOWN;
470 tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
472 _SERR("Sqlite create error");
473 return TAC_ERROR_UNKNOWN;
475 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
477 pkgmgrinfo_appinfo_metadata_filter_h handle;
478 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
479 if (ret != PMINFO_R_OK) {
480 return TAC_ERROR_UNKNOWN;
483 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
484 if (ret != PMINFO_R_OK) {
485 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
486 return TAC_ERROR_UNKNOWN;
489 ret = pkgmgrAppMDFilterForeach(handle, tlc_restoreDBCb, NULL);
491 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
492 return TAC_ERROR_UNKNOWN;
495 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
502 if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
503 _SERR("Failed to copy of %s", TLC_APP_LIST_DB);
504 return TAC_ERROR_UNKNOWN;
506 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
507 _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
508 return TAC_ERROR_UNKNOWN;
511 std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
512 if (!copyFile(dbRestoreJournal, dbJournal)) {
513 _SERR("Failed to copy of %s", dbJournal.c_str());
514 return TAC_ERROR_UNKNOWN;
516 if (!removeFile(dbRestoreJournal)) {
517 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
518 return TAC_ERROR_UNKNOWN;
521 auto convert = [](const std::string& path, const std::string& filename) {
522 bool isExist = false;
523 for (auto& library : restore_library) {
524 if (!strcmp(filename.c_str(), library.c_str())) {
530 if (!removeFile(path)) {
531 _ERR("Failed to remove of %s", path.c_str());
536 scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0);
538 return TAC_ERROR_NONE;