fix bugs which found by coverity
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
index b265ae8..e2cd5bd 100644 (file)
@@ -24,7 +24,6 @@
 #include <pkgmgr_installer_info.h>
 #include <sys/smack.h>
 #include <json/json.h>
-#include <openssl/sha.h>
 
 #include <cstdlib>
 #include <cstring>
 #include <unordered_map>
 #include <vector>
 #include <iterator>
+#include <fstream>
 #include <sstream>
 #include <map>
 
 #include "log.h"
 #include "utils.h"
 #include "path_manager.h"
-#include "db_manager.h"
-
-#define __XSTR(x) #x
-#define __STR(x) __XSTR(x)
-static const char* __TAC_DIR = __STR(TAC_DIR);
-#undef __STR
-#undef __XSTR
 
 static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
 {
@@ -603,122 +596,100 @@ bool removeAll(const bf::path& path) {
        return true;
 }
 
-static void SHA256(std::string path, char outputBuffer[65])
+//Parser the .deps.json file to get nuget information.
+std::vector<std::string> depsJsonParser(std::string rootPath, std::string execName, std::string tpaList)
 {
-       FILE *file = fopen(path.c_str(), "rb");
-       if (!file) {
-               return;
-       }
-
-       unsigned char hash[SHA256_DIGEST_LENGTH];
-       SHA256_CTX sha256;
-       SHA256_Init(&sha256);
-       int bytesRead = 0;
-       const int bufSize = 32768;
-       char *buffer = (char*)malloc(bufSize);
-       if (!buffer) {
-               fclose(file);
-               return;
-       }
+       std::vector<std::string> tpaAssemblies;
+       splitPath(tpaList, tpaAssemblies);
 
-       while ((bytesRead = fread(buffer, 1, bufSize, file))) {
-               SHA256_Update(&sha256, buffer, bytesRead);
-       }
-       SHA256_Final(hash, &sha256);
-       for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
-               sprintf(outputBuffer + (i * 2), "%02x", hash[i]);
-       }
-       outputBuffer[64] = 0;
-
-       fclose(file);
-       free(buffer);
-}
-
-std::vector<std::string> depsJsonParser(std::string pkgId, std::string rootPath, std::string execName, std::string tpaList, bool isTool, sqlite3 *tac_db)
-{
        std::vector<std::string> parserData;
        std::string depsJsonName = execName.substr(0, execName.rfind(".dll")) + ".deps.json";
        std::string depsJsonPath = concatPath(rootPath, depsJsonName);
-       std::string binPath = concatPath(rootPath, "bin");
-       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();
-                               return parserData;
-                       }
-                       const Json::Value runtimeTargetName = root["runtimeTarget"]["name"];
-                       const Json::Value nugetPackages = root["targets"][runtimeTargetName.asString().c_str()];
-                       for (auto& nuget : nugetPackages.getMemberNames()) {
-                               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) {
-                                       continue;
-                               } else {
-                                       const Json::Value assemblies = nugetPackages[nuget.c_str()]["runtime"];
-                                       if (assemblies != Json::nullValue) {
-                                               const Json::Value dependencies = nugetPackages[nuget.c_str()]["dependencies"];
-                                               bool isDependency = false;
-                                               for (auto& dependency : dependencies.getMemberNames()) {
-                                                       if (strstr(dependency.c_str(), TIZEN_DOTNET_NUGET) != NULL ||
-                                                               strstr(dependency.c_str(), NET_STANDARD_LIBRARY_NUGET) != NULL) {
-                                                               continue;
-                                                       } else {
-                                                               isDependency = true;
+       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);
                                                        }
                                                }
-                                               if (!isDependency) {
-                                                       bool isExistTpaAssembly = false;
-                                                       for (auto& assembly : assemblies.getMemberNames()) {
-                                                               std::string assembly_name = assembly.substr(assembly.rfind('/') + 1);
-                                                               if (strstr(replaceAll(tpaList, ".ni.dll", ".dll").c_str(), assembly_name.c_str()) != NULL) {
-                                                                       isExistTpaAssembly = true;
-                                                                       break;
+                                       }
+                               }
+                               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 (!isExistTpaAssembly) {
-                                                               _INFO("Nuget : %s", nuget.c_str());
-                                                               if (isTool) {
-                                                                       if (tac_db) {
-                                                                               std::string name = nuget.substr(0, nuget.find('/'));
-                                                                               std::string version = nuget.substr(nuget.rfind('/') + 1);
-                                                                               std::string sql = "INSERT INTO TAC (PKGID, NUGET, NAME, VERSION) " \
-                                                                               "VALUES ('" + pkgId + "', '" + nuget + "', '" + name + "', '" + version + "');";
-                                                                               dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql);
-                                                                               parserData.push_back(concatPath(__TAC_DIR, name));
-                                                                       } else {
-                                                                               std::string nugetPath = concatPath(__TAC_DIR, nuget);
-                                                                               if (bf::exists(nugetPath)) {
-                                                                                       for (auto& assembly : assemblies.getMemberNames()) {
-                                                                                               std::string assemblyName = assembly.substr(assembly.rfind('/') + 1);
-                                                                                               std::string originPath = concatPath(nugetPath, assemblyName);
-                                                                                               if (bf::exists(originPath)) {
-                                                                                                       parserData.push_back(originPath);
-                                                                                               }
-                                                                                       }
+                                                       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;
                                                                                }
                                                                        }
-                                                               } else {
+                                                                       if (isExistTpaAssembly) break;
+                                                               }
+                                                               if (!isExistTpaAssembly) {
                                                                        for (auto& assembly : assemblies.getMemberNames()) {
-                                                                               std::string assembly_name = assembly.substr(assembly.rfind('/') + 1);
-                                                                               char buffer[65] = {0};
-                                                                               SHA256(concatPath(binPath, assembly_name), buffer);
-                                                                               parserData.push_back(nuget + "/" + assembly_name + "/" + buffer);
-                                                                               _INFO("Assembly / SHA256 : %s / %s", assembly_name.c_str(), buffer);
+                                                                               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();
                        }
-                       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