Revert PR #509, #510 (#513)
author최종헌/MDE Lab(SR)/삼성전자 <j-h.choi@samsung.com>
Wed, 14 Feb 2024 03:07:12 +0000 (12:07 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 14 Feb 2024 03:07:12 +0000 (12:07 +0900)
* Revert "Sets the ZapDisable env according to the Alternative Sampling value of the coreprofiler"

This reverts commit 2fa05eebf1a9a61018efefea5c5cfe42a9fd5b7f.

* Revert "[Refactoring] Improve SAM score through DC and GV improvement"

This reverts commit 04a885649ac58636a6b7da2c20449adceb84e40b.

NativeLauncher/dotnet.debugger
NativeLauncher/hydra/hydra_main.cc
NativeLauncher/inc/launcher_env.h
NativeLauncher/installer-plugin/dotnet_apptype_plugin.cc
NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc
NativeLauncher/launcher/lib/core_runtime.cc
NativeLauncher/launcher/lib/injection.cc
NativeLauncher/launcher/lib/injection.h
NativeLauncher/tool/tac_common.cc
NativeLauncher/tool/tac_installer.cc
NativeLauncher/util/plugin_manager.cc

index 1f8f89a..12ccfc9 100644 (file)
@@ -67,6 +67,7 @@ DEFAULT_OPT   PROF_TRACE_FILENAME=/home/owner/share/tmp/sdk_tools/coreprofiler/tra
 DEFAULT_OPT    PROF_CONFIG_FILENAME=/home/owner/share/tmp/sdk_tools/coreprofiler/profiler.config
 DEFAULT_OPT    CORECLR_ENABLE_PROFILING=1
 DEFAULT_OPT    COMPlus_EnableEventLog=1
+DEFAULT_OPT    COMPlus_ZapDisable=1
 DEFAULT_OPT -c
 DEFAULT_OPT 6001
 DEFAULT_OPT -d
@@ -93,6 +94,7 @@ DEFAULT_OPT   PROF_TRACE_FILENAME=/home/owner/share/tmp/sdk_tools/coreprofiler/tra
 DEFAULT_OPT    PROF_CONFIG_FILENAME=/home/owner/share/tmp/sdk_tools/coreprofiler/profiler.config
 DEFAULT_OPT    CORECLR_ENABLE_PROFILING=1
 DEFAULT_OPT    COMPlus_EnableEventLog=1
+DEFAULT_OPT    COMPlus_ZapDisable=1
 DEFAULT_OPT -c
 DEFAULT_OPT 6001
 DEFAULT_OPT -d
index 7ced0a3..b14f1ef 100644 (file)
@@ -31,8 +31,8 @@
 #include "log.h"
 #include "launcher_env.h"
 
-static const char* __coreclr_lib = "/usr/share/dotnet.tizen/netcoreapp/libcoreclr.so";
-static const char* __dotnet_loader = "/usr/bin/dotnet-loader";
+const char* __coreclr_lib = "/usr/share/dotnet.tizen/netcoreapp/libcoreclr.so";
+const char* __dotnet_loader = "/usr/bin/dotnet-loader";
 
 typedef int (*coreclr_preload_assembly_ptr)(const char* assemblyPath);
 typedef int (*launcher_real_main_ptr)(int argc, char *argv[]);
index 6b24f87..df832c7 100644 (file)
@@ -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            "/opt/usr/dotnet/Libraries"
+#define TLC_LIBRARIES_DIR            "Libraries"
 #define TIZEN_DOTNET_NUGET           "Tizen.NET"
 #define TIZEN_DOTNET_SDK_NUGET       "Tizen.NET.Sdk"
 #define NET_STANDARD_LIBRARY_NUGET   "NETStandard.Library"
index 0cff386..48e0ed3 100644 (file)
@@ -30,7 +30,7 @@
 typedef struct _xmlDoc xmlDoc;
 typedef xmlDoc* xmlDocPtr;
 
-static bool pluginInstalled = false;
+bool pluginInstalled = false;
 
 extern "C" int PKGMGR_PARSER_PLUGIN_INSTALL(xmlDocPtr doc, const char* pkgId)
 {
index 840a40b..91928a7 100644 (file)
@@ -30,8 +30,8 @@
 #endif
 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
 
-static bool aotPluginInstalled = false;
-static bool aotPluginFinished = false;
+bool aotPluginInstalled = false;
+bool aotPluginFinished = false;
 
 extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list)
 {
index 03d8d8f..ba874aa 100644 (file)
@@ -97,8 +97,8 @@ static void setEnvFromFile()
 
 #define _unused(x) ((void)(x))
 
-static struct sigaction sig_abrt_new;
-static struct sigaction sig_abrt_old;
+struct sigaction sig_abrt_new;
+struct sigaction sig_abrt_old;
 
 static bool checkOnSigabrt = false;
 static bool checkOnTerminate = false;
@@ -318,9 +318,9 @@ int CoreRuntime::initialize(const char* appType, LaunchMode launchMode)
                return -1;
        }
 
-       // checkProfilerInjection checks dotnet-launcher run mode
+       // checkInjection checks dotnet-launcher run mode
        // At the moment, this mechanism is used only when the Memory Profiler is started.
-       int res = checkProfilerInjection();
+       int res = checkInjection();
        if (res != 0) {
                _ERR("Failed to initnialize Memory Profiler");
                return -1;
index 2c722a4..819af59 100644 (file)
  */
 
 #include "log.h"
-#include "utils.h"
 
 #include <cstdlib>
 #include <cstring>
 
 #include <dlfcn.h>
-#include <fstream>
-#include <string>
 
 #define HT_PATH "/home/owner/share/tmp/sdk_tools/heaptrack/"
 #define HT_LIB_PATH HT_PATH "libprofiler.so"
 #define HT_INJECTION_LIB_PATH HT_PATH "libheaptrack_inject.so"
 
-#define CP_PATH "/home/owner/share/tmp/sdk_tools/coreprofiler/"
-#define CP_LIB_PATH CP_PATH "libcoreprof.so"
-#define CP_INHECTION_CONFIG CP_PATH "profiler.config"
-
-static int injectHeaptrackLibrary()
+static int injectLibrary(const char path[])
 {
        typedef int inject_func();
 
@@ -41,10 +34,10 @@ static int injectHeaptrackLibrary()
        inject_func *inject = nullptr;
        const char *inject_sym = "dotnet_launcher_inject";
 
-       _INFO("Inject %s library", HT_INJECTION_LIB_PATH);
+       _INFO("Inject %s library", path);
 
        // Current implementation of heaptrack CLR profiler requires RTLD_GLOBAL
-       lib = dlopen(HT_INJECTION_LIB_PATH, RTLD_NOW | RTLD_GLOBAL);
+       lib = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
        if (lib == nullptr) {
                _ERR("%s", dlerror());
                return res;
@@ -52,7 +45,7 @@ static int injectHeaptrackLibrary()
 
        inject = reinterpret_cast<inject_func*>(dlsym(lib, inject_sym));
        if (inject == nullptr) {
-               _ERR("%s is not found in the %s", inject_sym, HT_INJECTION_LIB_PATH);
+               _ERR("%s is not found in the %s", inject_sym, path);
                return res;
        }
 
@@ -60,32 +53,7 @@ static int injectHeaptrackLibrary()
        return res;
 }
 
-static int injectCoreprofilerConfig()
-{
-       if (!exist(CP_INHECTION_CONFIG)) {
-               return -1;
-       }
-
-       _INFO("Inject %s", CP_INHECTION_CONFIG);
-
-       std::ifstream ifs(CP_INHECTION_CONFIG);
-       std::string get_conf;
-       if (ifs.is_open()) {
-               while (getline(ifs, get_conf)) {
-                       if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=1")) {
-                               setenv("COMPlus_ZapDisable", "0", 1);
-                               break;
-                       } else if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=0")) {
-                               setenv("COMPlus_ZapDisable", "1", 1);
-                               break;
-                       }
-               }
-               ifs.close();
-       }
-       return 0;
-}
-
-int checkProfilerInjection()
+int checkInjection()
 {
        char *env = nullptr;
 
@@ -94,21 +62,18 @@ int checkProfilerInjection()
                return 0;
        }
 
-       _INFO("##### Perform profiler injection #########");
-
        // At the moment, this mechanism is used only when the Memory Profiler is started.
-       if (!strcmp(env, HT_LIB_PATH)) {
-               if (injectHeaptrackLibrary() != 0) {
-                       _INFO("##### Heaptrack injection failed #########");
-                       return -1;
-               }
-       } else if (!strcmp(env, CP_LIB_PATH)) {
-               if (injectCoreprofilerConfig() != 0) {
-                       _INFO("##### Coreprofiler injection failed #########");
-                       return -1;
-               }
+       if (strcmp(env, HT_LIB_PATH) != 0) {
+               return 0;
+       }
+
+       _INFO("##### Perform injection #########");
+
+       if (injectLibrary(HT_INJECTION_LIB_PATH) != 0) {
+               _INFO("##### Injection failed #########");
+               return -1;
        }
-       _INFO("##### Injection profiler finished #########");
+
+       _INFO("##### Injection finished #########");
        return 0;
 }
-
index 042fe42..6bcf111 100644 (file)
@@ -17,6 +17,6 @@
 #ifndef __INJETION_INTERFACE_H__
 #define __INJETION_INTERFACE_H__
 
-int checkProfilerInjection();
+int checkInjection();
 
 #endif // __INJETION_INTERFACE_H__
index f7fb9ef..93462ec 100644 (file)
@@ -38,8 +38,8 @@ static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
 
 static sqlite3 *tac_db = NULL;
 static sqlite3 *tlc_db = NULL;
-static std::vector<std::string> restore_nuget;
-static std::vector<std::string> restore_library;
+std::vector<std::string> restore_nuget;
+std::vector<std::string> restore_library;
 
 static void cleanupDirectory()
 {
@@ -252,24 +252,6 @@ tac_error_e disableTACPackage(const std::string& pkgId)
        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;
-       bf::create_symlink(from, concatPath(tacDir, to), error);
-       if (error) {
-               _SERR("Failed to create symlink %s file", concatPath(tacDir, to).c_str());
-               return TAC_ERROR_UNKNOWN;
-       }
-       _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, to).c_str());
-       copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, to).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 enableTACPackage(const std::string& pkgId)
 {
        std::string rootPath = getRootPath(pkgId);
@@ -296,61 +278,76 @@ tac_error_e enableTACPackage(const std::string& pkgId)
                return TAC_ERROR_INVALID_PACKAGE;
        }
 
