1 // Copyright (c) 2020 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/installer_runner.h"
7 #include <manifest_parser/utils/logging.h>
8 #include <pkgmgr_parser_db.h>
14 #include "common/installer/app_installer.h"
15 #include "common/installer_context.h"
16 #include "common/installer_factory.h"
17 #include "common/pkgmgr_interface.h"
19 namespace common_installer {
21 InstallerRunner::InstallerRunner(
22 std::unique_ptr<InstallerFactory> factory, PkgMgrPtr pkgmgr)
23 : factory_(std::move(factory)), pkgmgr_(pkgmgr) {
25 if (pkgmgr->GetInstallationMode() == InstallationMode::ONLINE)
26 pi_ = pkgmgr->CreatePkgmgrSignal();
29 bool InstallerRunner::NeedCheckDependency() {
30 RequestType req_type = pkgmgr_->GetRequestType();
31 if (req_type == RequestType::Move)
33 if (req_type == RequestType::Recovery)
35 if (req_type == RequestType::RecoveryUpdate)
37 if (req_type == RequestType::DisablePkg)
39 if (req_type == RequestType::EnablePkg)
41 if (req_type == RequestType::MigrateExtImg)
43 if (req_type == RequestType::RecoverDB)
49 bool InstallerRunner::SortInstallers(DependencyChecker* dep_checker) {
50 for (auto& installer : installers_) {
51 if (!dep_checker->AddInstaller(std::move(installer)))
54 installers_ = dep_checker->GetSortedAppInstallers();
55 if (installers_.empty())
61 AppInstaller::Result InstallerRunner::Run() {
62 AppInstaller::Result result = AppInstaller::Result::OK;
63 if (installers_.size() == 0)
66 if (getuid() != 0 && NeedCheckDependency()) {
67 DependencyChecker dep_checker(pkgmgr_);
68 if (!SortInstallers(&dep_checker)) {
69 LOG(ERROR) << dep_checker.GetErrorMessage();
71 pi_->SetRequestType(pkgmgr_->GetRequestType());
72 pi_->SendStarted({}, {});
73 pi_->SendError(Step::Status::OPERATION_NOT_ALLOWED,
74 dep_checker.GetErrorMessage(), {}, {});
75 pi_->SendFinished(Step::Status::OPERATION_NOT_ALLOWED, {}, {});
77 return AppInstaller::Result::ERROR;
81 std::list<std::unique_ptr<AppInstaller>>::iterator it(installers_.begin());
82 for (; it != installers_.end(); ++it) {
83 result = (*it)->Process();
84 if (result != AppInstaller::Result::OK)
87 if (it != installers_.end() && result == AppInstaller::Result::ERROR) {
88 pkgmgr_parser_clear_cache_memory_db();
90 AppInstaller::Result ret = (*it)->Undo();
91 if (ret != AppInstaller::Result::OK && ret != AppInstaller::Result::ERROR)
92 result = AppInstaller::Result::UNDO_ERROR;
93 } while (it-- != installers_.begin());
95 pkgmgr_parser_clear_cache_memory_db();
96 if (pkgmgr_->GetRequestType() != RequestType::Recovery)
97 global_recovery_->AppendCleanUp();
100 // Clean operation always succeeds
102 } while (it-- != installers_.begin());
108 void InstallerRunner::Init() {
109 global_recovery_ = std::make_unique<GlobalRecoveryFile>(pkgmgr_);
110 bool is_global_request = (pkgmgr_->GetRequestInfoCount() > 1) ? true : false;
111 if (pkgmgr_->GetRequestType() != RequestType::Recovery &&
113 if (!global_recovery_->Init())
116 for (int i = 0; i < pkgmgr_->GetRequestInfoCount(); i++) {
117 std::unique_ptr<AppInstaller> installer =
118 factory_->CreateInstaller(pkgmgr_, i);
120 LOG(ERROR) << "Failed to create installer for "
121 << pkgmgr_->GetRequestInfo(i);
124 installer->SetIndex(i);
125 if (pkgmgr_->GetRequestType() != RequestType::Recovery &&
127 std::string recovery_filepath =
128 global_recovery_->AddPathWithType(
129 installer->context_->pkg_type.get());
130 installer->context_->recovery_info.set(RecoveryInfo(recovery_filepath));
132 installers_.emplace_back(std::move(installer));
136 } // namespace common_installer