From f9c1d06d8924b0a9a4897cf968e930f167603013 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=B5=9C=EC=A2=85=ED=97=8C/Common=20Platform=20Lab=28SR=29?= =?utf8?q?/Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 18 Jun 2020 13:11:11 +0900 Subject: [PATCH] Code cleanup (#251) Change-Id: Ibf9d8fe72f68384ab2f2431d926251e88f2ea9ff --- NativeLauncher/inc/ni_common.h | 8 -- NativeLauncher/inc/path_manager.h | 1 - NativeLauncher/inc/tac_common.h | 4 - NativeLauncher/inc/utils.h | 42 ++---- .../installer-plugin/prefer_dotnet_aot_plugin.cc | 36 ++--- .../installer-plugin/prefer_nuget_cache_plugin.cc | 145 +++++++------------- NativeLauncher/launcher/exec/launcher.cc | 3 +- NativeLauncher/launcher/exec/loader.cc | 1 - NativeLauncher/launcher/lib/dotnet_launcher.cc | 12 ++ NativeLauncher/tool/ni_common.cc | 56 ++++---- NativeLauncher/tool/nitool.cc | 5 + NativeLauncher/tool/tac_common.cc | 28 ++-- NativeLauncher/util/db_manager.cc | 2 +- NativeLauncher/util/log_manager.cc | 3 +- NativeLauncher/util/path_manager.cc | 3 +- NativeLauncher/util/plugin_manager.cc | 2 +- NativeLauncher/util/utils.cc | 146 ++++++++++++--------- 17 files changed, 231 insertions(+), 266 deletions(-) diff --git a/NativeLauncher/inc/ni_common.h b/NativeLauncher/inc/ni_common.h index 4107e87..d1ade3d 100644 --- a/NativeLauncher/inc/ni_common.h +++ b/NativeLauncher/inc/ni_common.h @@ -83,14 +83,6 @@ ni_error_e createNIPlatform(DWORD flags); ni_error_e createNIDll(const std::string& dllPath, DWORD flags); /** - * @brief create native images for TAC DLLs - * @param[i] rootPaths paths to TAC directories - * @param[i] count length of rootPaths - * @param[i] flags additional flags for the image generator - */ -void createNIUnderTAC(const std::string rootPaths[], int count, DWORD flags); - -/** * @brief creates a symbolic link file, the native image of TAC for specific package. * @param[i] pkgId package ID * @param[i] flags additional flags for the image generator diff --git a/NativeLauncher/inc/path_manager.h b/NativeLauncher/inc/path_manager.h index 2d6c1b7..6843c61 100644 --- a/NativeLauncher/inc/path_manager.h +++ b/NativeLauncher/inc/path_manager.h @@ -24,7 +24,6 @@ std::string getRuntimeDir(); std::string getTizenFXDir(); std::string getTizenFXRefDir(); std::string getExtraDirs(); - std::string getTPA(); #endif /* __DLL_PATH_MANAGER_H__ */ diff --git a/NativeLauncher/inc/tac_common.h b/NativeLauncher/inc/tac_common.h index 990164e..ae0a221 100644 --- a/NativeLauncher/inc/tac_common.h +++ b/NativeLauncher/inc/tac_common.h @@ -19,10 +19,6 @@ #include -#ifndef DWORD -#define DWORD uint32_t -#endif - typedef enum { TAC_ERROR_NONE = 0, TAC_ERROR_INVALID_PARAMETER = -1, diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index a25c62c..1c930ee 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -40,20 +40,6 @@ enum FSFlag : int { }; /** - * @brief an iterator to the begin element in the range that compares equal to option - * @param[in] begin element - * @param[in] end elment - * return return true when elements match - */ -bool cmdOptionExists(char** begin, char** end, const std::string& option); - -/** - * @brief get current executable path - * return std::string path - */ -std::string readSelfPath(); - -/** * @brief concat path with PATH_SEPARATOR * @param[in] destination path * @param[in] source path @@ -80,31 +66,38 @@ std::string getBaseName(const std::string& path); * @param[in] original string * @param[in] pattern to match * @param[in] replacement string - * return the modified string + * return std::string the modified string */ std::string replaceAll(const std::string& str, const std::string& pattern, const std::string& replace); /** * @brief get root path * @param[in] package id - * @param[out] root path + * return std::string root path */ -int getRootPath(const std::string& pkgId, std::string& rootPath); +std::string getRootPath(const std::string& pkgId); /** * @brief get exec name * @param[in] package id - * @param[out] exec name + * return std::string exec name + */ +std::string getExecName(const std::string& pkgId); + +/** + * @brief get app type + * @param[in] package id + * return std::string app type */ -int getExecName(const std::string& pkgId, std::string& execName); +std::string getAppType(const std::string& pkgId); /** * @brief get metadata value * @param[in] package id * @param[in] metadata key - * @param[out] metadata value + * return std::string metadata value */ -int getMetadataValue(const std::string& pkgId, const std::string& metadataKey, std::string& metadataValue); +std::string getMetadataValue(const std::string& pkgId, const std::string& key); /** * @brief split path with ":" delimiter and put that in the vector @@ -128,13 +121,6 @@ bool isFile(const std::string& path); bool isDirectory(const std::string& path); /** - * @brief get file size - * @param[in] source path - * @return size of file - */ -uintptr_t getFileSize(const std::string& path); - -/** * @brief check the file is managed assembly or not. * @param[in] file path * @return return true when the file is managed assembly. diff --git a/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc b/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc index b337820..17a0718 100644 --- a/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc +++ b/NativeLauncher/installer-plugin/prefer_dotnet_aot_plugin.cc @@ -29,11 +29,6 @@ #endif #define LOG_TAG "DOTNET_INSTALLER_PLUGIN" -typedef struct Metadata { - const char *key; - const char *value; -} Metadata; - bool aotPluginInstalled = false; extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *appId, GList *list) @@ -53,20 +48,15 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app } } - GList *tag = NULL; - bool mdValue = false; - Metadata *mdInfo = NULL; - tag = g_list_first(list); - while (tag) { - mdInfo = (Metadata*)tag->data; - if (strcmp(mdInfo->key, AOT_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) { - _DBG("Prefer dotnet application AOT set TRUE"); - mdValue = true; - } - tag = g_list_next(tag); + std::string metaValue = getMetadataValue(std::string(pkgId), AOT_METADATA_KEY); + if (metaValue.empty()) { + _ERR("Failed to get metadata from [%s]", pkgId); + return 0; } - if (mdValue) { + if (metaValue == METADATA_VALUE) { + _DBG("Prefer dotnet application AOT set TRUE"); + NICommonOption option = {std::string(), std::string(), std::string()}; if (initNICommon(&option) < 0) { _ERR("Fail to initialize NI Common"); @@ -80,20 +70,20 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app _INFO("Complete make application to native image"); } - std::string pkgRoot; - if (getRootPath(pkgId, pkgRoot) < 0) { + std::string rootPath = getRootPath(std::string(pkgId)); + if (rootPath.empty()) { _ERR("Failed to get root path from [%s]", pkgId); - return 0; + return -1; } - std::string binDir = concatPath(pkgRoot, "bin"); + std::string binDir = concatPath(rootPath, "bin"); std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR); if (bf::exists(tacDir)) { uid_t g_uid = 0; gid_t g_gid = 0; if (pkgmgr_installer_info_get_target_uid(&g_uid) < 0) { _ERR("Failed to get UID"); - return 0; + return -1; } try { for (auto& symlinkAssembly : bf::recursive_directory_iterator(tacDir)) { @@ -102,7 +92,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app std::string originPath = bf::read_symlink(symPath).string(); std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll"; if (!bf::exists(originNIPath)) { - if(createNIDllUnderPkgRoot(pkgId, originPath, 0) != NI_ERROR_NONE) { + if (createNIDllUnderPkgRoot(pkgId, originPath, 0) != NI_ERROR_NONE) { _ERR("Failed to create NI file [%s]", originPath.c_str()); return -1; } diff --git a/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc b/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc index 8ae9e00..769f2f6 100644 --- a/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc +++ b/NativeLauncher/installer-plugin/prefer_nuget_cache_plugin.cc @@ -43,84 +43,16 @@ static const char* __DOTNET_DIR = __STR(DOTNET_DIR); #undef __STR #undef __XSTR -typedef struct Metadata { - const char *key; - const char *value; -} Metadata; - std::vector nugetPackagesAssembliesSha; std::vector tacDB; std::vector createDirectories; std::vector updateTac; std::string status = ""; -std::string rootPath; -std::string execName; -std::string binPath; static sqlite3 *tac_db = NULL; bool tacPluginInstalled = false; bool tacPluginFinished = false; -bool metadataCheck(GList *list) -{ - GList *tag = NULL; - Metadata *mdInfo = NULL; - tag = g_list_first(list); - mdInfo = (Metadata*)tag->data; - if (strcmp(mdInfo->key, TAC_METADATA_KEY) == 0 && strcmp(mdInfo->value, METADATA_VALUE) == 0) { - _DBG("Prefer nuget cache set TRUE"); - if (initializePluginManager("normal")) { - _ERR("Fail to initialize PluginManager"); - return false; - } - if (initializePathManager(std::string(), std::string(), std::string())) { - _ERR("Fail to initialize PathManger"); - return false; - } - return true; - } - return false; -} - -bool appTypeCheck(std::string pkgId) -{ - uid_t uid = 0; - if (pkgmgr_installer_info_get_target_uid(&uid) < 0) { - _ERR("Failed to get UID"); - return false; - } - - pkgmgrinfo_pkginfo_h handle; - int ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle); - if (ret != PMINFO_R_OK) { - _ERR("Failed to get pkg info"); - return false; - } - - bool isDotnetAppType = false; - auto dotnetAppCounter = [] (pkgmgrinfo_appinfo_h handle, void *userData) -> int { - char* type = nullptr; - bool* dotnet = static_cast(userData); - if (pkgmgrinfo_appinfo_get_apptype(handle, &type) != PMINFO_R_OK) { - _ERR("Failed to get app type : %s", type); - return -1; - } - if (strcmp(type, "dotnet") == 0) { - *dotnet = true; - } - return 0; - }; - - if (pkgmgrinfo_appinfo_get_usr_list(handle, PMINFO_ALL_APP, dotnetAppCounter, &isDotnetAppType, uid) != PMINFO_R_OK) { - _ERR("Failed to get list of app in pkg : %s", pkgId.c_str()); - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - return false; - } - - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - return isDotnetAppType; -} - -void SHA256(std::string path, char outputBuffer[65]) +static void SHA256(std::string path, char outputBuffer[65]) { FILE *file = fopen(path.c_str(), "rb"); if (!file) { @@ -151,7 +83,7 @@ void SHA256(std::string path, char outputBuffer[65]) free(buffer); } -void createSHA256Info(std::string sha256Info, std::string nugetPackage) +static void createSHA256Info(std::string sha256Info, std::string nugetPackage) { std::ofstream ofs(sha256Info, std::ios::app); int assembly_count = 0; @@ -169,7 +101,7 @@ void createSHA256Info(std::string sha256Info, std::string nugetPackage) ofs.close(); } -int compareSHA256Info(std::string sha256Info, std::string nugetPackage) +static int compareSHA256Info(std::string sha256Info, std::string nugetPackage) { int compare_count = 0; int assembly_count = 0; @@ -202,7 +134,7 @@ int compareSHA256Info(std::string sha256Info, std::string nugetPackage) return 0; } -int copyNCreateSymlink(std::string tacVersionDir, std::string nugetPackage, bool isCreateTacDir) +static int copyNCreateSymlink(std::string binPath, std::string tacVersionDir, std::string nugetPackage, bool isCreateTacDir) { uid_t g_uid = 0; gid_t g_gid = 0; @@ -244,7 +176,8 @@ int copyNCreateSymlink(std::string tacVersionDir, std::string nugetPackage, bool return 0; } -void depsJsonCheck() { +static void depsJsonCheck(std::string rootPath, std::string binPath, std::string execName) +{ for (auto& npAssembly : depsJsonParser(rootPath, execName, getTPA())) { std::string nuget_package = npAssembly.substr(0, npAssembly.rfind(':')); std::string assembly_name = npAssembly.substr(npAssembly.rfind(':') + 1); @@ -270,20 +203,28 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app } tacPluginInstalled = true; - if (!appTypeCheck(std::string(pkgId))) { - _INFO("App type is not dotnet"); + if (initializePathManager(std::string(), std::string(), std::string())) { + _ERR("Fail to initialize PathManger"); + return 0; + } + + std::string appType = getAppType(std::string(pkgId)); + if (strstr(appType.c_str(), "dotnet") == NULL) { + _ERR("App type is not dotnet"); return 0; } - if (getExecName(std::string(pkgId), execName) < 0) { + std::string execName = getExecName(std::string(pkgId)); + std::string rootPath = getRootPath(std::string(pkgId)); + if (execName.empty() || rootPath.empty()) { return 0; } - if (getRootPath(std::string(pkgId), rootPath) < 0) { + std::string binPath = concatPath(rootPath, "bin"); + std::string metaValue = getMetadataValue(std::string(pkgId), TAC_METADATA_KEY); + if (metaValue.empty()) { return 0; - } else { - binPath = concatPath(rootPath, "bin"); } - if (metadataCheck(list)) { - depsJsonCheck(); + if (metaValue == METADATA_VALUE) { + depsJsonCheck(rootPath, binPath, execName); } status = "install"; @@ -320,7 +261,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app return -1; } - if (copyNCreateSymlink(tac_version_dir, np, true) < 0) { + if (copyNCreateSymlink(binPath, tac_version_dir, np, true) < 0) { _ERR("Failed to create symlink"); return -1; } @@ -334,7 +275,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str()); if (!bf::is_symlink(sha256_info)) { if (compareSHA256Info(sha256_info, np)) { - if (copyNCreateSymlink(tac_version_dir, np, false) < 0) { + if (copyNCreateSymlink(binPath, tac_version_dir, np, false) < 0) { _ERR("Failed to create symlink"); return -1; } @@ -353,7 +294,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app } } if (!bf::exists(sha256_info)) { - if(!removeAll(tac_version_dir)) { + if (!removeAll(tac_version_dir)) { _ERR("Failed to remove of %s", tac_version_dir.c_str()); } } @@ -361,13 +302,14 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgId, const char *app return 0; } -static int sqliteCb(void *count, int argc, char **argv, char **colName) { +static int sqliteCb(void *count, int argc, char **argv, char **colName) +{ int *c = (int*)count; *c = atoi(argv[0]); return 0; } -int updateTacDB(sqlite3 *sqlite) +static int updateTacDB(sqlite3 *sqlite) { for (auto& unp : updateTac) { int count = -1; @@ -409,23 +351,32 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app } tacPluginInstalled = true; - if (!appTypeCheck(std::string(pkgId))) { - _INFO("App type is not dotnet"); + if (initializePathManager(std::string(), std::string(), std::string())) { + _ERR("Fail to initialize PathManger"); return 0; } - if (getExecName(std::string(pkgId), execName) < 0) { + + std::string appType = getAppType(std::string(pkgId)); + if (strstr(appType.c_str(), "dotnet") == NULL) { + _ERR("App type is not dotnet"); return 0; } - if (getRootPath(std::string(pkgId), rootPath) < 0) { + std::string execName = getExecName(std::string(pkgId)); + std::string rootPath = getRootPath(std::string(pkgId)); + if (execName.empty() || rootPath.empty()) { return 0; - } else { - binPath = concatPath(rootPath, "bin"); } + std::string binPath = concatPath(rootPath, "bin"); + if (!strcmp("removed", status.c_str())) { _INFO("Skipped to parse of deps.json"); } else { - if (metadataCheck(list)) { - depsJsonCheck(); + std::string metaValue = getMetadataValue(std::string(pkgId), TAC_METADATA_KEY); + if (metaValue.empty()) { + return 0; + } + if (metaValue == METADATA_VALUE) { + depsJsonCheck(rootPath, binPath, execName); } } @@ -472,7 +423,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app return -1; } - if (copyNCreateSymlink(tac_version_dir, np, true) < 0) { + if (copyNCreateSymlink(binPath, tac_version_dir, np, true) < 0) { _ERR("Failed to create symlink"); return -1; } @@ -504,7 +455,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app _INFO("Exists tac_version_dir [%s]", tac_version_dir.c_str()); if (!bf::is_symlink(sha256_info)) { if (compareSHA256Info(sha256_info, np)) { - if (copyNCreateSymlink(tac_version_dir, np, false) < 0) { + if (copyNCreateSymlink(binPath, tac_version_dir, np, false) < 0) { _ERR("Failed to create symlink"); return -1; } @@ -541,7 +492,7 @@ extern "C" int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgId, const char *app } } if (!bf::exists(sha256_info)) { - if(!removeAll(tac_version_dir)) { + if (!removeAll(tac_version_dir)) { _ERR("Failed to remove of %s", tac_version_dir.c_str()); } } diff --git a/NativeLauncher/launcher/exec/launcher.cc b/NativeLauncher/launcher/exec/launcher.cc index f90650c..37b0743 100644 --- a/NativeLauncher/launcher/exec/launcher.cc +++ b/NativeLauncher/launcher/exec/launcher.cc @@ -42,7 +42,7 @@ static std::string PaddingOption("--PADDING_TO_CHANGE_CMDLINE_PADDING_TO_CHANGE_ int main(int argc, char *argv[]) { _INFO("##### Run in standalone mode #####"); - + char* standalonePath = nullptr; bool paddingExist = false; const char* appRootPath = NULL; @@ -104,4 +104,3 @@ int main(int argc, char *argv[]) return 0; } - diff --git a/NativeLauncher/launcher/exec/loader.cc b/NativeLauncher/launcher/exec/loader.cc index a42eedc..222de0a 100644 --- a/NativeLauncher/launcher/exec/loader.cc +++ b/NativeLauncher/launcher/exec/loader.cc @@ -203,4 +203,3 @@ int main(int argc, char *argv[]) return realMain(argc, argv, "candidate"); } - diff --git a/NativeLauncher/launcher/lib/dotnet_launcher.cc b/NativeLauncher/launcher/lib/dotnet_launcher.cc index 2e1bfe6..0a32533 100644 --- a/NativeLauncher/launcher/lib/dotnet_launcher.cc +++ b/NativeLauncher/launcher/lib/dotnet_launcher.cc @@ -265,6 +265,18 @@ static void setLang() free(lang); } +static std::string readSelfPath() +{ + char buff[PATH_MAX]; + ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1); + if (len != -1) { + buff[len] = '\0'; + return std::string(buff); + } + + return ""; +} + void CoreRuntime::preload() { typedef void (*PreloadDelegate)(); diff --git a/NativeLauncher/tool/ni_common.cc b/NativeLauncher/tool/ni_common.cc index 52ea3ef..a45043e 100644 --- a/NativeLauncher/tool/ni_common.cc +++ b/NativeLauncher/tool/ni_common.cc @@ -145,6 +145,17 @@ static bool checkNIExistence(const std::string& path) } #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT +static uintptr_t getFileSize(const std::string& path) +{ + struct stat sb; + + if (stat(path.c_str(), &sb) == 0) { + return sb.st_size; + } + + return 0; +} + // Get next base address to be used for system ni image from file // __SYSTEM_BASE_FILE should be checked for existance before calling this function static uintptr_t getNextBaseAddrFromFile() @@ -186,7 +197,7 @@ static uintptr_t getNextBaseAddr() } // Save base address of system ni image to file -static void updateBaseAddrFile(const std::string &absNIPath, uintptr_t baseAddr) +static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr) { uintptr_t niSize = getFileSize(absNIPath); if (niSize == 0) { @@ -206,7 +217,7 @@ static void updateBaseAddrFile(const std::string &absNIPath, uintptr_t baseAddr) } // check if dll is listed in TPA -static bool isTPADll(const std::string &dllPath) +static bool isTPADll(const std::string& dllPath) { std::string absDllPath = getAbsolutePath(dllPath); @@ -380,7 +391,7 @@ static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData) if (createTACPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) { fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId); return -1; - }else { + } else { fprintf(stdout, "Complete make symbolic link file to tac\n"); } @@ -491,7 +502,6 @@ void finalizeNICommon() __tpa.clear(); } - ni_error_e createNIPlatform(DWORD flags) { if (createCoreLibNI(flags) != NI_ERROR_NONE) { @@ -515,7 +525,7 @@ ni_error_e createNIDll(const std::string& dllPath, DWORD flags) return crossgen(dllPath, std::string(), flags); } -void createNIUnderTAC(std::vector nugets, DWORD flags) +static void createNIUnderTAC(const std::vector& nugets, DWORD flags) { std::string appPaths; for (auto& nuget : nugets) { @@ -544,14 +554,14 @@ ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags) return NI_ERROR_CORE_NI_FILE; } - std::string pkgRoot; - if (getRootPath(pkgId, pkgRoot) < 0) { + std::string rootPath = getRootPath(pkgId); + if (rootPath.empty()) { fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); return NI_ERROR_INVALID_PACKAGE; } - std::string binDir = concatPath(pkgRoot, "bin"); - std::string libDir = concatPath(pkgRoot, "lib"); + std::string binDir = concatPath(rootPath, "bin"); + std::string libDir = concatPath(rootPath, "lib"); std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR); std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR); std::string paths = binDir + ":" + libDir + ":" + tacDir; @@ -565,7 +575,7 @@ ni_error_e createTACPkgRoot(const std::string& pkgId, DWORD flags) std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll"; if (!bf::exists(originNIPath)) { flags |= NI_FLAGS_APPNI; - if(crossgen(originPath, paths, flags) != NI_ERROR_NONE) { + if (crossgen(originPath, paths, flags) != NI_ERROR_NONE) { fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str()); return NI_ERROR_UNKNOWN; } @@ -635,14 +645,14 @@ ni_error_e createNIUnderDirs(const std::string rootPaths[], int count, DWORD fla ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags) { - std::string pkgRoot; - if (getRootPath(pkgId, pkgRoot) < 0) { + std::string rootPath = getRootPath(pkgId); + if (rootPath.empty()) { fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); return NI_ERROR_INVALID_PACKAGE; } - std::string binDir = concatPath(pkgRoot, "bin"); - std::string libDir = concatPath(pkgRoot, "lib"); + std::string binDir = concatPath(rootPath, "bin"); + std::string libDir = concatPath(rootPath, "lib"); std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR); std::string paths[] = {binDir, libDir, tacDir}; @@ -656,14 +666,14 @@ ni_error_e createNIDllUnderPkgRoot(const std::string& pkgId, const std::string& return NI_ERROR_CORE_NI_FILE; } - std::string pkgRoot; - if (getRootPath(pkgId, pkgRoot) < 0) { + std::string rootPath = getRootPath(pkgId); + if (rootPath.empty()) { fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); return NI_ERROR_INVALID_PACKAGE; } - std::string binDir = concatPath(pkgRoot, "bin"); - std::string libDir = concatPath(pkgRoot, "lib"); + std::string binDir = concatPath(rootPath, "bin"); + std::string libDir = concatPath(rootPath, "lib"); std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR); std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR); std::string paths = binDir + ":" + libDir + ":" + tacDir; @@ -675,7 +685,7 @@ ni_error_e createNIDllUnderPkgRoot(const std::string& pkgId, const std::string& std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll"; if (!bf::exists(originNIPath)) { flags |= NI_FLAGS_APPNI; - if(crossgen(originPath, paths, flags) != NI_ERROR_NONE) { + if (crossgen(originPath, paths, flags) != NI_ERROR_NONE) { fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str()); return NI_ERROR_UNKNOWN; } @@ -757,14 +767,14 @@ void removeNIUnderDirs(const std::string rootPaths[], int count) ni_error_e removeNIUnderPkgRoot(const std::string& pkgId) { - std::string pkgRoot; - if (getRootPath(pkgId, pkgRoot) < 0) { + std::string rootPath = getRootPath(pkgId); + if (rootPath.empty()) { fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); return NI_ERROR_INVALID_PACKAGE; } - std::string binDir = concatPath(pkgRoot, "bin"); - std::string libDir = concatPath(pkgRoot, "lib"); + std::string binDir = concatPath(rootPath, "bin"); + std::string libDir = concatPath(rootPath, "lib"); std::string paths[] = {binDir, libDir}; removeNIUnderDirs(paths, 2); diff --git a/NativeLauncher/tool/nitool.cc b/NativeLauncher/tool/nitool.cc index 841be72..29b6b78 100644 --- a/NativeLauncher/tool/nitool.cc +++ b/NativeLauncher/tool/nitool.cc @@ -33,6 +33,11 @@ std::vector getCmdArgs(char** begin, char** end) return list; } +static bool cmdOptionExists(char** begin, char** end, const std::string& option) +{ + return std::find(begin, end, option) != end; +} + static void help(const char *argv0) { const char* helpDesc = diff --git a/NativeLauncher/tool/tac_common.cc b/NativeLauncher/tool/tac_common.cc index a5e0a02..844ad5e 100644 --- a/NativeLauncher/tool/tac_common.cc +++ b/NativeLauncher/tool/tac_common.cc @@ -219,14 +219,14 @@ tac_error_e restoreTACDB() tac_error_e resetTACPackage(const std::string& pkgId) { - std::string pkgRoot; - if (getRootPath(pkgId, pkgRoot) < 0) { + std::string rootPath = getRootPath(pkgId); + if (rootPath.empty()) { fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); return TAC_ERROR_INVALID_PACKAGE; } std::vector tacNativeImage; - std::string binDir = concatPath(pkgRoot, "bin"); + std::string binDir = concatPath(rootPath, "bin"); std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR); if (bf::exists(tacDir)) { try { @@ -255,13 +255,13 @@ tac_error_e resetTACPackage(const std::string& pkgId) tac_error_e disableTACPackage(const std::string& pkgId) { - std::string pkgRoot; - if (getRootPath(pkgId, pkgRoot) < 0) { + std::string rootPath = getRootPath(pkgId); + if (rootPath.empty()) { fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); return TAC_ERROR_INVALID_PACKAGE; } - std::string binDir = concatPath(pkgRoot, "bin"); + 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 (bf::exists(tacDir)) { @@ -302,21 +302,21 @@ tac_error_e disableTACPackage(const std::string& pkgId) tac_error_e enableTACPackage(const std::string& pkgId) { - std::string rootPath; - if (getRootPath(pkgId, rootPath) < 0) { + std::string rootPath = getRootPath(pkgId); + if (rootPath.empty()) { fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); return TAC_ERROR_INVALID_PACKAGE; } - std::string execName; - if (getExecName(pkgId, execName) < 0) { - fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); + std::string execName = getExecName(pkgId); + if (execName.empty()) { + fprintf(stderr, "Failed to get exec name from [%s]\n", pkgId.c_str()); return TAC_ERROR_INVALID_PACKAGE; } - std::string metaValue; - if (getMetadataValue(pkgId, TAC_METADATA_KEY, metaValue) < 0) { - fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str()); + std::string metaValue = getMetadataValue(pkgId, TAC_METADATA_KEY); + if (metaValue.empty()) { + fprintf(stderr, "Failed to get metadata from [%s]\n", pkgId.c_str()); return TAC_ERROR_INVALID_PACKAGE; } diff --git a/NativeLauncher/util/db_manager.cc b/NativeLauncher/util/db_manager.cc index 1cda879..d42c066 100644 --- a/NativeLauncher/util/db_manager.cc +++ b/NativeLauncher/util/db_manager.cc @@ -30,7 +30,7 @@ static int dbIntegrityCheckCb(void *user_data, int argc, char **argv, char **not bool* is_db_corrupted = static_cast(user_data); std::string result_DB_check = argv[0]; std::transform(result_DB_check.begin(), result_DB_check.end(), result_DB_check.begin(), ::tolower); - if(result_DB_check != "ok") { + if (result_DB_check != "ok") { *is_db_corrupted = true; _ERR("DB integrity check failed"); return -1; diff --git a/NativeLauncher/util/log_manager.cc b/NativeLauncher/util/log_manager.cc index b1c8dd0..72f48cd 100644 --- a/NativeLauncher/util/log_manager.cc +++ b/NativeLauncher/util/log_manager.cc @@ -90,7 +90,8 @@ int initializeLogManager() // if LogManager is not initialized, it will return 0; int redirectFD() { - if (!__isInit) return 0; + if (!__isInit) + return 0; if (__pfd[1] < 0) { _ERR("fail to create pipe for logging"); diff --git a/NativeLauncher/util/path_manager.cc b/NativeLauncher/util/path_manager.cc index e4761c2..3d73528 100644 --- a/NativeLauncher/util/path_manager.cc +++ b/NativeLauncher/util/path_manager.cc @@ -151,7 +151,7 @@ static std::string getPluginTPA() if (plugin_tpa_list) { _INFO("plugin TPA list found. use TPA list for plugin"); plugin_tpa = plugin_tpa_list; - } else if (!__dllPath->extra_dirs.empty()){ + } else if (!__dllPath->extra_dirs.empty()) { _INFO("plugin extra directory found. use plugin extra directroy for TPA"); addAssembliesFromDirectories(__dllPath->extra_dirs, plugin_tpa); } @@ -171,4 +171,3 @@ std::string getTPA() return getPlatformTPA() + ":" + getPluginTPA(); } - diff --git a/NativeLauncher/util/plugin_manager.cc b/NativeLauncher/util/plugin_manager.cc index c9e25d2..e402576 100644 --- a/NativeLauncher/util/plugin_manager.cc +++ b/NativeLauncher/util/plugin_manager.cc @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include #include "plugin_manager.h" @@ -136,4 +137,3 @@ void pluginFinalize() __pluginFunc->finalize(); } } - diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index f399b2c..5b047ae 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -60,23 +60,6 @@ bool isNativeImage(const std::string& fileName) return iCompare(fileName, fileName.size()-7, ".ni", 0, 3); } -bool cmdOptionExists(char** begin, char** end, const std::string& option) -{ - return std::find(begin, end, option) != end; -} - -std::string readSelfPath() -{ - char buff[PATH_MAX]; - ssize_t len = ::readlink("/proc/self/exe", buff, sizeof(buff)-1); - if (len != -1) { - buff[len] = '\0'; - return std::string(buff); - } - - return ""; -} - std::string concatPath(const std::string& path1, const std::string& path2) { std::string path(path1); @@ -112,109 +95,151 @@ std::string getAbsolutePath(const std::string& path) return absPath; } -int getRootPath(const std::string& pkgId, std::string& rootPath) +std::string getRootPath(const std::string& pkgId) { int ret = 0; char *path = 0; uid_t uid = 0; + std::string rootPath; if (pkgmgr_installer_info_get_target_uid(&uid) < 0) { _ERR("Failed to get UID"); - return -1; + return rootPath; } pkgmgrinfo_pkginfo_h handle; if (uid == 0) { ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle); if (ret != PMINFO_R_OK) { - return -1; + return rootPath; } } else { ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle); if (ret != PMINFO_R_OK) { - return -1; + return rootPath; } } ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path); if (ret != PMINFO_R_OK) { pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - return -1; + return rootPath; } rootPath = path; pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - return 0; + + return rootPath; } -int getExecName(const std::string& pkgId, std::string& execName) +std::string getExecName(const std::string& pkgId) { char *exec = NULL; char *appId = 0; + std::string execName; pkgmgrinfo_pkginfo_h pkg_handle; int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle); if (ret != PMINFO_R_OK) { - return -1; + return execName; } ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId); if (ret != PMINFO_R_OK) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - return -1; + return execName; } pkgmgrinfo_appinfo_h app_handle; ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle); if (ret != PMINFO_R_OK) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - return -1; + return execName; } ret = pkgmgrinfo_appinfo_get_exec(app_handle, &exec); if (ret != PMINFO_R_OK) { pkgmgrinfo_appinfo_destroy_appinfo(app_handle); pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - return -1; + return execName; } execName = std::string(exec).substr(std::string(exec).rfind('/') + 1); pkgmgrinfo_appinfo_destroy_appinfo(app_handle); pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - return 0; + + return execName; } -int getMetadataValue(const std::string& pkgId, const std::string& metadataKey, std::string& metadataValue) +std::string getAppType(const std::string& pkgId) +{ + char *appId = 0; + char *type = 0; + std::string appType; + + pkgmgrinfo_pkginfo_h pkg_handle; + int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle); + if (ret != PMINFO_R_OK) { + return appType; + } + ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId); + if (ret != PMINFO_R_OK) { + pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); + return appType; + } + + pkgmgrinfo_appinfo_h app_handle; + ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle); + if (ret != PMINFO_R_OK) { + pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); + return appType; + } + ret = pkgmgrinfo_appinfo_get_apptype(app_handle, &type); + if (ret != PMINFO_R_OK) { + pkgmgrinfo_appinfo_destroy_appinfo(app_handle); + pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); + return appType; + } + appType = type; + + pkgmgrinfo_appinfo_destroy_appinfo(app_handle); + pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); + + return appType; +} + +std::string getMetadataValue(const std::string& pkgId, const std::string& key) { char *value = NULL; char *appId = 0; + std::string metadataValue; pkgmgrinfo_pkginfo_h pkg_handle; int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle); if (ret != PMINFO_R_OK) { - return -1; + return metadataValue; } ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId); if (ret != PMINFO_R_OK) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - return -1; + return metadataValue; } pkgmgrinfo_appinfo_h app_handle; ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle); if (ret != PMINFO_R_OK) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - return -1; + return metadataValue; } - ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, metadataKey.c_str(), &value); + ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, key.c_str(), &value); if (ret != PMINFO_R_OK) { pkgmgrinfo_appinfo_destroy_appinfo(app_handle); pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - //Does not return error because the metadata key may not exist. - return 0; + return metadataValue; } metadataValue = std::string(value); pkgmgrinfo_appinfo_destroy_appinfo(app_handle); pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); - return 0; + + return metadataValue; } std::string getBaseName(const std::string& path) @@ -252,24 +277,14 @@ bool isDirectory(const std::string& path) struct stat sb; if (stat(path.c_str(), &sb) != 0) { return false; - } else if (sb.st_mode & S_IFDIR) { + } + if (sb.st_mode & S_IFDIR) { return true; } else { return false; } } -uintptr_t getFileSize(const std::string& path) -{ - struct stat sb; - - if (stat(path.c_str(), &sb) == 0) { - return sb.st_size; - } - - return 0; -} - std::string getAssemblyNameFromPath(const std::string& path) { std::string ret(getFileName(path)); @@ -284,7 +299,8 @@ std::string getAssemblyNameFromPath(const std::string& path) return ret; } -void addAssembliesFromDirectories(const std::vector& directories, std::string& list) { +void addAssembliesFromDirectories(const std::vector& directories, std::string& list) +{ std::vector assems; std::unordered_map assemPaths; @@ -401,7 +417,8 @@ void copySmackAndOwnership(const std::string& fromPath, const std::string& toPat } } -static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) { +static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) +{ int fd = open(path.c_str(), O_RDONLY); if (fd < 0) { _ERR("Can't open directory: %s", path.c_str()); @@ -416,7 +433,8 @@ static bool setOwnership(const bf::path& path, uid_t uid, gid_t gid) { return true; } -static bool setDirPermissions(const bf::path& path, bf::perms permissions) { +static bool setDirPermissions(const bf::path& path, bf::perms permissions) +{ bs::error_code error; bf::permissions(path, permissions, error); if (error) { @@ -426,7 +444,8 @@ static bool setDirPermissions(const bf::path& path, bf::perms permissions) { return true; } -static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid) { +static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permissions, uid_t uid, gid_t gid) +{ if (!setOwnership(path, uid, gid)) { _ERR("Failed to change owner: %s, (uid: %d, gid: %d)", path.c_str(), uid, gid); return false; @@ -438,7 +457,8 @@ static bool setDirOwnershipAndPermissions(const bf::path& path, bf::perms permis return true; } -static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) { +static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2) +{ if (!bf::exists(path)) { _ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str()); return false; @@ -455,7 +475,8 @@ static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& pa return true; } -bool createDir(const bf::path& path) { +bool createDir(const bf::path& path) +{ if (bf::exists(path)) { return true; } @@ -468,7 +489,8 @@ bool createDir(const bf::path& path) { return true; } -bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) { +bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) +{ try { // Check whether the function call is valid if (!bf::exists(path1) || !bf::is_directory(path1)) { @@ -549,7 +571,8 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags) { return true; } -bool copyFile(const bf::path& path1, const bf::path& path2) { +bool copyFile(const bf::path& path1, const bf::path& path2) +{ bs::error_code error; if (!bf::exists(path1)) { return false; @@ -562,7 +585,8 @@ bool copyFile(const bf::path& path1, const bf::path& path2) { return true; } -bool moveFile(const bf::path& path1, const bf::path& path2) { +bool moveFile(const bf::path& path1, const bf::path& path2) +{ if (!bf::exists(path1) || bf::exists(path2)) { return false; } @@ -584,7 +608,8 @@ bool moveFile(const bf::path& path1, const bf::path& path2) { return true; } -bool removeFile(const bf::path& path) { +bool removeFile(const bf::path& path) +{ if (!bf::exists(path)) { return true; } @@ -597,7 +622,8 @@ bool removeFile(const bf::path& path) { return true; } -bool removeAll(const bf::path& path) { +bool removeAll(const bf::path& path) +{ if (!exists(path)) { return true; } -- 2.7.4