-       if (strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) {
-               _SERR("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str());
-               return TAC_ERROR_NONE;
-       }
-
-       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 (!createDir(tacDir)) {
-               _SERR("Cannot create directory: %s", tacDir.c_str());
-               return TAC_ERROR_UNKNOWN;
-       }
-       copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
-
-       std::vector<std::string> 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)) {
+               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<std::string> 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;
-       }
+                       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);
 
-       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) {
+                                                       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 (!removeFile(concatPath(binDir, fileName))) {
+                                               _SERR("Failed to remove of %s", concatPath(binDir, fileName).c_str());
                                                return TAC_ERROR_UNKNOWN;
                                        }
                                }
                        }
-                       if (createSymlinkFile(tacDir, binDir, originPath, fileName) != TAC_ERROR_NONE) {
-                               return TAC_ERROR_UNKNOWN;
+                       if (enableNuget.empty()) {
+                               if (!removeAll(tacDir)) {
+                                       _SERR("Failed to remove of %s", tacDir.c_str());
+                               }
                        }
+                       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;
 }
 
@@ -531,7 +528,7 @@ tac_error_e tlc_restoreDB()
                }
        };
 
-       scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0);
+       scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
 
        return TAC_ERROR_NONE;
 }
index 5493a9a..c01a616 100644 (file)
@@ -40,16 +40,16 @@ static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
 #undef __STR
 #undef __XSTR
 
