• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     std::string insideInputUri = uri.ToString();
44     if (!data.WriteString(insideInputUri)) {
45         HILOG_ERROR("fail to WriteParcelable uri");
46         return E_IPCS;
47     }
48 
49     if (!data.WriteInt32(flags)) {
50         HILOG_ERROR("fail to WriteInt32 flags");
51         return E_IPCS;
52     }
53 
54     MessageParcel reply;
55     MessageOption option;
56     int err = Remote()->SendRequest(CMD_OPEN_FILE, data, reply, option);
57     if (err != ERR_OK) {
58         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
59         return err;
60     }
61 
62     int ret = E_IPCS;
63     if (!reply.ReadInt32(ret)) {
64         HILOG_ERROR("fail to ReadInt32 ret");
65         return E_IPCS;
66     }
67 
68     if (ret != ERR_OK) {
69         HILOG_ERROR("OpenFile operation failed ret : %{public}d", ret);
70         return ret;
71     }
72 
73     fd = reply.ReadFileDescriptor();
74     if (fd < ERR_OK) {
75         HILOG_ERROR("fail to ReadFileDescriptor fd: %{public}d", fd);
76         return E_GETRESULT;
77     }
78 
79     return ERR_OK;
80 }
81 
CreateFile(const Uri & parent,const std::string & displayName,Uri & newFile)82 int FileAccessExtProxy::CreateFile(const Uri &parent, const std::string &displayName, Uri &newFile)
83 {
84     UserAccessTracer trace;
85     trace.Start("CreateFile");
86     MessageParcel data;
87     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
88         HILOG_ERROR("WriteInterfaceToken failed");
89         return E_IPCS;
90     }
91 
92     std::string insideInputUri = parent.ToString();
93     if (!data.WriteString(insideInputUri)) {
94         HILOG_ERROR("fail to WriteParcelable parent");
95         return E_IPCS;
96     }
97 
98     if (!data.WriteString(displayName)) {
99         HILOG_ERROR("fail to WriteString displayName");
100         return E_IPCS;
101     }
102 
103     MessageParcel reply;
104     MessageOption option;
105     int err = Remote()->SendRequest(CMD_CREATE_FILE, data, reply, option);
106     if (err != ERR_OK) {
107         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
108         return err;
109     }
110 
111     int ret = E_IPCS;
112     if (!reply.ReadInt32(ret)) {
113         HILOG_ERROR("fail to ReadInt32 ret");
114         return E_IPCS;
115     }
116 
117     if (ret != ERR_OK) {
118         HILOG_ERROR("CreateFile operation failed ret : %{public}d", ret);
119         return ret;
120     }
121 
122     std::string tempUri;
123     if (!reply.ReadString(tempUri)) {
124         HILOG_ERROR("ReadParcelable value is nullptr.");
125         return E_IPCS;
126     }
127 
128     if (tempUri.empty()) {
129         HILOG_ERROR("get uri is empty.");
130         return E_GETRESULT;
131     }
132     newFile = Uri(tempUri);
133 
134     return ERR_OK;
135 }
136 
Mkdir(const Uri & parent,const std::string & displayName,Uri & newFile)137 int FileAccessExtProxy::Mkdir(const Uri &parent, const std::string &displayName, Uri &newFile)
138 {
139     UserAccessTracer trace;
140     trace.Start("Mkdir");
141     MessageParcel data;
142     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
143         HILOG_ERROR("WriteInterfaceToken failed");
144         return E_IPCS;
145     }
146 
147     std::string insideInputUri = parent.ToString();
148     if (!data.WriteString(insideInputUri)) {
149         HILOG_ERROR("fail to WriteParcelable parent");
150         return E_IPCS;
151     }
152 
153     if (!data.WriteString(displayName)) {
154         HILOG_ERROR("fail to WriteString displayName");
155         return E_IPCS;
156     }
157 
158     MessageParcel reply;
159     MessageOption option;
160     int err = Remote()->SendRequest(CMD_MKDIR, data, reply, option);
161     if (err != ERR_OK) {
162         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
163         return err;
164     }
165 
166     int ret = E_IPCS;
167     if (!reply.ReadInt32(ret)) {
168         HILOG_ERROR("fail to ReadInt32 ret");
169         return E_IPCS;
170     }
171 
172     if (ret != ERR_OK) {
173         HILOG_ERROR("Mkdir operation failed ret : %{public}d", ret);
174         return ret;
175     }
176 
177     std::string tempUri;
178     if (!reply.ReadString(tempUri)) {
179         HILOG_ERROR("ReadParcelable value is nullptr.");
180         return E_IPCS;
181     }
182 
183     if (tempUri.empty()) {
184         HILOG_ERROR("get uri is empty.");
185         return E_GETRESULT;
186     }
187     newFile = Uri(tempUri);
188 
189     return ERR_OK;
190 }
191 
Delete(const Uri & sourceFile)192 int FileAccessExtProxy::Delete(const Uri &sourceFile)
193 {
194     UserAccessTracer trace;
195     trace.Start("Delete");
196     MessageParcel data;
197     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
198         HILOG_ERROR("WriteInterfaceToken failed");
199         return E_IPCS;
200     }
201 
202     std::string insideInputUri = sourceFile.ToString();
203     if (!data.WriteString(insideInputUri)) {
204         HILOG_ERROR("fail to WriteParcelable sourceFile");
205         return E_IPCS;
206     }
207 
208     MessageParcel reply;
209     MessageOption option;
210     int err = Remote()->SendRequest(CMD_DELETE, data, reply, option);
211     if (err != ERR_OK) {
212         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
213         return err;
214     }
215 
216     int ret = E_IPCS;
217     if (!reply.ReadInt32(ret)) {
218         HILOG_ERROR("fail to ReadInt32 ret");
219         return E_IPCS;
220     }
221 
222     if (ret != ERR_OK) {
223         HILOG_ERROR("Delete operation failed ret : %{public}d", ret);
224         return ret;
225     }
226 
227     return ERR_OK;
228 }
229 
Move(const Uri & sourceFile,const Uri & targetParent,Uri & newFile)230 int FileAccessExtProxy::Move(const Uri &sourceFile, const Uri &targetParent, Uri &newFile)
231 {
232     UserAccessTracer trace;
233     trace.Start("Move");
234     MessageParcel data;
235     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
236         HILOG_ERROR("WriteInterfaceToken failed");
237         return E_IPCS;
238     }
239 
240     std::string insideInputSourceUri = sourceFile.ToString();
241     if (!data.WriteString(insideInputSourceUri)) {
242         HILOG_ERROR("fail to WriteParcelable sourceFile");
243         return E_IPCS;
244     }
245 
246     std::string insideInputTargetUri = targetParent.ToString();
247     if (!data.WriteString(insideInputTargetUri)) {
248         HILOG_ERROR("fail to WriteParcelable targetParent");
249         return E_IPCS;
250     }
251 
252     MessageParcel reply;
253     MessageOption option;
254     int err = Remote()->SendRequest(CMD_MOVE, data, reply, option);
255     if (err != ERR_OK) {
256         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
257         return err;
258     }
259 
260     int ret = E_IPCS;
261     if (!reply.ReadInt32(ret)) {
262         HILOG_ERROR("fail to ReadInt32 ret");
263         return E_IPCS;
264     }
265 
266     if (ret != ERR_OK) {
267         HILOG_ERROR("Move operation failed ret : %{public}d", ret);
268         return ret;
269     }
270 
271     std::string tempUri;
272     if (!reply.ReadString(tempUri)) {
273         HILOG_ERROR("ReadParcelable value is nullptr.");
274         return E_IPCS;
275     };
276 
277     if (tempUri.empty()) {
278         HILOG_ERROR("get uri is empty.");
279         return E_GETRESULT;
280     }
281     newFile = Uri(tempUri);
282 
283     return ERR_OK;
284 }
285 
WriteCopyFuncArguments(OHOS::MessageParcel & data,const Uri & sourceUri,const Uri & destUri,bool force)286 static int WriteCopyFuncArguments(OHOS::MessageParcel &data, const Uri &sourceUri, const Uri &destUri, bool force)
287 {
288     UserAccessTracer trace;
289     trace.Start("WriteCopyFuncArguments");
290     std::string insideInputSourceUri = sourceUri.ToString();
291     if (!data.WriteString(insideInputSourceUri)) {
292         HILOG_ERROR("fail to WriteParcelable insideInputSourceUri");
293         return E_IPCS;
294     }
295 
296     std::string insideInputTargetUri = destUri.ToString();
297     if (!data.WriteString(insideInputTargetUri)) {
298         HILOG_ERROR("fail to WriteParcelable insideInputTargetUri");
299         return E_IPCS;
300     }
301 
302     if (!data.WriteBool(force)) {
303         HILOG_ERROR("fail to WriteBool force");
304         return E_IPCS;
305     }
306     return ERR_OK;
307 }
308 
ReadCopyFuncResults(OHOS::MessageParcel & reply,std::vector<CopyResult> & copyResult)309 static int ReadCopyFuncResults(OHOS::MessageParcel &reply, std::vector<CopyResult> &copyResult)
310 {
311     UserAccessTracer trace;
312     trace.Start("ReadCopyFuncResults");
313     int ret = E_IPCS;
314     if (!reply.ReadInt32(ret)) {
315         HILOG_ERROR("fail to ReadInt32 ret");
316         return E_IPCS;
317     }
318     if (ret == ERR_OK) {
319         HILOG_ERROR("Copy operation success");
320         return ret;
321     }
322 
323     uint32_t count = 0;
324     if (!reply.ReadUint32(count)) {
325         HILOG_ERROR("Copy operation failed to Read count");
326         return E_IPCS;
327     }
328     if (count > MAX_COPY_ERROR_COUNT) {
329         HILOG_ERROR("Copy operation failed, count value greater than max count");
330         CopyResult result { "", "", E_COUNT, "Count value greater than max count"};
331         copyResult.clear();
332         copyResult.push_back(result);
333         return COPY_EXCEPTION;
334     }
335 
336     copyResult.clear();
337     for (uint32_t i = 0; i < count; i++) {
338         std::unique_ptr<CopyResult> copyResultPtr(reply.ReadParcelable<CopyResult>());
339         if (copyResultPtr != nullptr) {
340             copyResult.push_back(*copyResultPtr);
341         }
342     }
343     return ret;
344 }
345 
Copy(const Uri & sourceUri,const Uri & destUri,std::vector<CopyResult> & copyResult,bool force)346 int FileAccessExtProxy::Copy(const Uri &sourceUri, const Uri &destUri, std::vector<CopyResult> &copyResult,
347     bool force)
348 {
349     UserAccessTracer trace;
350     trace.Start("Copy");
351     MessageParcel data;
352     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
353         HILOG_ERROR("WriteInterfaceToken failed");
354         return E_IPCS;
355     }
356 
357     int ret = WriteCopyFuncArguments(data, sourceUri, destUri, force);
358     if (ret != ERR_OK) {
359         HILOG_ERROR("Write copy function arguments error, code: %{public}d", ret);
360         return ret;
361     }
362 
363     MessageParcel reply;
364     MessageOption option;
365     int err = Remote()->SendRequest(CMD_COPY, data, reply, option);
366     if (err != ERR_OK) {
367         HILOG_ERROR("fail to SendRequest, err: %{public}d", err);
368         return err;
369     }
370 
371     ret = ReadCopyFuncResults(reply, copyResult);
372     if (ret != ERR_OK) {
373         HILOG_ERROR("Read copy function result error, code: %{public}d", ret);
374         return ret;
375     }
376 
377     return ret;
378 }
379 
Rename(const Uri & sourceFile,const std::string & displayName,Uri & newFile)380 int FileAccessExtProxy::Rename(const Uri &sourceFile, const std::string &displayName, Uri &newFile)
381 {
382     UserAccessTracer trace;
383     trace.Start("Rename");
384     MessageParcel data;
385     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
386         HILOG_ERROR("WriteInterfaceToken failed");
387         return E_IPCS;
388     }
389 
390     std::string insideInputSourceUri = sourceFile.ToString();
391     if (!data.WriteString(insideInputSourceUri)) {
392         HILOG_ERROR("fail to WriteParcelable sourceFile");
393         return E_IPCS;
394     }
395 
396     if (!data.WriteString(displayName)) {
397         HILOG_ERROR("fail to WriteString displayName");
398         return E_IPCS;
399     }
400 
401     MessageParcel reply;
402     MessageOption option;
403     int err = Remote()->SendRequest(CMD_RENAME, data, reply, option);
404     if (err != ERR_OK) {
405         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
406         return err;
407     }
408 
409     int ret = E_IPCS;
410     if (!reply.ReadInt32(ret)) {
411         HILOG_ERROR("fail to ReadInt32 ret");
412         return E_IPCS;
413     }
414 
415     if (ret != ERR_OK) {
416         HILOG_ERROR("Rename operation failed ret : %{public}d", ret);
417         return ret;
418     }
419 
420     std::string tempUri;
421     if (!reply.ReadString(tempUri)) {
422         HILOG_ERROR("ReadParcelable value is nullptr.");
423         return E_IPCS;
424     }
425 
426     if (tempUri.empty()) {
427         HILOG_ERROR("get uri is empty.");
428         return E_GETRESULT;
429     }
430     newFile = Uri(tempUri);
431 
432     return ERR_OK;
433 }
434 
GetListFileResult(MessageParcel & reply,std::vector<FileInfo> & fileInfoVec)435 static int GetListFileResult(MessageParcel &reply, std::vector<FileInfo> &fileInfoVec)
436 {
437     int ret = E_IPCS;
438     if (!reply.ReadInt32(ret)) {
439         HILOG_ERROR("fail to ReadInt32 ret");
440         return E_IPCS;
441     }
442 
443     if (ret != ERR_OK) {
444         HILOG_ERROR("ListFile operation failed ret : %{public}d", ret);
445         return ret;
446     }
447 
448     int64_t count = 0;
449     if (!reply.ReadInt64(count)) {
450         HILOG_ERROR("ListFile operation failed to Read count");
451         return E_IPCS;
452     }
453 
454     fileInfoVec.clear();
455     for (int64_t i = 0; i < count; i++) {
456         std::unique_ptr<FileInfo> fileInfoPtr(reply.ReadParcelable<FileInfo>());
457         if (fileInfoPtr != nullptr) {
458             fileInfoVec.push_back(*fileInfoPtr);
459         }
460     }
461     return ERR_OK;
462 }
463 
ListFile(const FileInfo & fileInfo,const int64_t offset,const int64_t maxCount,const FileFilter & filter,std::vector<FileInfo> & fileInfoVec)464 int FileAccessExtProxy::ListFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount,
465     const FileFilter &filter, std::vector<FileInfo> &fileInfoVec)
466 {
467     UserAccessTracer trace;
468     trace.Start("ListFile");
469     MessageParcel data;
470     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
471         HILOG_ERROR("WriteInterfaceToken failed");
472         return E_IPCS;
473     }
474 
475     if (!data.WriteParcelable(&fileInfo)) {
476         HILOG_ERROR("fail to WriteParcelable fileInfo");
477         return E_IPCS;
478     }
479 
480     if (!data.WriteInt64(offset)) {
481         HILOG_ERROR("fail to WriteInt64 offset");
482         return E_IPCS;
483     }
484 
485     if (!data.WriteInt64(maxCount)) {
486         HILOG_ERROR("fail to WriteInt64 maxCount");
487         return E_IPCS;
488     }
489 
490     if (!data.WriteParcelable(&filter)) {
491         HILOG_ERROR("fail to WriteParcelable filter");
492         return E_IPCS;
493     }
494 
495     MessageParcel reply;
496     MessageOption option;
497     int err = Remote()->SendRequest(CMD_LIST_FILE, data, reply, option);
498     if (err != ERR_OK) {
499         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
500         return err;
501     }
502 
503     err = GetListFileResult(reply, fileInfoVec);
504     if (err != ERR_OK) {
505         HILOG_ERROR("fail to GetListFileResult. err: %{public}d", err);
506         return err;
507     }
508 
509     return ERR_OK;
510 }
511 
ScanFile(const FileInfo & fileInfo,const int64_t offset,const int64_t maxCount,const FileFilter & filter,std::vector<FileInfo> & fileInfoVec)512 int FileAccessExtProxy::ScanFile(const FileInfo &fileInfo, const int64_t offset, const int64_t maxCount,
513     const FileFilter &filter, std::vector<FileInfo> &fileInfoVec)
514 {
515     UserAccessTracer trace;
516     trace.Start("ScanFile");
517     MessageParcel data;
518     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
519         HILOG_ERROR("WriteInterfaceToken failed");
520         return E_IPCS;
521     }
522 
523     if (!data.WriteParcelable(&fileInfo)) {
524         HILOG_ERROR("fail to WriteParcelable fileInfo");
525         return E_IPCS;
526     }
527 
528     if (!data.WriteInt64(offset)) {
529         HILOG_ERROR("fail to WriteInt64 offset");
530         return E_IPCS;
531     }
532 
533     if (!data.WriteInt64(maxCount)) {
534         HILOG_ERROR("fail to WriteInt64 maxCount");
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     MessageParcel reply;
544     MessageOption option;
545     int err = Remote()->SendRequest(CMD_SCAN_FILE, data, reply, option);
546     if (err != ERR_OK) {
547         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
548         return err;
549     }
550 
551     err = GetListFileResult(reply, fileInfoVec);
552     if (err != ERR_OK) {
553         HILOG_ERROR("fail to GetListFileResult. err: %{public}d", err);
554         return err;
555     }
556 
557     return ERR_OK;
558 }
559 
GetQueryResult(MessageParcel & reply,std::vector<std::string> & results)560 static int GetQueryResult(MessageParcel &reply, std::vector<std::string> &results)
561 {
562     int ret = E_IPCS;
563     if (!reply.ReadInt32(ret)) {
564         HILOG_ERROR("fail to ReadInt32 ret");
565         return E_IPCS;
566     }
567 
568     if (ret != ERR_OK) {
569         HILOG_ERROR("Query operation failed ret : %{public}d", ret);
570         return ret;
571     }
572 
573     int64_t count = 0;
574     if (!reply.ReadInt64(count)) {
575         HILOG_ERROR("Query operation failed to Read count");
576         return E_IPCS;
577     }
578 
579     results.clear();
580     for (int64_t i = 0; i < count; i++) {
581         results.push_back(reply.ReadString());
582     }
583     return ERR_OK;
584 }
585 
Query(const Uri & uri,std::vector<std::string> & columns,std::vector<std::string> & results)586 int FileAccessExtProxy::Query(const Uri &uri, std::vector<std::string> &columns, std::vector<std::string> &results)
587 {
588     UserAccessTracer trace;
589     trace.Start("Query");
590     MessageParcel data;
591     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
592         HILOG_ERROR("WriteInterfaceToken failed");
593         return E_IPCS;
594     }
595 
596     std::string insideInputUri = uri.ToString();
597     if (!data.WriteString(insideInputUri)) {
598         HILOG_ERROR("fail to WriteParcelable sourceFile");
599         return E_IPCS;
600     }
601     int64_t count = columns.size();
602     if (!data.WriteInt64(count)) {
603         HILOG_ERROR("Parameter Query fail to WriteInt64 count");
604         return E_IPCS;
605     }
606     if (count > FILE_RESULT_TYPE.size()) {
607         HILOG_ERROR(" The number of query operations exceeds %{public}zu ", FILE_RESULT_TYPE.size());
608         return EINVAL;
609     }
610 
611     for (const auto &column : columns) {
612         if (!data.WriteString(column)) {
613             HILOG_ERROR("parameter Query fail to WriteParcelable column");
614             return E_IPCS;
615         }
616     }
617 
618     MessageParcel reply;
619     MessageOption option;
620     int err = Remote()->SendRequest(CMD_QUERY, data, reply, option);
621     if (err != ERR_OK) {
622         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
623         return err;
624     }
625 
626     err = GetQueryResult(reply, results);
627     if (err != ERR_OK) {
628         HILOG_ERROR("fail to GetQueryResult. err: %{public}d", err);
629         return err;
630     }
631     return ERR_OK;
632 }
633 
GetRoots(std::vector<RootInfo> & rootInfoVec)634 int FileAccessExtProxy::GetRoots(std::vector<RootInfo> &rootInfoVec)
635 {
636     UserAccessTracer trace;
637     trace.Start("GetRoots");
638     MessageParcel data;
639     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
640         HILOG_ERROR("WriteInterfaceToken failed");
641         return E_IPCS;
642     }
643 
644     MessageParcel reply;
645     MessageOption option;
646     int err = Remote()->SendRequest(CMD_GET_ROOTS, data, reply, option);
647     if (err != ERR_OK) {
648         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
649         return err;
650     }
651 
652     int ret = E_IPCS;
653     if (!reply.ReadInt32(ret)) {
654         HILOG_ERROR("fail to ReadInt32 ret");
655         return E_IPCS;
656     }
657 
658     if (ret != ERR_OK) {
659         HILOG_ERROR("GetRoots operation failed ret : %{public}d", ret);
660         return ret;
661     }
662 
663     uint64_t count = 0;
664     if (!reply.ReadUint64(count)) {
665         HILOG_ERROR("GetRoots operation failed to Read count");
666         return E_IPCS;
667     }
668 
669     rootInfoVec.clear();
670     for (uint64_t i = 0; i < count; i++) {
671         std::unique_ptr<RootInfo> rootInfoPtr(reply.ReadParcelable<RootInfo>());
672         if (rootInfoPtr != nullptr) {
673             rootInfoVec.push_back(*rootInfoPtr);
674         }
675     }
676 
677     return ERR_OK;
678 }
679 
WriteThumbnailArgs(MessageParcel & data,const Uri & uri,const ThumbnailSize & thumbnailSize)680 static int WriteThumbnailArgs(MessageParcel &data, const Uri &uri, const ThumbnailSize &thumbnailSize)
681 {
682     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
683         HILOG_ERROR("WriteInterfaceToken failed");
684         return E_IPCS;
685     }
686     std::string insideInputUri = uri.ToString();
687     if (!data.WriteString(insideInputUri)) {
688         HILOG_ERROR("fail to WriteParcelable selectfile");
689         return E_IPCS;
690     }
691 
692     if (!data.WriteParcelable(&thumbnailSize)) {
693         HILOG_ERROR("fail to WriteParcelable thumbnailSize");
694         return E_IPCS;
695     }
696     return ERR_OK;
697 }
698 
GetThumbnail(const Uri & uri,const ThumbnailSize & thumbnailSize,std::shared_ptr<PixelMap> & pixelMap)699 int FileAccessExtProxy::GetThumbnail(const Uri &uri, const ThumbnailSize &thumbnailSize,
700     std::shared_ptr<PixelMap> &pixelMap)
701 {
702     UserAccessTracer trace;
703     trace.Start("GetThumbnail");
704     MessageParcel data;
705     int err = WriteThumbnailArgs(data, uri, thumbnailSize);
706     if (err != ERR_OK) {
707         HILOG_ERROR("fail to WriteThumbnailArgs. err: %{public}d", err);
708         return err;
709     }
710 
711     MessageParcel reply;
712     MessageOption option;
713     err = Remote()->SendRequest(CMD_GET_THUMBNAIL, data, reply, option);
714     if (err != ERR_OK) {
715         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
716         return err;
717     }
718 
719     int ret = E_IPCS;
720     if (!reply.ReadInt32(ret)) {
721         HILOG_ERROR("Parameter GetThumbnail fail to WriteInt32 ret");
722         return E_IPCS;
723     }
724 
725     if (ret != ERR_OK) {
726         HILOG_ERROR("GetThumbnail operation failed ret : %{public}d", ret);
727         return ret;
728     }
729 
730     std::shared_ptr<PixelMap> tempPixelMap(reply.ReadParcelable<PixelMap>());
731     if (tempPixelMap == nullptr) {
732         HILOG_ERROR("ReadParcelable value is nullptr.");
733         return E_IPCS;
734     }
735     pixelMap = tempPixelMap;
736 
737     return ERR_OK;
738 }
739 
GetFileInfoFromUri(const Uri & selectFile,FileInfo & fileInfo)740 int FileAccessExtProxy::GetFileInfoFromUri(const Uri &selectFile, FileInfo &fileInfo)
741 {
742     UserAccessTracer trace;
743     trace.Start("GetFileInfoFromUri");
744     MessageParcel data;
745     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
746         HILOG_ERROR("WriteInterfaceToken failed");
747         return E_IPCS;
748     }
749 
750     std::string insideInputUri = selectFile.ToString();
751     if (!data.WriteString(insideInputUri)) {
752         HILOG_ERROR("fail to WriteParcelable selectFile");
753         return E_IPCS;
754     }
755 
756     MessageParcel reply;
757     MessageOption option;
758     int err = Remote()->SendRequest(CMD_GET_FILEINFO_FROM_URI, data, reply, option);
759     if (err != ERR_OK) {
760         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
761         return err;
762     }
763 
764     int ret = E_IPCS;
765     if (!reply.ReadInt32(ret)) {
766         HILOG_ERROR("fail to ReadInt32 ret");
767         return E_IPCS;
768     }
769 
770     if (ret != ERR_OK) {
771         HILOG_ERROR("GetFileInfoFromUri operation failed ret : %{public}d", ret);
772         return ret;
773     }
774 
775     std::unique_ptr<FileInfo> fileInfoTemp(reply.ReadParcelable<FileInfo>());
776     if (fileInfoTemp == nullptr) {
777         HILOG_ERROR("ReadParcelable value is nullptr.");
778         return E_IPCS;
779     }
780 
781     fileInfo = *fileInfoTemp;
782     return ERR_OK;
783 }
784 
GetFileInfoFromRelativePath(const std::string & selectFile,FileInfo & fileInfo)785 int FileAccessExtProxy::GetFileInfoFromRelativePath(const std::string &selectFile, FileInfo &fileInfo)
786 {
787     UserAccessTracer trace;
788     trace.Start("GetFileInfoFromRelativePath");
789     MessageParcel data;
790     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
791         HILOG_ERROR("WriteInterfaceToken failed");
792         return E_IPCS;
793     }
794 
795     if (!data.WriteString(selectFile)) {
796         HILOG_ERROR("fail to WriteParcelable selectFile");
797         return E_IPCS;
798     }
799 
800     MessageParcel reply;
801     MessageOption option;
802     int err = Remote()->SendRequest(CMD_GET_FILEINFO_FROM_RELATIVE_PATH, data, reply, option);
803     if (err != ERR_OK) {
804         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
805         return err;
806     }
807 
808     int ret = E_IPCS;
809     if (!reply.ReadInt32(ret)) {
810         HILOG_ERROR("fail to ReadInt32 ret");
811         return E_IPCS;
812     }
813 
814     if (ret != ERR_OK) {
815         HILOG_ERROR("GetFileInfoFromRelativePath operation failed ret : %{public}d", ret);
816         return ret;
817     }
818 
819     std::unique_ptr<FileInfo> fileInfoTemp(reply.ReadParcelable<FileInfo>());
820     if (fileInfoTemp == nullptr) {
821         HILOG_ERROR("ReadParcelable value is nullptr.");
822         return E_IPCS;
823     }
824 
825     fileInfo = *fileInfoTemp;
826     return ERR_OK;
827 }
828 
Access(const Uri & uri,bool & isExist)829 int FileAccessExtProxy::Access(const Uri &uri, bool &isExist)
830 {
831     UserAccessTracer trace;
832     trace.Start("Access");
833     MessageParcel data;
834     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
835         HILOG_ERROR("WriteInterfaceToken failed");
836         return E_IPCS;
837     }
838 
839     std::string insideInputUri = uri.ToString();
840     if (!data.WriteString(insideInputUri)) {
841         HILOG_ERROR("fail to WriteParcelable uri");
842         return E_IPCS;
843     }
844 
845     MessageParcel reply;
846     MessageOption option;
847     int err = Remote()->SendRequest(CMD_ACCESS, data, reply, option);
848     if (err != ERR_OK) {
849         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
850         return err;
851     }
852 
853     int ret = E_IPCS;
854     if (!reply.ReadInt32(ret)) {
855         HILOG_ERROR("fail to ReadInt32 ret");
856         return E_IPCS;
857     }
858 
859     if (ret != ERR_OK) {
860         HILOG_ERROR("Access operation failed ret : %{public}d", ret);
861         return ret;
862     }
863 
864     if (!reply.ReadBool(isExist)) {
865         HILOG_ERROR("fail to ReadInt32 isExist");
866         return E_IPCS;
867     }
868 
869     return ERR_OK;
870 }
871 
StartWatcher(const Uri & uri)872 int FileAccessExtProxy::StartWatcher(const Uri &uri)
873 {
874     UserAccessTracer trace;
875     trace.Start("StartWatcher");
876     MessageParcel data;
877     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
878         HILOG_ERROR("WriteInterfaceToken failed");
879         return E_IPCS;
880     }
881 
882     std::string uriString = uri.ToString();
883     if (!data.WriteString(uriString)) {
884         HILOG_ERROR("fail to WriteParcelable uri");
885         return E_IPCS;
886     }
887 
888     MessageParcel reply;
889     MessageOption option;
890     int err = Remote()->SendRequest(CMD_START_WATCHER, data, reply, option);
891     if (err != ERR_OK) {
892         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
893         return err;
894     }
895 
896     int ret = E_IPCS;
897     if (!reply.ReadInt32(ret)) {
898         HILOG_ERROR("fail to ReadInt32 ret");
899         return E_IPCS;
900     }
901 
902     if (ret != ERR_OK) {
903         HILOG_ERROR("StartWatcher operation failed ret : %{public}d", ret);
904         return ret;
905     }
906 
907     return ERR_OK;
908 }
909 
StopWatcher(const Uri & uri,bool isUnregisterAll)910 int FileAccessExtProxy::StopWatcher(const Uri &uri, bool isUnregisterAll)
911 {
912     UserAccessTracer trace;
913     trace.Start("StopWatcher");
914     MessageParcel data;
915     if (!data.WriteInterfaceToken(FileAccessExtProxy::GetDescriptor())) {
916         HILOG_ERROR("WriteInterfaceToken failed");
917         return E_IPCS;
918     }
919 
920     std::string uriString = uri.ToString();
921     if (!data.WriteString(uriString)) {
922         HILOG_ERROR("fail to WriteParcelable uri");
923         return E_IPCS;
924     }
925 
926     if (!data.WriteBool(isUnregisterAll)) {
927         HILOG_ERROR("fail to WriteBool isUnregisterAll");
928         return E_IPCS;
929     }
930 
931     MessageParcel reply;
932     MessageOption option;
933     int err = Remote()->SendRequest(CMD_STOP_WATCHER, data, reply, option);
934     if (err != ERR_OK) {
935         HILOG_ERROR("fail to SendRequest. err: %{public}d", err);
936         return err;
937     }
938 
939     int ret = E_IPCS;
940     if (!reply.ReadInt32(ret)) {
941         HILOG_ERROR("fail to ReadInt32 ret");
942         return E_IPCS;
943     }
944 
945     if (ret != ERR_OK) {
946         HILOG_ERROR("StopWatcher operation failed ret : %{public}d", ret);
947         return ret;
948     }
949 
950     return ERR_OK;
951 }
952 } // namespace FileAccessFwk
953 } // namespace OHOS
954