From 3dc25ced430a813b0b2e8cb103865b8d2a39bbb6 Mon Sep 17 00:00:00 2001 From: Woongsuk Cho Date: Tue, 6 Oct 2020 09:56:54 +0900 Subject: [PATCH] Use multi-user support pkgmgrinfo API pkgmgrinfo API provides an API set that considers multi-user. This patch is for clean-up multi-user related API. --- NativeLauncher/inc/utils.h | 31 ++++++++++ NativeLauncher/tool/ni_common.cc | 13 ++--- NativeLauncher/tool/tac_common.cc | 12 ++-- NativeLauncher/util/utils.cc | 119 +++++++++++++++++++++++++++----------- 4 files changed, 125 insertions(+), 50 deletions(-) diff --git a/NativeLauncher/inc/utils.h b/NativeLauncher/inc/utils.h index 77daa24..dc4d6c4 100644 --- a/NativeLauncher/inc/utils.h +++ b/NativeLauncher/inc/utils.h @@ -21,6 +21,7 @@ #include #include #include +#include #include @@ -242,4 +243,34 @@ std::string getFileName(const std::string& path); */ std::string SHA256(const std::string& path); +/** + * @brief Creates the package information handle from db which is not disabled. + * This function is a wrapper of pkgmgrinfo_pkginfo_get_pkginfo() to handle multi-user case + * @param[in] pkg id + * @param[out] pkginfo handle + * @return 0 if success, otherwise -1 + */ +int pkgmgrGetPkgInfo(const std::string& pkgId, pkgmgrinfo_pkginfo_h* handle); + +/** + * @brief Creates the application information handle from db. + * This function is a wrapper of pkgmgrinfo_appinfo_get_appinfo() to handle multi-user case + * @param[in] app id + * @param[out] appinfo handle + * @return 0 if success, otherwise -1 + */ +int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle); + +/** + * @brief Executes the metadata filter query for all the installed packages. + * This function is a wrapper of pkgmgrinfo_appinfo_metadata_filter_foreach() to handle multi-user case + * @param[in] metadata filter handle + * @param[in] callback function + * @param[in] user data + * @return 0 if success, otherwise -1 + */ +int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle, + pkgmgrinfo_app_list_cb app_cb, + void *user_data); + #endif /* __UTILS_H__ */ diff --git a/NativeLauncher/tool/ni_common.cc b/NativeLauncher/tool/ni_common.cc index 7e0dde0..1cd885f 100644 --- a/NativeLauncher/tool/ni_common.cc +++ b/NativeLauncher/tool/ni_common.cc @@ -711,15 +711,12 @@ ni_error_e regenerateAppNI(DWORD flags) return NI_ERROR_UNKNOWN; } - ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &flags); - if (ret != PMINFO_R_OK) { - fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n"); + ret = pkgmgrMDFilterForeach(handle, appAotCb, &flags); + if (ret != 0) { pkgmgrinfo_appinfo_metadata_filter_destroy(handle); return NI_ERROR_UNKNOWN; } - fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n"); - pkgmgrinfo_appinfo_metadata_filter_destroy(handle); return NI_ERROR_NONE; } @@ -795,13 +792,11 @@ ni_error_e regenerateTACNI(DWORD flags) return NI_ERROR_UNKNOWN; } - ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, regenTacCb, &flags); - if (ret != PMINFO_R_OK) { - fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n"); + ret = pkgmgrMDFilterForeach(handle, regenTacCb, &flags); + if (ret != 0) { pkgmgrinfo_appinfo_metadata_filter_destroy(handle); return NI_ERROR_UNKNOWN; } - fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n"); pkgmgrinfo_appinfo_metadata_filter_destroy(handle); diff --git a/NativeLauncher/tool/tac_common.cc b/NativeLauncher/tool/tac_common.cc index d56e519..92dbc78 100644 --- a/NativeLauncher/tool/tac_common.cc +++ b/NativeLauncher/tool/tac_common.cc @@ -167,13 +167,11 @@ tac_error_e tac_restoreDB() return TAC_ERROR_UNKNOWN; } - ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, tac_restoreDBCb, NULL); - if (ret != PMINFO_R_OK) { - fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n"); + ret = pkgmgrMDFilterForeach(handle, tac_restoreDBCb, NULL); + if (ret != 0) { pkgmgrinfo_appinfo_metadata_filter_destroy(handle); return TAC_ERROR_UNKNOWN; } - fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n"); pkgmgrinfo_appinfo_metadata_filter_destroy(handle); @@ -510,13 +508,11 @@ tac_error_e tlc_restoreDB() return TAC_ERROR_UNKNOWN; } - ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, tlc_restoreDBCb, NULL); - if (ret != PMINFO_R_OK) { - fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n"); + ret = pkgmgrMDFilterForeach(handle, tlc_restoreDBCb, NULL); + if (ret != 0) { pkgmgrinfo_appinfo_metadata_filter_destroy(handle); return TAC_ERROR_UNKNOWN; } - fprintf(stdout, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n"); pkgmgrinfo_appinfo_metadata_filter_destroy(handle); diff --git a/NativeLauncher/util/utils.cc b/NativeLauncher/util/utils.cc index bb1ea62..0dd7777 100644 --- a/NativeLauncher/util/utils.cc +++ b/NativeLauncher/util/utils.cc @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -101,34 +100,21 @@ 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"); + pkgmgrinfo_pkginfo_h pkg_handle; + ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle); + if (ret != 0) { return rootPath; } - pkgmgrinfo_pkginfo_h handle; - if (uid == 0) { - ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle); - if (ret != PMINFO_R_OK) { - return rootPath; - } - } else { - ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle); - if (ret != PMINFO_R_OK) { - return rootPath; - } - } - - ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path); + ret = pkgmgrinfo_pkginfo_get_root_path(pkg_handle, &path); if (ret != PMINFO_R_OK) { - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); return rootPath; } rootPath = path; - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); return rootPath; } @@ -140,10 +126,11 @@ std::string getExecName(const std::string& pkgId) std::string execName; pkgmgrinfo_pkginfo_h pkg_handle; - int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle); - if (ret != PMINFO_R_OK) { + int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle); + if (ret != 0) { return execName; } + ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId); if (ret != PMINFO_R_OK) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); @@ -151,11 +138,12 @@ std::string getExecName(const std::string& pkgId) } pkgmgrinfo_appinfo_h app_handle; - ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle); - if (ret != PMINFO_R_OK) { + ret = pkgmgrGetAppInfo(appId, &app_handle); + if (ret != 0) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); return execName; } + ret = pkgmgrinfo_appinfo_get_exec(app_handle, &exec); if (ret != PMINFO_R_OK) { pkgmgrinfo_appinfo_destroy_appinfo(app_handle); @@ -177,10 +165,11 @@ std::string getAppType(const std::string& pkgId) std::string appType; pkgmgrinfo_pkginfo_h pkg_handle; - int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle); - if (ret != PMINFO_R_OK) { + int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle); + if (ret != 0) { return appType; } + ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId); if (ret != PMINFO_R_OK) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); @@ -188,11 +177,12 @@ std::string getAppType(const std::string& pkgId) } pkgmgrinfo_appinfo_h app_handle; - ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle); - if (ret != PMINFO_R_OK) { + ret = pkgmgrGetAppInfo(appId, &app_handle); + if (ret != 0) { 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); @@ -214,10 +204,11 @@ std::string getMetadataValue(const std::string& pkgId, const std::string& key) std::string metadataValue; pkgmgrinfo_pkginfo_h pkg_handle; - int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle); - if (ret != PMINFO_R_OK) { + int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle); + if (ret != 0) { return metadataValue; } + ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId); if (ret != PMINFO_R_OK) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); @@ -225,11 +216,12 @@ std::string getMetadataValue(const std::string& pkgId, const std::string& key) } pkgmgrinfo_appinfo_h app_handle; - ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle); - if (ret != PMINFO_R_OK) { + ret = pkgmgrGetAppInfo(appId, &app_handle); + if (ret != 0) { pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle); return metadataValue; } + ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, key.c_str(), &value); if (ret != PMINFO_R_OK) { pkgmgrinfo_appinfo_destroy_appinfo(app_handle); @@ -700,4 +692,65 @@ std::string SHA256(const std::string& path) free(buffer); return output; -} \ No newline at end of file +} + +int pkgmgrGetPkgInfo(const std::string& pkgId, pkgmgrinfo_pkginfo_h* handle) +{ + uid_t uid = 0; + int ret = 0; + + if (pkgmgr_installer_info_get_target_uid(&uid) < 0) { + _ERR("Failed to get UID"); + return -1; + } + + ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, handle); + if (ret != PMINFO_R_OK) { + _ERR("Failed to get pkginfo (%d)", ret); + return -1; + } + + return 0; +} + +int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle) +{ + uid_t uid = 0; + int ret = 0; + + if (pkgmgr_installer_info_get_target_uid(&uid) < 0) { + _ERR("Failed to get UID"); + return -1; + } + + ret = pkgmgrinfo_appinfo_get_usr_appinfo(appId.c_str(), uid, handle); + if (ret != PMINFO_R_OK) { + _ERR("Failed to get appinfo (%d)", ret); + return -1; + } + + return 0; +} + +int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle, + pkgmgrinfo_app_list_cb app_cb, + void *user_data) +{ + uid_t uid = 0; + int ret = 0; + + if (pkgmgr_installer_info_get_target_uid(&uid) < 0) { + _ERR("Failed to get UID"); + return -1; + } + + + ret = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, uid); + if (ret != PMINFO_R_OK) { + _ERR("Failed to execute the metadata filter query (%d)", ret); + return -1; + } + + return 0; +} + -- 2.7.4