Get user app root dir for remove profile data accepted/tizen/8.0/unified/20231110.171756 accepted/tizen/unified/20231110.172151
authorWoongsuk Cho <ws77.cho@samsung.com>
Thu, 9 Nov 2023 06:06:16 +0000 (15:06 +0900)
committer조웅석/MDE Lab(SR)/삼성전자 <ws77.cho@samsung.com>
Thu, 9 Nov 2023 06:16:03 +0000 (15:16 +0900)
To remove duplicated API call(changing user), get userAppRoot dir instead of uid.

NativeLauncher/tool/profile_common.cc

index e052544..80ae73d 100644 (file)
 #include <pwd.h>
 #include <tzplatform_config.h>
 
-// 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<uid_t> 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<std::string> getUserAppRoots()
 {
        DIR *dir;
        struct dirent* entry;
        struct passwd *p;
-       std::vector<uid_t> uids;
+       std::vector<std::string> 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<uid_t> uidList;
+       std::vector<std::string> userAppRoots;
        if (user_data != NULL) {
-               uidList = *(std::vector<uid_t>*)user_data;
+               userAppRoots = *(std::vector<std::string>*)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<uid_t> uidList = getUserIds();
-       int ret = pkgmgrinfo_appinfo_get_installed_list(removeAppProfileListCb, &uidList);
+       std::vector<std::string> userAppRoots = getUserAppRoots();
+       int ret = pkgmgrinfo_appinfo_get_installed_list(removeAppProfileListCb, &userAppRoots);
        if (ret != PMINFO_R_OK) {
                _SERR("Failed to get installed list");
        }