-static std::vector<std::string> nugetPackagesAssembliesSha;
-static std::vector<std::string> tacDB;
-static std::vector<std::string> createDirectories;
-static std::vector<std::string> createLibraries;
-static std::vector<std::string> updateTac;
-static tac_state tacState = TAC_STATE_NONE;
+std::vector<std::string> nugetPackagesAssembliesSha;
+std::vector<std::string> tacDB;
+std::vector<std::string> createDirectories;
+std::vector<std::string> createLibraries;
+std::vector<std::string> updateTac;
+tac_state tacState = TAC_STATE_NONE;
 static sqlite3 *tac_db = NULL;
 static sqlite3 *tlc_db = NULL;
-static bool tacPluginInstalled = false;
-static bool tacPluginFinished = false;
+bool tacPluginInstalled = false;
+bool tacPluginFinished = false;
 
 static void createSHA256Info(std::string sha256Info, std::string nugetPackage)
 {
@@ -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<std::string> LibrariesInfo)
+static void copyLibraryCreateSymlink(const std::string pkgId, std::vector<std::string> LibrariesInfo, std::string tlcDir)
 {
        if (LibrariesInfo.empty()) {
                return;
@@ -178,31 +178,30 @@ static void copyLibraryCreateSymlink(const std::string pkgId, std::vector<std::s
                std::string library = librarySha.substr(0, librarySha.find(':'));
                std::string filename = library.substr(library.rfind('/') + 1);
                std::string fileSha = filename + ".." + librarySha.substr(librarySha.find(':') + 1);
-               std::string shaPath = concatPath(TLC_LIBRARIES_DIR, fileSha);
                bool fileCopied = false;
-               if (!exist(shaPath)) {
-                       if (!copyFile(library, shaPath)) {
+               if (!exist(concatPath(tlcDir, fileSha))) {
+                       if (!copyFile(library, concatPath(tlcDir, fileSha))) {
                                _ERR("Failed to copy of %s", filename.c_str());
                                continue;
                        }
                        fileCopied = true;
-                       createLibraries.push_back(shaPath);
+                       createLibraries.push_back(concatPath(tlcDir, fileSha));
                }
                if (!removeFile(library)) {
                        _ERR("Failed to remove of %s", library.c_str());
                        if (fileCopied) {
-                               removeFile(shaPath);
+                               removeFile(concatPath(tlcDir, fileSha));
                        }
                        continue;
                }
                bs::error_code error;
-               bf::create_symlink(shaPath, library, error);
+               bf::create_symlink(concatPath(tlcDir, fileSha), library, error);
                if (error) {
                        _ERR("Failed to create symlink %s file", library.c_str());
-                       copyFile(shaPath, library);
+                       copyFile(concatPath(tlcDir, fileSha), library);
                        copySmackAndOwnership(getBaseName(library), library);
                        if (fileCopied) {
-                               removeFile(shaPath);
+                               removeFile(concatPath(tlcDir, fileSha));
                        }
                        continue;
                }
@@ -212,10 +211,10 @@ static void copyLibraryCreateSymlink(const std::string pkgId, std::vector<std::s
                if (!insertDB(tlc_db, sql)) {
                        _ERR("Sqlite insert error");
                        sqlite3_free(sql);
-                       copyFile(shaPath, library);
+                       copyFile(concatPath(tlcDir, fileSha), library);
                        copySmackAndOwnership(getBaseName(library), library);
                        if (fileCopied) {
-                               removeFile(shaPath);
+                               removeFile(concatPath(tlcDir, fileSha));
                        }
                        continue;
                }
@@ -237,243 +236,70 @@ static void checkDepsJson(std::string rootPath, std::string binPath, std::string
        tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
 }
 
-static int tac_createDB()
+int tacInstall(const std::string& pkgId, tac_state state, bool tacForce)
 {
-       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;
-}
+       _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
+       _INFO("PackageID : %s", pkgId.c_str());
 
-static 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;
-               }
+       // Can be multiple apps in one package
+       if (tacPluginInstalled) {
+               _INFO("TAC plugin already installed");
+               return 0;
        }
-       sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
-       return 0;
-}
+       tacPluginInstalled = true;
 
-static 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;
+       std::string appType = getAppType(pkgId);
+       if (strstr(appType.c_str(), "dotnet") == NULL) {
+               _ERR("App type is not dotnet");
+               return 0;
        }
-       sqlite3_free(sql);
-       return 0;
-}
-
-static int sqliteCb(void *count, int argc, char **argv, char **colName)
-{
-       int *c = (int*)count;
-       *c = atoi(argv[0]);
-       return 0;
-}
-
-static int tac_countDB(const std::string& pkgId, const std::string& tac_name, int& count)
-{
-       char *sql = sqlite3_mprintf(
-               "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId.c_str(), tac_name.c_str());
-       if (sqlite3_exec(tac_db, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
-               _ERR("Sqlite select error");
-               sqlite3_free(sql);
-               return -1;
+       std::string execName = getExecName(pkgId);
+       std::string rootPath = getRootPath(pkgId);
+       if (execName.empty() || rootPath.empty()) {
+               return 0;
        }
-       sqlite3_free(sql);
-       return 0;
-}
 
-static void tac_selectDB(const std::string& pkgId)
-{
-       char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
-       updateTac = selectDB(tac_db, sql);
-       sqlite3_free(sql);
-}
-
-static 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;
+       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;
        }
-       sqlite3_free(sql);
-       return 0;
-}
-
-static void tacUpdateDB(sqlite3 *sqlite)
-{
-       for (auto& unp : updateTac) {
-               int count = -1;
-               char *sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
-               if (sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
-                       _ERR("Sqlite select error");
-                       sqlite3_free(sql);
-                       continue;
-               }
 
-               if (count == 0) {
-                       std::string tac_version_dir_prev = concatPath(__DOTNET_DIR, unp);
-                       std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
-                       if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
-                               _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
-                               sqlite3_free(sql);
-                               continue;
-                       }
-                       if (!removeAll(tac_version_dir_prev)) {
-                               _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
-                               sqlite3_free(sql);
-                               continue;
-                       }
+       std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
+       if (!tacForce) {
+               if (metaValue.empty()) {
+                       return 0;
                }
-               sqlite3_free(sql);
        }
-}
-
-static 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;
+       if (metaValue == METADATA_VALUE_TRUE || tacForce) {
+               checkDepsJson(rootPath, binPath, execName);
        }
-       sqlite3_free(sql);
-       return 0;
-}
 
-static int tlc_createDB()
-{
-       if (!createDir(TLC_LIBRARIES_DIR)) {
-               _ERR("Cannot create directory: %s", TLC_LIBRARIES_DIR);
-               return -1;
+       tacState = state;
+       if (tacDB.empty()) {
+               return 0;
        }
-       copySmackAndOwnership(__DOTNET_DIR, TLC_LIBRARIES_DIR);
 
-       tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
-       if (!tlc_db) {
+       tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
+       if (!tac_db) {
                _ERR("Sqlite create error. So restore the database.");
-               if (tlc_restoreDB() != TAC_ERROR_NONE) {
+               if (tac_restoreDB() != TAC_ERROR_NONE) {
                        _ERR("Sqlite create error");
                        return -1;
                }
-               tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
-               if (!tlc_db) {
+               tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
+               if (!tac_db) {
                        _ERR("Sqlite create error");
                        return -1;
                }
        }
-       sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
-       return 0;
-}
-
-static 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;
-}
-
-static void tlcUpdateDB(sqlite3 *sqlite, std::vector<std::string> updateTlc)
-{
-       for (auto& ulp : updateTlc) {
-               int count = -1;
-               char *sql = sqlite3_mprintf("SELECT COUNT(LIBRARY) FROM TLC WHERE LIBRARY = %Q;", ulp.c_str());
-               if (sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL) != SQLITE_OK) {
-                       _ERR("Sqlite select error");
-                       sqlite3_free(sql);
-                       continue;
-               }
-               if (count == 0) {
-                       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);
-       }
-}
-
-static int tlc_deleteDB(const std::string& pkgId)
-{
-       char *sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
-       std::vector<std::string> updateTlc = selectDB(tlc_db, sql);
-       sqlite3_free(sql);
-
-       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);
-
-       tlcUpdateDB(tlc_db, updateTlc);
-       return 0;
-}
+       sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
 
-static int generateTAC(const std::string& pkgId, const std::string& binPath)
-{
        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;
        }
-
        copySmackAndOwnership(binPath.c_str(), tac_dir.c_str());
 
        for (auto& np : tacDB) {
@@ -485,141 +311,166 @@ static int generateTAC(const std::string& pkgId, const std::string& binPath)
                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);
-               bool isCreateTacDir = false;
                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)) {
+                       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;
                        }
 
-                       createSHA256Info(sha256_info, np);
-
                        if (!exist(sha256_info)) {
                                tacState = TAC_STATE_RESTORE;
                                return -1;
                        }
 
-                       isCreateTacDir = true;
-               } else {
-                       _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
-
-                       if (isSymlinkFile(sha256_info)) {
-                               _ERR("Failed to create sha256_info. Symbolic link is detected");
+                       if (copyAssemblyCreateSymlink(binPath, tac_dir, np, true)) {
+                               _ERR("Failed to create symlink");
                                tacState = TAC_STATE_RESTORE;
                                return -1;
                        }
 
-                       if (!compareSHA256Info(sha256_info, np)) {
-                               _INFO("Different nuget : %s", np.c_str());
-                               continue;
-                       }
-
-                       isCreateTacDir = false;
-               }
-
-               if (copyAssemblyCreateSymlink(binPath, tac_dir, np, isCreateTacDir)) {
-                       _ERR("Failed to create symlink");
-                       tacState = TAC_STATE_RESTORE;
-                       return -1;
-               }
-
-               if (tacState == TAC_STATE_INSTALL) {
-                       if (tac_insertDB(pkgId, np, tac_name, tac_version) != 0) {
+                       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);
                                tacState = TAC_STATE_RESTORE;
                                return -1;
                        }
-               } else if (tacState == TAC_STATE_UPGRADE) {
-                       int count = -1;
-                       if (tac_countDB(pkgId, tac_name, count) != 0) {
+                       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;
+                                       }
+
+                                       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);
+                                               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;
                        }
-                       if (count == 1) {
-                               if (tac_updateDB(pkgId, np, tac_name, tac_version) != 0) {
-                                       tacState = TAC_STATE_RESTORE;
-                                       return -1;
-                               }
-                       } else if (count == 0) {
-                               if (tac_insertDB(pkgId, np, tac_name, tac_version) != 0) {
-                                       tacState = TAC_STATE_RESTORE;
-                                       return -1;
-                               }
-                       }
                }
        }
