1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ab_update.h"
17
18 #include "log/log.h"
19 #include "package/package.h"
20 #include "package/pkg_manager.h"
21 #include "scope_guard.h"
22 #include "utils.h"
23 #include "updater/updater_const.h"
24
25 namespace OHOS {
26 namespace SysInstaller {
27 using namespace Updater;
28
StartABUpdate(const std::string & pkgPath)29 UpdaterStatus ABUpdate::StartABUpdate(const std::string &pkgPath)
30 {
31 LOG(INFO) << "StartABUpdate start";
32 if (statusManager_ == nullptr) {
33 LOG(ERROR) << "statusManager_ nullptr";
34 return UPDATE_ERROR;
35 }
36
37 Hpackage::PkgManager::PkgManagerPtr pkgManager = Hpackage::PkgManager::CreatePackageInstance();
38 if (pkgManager == nullptr) {
39 LOG(ERROR) << "pkgManager is nullptr";
40 return UPDATE_ERROR;
41 }
42
43 STAGE(UPDATE_STAGE_BEGIN) << "StartABUpdate start";
44 LOG(INFO) << "ABUpdate start, pkg updaterPath : " << pkgPath.c_str();
45
46 UpdaterParams upParams;
47 upParams.updatePackage = {pkgPath};
48 upParams.initialProgress = statusManager_->GetUpdateProgress();
49 upParams.currentPercentage = 1 - upParams.initialProgress;
50 upParams.callbackProgress = std::bind(&ABUpdate::SetProgress, this, std::placeholders::_1);
51 UpdaterStatus updateRet = DoInstallUpdaterPackage(pkgManager, upParams, HOTA_UPDATE);
52 if (updateRet != UPDATE_SUCCESS) {
53 LOG(INFO) << "Install package failed!";
54 STAGE(UPDATE_STAGE_FAIL) << "Install package failed";
55 Hpackage::PkgManager::ReleasePackageInstance(pkgManager);
56 if (!DeleteUpdaterPath(GetWorkPath()) || !DeleteUpdaterPath(std::string(UPDATER_PATH))) {
57 LOG(WARNING) << "Delete Work Path fail.";
58 }
59 return updateRet;
60 }
61 LOG(INFO) << "Install package successfully!";
62 STAGE(UPDATE_STAGE_SUCCESS) << "Install package success";
63 Hpackage::PkgManager::ReleasePackageInstance(pkgManager);
64 if (!DeleteUpdaterPath(GetWorkPath()) || !DeleteUpdaterPath(std::string(UPDATER_PATH))) {
65 LOG(WARNING) << "Delete Work Path fail.";
66 }
67 return updateRet;
68 }
69
PerformAction()70 void ABUpdate::PerformAction()
71 {
72 InstallerErrCode errCode = SYS_UPDATE_SUCCESS;
73 std::string errStr = "";
74 UpdaterStatus updateRet = UpdaterStatus::UPDATE_SUCCESS;
75 Detail::ScopeGuard guard([&] {
76 LOG(INFO) << "PerformAction ret:" << updateRet;
77 if (updateRet != UpdaterStatus::UPDATE_SUCCESS) {
78 errCode = SYS_INSTALL_PARA_FAIL;
79 errStr = std::to_string(updateRet);
80 }
81 if (actionCallBack_ != nullptr) {
82 actionCallBack_(errCode, errStr);
83 }
84 });
85
86 updateRet = StartABUpdate(pkgPath_);
87 }
88
SetProgress(float value)89 void ABUpdate::SetProgress(float value)
90 {
91 if (statusManager_ == nullptr) {
92 LOG(ERROR) << "statusManager_ nullptr";
93 return;
94 }
95 statusManager_->SetUpdatePercent(static_cast<int>(value));
96 }
97 } // namespace SysInstaller
98 } // namespace OHOS
99