Fix static analysis issue
[platform/core/appfw/app-installers.git] / src / common / step / pkgmgr / step_remove_priv_sharedres.cc
1 // Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/step/pkgmgr/step_remove_priv_sharedres.h"
6
7 #include <pkgmgr-info.h>
8 #include <tzplatform_config.h>
9
10 #include <boost/filesystem.hpp>
11
12 #include "common/utils/file_util.h"
13 #include "common/utils/user_util.h"
14
15 namespace bf = boost::filesystem;
16
17 namespace {
18
19 bf::path GetRootPathForUid(uid_t uid) {
20   tzplatform_set_user(uid);
21   const char* rootpath = tzplatform_getenv(TZ_USER_HOME);
22   tzplatform_reset_user();
23   return rootpath;
24 }
25
26 bool RemoveResForUid(const std::string& pkgid, uid_t uid) {
27   bf::path target_path = GetRootPathForUid(uid) / pkgid;
28
29   if (bf::exists(target_path))
30     return true;
31
32   if (!common_installer::RemoveAll(target_path))
33     return false;
34
35   return true;
36 }
37
38 int SendUninstallRequest(const std::string& pkgid, uid_t uid) {
39   pkgmgr_client *pc = pkgmgr_client_new(PC_REQUEST);
40
41   int ret = pkgmgr_client_res_usr_uninstall(pc, pkgid.c_str(), uid);
42   pkgmgr_client_free(pc);
43
44   return ret;
45 }
46
47 }  // namespace
48
49 namespace common_installer {
50 namespace pkgmgr {
51
52 Step::Status StepRemovePrivSharedres::process() {
53   if (context_->installation_mode.get() == InstallationMode::OFFLINE)
54     return OfflineRemove();
55   else
56     return RequestRemove();
57 }
58
59 Step::Status StepRemovePrivSharedres::OfflineRemove() {
60   if (context_->request_mode.get() == RequestMode::GLOBAL) {
61     common_installer::UserList user_list = common_installer::GetUserList();
62     for (const auto& l : user_list) {
63       uid_t uid = std::get<0>(l);
64       if (!RemoveResForUid(context_->pkgid.get(), uid))
65         return Status::ERROR;
66     }
67   } else {
68     if (!RemoveResForUid(context_->pkgid.get(), context_->uid.get()))
69       return Status::ERROR;
70   }
71
72   return Status::OK;
73 }
74
75 Step::Status StepRemovePrivSharedres::RequestRemove() {
76   if (context_->request_mode.get() == RequestMode::GLOBAL) {
77     common_installer::UserList user_list = common_installer::GetUserList();
78     for (const auto& l : user_list) {
79       uid_t uid = std::get<0>(l);
80       int ret = SendUninstallRequest(context_->pkgid.get(), uid);
81       if (ret == PKGMGR_R_OK) {
82         return Status::OK;
83       } else if (ret == PKGMGR_R_ECOMM) {
84           return OfflineRemove();
85       } else {
86           LOG(ERROR) << "Failed to remove privileged shared resources";
87           return Status::ERROR;
88       }
89     }
90   } else {
91     int ret = SendUninstallRequest(context_->pkgid.get(), context_->uid.get());
92     if (ret == PKGMGR_R_OK) {
93       return Status::OK;
94     } else if (ret == PKGMGR_R_ECOMM) {
95       return OfflineRemove();
96     } else {
97       LOG(ERROR) << "Failed to remove privileged shared resources";
98       return Status::ERROR;
99     }
100   }
101   return Status::OK;
102 }
103
104 }  // namespace pkgmgr
105 }  // namespace common_installer