-       return 0;
-}
 
-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());
+       ///// 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);
 
-       std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
-       if (!tacForce) {
-               if (metaValue.empty()) {
+       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 (metaValue == METADATA_VALUE_TRUE || tacForce) {
-               checkDepsJson(rootPath, binPath, execName);
-       }
+       sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
 
-       tacState = state;
-       if (tacDB.empty()) {
-               return 0;
-       }
+       copyLibraryCreateSymlink(pkgId, getLibrariesInfo(rootPath), tlcDir);
 
-       if (tac_createDB() != 0) {
-               return -1;
-       }
+       return 0;
+}
 
-       if (generateTAC(pkgId, binPath) != 0) {
-               return -1;
-       }
+static int sqliteCb(void *count, int argc, char **argv, char **colName)
+{
+       int *c = (int*)count;
+       *c = atoi(argv[0]);
+       return 0;
+}
 
-       ///// TLC /////
-       if (tlc_createDB() != 0) {
-               return -1;
+static void tac_updateDB(sqlite3 *sqlite)
+{
+       for (auto& unp : updateTac) {
+               int count = -1;
+               char *sql = sqlite3_mprintf("SELECT COUNT(NUGET) FROM TAC WHERE NUGET = %Q;", unp.c_str());
+               int ret = sqlite3_exec(sqlite, sql, sqliteCb, &count, NULL);
+               if (ret != SQLITE_OK) {
+                       _ERR("Sqlite select error");
+                       sqlite3_free(sql);
+                       continue;
+               }
+               if (count == 0) {
+                       std::string tac_version_dir_prev = concatPath(__DOTNET_DIR, unp);
+                       std::string tac_version_dir_backup = tac_version_dir_prev + ".bck";
+                       if (!copyDir(tac_version_dir_prev, tac_version_dir_backup)) {
+                               _ERR("Failed to copy of %s to %s", tac_version_dir_prev.c_str(), tac_version_dir_backup.c_str());
+                               sqlite3_free(sql);
+                               continue;
+                       }
+                       if (!removeAll(tac_version_dir_prev)) {
+                               _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
+                               sqlite3_free(sql);
+                               continue;
+                       }
+               }
+               sqlite3_free(sql);
        }
+}
 
-       copyLibraryCreateSymlink(pkgId, getLibrariesInfo(rootPath));
-
-       return 0;
+static void tlc_updateDB(sqlite3 *sqlite, std::vector<std::string> updateTlc, std::string tlcDir)
+{
+       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);
+                       continue;
+               }
+               if (count == 0) {
+                       std::string library_prev = concatPath(tlcDir, 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 tacUpgrade(const std::string& pkgId, tac_state state, bool tacForce)
@@ -666,25 +517,171 @@ int tacUpgrade(const std::string& pkgId, tac_state state, bool tacForce)
        }
 
        tacState = TAC_STATE_UPGRADE;
-       if (tac_createDB() != 0) {
-               return -1;
+       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);
 
-       tac_selectDB(pkgId);
+       char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
+       updateTac = selectDB(tac_db, sql);
+       sqlite3_free(sql);
 
        bool skipTLC = false;
        if (tacDB.empty()) {
-               if (tac_deleteDB(pkgId) != 0) {
+               sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
+               if (!deleteDB(tac_db, sql)) {
+                       _ERR("Sqlite delete error");
+                       sqlite3_free(sql);
                        return -1;
                }
-               tacUpdateDB(tac_db);
+               sqlite3_free(sql);
+
+               tac_updateDB(tac_db);
 
                skipTLC = true;
        } else {
-               if (generateTAC(pkgId, binPath) != 0) {
-                       return -1;
+               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;
                }
+               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) {
@@ -694,25 +691,62 @@ int tacUpgrade(const std::string& pkgId, tac_state state, bool tacForce)
                                }
                        }
                        if (!isExits) {
-                               if (tac_deleteDB(pkgId, unp) != 0) {
+                               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);
                                        tacState = TAC_STATE_RESTORE;
                                        return -1;
                                }
+                               sqlite3_free(sql);
                        }
                }
