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
RemoveBundleDataDir(const std::string & bundleName,const int userid)81 ErrCode InstalldClient::RemoveBundleDataDir(
82 const std::string &bundleName, const int userid)
83 {
84 if (bundleName.empty() || userid < 0) {
85 APP_LOGE("params are invalid");
86 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
87 }
88
89 return CallService(&IInstalld::RemoveBundleDataDir, bundleName, userid);
90 }
91
RemoveModuleDataDir(const std::string & ModuleName,const int userid)92 ErrCode InstalldClient::RemoveModuleDataDir(const std::string &ModuleName, const int userid)
93 {
94 if (ModuleName.empty() || userid < 0) {
95 APP_LOGE("params are invalid");
96 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
97 }
98
99 return CallService(&IInstalld::RemoveModuleDataDir, ModuleName, userid);
100 }
101
RemoveDir(const std::string & dir)102 ErrCode InstalldClient::RemoveDir(const std::string &dir)
103 {
104 if (dir.empty()) {
105 APP_LOGE("dir removed is empty");
106 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
107 }
108
109 return CallService(&IInstalld::RemoveDir, dir);
110 }
111
CleanBundleDataDir(const std::string & bundleDir)112 ErrCode InstalldClient::CleanBundleDataDir(const std::string &bundleDir)
113 {
114 if (bundleDir.empty()) {
115 APP_LOGE("bundle dir is empty");
116 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
117 }
118
119 return CallService(&IInstalld::CleanBundleDataDir, bundleDir);
120 }
121
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats)122 ErrCode InstalldClient::GetBundleStats(
123 const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats)
124 {
125 if (bundleName.empty()) {
126 APP_LOGE("bundleName is empty");
127 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
128 }
129
130 return CallService(&IInstalld::GetBundleStats, bundleName, userId, bundleStats);
131 }
132
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl,bool isPreInstallApp,bool debug)133 ErrCode InstalldClient::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl,
134 bool isPreInstallApp, bool debug)
135 {
136 if (dir.empty() || bundleName.empty() || apl.empty()) {
137 APP_LOGE("params are invalid");
138 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
139 }
140
141 return CallService(&IInstalld::SetDirApl, dir, bundleName, apl, isPreInstallApp, debug);
142 }
143
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)144 ErrCode InstalldClient::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
145 {
146 if (dir.empty()) {
147 APP_LOGE("params are invalid");
148 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
149 }
150
151 return CallService(&IInstalld::GetBundleCachePath, dir, cachePath);
152 }
153
ResetInstalldProxy()154 void InstalldClient::ResetInstalldProxy()
155 {
156 if ((installdProxy_ != nullptr) && (installdProxy_->AsObject() != nullptr)) {
157 installdProxy_->AsObject()->RemoveDeathRecipient(recipient_);
158 }
159 installdProxy_ = nullptr;
160 }
161
GetInstalldProxy()162 bool InstalldClient::GetInstalldProxy()
163 {
164 if (installdProxy_ == nullptr) {
165 APP_LOGD("try to get installd proxy");
166 std::lock_guard<std::mutex> lock(mutex_);
167 if (installdProxy_ == nullptr) {
168 sptr<IInstalld> tempProxy =
169 iface_cast<IInstalld>(SystemAbilityHelper::GetSystemAbility(INSTALLD_SERVICE_ID));
170 if ((tempProxy == nullptr) || (tempProxy->AsObject() == nullptr)) {
171 APP_LOGE("the installd proxy or remote object is null");
172 return false;
173 }
174 recipient_ = new (std::nothrow) InstalldDeathRecipient();
175 if (recipient_ == nullptr) {
176 APP_LOGE("the death recipient is nullptr");
177 return false;
178 }
179 tempProxy->AsObject()->AddDeathRecipient(recipient_);
180 installdProxy_ = tempProxy;
181 }
182 }
183 return true;
184 }
185
ScanDir(const std::string & dir,ScanMode scanMode,ResultMode resultMode,std::vector<std::string> & paths)186 ErrCode InstalldClient::ScanDir(
187 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
188 {
189 if (dir.empty()) {
190 APP_LOGE("params are invalid");
191 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
192 }
193
194 return CallService(&IInstalld::ScanDir, dir, scanMode, resultMode, paths);
195 }
196
MoveFile(const std::string & oldPath,const std::string & newPath)197 ErrCode InstalldClient::MoveFile(const std::string &oldPath, const std::string &newPath)
198 {
199 if (oldPath.empty() || newPath.empty()) {
200 APP_LOGE("params are invalid");
201 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
202 }
203
204 return CallService(&IInstalld::MoveFile, oldPath, newPath);
205 }
206
CopyFile(const std::string & oldPath,const std::string & newPath,const std::string & signatureFilePath)207 ErrCode InstalldClient::CopyFile(const std::string &oldPath, const std::string &newPath,
208 const std::string &signatureFilePath)
209 {
210 if (oldPath.empty() || newPath.empty()) {
211 APP_LOGE("params are invalid");
212 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
213 }
214
215 return CallService(&IInstalld::CopyFile, oldPath, newPath, signatureFilePath);
216 }
217
Mkdir(const std::string & dir,const int32_t mode,const int32_t uid,const int32_t gid)218 ErrCode InstalldClient::Mkdir(
219 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid)
220 {
221 if (dir.empty()) {
222 APP_LOGE("params are invalid");
223 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
224 }
225
226 return CallService(&IInstalld::Mkdir, dir, mode, uid, gid);
227 }
228
GetFileStat(const std::string & file,FileStat & fileStat)229 ErrCode InstalldClient::GetFileStat(const std::string &file, FileStat &fileStat)
230 {
231 if (file.empty()) {
232 APP_LOGE("params are invalid");
233 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
234 }
235
236 return CallService(&IInstalld::GetFileStat, file, fileStat);
237 }
238
ExtractDiffFiles(const std::string & filePath,const std::string & targetPath,const std::string & cpuAbi)239 ErrCode InstalldClient::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
240 const std::string &cpuAbi)
241 {
242 if (filePath.empty() || targetPath.empty() || cpuAbi.empty()) {
243 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
244 }
245 return CallService(&IInstalld::ExtractDiffFiles, filePath, targetPath, cpuAbi);
246 }
247
ApplyDiffPatch(const std::string & oldSoPath,const std::string & diffFilePath,const std::string & newSoPath)248 ErrCode InstalldClient::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
249 const std::string &newSoPath)
250 {
251 if (oldSoPath.empty() || diffFilePath.empty() || newSoPath.empty()) {
252 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
253 }
254 return CallService(&IInstalld::ApplyDiffPatch, oldSoPath, diffFilePath, newSoPath);
255 }
256
IsExistDir(const std::string & dir,bool & isExist)257 ErrCode InstalldClient::IsExistDir(const std::string &dir, bool &isExist)
258 {
259 return CallService(&IInstalld::IsExistDir, dir, isExist);
260 }
261
IsExistFile(const std::string & path,bool & isExist)262 ErrCode InstalldClient::IsExistFile(const std::string &path, bool &isExist)
263 {
264 return CallService(&IInstalld::IsExistFile, path, isExist);
265 }
266
IsDirEmpty(const std::string & dir,bool & isDirEmpty)267 ErrCode InstalldClient::IsDirEmpty(const std::string &dir, bool &isDirEmpty)
268 {
269 return CallService(&IInstalld::IsDirEmpty, dir, isDirEmpty);
270 }
271
ObtainQuickFixFileDir(const std::string & dir,std::vector<std::string> & dirVec)272 ErrCode InstalldClient::ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec)
273 {
274 return CallService(&IInstalld::ObtainQuickFixFileDir, dir, dirVec);
275 }
276
CopyFiles(const std::string & sourceDir,const std::string & destinationDir)277 ErrCode InstalldClient::CopyFiles(const std::string &sourceDir, const std::string &destinationDir)
278 {
279 return CallService(&IInstalld::CopyFiles, sourceDir, destinationDir);
280 }
281
GetNativeLibraryFileNames(const std::string & filePath,const std::string & cpuAbi,std::vector<std::string> & fileNames)282 ErrCode InstalldClient::GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi,
283 std::vector<std::string> &fileNames)
284 {
285 return CallService(&IInstalld::GetNativeLibraryFileNames, filePath, cpuAbi, fileNames);
286 }
287
VerifyCodeSignature(const std::string & modulePath,const std::string & cpuAbi,const std::string & targetSoPath,const std::string & signatureFileDir)288 ErrCode InstalldClient::VerifyCodeSignature(const std::string &modulePath, const std::string &cpuAbi,
289 const std::string &targetSoPath, const std::string &signatureFileDir)
290 {
291 if (modulePath.empty()) {
292 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
293 }
294 return CallService(&IInstalld::VerifyCodeSignature, modulePath, cpuAbi, targetSoPath,
295 signatureFileDir);
296 }
297
MoveFiles(const std::string & srcDir,const std::string & desDir)298 ErrCode InstalldClient::MoveFiles(const std::string &srcDir, const std::string &desDir)
299 {
300 if (srcDir.empty() || desDir.empty()) {
301 return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
302 }
303 return CallService(&IInstalld::MoveFiles, srcDir, desDir);
304 }
305
StartInstalldService()306 bool InstalldClient::StartInstalldService()
307 {
308 return GetInstalldProxy();
309 }
310 } // namespace AppExecFwk
311 } // namespace OHOS
312