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.
19 #include "db_manager.h"
20 #include "tac_common.h"
27 #include <boost/filesystem.hpp>
29 #include <json/json.h>
30 #include <pkgmgr-info.h>
35 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
38 #define __STR(x) __XSTR(x)
39 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
43 std::vector<std::string> nugetPackagesAssembliesSha;
44 std::vector<std::string> tacDB;
45 std::vector<std::string> createDirectories;
46 std::vector<std::string> createLibraries;
47 std::vector<std::string> updateTac;
48 std::string status = "";
49 static sqlite3 *tac_db = NULL;
50 static sqlite3 *tlc_db = NULL;
51 bool tacPluginInstalled = false;
52 bool tacPluginFinished = false;
54 static void createSHA256Info(std::string sha256Info, std::string nugetPackage)
56 std::ofstream ofs(sha256Info, std::ios::app);
57 int assembly_count = 0;
58 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
59 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
60 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
61 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
62 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
63 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
64 ofs << assembly << ":" << sha << std::endl;
68 ofs << assembly_count << std::endl;
72 static bool compareSHA256Info(std::string sha256Info, std::string nugetPackage)
74 int compare_count = 0;
75 int assembly_count = 0;
76 std::string sha256_count = "0";
77 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
78 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
79 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
80 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
81 std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
82 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
84 std::ifstream ifs(sha256Info);
87 while (getline(ifs, get_str)) {
88 if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
91 sha256_count = get_str;
97 if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
98 !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
99 _INFO("Same nuget : %s", nugetPackage.c_str());
105 static bool copyAssemblyCreateSymlink(std::string binPath, std::string tacDir, std::string nugetPackage, bool isCreateTacDir)
107 std::string tac_version_dir = concatPath(__DOTNET_DIR, nugetPackage);
108 bool nuget_restoration = false;
109 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
110 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
111 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
112 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
113 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
114 if (exist(concatPath(binPath, assembly))) {
115 if (isCreateTacDir) {
116 if (!copyFile(concatPath(binPath, assembly), concatPath(tac_version_dir, assembly))) {
117 _ERR("Failed to copy of %s", assembly.c_str());
118 nuget_restoration = true;
122 bf::create_symlink(concatPath(tac_version_dir, assembly), concatPath(tacDir, assembly));
123 copySmackAndOwnership(tacDir, concatPath(tacDir, assembly), true);
125 if (!removeFile(concatPath(binPath, assembly))) {
126 _ERR("Failed to remove of %s", assembly.c_str());
127 nuget_restoration = true;
134 if (nuget_restoration) {
135 for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
136 std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
137 std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
138 std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
139 if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
140 copyFile(concatPath(tac_version_dir, assembly), concatPath(binPath, assembly));
141 copySmackAndOwnership(binPath, concatPath(binPath, assembly));
142 removeFile(concatPath(tacDir, assembly));
147 return nuget_restoration;
150 static void copyLibraryCreateSymlink(const char* pkgId, std::vector<std::string> LibrariesInfo, std::string tlcDir)
152 if (LibrariesInfo.empty()) {
156 for (auto& librarySha : LibrariesInfo) {
157 std::string library = librarySha.substr(0, librarySha.find(':'));
158 std::string filename = library.substr(library.rfind('/') + 1);
159 std::string fileSha = filename + ".." + librarySha.substr(librarySha.find(':') + 1);
160 bool fileCopied = false;
161 if (!exist(concatPath(tlcDir, fileSha))) {
162 if (!copyFile(library, concatPath(tlcDir, fileSha))) {
163 _ERR("Failed to copy of %s", filename.c_str());
167 createLibraries.push_back(concatPath(tlcDir, fileSha));
169 if (!removeFile(library)) {
170 _ERR("Failed to remove of %s", library.c_str());
172 removeFile(concatPath(tlcDir, fileSha));
176 bf::create_symlink(concatPath(tlcDir, fileSha), library);
177 copySmackAndOwnership(getBaseName(library), library, true);
179 char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId, fileSha.c_str());
180 if (!insertDB(tlc_db, sql)) {
181 _ERR("Sqlite insert error");
183 copyFile(concatPath(tlcDir, fileSha), library);
184 copySmackAndOwnership(getBaseName(library), library);
186 removeFile(concatPath(tlcDir, fileSha));
194 static void checkDepsJson(std::string rootPath, std::string binPath, std::string execName)
196 for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
197 std::string nuget_package = npAssembly.substr(0, npAssembly.rfind(':'));
198 std::string assembly_name = npAssembly.substr(npAssembly.rfind(':') + 1);
199 tacDB.push_back(nuget_package);
200 std::string buffer = SHA256(concatPath(binPath, assembly_name));
201 nugetPackagesAssembliesSha.push_back(nuget_package + ":" + assembly_name + ":" + buffer);
202 _INFO("Assembly : [%s] / SHA256 : [%s]", assembly_name.c_str(), buffer.c_str());
204 std::sort(tacDB.begin(), tacDB.end());
205 tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
208 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
210 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
211 _INFO("PackageID : %s", pkgId);
213 // Can be multiple apps in one package
214 if (tacPluginInstalled) {
215 _INFO("TAC plugin already installed");
218 tacPluginInstalled = true;
220 std::string appType = getAppType(std::string(pkgId));
221 if (strstr(appType.c_str(), "dotnet") == NULL) {
222 _ERR("App type is not dotnet");
225 std::string execName = getExecName(std::string(pkgId));
226 std::string rootPath = getRootPath(std::string(pkgId));
227 if (execName.empty() || rootPath.empty()) {
230 std::string binPath = concatPath(rootPath, "bin");
231 std::string metaValue = getMetadataValue(std::string(pkgId), TAC_METADATA_KEY);
232 if (metaValue.empty()) {
235 if (metaValue == METADATA_VALUE) {
236 checkDepsJson(rootPath, binPath, execName);
244 tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
246 _ERR("Sqlite create error. So restore the database.");
247 if (tac_restoreDB() != TAC_ERROR_NONE) {
248 _ERR("Sqlite create error");
251 tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
253 _ERR("Sqlite create error");
257 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
259 std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR);
260 if (!createDir(tac_dir)) {
261 _ERR("Cannot create directory: %s", tac_dir.c_str());
264 copySmackAndOwnership(binPath.c_str(), tac_dir.c_str());
266 for (auto& np : tacDB) {
267 std::string tac_name = np.substr(0, np.find('/'));
268 std::string tac_version = np.substr(np.rfind('/') + 1);
269 _INFO("TAC name : %s", tac_name.c_str());
270 _INFO("TAC version : %s", tac_version.c_str());
272 std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
273 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
274 if (!exist(tac_version_dir)) {
275 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
276 if (!createDir(tac_version_dir)) {
277 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
281 createDirectories.push_back(tac_version_dir);
282 if (!bf::is_symlink(sha256_info)) {
283 createSHA256Info(sha256_info, np);
285 _ERR("Failed to create sha256_info. Symbolic link is detected");
290 if (!exist(sha256_info)) {
295 if (copyAssemblyCreateSymlink(binPath, tac_dir, np, true)) {
296 _ERR("Failed to create symlink");
301 char *sql = sqlite3_mprintf(
302 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
303 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
304 if (!insertDB(tac_db, sql)) {
305 _ERR("Sqlite insert error");
312 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
313 if (!bf::is_symlink(sha256_info)) {
314 if (compareSHA256Info(sha256_info, np)) {
315 if (copyAssemblyCreateSymlink(binPath, tac_dir, np, false)) {
316 _ERR("Failed to create symlink");
321 char *sql = sqlite3_mprintf(
322 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
323 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
324 if (!insertDB(tac_db, sql)) {
325 _ERR("Sqlite insert error");
332 _INFO("Different nuget : %s", np.c_str());
336 _ERR("Failed to create sha256_info. Symbolic link is detected");
344 std::string tlcDir = concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR);
345 if (!createDir(tlcDir)) {
346 _ERR("Cannot create directory: %s", tlcDir.c_str());
349 copySmackAndOwnership(__DOTNET_DIR, tlcDir);
351 tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
353 _ERR("Sqlite create error. So restore the database.");
354 if (tlc_restoreDB() != TAC_ERROR_NONE) {
355 _ERR("Sqlite create error");
358 tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
360 _ERR("Sqlite create error");
364 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
366 copyLibraryCreateSymlink(pkgId, getLibrariesInfo(rootPath), tlcDir);
371 static int sqliteCb(void *count, int argc, char **argv, char **colName)
373 int *c = (int*)count;
378 static void tac_updateDB(sqlite3 *sqlite)
380 for (auto& unp : updateTac) {
382 char *sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
383 int ret = sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL);
384 if (ret != SQLITE_OK) {
385 _ERR("Sqlite select error");
390 std::string tac_version_dir_prev = concatPath(__DOTNET_DIR, unp);
391 std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
392 if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
393 _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
397 if (!removeAll(tac_version_dir_prev)) {
398 _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
407 static void tlc_updateDB(sqlite3 *sqlite, std::vector<std::string> updateTlc, std::string tlcDir)
409 for (auto& ulp : updateTlc) {
411 char *sql = sqlite3_mprintf("SELECT COUNT(LIBRARY) FROM TLC WHERE LIBRARY = %Q;", ulp.c_str());
412 int ret = sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL);
413 if (ret != SQLITE_OK) {
414 _ERR("Sqlite select error");
419 std::string library_prev = concatPath(tlcDir, ulp);
420 std::string library_backup = library_prev + ".bck";
421 if (!copyFile(library_prev, library_backup)) {
422 _ERR("Failed to copy of %s", library_prev.c_str());
426 if (!removeFile(library_prev)) {
427 _ERR("Failed to remove of %s", library_prev.c_str());
436 extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *appId, GList *list)
438 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
439 _INFO("PackageID : %s", pkgId);
441 // Can be multiple apps in one package
442 if (tacPluginInstalled) {
443 _INFO("TAC plugin already upgraded");
446 tacPluginInstalled = true;
448 std::string appType = getAppType(std::string(pkgId));
449 if (strstr(appType.c_str(), "dotnet") == NULL) {
450 _ERR("App type is not dotnet");
453 std::string execName = getExecName(std::string(pkgId));
454 std::string rootPath = getRootPath(std::string(pkgId));
455 if (execName.empty() || rootPath.empty()) {
458 std::string binPath = concatPath(rootPath, "bin");
460 if (!strcmp("removed", status.c_str())) {
461 _INFO("Skipped to parse of deps.json");
463 std::string metaValue = getMetadataValue(std::string(pkgId), TAC_METADATA_KEY);
464 if (metaValue.empty()) {
467 if (metaValue == METADATA_VALUE) {
468 checkDepsJson(rootPath, binPath, execName);
473 tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
475 _ERR("Sqlite create error. So restore the database.");
476 if (tac_restoreDB() != TAC_ERROR_NONE) {
477 _ERR("Sqlite create error");
480 tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
482 _ERR("Sqlite create error");
486 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
488 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
489 updateTac = selectDB(tac_db, sql);
492 bool skipTLC = false;
494 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
495 if (!deleteDB(tac_db, sql)) {
496 _ERR("Sqlite delete error");
502 tac_updateDB(tac_db);
506 std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR);
507 if (!createDir(tac_dir)) {
508 _ERR("Cannot create directory: %s", tac_dir.c_str());
511 copySmackAndOwnership(binPath.c_str(), tac_dir.c_str());
513 for (auto& np : tacDB) {
514 std::string tac_name = np.substr(0, np.find('/'));
515 std::string tac_version = np.substr(np.rfind('/') + 1);
516 _INFO("TAC name : %s", tac_name.c_str());
517 _INFO("TAC version : %s", tac_version.c_str());
519 std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
520 std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
521 if (!exist(tac_version_dir)) {
522 _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
523 if (!createDir(tac_version_dir)) {
524 _ERR("Cannot create directory: %s", tac_version_dir.c_str());
528 createDirectories.push_back(tac_version_dir);
529 if (!bf::is_symlink(sha256_info)) {
530 createSHA256Info(sha256_info, np);
532 _ERR("Failed to create sha256_info. Symbolic link is detected");
537 if (!exist(sha256_info)) {
542 if (copyAssemblyCreateSymlink(binPath, tac_dir, np, true)) {
543 _ERR("Failed to create symlink");
549 sql = sqlite3_mprintf(
550 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
551 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
552 if (ret != SQLITE_OK) {
553 _ERR("Sqlite select error");
560 sql = sqlite3_mprintf(
561 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
562 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
563 if (!updateDB(tac_db, sql)) {
564 _ERR("Sqlite update error");
570 } else if (count == 0) {
571 sql = sqlite3_mprintf(
572 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
573 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
574 if (!insertDB(tac_db, sql)) {
575 _ERR("Sqlite insert error");
583 _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
584 if (!bf::is_symlink(sha256_info)) {
585 if (compareSHA256Info(sha256_info, np)) {
586 if (copyAssemblyCreateSymlink(binPath, tac_dir, np, false)) {
587 _ERR("Failed to create symlink");
593 char *sql = sqlite3_mprintf(
594 "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, tac_name.c_str());
595 int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL);
596 if (ret != SQLITE_OK) {
597 _ERR("Sqlite select error");
604 sql = sqlite3_mprintf(
605 "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;",
606 tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId, tac_name.c_str());
607 if (!updateDB(tac_db, sql)) {
608 _ERR("Sqlite update error");
614 } else if (count == 0) {
615 sql = sqlite3_mprintf(
616 "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
617 "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
618 if (!insertDB(tac_db, sql)) {
619 _ERR("Sqlite insert error");
627 _INFO("Different nuget : %s", np.c_str());
631 _ERR("Failed to create sha256_info. Symbolic link is detected");
637 for (auto& unp : updateTac) {
638 bool isExits = false;
639 for (auto& np : tacDB) {
640 if (!strcmp(unp.c_str(), np.c_str())) {
646 char *sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId, unp.c_str());
647 if (!deleteDB(tac_db, sql)) {
648 _ERR("Sqlite delete error");
656 tac_updateDB(tac_db);
660 std::string tlcDir = concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR);
661 if (!createDir(tlcDir)) {
662 _ERR("Cannot create directory: %s", tlcDir.c_str());
665 copySmackAndOwnership(__DOTNET_DIR, tlcDir);
667 tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
669 _ERR("Sqlite create error. So restore the database.");
670 if (tlc_restoreDB() != TAC_ERROR_NONE) {
671 _ERR("Sqlite create error");
674 tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
676 _ERR("Sqlite create error");
680 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
682 sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId);
683 std::vector<std::string> updateTlc = selectDB(tlc_db, sql);
686 sql = sqlite3_mprintf("DELETE FROM TLC WHERE PKGID = %Q;", pkgId);
687 if (!deleteDB(tlc_db, sql)) {
688 _ERR("Sqlite delete error");
694 std::vector<std::string> librariesInfo;
696 librariesInfo = getLibrariesInfo(rootPath);
699 copyLibraryCreateSymlink(pkgId, librariesInfo, tlcDir);
701 tlc_updateDB(tlc_db, updateTlc, tlcDir);
706 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *appId, GList *list)
708 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
709 _INFO("PackageID : %s", pkgId);
711 // Can be multiple apps in one package
712 if (tacPluginInstalled) {
713 _INFO("TAC plugin already uninstalled");
716 tacPluginInstalled = true;
718 status = "uninstall";
719 tac_db = openDB(TAC_APP_LIST_DB);
721 _ERR("Sqlite open error. So restore the database.");
722 if (tac_restoreDB() != TAC_ERROR_NONE) {
723 _ERR("Sqlite open error");
726 tac_db = openDB(TAC_APP_LIST_DB);
728 _ERR("Sqlite open error");
732 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
734 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
735 updateTac = selectDB(tac_db, sql);
738 sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
740 if (!deleteDB(tac_db, sql)) {
741 _ERR("Sqlite delete error");
748 tac_updateDB(tac_db);
751 tlc_db = openDB(TLC_APP_LIST_DB);
753 _ERR("Sqlite open error. So restore the database.");
754 if (tlc_restoreDB() != TAC_ERROR_NONE) {
755 _ERR("Sqlite open error");
758 tlc_db = openDB(TLC_APP_LIST_DB);
760 _ERR("Sqlite open error");
764 sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
766 sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId);
767 std::vector<std::string> updateTlc = selectDB(tlc_db, sql);
770 sql = sqlite3_mprintf("DELETE FROM TLC WHERE PKGID = %Q;", pkgId);
771 if (!deleteDB(tlc_db, sql)) {
772 _ERR("Sqlite delete error");
778 tlc_updateDB(tlc_db, updateTlc, concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR));
783 extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *appId, GList *list)
785 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_REMOVED =====]");
786 _INFO("PackageID : %s", pkgId);
790 return PKGMGR_MDPARSER_PLUGIN_UPGRADE(pkgId, appId, list);
793 void undoStep(std::string tac)
795 std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
797 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
798 std::string bck_path = bck.path().string();
799 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
800 if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) {
801 _ERR("Failed to move %s", bck_path.c_str());
806 } catch (const bf::filesystem_error& error) {
807 _ERR("Failed to recursive directory: %s", error.what());
812 auto convert = [](const std::string& path, const std::string& filename) {
813 if (filename.rfind(".bck") != std::string::npos) {
814 if (!moveFile(path, replaceAll(path, ".bck", ""))) {
815 _ERR("Failed to move %s", path.c_str());
820 scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
825 for (auto& cd : createDirectories) {
826 if (!removeAll(cd)) {
827 _ERR("Failed to remove of %s", cd.c_str());
831 for (auto& cl : createLibraries) {
832 if (!removeFile(cl)) {
833 _ERR("Failed to remove of %s", cl.c_str());
838 void unInstall_Undo()
840 for (auto& unp : updateTac) {
848 if (!tacDB.empty()) {
849 for (auto& np : tacDB) {
856 extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId, GList *list)
858 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
859 _INFO("PackageID : %s", pkgId);
861 // Can be multiple apps in one package
862 if (tacPluginFinished) {
863 _INFO("TAC plugin already finished(UNDO)");
866 tacPluginFinished = true;
868 if (!strcmp("install", status.c_str())) {
870 } else if (!strcmp("update", status.c_str())) {
872 } else if (!strcmp("uninstall", status.c_str())) {
874 } else if (!strcmp("restore", status.c_str())) {
891 void changeOwnershipTAC(std::string current_tac)
893 copySmackAndOwnership(__DOTNET_DIR, current_tac);
895 for (auto& path : bf::recursive_directory_iterator(current_tac))
896 copySmackAndOwnership(__DOTNET_DIR, path.path().string());
897 } catch (const bf::filesystem_error& error) {
898 _ERR("Failed to recursive directory: %s", error.what());
902 void cleanStep(std::string tac)
904 std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
906 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
907 std::string bck_path = bck.path().string();
908 if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) {
909 if (!removeAll(bck_path)) {
910 _ERR("Failed to remove of %s", bck_path.c_str());
916 bool isExist = false;
917 for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
918 std::string bck_path = bck.path().string();
919 if (exist(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) {
925 if (!removeAll(current_tac)) {
926 _ERR("Failed to remove of %s", current_tac.c_str());
929 } catch (const bf::filesystem_error& error) {
930 _ERR("Failed to recursive directory: %s", error.what());
935 auto convert = [](const std::string& path, const std::string& filename) {
936 if (filename.rfind(".bck") != std::string::npos) {
937 if (!removeFile(path)) {
938 _ERR("Failed to remove of %s", path.c_str());
943 scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
948 for (auto& cd : createDirectories) {
949 changeOwnershipTAC(cd);
952 for (auto& cl : createLibraries) {
953 changeOwnershipTAC(cl);
957 void unInstall_Clean()
959 for (auto& unp : updateTac) {
967 if (!tacDB.empty()) {
968 for (auto& np : tacDB) {
970 changeOwnershipTAC(concatPath(__DOTNET_DIR, np.substr(0, np.find('/'))));
976 extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId, GList *list)
978 if (!strcmp("restore", status.c_str())) {
979 disableTACPackage(pkgId);
981 std::string rootPath = getRootPath(std::string(pkgId));
982 if (!rootPath.empty()) {
983 std::string binPath = concatPath(rootPath, "bin");
984 removeAll(concatPath(binPath, TAC_SYMLINK_SUB_DIR));
987 std::string runtimesDir = concatPath(rootPath, "bin/runtimes");
988 if (exist(runtimesDir)) {
990 sprintf(buffer, "(tizen|linux|unix|base|any)(.\\d.\\d.\\d)?(-%s)?", ARCHITECTURE_IDENTIFIER);
991 std::regex pattern(buffer);
994 for (auto& path : bf::recursive_directory_iterator(runtimesDir)) {
995 std::string symPath = path.path().string();
996 if (isDirectory(symPath) || !bf::is_symlink(symPath))
998 std::string targetDir = symPath.substr(symPath.rfind("/runtimes/") + 10);
999 if (!std::regex_match(targetDir.substr(0, targetDir.find('/')), pattern))
1001 if (symPath.rfind(".so") == std::string::npos)
1003 copyFile(bf::read_symlink(symPath).string(), symPath);
1005 } catch (const bf::filesystem_error& error) {
1006 _ERR("Failed to recursive directory: %s", error.what());
1010 return PKGMGR_MDPARSER_PLUGIN_UNDO(pkgId, appId, list);
1013 _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
1014 _INFO("PackageID : %s", pkgId);
1016 // Can be multiple apps in one package
1017 if (tacPluginFinished) {
1018 _INFO("TAC plugin already finished(CLEAN)");
1021 tacPluginFinished = true;
1023 if (!strcmp("install", status.c_str())) {
1025 } else if (!strcmp("update", status.c_str())) {
1027 } else if (!strcmp("uninstall", status.c_str())) {
1034 copySmackAndOwnership(__DOTNET_DIR, TAC_APP_LIST_DB);
1035 copySmackAndOwnership(__DOTNET_DIR, TAC_APP_LIST_DB + std::string("-journal"));
1041 copySmackAndOwnership(__DOTNET_DIR, TLC_APP_LIST_DB);
1042 copySmackAndOwnership(__DOTNET_DIR, TLC_APP_LIST_DB + std::string("-journal"));