1 /*
2 * Copyright (c) 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 "file_access_ext_proxy.h"
17
18 #include "file_access_framework_errno.h"
19 #include "file_access_extension_info.h"
20 #include "hilog_wrapper.h"
21 #include "hitrace_meter.h"
22 #include "ipc_types.h"
23 #include "message_option.h"
24 #include "message_parcel.h"
25 #include "user_access_tracer.h"
26
27 namespace OHOS {
28 namespace FileAccessFwk {
29 namespace {
30 constexpr int COPY_EXCEPTION = -1;
31 constexpr uint32_t MAX_COPY_ERROR_COUNT = 1000;
32 };
OpenFile(const Uri & uri,const int flags,int & fd)33 int FileAccessExtProxy::OpenFile(const Uri &uri, const int flags, int &fd)
34 {
35 UserAccessTracer trace;
36 trace.Start("OpenFile");
37 MessageParcel data;
38 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
39 HILOG_ERROR("WriteInterfaceToken failed");
40 return E_IPCS;
41 }
42
43 if (!data.WriteParcelable(&uri)) {
44 HILOG_ERROR("fail to WriteParcelable uri");
45 return E_IPCS;
46 }
47
48 if (!data.WriteInt32(flags)) {
49 HILOG_ERROR("fail to WriteInt32 flags");
50 return E_IPCS;
51 }
52
53 MessageParcel reply;
54 MessageOption option;
55 int err = Remote()->SendRequest(CMD_OPEN_FILE, data, reply, option);
56 if (err != ERR_OK) {
57 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
58 return err;
59 }
60
61 int ret = E_IPCS;
62 if (!reply.ReadInt32(ret)) {
63 HILOG_ERROR("fail to ReadInt32 ret");
64 return E_IPCS;
65 }
66
67 if (ret != ERR_OK) {
68 HILOG_ERROR("OpenFile operation failed ret : %{public}d", ret);
69 return ret;
70 }
71
72 fd = reply.ReadFileDescriptor();
73 if (fd < ERR_OK) {
74 HILOG_ERROR("fail to ReadFileDescriptor fd: %{public}d", fd);
75 return E_GETRESULT;
76 }
77
78 return ERR_OK;
79 }
80
CreateFile(const Uri & parent,const std::string & displayName,Uri & newFile)81 int FileAccessExtProxy::CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile)
82 {
83 UserAccessTracer trace;
84 trace.Start("CreateFile");
85 MessageParcel data;
86 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
87 HILOG_ERROR("WriteInterfaceToken failed");
88 return E_IPCS;
89 }
90
91 if (!data.WriteParcelable(&parent)) {
92 HILOG_ERROR("fail to WriteParcelable parent");
93 return E_IPCS;
94 }
95
96 if (!data.WriteString(displayName)) {
97 HILOG_ERROR("fail to WriteString displayName");
98 return E_IPCS;
99 }
100
101 MessageParcel reply;
102 MessageOption option;
103 int err = Remote()->SendRequest(CMD_CREATE_FILE, data, reply, option);
104 if (err != ERR_OK) {
105 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
106 return err;
107 }
108
109 int ret = E_IPCS;
110 if (!reply.ReadInt32(ret)) {
111 HILOG_ERROR("fail to ReadInt32 ret");
112 return E_IPCS;
113 }
114
115 if (ret != ERR_OK) {
116 HILOG_ERROR("CreateFile operation failed ret : %{public}d", ret);
117 return ret;
118 }
119
120 std::unique_ptr<Uri> tempUri(reply.ReadParcelable<Uri>());
121 if (tempUri == nullptr) {
122 HILOG_ERROR("ReadParcelable value is nullptr.");
123 return E_IPCS;
124 }
125
126 newFile = Uri(*tempUri);
127 if (newFile.ToString().empty()) {
128 HILOG_ERROR("get uri is empty.");
129 return E_GETRESULT;
130 }
131
132 return ERR_OK;
133 }
134
Mkdir(const Uri & parent,const std::string & displayName,Uri & newFile)135 int FileAccessExtProxy::Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile)
136 {
137 UserAccessTracer trace;
138 trace.Start("Mkdir");
139 MessageParcel data;
140 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
141 HILOG_ERROR("WriteInterfaceToken failed");
142 return E_IPCS;
143 }
144
145 if (!data.WriteParcelable(&parent)) {
146 HILOG_ERROR("fail to WriteParcelable parent");
147 return E_IPCS;
148 }
149
150 if (!data.WriteString(displayName)) {
151 HILOG_ERROR("fail to WriteString displayName");
152 return E_IPCS;
153 }
154
155 MessageParcel reply;
156 MessageOption option;
157 int err = Remote()->SendRequest(CMD_MKDIR, data, reply, option);
158 if (err != ERR_OK) {
159 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
160 return err;
161 }
162
163 int ret = E_IPCS;
164 if (!reply.ReadInt32(ret)) {
165 HILOG_ERROR("fail to ReadInt32 ret");
166 return E_IPCS;
167 }
168
169 if (ret != ERR_OK) {
170 HILOG_ERROR("Mkdir operation failed ret : %{public}d", ret);
171 return ret;
172 }
173
174 std::unique_ptr<Uri> tempUri(reply.ReadParcelable<Uri>());
175 if (tempUri == nullptr) {
176 HILOG_ERROR("ReadParcelable value is nullptr.");
177 return E_IPCS;
178 }
179
180 newFile = Uri(*tempUri);
181 if (newFile.ToString().empty()) {
182 HILOG_ERROR("get uri is empty.");
183 return E_GETRESULT;
184 }
185
186 return ERR_OK;
187 }
188
Delete(const Uri & sourceFile)189 int FileAccessExtProxy::Delete(const Uri &sourceFile)
190 {
191 UserAccessTracer trace;
192 trace.Start("Delete");
193 MessageParcel data;
194 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
195 HILOG_ERROR("WriteInterfaceToken failed");
196 return E_IPCS;
197 }
198
199 if (!data.WriteParcelable(&sourceFile)) {
200 HILOG_ERROR("fail to WriteParcelable sourceFile");
201 return E_IPCS;
202 }
203
204 MessageParcel reply;
205 MessageOption option;
206 int err = Remote()->SendRequest(CMD_DELETE, data, reply, option);
207 if (err != ERR_OK) {
208 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
209 return err;
210 }
211
212 int ret = E_IPCS;
213 if (!reply.ReadInt32(ret)) {
214 HILOG_ERROR("fail to ReadInt32 ret");
215 return E_IPCS;
216 }
217
218 if (ret != ERR_OK) {
219 HILOG_ERROR("Delete operation failed ret : %{public}d", ret);
220 return ret;
221 }
222
223 return ERR_OK;
224 }
225
Move(const Uri & sourceFile,const Uri & targetParent,Uri & newFile)226 int FileAccessExtProxy::Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile)
227 {
228 UserAccessTracer trace;
229 trace.Start("Move");
230 MessageParcel data;
231 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
232 HILOG_ERROR("WriteInterfaceToken failed");
233 return E_IPCS;
234 }
235
236 if (!data.WriteParcelable(&sourceFile)) {
237 HILOG_ERROR("fail to WriteParcelable sourceFile");
238 return E_IPCS;
239 }
240
241 if (!data.WriteParcelable(&targetParent)) {
242 HILOG_ERROR("fail to WriteParcelable targetParent");
243 return E_IPCS;
244 }
245
246 MessageParcel reply;
247 MessageOption option;
248 int err = Remote()->SendRequest(CMD_MOVE, data, reply, option);
249 if (err != ERR_OK) {
250 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
251 return err;
252 }
253
254 int ret = E_IPCS;
255 if (!reply.ReadInt32(ret)) {
256 HILOG_ERROR("fail to ReadInt32 ret");
257 return E_IPCS;
258 }
259
260 if (ret != ERR_OK) {
261 HILOG_ERROR("Move operation failed ret : %{public}d", ret);
262 return ret;
263 }
264
265 std::unique_ptr<Uri> tempUri(reply.ReadParcelable<Uri>());
266 if (tempUri == nullptr) {
267 HILOG_ERROR("ReadParcelable value is nullptr.");
268 return E_IPCS;
269 }
270
271 newFile = Uri(*tempUri);
272 if (newFile.ToString().empty()) {
273 HILOG_ERROR("get uri is empty.");
274 return E_GETRESULT;
275 }
276
277 return ERR_OK;
278 }
279
WriteCopyFuncArguments(OHOS::MessageParcel & data,const Uri & sourceUri,const Uri & destUri,bool force)280 static int WriteCopyFuncArguments(OHOS::MessageParcel &data, const Uri &sourceUri, const Uri &destUri, bool force)
281 {
282 UserAccessTracer trace;
283 trace.Start("WriteCopyFuncArguments");
284 if (!data.WriteParcelable(&sourceUri)) {
285 HILOG_ERROR("fail to WriteParcelable uri");
286 return E_IPCS;
287 }
288
289 if (!data.WriteParcelable(&destUri)) {
290 HILOG_ERROR("fail to WriteParcelable targetParent");
291 return E_IPCS;
292 }
293
294 if (!data.WriteBool(force)) {
295 HILOG_ERROR("fail to WriteBool force");
296 return E_IPCS;
297 }
298 return ERR_OK;
299 }
300
WriteCopyFileFuncArguments(OHOS::MessageParcel & data,const Uri & sourceUri,const Uri & destUri,const std::string & fileName)301 static int WriteCopyFileFuncArguments(OHOS::MessageParcel &data, const Uri &sourceUri, const Uri &destUri,
302 const std::string &fileName)
303 {
304 UserAccessTracer trace;
305 trace.Start("WriteCopyFuncArguments");
306 if (!data.WriteString(sourceUri.ToString())) {
307 HILOG_ERROR("fail to WriteParcelable sourceUri");
308 return E_IPCS;
309 }
310 if (!data.WriteString(destUri.ToString())) {
311 HILOG_ERROR("fail to WriteParcelable destUri");
312 return E_IPCS;
313 }
314 if (!data.WriteString(fileName)) {
315 HILOG_ERROR("fail to WriteString fileName");
316 return E_IPCS;
317 }
318 return ERR_OK;
319 }
320
ReadCopyFuncResults(OHOS::MessageParcel & reply,std::vector<Result> & copyResult)321 static int ReadCopyFuncResults(OHOS::MessageParcel &reply, std::vector<Result> ©Result)
322 {
323 UserAccessTracer trace;
324 trace.Start("ReadCopyFuncResults");
325 int ret = E_IPCS;
326 if (!reply.ReadInt32(ret)) {
327 HILOG_ERROR("fail to ReadInt32 ret");
328 return E_IPCS;
329 }
330 if (ret == ERR_OK) {
331 HILOG_ERROR("Copy operation success");
332 return ret;
333 }
334
335 uint32_t count = 0;
336 if (!reply.ReadUint32(count)) {
337 HILOG_ERROR("Copy operation failed to Read count");
338 return E_IPCS;
339 }
340 if (count > MAX_COPY_ERROR_COUNT) {
341 HILOG_ERROR("Copy operation failed, count value greater than max count");
342 Result result { "", "", E_COUNT, "Count value greater than max count"};
343 copyResult.clear();
344 copyResult.push_back(result);
345 return COPY_EXCEPTION;
346 }
347
348 copyResult.clear();
349 for (uint32_t i = 0; i < count; i++) {
350 std::unique_ptr<Result> copyResultPtr(reply.ReadParcelable<Result>());
351 if (copyResultPtr != nullptr) {
352 copyResult.push_back(*copyResultPtr);
353 }
354 }
355 return ret;
356 }
357
Copy(const Uri & sourceUri,const Uri & destUri,std::vector<Result> & copyResult,bool force)358 int FileAccessExtProxy::Copy(const Uri &sourceUri, const Uri &destUri, std::vector<Result> ©Result, bool force)
359 {
360 UserAccessTracer trace;
361 trace.Start("Copy");
362 MessageParcel data;
363 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
364 HILOG_ERROR("WriteInterfaceToken failed");
365 return E_IPCS;
366 }
367
368 int ret = WriteCopyFuncArguments(data, sourceUri, destUri, force);
369 if (ret != ERR_OK) {
370 HILOG_ERROR("Write copy function arguments error, code: %{public}d", ret);
371 return ret;
372 }
373
374 MessageParcel reply;
375 MessageOption option;
376 int err = Remote()->SendRequest(CMD_COPY, data, reply, option);
377 if (err != ERR_OK) {
378 HILOG_ERROR("fail to SendRequest, err: %{public}d", err);
379 return err;
380 }
381
382 ret = ReadCopyFuncResults(reply, copyResult);
383 if (ret != ERR_OK) {
384 HILOG_ERROR("Read copy function result error, code: %{public}d", ret);
385 return ret;
386 }
387
388 return ret;
389 }
390
CopyFile(const Uri & sourceUri,const Uri & destUri,const std::string & fileName,Uri & newFileUri)391 int FileAccessExtProxy::CopyFile(const Uri &sourceUri, const Uri &destUri, const std::string &fileName, Uri &newFileUri)
392 {
393 UserAccessTracer trace;
394 trace.Start("CopyFile");
395 MessageParcel data;
396 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
397 HILOG_ERROR("WriteInterfaceToken failed");
398 return E_IPCS;
399 }
400
401 int ret = WriteCopyFileFuncArguments(data, sourceUri, destUri, fileName);
402 if (ret != ERR_OK) {
403 HILOG_ERROR("Write copy file function arguments error, code: %{public}d", ret);
404 return ret;
405 }
406
407 MessageParcel reply;
408 MessageOption option;
409 int err = Remote()->SendRequest(CMD_COPY_FILE, data, reply, option);
410 if (err != ERR_OK) {
411 HILOG_ERROR("fail to SendRequest, err: %{public}d", err);
412 return err;
413 }
414
415 if (!reply.ReadInt32(ret)) {
416 HILOG_ERROR("fail to ReadInt32 ret");
417 return E_IPCS;
418 }
419 if (ret != ERR_OK) {
420 HILOG_ERROR("Copy file error, code:%{public}d", ret);
421 return ret;
422 }
423 std::string tempUri = "";
424 if (!reply.ReadString(tempUri)) {
425 HILOG_ERROR("fail to ReadString copyResult");
426 return E_IPCS;
427 }
428 if (tempUri.empty()) {
429 HILOG_ERROR("get uri is empty");
430 return E_GETRESULT;
431 }
432 newFileUri = Uri(tempUri);
433 return ERR_OK;
434 }
435
Rename(const Uri & sourceFile,const std::string & displayName,Uri & newFile)436 int FileAccessExtProxy::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile)
437 {
438 UserAccessTracer trace;
439 trace.Start("Rename");
440 MessageParcel data;
441 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
442 HILOG_ERROR("WriteInterfaceToken failed");
443 return E_IPCS;
444 }
445
446 if (!data.WriteParcelable(&sourceFile)) {
447 HILOG_ERROR("fail to WriteParcelable sourceFile");
448 return E_IPCS;
449 }
450
451 if (!data.WriteString(displayName)) {
452 HILOG_ERROR("fail to WriteString displayName");
453 return E_IPCS;
454 }
455
456 MessageParcel reply;
457 MessageOption option;
458 int err = Remote()->SendRequest(CMD_RENAME, data, reply, option);
459 if (err != ERR_OK) {
460 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
461 return err;
462 }
463
464 int ret = E_IPCS;
465 if (!reply.ReadInt32(ret)) {
466 HILOG_ERROR("fail to ReadInt32 ret");
467 return E_IPCS;
468 }
469
470 if (ret != ERR_OK) {
471 HILOG_ERROR("Rename operation failed ret : %{public}d", ret);
472 return ret;
473 }
474
475 std::unique_ptr<Uri> tempUri(reply.ReadParcelable<Uri>());
476 if (tempUri == nullptr) {
477 HILOG_ERROR("ReadParcelable value is nullptr.");
478 return E_IPCS;
479 }
480
481 newFile = Uri(*tempUri);
482 if (newFile.ToString().empty()) {
483 HILOG_ERROR("get uri is empty.");
484 return E_GETRESULT;
485 }
486
487 return ERR_OK;
488 }
489
GetListFileResult(MessageParcel & reply,std::vector<FileInfo> & fileInfoVec)490 static int GetListFileResult(MessageParcel &reply, std::vector<FileInfo> &fileInfoVec)
491 {
492 int ret = E_IPCS;
493 if (!reply.ReadInt32(ret)) {
494 HILOG_ERROR("fail to ReadInt32 ret");
495 return E_IPCS;
496 }
497
498 if (ret != ERR_OK) {
499 HILOG_ERROR("ListFile operation failed ret : %{public}d", ret);
500 return ret;
501 }
502
503 int64_t count = 0;
504 if (!reply.ReadInt64(count)) {
505 HILOG_ERROR("ListFile operation failed to Read count");
506 return E_IPCS;
507 }
508
509 fileInfoVec.clear();
510 for (int64_t i = 0; i < count; i++) {
511 std::unique_ptr<FileInfo> fileInfoPtr(reply.ReadParcelable<FileInfo>());
512 if (fileInfoPtr != nullptr) {
513 fileInfoVec.push_back(*fileInfoPtr);
514 }
515 }
516 return ERR_OK;
517 }
518
WriteFileFilterFuncArguments(MessageParcel & data,std::tuple<const FileInfo *,int64_t,const FileFilter *,SharedMemoryInfo * > args)519 static int WriteFileFilterFuncArguments(MessageParcel &data,
520 std::tuple<const FileInfo *, int64_t, const FileFilter *, SharedMemoryInfo *> args)
521 {
522 const FileInfo *fileInfo;
523 int64_t offset;
524 const FileFilter *filter;
525 SharedMemoryInfo *memInfo;
526 std::tie(fileInfo, offset, filter, memInfo) = args;
527
528 if (!data.WriteParcelable(fileInfo)) {
529 HILOG_ERROR("fail to WriteParcelable fileInfo");
530 return E_IPCS;
531 }
532
533 if (!data.WriteInt64(offset)) {
534 HILOG_ERROR("fail to WriteInt64 offset");
535 return E_IPCS;
536 }
537
538 if (!data.WriteParcelable(filter)) {
539 HILOG_ERROR("fail to WriteParcelable filter");
540 return E_IPCS;
541 }
542
543 if (!data.WriteParcelable(memInfo)) {
544 HILOG_ERROR("fail to WriteParcelable memInfo");
545 return E_IPCS;
546 }
547 return ERR_OK;
548 }
549
ReadFileFilterResults(MessageParcel & reply,SharedMemoryInfo & memInfo)550 static int ReadFileFilterResults(MessageParcel &reply, SharedMemoryInfo &memInfo)
551 {
552 if (!reply.ReadUint32(memInfo.dataCounts)) {
553 HILOG_ERROR("fail to ReadUnt64 dataCounts");
554 return E_IPCS;
555 }
556 memInfo.totalDataCounts = memInfo.dataCounts;
557
558 if (!reply.ReadUint64(memInfo.dataSize)) {
559 HILOG_ERROR("fail to ReadUnt64 dataSize");
560 return E_IPCS;
561 }
562
563 if (!reply.ReadUint32(memInfo.leftDataCounts)) {
564 HILOG_ERROR("fail to ReadUnt64 leftDataCounts");
565 return E_IPCS;
566 }
567
568 if (!reply.ReadBool(memInfo.isOver)) {
569 HILOG_ERROR("fail to ReadBool isOver");
570 return E_IPCS;
571 }
572 return ERR_OK;
573 }
574
ListFile(const FileInfo & fileInfo,const int64_t offset,const FileFilter & filter,SharedMemoryInfo & memInfo)575 int FileAccessExtProxy::ListFile(const FileInfo &fileInfo, const int64_t offset,
576 const FileFilter &filter, SharedMemoryInfo &memInfo)
577 {
578 UserAccessTracer trace;
579 trace.Start("ListFile");
580 MessageParcel data;
581 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
582 HILOG_ERROR("WriteInterfaceToken failed");
583 return E_IPCS;
584 }
585
586 int ret = WriteFileFilterFuncArguments(data, std::make_tuple(&fileInfo, offset, &filter, &memInfo));
587 if (ret != ERR_OK) {
588 return ret;
589 }
590
591 MessageParcel reply;
592 MessageOption option;
593 int err = Remote()->SendRequest(CMD_LIST_FILE, data, reply, option);
594 if (err != ERR_OK) {
595 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
596 return err;
597 }
598 if (!reply.ReadInt32(ret)) {
599 HILOG_ERROR("fail to ReadInt32 ret");
600 return E_IPCS;
601 }
602 if (ret != ERR_OK) {
603 HILOG_ERROR("ListFile operation failed ret : %{public}d", ret);
604 return ret;
605 }
606
607 ret = ReadFileFilterResults(reply, memInfo);
608 if (ret != ERR_OK) {
609 HILOG_ERROR("fail to read server return results. ret: %{public}d", ret);
610 return ret;
611 }
612 return ERR_OK;
613 }
614
ScanFile(const FileInfo & fileInfo,const int64_t offset,const int64_t maxCount,const FileFilter & filter,std::vector<FileInfo> & fileInfoVec)615 int FileAccessExtProxy::ScanFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount,
616 const FileFilter &filter, std::vector<FileInfo> &fileInfoVec)
617 {
618 UserAccessTracer trace;
619 trace.Start("ScanFile");
620 MessageParcel data;
621 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
622 HILOG_ERROR("WriteInterfaceToken failed");
623 return E_IPCS;
624 }
625
626 if (!data.WriteParcelable(&fileInfo)) {
627 HILOG_ERROR("fail to WriteParcelable fileInfo");
628 return E_IPCS;
629 }
630
631 if (!data.WriteInt64(offset)) {
632 HILOG_ERROR("fail to WriteInt64 offset");
633 return E_IPCS;
634 }
635
636 if (!data.WriteInt64(maxCount)) {
637 HILOG_ERROR("fail to WriteInt64 maxCount");
638 return E_IPCS;
639 }
640
641 if (!data.WriteParcelable(&filter)) {
642 HILOG_ERROR("fail to WriteParcelable filter");
643 return E_IPCS;
644 }
645
646 MessageParcel reply;
647 MessageOption option;
648 int err = Remote()->SendRequest(CMD_SCAN_FILE, data, reply, option);
649 if (err != ERR_OK) {
650 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
651 return err;
652 }
653
654 err = GetListFileResult(reply, fileInfoVec);
655 if (err != ERR_OK) {
656 HILOG_ERROR("fail to GetListFileResult. err: %{public}d", err);
657 return err;
658 }
659
660 return ERR_OK;
661 }
662
GetQueryResult(MessageParcel & reply,std::vector<std::string> & results)663 static int GetQueryResult(MessageParcel &reply, std::vector<std::string> &results)
664 {
665 int ret = E_IPCS;
666 if (!reply.ReadInt32(ret)) {
667 HILOG_ERROR("fail to ReadInt32 ret");
668 return E_IPCS;
669 }
670
671 if (ret != ERR_OK) {
672 HILOG_ERROR("Query operation failed ret : %{public}d", ret);
673 return ret;
674 }
675
676 int64_t count = 0;
677 if (!reply.ReadInt64(count)) {
678 HILOG_ERROR("Query operation failed to Read count");
679 return E_IPCS;
680 }
681
682 results.clear();
683 for (int64_t i = 0; i < count; i++) {
684 results.push_back(reply.ReadString());
685 }
686 return ERR_OK;
687 }
688
Query(const Uri & uri,std::vector<std::string> & columns,std::vector<std::string> & results)689 int FileAccessExtProxy::Query(const Uri &uri, std::vector<std::string> &columns, std::vector<std::string> &results)
690 {
691 UserAccessTracer trace;
692 trace.Start("Query");
693 MessageParcel data;
694 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
695 HILOG_ERROR("WriteInterfaceToken failed");
696 return E_IPCS;
697 }
698
699 if (!data.WriteParcelable(&uri)) {
700 HILOG_ERROR("fail to WriteParcelable sourceFile");
701 return E_IPCS;
702 }
703 size_t count = columns.size();
704 if (!data.WriteInt64(count)) {
705 HILOG_ERROR("Parameter Query fail to WriteInt64 count");
706 return E_IPCS;
707 }
708 if (count > FILE_RESULT_TYPE.size()) {
709 HILOG_ERROR(" The number of query operations exceeds %{public}zu ", FILE_RESULT_TYPE.size());
710 return EINVAL;
711 }
712
713 for (const auto &column : columns) {
714 if (!data.WriteString(column)) {
715 HILOG_ERROR("parameter Query fail to WriteParcelable column");
716 return E_IPCS;
717 }
718 }
719
720 MessageParcel reply;
721 MessageOption option;
722 int err = Remote()->SendRequest(CMD_QUERY, data, reply, option);
723 if (err != ERR_OK) {
724 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
725 return err;
726 }
727
728 err = GetQueryResult(reply, results);
729 if (err != ERR_OK) {
730 HILOG_ERROR("fail to GetQueryResult. err: %{public}d", err);
731 return err;
732 }
733 return ERR_OK;
734 }
735
GetRoots(std::vector<RootInfo> & rootInfoVec)736 int FileAccessExtProxy::GetRoots(std::vector<RootInfo> &rootInfoVec)
737 {
738 UserAccessTracer trace;
739 trace.Start("GetRoots");
740 MessageParcel data;
741 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
742 HILOG_ERROR("WriteInterfaceToken failed");
743 return E_IPCS;
744 }
745
746 MessageParcel reply;
747 MessageOption option;
748 int err = Remote()->SendRequest(CMD_GET_ROOTS, data, reply, option);
749 if (err != ERR_OK) {
750 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
751 return err;
752 }
753
754 int ret = E_IPCS;
755 if (!reply.ReadInt32(ret)) {
756 HILOG_ERROR("fail to ReadInt32 ret");
757 return E_IPCS;
758 }
759
760 if (ret != ERR_OK) {
761 HILOG_ERROR("GetRoots operation failed ret : %{public}d", ret);
762 return ret;
763 }
764
765 uint64_t count = 0;
766 if (!reply.ReadUint64(count)) {
767 HILOG_ERROR("GetRoots operation failed to Read count");
768 return E_IPCS;
769 }
770
771 rootInfoVec.clear();
772 for (uint64_t i = 0; i < count; i++) {
773 std::unique_ptr<RootInfo> rootInfoPtr(reply.ReadParcelable<RootInfo>());
774 if (rootInfoPtr != nullptr) {
775 rootInfoVec.push_back(*rootInfoPtr);
776 }
777 }
778
779 return ERR_OK;
780 }
781
GetFileInfoFromUri(const Uri & selectFile,FileInfo & fileInfo)782 int FileAccessExtProxy::GetFileInfoFromUri(const Uri &selectFile, FileInfo &fileInfo)
783 {
784 UserAccessTracer trace;
785 trace.Start("GetFileInfoFromUri");
786 MessageParcel data;
787 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
788 HILOG_ERROR("WriteInterfaceToken failed");
789 return E_IPCS;
790 }
791
792 if (!data.WriteParcelable(&selectFile)) {
793 HILOG_ERROR("fail to WriteParcelable selectFile");
794 return E_IPCS;
795 }
796
797 MessageParcel reply;
798 MessageOption option;
799 int err = Remote()->SendRequest(CMD_GET_FILEINFO_FROM_URI, data, reply, option);
800 if (err != ERR_OK) {
801 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
802 return err;
803 }
804
805 int ret = E_IPCS;
806 if (!reply.ReadInt32(ret)) {
807 HILOG_ERROR("fail to ReadInt32 ret");
808 return E_IPCS;
809 }
810
811 if (ret != ERR_OK) {
812 HILOG_ERROR("GetFileInfoFromUri operation failed ret : %{public}d", ret);
813 return ret;
814 }
815
816 std::unique_ptr<FileInfo> fileInfoTemp(reply.ReadParcelable<FileInfo>());
817 if (fileInfoTemp == nullptr) {
818 HILOG_ERROR("ReadParcelable value is nullptr.");
819 return E_IPCS;
820 }
821
822 fileInfo = *fileInfoTemp;
823 return ERR_OK;
824 }
825
GetFileInfoFromRelativePath(const std::string & selectFile,FileInfo & fileInfo)826 int FileAccessExtProxy::GetFileInfoFromRelativePath(const std::string &selectFile, FileInfo &fileInfo)
827 {
828 UserAccessTracer trace;
829 trace.Start("GetFileInfoFromRelativePath");
830 MessageParcel data;
831 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
832 HILOG_ERROR("WriteInterfaceToken failed");
833 return E_IPCS;
834 }
835
836 if (!data.WriteString(selectFile)) {
837 HILOG_ERROR("fail to WriteParcelable selectFile");
838 return E_IPCS;
839 }
840
841 MessageParcel reply;
842 MessageOption option;
843 int err = Remote()->SendRequest(CMD_GET_FILEINFO_FROM_RELATIVE_PATH, data, reply, option);
844 if (err != ERR_OK) {
845 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
846 return err;
847 }
848
849 int ret = E_IPCS;
850 if (!reply.ReadInt32(ret)) {
851 HILOG_ERROR("fail to ReadInt32 ret");
852 return E_IPCS;
853 }
854
855 if (ret != ERR_OK) {
856 HILOG_ERROR("GetFileInfoFromRelativePath operation failed ret : %{public}d", ret);
857 return ret;
858 }
859
860 std::unique_ptr<FileInfo> fileInfoTemp(reply.ReadParcelable<FileInfo>());
861 if (fileInfoTemp == nullptr) {
862 HILOG_ERROR("ReadParcelable value is nullptr.");
863 return E_IPCS;
864 }
865
866 fileInfo = *fileInfoTemp;
867 return ERR_OK;
868 }
869
Access(const Uri & uri,bool & isExist)870 int FileAccessExtProxy::Access(const Uri &uri, bool &isExist)
871 {
872 UserAccessTracer trace;
873 trace.Start("Access");
874 MessageParcel data;
875 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
876 HILOG_ERROR("WriteInterfaceToken failed");
877 return E_IPCS;
878 }
879
880 if (!data.WriteParcelable(&uri)) {
881 HILOG_ERROR("fail to WriteParcelable uri");
882 return E_IPCS;
883 }
884
885 MessageParcel reply;
886 MessageOption option;
887 int err = Remote()->SendRequest(CMD_ACCESS, data, reply, option);
888 if (err != ERR_OK) {
889 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
890 return err;
891 }
892
893 int ret = E_IPCS;
894 if (!reply.ReadInt32(ret)) {
895 HILOG_ERROR("fail to ReadInt32 ret");
896 return E_IPCS;
897 }
898
899 if (ret != ERR_OK) {
900 HILOG_ERROR("Access operation failed ret : %{public}d", ret);
901 return ret;
902 }
903
904 if (!reply.ReadBool(isExist)) {
905 HILOG_ERROR("fail to ReadInt32 isExist");
906 return E_IPCS;
907 }
908
909 return ERR_OK;
910 }
911
StartWatcher(const Uri & uri)912 int FileAccessExtProxy::StartWatcher(const Uri &uri)
913 {
914 UserAccessTracer trace;
915 trace.Start("StartWatcher");
916 MessageParcel data;
917 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
918 HILOG_ERROR("WriteInterfaceToken failed");
919 return E_IPCS;
920 }
921
922 std::string uriString = uri.ToString();
923 if (!data.WriteString(uriString)) {
924 HILOG_ERROR("fail to WriteParcelable uri");
925 return E_IPCS;
926 }
927
928 MessageParcel reply;
929 MessageOption option;
930 int err = Remote()->SendRequest(CMD_START_WATCHER, data, reply, option);
931 if (err != ERR_OK) {
932 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
933 return err;
934 }
935
936 int ret = E_IPCS;
937 if (!reply.ReadInt32(ret)) {
938 HILOG_ERROR("fail to ReadInt32 ret");
939 return E_IPCS;
940 }
941
942 if (ret != ERR_OK) {
943 HILOG_ERROR("StartWatcher operation failed ret : %{public}d", ret);
944 return ret;
945 }
946
947 return ERR_OK;
948 }
949
StopWatcher(const Uri & uri)950 int FileAccessExtProxy::StopWatcher(const Uri &uri)
951 {
952 UserAccessTracer trace;
953 trace.Start("StopWatcher");
954 MessageParcel data;
955 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
956 HILOG_ERROR("WriteInterfaceToken failed");
957 return E_IPCS;
958 }
959
960 std::string uriString = uri.ToString();
961 if (!data.WriteString(uriString)) {
962 HILOG_ERROR("fail to WriteParcelable uri");
963 return E_IPCS;
964 }
965
966 MessageParcel reply;
967 MessageOption option;
968 int err = Remote()->SendRequest(CMD_STOP_WATCHER, data, reply, option);
969 if (err != ERR_OK) {
970 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
971 return err;
972 }
973
974 int ret = E_IPCS;
975 if (!reply.ReadInt32(ret)) {
976 HILOG_ERROR("fail to ReadInt32 ret");
977 return E_IPCS;
978 }
979
980 if (ret != ERR_OK) {
981 HILOG_ERROR("StopWatcher operation failed ret : %{public}d", ret);
982 return ret;
983 }
984
985 return ERR_OK;
986 }
987
ReadMoveItemFuncResults(OHOS::MessageParcel & reply,std::vector<Result> & moveResult)988 static int ReadMoveItemFuncResults(OHOS::MessageParcel &reply, std::vector<Result> &moveResult)
989 {
990 UserAccessTracer trace;
991 trace.Start("ReadMoveItemFuncResults");
992 int ret = E_IPCS;
993 if (!reply.ReadInt32(ret)) {
994 HILOG_ERROR("fail to ReadInt32 ret");
995 return E_IPCS;
996 }
997 if (ret == ERR_OK) {
998 HILOG_ERROR("Move operation success");
999 return ret;
1000 }
1001
1002 uint32_t count = 0;
1003 if (!reply.ReadUint32(count)) {
1004 HILOG_ERROR("Move operation failed to Read count");
1005 return E_IPCS;
1006 }
1007 if (count > MAX_COPY_ERROR_COUNT) {
1008 HILOG_ERROR("Move operation failed, count value greater than max count");
1009 Result result { "", "", E_COUNT, "Count value greater than max count"};
1010 moveResult.clear();
1011 moveResult.push_back(result);
1012 return COPY_EXCEPTION;
1013 }
1014
1015 moveResult.clear();
1016 for (uint32_t i = 0; i < count; i++) {
1017 std::unique_ptr<Result> moveResultPtr(reply.ReadParcelable<Result>());
1018 if (moveResultPtr != nullptr) {
1019 moveResult.push_back(*moveResultPtr);
1020 }
1021 }
1022 return ret;
1023 }
1024
MoveItem(const Uri & sourceFile,const Uri & targetParent,std::vector<Result> & moveResult,bool force)1025 int FileAccessExtProxy::MoveItem(const Uri &sourceFile, const Uri &targetParent, std::vector<Result> &moveResult,
1026 bool force)
1027 {
1028 UserAccessTracer trace;
1029 trace.Start("MoveItem");
1030 MessageParcel data;
1031 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
1032 HILOG_ERROR("WriteInterfaceToken failed");
1033 return E_IPCS;
1034 }
1035
1036 std::string insideInputSourceUri = sourceFile.ToString();
1037 if (!data.WriteString(insideInputSourceUri)) {
1038 HILOG_ERROR("fail to WriteParcelable insideInputSourceUri");
1039 return E_IPCS;
1040 }
1041
1042 std::string insideInputTargetUri = targetParent.ToString();
1043 if (!data.WriteString(insideInputTargetUri)) {
1044 HILOG_ERROR("fail to WriteParcelable insideInputTargetUri");
1045 return E_IPCS;
1046 }
1047
1048 if (!data.WriteBool(force)) {
1049 HILOG_ERROR("fail to WriteBool force");
1050 return E_IPCS;
1051 }
1052
1053 MessageParcel reply;
1054 MessageOption option;
1055 int err = Remote()->SendRequest(CMD_MOVE_ITEM, data, reply, option);
1056 if (err != ERR_OK) {
1057 HILOG_ERROR("fail to SendRequest, err: %{public}d", err);
1058 return err;
1059 }
1060
1061 auto ret = ReadMoveItemFuncResults(reply, moveResult);
1062 if (ret != ERR_OK) {
1063 HILOG_ERROR("Read moveItem function result error, code: %{public}d", ret);
1064 return ret;
1065 }
1066
1067 return ret;
1068 }
1069
MoveFile(const Uri & sourceFile,const Uri & targetParent,std::string & fileName,Uri & newFile)1070 int FileAccessExtProxy::MoveFile(const Uri &sourceFile, const Uri &targetParent, std::string &fileName, Uri &newFile)
1071 {
1072 UserAccessTracer trace;
1073 trace.Start("MoveFile");
1074 MessageParcel data;
1075 if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
1076 HILOG_ERROR("WriteInterfaceToken failed");
1077 return E_IPCS;
1078 }
1079
1080 std::string insideInputSourceUri = sourceFile.ToString();
1081 if (!data.WriteString(insideInputSourceUri)) {
1082 HILOG_ERROR("fail to WriteParcelable sourceFile");
1083 return E_IPCS;
1084 }
1085
1086 std::string insideInputTargetUri = targetParent.ToString();
1087 if (!data.WriteString(insideInputTargetUri)) {
1088 HILOG_ERROR("fail to WriteParcelable targetParent");
1089 return E_IPCS;
1090 }
1091
1092 if (!data.WriteString(fileName)) {
1093 HILOG_ERROR("fail to WriteParcelable fileName");
1094 return E_IPCS;
1095 }
1096
1097 MessageParcel reply;
1098 MessageOption option;
1099 int err = Remote()->SendRequest(CMD_MOVE_FILE, data, reply, option);
1100 if (err != ERR_OK) {
1101 HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
1102 return err;
1103 }
1104
1105 int ret = E_IPCS;
1106 if (!reply.ReadInt32(ret)) {
1107 HILOG_ERROR("fail to ReadInt32 ret");
1108 return E_IPCS;
1109 }
1110
1111 if (ret != ERR_OK) {
1112 HILOG_ERROR("Move file operation failed ret : %{public}d", ret);
1113 return ret;
1114 }
1115
1116 std::string tempUri;
1117 if (!reply.ReadString(tempUri)) {
1118 HILOG_ERROR("ReadParcelable value is nullptr.");
1119 return E_IPCS;
1120 };
1121
1122 if (tempUri.empty()) {
1123 HILOG_ERROR("get uri is empty.");
1124 return E_GETRESULT;
1125 }
1126 newFile = Uri(tempUri);
1127
1128 return ERR_OK;
1129 }
1130 } // namespace FileAccessFwk
1131 } // namespace OHOS
1132