From cefe45ea96b6761956be5f4298244f1804022662 Mon Sep 17 00:00:00 2001 From: Woongsuk Cho Date: Thu, 9 Nov 2023 15:06:16 +0900 Subject: [PATCH] Get user app root dir for remove profile data To remove duplicated API call(changing user), get userAppRoot dir instead of uid. --- NativeLauncher/tool/profile_common.cc | 49 ++++++++++++++--------------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/NativeLauncher/tool/profile_common.cc b/NativeLauncher/tool/profile_common.cc index e052544..80ae73d 100644 --- a/NativeLauncher/tool/profile_common.cc +++ b/NativeLauncher/tool/profile_common.cc @@ -24,47 +24,36 @@ #include #include -// Gets the user's uid with a directory under the home directory. -// In order to reduce unnecessary operation, only uids related to installed app are obtained. -static std::vector getUserIds() +// Gets app root directories of users. +// In order to reduce unnecessary operation, it is obtained only when an app root exists. +static std::vector getUserAppRoots() { DIR *dir; struct dirent* entry; struct passwd *p; - std::vector uids; + std::vector userAppRoots; dir = opendir("/home"); if (dir == nullptr) { - return uids; + return userAppRoots; } while ((entry = readdir(dir)) != nullptr) { if (entry->d_type == DT_DIR) { if ((p = getpwnam(entry->d_name)) != NULL) { - uids.push_back(p->pw_uid); + tzplatform_set_user(p->pw_uid); + const char* tzUserApp = tzplatform_getenv(TZ_USER_APP); + if (tzUserApp != NULL && exist(tzUserApp)) { + userAppRoots.push_back(tzUserApp); + } + tzplatform_reset_user(); } } } closedir(dir); - return uids; -} - -static std::string getAppDataPath(const std::string& pkgId, uid_t uid) -{ - std::string pDataFile; - - tzplatform_set_user(uid); - - const char* tzUserApp = tzplatform_getenv(TZ_USER_APP); - if (tzUserApp != NULL) { - pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/"; - } - - tzplatform_reset_user(); - - return pDataFile; + return userAppRoots; } profile_error_e removeAppProfileData(const std::string& pkgId, void *user_data) @@ -73,16 +62,16 @@ profile_error_e removeAppProfileData(const std::string& pkgId, void *user_data) return PROFILE_ERROR_INVALID_PARAMETER; } - std::vector uidList; + std::vector userAppRoots; if (user_data != NULL) { - uidList = *(std::vector*)user_data; + userAppRoots = *(std::vector*)user_data; } else { - uidList = getUserIds(); + userAppRoots = getUserAppRoots(); } - for (auto it = uidList.begin(); it != uidList.end(); ++it) { + for (auto it = userAppRoots.begin(); it != userAppRoots.end(); ++it) { // get data path from pkgid and uid - std::string dataPath = getAppDataPath(pkgId, *it); + std::string dataPath = *it + "/" + pkgId + "/data/"; if (!dataPath.empty() && exist(dataPath)) { std::string pDataFile = dataPath + PROFILE_BASENAME; @@ -132,8 +121,8 @@ static int removeAppProfileListCb(pkgmgrinfo_appinfo_h handle, void *user_data) void removeAllAppProfileData() { // To reduce repeated getUserIds calls, get uids here. - std::vector uidList = getUserIds(); - int ret = pkgmgrinfo_appinfo_get_installed_list(removeAppProfileListCb, &uidList); + std::vector userAppRoots = getUserAppRoots(); + int ret = pkgmgrinfo_appinfo_get_installed_list(removeAppProfileListCb, &userAppRoots); if (ret != PMINFO_R_OK) { _SERR("Failed to get installed list"); } -- 2.7.4