1 /*
2  * Copyright (c) 2023 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 "firmware_install.h"
17 
18 #include <dirent.h>
19 #include <iostream>
20 #include <fstream>
21 #include <string>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <sys/statfs.h>
25 
26 #include "dupdate_errno.h"
27 #include "firmware_log.h"
28 #include "file_utils.h"
29 
30 namespace OHOS {
31 namespace UpdateEngine {
StartInstall(const std::vector<FirmwareComponent> & componentList,FirmwareInstallCallback & cb)32 void FirmwareInstall::StartInstall(const std::vector<FirmwareComponent> &componentList, FirmwareInstallCallback &cb)
33 {
34     if (IsInstalling()) {
35         FIRMWARE_LOGE("install is busying");
36         CallbackFailedResult(cb, "install is busying", DUPDATE_ERR_SYSTEM_BUSY_ON_INSTALL);
37         return;
38     }
39 
40     if (!IsComponentLegal(componentList)) {
41         FIRMWARE_LOGE("check component failed!");
42         CallbackFailedResult(cb, "check component failed!", DUPDATE_ERR_UPDATE_PRECHECK_FAIL);
43         return;
44     }
45 
46     SetIsInstalling(true);
47     onInstallCallback_ = cb;
48     bool result = PerformInstall(componentList);
49     SetIsInstalling(false);
50     CallbackResult(cb, result);
51 }
52 
IsInstalling()53 bool FirmwareInstall::IsInstalling()
54 {
55     std::lock_guard<std::mutex> lock(mutex_);
56     return isInstalling_;
57 }
58 
SetIsInstalling(bool isInstalling)59 void FirmwareInstall::SetIsInstalling(bool isInstalling)
60 {
61     std::lock_guard<std::mutex> lock(mutex_);
62     isInstalling_ = isInstalling;
63 }
64 
CallbackFailedResult(FirmwareInstallCallback & cb,const std::string & errorMsg,int32_t errCode)65 void FirmwareInstall::CallbackFailedResult(FirmwareInstallCallback &cb, const std::string &errorMsg, int32_t errCode)
66 {
67     ErrorMessage errMsg = { errCode, errorMsg };
68     cb.onFirmwareEvent(false, errMsg);
69 }
70 
CallbackResult(FirmwareInstallCallback & cb,const bool result)71 void FirmwareInstall::CallbackResult(FirmwareInstallCallback &cb, const bool result)
72 {
73     cb.onFirmwareEvent(result, errMsg_);
74 }
75 } // namespace UpdateEngine
76 } // namespace OHOS
77