[Refactoring] Improve SAM score through DC and GV improvement
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / tac_installer.cc
index c01a616..5493a9a 100644 (file)
@@ -40,16 +40,16 @@ static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
 #undef __STR
 #undef __XSTR
 
-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 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;
 static sqlite3 *tac_db = NULL;
 static sqlite3 *tlc_db = NULL;
-bool tacPluginInstalled = false;
-bool tacPluginFinished = false;
+static bool tacPluginInstalled = false;
+static 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, std::string tlcDir)
+static void copyLibraryCreateSymlink(const std::string pkgId, std::vector<std::string> LibrariesInfo)
 {
        if (LibrariesInfo.empty()) {
                return;
@@ -178,30 +178,31 @@ 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(concatPath(tlcDir, fileSha))) {
-                       if (!copyFile(library, concatPath(tlcDir, fileSha))) {
+               if (!exist(shaPath)) {
+                       if (!copyFile(library, shaPath)) {
                                _ERR("Failed to copy of %s", filename.c_str());
                                continue;
                        }
                        fileCopied = true;
-                       createLibraries.push_back(concatPath(tlcDir, fileSha));
+                       createLibraries.push_back(shaPath);
                }
                if (!removeFile(library)) {
                        _ERR("Failed to remove of %s", library.c_str());
                        if (fileCopied) {
-                               removeFile(concatPath(tlcDir, fileSha));
+                               removeFile(shaPath);
                        }
                        continue;
                }
                bs::error_code error;
-               bf::create_symlink(concatPath(tlcDir, fileSha), library, error);
+               bf::create_symlink(shaPath, library, error);
                if (error) {
                        _ERR("Failed to create symlink %s file", library.c_str());
-                       copyFile(concatPath(tlcDir, fileSha), library);
+                       copyFile(shaPath, library);
                        copySmackAndOwnership(getBaseName(library), library);
                        if (fileCopied) {
-                               removeFile(concatPath(tlcDir, fileSha));
+                               removeFile(shaPath);
                        }
                        continue;
                }
@@ -211,10 +212,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(concatPath(tlcDir, fileSha), library);
+                       copyFile(shaPath, library);
                        copySmackAndOwnership(getBaseName(library), library);
                        if (fileCopied) {
-                               removeFile(concatPath(tlcDir, fileSha));
+                               removeFile(shaPath);
                        }
                        continue;
                }
@@ -236,70 +237,243 @@ static void checkDepsJson(std::string rootPath, std::string binPath, std::string
        tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
 }
 
-int tacInstall(const std::string& pkgId, tac_state state, bool tacForce)
+static int tac_createDB()
 {
-       _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
-       _INFO("PackageID : %s", pkgId.c_str());
+       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;
+}
 
-       // Can be multiple apps in one package
-       if (tacPluginInstalled) {
-               _INFO("TAC plugin already installed");
-               return 0;
+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;
+               }
        }
-       tacPluginInstalled = true;
+       sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
+       return 0;
+}
 
-       std::string appType = getAppType(pkgId);
-       if (strstr(appType.c_str(), "dotnet") == NULL) {
-               _ERR("App type is not dotnet");
-               return 0;
+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 execName = getExecName(pkgId);
-       std::string rootPath = getRootPath(pkgId);
-       if (execName.empty() || rootPath.empty()) {
-               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;
        }
+       sqlite3_free(sql);
+       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;
+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;
        }
+       sqlite3_free(sql);
+       return 0;
+}
 
-       std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY);
-       if (!tacForce) {
-               if (metaValue.empty()) {
-                       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;
+                       }
                }
+               sqlite3_free(sql);
        }
-       if (metaValue == METADATA_VALUE_TRUE || tacForce) {
-               checkDepsJson(rootPath, binPath, execName);
+}
+
+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;
        }
+       sqlite3_free(sql);
+       return 0;
+}
 
