From 46e0e046275cc61bef04da504a094c47f34f6310 Mon Sep 17 00:00:00 2001 From: "j-h.choi" Date: Wed, 24 Jul 2019 08:59:08 +0900 Subject: [PATCH] Verify that the assembly is in the TPA list. --- NativeLauncher/inc/utils.h | 6 +- .../installer-plugin/prefer_nuget_cache_plugin.cc | 164 +++++++++++++-------- NativeLauncher/tool/ni_common.cc | 28 +++- NativeLauncher/tool/tac_common.cc | 43 +++++- NativeLauncher/util/path_manager.cc | 69 ++++----- NativeLauncher/util/plugin_manager.cc | 51 ++++--- NativeLauncher/util/utils.cc | 116 ++++----------- 7 files changed, 254 insertions(+), 223 deletions(-) diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index 6baf0ad..40db9d8 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -21,7 +21,6 @@ #include #include #include -#include #include @@ -219,14 +218,11 @@ bool removeAll(const bf::path& path); /** * @brief .deps.json file parser - * @param[in] package id * @param[in] root path * @param[in] exec name * @param[in] tpa list - * @param[in] dotnettool - * @param[in] sqlite3 * return std::vector parser data */ -std::vector depsJsonParser(std::string pkgId, std::string rootPath, std::string execName, std::string tpaList, bool isTool = false, sqlite3 *tac_db = NULL); +std::vector depsJsonParser(std::string rootPath, std::string execName, std::string tpaList); #endif /* __UTILS_H__ */ diff --git a/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc b/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc index 1d2f985..1f817f0 100644 --- a/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc +++ b/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc @@ -29,6 +29,7 @@ #include #include #include +#include #ifdef LOG_TAG #undef LOG_TAG @@ -117,19 +118,55 @@ bool appTypeCheck(std::string pkgId) return isDotnetAppType; } +void SHA256(std::string path, char outputBuffer[65]) +{ + 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; + } + + 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); +} + int createSymlink(std::string tac_version_dir, std::string np) { uid_t uid = 0; + if (pkgmgr_installer_info_get_target_uid(&uid) < 0) { + _ERR("Failed to get UID"); + return -1; + } + std::string tac_dir = concatPath(binPath, TAC_SYMLINK_SUB_DIR); if (!createDir(tac_dir)) { - _INFO("Cannot create directory: %s", tac_dir.c_str()); + _ERR("Cannot create directory: %s", tac_dir.c_str()); return -1; } 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 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 (bf::exists(concatPath(binPath, assembly))) { if (isCreateDirectory) { @@ -138,10 +175,6 @@ int createSymlink(std::string tac_version_dir, std::string np) return -1; } } - if (pkgmgr_installer_info_get_target_uid(&uid) < 0) { - _ERR("Failed to get UID"); - return -1; - } bf::create_symlink(concatPath(tac_version_dir, assembly), concatPath(tac_dir, assembly)); if (lchown(concatPath(tac_dir, assembly).c_str(), uid, 0)) { _ERR("Failed to change owner of: %s", concatPath(tac_dir, assembly).c_str()); @@ -157,6 +190,20 @@ int createSymlink(std::string tac_version_dir, std::string np) return 0; } +void depsJsonCheck() { + 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); + char buffer[65] = {0}; + SHA256(concatPath(binPath, assemblyName), buffer); + nugetPackagesAssembliesSha.push_back(nugetPackage + ":" + assemblyName + ":" + buffer); + _INFO("Assembly : [%s] / SHA256 : [%s]", assemblyName.c_str(), buffer); + } + std::sort(tacDB.begin(), tacDB.end()); + tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end()); +} + extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list) { _DBG("[===== PKGMGR_MDPARSER_PLUGIN_INSTALL =====]"); @@ -175,14 +222,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app binPath = concatPath(rootPath, "bin"); } if (metadataCheck(list)) { - nugetPackagesAssembliesSha = depsJsonParser(pkgId, rootPath, execName, getTPA()); - 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('/')); - tacDB.push_back(nuget_package); - } - std::sort(tacDB.begin(), tacDB.end()); - tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end()); + depsJsonCheck(); } status = "install"; @@ -198,7 +238,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app } if (tacDB.empty()) { - _ERR("Not exist .deps.json file"); + _INFO("Not exist .deps.json file"); return 0; } @@ -222,12 +262,12 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app 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); + 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; + ofs << assembly << ":" << sha << std::endl; assembly_count++; } } @@ -247,17 +287,17 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app 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); + 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())) { + if (!strcmp(get_str.c_str(), (assembly + ":" + sha).c_str())) { compare_count++; } sha256_count = get_str; @@ -268,7 +308,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app } 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", tac_name.c_str()); + _INFO("Same nuget : %s", np.c_str()); if (createSymlink(tac_version_dir, np) < 0) { _ERR("Failed to create symlink"); return -1; @@ -277,7 +317,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app "VALUES ('" + std::string(pkgId) + "', '" + np + "', '" + tac_name + "', '" + tac_version + "');"; dbInsert(tac_db, TAC_APP_LIST_DB, sql); } else { - _INFO("Different nuget : %s", tac_name.c_str()); + _INFO("Different nuget : %s", np.c_str()); } } if (!bf::exists(sha256_info)) { @@ -351,14 +391,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app _INFO("Skipped to parse of deps.json"); } else { if (metadataCheck(list)) { - nugetPackagesAssembliesSha = depsJsonParser(pkgId, rootPath, execName, getTPA()); - 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('/')); - tacDB.push_back(nuget_package); - } - std::sort(tacDB.begin(), tacDB.end()); - tacDB.erase(unique(tacDB.begin(), tacDB.end()), tacDB.end()); + depsJsonCheck(); } } @@ -401,20 +434,20 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app } isCreateDirectory = true; createDirectories.push_back(tac_version_dir); - std::ofstream ofs2(sha256_info, std::ios::app); + 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); + 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())) { - ofs2 << assembly << ";" << sha << std::endl; + ofs << assembly << ":" << sha << std::endl; assembly_count++; } } - ofs2 << assembly_count << std::endl; - ofs2.close(); + ofs << assembly_count << std::endl; + ofs.close(); if (createSymlink(tac_version_dir, np) < 0) { _ERR("Failed to create symlink"); return -1; @@ -441,29 +474,29 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app 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); + 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 ifs2(sha256_info); + std::ifstream ifs(sha256_info); std::string get_str; - if (ifs2.is_open()) { - while (getline(ifs2, get_str)) { - if (!strcmp(get_str.c_str(), (assembly + ";" + sha).c_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; } - ifs2.close(); + 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", tac_name.c_str()); + _INFO("Same nuget : %s", np.c_str()); if (createSymlink(tac_version_dir, np) < 0) { _ERR("Failed to create symlink"); return -1; @@ -485,7 +518,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app dbInsert(tac_db, TAC_APP_LIST_DB, sql); } } else { - _INFO("Different nuget : %s", tac_name.c_str()); + _INFO("Different nuget : %s", np.c_str()); } } if (!bf::exists(sha256_info)) { @@ -502,7 +535,6 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app break; } } - if (!isExits) { std::string sql = "DELETE FROM TAC WHERE PKGID = '" + std::string(pkgId) + "' AND NUGET = '" + unp + "';"; dbDelete(tac_db, TAC_APP_LIST_DB, sql); @@ -558,9 +590,10 @@ void cleanStep(std::string tac) { std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/'))); for (auto& bck : bf::recursive_directory_iterator(current_tac)) { - if (bf::is_directory(bck.path()) && strstr(bck.path().c_str(), ".bck") != NULL) { - if (!removeAll(bck.path().string())) { - _ERR("Failed to remove of %s", bck.path().c_str()); + std::string bck_path = bck.path().string(); + if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) { + if (!removeAll(bck_path)) { + _ERR("Failed to remove of %s", bck_path.c_str()); } break; } @@ -568,7 +601,8 @@ void cleanStep(std::string tac) bool isExist = false; for (auto& bck : bf::recursive_directory_iterator(current_tac)) { - if (bf::exists(bck.path()) && bf::is_directory(bck.path()) && strstr(bck.path().c_str(), ".bck") == NULL) { + std::string bck_path = bck.path().string(); + if (bf::exists(bck_path) && bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") == NULL) { isExist = true; break; } @@ -625,10 +659,10 @@ void undoStep(std::string tac) { std::string current_tac = concatPath(__TAC_DIR, tac.substr(0, tac.find('/'))); for (auto& bck : bf::recursive_directory_iterator(current_tac)) { - if (bf::is_directory(bck.path()) && strstr(bck.path().c_str(), ".bck") != NULL) { - if (!moveFile(bck.path(), bck.path().string().substr(0, bck.path().string().rfind(".bck")))) { - _ERR("Failed to move %s to %s", - bck.path().c_str(), bck.path().string().substr(0, bck.path().string().rfind(".bck")).c_str()); + std::string bck_path = bck.path().string(); + if (bf::is_directory(bck_path) && strstr(bck_path.c_str(), ".bck") != NULL) { + if (!moveFile(bck_path, bck_path.substr(0, bck_path.rfind(".bck")))) { + _ERR("Failed to move %s", bck_path.c_str()); } break; } diff --git a/NativeLauncher/tool/ni_common.cc b/NativeLauncher/tool/ni_common.cc index ce2cff1..69c2dcf 100644 --- a/NativeLauncher/tool/ni_common.cc +++ b/NativeLauncher/tool/ni_common.cc @@ -453,10 +453,20 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, if (appPaths.back() == ':') appPaths.pop_back(); - auto convert = [&appPaths, enableR2R, isAppNI](const std::string& path, const char* name) { + std::vector tpaAssemblies; + splitPath(__tpa, tpaAssemblies); + + auto convert = [&appPaths, enableR2R, isAppNI, tpaAssemblies](const std::string& path, const char* name) { if (isAppNI) { std::string assembly = path.substr(path.rfind('/') + 1); - if (strstr(replaceAll(__tpa, ".ni.dll", ".dll").c_str(), assembly.c_str()) != NULL) { + bool isExist = false; + for (auto& tpa : tpaAssemblies) { + if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) { + isExist = true; + break; + } + } + if (isExist) { fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", path.c_str()); return; } @@ -469,6 +479,8 @@ void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, for (int i = 0; i < count; i++) { scanFilesInDir(rootPaths[i], convert, 1); } + + tpaAssemblies.clear(); } ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R) @@ -522,7 +534,17 @@ ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& return NI_ERROR_NONE; } else { std::string assembly = dllPath.substr(dllPath.rfind('/') + 1); - if (strstr(replaceAll(__tpa, ".ni.dll", ".dll").c_str(), assembly.c_str()) != NULL) { + std::vector tpaAssemblies; + splitPath(__tpa, tpaAssemblies); + bool isExist = false; + for (auto& tpa : tpaAssemblies) { + if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) { + isExist = true; + break; + } + } + tpaAssemblies.clear(); + if (isExist) { fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", dllPath.c_str()); return NI_ERROR_NONE; } else { diff --git a/NativeLauncher/tool/tac_common.cc b/NativeLauncher/tool/tac_common.cc index 7b3dfeb..1f9c76b 100644 --- a/NativeLauncher/tool/tac_common.cc +++ b/NativeLauncher/tool/tac_common.cc @@ -17,7 +17,6 @@ #include #include #include -#include #include "log.h" #include "utils.h" @@ -39,24 +38,24 @@ static const char* __TAC_DIR = __STR(TAC_DIR); static sqlite3 *tac_db = NULL; std::vector restoreNuget; -std::vector enableNuget; void cleanupDirectory() { std::vector removeNuget; for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) { bool isExist = false; + std::string nugetPath = nuget.path().string(); for (auto& restore : restoreNuget) { - if (!bf::is_directory(nuget.path())) { + if (!bf::is_directory(nugetPath)) { isExist = true; } - if (strstr(nuget.path().c_str(), restore.c_str()) != NULL) { + if (strstr(nugetPath.c_str(), restore.c_str()) != NULL) { isExist = true; break; } } if (!isExist) { - removeNuget.push_back(nuget.path().string()); + removeNuget.push_back(nugetPath); } } @@ -97,7 +96,25 @@ static int restoreDBCb(pkgmgrinfo_appinfo_h handle, void *userData) } execName = std::string(exec).substr(std::string(exec).rfind('/') + 1); - restoreNuget = depsJsonParser(pkgId, rootPath, execName, getTPA(), true, tac_db); + std::vector parserData; + for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) { + std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':')); + parserData.push_back(nugetPackage); + } + std::sort(parserData.begin(), parserData.end()); + parserData.erase(unique(parserData.begin(), parserData.end()), parserData.end()); + + for (auto& nuget : parserData) { + 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 ('" + std::string(pkgId) + "', '" + nuget + "', '" + name + "', '" + version + "');"; + dbInsert(tac_db, TAC_APP_LIST_RESTORE_DB, sql); + restoreNuget.push_back(concatPath(__TAC_DIR, name)); + } + } + parserData.clear(); return 0; } @@ -341,7 +358,18 @@ tac_error_e enableTACPackage(const std::string& pkgId) } updateAssemblyInfo(binDir.c_str(), tacDir.c_str()); - enableNuget = depsJsonParser(pkgId, rootPath, execName, getTPA(), true); + std::vector enableNuget; + for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) { + std::string nugetPackage = npAssembly.substr(0, npAssembly.rfind(':')); + std::string assemblyName = npAssembly.substr(npAssembly.rfind(':') + 1); + std::string nugetPath = concatPath(__TAC_DIR, nugetPackage); + if (bf::exists(nugetPath)) { + std::string originPath = concatPath(nugetPath, assemblyName); + if (bf::exists(originPath)) { + enableNuget.push_back(originPath); + } + } + } for (auto& originPath : enableNuget) { if (bf::exists(originPath)) { @@ -375,6 +403,7 @@ tac_error_e enableTACPackage(const std::string& pkgId) _ERR("Failed to remove of %s", tacDir.c_str()); } } + enableNuget.clear(); } } else { fprintf(stderr, "The metadata key is missing or the metadata value is false of [%s]\n", pkgId.c_str()); diff --git a/NativeLauncher/util/path_manager.cc b/NativeLauncher/util/path_manager.cc index b10372f..b207209 100644 --- a/NativeLauncher/util/path_manager.cc +++ b/NativeLauncher/util/path_manager.cc @@ -47,47 +47,49 @@ bool initializedPathManager = false; // on success, return 0. otherwise return -1. int initializePathManager(const std::string& runtimeDir, const std::string& tizenFXDir, const std::string& extraDir) { - if (!initializedPathManager) { - __dllPath = new DllPath(); - if (!__dllPath) { - _ERR("fail to allocate memory for dll path structure\n"); - return -1; - } - - if (!runtimeDir.empty()) { - __dllPath->runtime_dir = absolutePath(runtimeDir); - } else { - __dllPath->runtime_dir = absolutePath(__RUNTIME_DIR); - } + if (initializedPathManager) { + _INFO("Path manager already initialized"); + return 0; + } - if (!tizenFXDir.empty()) { - __dllPath->tizenfx_dir = absolutePath(tizenFXDir); - } else { - char* tmp = vconf_get_str(__TIZEN_API_PATH_KEY); - if (tmp) { - __dllPath->tizenfx_dir = std::string(tmp); - _DBG("Device API Directory is set by vconf : %s", tmp); - } else { - __dllPath->tizenfx_dir = absolutePath(__DEVICE_API_DIR); - } - } + __dllPath = new DllPath(); + if (!__dllPath) { + _ERR("fail to allocate memory for dll path structure\n"); + return -1; + } - __dllPath->tizenfx_ref_dir = __dllPath->tizenfx_dir + "/ref"; + if (!runtimeDir.empty()) { + __dllPath->runtime_dir = absolutePath(runtimeDir); + } else { + __dllPath->runtime_dir = absolutePath(__RUNTIME_DIR); + } - // ":" seperated extra directories - if (!extraDir.empty()) { - splitPath(extraDir, __dllPath->extra_dirs); + if (!tizenFXDir.empty()) { + __dllPath->tizenfx_dir = absolutePath(tizenFXDir); + } else { + char* tmp = vconf_get_str(__TIZEN_API_PATH_KEY); + if (tmp) { + __dllPath->tizenfx_dir = std::string(tmp); + _DBG("Device API Directory is set by vconf : %s", tmp); } else { - char* extraPath = pluginGetDllPath(); - if (extraPath) { - splitPath(extraPath, __dllPath->extra_dirs); - } + __dllPath->tizenfx_dir = absolutePath(__DEVICE_API_DIR); } + } + + __dllPath->tizenfx_ref_dir = __dllPath->tizenfx_dir + "/ref"; - _INFO("Path manager initialize success"); + // ":" seperated extra directories + if (!extraDir.empty()) { + splitPath(extraDir, __dllPath->extra_dirs); } else { - _INFO("Skip to initialize Path manager"); + char* extraPath = pluginGetDllPath(); + if (extraPath) { + splitPath(extraPath, __dllPath->extra_dirs); + } } + + _INFO("Path manager initialize success"); + initializedPathManager = true; return 0; } @@ -98,6 +100,7 @@ void finalizePathManager() delete __dllPath; __dllPath = NULL; } + initializedPathManager = false; } diff --git a/NativeLauncher/util/plugin_manager.cc b/NativeLauncher/util/plugin_manager.cc index 91529e4..88cd6d4 100644 --- a/NativeLauncher/util/plugin_manager.cc +++ b/NativeLauncher/util/plugin_manager.cc @@ -25,34 +25,36 @@ bool initializedPluginManager = false; int initializePluginManager(const char* mode) { - if (!initializedPluginManager) { - if (isFileExist(PLUGIN_PATH)) { - __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE); - if (__pluginLib) { - __pluginFunc = (PluginFunc*)calloc(sizeof(PluginFunc), 1); - if (!__pluginFunc) { - _ERR("fail to allocate memory for plugin function structure"); - return -1; - } - __pluginFunc->initialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize"); - __pluginFunc->preload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload"); - __pluginFunc->has_log_control = (plugin_has_log_control_ptr)dlsym(__pluginLib, "plugin_has_log_control"); - __pluginFunc->set_app_info = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info"); - __pluginFunc->set_coreclr_info = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info"); - __pluginFunc->get_dll_path = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path"); - __pluginFunc->get_tpa = (plugin_get_tpa_ptr)dlsym(__pluginLib, "plugin_get_tpa"); - __pluginFunc->before_execute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute"); - __pluginFunc->finalize = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize"); - } + if (initializedPluginManager) { + _INFO("Plugin manager already initialized"); + return 0; + } - if (__pluginFunc->initialize) - __pluginFunc->initialize(mode); + if (isFileExist(PLUGIN_PATH)) { + __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE); + if (__pluginLib) { + __pluginFunc = (PluginFunc*)calloc(sizeof(PluginFunc), 1); + if (!__pluginFunc) { + _ERR("fail to allocate memory for plugin function structure"); + return -1; + } + __pluginFunc->initialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize"); + __pluginFunc->preload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload"); + __pluginFunc->has_log_control = (plugin_has_log_control_ptr)dlsym(__pluginLib, "plugin_has_log_control"); + __pluginFunc->set_app_info = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info"); + __pluginFunc->set_coreclr_info = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info"); + __pluginFunc->get_dll_path = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path"); + __pluginFunc->get_tpa = (plugin_get_tpa_ptr)dlsym(__pluginLib, "plugin_get_tpa"); + __pluginFunc->before_execute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute"); + __pluginFunc->finalize = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize"); } - _INFO("Plugin manager initialize success"); - } else { - _INFO("Skip to initialize Plugin manager"); + if (__pluginFunc->initialize) + __pluginFunc->initialize(mode); } + + _INFO("Plugin manager initialize success"); + initializedPluginManager = true; return 0; } @@ -69,6 +71,7 @@ void finalizePluginManager() dlclose(__pluginLib); __pluginLib = NULL; } + initializedPluginManager = false; } diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index b265ae8..45b2bad 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -32,19 +31,13 @@ #include #include #include +#include #include #include #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,43 +596,15 @@ 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 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; - } - - 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; + std::vector tpaAssemblies; + splitPath(tpaList, tpaAssemblies); - fclose(file); - free(buffer); -} - -std::vector depsJsonParser(std::string pkgId, std::string rootPath, std::string execName, std::string tpaList, bool isTool, sqlite3 *tac_db) -{ std::vector 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; @@ -649,68 +614,46 @@ std::vector depsJsonParser(std::string pkgId, std::string rootPath, 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()]; 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 { + //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 isDependency = false; + bool hasDependency = 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; + //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; } } - if (!isDependency) { + if (!hasDependency) { 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; + 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) { - _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); - } - } - } - } - } else { - 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); - } + 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()); } } } @@ -720,5 +663,6 @@ std::vector depsJsonParser(std::string pkgId, std::string rootPath, ifs.close(); } } + tpaAssemblies.clear(); return parserData; } \ No newline at end of file -- 2.7.4