1 // Copyright (c) 2015 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.
5 #include "common/pkgmgr_interface.h"
7 #include <boost/filesystem/path.hpp>
8 #include <boost/filesystem.hpp>
10 #include <tzplatform_config.h>
17 #include "common/app_query_interface.h"
18 #include "common/pkgmgr_query.h"
19 #include "common/pkgmgr_signal.h"
21 namespace bf = boost::filesystem;
25 const char kDeltaFileExtension[] = ".delta";
26 const char kLegacyContentPath[] = "/opt/usr/media";
30 namespace common_installer {
32 bool PkgmgrInstaller::CreatePkgMgrInstaller(pkgmgr_installer** installer,
33 InstallationMode* mode) {
34 *installer = pkgmgr_installer_new();
36 LOG(WARNING) << "Cannot create pkgmgr_installer object. Will try offline";
37 // We assume offline mode if we cannot connect to pkgmgr-server
38 *installer = pkgmgr_installer_offline_new();
42 *mode = InstallationMode::OFFLINE;
44 *mode = InstallationMode::ONLINE;
49 bool PkgmgrInstaller::ShouldCreateSignal() const {
53 PkgMgrPtr PkgMgrInterface::Create(int argc, char** argv,
54 PkgmgrInstallerInterface* pkgmgr_installer_interface,
55 AppQueryInterface* interface) {
56 PkgMgrPtr instance(new PkgMgrInterface(pkgmgr_installer_interface,
58 int result = instance->InitInternal(argc, argv);
65 int PkgMgrInterface::InitInternal(int argc, char** argv) {
66 if (!pkgmgr_installer_interface_->CreatePkgMgrInstaller(&pi_,
68 LOG(ERROR) << "Cannot create pkgmgr_installer object. Aborting.";
72 int result = pkgmgr_installer_receive_request(pi_, argc, argv);
74 LOG(ERROR) << "Cannot receive request. Invalid arguments?";
75 // no need to free pkgmgr_installer here. it will be freed in DTOR.
81 PkgMgrInterface::~PkgMgrInterface() {
83 pkgmgr_installer_free(pi_);
86 RequestType PkgMgrInterface::GetRequestType() const {
87 if (!is_app_installed_) {
90 query_interface_->IsPkgInstalled(GetRequestInfo(), GetUid());
92 is_app_installed_ = false;
94 switch (pkgmgr_installer_get_request_type(pi_)) {
95 case PKGMGR_REQ_INSTALL : {
96 auto request_info = GetRequestInfo();
97 if (request_info.empty())
98 return RequestType::Unknown;
99 std::string extension = bf::path(request_info).extension().string();
100 if (!is_app_installed_.get()) {
101 if (extension == kDeltaFileExtension) {
102 LOG(ERROR) << "Package is not installed. "
103 "Cannot update from delta package";
104 return RequestType::Unknown;
106 return RequestType::Install;
109 if (extension == kDeltaFileExtension) {
110 return RequestType::Delta;
112 std::string pkgid = query_interface_->GetPkgId(GetRequestInfo());
113 if (!GetIsPreloadRequest() &&
114 QueryIsReadonlyPackage(pkgid, GetUid()) &&
115 !QueryIsUpdatedReadonlyPackage(pkgid, GetUid()))
116 return RequestType::ReadonlyUpdateInstall;
118 return RequestType::Update;
122 case PKGMGR_REQ_UNINSTALL: {
123 std::string pkgid = GetRequestInfo();
124 if (!GetIsPreloadRequest() &&
125 QueryIsUpdatedReadonlyPackage(pkgid, GetUid()))
126 return RequestType::ReadonlyUpdateUninstall;
127 else if (GetIsPreloadRequest() && GetIsPartialRW())
128 return RequestType::PartialUninstall;
130 return RequestType::Uninstall;
132 case PKGMGR_REQ_REINSTALL:
133 return RequestType::Reinstall;
134 case PKGMGR_REQ_CLEAR:
135 return RequestType::Clear;
136 case PKGMGR_REQ_RECOVER:
137 return RequestType::Recovery;
138 case PKGMGR_REQ_MOVE:
139 return RequestType::Move;
140 case PKGMGR_REQ_MANIFEST_DIRECT_INSTALL:
141 if (!is_app_installed_.get()) {
142 if (GetIsPreloadRequest() && GetIsPartialRW())
143 return RequestType::ManifestPartialInstall;
145 return RequestType::ManifestDirectInstall;
147 if (GetIsPreloadRequest() && GetIsPartialRW())
148 return RequestType::ManifestPartialUpdate;
150 return RequestType::ManifestDirectUpdate;
152 case PKGMGR_REQ_MOUNT_INSTALL:
153 if (!is_app_installed_.get())
154 return RequestType::MountInstall;
156 return RequestType::MountUpdate;
157 case PKGMGR_REQ_DISABLE_PKG:
158 return RequestType::DisablePkg;
159 case PKGMGR_REQ_ENABLE_PKG:
160 return RequestType::EnablePkg;
162 return RequestType::Unknown;
166 uid_t PkgMgrInterface::GetUid() const {
167 return pkgmgr_installer_get_uid(pi_);
170 std::string PkgMgrInterface::GetRequestInfo() const {
172 const char* raw_info = pkgmgr_installer_get_request_info(pi_);
173 if (raw_info == nullptr)
175 // to support legacy content directory path,
176 // if the requested path starts with "/opt/usr/media", return modified path.
177 // note that "/opt/usr/media" is a mount point for TZ_USER_CONTENT.
178 if (!strncmp(raw_info, kLegacyContentPath, strlen(kLegacyContentPath))) {
179 tzplatform_set_user(GetUid());
180 const char* content_path = tzplatform_getenv(TZ_USER_CONTENT);
181 tzplatform_reset_user();
182 info = std::string(content_path) +
183 std::string(raw_info + strlen(kLegacyContentPath));
185 info = std::string(raw_info);
190 boost::filesystem::path PkgMgrInterface::GetTepPath() const {
191 if (pkgmgr_installer_get_tep_path(pi_) == nullptr)
192 return boost::filesystem::path("");
194 return boost::filesystem::path(pkgmgr_installer_get_tep_path(pi_));
197 bool PkgMgrInterface::GetIsTepMove() const {
198 return (pkgmgr_installer_get_tep_move_type(pi_) == 1);
201 bool PkgMgrInterface::GetIsMoveToExternal() const {
202 return (pkgmgr_installer_get_move_type(pi_) == 1);
205 bool PkgMgrInterface::GetIsPreloadRequest() const {
206 return (pkgmgr_installer_get_is_preload(pi_) == 1);
209 bool PkgMgrInterface::GetIsPreloadRWRequest() const {
210 return (pkgmgr_installer_get_is_preload_rw(pi_) == 1);
213 bool PkgMgrInterface::GetIsKeepRWData() const {
214 return (pkgmgr_installer_get_keep_rwdata(pi_) == 1);
217 bool PkgMgrInterface::GetIsPartialRW() const {
218 return (pkgmgr_installer_get_partial_rw(pi_) == 1);
221 std::unique_ptr<PkgmgrSignal> PkgMgrInterface::CreatePkgmgrSignal() const {
222 if (!pkgmgr_installer_interface_->ShouldCreateSignal())
224 return std::unique_ptr<PkgmgrSignal>(new PkgmgrSignal(pi_, GetRequestType()));
227 bool PkgMgrInterface::GetIsForceRemoval() const {
228 return (pkgmgr_installer_get_force_removal(pi_) == 1);
231 bool PkgMgrInterface::GetIsNoRemoval() const {
232 return (pkgmgr_installer_get_no_removal(pi_) == 1);
235 } // namespace common_installer