From: j-h.choi Date: Wed, 23 Apr 2025 03:24:16 +0000 (+0900) Subject: Refactoring TAC to support multi-package installation X-Git-Tag: accepted/tizen/unified/20250604.100251~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3ede9e1a62b7c16e93fd214e4c6d5dd93641bee8;p=platform%2Fcore%2Fdotnet%2Flauncher.git Refactoring TAC to support multi-package installation Change-Id: I57b52ad0edeb8cb9c9944de30a43940d0c84aeca --- diff --git a/NativeLauncher/tool/tac_installer.cc b/NativeLauncher/tool/tac_installer.cc index fffcaef..675269a 100644 --- a/NativeLauncher/tool/tac_installer.cc +++ b/NativeLauncher/tool/tac_installer.cc @@ -42,34 +42,56 @@ static const char* __READ_ONLY_TAC_DIR = __STR(READ_ONLY_TAC_DIR); #undef __STR #undef __XSTR -static std::vector nugetPackagesAssembliesSha; -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 std::string prevInstallPkgId = std::string(""); -static std::string prevFinishPkgId = std::string(""); -static std::string tacLocation = __DOTNET_DIR; -static bool isTacReadonly = false; +typedef struct TACInfo { + std::vector nugetPackagesAssembliesSha; + std::vector tacDB; + std::vector createDirectories; + std::vector createLibraries; + std::vector updateTac; + std::vector updateTlc; + tac_state tacState = TAC_STATE_NONE; + std::string prevInstallPkgId = std::string(""); + std::string prevFinishPkgId = std::string(""); + std::string tacLocation = __DOTNET_DIR; + bool isTacReadonly = false; +} TACInfo; + +std::map TacMgr; + +static void addTacMgr(TACInfo& tacInfo, const std::string& pkgId) +{ + TacMgr[pkgId] = tacInfo; +} + +static TACInfo findTacMgr(const std::string& pkgId) +{ + return TacMgr.at(pkgId); +} // initialize static vector to support multi-package install scenario -static void tacInitialize() +static TACInfo tacInitialize(const std::string& pkgId) { - nugetPackagesAssembliesSha.clear(); - tacDB.clear(); - createDirectories.clear(); - createLibraries.clear(); - updateTac.clear(); - updateTlc.clear(); + TACInfo tacInfo; + tacInfo.nugetPackagesAssembliesSha.clear(); + tacInfo.tacDB.clear(); + tacInfo.createDirectories.clear(); + tacInfo.createLibraries.clear(); + tacInfo.updateTac.clear(); + tacInfo.updateTlc.clear(); + tacInfo.tacState = TAC_STATE_NONE; + tacInfo.prevInstallPkgId = std::string(""); + tacInfo.prevFinishPkgId = std::string(""); + tacInfo.tacLocation = __DOTNET_DIR; + tacInfo.isTacReadonly = false; + addTacMgr(tacInfo, pkgId); + return tacInfo; } -static void createSHA256Info(std::string sha256Info, std::string nugetPackage) +static void createSHA256Info(TACInfo& tacInfo, const std::string& sha256Info, const std::string& nugetPackage) { std::ofstream ofs(sha256Info, std::ios::app); int assembly_count = 0; - for (auto& npAssemblySha : nugetPackagesAssembliesSha) { + for (auto& npAssemblySha : tacInfo.nugetPackagesAssembliesSha) { std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':')); std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':')); std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1); @@ -83,12 +105,12 @@ static void createSHA256Info(std::string sha256Info, std::string nugetPackage) ofs.close(); } -static bool compareSHA256Info(std::string sha256Info, std::string nugetPackage) +static bool compareSHA256Info(TACInfo& tacInfo, const std::string& sha256Info, const std::string& nugetPackage) { int compare_count = 0; int assembly_count = 0; std::string sha256_count = "0"; - for (auto& npAssemblySha : nugetPackagesAssembliesSha) { + for (auto& npAssemblySha : tacInfo.nugetPackagesAssembliesSha) { std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':')); std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':')); std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1); @@ -116,12 +138,12 @@ static bool compareSHA256Info(std::string sha256Info, std::string nugetPackage) return false; } -static bool copyAssemblyCreateSymlink(std::string binPath, std::string tacDir, std::string nugetPackage, bool isCreateTacDir) +static bool copyAssemblyCreateSymlink(TACInfo& tacInfo, const std::string& binPath, const std::string& tacDir, const std::string& nugetPackage, bool isCreateTacDir) { std::string binNiPath = concatPath(binPath, APP_NI_SUB_DIR); - std::string tac_version_dir = concatPath(tacLocation, nugetPackage); + std::string tac_version_dir = concatPath(tacInfo.tacLocation, nugetPackage); bool nuget_restoration = false; - for (auto& npAssemblySha : nugetPackagesAssembliesSha) { + for (auto& npAssemblySha : tacInfo.nugetPackagesAssembliesSha) { std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':')); std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':')); std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1); @@ -173,7 +195,7 @@ static bool copyAssemblyCreateSymlink(std::string binPath, std::string tacDir, s } if (nuget_restoration) { - for (auto& npAssemblySha : nugetPackagesAssembliesSha) { + for (auto& npAssemblySha : tacInfo.nugetPackagesAssembliesSha) { std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':')); std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':')); std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1); @@ -188,7 +210,7 @@ static bool copyAssemblyCreateSymlink(std::string binPath, std::string tacDir, s return nuget_restoration; } -static void copyLibraryCreateSymlink(const std::string pkgId, std::vector LibrariesInfo) +static void copyLibraryCreateSymlink(TACInfo& tacInfo, const std::string& pkgId, std::vector LibrariesInfo) { if (LibrariesInfo.empty()) { return; @@ -198,7 +220,7 @@ static void copyLibraryCreateSymlink(const std::string pkgId, std::vector() : getLibrariesInfo(rootPath)); + copyLibraryCreateSymlink(tacInfo, pkgId, skipTLC ? std::vector() : getLibrariesInfo(rootPath)); tac_closeDB(); tlc_closeDB(); + addTacMgr(tacInfo, pkgId); + return 0; } @@ -580,39 +610,39 @@ int tacUninstall(const std::string& pkgId, tac_state state) _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]"); _INFO("PackageID : %s", pkgId.c_str()); - tacInitialize(); + TACInfo tacInfo = tacInitialize(pkgId); - isTacReadonly = isReadOnlyPkg(pkgId); - tacLocation = isTacReadonly ? __READ_ONLY_TAC_DIR : __DOTNET_DIR; + tacInfo.isTacReadonly = isReadOnlyPkg(pkgId); + tacInfo.tacLocation = tacInfo.isTacReadonly ? __READ_ONLY_TAC_DIR : __DOTNET_DIR; // Can be multiple apps in one package - if (strcmp(pkgId.c_str(), prevInstallPkgId.c_str()) == 0) { + if (strcmp(pkgId.c_str(), tacInfo.prevInstallPkgId.c_str()) == 0) { _INFO("TAC Plugin(UNINSTALL) already run for same pkgId (%s)", pkgId.c_str()); return 0; } - prevInstallPkgId = pkgId; + tacInfo.prevInstallPkgId = pkgId; - tacState= state; - if (tac_openDB(isTacReadonly) != 0) { + tacInfo.tacState = state; + if (tac_openDB(tacInfo.isTacReadonly) != 0) { return -1; } - updateTac = tac_selectDB(pkgId); + tacInfo.updateTac = tac_selectDB(pkgId); if (tac_deleteDB(pkgId, "") != 0) { - tacState = TAC_STATE_RESTORE; + tacInfo.tacState = TAC_STATE_RESTORE; tac_closeDB(); return -1; } - tacUpdateDB(pkgId); + tacUpdateDB(tacInfo, pkgId); ///// TLC ///// - if (tlc_openDB(isTacReadonly) != 0) { + if (tlc_openDB(tacInfo.isTacReadonly) != 0) { tac_closeDB(); return -1; } - updateTlc = tlc_selectDB(pkgId); + tacInfo.updateTlc = tlc_selectDB(pkgId); if (tlc_deleteDB(pkgId) != 0) { tac_closeDB(); @@ -620,11 +650,13 @@ int tacUninstall(const std::string& pkgId, tac_state state) return -1; } - tlcUpdateDB(pkgId); + tlcUpdateDB(tacInfo, pkgId); tac_closeDB(); tlc_closeDB(); + addTacMgr(tacInfo, pkgId); + return 0; } @@ -636,9 +668,9 @@ int tacRemoved(const std::string& pkgId) return tacUpgrade(pkgId, TAC_STATE_REMOVED); } -void undoStep(std::string tac) +void undoStep(TACInfo& tacInfo, const std::string& tac) { - std::string current_tac = concatPath(tacLocation, tac.substr(0, tac.find('/'))); + std::string current_tac = concatPath(tacInfo.tacLocation, tac.substr(0, tac.find('/'))); try { for (auto& bck : bf::recursive_directory_iterator(current_tac)) { std::string bck_path = bck.path().string(); @@ -663,40 +695,40 @@ void undoStep(std::string tac) } }; - scanFilesInDirectory(tlcLBPath(isTacReadonly), convert, 0); + scanFilesInDirectory(tlcLBPath(tacInfo.isTacReadonly), convert, 0); } -void install_Undo() +void install_Undo(TACInfo& tacInfo) { - for (auto& cd : createDirectories) { + for (auto& cd : tacInfo.createDirectories) { if (!removeAll(cd)) { _ERR("Failed to remove of %s", cd.c_str()); } } - for (auto& cl : createLibraries) { + for (auto& cl : tacInfo.createLibraries) { if (!removeFile(cl)) { _ERR("Failed to remove of %s", cl.c_str()); } } } -void unInstall_Undo() +void unInstall_Undo(TACInfo& tacInfo) { - for (auto& unp : updateTac) { - undoStep(unp); + for (auto& unp : tacInfo.updateTac) { + undoStep(tacInfo, unp); } } -void update_Undo() +void update_Undo(TACInfo& tacInfo) { - install_Undo(); - if (!tacDB.empty()) { - for (auto& np : tacDB) { - undoStep(np); + install_Undo(tacInfo); + if (!tacInfo.tacDB.empty()) { + for (auto& np : tacInfo.tacDB) { + undoStep(tacInfo, np); } } - unInstall_Undo(); + unInstall_Undo(tacInfo); } int tacUndo(const std::string& pkgId) @@ -704,24 +736,23 @@ int tacUndo(const std::string& pkgId) _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]"); _INFO("PackageID : %s", pkgId.c_str()); - isTacReadonly = isReadOnlyPkg(pkgId); - tacLocation = isTacReadonly ? __READ_ONLY_TAC_DIR : __DOTNET_DIR; + TACInfo tacInfo = findTacMgr(pkgId); // Can be multiple apps in one package - if (strcmp(pkgId.c_str(), prevFinishPkgId.c_str()) == 0) { + if (strcmp(pkgId.c_str(), tacInfo.prevFinishPkgId.c_str()) == 0) { _INFO("TAC Plugin(UNDO) already run for same pkgId (%s)", pkgId.c_str()); return 0; } - prevFinishPkgId = pkgId; + tacInfo.prevFinishPkgId = pkgId; - if (tacState == TAC_STATE_INSTALL) { - install_Undo(); - } else if (tacState == TAC_STATE_UPGRADE) { - update_Undo(); - } else if (tacState == TAC_STATE_UNINSTALL) { - unInstall_Undo(); - } else if (tacState == TAC_STATE_RESTORE) { - update_Undo(); + if (tacInfo.tacState == TAC_STATE_INSTALL) { + install_Undo(tacInfo); + } else if (tacInfo.tacState == TAC_STATE_UPGRADE) { + update_Undo(tacInfo); + } else if (tacInfo.tacState == TAC_STATE_UNINSTALL) { + unInstall_Undo(tacInfo); + } else if (tacInfo.tacState == TAC_STATE_RESTORE) { + update_Undo(tacInfo); } tac_rollbackDB(); @@ -731,20 +762,20 @@ int tacUndo(const std::string& pkgId) return 0; } -void changeOwnershipTAC(std::string current_tac) +void changeOwnershipTAC(TACInfo& tacInfo, const std::string& current_tac) { - copySmackAndOwnership(tacLocation, current_tac); + copySmackAndOwnership(tacInfo.tacLocation, current_tac); try { for (auto& path : bf::recursive_directory_iterator(current_tac)) - copySmackAndOwnership(tacLocation, path.path().string()); + copySmackAndOwnership(tacInfo.tacLocation, path.path().string()); } catch (const bf::filesystem_error& error) { _ERR("Failed to recursive directory: %s", error.what()); } } -void cleanStep(std::string tac) +void cleanStep(TACInfo& tacInfo, const std::string tac) { - std::string current_tac = concatPath(tacLocation, tac.substr(0, tac.find('/'))); + std::string current_tac = concatPath(tacInfo.tacLocation, tac.substr(0, tac.find('/'))); try { for (auto& bck : bf::recursive_directory_iterator(current_tac)) { std::string bck_path = bck.path().string(); @@ -783,43 +814,45 @@ void cleanStep(std::string tac) } }; - scanFilesInDirectory(tlcLBPath(isTacReadonly), convert, 0); + scanFilesInDirectory(tlcLBPath(tacInfo.isTacReadonly), convert, 0); } -void install_Clean() +void install_Clean(TACInfo& tacInfo) { - for (auto& cd : createDirectories) { - changeOwnershipTAC(cd); - copySmackAndOwnership(tacLocation, cd.substr(0, cd.rfind('/'))); + for (auto& cd : tacInfo.createDirectories) { + changeOwnershipTAC(tacInfo, cd); + copySmackAndOwnership(tacInfo.tacLocation, cd.substr(0, cd.rfind('/'))); } - for (auto& cl : createLibraries) { - copySmackAndOwnership(tacLocation, cl); + for (auto& cl : tacInfo.createLibraries) { + copySmackAndOwnership(tacInfo.tacLocation, cl); } } -void unInstall_Clean() +void unInstall_Clean(TACInfo& tacInfo) { - for (auto& unp : updateTac) { - cleanStep(unp); + for (auto& unp : tacInfo.updateTac) { + cleanStep(tacInfo, unp); } } -void update_Clean() +void update_Clean(TACInfo& tacInfo) { - install_Clean(); - if (!tacDB.empty()) { - for (auto& np : tacDB) { - cleanStep(np); - changeOwnershipTAC(concatPath(tacLocation, np.substr(0, np.find('/')))); + install_Clean(tacInfo); + if (!tacInfo.tacDB.empty()) { + for (auto& np : tacInfo.tacDB) { + cleanStep(tacInfo, np); + changeOwnershipTAC(tacInfo, concatPath(tacInfo.tacLocation, np.substr(0, np.find('/')))); } } - unInstall_Clean(); + unInstall_Clean(tacInfo); } int tacClean(const std::string& pkgId) { - if (tacState == TAC_STATE_RESTORE) { + TACInfo tacInfo = findTacMgr(pkgId); + + if (tacInfo.tacState == TAC_STATE_RESTORE) { disableTACPackage(pkgId); std::string rootPath = getRootPath(pkgId); @@ -858,30 +891,30 @@ int tacClean(const std::string& pkgId) _INFO("PackageID : %s", pkgId.c_str()); // Can be multiple apps in one package - if (strcmp(pkgId.c_str(), prevFinishPkgId.c_str()) == 0) { + if (strcmp(pkgId.c_str(), tacInfo.prevFinishPkgId.c_str()) == 0) { _INFO("TAC Plugin(CLEAN) already run for same pkgId (%s)", pkgId.c_str()); return 0; } - prevFinishPkgId = pkgId; + tacInfo.prevFinishPkgId = pkgId; - if (tacState == TAC_STATE_INSTALL) { - install_Clean(); - } else if (tacState == TAC_STATE_UPGRADE) { - update_Clean(); - } else if (tacState == TAC_STATE_UNINSTALL) { - unInstall_Clean(); + if (tacInfo.tacState == TAC_STATE_INSTALL) { + install_Clean(tacInfo); + } else if (tacInfo.tacState == TAC_STATE_UPGRADE) { + update_Clean(tacInfo); + } else if (tacInfo.tacState == TAC_STATE_UNINSTALL) { + unInstall_Clean(tacInfo); } if (tac_closeDB()) { - std::string tacDbPath = tacDBPath(isTacReadonly, TAC_APP_LIST_DB); - copySmackAndOwnership(tacLocation, tacDbPath); - copySmackAndOwnership(tacLocation, tacDbPath + std::string("-journal")); + std::string tacDbPath = tacDBPath(tacInfo.isTacReadonly, TAC_APP_LIST_DB); + copySmackAndOwnership(tacInfo.tacLocation, tacDbPath); + copySmackAndOwnership(tacInfo.tacLocation, tacDbPath + std::string("-journal")); } if (tlc_closeDB()) { - std::string tlcDbPath = tacDBPath(isTacReadonly, TLC_APP_LIST_DB); - copySmackAndOwnership(tacLocation, tlcDbPath); - copySmackAndOwnership(tacLocation, tlcDbPath + std::string("-journal")); + std::string tlcDbPath = tacDBPath(tacInfo.isTacReadonly, TLC_APP_LIST_DB); + copySmackAndOwnership(tacInfo.tacLocation, tlcDbPath); + copySmackAndOwnership(tacInfo.tacLocation, tlcDbPath + std::string("-journal")); } return 0;