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.currentPercentage = 1;
49 upParams.callbackProgress = std::bind(&ABUpdate::SetProgress, this, std::placeholders::_1);
50 UpdaterStatus updateRet = DoInstallUpdaterPackage(pkgManager, upParams, HOTA_UPDATE);
51 if (updateRet != UPDATE_SUCCESS) {
52 LOG(INFO) << "Install package failed!";
53 STAGE(UPDATE_STAGE_FAIL) << "Install package failed";
54 Hpackage::PkgManager::ReleasePackageInstance(pkgManager);
55 if (!DeleteUpdaterPath(GetWorkPath()) || !DeleteUpdaterPath(std::string(UPDATER_PATH))) {
56 LOG(WARNING) << "Delete Work Path fail.";
57 }
58 return updateRet;
59 }
60 LOG(INFO) << "Install package successfully!";
61 STAGE(UPDATE_STAGE_SUCCESS) << "Install package success";
62 Hpackage::PkgManager::ReleasePackageInstance(pkgManager);
63 if (!DeleteUpdaterPath(GetWorkPath()) || !DeleteUpdaterPath(std::string(UPDATER_PATH))) {
64 LOG(WARNING) << "Delete Work Path fail.";
65 }
66 return updateRet;
67 }
68
PerformAction()69 void ABUpdate::PerformAction()
70 {
71 InstallerErrCode errCode = SYS_UPDATE_SUCCESS;
72 std::string errStr = "";
73 UpdaterStatus updateRet = UpdaterStatus::UPDATE_SUCCESS;
74 Detail::ScopeGuard guard([&] {
75 LOG(INFO) << "PerformAction ret:" << updateRet;
76 if (updateRet != UpdaterStatus::UPDATE_SUCCESS) {
77 errCode = SYS_INSTALL_PARA_FAIL;
78 errStr = std::to_string(updateRet);
79 }
80 if (actionCallBack_ != nullptr) {
81 actionCallBack_(errCode, errStr);
82 }
83 });
84
85 updateRet = StartABUpdate(pkgPath_);
86 }
87
SetProgress(float value)88 void ABUpdate::SetProgress(float value)
89 {
90 if (statusManager_ == nullptr) {
91 LOG(ERROR) << "statusManager_ nullptr";
92 return;
93 }
94 statusManager_->SetUpdatePercent(static_cast<int>(value));
95 }
96 } // namespace SysInstaller
97 } // namespace OHOS
98