2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "profile_common.h"
20 #include "launcher_env.h"
22 #include <sys/types.h>
25 #include <tzplatform_config.h>
27 // Gets the user's uid with a directory under the home directory.
28 // In order to reduce unnecessary operation, only uids related to installed app are obtained.
29 static std::vector<uid_t> getUserIds()
34 std::vector<uid_t> uids;
36 dir = opendir("/home");
41 while ((entry = readdir(dir)) != nullptr) {
42 if (entry->d_type == DT_DIR) {
43 if ((p = getpwnam(entry->d_name)) != NULL) {
44 uids.push_back(p->pw_uid);
54 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
56 std::string pDataFile;
58 tzplatform_set_user(uid);
60 const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
61 if (tzUserApp != NULL) {
62 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
65 tzplatform_reset_user();
70 profile_error_e removeAppProfileData(const std::string& pkgId, void *user_data)
73 return PROFILE_ERROR_INVALID_PARAMETER;
76 std::vector<uid_t> uidList;
77 if (user_data != NULL) {
78 uidList = *(std::vector<uid_t>*)user_data;
80 uidList = getUserIds();
83 for (auto it = uidList.begin(); it != uidList.end(); ++it) {
84 // get data path from pkgid and uid
85 std::string dataPath = getAppDataPath(pkgId, *it);
86 if (!dataPath.empty() && exist(dataPath)) {
87 std::string pDataFile = dataPath + PROFILE_BASENAME;
89 if (exist(pDataFile)) {
90 if (!removeFile(pDataFile)) {
91 _SERR("Failed to remove profile data file (%s).", pDataFile.c_str());
92 return PROFILE_ERROR_UNKNOWN;
94 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
99 return PROFILE_ERROR_NONE;
102 static int removeAppProfileListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
105 char* appType = NULL;
107 ret = pkgmgrinfo_appinfo_get_apptype(handle, &appType);
108 if (ret != PMINFO_R_OK) {
109 _SERR("Failed to get apptype");
113 // skip if appType is NULL or appType does not contains "dotnet",
114 if (appType == NULL || (appType != NULL && strstr(appType, "dotnet") == NULL)) {
119 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
120 if (ret != PMINFO_R_OK || pkgId == NULL) {
121 _SERR("Failed to get package id");
125 if (removeAppProfileData(pkgId, user_data) != PROFILE_ERROR_NONE) {
126 _SERR("Failed to remove profile data for (%s)", pkgId);
132 void removeAllAppProfileData()
134 // To reduce repeated getUserIds calls, get uids here.
135 std::vector<uid_t> uidList = getUserIds();
136 int ret = pkgmgrinfo_appinfo_get_installed_list(removeAppProfileListCb, &uidList);
137 if (ret != PMINFO_R_OK) {
138 _SERR("Failed to get installed list");