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 for (auto& nuget : nugetPackages.getMemberNames()) {
375 //Skip the nuget package related to Tizen
376 if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
377 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
378 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
379 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
380 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
381 if (assemblies != Json::nullValue) {
382 // handle assembly even though that is included in the TPA.
383 for (auto& assembly : assemblies.getMemberNames()) {
384 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
385 parserData.push_back(nuget + ":" + assemblyName);
386 _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
394 } catch (const Json::Exception& error) {
395 _ERR("Failed to parse Json: %s", error.what());
400 std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
402 std::vector<std::string> LibrariesInfo;
403 std::string binDir = concatPath(rootPath, "bin");
405 return LibrariesInfo;
407 auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
408 if (filename.find(".so", filename.size() - 3) != std::string::npos || filepath.rfind(".so.") != std::string::npos) {
409 std::string buffer = SHA256(filepath);
410 LibrariesInfo.push_back(filepath + ":" + buffer);
411 _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
414 scanFilesInDirectory(binDir, convert, -1);
416 return LibrariesInfo;
419 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
420 static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
424 std::string rootPath;
426 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
427 if (ret != PMINFO_R_OK) {
428 _SERR("Failed to get pkgid");
432 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
433 if (ret != PMINFO_R_OK) {
434 _SERR("Failed to get root path");
437 rootPath = std::string(root);
439 for (auto& librarySha : getLibrariesInfo(rootPath)) {
440 std::string library = librarySha.substr(0, librarySha.find(':'));
441 if (exist(library)) {
442 std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
443 char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
444 insertDB(tlc_db, sql);
445 restore_library.push_back(fileSha);
452 tac_error_e tlc_restoreDB()
454 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
455 _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
456 return TAC_ERROR_UNKNOWN;
459 std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
460 if (!removeFile(dbRestoreJournal)) {
461 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
462 return TAC_ERROR_UNKNOWN;
465 tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
467 _SERR("Sqlite create error");
468 return TAC_ERROR_UNKNOWN;
470 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
472 pkgmgrinfo_appinfo_metadata_filter_h handle;
473 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
474 if (ret != PMINFO_R_OK) {
475 return TAC_ERROR_UNKNOWN;
478 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
479 if (ret != PMINFO_R_OK) {
480 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
481 return TAC_ERROR_UNKNOWN;
484 ret = pkgmgrMDFilterForeach(handle, tlc_restoreDBCb, NULL);
486 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
487 return TAC_ERROR_UNKNOWN;
490 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
497 if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
498 _SERR("Failed to copy of %s", TLC_APP_LIST_DB);
499 return TAC_ERROR_UNKNOWN;
501 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
502 _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
503 return TAC_ERROR_UNKNOWN;
506 std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
507 if (!copyFile(dbRestoreJournal, dbJournal)) {
508 _SERR("Failed to copy of %s", dbJournal.c_str());
509 return TAC_ERROR_UNKNOWN;
511 if (!removeFile(dbRestoreJournal)) {
512 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
513 return TAC_ERROR_UNKNOWN;
516 auto convert = [](const std::string& path, const std::string& filename) {
517 bool isExist = false;
518 for (auto& library : restore_library) {
519 if (!strcmp(filename.c_str(), library.c_str())) {
525 if (!removeFile(path)) {
526 _ERR("Failed to remove of %s", path.c_str());
531 scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
533 return TAC_ERROR_NONE;