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)
89 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
90 if (ret != PMINFO_R_OK) {
91 _SERR("Failed to get pkgid");
95 enableTACPackage(std::string(pkgId));
97 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
98 if (ret != PMINFO_R_OK) {
99 _SERR("Failed to get root path");
102 rootPath = std::string(root);
104 ret = pkgmgrinfo_appinfo_get_exec(handle, &exec);
105 if (ret != PMINFO_R_OK) {
106 _SERR("Failed to get exec name");
109 execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
111 std::vector<std::string> parserData;
112 std::string binDir = concatPath(rootPath, "bin");
113 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
114 for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
115 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
116 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
117 if (exist(tacDir) && exist(concatPath(tacDir, assemblyName))) {
118 parserData.push_back(nugetPackage);
121 std::sort(parserData.begin(), parserData.end());
122 parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
124 for (auto& nuget : parserData) {
126 std::string name = nuget.substr(0, nuget.find('/'));
127 std::string version = nuget.substr(nuget.rfind('/') + 1);
128 char *sql = sqlite3_mprintf(
129 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
130 "VALUES (%Q, %Q, %Q, %Q);", pkgId, nuget.c_str(), name.c_str(), version.c_str());
131 insertDB(tac_db, sql);
132 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 = pkgmgrMDFilterForeach(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 tac_error_e disableTACPackage(const std::string& pkgId)
211 std::string rootPath = getRootPath(pkgId);
212 if (rootPath.empty()) {
213 _SERR("Failed to get root path from [%s]", pkgId.c_str());
214 return TAC_ERROR_INVALID_PACKAGE;
217 std::string binDir = concatPath(rootPath, "bin");
218 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
219 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
222 for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
223 std::string symPath = symlinkAssembly.path().string();
224 std::string fileName = symlinkAssembly.path().filename().string();
225 if (isSymlinkFile(symPath)) {
226 std::string originPath = bf::read_symlink(symPath).string();
227 if (!isNativeImage(symPath)) {
228 std::string dllPath = concatPath(binDir, fileName);
229 if (!copyFile(originPath, dllPath)) {
230 _SERR("Failed to copy of %s", dllPath.c_str());
231 return TAC_ERROR_UNKNOWN;
233 copySmackAndOwnership(binDir.c_str(), concatPath(binDir, fileName).c_str());
235 std::string niPath = concatPath(binNIDir, fileName);
236 if (!copyFile(originPath, niPath)) {
237 _SERR("Failed to copy of %s", niPath.c_str());
238 return TAC_ERROR_UNKNOWN;
240 copySmackAndOwnership(binDir.c_str(), niPath.c_str());
244 if (!removeAll(tacDir)) {
245 _SERR("Failed to remove of %s", tacDir.c_str());
246 return TAC_ERROR_UNKNOWN;
248 } catch (const bf::filesystem_error& error) {
249 _SERR("Failed to recursive directory: %s", error.what());
250 return TAC_ERROR_UNKNOWN;
253 return TAC_ERROR_NONE;
256 tac_error_e enableTACPackage(const std::string& pkgId)
258 std::string rootPath = getRootPath(pkgId);
259 if (rootPath.empty()) {
260 _SERR("Failed to get root path from [%s]", pkgId.c_str());
261 return TAC_ERROR_INVALID_PACKAGE;
264 std::string execName = getExecName(pkgId);
265 if (execName.empty()) {
266 _SERR("Failed to get exec name from [%s]", pkgId.c_str());
267 return TAC_ERROR_INVALID_PACKAGE;
270 std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
271 if (metaValue.empty()) {
272 _SERR("Failed to get metadata from [%s]", pkgId.c_str());
273 return TAC_ERROR_INVALID_PACKAGE;
276 if (!strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) {
277 std::string binDir = concatPath(rootPath, "bin");
278 std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
279 std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
280 if (!exist(tacDir)) {
281 if (!createDir(tacDir)) {
282 _SERR("Cannot create directory: %s", tacDir.c_str());
283 return TAC_ERROR_UNKNOWN;
285 copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
287 std::vector<std::string> enableNuget;
288 for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
289 std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
290 std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
291 std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
292 if (exist(nugetPath)) {
293 std::string originPath = concatPath(nugetPath, assemblyName);
294 if (exist(originPath)) {
295 enableNuget.push_back(originPath);
300 bs::error_code error;
301 for (auto& originPath : enableNuget) {
302 if (exist(originPath)) {
303 std::string fileName = originPath.substr(originPath.rfind('/') + 1);
304 std::string NIFileName = changeExtension(fileName, "dll", "ni.dll");
305 if (exist(binNIDir)) {
306 std::string originNIPath = changeExtension(originPath, "dll", "ni.dll");
307 if (exist(originNIPath)) {
308 bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName), error);
310 _SERR("Failed to create symlink %s file", concatPath(tacDir, NIFileName).c_str());
311 return TAC_ERROR_UNKNOWN;
313 _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, NIFileName).c_str());
314 copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
316 if (!removeFile(concatPath(binNIDir, NIFileName))) {
317 _SERR("Failed to remove of %s", concatPath(binNIDir, NIFileName).c_str());
318 return TAC_ERROR_UNKNOWN;
322 bf::create_symlink(originPath, concatPath(tacDir, fileName), error);
324 _SERR("Failed to create symlink %s file", concatPath(tacDir, fileName).c_str());
325 return TAC_ERROR_UNKNOWN;
327 _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, fileName).c_str());
328 copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
330 if (!removeFile(concatPath(binDir, fileName))) {
331 _SERR("Failed to remove of %s", concatPath(binDir, fileName).c_str());
332 return TAC_ERROR_UNKNOWN;
336 if (enableNuget.empty()) {
337 if (!removeAll(tacDir)) {
338 _SERR("Failed to remove of %s", tacDir.c_str());
344 _SERR("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str());
346 return TAC_ERROR_NONE;
349 //Parser the .deps.json file to get nuget information.
350 std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::string& execName)
352 std::vector<std::string> parserData;
353 std::string depsJsonName = changeExtension(execName, "dll", "deps.json");
354 std::string depsJsonPath = concatPath(rootPath, depsJsonName);
356 if (exist(depsJsonPath)) {
357 std::ifstream ifs(depsJsonPath);
358 Json::CharReaderBuilder reader;
362 if (!Json::parseFromStream(reader, ifs, &root, &error)) {
363 _ERR("Failed to parse of deps.json");
367 const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
368 const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
369 std::vector<std::string> appDependencies;
370 for (auto& nuget : nugetPackages.getMemberNames()) {
371 if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
372 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
373 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
374 if (assemblies != Json::nullValue) {
375 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
376 for (auto& dependency : dependencies.getMemberNames()) {
377 appDependencies.push_back(dependency);
382 for (auto& nuget : nugetPackages.getMemberNames()) {
383 //Skip the nuget package related to Tizen
384 if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
385 strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
386 strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
387 strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
388 const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
389 if (assemblies != Json::nullValue) {
390 const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
391 bool hasDependency = false;
392 for (auto& dependency : dependencies.getMemberNames()) {
393 //Skip the nugget package that is dependent on another nuget package
394 if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
395 strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
396 hasDependency = true;
397 for (auto& ad : appDependencies) {
398 if (!strcmp(ad.c_str(), dependency.c_str())) {
399 hasDependency = true;
402 hasDependency = false;
405 if (hasDependency) break;
408 if (!hasDependency) {
409 // handle assembly even though that is included in the TPA.
410 for (auto& assembly : assemblies.getMemberNames()) {
411 std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
412 parserData.push_back(nuget + ":" + assemblyName);
413 _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
419 appDependencies.clear();
423 } catch (const Json::LogicError& error) {
424 _ERR("Failed to parse Json: %s", error.what());
429 std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
431 std::vector<std::string> LibrariesInfo;
432 std::string binDir = concatPath(rootPath, "bin");
434 return LibrariesInfo;
436 auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
437 if (filepath.rfind(".so") != std::string::npos) {
438 std::string buffer = SHA256(filepath);
439 LibrariesInfo.push_back(filepath + ":" + buffer);
440 _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
443 scanFilesInDirectory(binDir, convert, -1);
445 return LibrariesInfo;
448 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
449 static int tlc_restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
453 std::string rootPath;
455 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
456 if (ret != PMINFO_R_OK) {
457 _SERR("Failed to get pkgid");
461 ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
462 if (ret != PMINFO_R_OK) {
463 _SERR("Failed to get root path");
466 rootPath = std::string(root);
468 for (auto& librarySha : getLibrariesInfo(rootPath)) {
469 std::string library = librarySha.substr(0, librarySha.find(':'));
470 if (exist(library)) {
471 std::string fileSha = library.substr(library.rfind('/') + 1) + ".." + librarySha.substr(librarySha.find(':') + 1);
472 char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
473 insertDB(tlc_db, sql);
474 restore_library.push_back(fileSha);
481 tac_error_e tlc_restoreDB()
483 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
484 _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
485 return TAC_ERROR_UNKNOWN;
488 std::string dbRestoreJournal = TLC_APP_LIST_RESTORE_DB + std::string("-journal");
489 if (!removeFile(dbRestoreJournal)) {
490 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
491 return TAC_ERROR_UNKNOWN;
494 tlc_db = createDB(TLC_APP_LIST_RESTORE_DB, CREATE_TLC_DB_TABLE);
496 _SERR("Sqlite create error");
497 return TAC_ERROR_UNKNOWN;
499 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
501 pkgmgrinfo_appinfo_metadata_filter_h handle;
502 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
503 if (ret != PMINFO_R_OK) {
504 return TAC_ERROR_UNKNOWN;
507 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
508 if (ret != PMINFO_R_OK) {
509 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
510 return TAC_ERROR_UNKNOWN;
513 ret = pkgmgrMDFilterForeach(handle, tlc_restoreDBCb, NULL);
515 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
516 return TAC_ERROR_UNKNOWN;
519 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
526 if (!copyFile(TLC_APP_LIST_RESTORE_DB, TLC_APP_LIST_DB)) {
527 _SERR("Failed to copy of %s", TLC_APP_LIST_DB);
528 return TAC_ERROR_UNKNOWN;
530 if (!removeFile(TLC_APP_LIST_RESTORE_DB)) {
531 _SERR("Failed to remove of %s", TLC_APP_LIST_RESTORE_DB);
532 return TAC_ERROR_UNKNOWN;
535 std::string dbJournal = TLC_APP_LIST_DB + std::string("-journal");
536 if (!copyFile(dbRestoreJournal, dbJournal)) {
537 _SERR("Failed to copy of %s", dbJournal.c_str());
538 return TAC_ERROR_UNKNOWN;
540 if (!removeFile(dbRestoreJournal)) {
541 _SERR("Failed to remove of %s", dbRestoreJournal.c_str());
542 return TAC_ERROR_UNKNOWN;
545 auto convert = [](const std::string& path, const std::string& filename) {
546 bool isExist = false;
547 for (auto& library : restore_library) {
548 if (!strcmp(filename.c_str(), library.c_str())) {
554 if (!removeFile(path)) {
555 _ERR("Failed to remove of %s", path.c_str());
560 scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
562 return TAC_ERROR_NONE;