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)29UpdaterStatus 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 UpdaterStatus updateRet = DoInstallUpdaterPackage(pkgManager, upParams, HOTA_UPDATE); 54 if (updateRet != UPDATE_SUCCESS) { 55 LOG(INFO) << "Install package failed!"; 56 STAGE(UPDATE_STAGE_FAIL) << "Install package failed"; 57 Hpackage::PkgManager::ReleasePackageInstance(pkgManager); 58 if (!DeleteUpdaterPath(GetWorkPath()) || !DeleteUpdaterPath(std::string(UPDATER_PATH))) { 59 LOG(WARNING) << "Delete Work Path fail."; 60 } 61 return updateRet; 62 } 63 LOG(INFO) << "Install package successfully!"; 64 STAGE(UPDATE_STAGE_SUCCESS) << "Install package success"; 65 66 // app hot patch need remount patch partition 67 if (pkgPath.find(PATCH_PACKAGE_NAME) != std::string::npos) { 68 SysInstallerManagerInit::GetInstance().InvokeEvent(SYS_APP_QUICKFIX_EVENT); 69 } 70 Hpackage::PkgManager::ReleasePackageInstance(pkgManager); 71 if (!DeleteUpdaterPath(GetWorkPath()) || !DeleteUpdaterPath(std::string(UPDATER_PATH))) { 72 LOG(WARNING) << "Delete Work Path fail."; 73 } 74 return updateRet; 75 } 76 PerformAction()77void ABUpdate::PerformAction() 78 { 79 InstallerErrCode errCode = SYS_UPDATE_SUCCESS; 80 std::string errStr = ""; 81 UpdaterStatus updateRet = UpdaterStatus::UPDATE_SUCCESS; 82 Detail::ScopeGuard guard([&] { 83 LOG(INFO) << "PerformAction ret:" << updateRet; 84 if (updateRet != UpdaterStatus::UPDATE_SUCCESS) { 85 errCode = SYS_INSTALL_PARA_FAIL; 86 errStr = std::to_string(updateRet); 87 } 88 if (actionCallBack_ != nullptr) { 89 actionCallBack_(errCode, errStr); 90 } 91 }); 92 93 updateRet = StartABUpdate(pkgPath_); 94 } 95 SetProgress(float value)96void ABUpdate::SetProgress(float value) 97 { 98 if (statusManager_ == nullptr) { 99 LOG(ERROR) << "statusManager_ nullptr"; 100 return; 101 } 102 statusManager_->SetUpdatePercent(static_cast<int>(value)); 103 } 104 } // namespace SysInstaller 105 } // namespace OHOS 106