fix bugs which found by coverity
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
index 92075bb..e2cd5bd 100644 (file)
@@ -23,6 +23,7 @@
 #include <pkgmgr-info.h>
 #include <pkgmgr_installer_info.h>
 #include <sys/smack.h>
+#include <json/json.h>
 
 #include <cstdlib>
 #include <cstring>
@@ -30,6 +31,7 @@
 #include <unordered_map>
 #include <vector>
 #include <iterator>
+#include <fstream>
 #include <sstream>
 #include <map>
 
@@ -223,6 +225,20 @@ std::string baseName(const std::string& path)
        return path;
 }
 
+std::string replaceAll(const std::string &str, const std::string &pattern, const std::string &replace)
+{
+       std::string result = str;
+       std::string::size_type pos = 0;
+       std::string::size_type offset = 0;
+
+       while ((pos = result.find(pattern, offset)) != std::string::npos) {
+               result.replace(result.begin() + pos, result.begin() + pos + pattern.size(), replace);
+               offset = pos + replace.size();
+       }
+
+       return result;
+}
+
 bool isFileExist(const std::string& path)
 {
        struct stat sb;
@@ -579,3 +595,101 @@ bool removeAll(const bf::path& path) {
        }
        return true;
 }
+
+//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;
+}
\ No newline at end of file