1 /*
2 * Copyright (c) 2023-2025 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 "distributed_file_daemon_proxy.h"
17
18 #include <sstream>
19
20 #include "copy/ipc_wrapper.h"
21 #include "dfs_error.h"
22 #include "ipc/distributed_file_daemon_ipc_interface_code.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 #include "utils_log.h"
26
27 #undef LOG_DOMAIN
28 #undef LOG_TAG
29 #define LOG_DOMAIN 0xD004315
30 #define LOG_TAG "distributedfile_daemon"
31
32 namespace OHOS {
33 namespace Storage {
34 namespace DistributedFile {
35 using namespace std;
36 using namespace OHOS::Storage;
37 constexpr int32_t INDEX1 = 1;
38 constexpr int32_t INDEX2 = 2;
39 constexpr int32_t STEP = 3;
40
ReadUriInfoFromResult(const std::vector<std::string> & result,std::unordered_map<std::string,AppFileService::ModuleRemoteFileShare::HmdfsUriInfo> & uriToDfsUriMaps)41 static int32_t ReadUriInfoFromResult(const std::vector<std::string> &result,
42 std::unordered_map<std::string, AppFileService::ModuleRemoteFileShare::HmdfsUriInfo> &uriToDfsUriMaps)
43 {
44 for (size_t i = 0; (i < result.size()) && (i + INDEX2 < result.size()); i += STEP) {
45 AppFileService::ModuleRemoteFileShare::HmdfsUriInfo info;
46 info.uriStr = result[i + INDEX1];
47 std::istringstream iss(result[i + INDEX2]);
48 iss >> info.fileSize;
49 if (iss.fail()) {
50 LOGE("read fileSize failed: %{public}s", result[i + INDEX2].c_str());
51 return OHOS::FileManagement::E_INVAL_ARG;
52 }
53 uriToDfsUriMaps[result[i]] = info;
54 }
55 LOGI("ReadUriInfoFromResult success, uriToDfsUriMaps size:%{public}d", static_cast<int>(uriToDfsUriMaps.size()));
56 return OHOS::FileManagement::E_OK;
57 }
58
OnRemoteSaDied(const wptr<IRemoteObject> & remote)59 void DistributedFileDaemonProxy::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
60 {
61 LOGI("dfs service death");
62 unique_lock<mutex> lock(proxyMutex_);
63 daemonProxy_ = nullptr;
64 }
65
SetDeathRecipient(RemoteDiedHandler handler)66 void DistributedFileDaemonProxy::DaemonDeathRecipient::SetDeathRecipient(RemoteDiedHandler handler)
67 {
68 handler_ = std::move(handler);
69 }
70
OnRemoteDied(const wptr<IRemoteObject> & remote)71 void DistributedFileDaemonProxy::DaemonDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
72 {
73 LOGI("start");
74 if (handler_ != nullptr) {
75 handler_(remote);
76 }
77 }
78
GetInstance()79 sptr<IDaemon> DistributedFileDaemonProxy::GetInstance()
80 {
81 LOGI("getinstance");
82 unique_lock<mutex> lock(proxyMutex_);
83 if (daemonProxy_ != nullptr) {
84 return daemonProxy_;
85 }
86
87 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
88 if (samgr == nullptr) {
89 LOGE("Samgr is nullptr");
90 return nullptr;
91 }
92
93 auto object = samgr->CheckSystemAbility(FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID);
94 if (object == nullptr) {
95 LOGE("object == nullptr");
96 return nullptr;
97 }
98
99 if (deathRecipient_ == nullptr) {
100 deathRecipient_ = new (std::nothrow) DaemonDeathRecipient();
101 if (deathRecipient_ == nullptr) {
102 LOGE("new death recipient failed");
103 return nullptr;
104 }
105 }
106 deathRecipient_->SetDeathRecipient([](const wptr<IRemoteObject> &remote) { OnRemoteSaDied(remote); });
107 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
108 LOGE("failed to add death recipient.");
109 return nullptr;
110 }
111
112 daemonProxy_ = iface_cast<IDaemon>(object);
113 if (daemonProxy_ == nullptr) {
114 LOGE("service == nullptr");
115 return nullptr;
116 }
117 return daemonProxy_;
118 }
119
OpenP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)120 int32_t DistributedFileDaemonProxy::OpenP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
121 {
122 LOGI("Open p2p connection");
123 MessageParcel data;
124 MessageParcel reply;
125 MessageOption option;
126 if (!data.WriteInterfaceToken(GetDescriptor())) {
127 LOGE("Failed to write interface token");
128 return OHOS::FileManagement::E_BROKEN_IPC;
129 }
130 if (!data.WriteCString(deviceInfo.deviceId)) {
131 LOGE("Failed to send device id");
132 return OHOS::FileManagement::E_INVAL_ARG;
133 }
134 if (!data.WriteCString(deviceInfo.deviceName)) {
135 LOGE("Failed to send device name");
136 return OHOS::FileManagement::E_INVAL_ARG;
137 }
138 if (!data.WriteCString(deviceInfo.networkId)) {
139 LOGE("Failed to send network id");
140 return OHOS::FileManagement::E_INVAL_ARG;
141 }
142 if (!data.WriteUint16(deviceInfo.deviceTypeId)) {
143 LOGE("Failed to send deviceTypeId");
144 return OHOS::FileManagement::E_INVAL_ARG;
145 }
146 if (!data.WriteUint32(deviceInfo.range)) {
147 LOGE("Failed to send range");
148 return OHOS::FileManagement::E_INVAL_ARG;
149 }
150 if (!data.WriteInt32(deviceInfo.authForm)) {
151 LOGE("Failed to send user id");
152 return OHOS::FileManagement::E_INVAL_ARG;
153 }
154 auto remote = Remote();
155 if (!remote) {
156 LOGE("remote is nullptr");
157 return OHOS::FileManagement::E_BROKEN_IPC;
158 }
159 int32_t ret = remote->SendRequest(
160 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION),
161 data, reply, option);
162 if (ret != 0) {
163 stringstream ss;
164 ss << "Failed to send out the requeset, errno:" << ret;
165 LOGE("%{public}s", ss.str().c_str());
166 return OHOS::FileManagement::E_BROKEN_IPC;
167 }
168 LOGI("Open p2p connection Success");
169 return reply.ReadInt32();
170 }
171
CloseP2PConnection(const DistributedHardware::DmDeviceInfo & deviceInfo)172 int32_t DistributedFileDaemonProxy::CloseP2PConnection(const DistributedHardware::DmDeviceInfo &deviceInfo)
173 {
174 LOGI("Close p2p connection");
175 MessageParcel data;
176 MessageParcel reply;
177 MessageOption option;
178 if (!data.WriteInterfaceToken(GetDescriptor())) {
179 LOGE("Failed to write interface token");
180 return OHOS::FileManagement::E_BROKEN_IPC;
181 }
182 if (!data.WriteCString(deviceInfo.deviceId)) {
183 LOGE("Failed to send device id");
184 return OHOS::FileManagement::E_INVAL_ARG;
185 }
186 if (!data.WriteCString(deviceInfo.deviceName)) {
187 LOGE("Failed to send device name");
188 return OHOS::FileManagement::E_INVAL_ARG;
189 }
190 if (!data.WriteCString(deviceInfo.networkId)) {
191 LOGE("Failed to send network id");
192 return OHOS::FileManagement::E_INVAL_ARG;
193 }
194 if (!data.WriteUint16(deviceInfo.deviceTypeId)) {
195 LOGE("Failed to send deviceTypeId");
196 return OHOS::FileManagement::E_INVAL_ARG;
197 }
198 if (!data.WriteUint32(deviceInfo.range)) {
199 LOGE("Failed to send range");
200 return OHOS::FileManagement::E_INVAL_ARG;
201 }
202 if (!data.WriteInt32(deviceInfo.authForm)) {
203 LOGE("Failed to send user id");
204 return OHOS::FileManagement::E_INVAL_ARG;
205 }
206 auto remote = Remote();
207 if (!remote) {
208 LOGE("remote is nullptr");
209 return OHOS::FileManagement::E_BROKEN_IPC;
210 }
211 int32_t ret = remote->SendRequest(
212 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION),
213 data, reply, option);
214 if (ret != 0) {
215 stringstream ss;
216 ss << "Failed to send out the requeset, errno:" << ret;
217 LOGE("%{public}s", ss.str().c_str());
218 return OHOS::FileManagement::E_BROKEN_IPC;
219 }
220 LOGI("Close p2p connection Success");
221 return reply.ReadInt32();
222 }
223
OpenP2PConnectionEx(const std::string & networkId,sptr<IFileDfsListener> remoteReverseObj)224 int32_t DistributedFileDaemonProxy::OpenP2PConnectionEx(const std::string &networkId,
225 sptr<IFileDfsListener> remoteReverseObj)
226 {
227 LOGI("DistributedFileDaemonProxy::OpenP2PConnectionEx start.");
228 MessageParcel data;
229 MessageParcel reply;
230 MessageOption option;
231 if (!data.WriteInterfaceToken(GetDescriptor())) {
232 LOGE("Failed to write interface token.");
233 return OHOS::FileManagement::E_BROKEN_IPC;
234 }
235 if (!data.WriteString(networkId)) {
236 LOGE("Failed to send network id.");
237 return OHOS::FileManagement::E_INVAL_ARG;
238 }
239 if (remoteReverseObj == nullptr) {
240 LOGE("remoteReverseObj is nullptr.");
241 return OHOS::FileManagement::E_BROKEN_IPC;
242 }
243 if (!data.WriteRemoteObject(remoteReverseObj->AsObject())) {
244 LOGE("fail to WriteRemoteObject remoteReverseObj");
245 return OHOS::FileManagement::E_BROKEN_IPC;
246 }
247
248 auto remote = Remote();
249 if (!remote) {
250 LOGE("remote is nullptr.");
251 return OHOS::FileManagement::E_BROKEN_IPC;
252 }
253 int32_t ret = remote->SendRequest(
254 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_OPEN_P2P_CONNECTION_EX),
255 data, reply, option);
256 if (ret != 0) {
257 LOGE("SendRequest failed, ret = %{public}d", ret);
258 return OHOS::FileManagement::E_BROKEN_IPC;
259 }
260 LOGI("DistributedFileDaemonProxy::OpenP2PConnectionEx success.");
261 return reply.ReadInt32();
262 }
263
CloseP2PConnectionEx(const std::string & networkId)264 int32_t DistributedFileDaemonProxy::CloseP2PConnectionEx(const std::string &networkId)
265 {
266 LOGI("Close p2p connection");
267 MessageParcel data;
268 MessageParcel reply;
269 MessageOption option;
270 if (!data.WriteInterfaceToken(GetDescriptor())) {
271 LOGE("Failed to write interface token.");
272 return OHOS::FileManagement::E_BROKEN_IPC;
273 }
274 if (!data.WriteString(networkId)) {
275 LOGE("Failed to send device id.");
276 return OHOS::FileManagement::E_INVAL_ARG;
277 }
278 auto remote = Remote();
279 if (!remote) {
280 LOGE("remote is nullptr.");
281 return OHOS::FileManagement::E_BROKEN_IPC;
282 }
283 int32_t ret = remote->SendRequest(
284 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CLOSE_P2P_CONNECTION_EX),
285 data, reply, option);
286 if (ret != 0) {
287 LOGE("SendRequest failed, ret = %{public}d", ret);
288 return OHOS::FileManagement::E_BROKEN_IPC;
289 }
290 LOGI("DistributedFileDaemonProxy::Close p2p connection Success");
291 return reply.ReadInt32();
292 }
293
PrepareSession(const std::string & srcUri,const std::string & dstUri,const std::string & srcDeviceId,const sptr<IRemoteObject> & listener,HmdfsInfo & info)294 int32_t DistributedFileDaemonProxy::PrepareSession(const std::string &srcUri,
295 const std::string &dstUri,
296 const std::string &srcDeviceId,
297 const sptr<IRemoteObject> &listener,
298 HmdfsInfo &info)
299 {
300 MessageParcel data;
301 MessageParcel reply;
302 MessageOption option;
303 if (!data.WriteInterfaceToken(GetDescriptor())) {
304 LOGE("Failed to write interface token");
305 return OHOS::FileManagement::E_BROKEN_IPC;
306 }
307 if (!data.WriteString(srcUri)) {
308 LOGE("Failed to send srcUri");
309 return OHOS::FileManagement::E_INVAL_ARG;
310 }
311 if (!data.WriteString(dstUri)) {
312 LOGE("Failed to send dstUri");
313 return OHOS::FileManagement::E_INVAL_ARG;
314 }
315 if (!data.WriteString(srcDeviceId)) {
316 LOGE("Failed to send remoteDeviceId");
317 return OHOS::FileManagement::E_INVAL_ARG;
318 }
319 if (!data.WriteRemoteObject(listener)) {
320 LOGE("Failed to send the listener callback stub");
321 return OHOS::FileManagement::E_INVAL_ARG;
322 }
323 if (!data.WriteString(info.copyPath)) {
324 LOGE("Failed to send info.copyPath");
325 return OHOS::FileManagement::E_INVAL_ARG;
326 }
327 if (!data.WriteBool(info.dirExistFlag)) {
328 LOGE("Failed to send info.dirExistFlag");
329 return OHOS::FileManagement::E_INVAL_ARG;
330 }
331 auto remote = Remote();
332 if (remote == nullptr) {
333 LOGE("remote is nullptr");
334 return OHOS::FileManagement::E_BROKEN_IPC;
335 }
336 int32_t ret =
337 remote->SendRequest(static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PREPARE_SESSION),
338 data, reply, option);
339 if (!reply.ReadString(info.sessionName)) {
340 LOGE("Failed to receive info.sessionName");
341 return OHOS::FileManagement::E_INVAL_ARG;
342 }
343 if (ret != 0) {
344 LOGE("SendRequest failed, ret = %{public}d", ret);
345 return OHOS::FileManagement::E_BROKEN_IPC;
346 }
347 return reply.ReadInt32();
348 }
349
RequestSendFile(const std::string & srcUri,const std::string & dstPath,const std::string & remoteDeviceId,const std::string & sessionName)350 int32_t DistributedFileDaemonProxy::RequestSendFile(const std::string &srcUri,
351 const std::string &dstPath,
352 const std::string &remoteDeviceId,
353 const std::string &sessionName)
354 {
355 MessageParcel data;
356 MessageParcel reply;
357 MessageOption option;
358 if (!data.WriteInterfaceToken(GetDescriptor())) {
359 LOGE("Failed to write interface token");
360 return OHOS::FileManagement::E_BROKEN_IPC;
361 }
362 if (!data.WriteString(srcUri)) {
363 LOGE("Failed to send srcUri");
364 return OHOS::FileManagement::E_INVAL_ARG;
365 }
366 if (!data.WriteString(dstPath)) {
367 LOGE("Failed to send dstPath");
368 return OHOS::FileManagement::E_INVAL_ARG;
369 }
370 if (!data.WriteString(remoteDeviceId)) {
371 LOGE("Failed to send remoteDeviceId");
372 return OHOS::FileManagement::E_INVAL_ARG;
373 }
374 if (!data.WriteString(sessionName)) {
375 LOGE("Failed to send sessionName");
376 return OHOS::FileManagement::E_INVAL_ARG;
377 }
378 auto remote = Remote();
379 if (remote == nullptr) {
380 LOGE("remote is nullptr");
381 return OHOS::FileManagement::E_BROKEN_IPC;
382 }
383 int32_t ret = remote->SendRequest(
384 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REQUEST_SEND_FILE), data, reply,
385 option);
386 if (ret != 0) {
387 LOGE("SendRequest failed, ret = %{public}d", ret);
388 return OHOS::FileManagement::E_BROKEN_IPC;
389 }
390 return reply.ReadInt32();
391 }
392
GetRemoteCopyInfo(const std::string & srcUri,bool & isFile,bool & isDir)393 int32_t DistributedFileDaemonProxy::GetRemoteCopyInfo(const std::string &srcUri, bool &isFile, bool &isDir)
394 {
395 MessageParcel data;
396 MessageParcel reply;
397 MessageOption option;
398 if (!data.WriteInterfaceToken(GetDescriptor())) {
399 LOGE("Failed to write interface token");
400 return OHOS::FileManagement::E_BROKEN_IPC;
401 }
402 if (!data.WriteString(srcUri)) {
403 LOGE("Failed to send srcUri");
404 return OHOS::FileManagement::E_INVAL_ARG;
405 }
406 auto remote = Remote();
407 if (remote == nullptr) {
408 LOGE("remote is nullptr");
409 return OHOS::FileManagement::E_BROKEN_IPC;
410 }
411 auto ret = remote->SendRequest(
412 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_REMOTE_COPY_INFO), data, reply,
413 option);
414 if (ret != 0) {
415 LOGE("SendRequest failed, ret = %{public}d", ret);
416 return OHOS::FileManagement::E_BROKEN_IPC;
417 }
418 if (!reply.ReadBool(isFile)) {
419 LOGE("read isFile failed");
420 return OHOS::FileManagement::E_INVAL_ARG;
421 }
422 if (!reply.ReadBool(isDir)) {
423 LOGE("read isDir failed");
424 return OHOS::FileManagement::E_INVAL_ARG;
425 }
426 if (!reply.ReadInt32(ret)) {
427 LOGE("read res failed");
428 return OHOS::FileManagement::E_INVAL_ARG;
429 }
430 return ret;
431 }
432
CancelCopyTask(const std::string & sessionName)433 int32_t DistributedFileDaemonProxy::CancelCopyTask(const std::string &sessionName)
434 {
435 MessageParcel data;
436 MessageParcel reply;
437 MessageOption option;
438 if (!data.WriteInterfaceToken(GetDescriptor())) {
439 LOGE("Failed to write interface token");
440 return OHOS::FileManagement::E_BROKEN_IPC;
441 }
442 if (!data.WriteString(sessionName)) {
443 LOGE("Failed to send sessionName");
444 return OHOS::FileManagement::E_INVAL_ARG;
445 }
446 auto remote = Remote();
447 if (remote == nullptr) {
448 LOGE("remote is nullptr");
449 return OHOS::FileManagement::E_BROKEN_IPC;
450 }
451 int32_t ret = remote->SendRequest(
452 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CANCEL_COPY_TASK), data, reply,
453 option);
454 if (ret != 0) {
455 LOGE("SendRequest failed, ret = %{public}d", ret);
456 return OHOS::FileManagement::E_BROKEN_IPC;
457 }
458 return reply.ReadInt32();
459 }
460
CancelCopyTask(const std::string & srcUri,const std::string & dstUri)461 int32_t DistributedFileDaemonProxy::CancelCopyTask(const std::string &srcUri, const std::string &dstUri)
462 {
463 MessageParcel data;
464 MessageParcel reply;
465 MessageOption option;
466 if (!data.WriteInterfaceToken(GetDescriptor())) {
467 LOGE("Failed to write interface token");
468 return OHOS::FileManagement::E_BROKEN_IPC;
469 }
470 if (!data.WriteString(srcUri)) {
471 LOGE("Failed to send srcUri");
472 return OHOS::FileManagement::E_INVAL_ARG;
473 }
474 if (!data.WriteString(dstUri)) {
475 LOGE("Failed to send dstUri");
476 return OHOS::FileManagement::E_INVAL_ARG;
477 }
478 auto remote = Remote();
479 if (remote == nullptr) {
480 LOGE("remote is nullptr");
481 return OHOS::FileManagement::E_BROKEN_IPC;
482 }
483 int32_t ret = remote->SendRequest(
484 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_CANCEL_INNER_COPY_TASK), data, reply,
485 option);
486 if (ret != 0) {
487 LOGE("SendRequest failed, ret = %{public}d", ret);
488 return OHOS::FileManagement::E_BROKEN_IPC;
489 }
490 return reply.ReadInt32();
491 }
492
PushAsset(int32_t userId,const sptr<AssetObj> & assetObj,const sptr<IAssetSendCallback> & sendCallback)493 int32_t DistributedFileDaemonProxy::PushAsset(int32_t userId,
494 const sptr<AssetObj> &assetObj,
495 const sptr<IAssetSendCallback> &sendCallback)
496 {
497 MessageParcel data;
498 MessageParcel reply;
499 MessageOption option;
500 if (!data.WriteInterfaceToken(GetDescriptor())) {
501 LOGE("Failed to write interface token");
502 return OHOS::FileManagement::E_BROKEN_IPC;
503 }
504
505 if (!data.WriteInt32(userId)) {
506 LOGE("Failed to send the user id");
507 return OHOS::FileManagement::E_INVAL_ARG;
508 }
509 if (!data.WriteParcelable(assetObj)) {
510 LOGE("Failed to send the assetInfoObj");
511 return OHOS::FileManagement::E_INVAL_ARG;
512 }
513 if (sendCallback == nullptr) {
514 LOGE("sendCallback is nullptr.");
515 return OHOS::FileManagement::E_INVAL_ARG;
516 }
517 if (!data.WriteRemoteObject(sendCallback->AsObject())) {
518 LOGE("Failed to send the listener callback stub");
519 return OHOS::FileManagement::E_INVAL_ARG;
520 }
521 auto remote = Remote();
522 if (remote == nullptr) {
523 LOGE("remote is nullptr");
524 return OHOS::FileManagement::E_BROKEN_IPC;
525 }
526 int32_t ret = remote->SendRequest(
527 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_PUSH_ASSET), data, reply,
528 option);
529 if (ret != 0) {
530 LOGE("UnRegisterAssetCallback failed, ret = %{public}d", ret);
531 return OHOS::FileManagement::E_BROKEN_IPC;
532 }
533 return reply.ReadInt32();
534 }
535
RegisterAssetCallback(const sptr<IAssetRecvCallback> & recvCallback)536 int32_t DistributedFileDaemonProxy::RegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback)
537 {
538 MessageParcel data;
539 MessageParcel reply;
540 MessageOption option;
541 if (!data.WriteInterfaceToken(GetDescriptor())) {
542 LOGE("Failed to write interface token");
543 return OHOS::FileManagement::E_BROKEN_IPC;
544 }
545 if (recvCallback == nullptr) {
546 LOGE("recvCallback is nullptr.");
547 return OHOS::FileManagement::E_INVAL_ARG;
548 }
549 if (!data.WriteRemoteObject(recvCallback->AsObject())) {
550 LOGE("Failed to send the listener callback stub");
551 return OHOS::FileManagement::E_INVAL_ARG;
552 }
553 auto remote = Remote();
554 if (remote == nullptr) {
555 LOGE("remote is nullptr");
556 return OHOS::FileManagement::E_BROKEN_IPC;
557 }
558 int32_t ret = remote->SendRequest(
559 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_REGISTER_ASSET_CALLBACK), data,
560 reply, option);
561 if (ret != 0) {
562 LOGE("RegisterAssetCallback failed, ret = %{public}d", ret);
563 return OHOS::FileManagement::E_BROKEN_IPC;
564 }
565 return reply.ReadInt32();
566 }
567
UnRegisterAssetCallback(const sptr<IAssetRecvCallback> & recvCallback)568 int32_t DistributedFileDaemonProxy::UnRegisterAssetCallback(const sptr<IAssetRecvCallback> &recvCallback)
569 {
570 MessageParcel data;
571 MessageParcel reply;
572 MessageOption option;
573 if (!data.WriteInterfaceToken(GetDescriptor())) {
574 LOGE("Failed to write interface token");
575 return OHOS::FileManagement::E_BROKEN_IPC;
576 }
577 if (recvCallback == nullptr) {
578 LOGE("recvCallback is nullptr.");
579 return OHOS::FileManagement::E_INVAL_ARG;
580 }
581 if (!data.WriteRemoteObject(recvCallback->AsObject())) {
582 LOGE("Failed to send the listener callback stub");
583 return OHOS::FileManagement::E_INVAL_ARG;
584 }
585 auto remote = Remote();
586 if (remote == nullptr) {
587 LOGE("remote is nullptr");
588 return OHOS::FileManagement::E_BROKEN_IPC;
589 }
590 int32_t ret = remote->SendRequest(
591 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_UN_REGISTER_ASSET_CALLBACK), data,
592 reply, option);
593 if (ret != 0) {
594 LOGE("UnRegisterAssetCallback failed, ret = %{public}d", ret);
595 return OHOS::FileManagement::E_BROKEN_IPC;
596 }
597 return reply.ReadInt32();
598 }
599
GetDfsUrisDirFromLocal(const std::vector<std::string> & uriList,const int32_t userId,std::unordered_map<std::string,AppFileService::ModuleRemoteFileShare::HmdfsUriInfo> & uriToDfsUriMaps)600 int32_t DistributedFileDaemonProxy::GetDfsUrisDirFromLocal(const std::vector<std::string> &uriList,
601 const int32_t userId,
602 std::unordered_map<std::string,
603 AppFileService::ModuleRemoteFileShare::HmdfsUriInfo>
604 &uriToDfsUriMaps)
605 {
606 LOGI("GetDfsUrisDirFromLocal begin");
607 MessageParcel data;
608 MessageParcel reply;
609 MessageOption option;
610
611 if (!data.WriteInterfaceToken(GetDescriptor())) {
612 LOGE("Failed to write interface token");
613 return OHOS::FileManagement::E_BROKEN_IPC;
614 }
615 if (!IpcWrapper::WriteBatchUris(data, uriList)) {
616 LOGE("Failed to send uriList");
617 return OHOS::FileManagement::E_INVAL_ARG;
618 }
619 if (!data.WriteInt32(userId)) {
620 LOGE("Failed to send userId");
621 return OHOS::FileManagement::E_INVAL_ARG;
622 }
623
624 auto remote = Remote();
625 if (!remote) {
626 LOGE("remote is nullptr");
627 return OHOS::FileManagement::E_BROKEN_IPC;
628 }
629
630 int32_t ret = remote->SendRequest(
631 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::GET_DFS_URI_IS_DIR_FROM_LOCAL),
632 data, reply, option);
633 if (ret != 0) {
634 stringstream ss;
635 ss << "Failed to send out the requeset, errno:" << ret;
636 LOGE("%{public}s", ss.str().c_str());
637 return OHOS::FileManagement::E_BROKEN_IPC;
638 }
639
640 if (!reply.ReadInt32(ret)) {
641 LOGE("Failed to read res");
642 return OHOS::FileManagement::E_BROKEN_IPC;
643 }
644
645 if (ret != OHOS::FileManagement::E_OK) {
646 LOGE("Failed to GetDfsUrisDirFromLocal, res: %{public}d", ret);
647 return ret;
648 }
649 std::vector<std::string> total;
650 if (IpcWrapper::ReadBatchUris(reply, total) != FileManagement::E_OK) {
651 LOGE("read total failed");
652 return OHOS::FileManagement::E_IPC_READ_FAILED;
653 }
654
655 LOGI("proxy total.size(): %{public}d", static_cast<int>(total.size()));
656 return ReadUriInfoFromResult(total, uriToDfsUriMaps);
657 }
658
GetDfsSwitchStatus(const std::string & networkId,int32_t & switchStatus)659 int32_t DistributedFileDaemonProxy::GetDfsSwitchStatus(const std::string &networkId, int32_t &switchStatus)
660 {
661 MessageParcel data;
662 MessageParcel reply;
663 MessageOption option;
664 if (!data.WriteInterfaceToken(GetDescriptor())) {
665 LOGE("Failed to write interface token");
666 return OHOS::FileManagement::E_BROKEN_IPC;
667 }
668 if (!data.WriteString(networkId)) {
669 LOGE("Failed to write network id.");
670 return OHOS::FileManagement::E_BROKEN_IPC;
671 }
672 auto remote = Remote();
673 if (remote == nullptr) {
674 LOGE("Remote is nullptr");
675 return OHOS::FileManagement::E_BROKEN_IPC;
676 }
677 int32_t ret = remote->SendRequest(
678 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_DFS_SWITCH_STATUS), data,
679 reply, option);
680 if (ret != FileManagement::E_OK) {
681 LOGE("GetDfsSwitchStatus ipc failed, ret = %{public}d", ret);
682 return OHOS::FileManagement::E_BROKEN_IPC;
683 }
684 if (!reply.ReadInt32(ret)) {
685 LOGE("Read ret failed");
686 return OHOS::FileManagement::E_BROKEN_IPC;
687 }
688 if (ret != FileManagement::E_OK) {
689 LOGE("GetDfsSwitchStatus failed, ret = %{public}d", ret);
690 return ret;
691 }
692 if (!reply.ReadInt32(switchStatus)) {
693 LOGE("Read switchStatus failed");
694 return OHOS::FileManagement::E_BROKEN_IPC;
695 }
696 return ret;
697 }
698
UpdateDfsSwitchStatus(int32_t switchStatus)699 int32_t DistributedFileDaemonProxy::UpdateDfsSwitchStatus(int32_t switchStatus)
700 {
701 MessageParcel data;
702 MessageParcel reply;
703 MessageOption option;
704 if (!data.WriteInterfaceToken(GetDescriptor())) {
705 LOGE("Failed to write interface token");
706 return OHOS::FileManagement::E_BROKEN_IPC;
707 }
708 if (!data.WriteInt32(switchStatus)) {
709 LOGE("Failed to write the switch status");
710 return OHOS::FileManagement::E_BROKEN_IPC;
711 }
712 auto remote = Remote();
713 if (remote == nullptr) {
714 LOGE("Remote is nullptr");
715 return OHOS::FileManagement::E_BROKEN_IPC;
716 }
717 int32_t ret = remote->SendRequest(
718 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_UPDATE_DFS_SWITCH_STATUS), data,
719 reply, option);
720 if (ret != FileManagement::E_OK) {
721 LOGE("UpdateDfsSwitchStatus failed, ret = %{public}d", ret);
722 return OHOS::FileManagement::E_BROKEN_IPC;
723 }
724 if (!reply.ReadInt32(ret)) {
725 LOGE("Read ret failed");
726 return OHOS::FileManagement::E_BROKEN_IPC;
727 }
728 return ret;
729 }
730
GetConnectedDeviceList(std::vector<DfsDeviceInfo> & deviceList)731 int32_t DistributedFileDaemonProxy::GetConnectedDeviceList(std::vector<DfsDeviceInfo> &deviceList)
732 {
733 MessageParcel data;
734 MessageParcel reply;
735 MessageOption option;
736 if (!data.WriteInterfaceToken(GetDescriptor())) {
737 LOGE("Failed to write interface token");
738 return OHOS::FileManagement::E_BROKEN_IPC;
739 }
740 auto remote = Remote();
741 if (remote == nullptr) {
742 LOGE("remote is nullptr");
743 return OHOS::FileManagement::E_BROKEN_IPC;
744 }
745 int32_t ret = remote->SendRequest(
746 static_cast<uint32_t>(DistributedFileDaemonInterfaceCode::DISTRIBUTED_FILE_GET_CONNECTED_DEVICE_LIST), data,
747 reply, option);
748 if (ret != FileManagement::E_OK) {
749 LOGE("GetConnectedDeviceList ipc failed, ret: %{public}d", ret);
750 return OHOS::FileManagement::E_BROKEN_IPC;
751 }
752 if (!reply.ReadInt32(ret)) {
753 LOGE("Read res failed");
754 return OHOS::FileManagement::E_BROKEN_IPC;
755 }
756 if (ret != FileManagement::E_OK) {
757 LOGE("GetConnectedDeviceList failed, ret: %{public}d", ret);
758 return ret;
759 }
760 int32_t len = 0;
761 if (!reply.ReadInt32(len)) {
762 LOGE("Read devicelist length failed");
763 return OHOS::FileManagement::E_BROKEN_IPC;
764 }
765 deviceList.clear();
766 for (int32_t i = 0; i < len; ++i) {
767 DfsDeviceInfo deviceInfo;
768 if (!reply.ReadString(deviceInfo.networkId_)) {
769 LOGE("Failed to read networkId");
770 return OHOS::FileManagement::E_BROKEN_IPC;
771 }
772 if (!reply.ReadString(deviceInfo.path_)) {
773 LOGE("Failed to read path");
774 return OHOS::FileManagement::E_BROKEN_IPC;
775 }
776 deviceList.emplace_back(deviceInfo);
777 }
778 return ret;
779 }
780 } // namespace DistributedFile
781 } // namespace Storage
782 } // namespace OHOS