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 "ipc/installd_proxy.h"
17
18 #include "ipc_types.h"
19
20 #include "app_log_wrapper.h"
21 #include "bundle_constants.h"
22 #include "parcel_macro.h"
23 #include "string_ex.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 constexpr int32_t WAIT_TIME = 3000;
29 }
30
InstalldProxy(const sptr<IRemoteObject> & object)31 InstalldProxy::InstalldProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IInstalld>(object)
32 {
33 APP_LOGI("installd proxy instance is created");
34 }
35
~InstalldProxy()36 InstalldProxy::~InstalldProxy()
37 {
38 APP_LOGI("installd proxy instance is destroyed");
39 }
40
CreateBundleDir(const std::string & bundleDir)41 ErrCode InstalldProxy::CreateBundleDir(const std::string &bundleDir)
42 {
43 MessageParcel data;
44 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
45 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleDir));
46
47 MessageParcel reply;
48 MessageOption option;
49 return TransactInstalldCmd(IInstalld::Message::CREATE_BUNDLE_DIR, data, reply, option);
50 }
51
ExtractModuleFiles(const std::string & srcModulePath,const std::string & targetPath,const std::string & targetSoPath,const std::string & cpuAbi)52 ErrCode InstalldProxy::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath,
53 const std::string &targetSoPath, const std::string &cpuAbi)
54 {
55 MessageParcel data;
56 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
57 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(srcModulePath));
58 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(targetPath));
59 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(targetSoPath));
60 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(cpuAbi));
61
62 MessageParcel reply;
63 MessageOption option;
64 return TransactInstalldCmd(IInstalld::Message::EXTRACT_MODULE_FILES, data, reply, option);
65 }
66
ExtractFiles(const ExtractParam & extractParam)67 ErrCode InstalldProxy::ExtractFiles(const ExtractParam &extractParam)
68 {
69 MessageParcel data;
70 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
71 if (!data.WriteParcelable(&extractParam)) {
72 APP_LOGE("WriteParcelable extractParam failed.");
73 return ERR_APPEXECFWK_PARCEL_ERROR;
74 }
75
76 MessageParcel reply;
77 MessageOption option;
78 return TransactInstalldCmd(IInstalld::Message::EXTRACT_FILES, data, reply, option);
79 }
80
RenameModuleDir(const std::string & oldPath,const std::string & newPath)81 ErrCode InstalldProxy::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
82 {
83 MessageParcel data;
84 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
85 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(oldPath));
86 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(newPath));
87
88 MessageParcel reply;
89 MessageOption option;
90 return TransactInstalldCmd(IInstalld::Message::RENAME_MODULE_DIR, data, reply, option);
91 }
92
CreateBundleDataDir(const std::string & bundleName,const int userid,const int uid,const int gid,const std::string & apl)93 ErrCode InstalldProxy::CreateBundleDataDir(const std::string &bundleName,
94 const int userid, const int uid, const int gid, const std::string &apl)
95 {
96 MessageParcel data;
97 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
98 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleName));
99 INSTALLD_PARCEL_WRITE(data, Int32, userid);
100 INSTALLD_PARCEL_WRITE(data, Int32, uid);
101 INSTALLD_PARCEL_WRITE(data, Int32, gid);
102 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(apl));
103
104 MessageParcel reply;
105 MessageOption option;
106 return TransactInstalldCmd(IInstalld::Message::CREATE_BUNDLE_DATA_DIR, data, reply, option);
107 }
108
RemoveBundleDataDir(const std::string & bundleName,const int userid)109 ErrCode InstalldProxy::RemoveBundleDataDir(const std::string &bundleName, const int userid)
110 {
111 MessageParcel data;
112 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
113 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleName));
114 INSTALLD_PARCEL_WRITE(data, Int32, userid);
115
116 MessageParcel reply;
117 MessageOption option;
118 return TransactInstalldCmd(IInstalld::Message::REMOVE_BUNDLE_DATA_DIR, data, reply, option);
119 }
120
RemoveModuleDataDir(const std::string & ModuleName,const int userid)121 ErrCode InstalldProxy::RemoveModuleDataDir(const std::string &ModuleName, const int userid)
122 {
123 MessageParcel data;
124 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
125 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(ModuleName));
126 INSTALLD_PARCEL_WRITE(data, Int32, userid);
127
128 MessageParcel reply;
129 MessageOption option;
130 return TransactInstalldCmd(IInstalld::Message::REMOVE_MODULE_DATA_DIR, data, reply, option);
131 }
132
RemoveDir(const std::string & dir)133 ErrCode InstalldProxy::RemoveDir(const std::string &dir)
134 {
135 MessageParcel data;
136 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
137 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
138
139 MessageParcel reply;
140 MessageOption option(MessageOption::TF_SYNC);
141 return TransactInstalldCmd(IInstalld::Message::REMOVE_DIR, data, reply, option);
142 }
143
CleanBundleDataDir(const std::string & bundleDir)144 ErrCode InstalldProxy::CleanBundleDataDir(const std::string &bundleDir)
145 {
146 MessageParcel data;
147 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
148 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleDir));
149
150 MessageParcel reply;
151 MessageOption option(MessageOption::TF_SYNC, WAIT_TIME);
152 return TransactInstalldCmd(IInstalld::Message::CLEAN_BUNDLE_DATA_DIR, data, reply, option);
153 }
154
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats)155 ErrCode InstalldProxy::GetBundleStats(
156 const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats)
157 {
158 MessageParcel data;
159 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
160 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleName));
161 INSTALLD_PARCEL_WRITE(data, Int32, userId);
162 MessageParcel reply;
163 MessageOption option(MessageOption::TF_SYNC);
164 auto ret = TransactInstalldCmd(IInstalld::Message::GET_BUNDLE_STATS, data, reply, option);
165 if (ret == ERR_OK) {
166 if (reply.ReadInt64Vector(&bundleStats)) {
167 return ERR_OK;
168 } else {
169 return ERR_APPEXECFWK_PARCEL_ERROR;
170 }
171 }
172 return ret;
173 }
174
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl)175 ErrCode InstalldProxy::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl)
176 {
177 MessageParcel data;
178 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
179 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
180 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleName));
181 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(apl));
182
183 MessageParcel reply;
184 MessageOption option(MessageOption::TF_SYNC);
185 return TransactInstalldCmd(IInstalld::Message::SET_DIR_APL, data, reply, option);
186 }
187
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)188 ErrCode InstalldProxy::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
189 {
190 MessageParcel data;
191 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
192 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
193 MessageParcel reply;
194 MessageOption option(MessageOption::TF_SYNC);
195 auto ret = TransactInstalldCmd(IInstalld::Message::GET_BUNDLE_CACHE_PATH, data, reply, option);
196 if (ret == ERR_OK) {
197 if (reply.ReadStringVector(&cachePath)) {
198 return ERR_OK;
199 } else {
200 return ERR_APPEXECFWK_PARCEL_ERROR;
201 }
202 }
203 return ret;
204 }
205
ScanDir(const std::string & dir,ScanMode scanMode,ResultMode resultMode,std::vector<std::string> & paths)206 ErrCode InstalldProxy::ScanDir(
207 const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
208 {
209 MessageParcel data;
210 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
211 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
212 INSTALLD_PARCEL_WRITE(data, Int32, static_cast<int32_t>(scanMode));
213 INSTALLD_PARCEL_WRITE(data, Int32, static_cast<int32_t>(resultMode));
214
215 MessageParcel reply;
216 MessageOption option(MessageOption::TF_SYNC);
217 auto ret = TransactInstalldCmd(IInstalld::Message::SCAN_DIR, data, reply, option);
218 if (ret != ERR_OK) {
219 return ret;
220 }
221
222 if (!reply.ReadStringVector(&paths)) {
223 return ERR_APPEXECFWK_PARCEL_ERROR;
224 }
225
226 return ERR_OK;
227 }
228
MoveFile(const std::string & oldPath,const std::string & newPath)229 ErrCode InstalldProxy::MoveFile(const std::string &oldPath, const std::string &newPath)
230 {
231 MessageParcel data;
232 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
233 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(oldPath));
234 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(newPath));
235
236 MessageParcel reply;
237 MessageOption option(MessageOption::TF_SYNC);
238 return TransactInstalldCmd(IInstalld::Message::MOVE_FILE, data, reply, option);
239 }
240
CopyFile(const std::string & oldPath,const std::string & newPath)241 ErrCode InstalldProxy::CopyFile(const std::string &oldPath, const std::string &newPath)
242 {
243 MessageParcel data;
244 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
245 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(oldPath));
246 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(newPath));
247
248 MessageParcel reply;
249 MessageOption option(MessageOption::TF_SYNC);
250 return TransactInstalldCmd(IInstalld::Message::COPY_FILE, data, reply, option);
251 }
252
Mkdir(const std::string & dir,const int32_t mode,const int32_t uid,const int32_t gid)253 ErrCode InstalldProxy::Mkdir(
254 const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid)
255 {
256 MessageParcel data;
257 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
258 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
259 INSTALLD_PARCEL_WRITE(data, Int32, mode);
260 INSTALLD_PARCEL_WRITE(data, Int32, uid);
261 INSTALLD_PARCEL_WRITE(data, Int32, gid);
262
263 MessageParcel reply;
264 MessageOption option(MessageOption::TF_SYNC);
265 return TransactInstalldCmd(IInstalld::Message::MKDIR, data, reply, option);
266 }
267
GetFileStat(const std::string & file,FileStat & fileStat)268 ErrCode InstalldProxy::GetFileStat(const std::string &file, FileStat &fileStat)
269 {
270 MessageParcel data;
271 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
272 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(file));
273
274 MessageParcel reply;
275 MessageOption option(MessageOption::TF_SYNC);
276 auto ret = TransactInstalldCmd(IInstalld::Message::GET_FILE_STAT, data, reply, option);
277 if (ret != ERR_OK) {
278 APP_LOGE("TransactInstalldCmd failed");
279 return ret;
280 }
281
282 std::unique_ptr<FileStat> info(reply.ReadParcelable<FileStat>());
283 if (info == nullptr) {
284 APP_LOGE("readParcelableInfo failed");
285 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
286 }
287
288 fileStat = *info;
289 return ERR_OK;
290 }
291
ExtractDiffFiles(const std::string & filePath,const std::string & targetPath,const std::string & cpuAbi)292 ErrCode InstalldProxy::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
293 const std::string &cpuAbi)
294 {
295 MessageParcel data;
296 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
297 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(filePath));
298 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(targetPath));
299 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(cpuAbi));
300
301 MessageParcel reply;
302 MessageOption option(MessageOption::TF_SYNC);
303 return TransactInstalldCmd(IInstalld::Message::EXTRACT_DIFF_FILES, data, reply, option);
304 }
305
ApplyDiffPatch(const std::string & oldSoPath,const std::string & diffFilePath,const std::string & newSoPath)306 ErrCode InstalldProxy::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
307 const std::string &newSoPath)
308 {
309 MessageParcel data;
310 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
311 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(oldSoPath));
312 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(diffFilePath));
313 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(newSoPath));
314
315 MessageParcel reply;
316 MessageOption option(MessageOption::TF_SYNC);
317 return TransactInstalldCmd(IInstalld::Message::APPLY_DIFF_PATCH, data, reply, option);
318 }
319
IsExistDir(const std::string & dir,bool & isExist)320 ErrCode InstalldProxy::IsExistDir(const std::string &dir, bool &isExist)
321 {
322 MessageParcel data;
323 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
324 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
325
326 MessageParcel reply;
327 MessageOption option(MessageOption::TF_SYNC);
328 auto ret = TransactInstalldCmd(IInstalld::Message::IS_EXIST_DIR, data, reply, option);
329 if (ret != ERR_OK) {
330 APP_LOGE("TransactInstalldCmd failed");
331 return ret;
332 }
333 isExist = reply.ReadBool();
334 return ERR_OK;
335 }
336
IsDirEmpty(const std::string & dir,bool & isDirEmpty)337 ErrCode InstalldProxy::IsDirEmpty(const std::string &dir, bool &isDirEmpty)
338 {
339 MessageParcel data;
340 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
341 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
342
343 MessageParcel reply;
344 MessageOption option(MessageOption::TF_SYNC);
345 auto ret = TransactInstalldCmd(IInstalld::Message::IS_DIR_EMPTY, data, reply, option);
346 if (ret != ERR_OK) {
347 APP_LOGE("TransactInstalldCmd failed");
348 return ret;
349 }
350 isDirEmpty = reply.ReadBool();
351 return ERR_OK;
352 }
353
ObtainQuickFixFileDir(const std::string & dir,std::vector<std::string> & dirVec)354 ErrCode InstalldProxy::ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec)
355 {
356 MessageParcel data;
357 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
358 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
359
360 MessageParcel reply;
361 MessageOption option(MessageOption::TF_SYNC);
362 auto ret = TransactInstalldCmd(IInstalld::Message::OBTAIN_QUICK_FIX_DIR, data, reply, option);
363 if (ret != ERR_OK) {
364 APP_LOGE("TransactInstalldCmd failed");
365 return ret;
366 }
367 if (!reply.ReadStringVector(&dirVec)) {
368 return ERR_APPEXECFWK_PARCEL_ERROR;
369 }
370 return ERR_OK;
371 }
372
CopyFiles(const std::string & sourceDir,const std::string & destinationDir)373 ErrCode InstalldProxy::CopyFiles(const std::string &sourceDir, const std::string &destinationDir)
374 {
375 MessageParcel data;
376 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
377 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(sourceDir));
378 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(destinationDir));
379
380 MessageParcel reply;
381 MessageOption option(MessageOption::TF_SYNC);
382 auto ret = TransactInstalldCmd(IInstalld::Message::COPY_FILES, data, reply, option);
383 if (ret != ERR_OK) {
384 APP_LOGE("TransactInstalldCmd failed");
385 return ret;
386 }
387 return ERR_OK;
388 }
389
TransactInstalldCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)390 ErrCode InstalldProxy::TransactInstalldCmd(uint32_t code, MessageParcel &data, MessageParcel &reply,
391 MessageOption &option)
392 {
393 sptr<IRemoteObject> remote = Remote();
394 if (remote == nullptr) {
395 APP_LOGE("fail to send %{public}u cmd to service due to remote object is null", code);
396 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
397 }
398
399 if (remote->SendRequest(code, data, reply, option) != OHOS::NO_ERROR) {
400 APP_LOGE("fail to send %{public}u request to service due to transact error", code);
401 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
402 }
403 return reply.ReadInt32();
404 }
405 } // namespace AppExecFwk
406 } // namespace OHOS