From: j-h.choi Date: Wed, 14 Feb 2024 07:09:54 +0000 (+0900) Subject: Refactoring to extract duplicate logic as a separate method X-Git-Tag: accepted/tizen/unified/20240220.115648~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b71ac0934161077f3471f188947ebe9980ad75cf;hp=291f923faa896e766655c9913775d50188380b99;p=platform%2Fcore%2Fdotnet%2Flauncher.git Refactoring to extract duplicate logic as a separate method Change-Id: I765201c0249f6415b6c1e3654888ae0567af5889 --- diff --git a/NativeLauncher/CMakeLists.txt b/NativeLauncher/CMakeLists.txt index d89bba3..8dd5f33 100644 --- a/NativeLauncher/CMakeLists.txt +++ b/NativeLauncher/CMakeLists.txt @@ -185,6 +185,7 @@ SET(TAC_COMMON "tac_common") SET(${TAC_COMMON}_SOURCE_FILES tool/tac_common.cc tool/tac_installer.cc + tool/tac_db.cc util/db_manager.cc ) ADD_LIBRARY(${TAC_COMMON} SHARED ${${TAC_COMMON}_SOURCE_FILES}) @@ -300,6 +301,7 @@ INSTALL(FILES inc/dotnet_launcher_plugin.h DESTINATION ${INCLUDEDIR}) INSTALL(FILES inc/ni_common.h DESTINATION ${INCLUDEDIR}) INSTALL(FILES inc/tac_common.h DESTINATION ${INCLUDEDIR}) INSTALL(FILES inc/tac_installer.h DESTINATION ${INCLUDEDIR}) +INSTALL(FILES inc/tac_db.h DESTINATION ${INCLUDEDIR}) INSTALL(FILES inc/profile_common.h DESTINATION ${INCLUDEDIR}) INSTALL(FILES inc/privilege_common.h DESTINATION ${INCLUDEDIR}) INSTALL(FILES ../dotnet-launcher.pc DESTINATION ${LIBDIR}/pkgconfig) diff --git a/NativeLauncher/inc/launcher_env.h b/NativeLauncher/inc/launcher_env.h index df832c7..6b24f87 100644 --- a/NativeLauncher/inc/launcher_env.h +++ b/NativeLauncher/inc/launcher_env.h @@ -31,7 +31,7 @@ #define TAC_APP_LIST_RESTORE_DB "/opt/usr/dotnet/.TAC.App.list.restore.db" #define TLC_APP_LIST_DB "/opt/usr/dotnet/.TLC.App.list.db" #define TLC_APP_LIST_RESTORE_DB "/opt/usr/dotnet/.TLC.App.list.restore.db" -#define TLC_LIBRARIES_DIR "Libraries" +#define TLC_LIBRARIES_DIR "/opt/usr/dotnet/Libraries" #define TIZEN_DOTNET_NUGET "Tizen.NET" #define TIZEN_DOTNET_SDK_NUGET "Tizen.NET.Sdk" #define NET_STANDARD_LIBRARY_NUGET "NETStandard.Library" diff --git a/NativeLauncher/inc/tac_db.h b/NativeLauncher/inc/tac_db.h new file mode 100644 index 0000000..896f20d --- /dev/null +++ b/NativeLauncher/inc/tac_db.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __TAC_DB_H__ +#define __TAC_DB_H__ + +#include +#include + +int sqliteCb(void *count, int argc, char **argv, char **colName); + + +/* TAC related DB functions */ +int tac_createDB(); + +int tac_openDB(); + +int tac_insertDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version); + +int tac_countDB(const std::string& pkgId, const std::string& tac_name, const std::string& unp, int& count); + +std::vector tac_selectDB(const std::string& pkgId); + +int tac_updateDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version); + +int tac_deleteDB(const std::string& pkgId, const std::string& unp); + +void tac_rollbackDB(); + +bool tac_closeDB(); + + +/* TAC related DB functions */ +int tlc_createDB(); + +int tlc_openDB(); + +int tlc_insertDB(const std::string& pkgId, const std::string& fileSha); + +int tlc_countDB(const std::string& pkgId, const std::string& ulp, int& count); + +std::vector tlc_selectDB(const std::string& pkgId); + +int tlc_deleteDB(const std::string& pkgId); + +void tlc_rollbackDB(); + +bool tlc_closeDB(); + +#endif /* __TAC_DB_H__ */ diff --git a/NativeLauncher/tool/tac_common.cc b/NativeLauncher/tool/tac_common.cc index 401cf8b..03ff5a8 100644 --- a/NativeLauncher/tool/tac_common.cc +++ b/NativeLauncher/tool/tac_common.cc @@ -206,6 +206,25 @@ tac_error_e tac_restoreDB() return TAC_ERROR_NONE; } +static tac_error_e createSymlinkFile(const std::string& tacDir, const std::string& binDir, const std::string& from, const std::string& to) +{ + bs::error_code error; + std::string symlinkFile = concatPath(tacDir, to); + bf::create_symlink(from, symlinkFile, error); + if (error) { + _SERR("Failed to create symlink %s file", symlinkFile.c_str()); + return TAC_ERROR_UNKNOWN; + } + _SOUT("%s symbolic link file generated successfully.", symlinkFile.c_str()); + copySmackAndOwnership(tacDir.c_str(), symlinkFile.c_str(), true); + + if (!removeFile(concatPath(binDir, to))) { + _SERR("Failed to remove of %s", concatPath(binDir, to).c_str()); + return TAC_ERROR_UNKNOWN; + } + return TAC_ERROR_NONE; +} + tac_error_e disableTACPackage(const std::string& pkgId) { std::string rootPath = getRootPath(pkgId); @@ -279,76 +298,61 @@ tac_error_e enableTACPackage(const std::string& pkgId) return TAC_ERROR_INVALID_PACKAGE; } - if (!strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) { - std::string binDir = concatPath(rootPath, "bin"); - std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR); - std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR); - if (!exist(tacDir)) { - if (!createDir(tacDir)) { - _SERR("Cannot create directory: %s", tacDir.c_str()); - return TAC_ERROR_UNKNOWN; - } - copySmackAndOwnership(binDir.c_str(), tacDir.c_str()); - - std::vector enableNuget; - for (auto& npAssembly : depsJsonParser(rootPath, execName)) { - std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':')); - std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1); - std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage); - if (exist(nugetPath)) { - std::string originPath = concatPath(nugetPath, assemblyName); - if (exist(originPath)) { - enableNuget.push_back(originPath); - } - } - } + if (strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) { + _INFO("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str()); + return TAC_ERROR_NONE; + } - bs::error_code error; - for (auto& originPath : enableNuget) { - if (exist(originPath)) { - std::string fileName = originPath.substr(originPath.rfind('/') + 1); - std::string NIFileName = changeExtension(fileName, "dll", "ni.dll"); - if (exist(binNIDir)) { - std::string originNIPath = changeExtension(originPath, "dll", "ni.dll"); - if (exist(originNIPath)) { - bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName), error); - if (error) { - _SERR("Failed to create symlink %s file", concatPath(tacDir, NIFileName).c_str()); - return TAC_ERROR_UNKNOWN; - } - _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, NIFileName).c_str()); - copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true); + std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR); + std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR); + if (exist(tacDir)) { + return TAC_ERROR_NONE; + } - if (!removeFile(concatPath(binNIDir, NIFileName))) { - _SERR("Failed to remove of %s", concatPath(binNIDir, NIFileName).c_str()); - return TAC_ERROR_UNKNOWN; - } - } - } - bf::create_symlink(originPath, concatPath(tacDir, fileName), error); - if (error) { - _SERR("Failed to create symlink %s file", concatPath(tacDir, fileName).c_str()); - return TAC_ERROR_UNKNOWN; - } - _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, fileName).c_str()); - copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true); + if (!createDir(tacDir)) { + _SERR("Cannot create directory: %s", tacDir.c_str()); + return TAC_ERROR_UNKNOWN; + } + copySmackAndOwnership(binDir.c_str(), tacDir.c_str()); - if (!removeFile(concatPath(binDir, fileName))) { - _SERR("Failed to remove of %s", concatPath(binDir, fileName).c_str()); + std::vector enableNuget; + for (auto& npAssembly : depsJsonParser(rootPath, execName)) { + std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':')); + std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1); + std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage); + if (exist(nugetPath)) { + std::string originPath = concatPath(nugetPath, assemblyName); + if (exist(originPath)) { + enableNuget.push_back(originPath); + } + } + } + + if (enableNuget.empty()) { + if (!removeAll(tacDir)) { + _SERR("Failed to remove of %s", tacDir.c_str()); + } + return TAC_ERROR_NONE; + } + + for (auto& originPath : enableNuget) { + if (exist(originPath)) { + std::string fileName = originPath.substr(originPath.rfind('/') + 1); + if (exist(binNIDir)) { + std::string originNIPath = changeExtension(originPath, "dll", "ni.dll"); + if (exist(originNIPath)) { + if (createSymlinkFile(tacDir, binNIDir, originNIPath, changeExtension(fileName, "dll", "ni.dll")) != TAC_ERROR_NONE) { return TAC_ERROR_UNKNOWN; } } } - if (enableNuget.empty()) { - if (!removeAll(tacDir)) { - _SERR("Failed to remove of %s", tacDir.c_str()); - } + if (createSymlinkFile(tacDir, binDir, originPath, fileName) != TAC_ERROR_NONE) { + return TAC_ERROR_UNKNOWN; } - enableNuget.clear(); } - } else { - _SERR("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str()); } + enableNuget.clear(); + return TAC_ERROR_NONE; } @@ -529,7 +533,7 @@ tac_error_e tlc_restoreDB() } }; - scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0); + scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0); return TAC_ERROR_NONE; } diff --git a/NativeLauncher/tool/tac_db.cc b/NativeLauncher/tool/tac_db.cc new file mode 100644 index 0000000..3484b5f --- /dev/null +++ b/NativeLauncher/tool/tac_db.cc @@ -0,0 +1,275 @@ +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "log.h" +#include "utils.h" +#include "db_manager.h" +#include "tac_common.h" +#include "tac_db.h" + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "DOTNET_INSTALLER_PLUGIN" + +#define __XSTR(x) #x +#define __STR(x) __XSTR(x) +static const char* __DOTNET_DIR = __STR(DOTNET_DIR); +#undef __STR +#undef __XSTR + +static sqlite3 *tac_db = NULL; +static sqlite3 *tlc_db = NULL; + +// callback function of "sqlite3_exec" +int sqliteCb(void *count, int argc, char **argv, char **colName) +{ + int *c = (int*)count; + *c = atoi(argv[0]); + return 0; +} + +int tac_createDB() +{ + tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE); + if (!tac_db) { + _ERR("Sqlite create error. So restore the database."); + if (tac_restoreDB() != TAC_ERROR_NONE) { + _ERR("Sqlite create error"); + return -1; + } + tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE); + if (!tac_db) { + _ERR("Sqlite create error"); + return -1; + } + } + sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL); + return 0; +} + +int tac_openDB() +{ + tac_db = openDB(TAC_APP_LIST_DB); + if (!tac_db) { + _ERR("Sqlite open error. So restore the database."); + if (tac_restoreDB() != TAC_ERROR_NONE) { + _ERR("Sqlite open error"); + return -1; + } + tac_db = openDB(TAC_APP_LIST_DB); + if (!tac_db) { + _ERR("Sqlite open error"); + return -1; + } + } + sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL); + return 0; +} + +int tac_insertDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version) +{ + char *sql = sqlite3_mprintf( + "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \ + "VALUES (%Q, %Q, %Q, %Q);", pkgId.c_str(), np.c_str(), tac_name.c_str(), tac_version.c_str()); + if (!insertDB(tac_db, sql)) { + _ERR("Sqlite insert error"); + sqlite3_free(sql); + return -1; + } + sqlite3_free(sql); + return 0; +} + +int tac_countDB(const std::string& pkgId, const std::string& tac_name, const std::string& unp, int& count) +{ + char *sql = NULL; + if (unp == "") { + sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId.c_str(), tac_name.c_str()); + } else { + sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str()); + } + if (sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) { + _ERR("Sqlite select error"); + sqlite3_free(sql); + return -1; + } + sqlite3_free(sql); + return 0; +} + +std::vector tac_selectDB(const std::string& pkgId) +{ + char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str()); + std::vector update_tac = selectDB(tac_db, sql); + sqlite3_free(sql); + return update_tac; +} + +int tac_updateDB(const std::string& pkgId, const std::string& np, const std::string& tac_name, const std::string& tac_version) +{ + char *sql = sqlite3_mprintf( + "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;", + tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId.c_str(), tac_name.c_str()); + if (!updateDB(tac_db, sql)) { + _ERR("Sqlite update error"); + sqlite3_free(sql); + return -1; + } + sqlite3_free(sql); + return 0; +} + +int tac_deleteDB(const std::string& pkgId, const std::string& unp) +{ + char *sql = NULL; + if (unp == "") { + sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId.c_str()); + } else { + sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId.c_str(), unp.c_str()); + } + if (!deleteDB(tac_db, sql)) { + _ERR("Sqlite delete error"); + sqlite3_free(sql); + return -1; + } + sqlite3_free(sql); + return 0; +} + +void tac_rollbackDB() +{ + if (tac_db) { + rollbackDB(tac_db); + tac_db = NULL; + } +} + +bool tac_closeDB() +{ + if (tac_db) { + closeDB(tac_db); + tac_db = NULL; + return true; + } + return false; +} + +int tlc_createDB() +{ + if (!createDir(TLC_LIBRARIES_DIR)) { + _ERR("Cannot create directory: %s", TLC_LIBRARIES_DIR); + return -1; + } + copySmackAndOwnership(__DOTNET_DIR, TLC_LIBRARIES_DIR); + + tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE); + if (!tlc_db) { + _ERR("Sqlite create error. So restore the database."); + if (tlc_restoreDB() != TAC_ERROR_NONE) { + _ERR("Sqlite create error"); + return -1; + } + tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE); + if (!tlc_db) { + _ERR("Sqlite create error"); + return -1; + } + } + sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL); + return 0; +} + +int tlc_openDB() +{ + tlc_db = openDB(TLC_APP_LIST_DB); + if (!tlc_db) { + _ERR("Sqlite open error. So restore the database."); + if (tlc_restoreDB() != TAC_ERROR_NONE) { + _ERR("Sqlite open error"); + return 0; + } + tlc_db = openDB(TLC_APP_LIST_DB); + if (!tlc_db) { + _ERR("Sqlite open error"); + return 0; + } + } + sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL); + return 0; +} + +int tlc_insertDB(const std::string& pkgId, const std::string& fileSha) +{ + char *sql = sqlite3_mprintf("INSERT INTO TLC (PKGID, LIBRARY) VALUES (%Q, %Q);", pkgId.c_str(), fileSha.c_str()); + if (!insertDB(tlc_db, sql)) { + _ERR("Sqlite insert error"); + sqlite3_free(sql); + return -1; + } + sqlite3_free(sql); + return 0; +} + +int tlc_countDB(const std::string& pkgId, const std::string& ulp, int& count) +{ + char *sql = sqlite3_mprintf("SELECT COUNT(LIBRARY) FROM TLC WHERE LIBRARY = %Q;", ulp.c_str()); + if (sqlite3_exec(tlc_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) { + _ERR("Sqlite select error"); + sqlite3_free(sql); + return -1; + } + sqlite3_free(sql); + return 0; +} + +std::vector tlc_selectDB(const std::string& pkgId) +{ + char *sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str()); + std::vector update_tlc = selectDB(tlc_db, sql); + sqlite3_free(sql); + return update_tlc; +} + +int tlc_deleteDB(const std::string& pkgId) +{ + char *sql = sqlite3_mprintf("DELETE FROM TLC WHERE PKGID = %Q;", pkgId.c_str()); + if (!deleteDB(tlc_db, sql)) { + _ERR("Sqlite delete error"); + sqlite3_free(sql); + return -1; + } + sqlite3_free(sql); + return 0; +} + +void tlc_rollbackDB() +{ + if (tlc_db) { + rollbackDB(tlc_db); + tlc_db = NULL; + } +} + +bool tlc_closeDB() +{ + if (tlc_db) { + closeDB(tlc_db); + tlc_db = NULL; + return true; + } + return false; +} diff --git a/NativeLauncher/tool/tac_installer.cc b/NativeLauncher/tool/tac_installer.cc index 6bf9b57..9644e08 100644 --- a/NativeLauncher/tool/tac_installer.cc +++ b/NativeLauncher/tool/tac_installer.cc @@ -19,6 +19,7 @@ #include "db_manager.h" #include "tac_common.h" #include "tac_installer.h" +#include "tac_db.h" #include #include @@ -45,9 +46,8 @@ static std::vector tacDB; static std::vector createDirectories; static std::vector createLibraries; static std::vector updateTac; +static std::vector updateTlc; static tac_state tacState = TAC_STATE_NONE; -static sqlite3 *tac_db = NULL; -static sqlite3 *tlc_db = NULL; static bool tacPluginInstalled = false; static bool tacPluginFinished = false; @@ -168,7 +168,7 @@ static bool copyAssemblyCreateSymlink(std::string binPath, std::string tacDir, s return nuget_restoration; } -static void copyLibraryCreateSymlink(const std::string pkgId, std::vector LibrariesInfo, std::string tlcDir) +static void copyLibraryCreateSymlink(const std::string pkgId, std::vector LibrariesInfo) { if (LibrariesInfo.empty()) { return; @@ -178,47 +178,44 @@ static void copyLibraryCreateSymlink(const std::string pkgId, std::vector updateTlc, std::string tlcDir) +void tlcUpdateDB(const std::string& pkgId) { for (auto& ulp : updateTlc) { int count = -1; - char *sql = sqlite3_mprintf("SELECT COUNT(LIBRARY) FROM TLC WHERE LIBRARY = %Q;", ulp.c_str()); - int ret = sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL); - if (ret != SQLITE_OK) { - _ERR("Sqlite select error"); - sqlite3_free(sql); + if (tlc_countDB(pkgId, ulp, count) != 0) { continue; } + if (count == 0) { - std::string library_prev = concatPath(tlcDir, ulp); + std::string library_prev = concatPath(TLC_LIBRARIES_DIR, ulp); std::string library_backup = library_prev + ".bck"; if (!copyFile(library_prev, library_backup)) { _ERR("Failed to copy of %s", library_prev.c_str()); - sqlite3_free(sql); continue; } if (!removeFile(library_prev)) { _ERR("Failed to remove of %s", library_prev.c_str()); - sqlite3_free(sql); continue; } } - sqlite3_free(sql); } } +int tacInstall(const std::string& pkgId, tac_state state, bool tacForce) +{ + _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]"); + _INFO("PackageID : %s", pkgId.c_str()); + + // Can be multiple apps in one package + if (tacPluginInstalled) { + _INFO("TAC plugin already installed"); + return 0; + } + tacPluginInstalled = true; + + std::string appType = getAppType(pkgId); + if (strstr(appType.c_str(), "dotnet") == NULL) { + _ERR("App type is not dotnet"); + return 0; + } + std::string execName = getExecName(pkgId); + std::string rootPath = getRootPath(pkgId); + if (execName.empty() || rootPath.empty()) { + return 0; + } + + std::string binPath = concatPath(rootPath, "bin"); + if (exist(concatPath(binPath, PRE_COMPILED_PACKAGE_FILE))) { + _INFO("The %s is a Pre-Compiled package. So, skip the TAC", pkgId.c_str()); + return 0; + } + + std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY); + if (!tacForce) { + if (metaValue.empty()) { + return 0; + } + } + if (metaValue == METADATA_VALUE_TRUE || tacForce) { + checkDepsJson(rootPath, binPath, execName); + } + + tacState = state; + if (tacDB.empty()) { + return 0; + } + + if (tac_createDB() != 0) { + return -1; + } + + if (generateTAC(pkgId, binPath) != 0) { + return -1; + } + + ///// TLC ///// + if (tlc_createDB() != 0) { + return -1; + } + + copyLibraryCreateSymlink(pkgId, getLibrariesInfo(rootPath)); + + return 0; +} + int tacUpgrade(const std::string& pkgId, tac_state state, bool tacForce) { _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]"); @@ -517,171 +479,25 @@ int tacUpgrade(const std::string& pkgId, tac_state state, bool tacForce) } tacState = TAC_STATE_UPGRADE; - tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE); - if (!tac_db) { - _ERR("Sqlite create error. So restore the database."); - if (tac_restoreDB() != TAC_ERROR_NONE) { - _ERR("Sqlite create error"); - return -1; - } - tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE); - if (!tac_db) { - _ERR("Sqlite create error"); - return -1; - } + if (tac_createDB() != 0) { + return -1; } - sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL); - char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str()); - updateTac = selectDB(tac_db, sql); - sqlite3_free(sql); + updateTac = tac_selectDB(pkgId); bool skipTLC = false; if (tacDB.empty()) { - sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId.c_str()); - if (!deleteDB(tac_db, sql)) { - _ERR("Sqlite delete error"); - sqlite3_free(sql); + if (tac_deleteDB(pkgId, "") != 0) { return -1; } - sqlite3_free(sql); - - tac_updateDB(tac_db); + tacUpdateDB(pkgId); skipTLC = true; } else { - std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR); - if (!createDir(tac_dir)) { - _ERR("Cannot create directory: %s", tac_dir.c_str()); - return 0; + if (generateTAC(pkgId, binPath) != 0) { + return -1; } - copySmackAndOwnership(binPath.c_str(), tac_dir.c_str()); - - for (auto& np : tacDB) { - std::string tac_name = np.substr(0, np.find('/')); - std::string tac_version = np.substr(np.rfind('/') + 1); - _INFO("TAC name : %s", tac_name.c_str()); - _INFO("TAC version : %s", tac_version.c_str()); - - bs::error_code error; - std::string tac_version_dir = concatPath(__DOTNET_DIR, np); - std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO); - if (!exist(tac_version_dir)) { - _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str()); - if (!createDir(tac_version_dir)) { - _ERR("Cannot create directory: %s", tac_version_dir.c_str()); - tacState = TAC_STATE_RESTORE; - return -1; - } - createDirectories.push_back(tac_version_dir); - if (!isSymlinkFile(sha256_info)) { - createSHA256Info(sha256_info, np); - } else { - _ERR("Failed to create sha256_info. Symbolic link is detected"); - tacState = TAC_STATE_RESTORE; - return -1; - } - - if (!exist(sha256_info)) { - tacState = TAC_STATE_RESTORE; - return -1; - } - - if (copyAssemblyCreateSymlink(binPath, tac_dir, np, true)) { - _ERR("Failed to create symlink"); - tacState = TAC_STATE_RESTORE; - return -1; - } - int count = -1; - sql = sqlite3_mprintf( - "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId.c_str(), tac_name.c_str()); - int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL); - if (ret != SQLITE_OK) { - _ERR("Sqlite select error"); - sqlite3_free(sql); - tacState = TAC_STATE_RESTORE; - return -1; - } - sqlite3_free(sql); - if (count == 1) { - sql = sqlite3_mprintf( - "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;", - tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId.c_str(), tac_name.c_str()); - if (!updateDB(tac_db, sql)) { - _ERR("Sqlite update error"); - sqlite3_free(sql); - tacState = TAC_STATE_RESTORE; - return -1; - } - sqlite3_free(sql); - } else if (count == 0) { - sql = sqlite3_mprintf( - "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \ - "VALUES (%Q, %Q, %Q, %Q);", pkgId.c_str(), np.c_str(), tac_name.c_str(), tac_version.c_str()); - if (!insertDB(tac_db, sql)) { - _ERR("Sqlite insert error"); - sqlite3_free(sql); - tacState = TAC_STATE_RESTORE; - return -1; - } - sqlite3_free(sql); - } - } else { - _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str()); - if (!isSymlinkFile(sha256_info)) { - if (compareSHA256Info(sha256_info, np)) { - if (copyAssemblyCreateSymlink(binPath, tac_dir, np, false)) { - _ERR("Failed to create symlink"); - tacState = TAC_STATE_RESTORE; - return -1; - } - - int count = -1; - char *sql = sqlite3_mprintf( - "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId.c_str(), tac_name.c_str()); - int ret = sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL); - if (ret != SQLITE_OK) { - _ERR("Sqlite select error"); - sqlite3_free(sql); - tacState = TAC_STATE_RESTORE; - return -1; - } - sqlite3_free(sql); - if (count == 1) { - sql = sqlite3_mprintf( - "UPDATE TAC SET NAME = %Q, VERSION = %Q, NUGET = %Q WHERE PKGID = %Q AND NAME = %Q;", - tac_name.c_str(), tac_version.c_str(), np.c_str(), pkgId.c_str(), tac_name.c_str()); - if (!updateDB(tac_db, sql)) { - _ERR("Sqlite update error"); - sqlite3_free(sql); - tacState = TAC_STATE_RESTORE; - return -1; - } - sqlite3_free(sql); - } else if (count == 0) { - sql = sqlite3_mprintf( - "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \ - "VALUES (%Q, %Q, %Q, %Q);", pkgId.c_str(), np.c_str(), tac_name.c_str(), tac_version.c_str()); - if (!insertDB(tac_db, sql)) { - _ERR("Sqlite insert error"); - sqlite3_free(sql); - tacState = TAC_STATE_RESTORE; - return -1; - } - sqlite3_free(sql); - } - } else { - _INFO("Different nuget : %s", np.c_str()); - continue; - } - } else { - _ERR("Failed to create sha256_info. Symbolic link is detected"); - tacState = TAC_STATE_RESTORE; - return -1; - } - } - } for (auto& unp : updateTac) { bool isExits = false; for (auto& np : tacDB) { @@ -691,62 +507,29 @@ int tacUpgrade(const std::string& pkgId, tac_state state, bool tacForce) } } if (!isExits) { - char *sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId.c_str(), unp.c_str()); - if (!deleteDB(tac_db, sql)) { - _ERR("Sqlite delete error"); - sqlite3_free(sql); + if (tac_deleteDB(pkgId, unp) != 0) { tacState = TAC_STATE_RESTORE; return -1; } - sqlite3_free(sql); } } - tac_updateDB(tac_db); + tacUpdateDB(pkgId); } ///// TLC ///// - std::string tlcDir = concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR); - if (!createDir(tlcDir)) { - _ERR("Cannot create directory: %s", tlcDir.c_str()); - return 0; - } - copySmackAndOwnership(__DOTNET_DIR, tlcDir); - - tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE); - if (!tlc_db) { - _ERR("Sqlite create error. So restore the database."); - if (tlc_restoreDB() != TAC_ERROR_NONE) { - _ERR("Sqlite create error"); - return 0; - } - tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE); - if (!tlc_db) { - _ERR("Sqlite create error"); - return 0; - } + if (tlc_createDB() != 0) { + return -1; } - sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL); - sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str()); - std::vector updateTlc = selectDB(tlc_db, sql); - sqlite3_free(sql); + updateTlc = tlc_selectDB(pkgId); - sql = sqlite3_mprintf("DELETE FROM TLC WHERE PKGID = %Q;", pkgId.c_str()); - if (!deleteDB(tlc_db, sql)) { - _ERR("Sqlite delete error"); - sqlite3_free(sql); - return 0; - } - sqlite3_free(sql); - - std::vector librariesInfo; - if (!skipTLC) { - librariesInfo = getLibrariesInfo(rootPath); + if (tlc_deleteDB(pkgId) != 0) { + return -1; } - copyLibraryCreateSymlink(pkgId, librariesInfo, tlcDir); + tlcUpdateDB(pkgId); - tlc_updateDB(tlc_db, updateTlc, tlcDir); + copyLibraryCreateSymlink(pkgId, skipTLC ? std::vector() : getLibrariesInfo(rootPath)); return 0; } @@ -764,66 +547,30 @@ int tacUninstall(const std::string& pkgId, tac_state state) tacPluginInstalled = true; tacState= state; - tac_db = openDB(TAC_APP_LIST_DB); - if (!tac_db) { - _ERR("Sqlite open error. So restore the database."); - if (tac_restoreDB() != TAC_ERROR_NONE) { - _ERR("Sqlite open error"); - return -1; - } - tac_db = openDB(TAC_APP_LIST_DB); - if (!tac_db) { - _ERR("Sqlite open error"); - return -1; - } + if (tac_openDB() != 0) { + return -1; } - sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL); - char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str()); - updateTac = selectDB(tac_db, sql); - sqlite3_free(sql); + updateTac = tac_selectDB(pkgId); - sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId.c_str()); - - if (!deleteDB(tac_db, sql)) { - _ERR("Sqlite delete error"); - sqlite3_free(sql); + if (tac_deleteDB(pkgId, "") != 0) { tacState = TAC_STATE_RESTORE; return -1; } - sqlite3_free(sql); - - tac_updateDB(tac_db); + tacUpdateDB(pkgId); ///// TLC ///// - tlc_db = openDB(TLC_APP_LIST_DB); - if (!tlc_db) { - _ERR("Sqlite open error. So restore the database."); - if (tlc_restoreDB() != TAC_ERROR_NONE) { - _ERR("Sqlite open error"); - return 0; - } - tlc_db = openDB(TLC_APP_LIST_DB); - if (!tlc_db) { - _ERR("Sqlite open error"); - return 0; - } + if (tlc_openDB() != 0) { + return -1; } - sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL); - sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str()); - std::vector updateTlc = selectDB(tlc_db, sql); - sqlite3_free(sql); + updateTlc = tlc_selectDB(pkgId); - sql = sqlite3_mprintf("DELETE FROM TLC WHERE PKGID = %Q;", pkgId.c_str()); - if (!deleteDB(tlc_db, sql)) { - _ERR("Sqlite delete error"); - sqlite3_free(sql); - return 0; + if (tlc_deleteDB(pkgId) != 0) { + return -1; } - sqlite3_free(sql); - tlc_updateDB(tlc_db, updateTlc, concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR)); + tlcUpdateDB(pkgId); return 0; } @@ -863,7 +610,7 @@ void undoStep(std::string tac) } }; - scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0); + scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0); } void install_Undo() @@ -921,15 +668,9 @@ int tacUndo(const std::string& pkgId) update_Undo(); } - if (tac_db) { - rollbackDB(tac_db); - tac_db = NULL; - } + tac_rollbackDB(); - if (tlc_db) { - rollbackDB(tlc_db); - tlc_db = NULL; - } + tlc_rollbackDB(); return 0; } @@ -986,7 +727,7 @@ void cleanStep(std::string tac) } }; - scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0); + scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0); } void install_Clean() @@ -1075,16 +816,12 @@ int tacClean(const std::string& pkgId) unInstall_Clean(); } - if (tac_db) { - closeDB(tac_db); - tac_db = NULL; + if (tac_closeDB()) { copySmackAndOwnership(__DOTNET_DIR, TAC_APP_LIST_DB); copySmackAndOwnership(__DOTNET_DIR, TAC_APP_LIST_DB + std::string("-journal")); } - if (tlc_db) { - closeDB(tlc_db); - tlc_db = NULL; + if (tlc_closeDB()) { copySmackAndOwnership(__DOTNET_DIR, TLC_APP_LIST_DB); copySmackAndOwnership(__DOTNET_DIR, TLC_APP_LIST_DB + std::string("-journal")); } diff --git a/packaging/dotnet-launcher.spec b/packaging/dotnet-launcher.spec index 3a99d4e..8197f24 100644 --- a/packaging/dotnet-launcher.spec +++ b/packaging/dotnet-launcher.spec @@ -254,6 +254,7 @@ chsmack -a User /usr/bin/dotnet-uts-loader %{_includedir}/ni_common.h %{_includedir}/tac_common.h %{_includedir}/tac_installer.h +%{_includedir}/tac_db.h %{_includedir}/profile_common.h %{_includedir}/privilege_common.h %{_includedir}/dotnet_launcher_plugin.h