-               tacUpdateDB(tac_db);
+               tac_updateDB(tac_db);
        }
 
        ///// TLC /////
-       if (tlc_createDB() != 0) {
-               return -1;
+       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);
 
-       if (tlc_deleteDB(pkgId) != 0) {
-               return -1;
+       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;
+               }
+       }
+       sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
+
+       sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
+       std::vector<std::string> updateTlc = selectDB(tlc_db, sql);
+       sqlite3_free(sql);
+
+       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);
 
-       copyLibraryCreateSymlink(pkgId, skipTLC ? std::vector<std::string>() : getLibrariesInfo(rootPath));
+       std::vector<std::string> librariesInfo;
+       if (!skipTLC) {
+               librariesInfo = getLibrariesInfo(rootPath);
+       }
+
+       copyLibraryCreateSymlink(pkgId, librariesInfo, tlcDir);
+
+       tlc_updateDB(tlc_db, updateTlc, tlcDir);
 
        return 0;
 }
@@ -730,24 +764,68 @@ int tacUninstall(const std::string& pkgId, tac_state state)
        tacPluginInstalled = true;
 
        tacState= state;
-       if (tac_openDB() != 0) {
-               return -1;
+       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);
 
-       tac_selectDB(pkgId);
+       char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
+       updateTac = selectDB(tac_db, sql);
+       sqlite3_free(sql);
 
