• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static constexpr const char *PATCH_PACKAGE_NAME = "/updater.zip";
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 = [this](float value) {
51         this->SetProgress(value);
52         };
53     if ((pkgPath.find(PATCH_PACKAGE_NAME) != std::string::npos) &&
54         (SetUpdateSlotParam(upParams, true) != UPDATE_SUCCESS)) {
55         LOG(ERROR) << "set slot param failed";
56         Hpackage::PkgManager::ReleasePackageInstance(pkgManager);
57         return UPDATE_ERROR;
58     }
59     UpdaterStatus updateRet = DoInstallUpdaterPackage(pkgManager, upParams, HOTA_UPDATE);
60     if (updateRet != UPDATE_SUCCESS) {
61         LOG(INFO) << "Install package failed!";
62         STAGE(UPDATE_STAGE_FAIL) << "Install package failed";
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     LOG(INFO) << "Install package successfully!";
70     STAGE(UPDATE_STAGE_SUCCESS) << "Install package success";
71 
72     // app hot patch need remount patch partition
73     if (pkgPath.find(PATCH_PACKAGE_NAME) != std::string::npos) {
74         SysInstallerManagerInit::GetInstance().InvokeEvent(SYS_APP_QUICKFIX_EVENT);
75     }
76     Hpackage::PkgManager::ReleasePackageInstance(pkgManager);
77     if (!DeleteUpdaterPath(GetWorkPath()) || !DeleteUpdaterPath(std::string(UPDATER_PATH))) {
78         LOG(WARNING) << "Delete Work Path fail.";
79     }
80     return updateRet;
81 }
82 
PerformAction()83 void ABUpdate::PerformAction()
84 {
85     InstallerErrCode errCode = SYS_UPDATE_SUCCESS;
86     std::string errStr = "";
87     UpdaterStatus updateRet = UpdaterStatus::UPDATE_SUCCESS;
88     Detail::ScopeGuard guard([&] {
89         LOG(INFO) << "PerformAction ret:" << updateRet;
90         if (updateRet != UpdaterStatus::UPDATE_SUCCESS) {
91             errCode = SYS_INSTALL_PARA_FAIL;
92             errStr = std::to_string(updateRet);
93         }
94         if (actionCallBack_ != nullptr) {
95             actionCallBack_(errCode, errStr);
96         }
97     });
98 
99     updateRet = StartABUpdate(pkgPath_);
100 }
101 
SetProgress(float value)102 void ABUpdate::SetProgress(float value)
103 {
104     if (statusManager_ == nullptr) {
105         LOG(ERROR) << "statusManager_ nullptr";
106         return;
107     }
108     statusManager_->SetUpdatePercent(static_cast<int>(value));
109 }
110 } // namespace SysInstaller
111 } // namespace OHOS
112