Modify to enable TAC when restoring a database of TAC
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / tac_common.cc
index 78c20cf..0cce217 100644 (file)
  */
 
 #include <fstream>
+#include <json/json.h>
 #include <pkgmgr-info.h>
 #include <pkgmgr_installer_info.h>
 
 #include "log.h"
 #include "utils.h"
-#include "ni_common.h"
 #include "tac_common.h"
 #include "db_manager.h"
 #include "path_manager.h"
@@ -37,20 +37,21 @@ static const char* __TAC_DIR = __STR(TAC_DIR);
 #undef __XSTR
 
 static sqlite3 *tac_db = NULL;
-std::vector<std::string> restoreNuget;
+std::vector<std::string> restore_nuget;
 
-void cleanupDirectory()
+static void cleanupDirectory()
 {
        std::vector<std::string> removeNuget;
        try {
                for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
                        bool isExist = false;
                        std::string nugetPath = nuget.path().string();
-                       for (auto& restore : restoreNuget) {
+                       for (auto& restore : restore_nuget) {
                                if (!bf::is_directory(nugetPath)) {
                                        isExist = true;
                                }
-                               if (strstr(nugetPath.c_str(), restore.c_str()) != NULL) {
+                               if (!strcmp(nugetPath.c_str(), restore.c_str()) ||
+                                       !strcmp(nugetPath.c_str(), restore.substr(0, restore.rfind('/')).c_str())) {
                                        isExist = true;
                                        break;
                                }
@@ -62,12 +63,12 @@ void cleanupDirectory()
 
                for (auto& rm : removeNuget) {
                        if (!removeAll(rm)) {
-                               _ERR("Failed to remove of %s", rm.c_str());
+                               fprintf(stderr, "Failed to remove of %s\n", rm.c_str());
                        }
                }
                removeNuget.clear();
        } catch (const bf::filesystem_error& error) {
-               _ERR("Failed to recursive directory: %s", error.what());
+               fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
                return;
        }
 }
@@ -87,6 +88,8 @@ static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
                return -1;
        }
 
+       enableTACPackage(std::string(pkgId));
+
        ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
        if (ret != PMINFO_R_OK) {
                fprintf(stderr, "Failed to get root path\n");
@@ -102,9 +105,14 @@ static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
        execName = std::string(exec).substr(std::string(exec).rfind('/') + 1);
 
        std::vector<std::string> parserData;
+       std::string binDir = concatPath(rootPath, "bin");
+       std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
        for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) {
                std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
-               parserData.push_back(nugetPackage);
+               std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
+               if (bf::exists(tacDir) && bf::exists(concatPath(tacDir, assemblyName))) {
+                       parserData.push_back(nugetPackage);
+               }
        }
        std::sort(parserData.begin(), parserData.end());
        parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end());
@@ -116,7 +124,7 @@ static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
                        std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
                                                "VALUES ('" + std::string(pkgId) + "', '" + nuget + "', '" + name + "', '" + version + "');";
                        dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql);
-                       restoreNuget.push_back(concatPath(__TAC_DIR, name));
+                       restore_nuget.push_back(concatPath(__TAC_DIR, nuget));
                }
        }
        parserData.clear();
@@ -126,27 +134,24 @@ static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData)
 tac_error_e restoreTACDB()
 {
        if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
-               fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
+               fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
                return TAC_ERROR_UNKNOWN;
        }
 
        std::string dbRestoreJournal = TAC_APP_LIST_RESTORE_DB + std::string("-journal");
        if (!removeFile(dbRestoreJournal)) {
-               fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
+               fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }
 
        if (initializePathManager(std::string(), std::string(), std::string())) {
-               fprintf(stderr, "Fail to initialize PathManger");
+               fprintf(stderr, "Fail to initialize PathManger\n");
                return TAC_ERROR_UNKNOWN;
        }
 
        tac_db = dbCreate(TAC_APP_LIST_RESTORE_DB);
-       if (tac_db) {
-               if (!dbOpen(tac_db, TAC_APP_LIST_RESTORE_DB)) {
-                       return TAC_ERROR_UNKNOWN;
-               }
-       } else {
+       if (!tac_db) {
+               fprintf(stderr, "Sqlite create error\n");
                return TAC_ERROR_UNKNOWN;
        }
 
@@ -177,26 +182,36 @@ tac_error_e restoreTACDB()
                tac_db = NULL;
        }
 
+       uid_t g_uid = 301; // app_fw
+       gid_t g_gid = 301; // app_fw
+
        if (!copyFile(TAC_APP_LIST_RESTORE_DB, TAC_APP_LIST_DB)) {
-               fprintf(stderr, "Failed to copy of %s", TAC_APP_LIST_DB);
+               fprintf(stderr, "Failed to copy of %s\n", TAC_APP_LIST_DB);
                return TAC_ERROR_UNKNOWN;
        }
        if (!removeFile(TAC_APP_LIST_RESTORE_DB)) {
-               fprintf(stderr, "Failed to remove of %s", TAC_APP_LIST_RESTORE_DB);
+               fprintf(stderr, "Failed to remove of %s\n", TAC_APP_LIST_RESTORE_DB);
                return TAC_ERROR_UNKNOWN;
        }
