[Refactoring] Improve SAM score through DC and GV improvement
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / tac_common.cc
index 874b4b8..f7fb9ef 100644 (file)
@@ -38,8 +38,8 @@ static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
 
 static sqlite3 *tac_db = NULL;
 static sqlite3 *tlc_db = NULL;
-std::vector<std::string> restore_nuget;
-std::vector<std::string> restore_library;
+static std::vector<std::string> restore_nuget;
+static std::vector<std::string> restore_library;
 
 static void cleanupDirectory()
 {
@@ -252,6 +252,24 @@ tac_error_e disableTACPackage(const std::string& pkgId)
        return TAC_ERROR_NONE;
 }
 
+static tac_error_e createSymlinkFile(const std::string& tacDir, const std::string& binDir, const std::string& from, const std::string& to)
+{
+       bs::error_code error;
+       bf::create_symlink(from, concatPath(tacDir, to), error);
+       if (error) {
+               _SERR("Failed to create symlink %s file", concatPath(tacDir, to).c_str());
+               return TAC_ERROR_UNKNOWN;
+       }
+       _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, to).c_str());
+       copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, to).c_str(), true);
+
+       if (!removeFile(concatPath(binDir, to))) {
+               _SERR("Failed to remove of %s", concatPath(binDir, to).c_str());
+               return TAC_ERROR_UNKNOWN;
+       }
+       return TAC_ERROR_NONE;
+}
+
 tac_error_e enableTACPackage(const std::string& pkgId)
 {
        std::string rootPath = getRootPath(pkgId);
@@ -278,76 +296,61 @@ tac_error_e enableTACPackage(const std::string& pkgId)
                return TAC_ERROR_INVALID_PACKAGE;
        }
 
-       if (!strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) {
-               std::string binDir = concatPath(rootPath, "bin");
-               std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
-               std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
-               if (!exist(tacDir)) {
-                       if (!createDir(tacDir)) {
-                               _SERR("Cannot create directory: %s", tacDir.c_str());
-                               return TAC_ERROR_UNKNOWN;
-                       }
-                       copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
-
-                       std::vector<std::string> enableNuget;
-                       for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
-                               std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
-                               std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
-                               std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
-                               if (exist(nugetPath)) {
-                                       std::string originPath = concatPath(nugetPath, assemblyName);
-                                       if (exist(originPath)) {
-                                               enableNuget.push_back(originPath);
-                                       }
-                               }
-                       }
+       if (strcmp(metaValue.c_str(), METADATA_VALUE_TRUE)) {
+               _SERR("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str());
+               return TAC_ERROR_NONE;
+       }
 
-                       bs::error_code error;
-                       for (auto& originPath : enableNuget) {
-                               if (exist(originPath)) {
-                                       std::string fileName = originPath.substr(originPath.rfind('/') + 1);
-                                       std::string NIFileName = changeExtension(fileName, "dll", "ni.dll");
-                                       if (exist(binNIDir)) {
-                                               std::string originNIPath = changeExtension(originPath, "dll", "ni.dll");
-                                               if (exist(originNIPath)) {
-                                                       bf::create_symlink(originNIPath, concatPath(tacDir, NIFileName), error);
-                                                       if (error) {
-                                                               _SERR("Failed to create symlink %s file", concatPath(tacDir, NIFileName).c_str());
-                                                               return TAC_ERROR_UNKNOWN;
-                                                       }
-                                                       _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, NIFileName).c_str());
-                                                       copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, NIFileName).c_str(), true);
+       std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
+       std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
+       if (exist(tacDir)) {
+               return TAC_ERROR_NONE;
+       }
 
-                                                       if (!removeFile(concatPath(binNIDir, NIFileName))) {
-                                                               _SERR("Failed to remove of %s", concatPath(binNIDir, NIFileName).c_str());
-                                                               return TAC_ERROR_UNKNOWN;
-                                                       }
-                                               }
-                                       }
-                                       bf::create_symlink(originPath, concatPath(tacDir, fileName), error);
-                                       if (error) {
-                                               _SERR("Failed to create symlink %s file", concatPath(tacDir, fileName).c_str());
-                                               return TAC_ERROR_UNKNOWN;
-                                       }
-                                       _SOUT("%s symbolic link file generated successfully.", concatPath(tacDir, fileName).c_str());
-                                       copySmackAndOwnership(tacDir.c_str(), concatPath(tacDir, fileName).c_str(), true);
+       if (!createDir(tacDir)) {
+               _SERR("Cannot create directory: %s", tacDir.c_str());
+               return TAC_ERROR_UNKNOWN;
+       }
+       copySmackAndOwnership(binDir.c_str(), tacDir.c_str());
+
+       std::vector<std::string> enableNuget;
+       for (auto& npAssembly : depsJsonParser(rootPath, execName)) {
+               std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':'));
+               std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1);
+               std::string nugetPath = concatPath(__DOTNET_DIR, nugetPackage);
+               if (exist(nugetPath)) {
+                       std::string originPath = concatPath(nugetPath, assemblyName);
+                       if (exist(originPath)) {
+                               enableNuget.push_back(originPath);
+                       }
+               }
+       }
+
+       if (enableNuget.empty()) {
+               if (!removeAll(tacDir)) {
+                       _SERR("Failed to remove of %s", tacDir.c_str());
+               }
+               return TAC_ERROR_NONE;
+       }
 
