1 /*
2 * Copyright (c) 2021-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 "installd_client.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "installd_death_recipient.h"
21 #include "system_ability_definition.h"
22 #include "system_ability_helper.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
CreateBundleDir(const std::string & bundleDir)26 ErrCode InstalldClient::CreateBundleDir(const std::string &bundleDir)
27 {
28 if (bundleDir.empty()) {
29 APP_LOGE("bundle dir is empty");
30 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
31 }
32
33 return CallService(&IInstalld::CreateBundleDir, bundleDir);
34 }
35
ExtractModuleFiles(const std::string & srcModulePath,const std::string & targetPath,const std::string & targetSoPath,const std::string & cpuAbi)36 ErrCode InstalldClient::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath,
37 const std::string &targetSoPath, const std::string &cpuAbi)
38 {
39 if (srcModulePath.empty() || targetPath.empty()) {
40 APP_LOGE("src module path or target path is empty");
41 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
42 }
43
44 return CallService(&IInstalld::ExtractModuleFiles, srcModulePath, targetPath, targetSoPath, cpuAbi);
45 }
46
ExtractFiles(const ExtractParam & extractParam)47 ErrCode InstalldClient::ExtractFiles(const ExtractParam &extractParam)
48 {
49 if (extractParam.srcPath.empty() || extractParam.targetPath.empty()) {
50 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
51 }
52 return CallService(&IInstalld::ExtractFiles, extractParam);
53 }
54
RenameModuleDir(const std::string & oldPath,const std::string & newPath)55 ErrCode InstalldClient::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
56 {
57 if (oldPath.empty() || newPath.empty()) {
58 APP_LOGE("rename path is empty");
59 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
60 }
61
62 return CallService(&IInstalld::RenameModuleDir, oldPath, newPath);
63 }
64
CreateBundleDataDir(const std::string & bundleName,const int userid,const int uid,const int gid,const std::string & apl)65 ErrCode InstalldClient::CreateBundleDataDir(const std::string &bundleName,
66 const int userid, const int uid, const int gid, const std::string &apl)
67 {
68 if (bundleName.empty() || userid < 0 || uid < 0 || gid < 0) {
69 APP_LOGE("params are invalid");
70 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
71 }
72
73 return CallService(&IInstalld::CreateBundleDataDir, bundleName, userid, uid, gid, apl);
74 }
75
RemoveBundleDataDir(const std::string & bundleName,const int userid)76 ErrCode InstalldClient::RemoveBundleDataDir(
77 const std::string &bundleName, const int userid)
78 {
79 if (bundleName.empty() || userid < 0) {
80 APP_LOGE("params are invalid");
81 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
82 }
83
84 return CallService(&IInstalld::RemoveBundleDataDir, bundleName, userid);
85 }
86
RemoveModuleDataDir(const std::string & ModuleName,const int userid)87 ErrCode InstalldClient::RemoveModuleDataDir(const std::string &ModuleName, const int userid)
88 {
89 if (ModuleName.empty() || userid < 0) {
90 APP_LOGE("params are invalid");
91 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
92 }
93
94 return CallService(&IInstalld::RemoveModuleDataDir, ModuleName, userid);
95 }
96
RemoveDir(const std::string & dir)97 ErrCode InstalldClient::RemoveDir(const std::string &dir)
98 {
99 if (dir.empty()) {
100 APP_LOGE("dir removed is empty");
101 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
102 }
103
104 return CallService(&IInstalld::RemoveDir, dir);
105 }
106
CleanBundleDataDir(const std::string & bundleDir)107 ErrCode InstalldClient::CleanBundleDataDir(const std::string &bundleDir)
108 {
109 if (bundleDir.empty()) {
110 APP_LOGE("bundle dir is empty");
111 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
112 }
113
114 return CallService(&IInstalld::CleanBundleDataDir, bundleDir);
115 }
116
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats)117 ErrCode InstalldClient::GetBundleStats(
118 const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats)
119 {
120 if (bundleName.empty()) {
121 APP_LOGE("bundleName is empty");
122 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
123 }
124
125 return CallService(&IInstalld::GetBundleStats, bundleName, userId, bundleStats);
126 }
127
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl)128 ErrCode InstalldClient::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl)
129 {
130 if (dir.empty() || bundleName.empty() || apl.empty()) {
131 APP_LOGE("params are invalid");
132 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
133 }
134
135 return CallService(&IInstalld::SetDirApl, dir, bundleName, apl);
136 }
137
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)138 ErrCode InstalldClient::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
139 {
140 if (dir.empty()) {
141 APP_LOGE("params are invalid");
142 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
143 }
144
145 return CallService(&IInstalld::GetBundleCachePath, dir, cachePath);
146 }
147
ResetInstalldProxy()148 void InstalldClient::ResetInstalldProxy()
149 {
150 if ((installdProxy_ != nullptr) && (installdProxy_->AsObject() != nullptr)) {
151 installdProxy_->AsObject()->RemoveDeathRecipient(recipient_);
152 }
153 installdProxy_ = nullptr;
154 }
155
GetInstalldProxy()156 bool InstalldClient::GetInstalldProxy()
157 {
158 if (installdProxy_ == nullptr) {
159 APP_LOGD("try to get installd proxy");
160 std::lock_guard<std::mutex> lock(mutex_);
161 if (installdProxy_ == nullptr) {
162 sptr<IInstalld> tempProxy =
163 iface_cast<IInstalld>(SystemAbilityHelper::GetSystemAbility(INSTALLD_SERVICE_ID));
164 if ((tempProxy == nullptr) || (tempProxy->AsObject() == nullptr)) {
165 APP_LOGE("the installd proxy or remote object is null");
166 return false;
167 }
168 recipient_ = new (std::nothrow) InstalldDeathRecipient();
169 if (recipient_ == nullptr) {
170 APP_LOGE("the death recipient is nullptr");
171 return false;
172 }
173 tempProxy->AsObject()->AddDeathRecipient(recipient_);
174 installdProxy_ = tempProxy;
175 }
176 }
177 return true;
178 }
179
ScanDir(const std::string & dir,ScanMode scanMode,ResultMode resultMode,std::vector<std::string> & paths)180 ErrCode InstalldClient::ScanDir(
181 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
182 {
183 if (dir.empty()) {
184 APP_LOGE("params are invalid");
185 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
186 }
187
188 return CallService(&IInstalld::ScanDir, dir, scanMode, resultMode, paths);
189 }
190
MoveFile(const std::string & oldPath,const std::string & newPath)191 ErrCode InstalldClient::MoveFile(const std::string &oldPath, const std::string &newPath)
192 {
193 if (oldPath.empty() || newPath.empty()) {
194 APP_LOGE("params are invalid");
195 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
196 }
197
198 return CallService(&IInstalld::MoveFile, oldPath, newPath);
199 }
200
CopyFile(const std::string & oldPath,const std::string & newPath)201 ErrCode InstalldClient::CopyFile(const std::string &oldPath, const std::string &newPath)
202 {
203 if (oldPath.empty() || newPath.empty()) {
204 APP_LOGE("params are invalid");
205 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
206 }
207
208 return CallService(&IInstalld::CopyFile, oldPath, newPath);
209 }
210
Mkdir(const std::string & dir,const int32_t mode,const int32_t uid,const int32_t gid)211 ErrCode InstalldClient::Mkdir(
212 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid)
213 {
214 if (dir.empty()) {
215 APP_LOGE("params are invalid");
216 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
217 }
218
219 return CallService(&IInstalld::Mkdir, dir, mode, uid, gid);
220 }
221
GetFileStat(const std::string & file,FileStat & fileStat)222 ErrCode InstalldClient::GetFileStat(const std::string &file, FileStat &fileStat)
223 {
224 if (file.empty()) {
225 APP_LOGE("params are invalid");
226 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
227 }
228
229 return CallService(&IInstalld::GetFileStat, file, fileStat);
230 }
231
ExtractDiffFiles(const std::string & filePath,const std::string & targetPath,const std::string & cpuAbi)232 ErrCode InstalldClient::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
233 const std::string &cpuAbi)
234 {
235 if (filePath.empty() || targetPath.empty() || cpuAbi.empty()) {
236 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
237 }
238 return CallService(&IInstalld::ExtractDiffFiles, filePath, targetPath, cpuAbi);
239 }
240
ApplyDiffPatch(const std::string & oldSoPath,const std::string & diffFilePath,const std::string & newSoPath)241 ErrCode InstalldClient::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
242 const std::string &newSoPath)
243 {
244 if (oldSoPath.empty() || diffFilePath.empty() || newSoPath.empty()) {
245 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
246 }
247 return CallService(&IInstalld::ApplyDiffPatch, oldSoPath, diffFilePath, newSoPath);
248 }
249
IsExistDir(const std::string & dir,bool & isExist)250 ErrCode InstalldClient::IsExistDir(const std::string &dir, bool &isExist)
251 {
252 return CallService(&IInstalld::IsExistDir, dir, isExist);
253 }
254
IsDirEmpty(const std::string & dir,bool & isDirEmpty)255 ErrCode InstalldClient::IsDirEmpty(const std::string &dir, bool &isDirEmpty)
256 {
257 return CallService(&IInstalld::IsDirEmpty, dir, isDirEmpty);
258 }
259
ObtainQuickFixFileDir(const std::string & dir,std::vector<std::string> & dirVec)260 ErrCode InstalldClient::ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec)
261 {
262 return CallService(&IInstalld::ObtainQuickFixFileDir, dir, dirVec);
263 }
264
CopyFiles(const std::string & sourceDir,const std::string & destinationDir)265 ErrCode InstalldClient::CopyFiles(const std::string &sourceDir, const std::string &destinationDir)
266 {
267 return CallService(&IInstalld::CopyFiles, sourceDir, destinationDir);
268 }
269 } // namespace AppExecFwk
270 } // namespace OHOS
271