91065f1a925c0cd5b21562b68da3fc14a9b6962b
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / profile_common.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
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  */
16
17 #include "log.h"
18 #include "utils.h"
19 #include "profile_common.h"
20 #include "launcher_env.h"
21
22 #include <sys/types.h>
23 #include <pwd.h>
24 #include <tzplatform_config.h>
25
26 static std::vector<uid_t> getUserIds()
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 }
63
64 profile_error_e removeAppProfileData(const std::string& pkgId)
65 {
66         if (pkgId.empty()) {
67                 return PROFILE_ERROR_INVALID_PARAMETER;
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());
80                                         return PROFILE_ERROR_UNKNOWN;
81                                 }
82                                 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
83                         }
84                 }
85         }
86
87         return PROFILE_ERROR_NONE;
88 }
89
90 static int removeAppProfileListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
91 {
92         int ret = 0;
93         char* appType = NULL;
94
95         ret = pkgmgrinfo_appinfo_get_apptype(handle, &appType);
96         if (ret != PMINFO_R_OK) {
97                 _SERR("Failed to get apptype");
98                 return 0;
99         }
100
101         // skip if appType is NULL or appType does not contains "dotnet",
102         if (appType == NULL || (appType != NULL && strstr(appType, "dotnet") == NULL)) {
103                 return 0;
104         }
105
106         char *pkgId = NULL;
107         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
108         if (ret != PMINFO_R_OK || pkgId == NULL) {
109                 _SERR("Failed to get package id");
110                 return 0;
111         }
112
113         if (removeAppProfileData(pkgId) != PROFILE_ERROR_NONE) {
114                 _SERR("Failed to remove profile data for (%s)", pkgId);
115         }
116
117         return 0;
118 }
119
120 void removeAllAppProfileData()
121 {
122         int ret = pkgmgrinfo_appinfo_get_installed_list(removeAppProfileListCb, NULL);
123         if (ret != PMINFO_R_OK) {
124                 _SERR("Failed to get installed list");
125         }
126 }