ace461e095c9202cccb588df8a7affa602fd547c
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / profile_common.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved\r
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */\r
16 \r
17 #include "log.h"
18 #include "utils.h"\r
19 #include "profile_common.h"\r
20 #include "launcher_env.h"\r
21 \r
22 #include <sys/types.h>\r
23 #include <pwd.h>\r
24 #include <tzplatform_config.h>\r
25 \r
26 static std::vector<uid_t> getUserIds()\r
27 {
28         std::vector<uid_t> list;
29
30         while (true) {
31                 errno = 0; // so we can distinguish errors from no more entries
32                 passwd* entry = getpwent();
33                 if (!entry) {
34                         if (errno) {
35                                 _SERR("Error while getting userIDs");
36                                 list.clear();
37                                 return list;
38                         }
39                         break;
40                 }
41                 list.push_back(entry->pw_uid);
42         }
43         endpwent();
44
45         return list;
46 }
47
48 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
49 {
50         std::string pDataFile;
51
52         tzplatform_set_user(uid);
53
54         const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
55         if (tzUserApp != NULL) {
56                 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
57         }
58
59         tzplatform_reset_user();
60
61         return pDataFile;
62 }\r
63 \r
64 profile_error_e removeAppProfileData(const std::string& pkgId)\r
65 {
66         if (pkgId.empty()) {
67                 return PROFILE_ERROR_INVALID_PARAMETER;\r
68         }
69
70         std::vector<uid_t> uidList = getUserIds();
71         for (auto& uid : uidList) {
72                 // get data path from pkgid
73                 std::string dataPath = getAppDataPath(pkgId, uid);
74                 if (!dataPath.empty() && exist(dataPath)) {
75                         std::string pDataFile = dataPath + PROFILE_BASENAME;
76
77                         if (exist(pDataFile)) {
78                                 if (!removeFile(pDataFile)) {
79                                         _SERR("Failed to remove profile data file (%s).", pDataFile.c_str());\r
80                                         return PROFILE_ERROR_UNKNOWN;\r
81                                 }
82                                 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
83                         }
84                 }
85         }
86
87         return PROFILE_ERROR_NONE;\r
88 }
89
90 static int removeAppProfileListCb(pkgmgrinfo_appinfo_h handle, void *user_data)\r
91 {
92         char *pkgId = NULL;
93         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
94         if (ret != PMINFO_R_OK || pkgId == NULL) {
95                 _SERR("Failed to get package id");\r
96                 return 0;
97         }
98
99         if (removeAppProfileData(pkgId) != PROFILE_ERROR_NONE) {\r
100                 _SERR("Failed to remove profile data for (%s)", pkgId);\r
101         }
102
103         return 0;
104 }
105
106 static void removeAppProfileByAppType(const char* type)\r
107 {
108         pkgmgrinfo_appinfo_filter_h filter;\r
109
110         int ret = pkgmgrinfo_appinfo_filter_create(&filter);\r
111         if (ret != PMINFO_R_OK) {
112                 _SERR("Failed to create appinfo filter");\r
113                 return;\r
114         }
115
116         ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
117         if (ret != PMINFO_R_OK) {
118                 pkgmgrinfo_appinfo_filter_destroy(filter);
119                 _SERR("Failed to add appinfo filter (%s)", type);\r
120                 return;\r
121         }
122
123         ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, removeAppProfileListCb, NULL);\r
124         if (ret != PMINFO_R_OK) {
125                 _SERR("Failed to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");\r
126                 pkgmgrinfo_appinfo_filter_destroy(filter);
127                 return;\r
128         }
129
130         pkgmgrinfo_appinfo_filter_destroy(filter);
131
132         return;\r
133 }
134
135 void removeAllAppProfileData()
136 {
137         std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
138
139         for (auto& type : appTypeList) {
140                 removeAppProfileByAppType(type);\r
141         }
142 }\r
143 \r