Code cleanup (#251)
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / prefer_nuget_cache_plugin.cc
index f204e72..769f2f6 100644 (file)
 
 #define __XSTR(x) #x
 #define __STR(x) __XSTR(x)
-static const char* __TAC_DIR = __STR(TAC_DIR);
+static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
 #undef __STR
 #undef __XSTR
 
-typedef struct Metadata {
-       const char *key;
-       const char *value;
-} Metadata;
-
 std::vector<std::string> nugetPackagesAssembliesSha;
 std::vector<std::string> tacDB;
 std::vector<std::string> createDirectories;
 std::vector<std::string> updateTac;
 std::string status = "";
-std::string rootPath;
-std::string execName;
-std::string binPath;
 static sqlite3 *tac_db = NULL;
+bool tacPluginInstalled = false;
+bool tacPluginFinished = false;
 
-bool metadataCheck(GList *list)
-{
-       GList *tag = NULL;
-       Metadata *mdInfo = NULL;
-       tag = g_list_first(list);
-       mdInfo = (Metadata*)tag->data;
-       if (strcmp(mdInfo->key, TAC_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) {
-               _DBG("Prefer nuget cache set TRUE");
-               if (initializePluginManager("normal")) {
-                       _ERR("Fail to initialize PluginManager");
-                       return false;
-               }
-               if (initializePathManager(std::string(), std::string(), std::string())) {
-                       _ERR("Fail to initialize PathManger");
-                       return false;
-               }
-               return true;
-       }
-       return false;
-}
-
-bool appTypeCheck(std::string pkgId)
-{
-       uid_t uid = 0;
-       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
-               _ERR("Failed to get UID");
-               return false;
-       }
-
-       pkgmgrinfo_pkginfo_h handle;
-       int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
-       if (ret != PMINFO_R_OK) {
-               _ERR("Failed to get pkg info");
-               return false;
-       }
-
-       bool isDotnetAppType = false;
-       auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int {
-               char* type = nullptr;
-               bool* dotnet = static_cast<bool*>(userData);
-               if (pkgmgrinfo_appinfo_get_apptype(handle, &type) != PMINFO_R_OK) {
-                       _ERR("Failed to get app type : %s", type);
-                       return -1;
-               }
-               if (strcmp(type, "dotnet") == 0) {
-                       *dotnet = true;
-               }
-               return 0;
-       };
-
-       if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &isDotnetAppType, uid) != PMINFO_R_OK) {
-               _ERR("Failed to get list of app in pkg : %s", pkgId.c_str());
-               pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-               return false;
-       }
-
-       pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
-       return isDotnetAppType;
-}
-
-void SHA256(std::string path, char outputBuffer[65])
+static void SHA256(std::string path, char outputBuffer[65])
 {
        FILE *file = fopen(path.c_str(), "rb");
        if (!file) {
@@ -149,10 +83,62 @@ void SHA256(std::string path, char outputBuffer[65])
        free(buffer);
 }
 
-int copyNCreateSymlink(std::string tac_version_dir, std::string np, bool is_create_tac_dir)
+static void createSHA256Info(std::string sha256Info, std::string nugetPackage)
+{
+       std::ofstream ofs(sha256Info, std::ios::app);
+       int assembly_count = 0;
+       for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
+               std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
+               std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
+               std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
+               std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
+               if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
+                       ofs << assembly << ":" << sha << std::endl;
+                       assembly_count++;
+               }
+       }
+       ofs << assembly_count << std::endl;
+       ofs.close();
+}
+
+static int compareSHA256Info(std::string sha256Info, std::string nugetPackage)
+{
+       int compare_count = 0;
+       int assembly_count = 0;
+       std::string sha256_count = "0";
+       for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
+               std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
+               std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
+               std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
+               std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
+               if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
+                       assembly_count++;
+                       std::ifstream ifs(sha256Info);
+                       std::string get_str;
+                       if (ifs.is_open()) {
+                               while (getline(ifs, get_str)) {
+                                       if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
+                                               compare_count++;
+                                       }
+                                       sha256_count = get_str;
+                               }
+                               ifs.close();
+                       }
+               }
+       }
+       if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
+               !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
+               _INFO("Same nuget : %s", nugetPackage.c_str());
+               return 1;
+       }
+       return 0;
+}
+
+static int copyNCreateSymlink(std::string binPath, std::string tacVersionDir, std::string nugetPackage, bool isCreateTacDir)
 {
-       uid_t uid = 0;
-       if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+       uid_t g_uid = 0;
+       gid_t g_gid = 0;
+       if (pkgmgr_installer_info_get_target_uid(&g_uid) < 0) {
                _ERR("Failed to get UID");
                return -1;
        }
@@ -167,16 +153,16 @@ int copyNCreateSymlink(std::string tac_version_dir, std::string np, bool is_crea
                std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
                std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
                std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
-               if (!strcmp(nuget_package.c_str(), np.c_str())) {
+               if (!strcmp(nuget_package.c_str(), nugetPackage.c_str())) {
                        if (bf::exists(concatPath(binPath, assembly))) {
-                               if (is_create_tac_dir) {
-                                       if (!copyFile(concatPath(binPath, assembly), concatPath(tac_version_dir, assembly))) {
+                               if (isCreateTacDir) {
+                                       if (!copyFile(concatPath(binPath, assembly), concatPath(tacVersionDir, assembly))) {
                                                _ERR("Failed to copy of %s", assembly.c_str());
                                                return -1;
                                        }
                                }
-                               bf::create_symlink(concatPath(tac_version_dir, assembly), concatPath(tac_dir, assembly));
-                               if (lchown(concatPath(tac_dir, assembly).c_str(), uid, 0)) {
+                               bf::create_symlink(concatPath(tacVersionDir, assembly), concatPath(tac_dir, assembly));
+                               if (lchown(concatPath(tac_dir, assembly).c_str(), g_uid, g_gid)) {
                                        _ERR("Failed to change owner of: %s", concatPath(tac_dir, assembly).c_str());
                                        return -1;
                                }
@@ -190,15 +176,16 @@ int copyNCreateSymlink(std::string tac_version_dir, std::string np, bool is_crea
        return 0;
 }
 
-void depsJsonCheck() {
+static void depsJsonCheck(std::string rootPath, std::string binPath, std::string execName)
+{
        for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
-               std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
-               std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
-               tacDB.push_back(nugetPackage);
+               std::string nuget_package = npAssembly.substr(0, npAssembly.rfind(':'));
+               std::string assembly_name = npAssembly.substr(npAssembly.rfind(':') + 1);
+               tacDB.push_back(nuget_package);
                char buffer[65] = {0};
-               SHA256(concatPath(binPath, assemblyName), buffer);
-               nugetPackagesAssembliesSha.push_back(nugetPackage + ":" + assemblyName + ":" + buffer);
-               _INFO("Assembly : [%s] / SHA256 : [%s]", assemblyName.c_str(), buffer);
+               SHA256(concatPath(binPath, assembly_name), buffer);
+               nugetPackagesAssembliesSha.push_back(nuget_package + ":" + assembly_name + ":" + buffer);
+               _INFO("Assembly : [%s] / SHA256 : [%s]", assembly_name.c_str(), buffer);
        }
        std::sort(tacDB.begin(), tacDB.end());
        tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end());
@@ -209,31 +196,46 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app
        _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]");
        _INFO("PackageID : %s", pkgId);
 
-       if (!appTypeCheck(std::string(pkgId))) {
-               _INFO("App type is not dotnet");
+       // Can be multiple apps in one package
+       if (tacPluginInstalled) {
+               _INFO("TAC plugin already installed");
                return 0;
        }
-       if (getExecName(std::string(pkgId), execName) < 0) {
+       tacPluginInstalled = true;
+
+       if (initializePathManager(std::string(), std::string(), std::string())) {
+               _ERR("Fail to initialize PathManger");
                return 0;
        }
-       if (getRootPath(std::string(pkgId), rootPath) < 0) {
+
+       std::string appType = getAppType(std::string(pkgId));
+       if (strstr(appType.c_str(), "dotnet") == NULL) {
+               _ERR("App type is not dotnet");
                return 0;
-       } else {
-               binPath = concatPath(rootPath, "bin");
        }
-       if (metadataCheck(list)) {
-               depsJsonCheck();
+       std::string execName = getExecName(std::string(pkgId));
+       std::string rootPath = getRootPath(std::string(pkgId));
+       if (execName.empty() || rootPath.empty()) {
+               return 0;
+       }
+       std::string binPath = concatPath(rootPath, "bin");
+       std::string metaValue = getMetadataValue(std::string(pkgId), TAC_METADATA_KEY);
+       if (metaValue.empty()) {
+               return 0;
+       }
+       if (metaValue == METADATA_VALUE) {
+               depsJsonCheck(rootPath, binPath, execName);
        }
 
        status = "install";
-       tac_db = dbCreate(TAC_APP_LIST_DB);
+       tac_db = dbCreate(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
        if (!tac_db) {
                _ERR("Sqlite create error");
                return 0;
        }
 
        if (tacDB.empty()) {
-               _INFO("Not exist .deps.json file");
+               _INFO("Not exist data for TAC in %s", pkgId);
                return 0;
        }
 
@@ -243,7 +245,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app
                _INFO("TAC name : %s", tac_name.c_str());
                _INFO("TAC version : %s", tac_version.c_str());
 
-               std::string tac_version_dir = concatPath(__TAC_DIR, np);
+               std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
                std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
                if (!bf::exists(tac_version_dir)) {
                        _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
@@ -252,69 +254,47 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app
                                return 0;
                        }
                        createDirectories.push_back(tac_version_dir);
-                       std::ofstream ofs(sha256_info, std::ios::app);
-                       int assembly_count = 0;
-                       for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
-                               std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
-                               std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
-                               std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
-                               std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
-                               if (!strcmp(nuget_package.c_str(), np.c_str())) {
-                                       ofs << assembly << ":" << sha << std::endl;
-                                       assembly_count++;
-                               }
+                       if (!bf::is_symlink(sha256_info)) {
+                               createSHA256Info(sha256_info, np);
+                       } else {
+                               _ERR("Failed to create sha256_info. Symbolic link is detected");
+                               return -1;
                        }
-                       ofs << assembly_count << std::endl;
-                       ofs.close();
 
-                       if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
+                       if (copyNCreateSymlink(binPath, tac_version_dir, np, true) < 0) {
                                _ERR("Failed to create symlink");
                                return -1;
                        }
-                       std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
-                                       "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
+
+                       char *sql = sqlite3_mprintf(
+                               "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
+                               "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
                        dbInsert(tac_db, TAC_APP_LIST_DB, sql);
+                       sqlite3_free(sql);
                } else {
                        _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
-                       int compare_count = 0;
-                       int assembly_count = 0;
-                       std::string sha256_count = "0";
-                       for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
-                               std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
-                               std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
-                               std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
-                               std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
-                               if (!strcmp(nuget_package.c_str(), np.c_str())) {
-                                       assembly_count++;
-                                       std::ifstream ifs(sha256_info);
-                                       std::string get_str;
-                                       if (ifs.is_open()) {
-                                               while (getline(ifs, get_str)) {
-                                                       if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
-                                                               compare_count++;
-                                                       }
-                                                       sha256_count = get_str;
-                                               }
-                                               ifs.close();
+                       if (!bf::is_symlink(sha256_info)) {
+                               if (compareSHA256Info(sha256_info, np)) {
+                                       if (copyNCreateSymlink(binPath, tac_version_dir, np, false) < 0) {
+                                               _ERR("Failed to create symlink");
+                                               return -1;
                                        }
+
+                                       char *sql = sqlite3_mprintf(
+                                               "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
+                                               "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
+                                       dbInsert(tac_db, TAC_APP_LIST_DB, sql);
+                                       sqlite3_free(sql);
+                               } else {
+                                       _INFO("Different nuget : %s", np.c_str());
                                }
-                       }
-                       if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
-                               !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
-                               _INFO("Same nuget : %s", np.c_str());
-                               if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
-                                       _ERR("Failed to create symlink");
-                                       return -1;
-                               }
-                               std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
-                                               "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
-                               dbInsert(tac_db, TAC_APP_LIST_DB, sql);
                        } else {
-                               _INFO("Different nuget : %s", np.c_str());
+                               _ERR("Failed to create sha256_info. Symbolic link is detected");
+                               return -1;
                        }
                }
                if (!bf::exists(sha256_info)) {
-                       if(!removeAll(tac_version_dir)) {
+                       if (!removeAll(tac_version_dir)) {
                                _ERR("Failed to remove of %s", tac_version_dir.c_str());
                        }
                }
@@ -322,34 +302,39 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app
        return 0;
 }
 
-static int sqliteCb(void *count, int argc, char **argv, char **colName) {
+static int sqliteCb(void *count, int argc, char **argv, char **colName)
+{
        int *c = (int*)count;
        *c = atoi(argv[0]);
        return 0;
 }
 
-int updateTacDB(sqlite3 *sqlite)
+static int updateTacDB(sqlite3 *sqlite)
 {
        for (auto& unp : updateTac) {
                int count = -1;
-               std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE NUGET = '" + unp + "';";
-               int ret = sqlite3_exec(sqlite, sql.c_str(), sqliteCb, &count, NULL);
+               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);
                        return -1;
                }
                if (count == 0) {
-                       std::string tac_version_dir_prev = concatPath(__TAC_DIR, unp);
+                       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);
                                return -1;
                        }
                        if (!removeAll(tac_version_dir_prev)) {
                                _ERR("Failed to remove of %s", tac_version_dir_prev.c_str());
+                               sqlite3_free(sql);
                                return -1;
                        }
                }
+               sqlite3_free(sql);
        }
        return 0;
 }
@@ -359,42 +344,62 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app
        _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UPGRADE =====]");
        _INFO("PackageID : %s", pkgId);
 
-       if (!appTypeCheck(std::string(pkgId))) {
-               _INFO("App type is not dotnet");
+       // Can be multiple apps in one package
+       if (tacPluginInstalled) {
+               _INFO("TAC plugin already upgraded");
                return 0;
        }
-       if (getExecName(std::string(pkgId), execName) < 0) {
+       tacPluginInstalled = true;
+
+       if (initializePathManager(std::string(), std::string(), std::string())) {
+               _ERR("Fail to initialize PathManger");
                return 0;
        }
-       if (getRootPath(std::string(pkgId), rootPath) < 0) {
+
+       std::string appType = getAppType(std::string(pkgId));
+       if (strstr(appType.c_str(), "dotnet") == NULL) {
+               _ERR("App type is not dotnet");
+               return 0;
+       }
+       std::string execName = getExecName(std::string(pkgId));
+       std::string rootPath = getRootPath(std::string(pkgId));
+       if (execName.empty() || rootPath.empty()) {
                return 0;
-       } else {
-               binPath = concatPath(rootPath, "bin");
        }
+       std::string binPath = concatPath(rootPath, "bin");
+
        if (!strcmp("removed", status.c_str())) {
                _INFO("Skipped to parse of deps.json");
        } else {
-               if (metadataCheck(list)) {
-                       depsJsonCheck();
+               std::string metaValue = getMetadataValue(std::string(pkgId), TAC_METADATA_KEY);
+               if (metaValue.empty()) {
+                       return 0;
+               }
+               if (metaValue == METADATA_VALUE) {
+                       depsJsonCheck(rootPath, binPath, execName);
                }
        }
 
        status = "update";
-       tac_db = dbCreate(TAC_APP_LIST_DB);
+       tac_db = dbCreate(TAC_APP_LIST_DB, CREATE_TAC_DB_TABLE);
        if (!tac_db) {
                _ERR("Sqlite open error");
                return 0;
        }
 
-       std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
+       char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
        updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
+       sqlite3_free(sql);
 
        if (tacDB.empty()) {
-               sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
+               sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
                dbDelete(tac_db, TAC_APP_LIST_DB, sql);
+               sqlite3_free(sql);
                if (updateTacDB(tac_db) < 0) {
                        return -1;
                }
+               _INFO("Not exist data for TAC in %s", pkgId);
+               return 0;
        } else {
                for (auto& np : tacDB) {
                        std::string tac_name = np.substr(0, np.find('/'));
@@ -402,7 +407,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app
                        _INFO("TAC name : %s", tac_name.c_str());
                        _INFO("TAC version : %s", tac_version.c_str());
 
-                       std::string tac_version_dir = concatPath(__TAC_DIR, np);
+                       std::string tac_version_dir = concatPath(__DOTNET_DIR, np);
                        std::string sha256_info = concatPath(tac_version_dir, TAC_SHA_256_INFO);
                        if (!bf::exists(tac_version_dir)) {
                                _INFO("Create tac_version_dir [%s]", tac_version_dir.c_str());
@@ -411,95 +416,83 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app
                                        return 0;
                                }
                                createDirectories.push_back(tac_version_dir);
-                               std::ofstream ofs(sha256_info, std::ios::app);
-                               int assembly_count = 0;
-                               for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
-                                       std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
-                                       std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
-                                       std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
-                                       std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
-                                       if (!strcmp(nuget_package.c_str(), np.c_str())) {
-                                               ofs << assembly << ":" << sha << std::endl;
-                                               assembly_count++;
-                                       }
+                               if (!bf::is_symlink(sha256_info)) {
+                                       createSHA256Info(sha256_info, np);
+                               } else {
+                                       _ERR("Failed to create sha256_info. Symbolic link is detected");
+                                       return -1;
                                }
-                               ofs << assembly_count << std::endl;
-                               ofs.close();
-                               if (copyNCreateSymlink(tac_version_dir, np, true) < 0) {
+
+                               if (copyNCreateSymlink(binPath, tac_version_dir, np, true) < 0) {
                                        _ERR("Failed to create symlink");
                                        return -1;
                                }
 
                                int count = -1;
-                               sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
-                               int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
+                               sql = sqlite3_mprintf(
+                                               "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, 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);
                                        return -1;
                                }
+                               sqlite3_free(sql);
                                if (count == 1) {
-                                       sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
+                                       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, tac_name.c_str());
                                        dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
+                                       sqlite3_free(sql);
                                } else if (count == 0) {
-                                       sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
-                                               "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
+                                       sql = sqlite3_mprintf(
+                                               "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
+                                               "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
                                        dbInsert(tac_db, TAC_APP_LIST_DB, sql);
+                                       sqlite3_free(sql);
                                }
                        } else {
                                _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str());
-                               int compare_count = 0;
-                               int assembly_count = 0;
-                               std::string sha256_count = "0";
-                               for (auto& npAssemblySha : nugetPackagesAssembliesSha) {
-                                       std::string nuget_package_assembly = npAssemblySha.substr(0, npAssemblySha.rfind(':'));
-                                       std::string nuget_package = nuget_package_assembly.substr(0, nuget_package_assembly.rfind(':'));
-                                       std::string assembly = nuget_package_assembly.substr(nuget_package_assembly.rfind(':') + 1);
-                                       std::string sha = npAssemblySha.substr(npAssemblySha.rfind(':') + 1);
-                                       if (!strcmp(nuget_package.c_str(), np.c_str())) {
-                                               assembly_count++;
-                                               std::ifstream ifs(sha256_info);
-                                               std::string get_str;
-                                               if (ifs.is_open()) {
-                                                       while (getline(ifs, get_str)) {
-                                                               if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) {
-                                                                       compare_count++;
-                                                               }
-                                                               sha256_count = get_str;
-                                                       }
-                                                       ifs.close();
+                               if (!bf::is_symlink(sha256_info)) {
+                                       if (compareSHA256Info(sha256_info, np)) {
+                                               if (copyNCreateSymlink(binPath, tac_version_dir, np, false) < 0) {
+                                                       _ERR("Failed to create symlink");
+                                                       return -1;
                                                }
-                                       }
-                               }
 
-                               if (!strcmp(std::to_string(assembly_count).c_str(), std::to_string(compare_count).c_str()) &&
-                                       !strcmp(std::to_string(assembly_count).c_str(), sha256_count.c_str())) {
-                                       _INFO("Same nuget : %s", np.c_str());
-                                       if (copyNCreateSymlink(tac_version_dir, np, false) < 0) {
-                                               _ERR("Failed to create symlink");
-                                               return -1;
-                                       }
-
-                                       int count = -1;
-                                       std::string sql = "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
-                                       int ret = sqlite3_exec(tac_db, sql.c_str(), sqliteCb, &count, NULL);
-                                       if (ret != SQLITE_OK) {
-                                               _ERR("Sqlite select error");
-                                               return -1;
-                                       }
-                                       if (count == 1) {
-                                               sql = "UPDATE TAC SET NAME = '" + tac_name + "', VERSION = '" + tac_version + "', NUGET = '" + np + "' WHERE PKGID = '" + std::string(pkgId) + "' AND NAME = '" + tac_name + "';";
-                                               dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
-                                       } else if (count == 0) {
-                                               sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
-                                                       "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');";
-                                               dbInsert(tac_db, TAC_APP_LIST_DB, sql);
+                                               int count = -1;
+                                               char *sql = sqlite3_mprintf(
+                                                       "SELECT COUNT(NUGET) FROM TAC WHERE PKGID = %Q AND NAME = %Q;", pkgId, 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);
+                                                       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, tac_name.c_str());
+                                                       dbUpdate(tac_db, TAC_APP_LIST_DB, sql);
+                                                       sqlite3_free(sql);
+                                               } else if (count == 0) {
+                                                       sql = sqlite3_mprintf(
+                                                               "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
+                                                               "VALUES (%Q, %Q, %Q, %Q);", pkgId, np.c_str(), tac_name.c_str(), tac_version.c_str());
+                                                       dbInsert(tac_db, TAC_APP_LIST_DB, sql);
+                                                       sqlite3_free(sql);
+                                               }
+                                       } else {
+                                               _INFO("Different nuget : %s", np.c_str());
                                        }
                                } else {
-                                       _INFO("Different nuget : %s", np.c_str());
+                                       _ERR("Failed to create sha256_info. Symbolic link is detected");
+                                       return -1;
                                }
                        }
                        if (!bf::exists(sha256_info)) {
-                               if(!removeAll(tac_version_dir)) {
+                               if (!removeAll(tac_version_dir)) {
                                        _ERR("Failed to remove of %s", tac_version_dir.c_str());
                                }
                        }
@@ -513,8 +506,9 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app
                                }
                        }
                        if (!isExits) {
-                               std::string sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NUGET = '" + unp + "';";
+                               char *sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q AND NUGET = %Q;", pkgId, unp.c_str());
                                dbDelete(tac_db, TAC_APP_LIST_DB, sql);
+                               sqlite3_free(sql);
                        }
                }
                if (updateTacDB(tac_db) < 0) {
@@ -529,6 +523,13 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *a
        _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNINSTALL =====]");
        _INFO("PackageID : %s", pkgId);
 
+       // Can be multiple apps in one package
+       if (tacPluginInstalled) {
+               _INFO("TAC plugin already uninstalled");
+               return 0;
+       }
+       tacPluginInstalled = true;
+
        status = "uninstall";
        tac_db = dbOpen(TAC_APP_LIST_DB);
        if (!tac_db) {
@@ -536,11 +537,13 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgId, const char *a
                return 0;
        }
 
-       std::string sql = "SELECT * FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
+       char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
        updateTac = dbSelect(tac_db, TAC_APP_LIST_DB, sql);
+       sqlite3_free(sql);
 
-       sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "';";
+       sql = sqlite3_mprintf("DELETE FROM TAC WHERE PKGID = %Q;", pkgId);
        dbDelete(tac_db, TAC_APP_LIST_DB, sql);
+       sqlite3_free(sql);
 
        if (updateTacDB(tac_db) < 0) {
                return -1;
@@ -560,7 +563,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_REMOVED(const char *pkgId, const char *app
 
 void cleanStep(std::string tac)
 {
-       std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
+       std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
        try {
                for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
                        std::string bck_path = bck.path().string();
@@ -618,6 +621,13 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId
        _DBG("[===== PKGMGR_MDPARSER_PLUGIN_CLEAN =====]");
        _INFO("PackageID : %s", pkgId);
 
+       // Can be multiple apps in one package
+       if (tacPluginFinished) {
+               _INFO("TAC plugin already finished(CLEAN)");
+               return 0;
+       }
+       tacPluginFinished = true;
+
        if (tac_db) {
                dbClose(tac_db);
                tac_db = NULL;
@@ -634,7 +644,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_CLEAN(const char *pkgId, const char *appId
 
 void undoStep(std::string tac)
 {
-       std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/')));
+       std::string current_tac = concatPath(__DOTNET_DIR, tac.substr(0, tac.find('/')));
        try {
                for (auto& bck : bf::recursive_directory_iterator(current_tac)) {
                        std::string bck_path = bck.path().string();
@@ -683,6 +693,13 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UNDO(const char *pkgId, const char *appId,
        _DBG("[===== PKGMGR_MDPARSER_PLUGIN_UNDO =====]");
        _INFO("PackageID : %s", pkgId);
 
+       // Can be multiple apps in one package
+       if (tacPluginFinished) {
+               _INFO("TAC plugin already finished(UNDO)");
+               return 0;
+       }
+       tacPluginFinished = true;
+
        if (tac_db) {
                dbRollback(tac_db);
                tac_db = NULL;