+       if (chown(TAC_APP_LIST_DB, g_uid, g_gid) == -1) {
+               fprintf(stderr, "Failed to change owner and group name\n");
+       }
 
        std::string dbJournal = TAC_APP_LIST_DB + std::string("-journal");
        if (!copyFile(dbRestoreJournal, dbJournal)) {
-               fprintf(stderr, "Failed to copy of %s", dbJournal.c_str());
+               fprintf(stderr, "Failed to copy of %s\n", dbJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }
        if (!removeFile(dbRestoreJournal)) {
-               fprintf(stderr, "Failed to remove of %s", dbRestoreJournal.c_str());
+               fprintf(stderr, "Failed to remove of %s\n", dbRestoreJournal.c_str());
                return TAC_ERROR_UNKNOWN;
        }
+       if (chown(dbJournal.c_str(), g_uid, g_gid) == -1) {
+               fprintf(stderr, "Failed to change owner and group name\n");
+       }
 
        cleanupDirectory();
+
        return TAC_ERROR_NONE;
 }
 
@@ -222,85 +237,19 @@ tac_error_e resetTACPackage(const std::string& pkgId)
                        }
                        for (auto& path : tacNativeImage) {
                                if (!removeFile(path)) {
-                                       fprintf(stderr, "Failed to remove of %s", path.c_str());
+                                       fprintf(stderr, "Failed to remove of %s\n", path.c_str());
                                        return TAC_ERROR_UNKNOWN;
                                }
                        }
                        tacNativeImage.clear();
                } catch (const bf::filesystem_error& error) {
-                       _ERR("Failed to recursive directory: %s", error.what());
+                       fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
                        return TAC_ERROR_UNKNOWN;
                }
        }
        return TAC_ERROR_NONE;
 }
 
-tac_error_e createTACPackage(const std::string& pkgId)
-{
-       std::string pkgRoot;
-       if (getRootPath(pkgId, pkgRoot) < 0) {
-               return TAC_ERROR_INVALID_PACKAGE;
-       }
-
-       std::string binDir = concatPath(pkgRoot, "bin");
-       std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
-       std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
-       if (bf::exists(tacDir)) {
-               try {
-                       for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) {
-                               std::string symPath = symlinkAssembly.path().string();
-                               if (bf::is_symlink(symPath)) {
-                                       if (!isNativeImage(symPath)) {
-                                               std::string originPath = bf::read_symlink(symPath).string();
-                                               std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
-                                               if (!bf::exists(originNiPath)) {
-                                                       if(createNiDll(originPath, false) != NI_ERROR_NONE) {
-                                                               fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
-                                                               return TAC_ERROR_UNKNOWN;
-                                                       }
-                                               }
-                                               std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
-                                               if (!bf::exists(symNIPath)) {
-                                                       bf::create_symlink(originNiPath, symNIPath);
-                                                       fprintf(stderr, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
-                                                       updateAssemblyInfo(tacDir.c_str(), symNIPath.c_str(), true);
-
-                                                       std::string NIFileName = symNIPath.substr(symNIPath.rfind('/') + 1);
-                                                       if (!removeFile(concatPath(binNIDir, NIFileName))) {
-                                                               fprintf(stderr, "Failed to remove of %s\n", concatPath(binNIDir, NIFileName).c_str());
-                                                               return TAC_ERROR_UNKNOWN;
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-               } catch (const bf::filesystem_error& error) {
-                       _ERR("Failed to recursive directory: %s", error.what());
-                       return TAC_ERROR_UNKNOWN;
-               }
-       }
-       return TAC_ERROR_NONE;
-}
-
-tac_error_e regenerateTAC()
-{
-       const std::string tacDir[] = {__TAC_DIR};
-       removeNiUnderDirs(tacDir, 1);
-
-       auto convert = [](const std::string& path, std::string name) {
-               if (strstr(path.c_str(), TAC_APP_LIST_DB) != NULL ||
-                       strstr(path.c_str(), TAC_APP_LIST_RESTORE_DB) != NULL ||
-                       strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
-                       return;
-               if(createNiDll(path, false) != NI_ERROR_NONE) {
-                       fprintf(stderr, "Failed to create NI file [%s]\n", path.c_str());
-                       return;
-               }
-       };
-       scanFilesInDir(tacDir[0], convert, -1);
-       return TAC_ERROR_NONE;
-}
-
 tac_error_e disableTACPackage(const std::string& pkgId)
 {
        std::string pkgRoot;
@@ -340,7 +289,7 @@ tac_error_e disableTACPackage(const std::string& pkgId)
                                return TAC_ERROR_UNKNOWN;
                        }
                } catch (const bf::filesystem_error& error) {
-                       _ERR("Failed to recursive directory: %s", error.what());
+                       fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
                        return TAC_ERROR_UNKNOWN;
                }
        }
@@ -365,7 +314,7 @@ tac_error_e enableTACPackage(const std::string& pkgId)
        }
 
        if (initializePathManager(std::string(), std::string(), std::string())) {
-               fprintf(stderr, "Fail to initialize PathManger");
+               fprintf(stderr, "Fail to initialize PathManger\n");
                return TAC_ERROR_UNKNOWN;
        }
 
@@ -422,7 +371,7 @@ tac_error_e enableTACPackage(const std::string& pkgId)
                        }
                        if (enableNuget.empty()) {
                                if (!removeAll(tacDir)) {
-                                       _ERR("Failed to remove of %s", tacDir.c_str());
+                                       fprintf(stderr, "Failed to remove of %s\n", tacDir.c_str());
                                }
                        }
                        enableNuget.clear();
@@ -432,3 +381,101 @@ tac_error_e enableTACPackage(const std::string& pkgId)
        }
        return TAC_ERROR_NONE;
 }
