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 "quick_fix_manager_host_impl.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "bundle_permission_mgr.h"
21 #include "bundle_util.h"
22 #include "quick_fix_data_mgr.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
QuickFixManagerHostImpl()26 QuickFixManagerHostImpl::QuickFixManagerHostImpl()
27 {
28 APP_LOGI("create QuickFixManagerHostImpl");
29 }
30
~QuickFixManagerHostImpl()31 QuickFixManagerHostImpl::~QuickFixManagerHostImpl()
32 {
33 APP_LOGI("destory QuickFixManagerHostImpl");
34 }
35
DeployQuickFix(const std::vector<std::string> & bundleFilePaths,const sptr<IQuickFixStatusCallback> & statusCallback)36 ErrCode QuickFixManagerHostImpl::DeployQuickFix(const std::vector<std::string> &bundleFilePaths,
37 const sptr<IQuickFixStatusCallback> &statusCallback)
38 {
39 APP_LOGI("QuickFixManagerHostImpl::DeployQuickFix start");
40 if (bundleFilePaths.empty() || (statusCallback == nullptr)) {
41 APP_LOGE("QuickFixManagerHostImpl::DeployQuickFix wrong parms");
42 return ERR_BUNDLEMANAGER_QUICK_FIX_PARAM_ERROR;
43 }
44 if (!GetQuickFixMgr()) {
45 APP_LOGE("QuickFixManagerHostImpl::DeployQuickFix quickFixerMgr is nullptr");
46 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
47 }
48
49 return quickFixMgr_->DeployQuickFix(bundleFilePaths, statusCallback);
50 }
51
SwitchQuickFix(const std::string & bundleName,bool enable,const sptr<IQuickFixStatusCallback> & statusCallback)52 ErrCode QuickFixManagerHostImpl::SwitchQuickFix(const std::string &bundleName, bool enable,
53 const sptr<IQuickFixStatusCallback> &statusCallback)
54 {
55 APP_LOGI("QuickFixManagerHostImpl::SwitchQuickFix start");
56 if (bundleName.empty() || (statusCallback == nullptr)) {
57 APP_LOGE("QuickFixManagerHostImpl::SwitchQuickFix wrong parms");
58 return ERR_BUNDLEMANAGER_QUICK_FIX_PARAM_ERROR;
59 }
60 if (!GetQuickFixMgr()) {
61 APP_LOGE("QuickFixManagerHostImpl::SwitchQuickFix quickFixerMgr is nullptr");
62 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
63 }
64
65 return quickFixMgr_->SwitchQuickFix(bundleName, enable, statusCallback);
66 }
67
DeleteQuickFix(const std::string & bundleName,const sptr<IQuickFixStatusCallback> & statusCallback)68 ErrCode QuickFixManagerHostImpl::DeleteQuickFix(const std::string &bundleName,
69 const sptr<IQuickFixStatusCallback> &statusCallback)
70 {
71 APP_LOGI("QuickFixManagerHostImpl::DeleteQuickFix start");
72 if (bundleName.empty() || (statusCallback == nullptr)) {
73 APP_LOGE("QuickFixManagerHostImpl::DeleteQuickFix wrong parms");
74 return ERR_BUNDLEMANAGER_QUICK_FIX_PARAM_ERROR;
75 }
76 if (!GetQuickFixMgr()) {
77 APP_LOGE("QuickFixManagerHostImpl::DeleteQuickFix quickFixerMgr is nullptr");
78 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
79 }
80
81 return quickFixMgr_->DeleteQuickFix(bundleName, statusCallback);
82 }
83
CreateFd(const std::string & fileName,int32_t & fd,std::string & path)84 ErrCode QuickFixManagerHostImpl::CreateFd(const std::string &fileName, int32_t &fd, std::string &path)
85 {
86 APP_LOGD("QuickFixManagerHostImpl::CreateFd start.");
87 if (!BundlePermissionMgr::VerifyCallingPermission(Constants::PERMISSION_INSTALL_BUNDLE)) {
88 APP_LOGE("verify install permission failed.");
89 return ERR_BUNDLEMANAGER_QUICK_FIX_PERMISSION_DENIED;
90 }
91 if (!BundleUtil::CheckFileType(fileName, Constants::QUICK_FIX_FILE_SUFFIX)) {
92 APP_LOGE("not quick fix file.");
93 return ERR_BUNDLEMANAGER_QUICK_FIX_PARAM_ERROR;
94 }
95 if (!IsFileNameValid(fileName)) {
96 APP_LOGE("invalid fileName");
97 return ERR_BUNDLEMANAGER_QUICK_FIX_PARAM_ERROR;
98 }
99 std::string tmpDir = BundleUtil::CreateInstallTempDir(++id_, DirType::QUICK_FIX_DIR);
100 if (tmpDir.empty()) {
101 APP_LOGE("create tmp dir failed.");
102 return ERR_BUNDLEMANAGER_QUICK_FIX_CREATE_TARGET_DIR_FAILED;
103 }
104 path = tmpDir + fileName;
105 if ((fd = BundleUtil::CreateFileDescriptor(path, 0)) < 0) {
106 APP_LOGE("create file descriptor failed.");
107 BundleUtil::DeleteDir(tmpDir);
108 return ERR_BUNDLEMANAGER_QUICK_FIX_CREATE_FD_FAILED;
109 }
110 return ERR_OK;
111 }
112
GetQuickFixMgr()113 bool QuickFixManagerHostImpl::GetQuickFixMgr()
114 {
115 if (quickFixMgr_ == nullptr) {
116 auto quickFixerRunner = EventRunner::Create(Constants::QUICK_FIX_MGR);
117 if (quickFixerRunner == nullptr) {
118 APP_LOGE("create quickFixer runner fail");
119 return false;
120 }
121 quickFixMgr_ = std::make_shared<QuickFixMgr>(quickFixerRunner);
122 }
123 return true;
124 }
125
IsFileNameValid(const std::string & fileName) const126 bool QuickFixManagerHostImpl::IsFileNameValid(const std::string &fileName) const
127 {
128 if (fileName.find("..") != std::string::npos
129 || fileName.find("/") != std::string::npos
130 || fileName.find("\\") != std::string::npos
131 || fileName.find("%") != std::string::npos) {
132 return false;
133 }
134 return true;
135 }
136 }
137 } // namespace OHOS
138