• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "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 
ExecuteAOT(const AOTArgs & aotArgs)55 ErrCode InstalldClient::ExecuteAOT(const AOTArgs &aotArgs)
56 {
57     return CallService(&IInstalld::ExecuteAOT, aotArgs);
58 }
59 
RenameModuleDir(const std::string & oldPath,const std::string & newPath)60 ErrCode InstalldClient::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
61 {
62     if (oldPath.empty() || newPath.empty()) {
63         APP_LOGE("rename path is empty");
64         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
65     }
66 
67     return CallService(&IInstalld::RenameModuleDir, oldPath, newPath);
68 }
69 
CreateBundleDataDir(const CreateDirParam & createDirParam)70 ErrCode InstalldClient::CreateBundleDataDir(const CreateDirParam &createDirParam)
71 {
72     if (createDirParam.bundleName.empty() || createDirParam.userId < 0
73         || createDirParam.uid < 0 || createDirParam.gid < 0) {
74         APP_LOGE("params are invalid");
75         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
76     }
77 
78     return CallService(&IInstalld::CreateBundleDataDir, createDirParam);
79 }
80 
CreateBundleDataDirWithVector(const std::vector<CreateDirParam> & createDirParams)81 ErrCode InstalldClient::CreateBundleDataDirWithVector(const std::vector<CreateDirParam> &createDirParams)
82 {
83     return ERR_OK;
84 }
85 
RemoveBundleDataDir(const std::string & bundleName,const int userid)86 ErrCode InstalldClient::RemoveBundleDataDir(
87     const std::string &bundleName, const int userid)
88 {
89     if (bundleName.empty() || userid < 0) {
90         APP_LOGE("params are invalid");
91         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
92     }
93 
94     return CallService(&IInstalld::RemoveBundleDataDir, bundleName, userid);
95 }
96 
RemoveModuleDataDir(const std::string & ModuleName,const int userid)97 ErrCode InstalldClient::RemoveModuleDataDir(const std::string &ModuleName, const int userid)
98 {
99     if (ModuleName.empty() || userid < 0) {
100         APP_LOGE("params are invalid");
101         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
102     }
103 
104     return CallService(&IInstalld::RemoveModuleDataDir, ModuleName, userid);
105 }
106 
RemoveDir(const std::string & dir)107 ErrCode InstalldClient::RemoveDir(const std::string &dir)
108 {
109     if (dir.empty()) {
110         APP_LOGE("dir removed is empty");
111         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
112     }
113 
114     return CallService(&IInstalld::RemoveDir, dir);
115 }
116 
CleanBundleDataDir(const std::string & bundleDir)117 ErrCode InstalldClient::CleanBundleDataDir(const std::string &bundleDir)
118 {
119     if (bundleDir.empty()) {
120         APP_LOGE("bundle dir is empty");
121         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
122     }
123 
124     return CallService(&IInstalld::CleanBundleDataDir, bundleDir);
125 }
126 
CleanBundleDataDirByName(const std::string & bundleName,const int userid)127 ErrCode InstalldClient::CleanBundleDataDirByName(
128     const std::string &bundleName, const int userid)
129 {
130     if (bundleName.empty() || userid < 0) {
131         APP_LOGE("params are invalid");
132         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
133     }
134 
135     return CallService(&IInstalld::CleanBundleDataDirByName, bundleName, userid);
136 }
137 
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats,const int32_t uid)138 ErrCode InstalldClient::GetBundleStats(
139     const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats, const int32_t uid)
140 {
141     if (bundleName.empty()) {
142         APP_LOGE("bundleName is empty");
143         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
144     }
145 
146     return CallService(&IInstalld::GetBundleStats, bundleName, userId, bundleStats, uid);
147 }
148 
GetAllBundleStats(const std::vector<std::string> & bundleNames,const int32_t userId,std::vector<int64_t> & bundleStats,const std::vector<int32_t> & uids)149 ErrCode InstalldClient::GetAllBundleStats(const std::vector<std::string> &bundleNames, const int32_t userId,
150     std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids)
151 {
152     if (bundleNames.empty()) {
153         APP_LOGE("bundleName is empty");
154         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
155     }
156 
157     if (bundleNames.size() != uids.size()) {
158         APP_LOGE("bundleNames size is not equal to uids size");
159         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
160     }
161     return CallService(&IInstalld::GetAllBundleStats, bundleNames, userId, bundleStats, uids);
162 }
163 
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl,bool isPreInstallApp,bool debug)164 ErrCode InstalldClient::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl,
165     bool isPreInstallApp, bool debug)
166 {
167     if (dir.empty() || bundleName.empty() || apl.empty()) {
168         APP_LOGE("params are invalid");
169         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
170     }
171 
172     return CallService(&IInstalld::SetDirApl, dir, bundleName, apl, isPreInstallApp, debug);
173 }
174 
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)175 ErrCode InstalldClient::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
176 {
177     if (dir.empty()) {
178         APP_LOGE("params are invalid");
179         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
180     }
181 
182     return CallService(&IInstalld::GetBundleCachePath, dir, cachePath);
183 }
184 
ResetInstalldProxy()185 void InstalldClient::ResetInstalldProxy()
186 {
187     if ((installdProxy_ != nullptr) && (installdProxy_->AsObject() != nullptr)) {
188         installdProxy_->AsObject()->RemoveDeathRecipient(recipient_);
189     }
190     installdProxy_ = nullptr;
191 }
192 
GetInstalldProxy()193 bool InstalldClient::GetInstalldProxy()
194 {
195     if (installdProxy_ == nullptr) {
196         APP_LOGD("try to get installd proxy");
197         std::lock_guard<std::mutex> lock(mutex_);
198         if (installdProxy_ == nullptr) {
199             sptr<IInstalld> tempProxy =
200                 iface_cast<IInstalld>(SystemAbilityHelper::GetSystemAbility(INSTALLD_SERVICE_ID));
201             if ((tempProxy == nullptr) || (tempProxy->AsObject() == nullptr)) {
202                 APP_LOGE("the installd proxy or remote object is null");
203                 return false;
204             }
205             recipient_ = new (std::nothrow) InstalldDeathRecipient();
206             if (recipient_ == nullptr) {
207                 APP_LOGE("the death recipient is nullptr");
208                 return false;
209             }
210             tempProxy->AsObject()->AddDeathRecipient(recipient_);
211             installdProxy_ = tempProxy;
212         }
213     }
214     return true;
215 }
216 
ScanDir(const std::string & dir,ScanMode scanMode,ResultMode resultMode,std::vector<std::string> & paths)217 ErrCode InstalldClient::ScanDir(
218     const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
219 {
220     if (dir.empty()) {
221         APP_LOGE("params are invalid");
222         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
223     }
224 
225     return CallService(&IInstalld::ScanDir, dir, scanMode, resultMode, paths);
226 }
227 
MoveFile(const std::string & oldPath,const std::string & newPath)228 ErrCode InstalldClient::MoveFile(const std::string &oldPath, const std::string &newPath)
229 {
230     if (oldPath.empty() || newPath.empty()) {
231         APP_LOGE("params are invalid");
232         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
233     }
234 
235     return CallService(&IInstalld::MoveFile, oldPath, newPath);
236 }
237 
CopyFile(const std::string & oldPath,const std::string & newPath,const std::string & signatureFilePath)238 ErrCode InstalldClient::CopyFile(const std::string &oldPath, const std::string &newPath,
239     const std::string &signatureFilePath)
240 {
241     if (oldPath.empty() || newPath.empty()) {
242         APP_LOGE("params are invalid");
243         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
244     }
245 
246     return CallService(&IInstalld::CopyFile, oldPath, newPath, signatureFilePath);
247 }
248 
Mkdir(const std::string & dir,const int32_t mode,const int32_t uid,const int32_t gid)249 ErrCode InstalldClient::Mkdir(
250     const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid)
251 {
252     if (dir.empty()) {
253         APP_LOGE("params are invalid");
254         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
255     }
256 
257     return CallService(&IInstalld::Mkdir, dir, mode, uid, gid);
258 }
259 
GetFileStat(const std::string & file,FileStat & fileStat)260 ErrCode InstalldClient::GetFileStat(const std::string &file, FileStat &fileStat)
261 {
262     if (file.empty()) {
263         APP_LOGE("params are invalid");
264         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
265     }
266 
267     return CallService(&IInstalld::GetFileStat, file, fileStat);
268 }
269 
ExtractDiffFiles(const std::string & filePath,const std::string & targetPath,const std::string & cpuAbi)270 ErrCode InstalldClient::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
271     const std::string &cpuAbi)
272 {
273     if (filePath.empty() || targetPath.empty() || cpuAbi.empty()) {
274         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
275     }
276     return CallService(&IInstalld::ExtractDiffFiles, filePath, targetPath, cpuAbi);
277 }
278 
ApplyDiffPatch(const std::string & oldSoPath,const std::string & diffFilePath,const std::string & newSoPath,int32_t uid)279 ErrCode InstalldClient::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
280     const std::string &newSoPath, int32_t uid)
281 {
282     if (oldSoPath.empty() || diffFilePath.empty() || newSoPath.empty()) {
283         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
284     }
285     return CallService(&IInstalld::ApplyDiffPatch, oldSoPath, diffFilePath, newSoPath, uid);
286 }
287 
IsExistDir(const std::string & dir,bool & isExist)288 ErrCode InstalldClient::IsExistDir(const std::string &dir, bool &isExist)
289 {
290     return CallService(&IInstalld::IsExistDir, dir, isExist);
291 }
292 
IsExistFile(const std::string & path,bool & isExist)293 ErrCode InstalldClient::IsExistFile(const std::string &path, bool &isExist)
294 {
295     return CallService(&IInstalld::IsExistFile, path, isExist);
296 }
297 
IsExistApFile(const std::string & path,bool & isExist)298 ErrCode InstalldClient::IsExistApFile(const std::string &path, bool &isExist)
299 {
300     return CallService(&IInstalld::IsExistApFile, path, isExist);
301 }
302 
IsDirEmpty(const std::string & dir,bool & isDirEmpty)303 ErrCode InstalldClient::IsDirEmpty(const std::string &dir, bool &isDirEmpty)
304 {
305     return CallService(&IInstalld::IsDirEmpty, dir, isDirEmpty);
306 }
307 
ObtainQuickFixFileDir(const std::string & dir,std::vector<std::string> & dirVec)308 ErrCode InstalldClient::ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec)
309 {
310     return CallService(&IInstalld::ObtainQuickFixFileDir, dir, dirVec);
311 }
312 
CopyFiles(const std::string & sourceDir,const std::string & destinationDir)313 ErrCode InstalldClient::CopyFiles(const std::string &sourceDir, const std::string &destinationDir)
314 {
315     return CallService(&IInstalld::CopyFiles, sourceDir, destinationDir);
316 }
317 
GetNativeLibraryFileNames(const std::string & filePath,const std::string & cpuAbi,std::vector<std::string> & fileNames)318 ErrCode InstalldClient::GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi,
319     std::vector<std::string> &fileNames)
320 {
321     return CallService(&IInstalld::GetNativeLibraryFileNames, filePath, cpuAbi, fileNames);
322 }
323 
VerifyCodeSignature(const CodeSignatureParam & codeSignatureParam)324 ErrCode InstalldClient::VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam)
325 {
326     if (codeSignatureParam.modulePath.empty()) {
327         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
328     }
329     return CallService(&IInstalld::VerifyCodeSignature, codeSignatureParam);
330 }
331 
CheckEncryption(const CheckEncryptionParam & checkEncryptionParam,bool & isEncryption)332 ErrCode InstalldClient::CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption)
333 {
334     if (checkEncryptionParam.modulePath.empty()) {
335         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
336     }
337     return CallService(&IInstalld::CheckEncryption, checkEncryptionParam, isEncryption);
338 }
339 
MoveFiles(const std::string & srcDir,const std::string & desDir)340 ErrCode InstalldClient::MoveFiles(const std::string &srcDir, const std::string &desDir)
341 {
342     if (srcDir.empty() || desDir.empty()) {
343         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
344     }
345     return CallService(&IInstalld::MoveFiles, srcDir, desDir);
346 }
347 
ExtractDriverSoFiles(const std::string & srcPath,const std::unordered_multimap<std::string,std::string> & dirMap)348 ErrCode InstalldClient::ExtractDriverSoFiles(const std::string &srcPath,
349     const std::unordered_multimap<std::string, std::string> &dirMap)
350 {
351     if (srcPath.empty() || dirMap.empty()) {
352         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
353     }
354     return CallService(&IInstalld::ExtractDriverSoFiles, srcPath, dirMap);
355 }
356 
ExtractEncryptedSoFiles(const std::string & hapPath,const std::string & realSoFilesPath,const std::string & cpuAbi,const std::string & tmpSoPath,int32_t uid)357 ErrCode InstalldClient::ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath,
358     const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid)
359 {
360     if (hapPath.empty() || tmpSoPath.empty() || cpuAbi.empty()) {
361         APP_LOGE("params are invalid");
362         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
363     }
364     return CallService(&IInstalld::ExtractEncryptedSoFiles, hapPath, realSoFilesPath, cpuAbi, tmpSoPath, uid);
365 }
366 
VerifyCodeSignatureForHap(const CodeSignatureParam & codeSignatureParam)367 ErrCode InstalldClient::VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam)
368 {
369     if (codeSignatureParam.modulePath.empty()) {
370         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
371     }
372     return CallService(&IInstalld::VerifyCodeSignatureForHap, codeSignatureParam);
373 }
374 
DeliverySignProfile(const std::string & bundleName,int32_t profileBlockLength,const unsigned char * profileBlock)375 ErrCode InstalldClient::DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength,
376     const unsigned char *profileBlock)
377 {
378     if (bundleName.empty() || profileBlock == nullptr) {
379         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
380     }
381     return CallService(&IInstalld::DeliverySignProfile, bundleName, profileBlockLength, profileBlock);
382 }
383 
RemoveSignProfile(const std::string & bundleName)384 ErrCode InstalldClient::RemoveSignProfile(const std::string &bundleName)
385 {
386     if (bundleName.empty()) {
387         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
388     }
389     return CallService(&IInstalld::RemoveSignProfile, bundleName);
390 }
391 
StartInstalldService()392 bool InstalldClient::StartInstalldService()
393 {
394     return GetInstalldProxy();
395 }
396 }  // namespace AppExecFwk
397 }  // namespace OHOS
398