-                                       if (!removeFile(concatPath(binDir, fileName))) {
-                                               _SERR("Failed to remove of %s", concatPath(binDir, fileName).c_str());
+       for (auto& originPath : enableNuget) {
+               if (exist(originPath)) {
+                       std::string fileName = originPath.substr(originPath.rfind('/') + 1);
+                       if (exist(binNIDir)) {
+                               std::string originNIPath = changeExtension(originPath, "dll", "ni.dll");
+                               if (exist(originNIPath)) {
+                                       if (createSymlinkFile(tacDir, binNIDir, originNIPath, changeExtension(fileName, "dll", "ni.dll")) != TAC_ERROR_NONE) {
                                                return TAC_ERROR_UNKNOWN;
                                        }
                                }
                        }
-                       if (enableNuget.empty()) {
-                               if (!removeAll(tacDir)) {
-                                       _SERR("Failed to remove of %s", tacDir.c_str());
-                               }
+                       if (createSymlinkFile(tacDir, binDir, originPath, fileName) != TAC_ERROR_NONE) {
+                               return TAC_ERROR_UNKNOWN;
                        }
-                       enableNuget.clear();
                }
-       } else {
-               _SERR("The metadata key is missing or the metadata value is false of [%s]", pkgId.c_str());
        }
+       enableNuget.clear();
+
        return TAC_ERROR_NONE;
 }
 
@@ -371,19 +374,6 @@ std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::
                                }
                                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 &&
@@ -392,36 +382,15 @@ std::vector<std::string> depsJsonParser(const std::string& rootPath, const std::
                                                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) {
-                                                               // handle assembly even though that is included in the TPA.
-                                                               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());
-                                                               }
+                                                       // handle assembly even though that is included in the TPA.
+                                                       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();
                        }
                }
@@ -439,7 +408,7 @@ std::vector<std::string> getLibrariesInfo(const std::string& rootPath)
                return LibrariesInfo;
 
        auto convert = [&LibrariesInfo](const std::string& filepath, const std::string& filename) {
-               if (filepath.rfind(".so") != std::string::npos) {
+               if (filename.find(".so", filename.size() - 3) != std::string::npos || filepath.rfind(".so.") != std::string::npos) {
                        std::string buffer = SHA256(filepath);
                        LibrariesInfo.push_back(filepath + ":" + buffer);
                        _INFO("Library : [%s] / SHA256 : [%s]", filename.c_str(), buffer.c_str());
@@ -562,7 +531,7 @@ tac_error_e tlc_restoreDB()
                }
        };
 
-       scanFilesInDirectory(concatPath(__DOTNET_DIR, TLC_LIBRARIES_DIR), convert, 0);
+       scanFilesInDirectory(TLC_LIBRARIES_DIR, convert, 0);
 
        return TAC_ERROR_NONE;
 }