-       tacState = state;
-       if (tacDB.empty()) {
-               return 0;
+static int tlc_createDB()
+{
+       if (!createDir(TLC_LIBRARIES_DIR)) {
+               _ERR("Cannot create directory: %s", TLC_LIBRARIES_DIR);
+               return -1;
        }
+       copySmackAndOwnership(__DOTNET_DIR, TLC_LIBRARIES_DIR);
 
-       tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
-       if (!tac_db) {
+       tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
+       if (!tlc_db) {
                _ERR("Sqlite create error. So restore the database.");
-               if (tac_restoreDB() != TAC_ERROR_NONE) {
+               if (tlc_restoreDB() != TAC_ERROR_NONE) {
                        _ERR("Sqlite create error");
                        return -1;
                }
-               tac_db = createDB(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
-               if (!tac_db) {
+               tlc_db = createDB(TLC_APP_LIST_DB, CREATE_TLC_DB_TABLE);
+               if (!tlc_db) {
                        _ERR("Sqlite create error");
                        return -1;
                }
        }
-       sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
+       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;
+}
+
+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) {
@@ -311,166 +485,141 @@ int tacInstall(const std::string& pkgId, tac_state state, bool tacForce)
                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)) {
-                               createSHA256Info(sha256_info, np);
-                       } else {
+
+                       if (isSymlinkFile(sha256_info)) {
                                _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;
                        }
 
-                       if (copyAssemblyCreateSymlink(binPath, tac_dir, np, true)) {
-                               _ERR("Failed to create symlink");
+                       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");
                                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);
+                       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) {
                                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;
-                                       }
-
-                                       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");
+               } else if (tacState == TAC_STATE_UPGRADE) {
+                       int count = -1;
+                       if (tac_countDB(pkgId, tac_name, count) != 0) {
                                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;
+}
 
-       ///// TLC /////
-       std::string tlcDir = concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR);
-       if (!createDir(tlcDir)) {
-               _ERR("Cannot create directory: %s", tlcDir.c_str());
+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;
        }
-       copySmackAndOwnership(__DOTNET_DIR, tlcDir);
+       tacPluginInstalled = true;
 
-       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");
+       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;
                }
        }
-       sqlite3_exec(tlc_db, "BEGIN;", NULL, NULL, NULL);
-
-       copyLibraryCreateSymlink(pkgId, getLibrariesInfo(rootPath), tlcDir);
+       if (metaValue == METADATA_VALUE_TRUE || tacForce) {
+               checkDepsJson(rootPath, binPath, execName);
+       }
 
-       return 0;
-}
+       tacState = state;
+       if (tacDB.empty()) {
+               return 0;
+       }
 
-static int sqliteCb(void *count, int argc, char **argv, char **colName)
-{
-       int *c = (int*)count;
-       *c = atoi(argv[0]);
-       return 0;
-}
+       if (tac_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);
+       if (generateTAC(pkgId, binPath) != 0) {
+               return -1;
        }
-}
 
-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);
+       ///// TLC /////
+       if (tlc_createDB() != 0) {
+               return -1;
        }
+
+       copyLibraryCreateSymlink(pkgId, getLibrariesInfo(rootPath));
+
+       return 0;
 }
 
 int tacUpgrade(const std::string& pkgId, tac_state state, bool tacForce)
@@ -517,171 +666,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);
+       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(tac_db);
 
                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 +694,25 @@ 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(tac_db);
        }
 
        ///// 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;
-               }
-       }
-       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;
+       if (tlc_createDB() != 0) {
+               return -1;
        }
-       sqlite3_free(sql);
 
-       std::vector<std::string> librariesInfo;
-       if (!skipTLC) {
-               librariesInfo = getLibrariesInfo(rootPath);
+       if (tlc_deleteDB(pkgId) != 0) {
+               return -1;
        }
 
-       copyLibraryCreateSymlink(pkgId, librariesInfo, tlcDir);
-
-       tlc_updateDB(tlc_db, updateTlc, tlcDir);
+       copyLibraryCreateSymlink(pkgId, skipTLC ? std::vector<std::string>() : getLibrariesInfo(rootPath));
 
        return 0;
 }
@@ -764,68 +730,24 @@ 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);
+       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(tac_db);
 
        ///// 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;
-               }
-       }
-       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;
+       if (tlc_openDB() != 0) {
+               return -1;
        }
-       sqlite3_free(sql);
-
-       tlc_updateDB(tlc_db, updateTlc, concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR));
 
-       return 0;
+       return tlc_deleteDB(pkgId);
 }
 
 int tacRemoved(const std::string& pkgId)
@@ -863,7 +785,7 @@ void undoStep(std::string tac)
                }
        };
 
-       scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
+       scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0);
 }
 
 void install_Undo()
@@ -986,7 +908,7 @@ void cleanStep(std::string tac)
                }
        };
 
-       scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
+       scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0);
 }
 
 void install_Clean()