1 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by a apache 2.0 license that can be
3 // found in the LICENSE file.
5 #include "common/step/configuration/step_configure.h"
7 #include <boost/filesystem/path.hpp>
9 #include <pkgmgr-info.h>
11 #include <sys/types.h>
13 #include <tzplatform_config.h>
19 #include "common/utils/pkgmgr_query.h"
20 #include "common/utils/file_util.h"
21 #include "common/utils/user_util.h"
22 #include "common/utils/request.h"
24 namespace bf = boost::filesystem;
26 namespace common_installer {
27 namespace configuration {
29 const char kStrEmpty[] = "";
30 const char kAppFWUser[] = "app_fw";
32 StepConfigure::StepConfigure(InstallerContext* context, PkgMgrPtr pkgmgr)
37 Step::Status StepConfigure::process() {
38 SetupRequestMode(pkgmgr_->GetUid());
40 SetupFileCreationMask();
42 SetupSkipCheckReference();
43 SetupSkipOptimization();
45 if (!SetupRootAppDirectory())
46 return Status::CONFIG_ERROR;
48 RequestType request_type = context_->request_type.get();
49 switch (request_type) {
50 case RequestType::Install:
51 case RequestType::Update:
52 context_->file_path.set(pkgmgr_->GetRequestInfo(context_->index.get()));
53 if (!pkgmgr_->GetTepPath().empty()) {
54 context_->tep_path.set(pkgmgr_->GetTepPath());
55 context_->is_tep_move.set(pkgmgr_->GetIsTepMove());
58 case RequestType::PartialUninstall:
59 case RequestType::Uninstall:
60 if (request_type == RequestType::PartialUninstall)
63 SetupIsForceRemoval();
64 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
65 context_->file_path.set(kStrEmpty);
67 case RequestType::Reinstall:
68 context_->unpacked_dir_path.set(
69 bf::path(tzplatform_getenv(TZ_SDK_TOOLS)) / "tmp" /
70 pkgmgr_->GetRequestInfo(context_->index.get()));
71 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
72 context_->file_path.set(kStrEmpty);
74 case RequestType::Delta:
75 context_->unpacked_dir_path.set(kStrEmpty);
76 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
77 context_->file_path.set(pkgmgr_->GetRequestInfo(context_->index.get()));
79 case RequestType::Move:
80 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
81 context_->is_move_to_external.set(pkgmgr_->GetIsMoveToExternal());
84 case RequestType::Recovery:
85 context_->file_path.set(pkgmgr_->GetRequestInfo(context_->index.get()));
86 context_->recovery_info.set(
87 RecoveryInfo(context_->file_path.get(),
88 pkgmgr_->GetRecoveryCleanup()));
90 case RequestType::MountInstall:
91 case RequestType::MountUpdate:
92 context_->file_path.set(pkgmgr_->GetRequestInfo(context_->index.get()));
93 if (!pkgmgr_->GetTepPath().empty()) {
94 context_->tep_path.set(pkgmgr_->GetTepPath());
95 context_->is_tep_move.set(pkgmgr_->GetIsTepMove());
98 case RequestType::ManifestPartialInstall:
99 case RequestType::ManifestPartialUpdate:
100 case RequestType::ManifestDirectInstall:
101 case RequestType::ManifestDirectUpdate: {
102 if (request_type == RequestType::ManifestPartialInstall ||
103 request_type == RequestType::ManifestPartialUpdate)
105 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
107 bf::path(getUserManifestPath(context_->uid.get(),
108 context_->is_readonly_package.get()))
109 / bf::path(context_->pkgid.get());
111 context_->unpacked_dir_path.set(context_->GetPkgPath());
112 context_->xml_path.set(xml_path);
115 case RequestType::ReadonlyUpdateInstall:
116 context_->file_path.set(pkgmgr_->GetRequestInfo(context_->index.get()));
117 if (!pkgmgr_->GetTepPath().empty()) {
118 context_->tep_path.set(pkgmgr_->GetTepPath());
119 context_->is_tep_move.set(pkgmgr_->GetIsTepMove());
122 case RequestType::ReadonlyUpdateUninstall: {
123 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
124 bf::path original_path =
125 bf::path(tzplatform_getenv(TZ_SYS_RO_APP)) / context_->pkgid.get();
126 context_->unpacked_dir_path.set(original_path);
127 context_->file_path.set(kStrEmpty);
131 case RequestType::DisablePkg:
132 case RequestType::EnablePkg:
133 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
135 case RequestType::MigrateExtImg: {
136 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
139 case RequestType::RecoverDB: {
140 context_->pkgid.set(pkgmgr_->GetRequestInfo(context_->index.get()));
144 LOG(ERROR) << "Unknown request type";
145 return Status::CONFIG_ERROR;
148 LOG(INFO) << "Request Type: " << GetRequestTypeString(request_type);
153 Step::Status StepConfigure::precheck() {
154 SetupIsPreloadRequest();
155 SetupIsPreloadRWRequest();
157 bool is_readonly = context_->is_readonly_package.get();
158 bool is_preload_rw = context_->is_preload_rw_package.get();
159 if (is_readonly && is_preload_rw) {
160 LOG(ERROR) << "Conflict of preload request!";
161 return Status::ERROR;
164 if (is_readonly || is_preload_rw)
167 uid_t uid = pkgmgr_->GetUid();
168 context_->uid.set(uid);
172 boost::optional<uid_t> appfw_uid = GetUidByUserName(kAppFWUser);
174 return Status::ERROR;
176 if (getuid() != *appfw_uid) {
177 LOG(ERROR) << "App-installer should not run with normal user!";
178 return Status::OPERATION_NOT_ALLOWED;
184 bool StepConfigure::SetupRootAppDirectory() {
185 if (context_->root_application_path.get().empty()) {
186 std::string root_app_path =
187 GetRootAppPath(context_->is_readonly_package.get(),
188 context_->uid.get());
189 if (root_app_path.empty())
192 context_->root_application_path.set(root_app_path);
194 if (!boost::filesystem::exists(context_->root_application_path.get())) {
195 boost::system::error_code error;
196 boost::filesystem::create_directories(
197 context_->root_application_path.get());
199 LOG(ERROR) << "Cannot create directory: "
200 << context_->root_application_path.get();
204 LOG(INFO) << "AppDir(" << context_->root_application_path.get() << ")";
208 void StepConfigure::SetupRequestMode(uid_t uid) {
209 context_->request_mode.set(GetRequestMode(uid));
212 void StepConfigure::SetupRequestType() {
213 context_->request_type.set(pkgmgr_->GetRequestType(context_->index.get()));
216 void StepConfigure::SetupFileCreationMask() {
217 mode_t old_mask, new_mask;
218 old_mask = new_mask = 0;
220 switch (context_->request_mode.get()) {
221 case RequestMode::USER:
222 new_mask = 033; // results in 744 privileges
224 case RequestMode::GLOBAL:
225 new_mask = 022; // results in 755 privileges
229 old_mask = umask(new_mask);
231 LOG(INFO) << "Changed file creation mask from " << std::oct << old_mask
232 << " to " << std::oct << new_mask;
235 void StepConfigure::SetupIsPreloadRequest() {
236 context_->is_readonly_package.set(pkgmgr_->GetIsPreloadRequest());
239 void StepConfigure::SetupIsPreloadRWRequest() {
240 context_->is_preload_rw_package.set(pkgmgr_->GetIsPreloadRWRequest());
243 void StepConfigure::SetupIsForceRemoval() {
244 context_->force_remove.set(pkgmgr_->GetIsForceRemoval());
247 void StepConfigure::SetupIsNoRemoval() {
248 context_->no_remove.set(pkgmgr_->GetIsNoRemoval());
251 void StepConfigure::SetupIsKeepRWData() {
252 context_->keep_rwdata.set(pkgmgr_->GetIsKeepRWData());
255 void StepConfigure::SetupIsPartialRW() {
256 context_->partial_rw.set(pkgmgr_->GetIsPartialRW());
259 void StepConfigure::SetupDebugMode() {
260 context_->debug_mode.set(pkgmgr_->GetDebugMode());
263 void StepConfigure::SetupSkipCheckReference() {
264 context_->skip_check_reference.set(pkgmgr_->GetIsSkipCheckReference());
267 void StepConfigure::SetupSkipOptimization() {
268 context_->skip_optimization.set(pkgmgr_->GetSkipOptimization());
271 void StepConfigure::SetupMoveType() {
272 int move_type = pkgmgr_->GetMoveType();
275 context_->move_type.set(MoveType::TO_INTERNAL);
278 context_->move_type.set(MoveType::TO_EXTERNAL);
281 context_->move_type.set(MoveType::TO_EXTENDED);
284 context_->move_type.set(MoveType::TO_INTERNAL);
289 } // namespace configuration
290 } // namespace common_installer