+
+//Parser the .deps.json file to get nuget information.
+std::vector<std::string> depsJsonParser(std::string rootPath, std::string execName, std::string tpaList)
+{
+       std::vector<std::string> tpaAssemblies;
+       splitPath(tpaList, tpaAssemblies);
+
+       std::vector<std::string> parserData;
+       std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
+       std::string depsJsonPath = concatPath(rootPath, depsJsonName);
+       try {
+               if (bf::exists(depsJsonPath)) {
+                       std::ifstream ifs(depsJsonPath);
+                       Json::CharReaderBuilder reader;
+                       Json::Value root;
+                       std::string error;
+                       if (ifs.is_open()) {
+                               if (!Json::parseFromStream(reader, ifs, &root, &error)) {
+                                       _ERR("Failed to parse of deps.json");
+                                       ifs.close();
+                                       tpaAssemblies.clear();
+                                       return parserData;
+                               }
+                               const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
+                               const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
+                               std::vector<std::string> appDependencies;
+                               for (auto& nuget : nugetPackages.getMemberNames()) {
+                                       if (strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) != NULL ||
+                                               strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) != NULL) {
+                                               const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
+                                               if (assemblies != Json::nullValue) {
+                                                       const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
+                                                       for (auto& dependency : dependencies.getMemberNames()) {
+                                                               appDependencies.push_back(dependency);
+                                                       }
+                                               }
+                                       }
+                               }
+                               for (auto& nuget : nugetPackages.getMemberNames()) {
+                                       //Skip the nuget package related to Tizen
+                                       if (strstr(nuget.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
+                                               strstr(nuget.c_str(), TIZEN_DOTNET_SDK_NUGET) == NULL &&
+                                               strstr(nuget.c_str(), (execName.substr(0, execName.find(".Tizen."))).c_str()) == NULL &&
+                                               strstr(nuget.c_str(), (execName.substr(0, execName.find(".dll"))).c_str()) == NULL) {
+                                               const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
+                                               if (assemblies != Json::nullValue) {
+                                                       const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
+                                                       bool hasDependency = false;
+                                                       for (auto& dependency : dependencies.getMemberNames()) {
+                                                               //Skip the nugget package that is dependent on another nuget package
+                                                               if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) == NULL &&
+                                                                       strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) == NULL) {
+                                                                       hasDependency = true;
+                                                                       for (auto& ad : appDependencies) {
+                                                                               if (!strcmp(ad.c_str(), dependency.c_str())) {
+                                                                                       hasDependency = true;
+                                                                                       break;
+                                                                               } else {
+                                                                                       hasDependency = false;
+                                                                               }
+                                                                       }
+                                                                       if (hasDependency) break;
+                                                               }
+                                                       }
+                                                       if (!hasDependency) {
+                                                               bool isExistTpaAssembly = false;
+                                                               for (auto& assembly : assemblies.getMemberNames()) {
+                                                                       std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
+                                                                       //Skip the assembly present in the TPA list
+                                                                       for (auto& tpa : tpaAssemblies) {
+                                                                               if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
+                                                                                       isExistTpaAssembly = true;
+                                                                                       break;
+                                                                               }
+                                                                       }
+                                                                       if (isExistTpaAssembly) break;
+                                                               }
+                                                               if (!isExistTpaAssembly) {
+                                                                       for (auto& assembly : assemblies.getMemberNames()) {
+                                                                               std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
+                                                                               parserData.push_back(nuget + ":" + assemblyName);
+                                                                               _INFO("Nuget : [%s] / Assembly : [%s]", nuget.c_str(), assemblyName.c_str());
+                                                                       }
+                                                               }
+                                                       }
+                                               }
+                                       }
+                               }
+                               appDependencies.clear();
+                               ifs.close();
+                       }
+               }
+       } catch (const Json::LogicError& error) {
+               _ERR("Failed to parse Json: %s", error.what());
+       }
+       tpaAssemblies.clear();
+       return parserData;
+}