-       if (tac_deleteDB(pkgId) != 0) {
+       sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId.c_str());
+
+       if (!deleteDB(tac_db, sql)) {
+               _ERR("Sqlite delete error");
+               sqlite3_free(sql);
                tacState = TAC_STATE_RESTORE;
                return -1;
        }
-       tacUpdateDB(tac_db);
+       sqlite3_free(sql);
+
+       tac_updateDB(tac_db);
 
        ///// TLC /////
-       if (tlc_openDB() != 0) {
-               return -1;
+       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);
+
+       sql = sqlite3_mprintf("SELECT * FROM TLC WHERE PKGID = %Q;", pkgId.c_str());
+       std::vector<std::string> updateTlc = selectDB(tlc_db, sql);
+       sqlite3_free(sql);
 
-       return tlc_deleteDB(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);
+
+       tlc_updateDB(tlc_db, updateTlc, concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR));
+
+       return 0;
 }
 
 int tacRemoved(const std::string& pkgId)
@@ -785,7 +863,7 @@ void undoStep(std::string tac)
                }
        };
 
-       scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0);
+       scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
 }
 
 void install_Undo()
@@ -908,7 +986,7 @@ void cleanStep(std::string tac)
                }
        };
 
-       scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0);
+       scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
 }
 
 void install_Clean()
index 472415d..4fcd787 100644 (file)
@@ -22,7 +22,7 @@
 
 static PluginFunc* __pluginFunc = NULL;
 static void* __pluginLib;
-static bool initializedPluginManager = false;
+bool initializedPluginManager = false;
 
 int initializePluginManager(const char* appType)
 {