1 // Copyright (c) 2016 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/filesystem/step_migrate_legacy_external_image.h"
7 #include <app2ext_interface.h>
8 #include <tzplatform_config.h>
13 #include "common/security_registration.h"
14 #include "common/utils/pkgmgr_query.h"
16 namespace fs = std::filesystem;
20 const uid_t kDefaultUserUid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
21 const uid_t kGlobalUserUid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
22 const char kInstalledExternally[] = "installed_external";
26 namespace common_installer {
29 Step::Status StepMigrateLegacyExtImage::precheck() {
30 // Policy : migration target user is 'default' user.
31 uid_t uid = context_->uid.get();
32 if (uid != kDefaultUserUid && uid != kGlobalUserUid) {
33 LOG(ERROR) << "current target uid (" << uid <<") is not allowed";
34 return Step::Status::INVALID_VALUE;
37 if (context_->GetPkgPath().empty()) {
38 LOG(ERROR) << "pkg_path attribute is empty";
39 return Step::Status::INVALID_VALUE;
41 if (!std::filesystem::exists(context_->GetPkgPath())) {
42 LOG(ERROR) << "pkg_path ("
43 << context_->GetPkgPath()
44 << ") path does not exist";
45 return Step::Status::INVALID_VALUE;
48 if (context_->pkgid.get().empty()) {
49 LOG(ERROR) << "pkgid attribute is empty";
50 return Step::Status::INVALID_VALUE;
56 Step::Status StepMigrateLegacyExtImage::process() {
57 std::string pkgid = context_->pkgid.get();
58 uid_t uid = context_->uid.get();
59 PkgQueryInterface pkg_query(pkgid, uid);
60 if (pkg_query.IsGlobalPackage())
61 context_->uid.set(kGlobalUserUid);
63 std::string storage_str = pkg_query.StorageForPkgId();
64 if (strcmp(storage_str.c_str(), kInstalledExternally)) {
65 LOG(ERROR) << "Pkg not installed at external storage";
66 return Step::Status::ERROR;
69 context_->external_storage =
70 ExternalStorage::MigrateExternalStorage(context_->request_type.get(),
71 context_->pkgid.get(),
73 if (!context_->external_storage) {
74 LOG(ERROR) << "Can not initialize external storage";
75 return Step::Status::ERROR;
78 std::string error_message;
79 if (!RegisterSecurityContextForPathExternalOnly(pkgid,
80 context_->pkg_type.get(), context_->GetPkgPath(),
81 uid, &error_message)) {
82 if (!error_message.empty()) {
83 LOG(ERROR) << "error_message: " << error_message;
84 on_error_->on_error(Status::SECURITY_ERROR, error_message);
86 return Status::SECURITY_ERROR;
89 LOG(DEBUG) << "Security context installed";
93 Step::Status StepMigrateLegacyExtImage::undo() {
94 if (context_->external_storage)
95 context_->external_storage->Abort();
99 Step::Status StepMigrateLegacyExtImage::clean() {
100 if (context_->external_storage)
101 context_->external_storage->Commit();
105 } // namespace security
106 } // namespace common_installer