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) {
89 AppInstaller::Result ret = (*it)->Undo();
90 if (ret != AppInstaller::Result::OK && ret != AppInstaller::Result::ERROR)
91 result = AppInstaller::Result::UNDO_ERROR;
92 } while (it-- != installers_.begin());
94 if (pkgmgr_->GetRequestType() != RequestType::Recovery)
95 global_recovery_->AppendCleanUp();
98 // Clean operation always succeeds
100 } while (it-- != installers_.begin());
106 void InstallerRunner::Init() {
107 global_recovery_ = std::make_unique<GlobalRecoveryFile>(pkgmgr_);
108 bool is_global_request = (pkgmgr_->GetRequestInfoCount() > 1) ? true : false;
109 if (pkgmgr_->GetRequestType() != RequestType::Recovery &&
111 if (!global_recovery_->Init())
114 for (int i = 0; i < pkgmgr_->GetRequestInfoCount(); i++) {
115 std::unique_ptr<AppInstaller> installer =
116 factory_->CreateInstaller(pkgmgr_, i);
118 LOG(ERROR) << "Failed to create installer for "
119 << pkgmgr_->GetRequestInfo(i);
122 installer->SetIndex(i);
123 if (pkgmgr_->GetRequestType() != RequestType::Recovery &&
125 std::string recovery_filepath =
126 global_recovery_->AddPathWithType(
127 installer->context_->pkg_type.get());
128 installer->context_->recovery_info.set(RecoveryInfo(recovery_filepath));
130 installers_.emplace_back(std::move(installer));
134 } // namespace common_installer