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/external_storage.h"
9 #include <manifest_parser/utils/logging.h>
11 #include "common/utils/byte_size_literals.h"
12 #include "common/utils/file_util.h"
14 namespace bf = boost::filesystem;
18 const char kWgtType[] = "wgt";
19 const char kExternalDirForWgt[] = "res";
21 const std::vector<std::string> kExternalDirsForTpk = {
27 int64_t SizeInMB(int64_t size) {
28 return (size + 1_MB - 1) / 1_MB;
33 namespace common_installer {
35 ExternalStorage::ExternalStorage(RequestType type,
36 const std::string& pkgid, const std::string& package_type,
37 const boost::filesystem::path& application_root, uid_t uid)
40 package_type_(package_type),
41 application_root_(application_root),
44 if (package_type_ == kWgtType) {
45 external_dirs_.push_back(kExternalDirForWgt);
47 external_dirs_ = kExternalDirsForTpk;
52 ExternalStorage::ExternalStorage(RequestType type,
53 const std::string& pkgid, const std::string& package_type,
54 const boost::filesystem::path& application_root, uid_t uid,
55 bool is_external_move)
58 package_type_(package_type),
59 application_root_(application_root),
62 if (package_type_ == kWgtType) {
63 external_dirs_.push_back(kExternalDirForWgt);
65 external_dirs_ = kExternalDirsForTpk;
68 move_type_ = APP2EXT_MOVE_TO_EXT;
70 move_type_ = APP2EXT_MOVE_TO_PHONE;
73 ExternalStorage::~ExternalStorage() {
75 app2ext_deinit(handle_);
78 bool ExternalStorage::Finalize(bool success) {
79 int ret = APP2EXT_STATUS_SUCCESS;
81 case RequestType::Install:
82 ret = handle_->interface.client_usr_post_install(pkgid_.c_str(),
83 success ? APP2EXT_STATUS_SUCCESS : APP2EXT_STATUS_FAILED, uid_);
85 case RequestType::Update:
86 case RequestType::Delta:
87 ret = handle_->interface.client_usr_post_upgrade(pkgid_.c_str(),
88 success ? APP2EXT_STATUS_SUCCESS : APP2EXT_STATUS_FAILED, uid_);
90 case RequestType::Uninstall:
91 ret = handle_->interface.client_usr_post_uninstall(pkgid_.c_str(), uid_);
93 case RequestType::Move:
94 ret = handle_->interface.client_usr_post_move(pkgid_.c_str(),
95 static_cast<app2ext_move_type_t>(move_type_),
99 assert(false && "Not supported installation mode");
101 return ret == APP2EXT_STATUS_SUCCESS;
104 bool ExternalStorage::Commit() {
105 return Finalize(true);
108 bool ExternalStorage::Abort() {
109 return Finalize(false);
112 const std::vector<std::string>& ExternalStorage::external_dirs() const {
113 return external_dirs_;
116 bool ExternalStorage::Initialize(
117 const boost::filesystem::path& space_requirement) {
118 // external size in MB, set any-non zero size as default
119 int external_size = 1;
121 if (!space_requirement.empty()) {
122 if (package_type_ == kWgtType) {
123 for (auto& dir : kExternalDirsForTpk) {
124 bf::path requirement = space_requirement / dir;
125 if (!bf::exists(requirement))
128 SizeInMB(GetDirectorySize(requirement));
131 // for wgt whole content of package goes to res/
133 SizeInMB(GetDirectorySize(space_requirement));
137 if (external_size == 0)
140 handle_ = app2ext_init(APP2EXT_SD_CARD);
142 LOG(ERROR) << "app2ext_init() failed";
146 GList* glist = nullptr;
147 for (auto& dir : external_dirs_) {
148 app2ext_dir_details* dir_detail = reinterpret_cast<app2ext_dir_details*>(
149 calloc(1, sizeof(app2ext_dir_details)));
150 dir_detail->name = strdup(dir.c_str());
151 dir_detail->type = APP2EXT_DIR_RO;
152 glist = g_list_append(glist, dir_detail);
157 case RequestType::Install:
158 ret = handle_->interface.client_usr_force_clean(pkgid_.c_str(), uid_);
160 LOG(ERROR) << "force_clean failed";
163 ret = handle_->interface.client_usr_pre_install(pkgid_.c_str(), glist,
164 external_size, uid_);
166 case RequestType::Update:
167 case RequestType::Delta:
168 ret = handle_->interface.client_usr_pre_upgrade(pkgid_.c_str(), glist,
169 external_size, uid_);
171 case RequestType::Move:
172 if (move_type_ == -1) {
173 LOG(ERROR) << "Invalid request [" << move_type_ << "]";
178 // try umount before move
179 ret = handle_->interface.client_usr_disable(pkgid_.c_str(), uid_);
181 ret = handle_->interface.client_usr_pre_move(pkgid_.c_str(), glist,
182 static_cast<app2ext_move_type_t>(move_type_),
185 case RequestType::Uninstall:
186 ret = handle_->interface.client_usr_pre_uninstall(pkgid_.c_str(), uid_);
188 case RequestType::Clear:
189 case RequestType::Reinstall:
190 case RequestType::Recovery:
191 case RequestType::ManifestDirectInstall:
192 case RequestType::ManifestDirectUpdate:
193 case RequestType::MountInstall:
194 case RequestType::MountUpdate:
195 LOG(ERROR) << "Installation type is not supported by external installation";
199 assert(false && "Invalid installation mode");
202 g_list_free_full(glist, [](gpointer data) {
203 app2ext_dir_details* dir_detail =
204 reinterpret_cast<app2ext_dir_details*>(data);
205 free(dir_detail->name);
211 std::unique_ptr<ExternalStorage> ExternalStorage::MoveInstalledStorage(
212 RequestType type, const boost::filesystem::path& application_root,
213 const std::string& pkgid, const std::string& package_type,
214 uid_t uid, bool is_external_move) {
216 std::unique_ptr<ExternalStorage> external_storage(
217 new ExternalStorage(type, pkgid, package_type, application_root, uid,
219 if (!external_storage->Initialize(application_root)) {
220 LOG(WARNING) << "Cannot initialize external storage for move";
224 return external_storage;
227 std::unique_ptr<ExternalStorage> ExternalStorage::AcquireExternalStorage(
228 RequestType type, const boost::filesystem::path& application_root,
229 const std::string& pkgid, const std::string& package_type,
230 const boost::filesystem::path& space_requirement,
232 std::unique_ptr<ExternalStorage> external_storage(
233 new ExternalStorage(type, pkgid, package_type, application_root, uid));
234 if (!external_storage->Initialize(space_requirement)) {
235 LOG(WARNING) << "Cannot initialize external storage for request";
238 return external_storage;
241 } // namespace common_installer