• 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_stub.h"
17 
18 #include <cstdint>
19 #include <map>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 #include "user_access_tracer.h"
25 #include "access_token.h"
26 #include "accesstoken_kit.h"
27 #include "file_access_extension_info.h"
28 #include "file_access_framework_errno.h"
29 #include "image_source.h"
30 #include "hilog_wrapper.h"
31 #include "hitrace_meter.h"
32 #include "ipc_object_stub.h"
33 #include "ipc_skeleton.h"
34 #include "ipc_types.h"
35 #include "unique_fd.h"
36 #include "uri.h"
37 
38 namespace OHOS {
39 namespace FileAccessFwk {
40 using namespace Media;
41 namespace {
42     const std::string FILE_ACCESS_PERMISSION = "ohos.permission.FILE_ACCESS_MANAGER";
43 }
FileAccessExtStub()44 FileAccessExtStub::FileAccessExtStub()
45 {
46     stubFuncMap_[CMD_OPEN_FILE] = &FileAccessExtStub::CmdOpenFile;
47     stubFuncMap_[CMD_CREATE_FILE] = &FileAccessExtStub::CmdCreateFile;
48     stubFuncMap_[CMD_START_WATCHER] = &FileAccessExtStub::CmdStartWatcher;
49     stubFuncMap_[CMD_STOP_WATCHER] = &FileAccessExtStub::CmdStopWatcher;
50     stubFuncMap_[CMD_MKDIR] = &FileAccessExtStub::CmdMkdir;
51     stubFuncMap_[CMD_DELETE] = &FileAccessExtStub::CmdDelete;
52     stubFuncMap_[CMD_MOVE] = &FileAccessExtStub::CmdMove;
53     stubFuncMap_[CMD_COPY] = &FileAccessExtStub::CmdCopy;
54     stubFuncMap_[CMD_RENAME] = &FileAccessExtStub::CmdRename;
55     stubFuncMap_[CMD_LIST_FILE] = &FileAccessExtStub::CmdListFile;
56     stubFuncMap_[CMD_SCAN_FILE] = &FileAccessExtStub::CmdScanFile;
57     stubFuncMap_[CMD_QUERY] = &FileAccessExtStub::CmdQuery;
58     stubFuncMap_[CMD_GET_ROOTS] = &FileAccessExtStub::CmdGetRoots;
59     stubFuncMap_[CMD_ACCESS] = &FileAccessExtStub::CmdAccess;
60     stubFuncMap_[CMD_GET_THUMBNAIL] = &FileAccessExtStub::CmdGetThumbnail;
61     stubFuncMap_[CMD_GET_FILEINFO_FROM_URI] = &FileAccessExtStub::CmdGetFileInfoFromUri;
62     stubFuncMap_[CMD_GET_FILEINFO_FROM_RELATIVE_PATH] = &FileAccessExtStub::CmdGetFileInfoFromRelativePath;
63 }
64 
~FileAccessExtStub()65 FileAccessExtStub::~FileAccessExtStub()
66 {
67     stubFuncMap_.clear();
68 }
69 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)70 int FileAccessExtStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
71     MessageOption& option)
72 {
73     UserAccessTracer trace;
74     trace.Start("OnRemoteRequest");
75     std::u16string descriptor = FileAccessExtStub::GetDescriptor();
76     std::u16string remoteDescriptor = data.ReadInterfaceToken();
77     if (descriptor != remoteDescriptor) {
78         return ERR_INVALID_STATE;
79     }
80 
81     const auto &itFunc = stubFuncMap_.find(code);
82     if (itFunc != stubFuncMap_.end()) {
83         return (this->*(itFunc->second))(data, reply);
84     }
85 
86     if (!CheckCallingPermission(FILE_ACCESS_PERMISSION)) {
87         HILOG_ERROR("permission error");
88         return E_PERMISSION;
89     }
90     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
91 }
92 
CmdOpenFile(MessageParcel & data,MessageParcel & reply)93 ErrCode FileAccessExtStub::CmdOpenFile(MessageParcel &data, MessageParcel &reply)
94 {
95     UserAccessTracer trace;
96     trace.Start("CmdOpenFile");
97     std::string insideInputUri;
98     if (!data.ReadString(insideInputUri)) {
99         HILOG_ERROR("Parameter OpenFile fail to ReadParcelable uri");
100         return E_IPCS;
101     }
102 
103     if (insideInputUri.empty()) {
104         HILOG_ERROR("Parameter OpenFile insideInputUri is empty");
105         return EINVAL;
106     }
107 
108     int flags = E_IPCS;
109     if (!data.ReadInt32(flags)) {
110         HILOG_ERROR("Parameter OpenFile fail to ReadInt32 flags");
111         return E_IPCS;
112     }
113 
114     if (flags < 0) {
115         HILOG_ERROR("Parameter OpenFile flags is invalid");
116         return EINVAL;
117     }
118 
119     int fd = -1;
120     Uri uri(insideInputUri);
121     int ret = OpenFile(uri, flags, fd);
122     UniqueFd uniqueFd(fd);
123     if (!reply.WriteInt32(ret)) {
124         HILOG_ERROR("Parameter OpenFile fail to WriteInt32 ret");
125         return E_IPCS;
126     }
127 
128     if (!reply.WriteFileDescriptor(fd)) {
129         HILOG_ERROR("Parameter OpenFile fail to WriteFileDescriptor fd");
130         return E_IPCS;
131     }
132 
133     return ERR_OK;
134 }
135 
CmdCreateFile(MessageParcel & data,MessageParcel & reply)136 ErrCode FileAccessExtStub::CmdCreateFile(MessageParcel &data, MessageParcel &reply)
137 {
138     UserAccessTracer trace;
139     trace.Start("CmdCreateFile");
140     std::string insideInputUri;
141     if (!data.ReadString(insideInputUri)) {
142         HILOG_ERROR("Parameter OpenFile fail to ReadParcelable uri");
143         return E_IPCS;
144     }
145 
146     if (insideInputUri.empty()) {
147         HILOG_ERROR("Parameter CreateFile insideInputUri is empty");
148         return EINVAL;
149     }
150 
151     std::string displayName = "";
152     if (!data.ReadString(displayName)) {
153         HILOG_ERROR("Parameter CreateFile fail to ReadString displayName");
154         return E_IPCS;
155     }
156 
157     if (displayName.empty()) {
158         HILOG_ERROR("Parameter CreateFile displayName is empty");
159         return EINVAL;
160     }
161 
162     std::string newFile = "";
163     OHOS::Uri newFileUri(newFile);
164     Uri uri(insideInputUri);
165     int ret = CreateFile(uri, displayName, newFileUri);
166     if (!reply.WriteInt32(ret)) {
167         HILOG_ERROR("Parameter CreateFile fail to WriteInt32 ret");
168         return E_IPCS;
169     }
170 
171     std::string insideOutputUri = newFileUri.ToString();
172     if (!reply.WriteString(insideOutputUri)) {
173         HILOG_ERROR("Parameter CreateFile fail to WriteParcelable newFileUri");
174         return E_IPCS;
175     }
176 
177     return ERR_OK;
178 }
179 
CmdMkdir(MessageParcel & data,MessageParcel & reply)180 ErrCode FileAccessExtStub::CmdMkdir(MessageParcel &data, MessageParcel &reply)
181 {
182     UserAccessTracer trace;
183     trace.Start("CmdMkdir");
184     std::string insideInputUri;
185     if (!data.ReadString(insideInputUri)) {
186         HILOG_ERROR("Parameter Mkdir fail to ReadParcelable uri");
187         return E_IPCS;
188     }
189 
190     if (insideInputUri.empty()) {
191         HILOG_ERROR("Parameter Mkdir insideInputUri is empty");
192         return EINVAL;
193     }
194 
195     std::string displayName = "";
196     if (!data.ReadString(displayName)) {
197         HILOG_ERROR("Parameter Mkdir fail to ReadString displayName");
198         return E_IPCS;
199     }
200 
201     if (displayName.empty()) {
202         HILOG_ERROR("Parameter Mkdir displayName is empty");
203         return EINVAL;
204     }
205 
206     std::string newFile = "";
207     OHOS::Uri newFileUri(newFile);
208     Uri uri(insideInputUri);
209     int ret = Mkdir(uri, displayName, newFileUri);
210     if (!reply.WriteInt32(ret)) {
211         HILOG_ERROR("Parameter Mkdir fail to WriteInt32 ret");
212         return E_IPCS;
213     }
214 
215     std::string insideOutputUri = newFileUri.ToString();
216     if (!reply.WriteString(insideOutputUri)) {
217         HILOG_ERROR("Parameter Mkdir fail to WriteParcelable newFileUri");
218         return E_IPCS;
219     }
220 
221     return ERR_OK;
222 }
223 
CmdDelete(MessageParcel & data,MessageParcel & reply)224 ErrCode FileAccessExtStub::CmdDelete(MessageParcel &data, MessageParcel &reply)
225 {
226     UserAccessTracer trace;
227     trace.Start("CmdDelete");
228     std::string insideInputUri;
229     if (!data.ReadString(insideInputUri)) {
230         HILOG_ERROR("Parameter Delete fail to ReadParcelable uri");
231         return E_IPCS;
232     }
233 
234     if (insideInputUri.empty()) {
235         HILOG_ERROR("Parameter Delete insideInputUri is empty");
236         return EINVAL;
237     }
238 
239     Uri uri(insideInputUri);
240     int ret = Delete(uri);
241     if (!reply.WriteInt32(ret)) {
242         HILOG_ERROR("Parameter Delete fail to WriteInt32 ret");
243         return E_IPCS;
244     }
245 
246     return ERR_OK;
247 }
248 
CmdMove(MessageParcel & data,MessageParcel & reply)249 ErrCode FileAccessExtStub::CmdMove(MessageParcel &data, MessageParcel &reply)
250 {
251     UserAccessTracer trace;
252     trace.Start("CmdMove");
253     std::string sourceFile;
254     if (!data.ReadString(sourceFile)) {
255         HILOG_ERROR("Parameter Move fail to ReadParcelable uri");
256         return E_IPCS;
257     }
258 
259     std::string targetParent;
260     if (!data.ReadString(targetParent)) {
261         HILOG_ERROR("Parameter Move fail to ReadParcelable uri");
262         return E_IPCS;
263     }
264 
265     std::string newFile = "";
266     OHOS::Uri newFileUri(newFile);
267     Uri source(sourceFile);
268     Uri target(targetParent);
269     int ret = Move(source, target, newFileUri);
270     if (!reply.WriteInt32(ret)) {
271         HILOG_ERROR("Parameter Move fail to WriteInt32 ret");
272         return E_IPCS;
273     }
274 
275     std::string insideOutputUri = newFileUri.ToString();
276     if (!reply.WriteString(insideOutputUri)) {
277         HILOG_ERROR("Parameter Move fail to WriteParcelable newFileUri");
278         return E_IPCS;
279     }
280 
281     return ERR_OK;
282 }
283 
CmdCopy(MessageParcel & data,MessageParcel & reply)284 ErrCode FileAccessExtStub::CmdCopy(MessageParcel &data, MessageParcel &reply)
285 {
286     UserAccessTracer trace;
287     trace.Start("CmdCopy");
288     std::string sourceUri;
289     if (!data.ReadString(sourceUri)) {
290         HILOG_ERROR("Parameter Copy fail to ReadParcelable uri");
291         return E_IPCS;
292     }
293 
294     std::string destUri;
295     if (!data.ReadString(destUri)) {
296         HILOG_ERROR("Parameter Copy fail to ReadParcelable uri");
297         return E_IPCS;
298     }
299 
300     bool force = false;
301     if (!data.ReadBool(force)) {
302         HILOG_ERROR("Parameter Copy fail to ReadBool force");
303         return E_IPCS;
304     }
305 
306     std::vector<CopyResult> copyResult;
307     Uri source(sourceUri);
308     Uri dest(destUri);
309     int ret = Copy(source, dest, copyResult, force);
310     if (!reply.WriteInt32(ret)) {
311         HILOG_ERROR("Parameter Copy fail to WriteInt32 ret");
312         return E_IPCS;
313     }
314 
315     if (!reply.WriteUint32(copyResult.size())) {
316         HILOG_ERROR("Parameter Copy fail to WriteInt32 size of copyResult");
317         return E_IPCS;
318     }
319 
320     for (auto &result : copyResult) {
321         if (!reply.WriteParcelable(&result)) {
322             HILOG_ERROR("Parameter Copy fail to WriteParcelable copyResult");
323             return E_IPCS;
324         }
325     }
326 
327     return ERR_OK;
328 }
329 
CmdRename(MessageParcel & data,MessageParcel & reply)330 ErrCode FileAccessExtStub::CmdRename(MessageParcel &data, MessageParcel &reply)
331 {
332     UserAccessTracer trace;
333     trace.Start("CmdRename");
334     std::string sourceFile;
335     if (!data.ReadString(sourceFile)) {
336         HILOG_ERROR("Parameter Rename fail to ReadParcelable uri");
337         return E_IPCS;
338     }
339 
340     std::string displayName = "";
341     if (!data.ReadString(displayName)) {
342         HILOG_ERROR("Parameter Rename fail to ReadString displayName");
343         return E_IPCS;
344     }
345 
346     if (displayName.empty()) {
347         HILOG_ERROR("Parameter Rename displayName is empty");
348         return EINVAL;
349     }
350 
351     std::string newFile = "";
352     OHOS::Uri newFileUri(newFile);
353     Uri source(sourceFile);
354     int ret = Rename(source, displayName, newFileUri);
355     if (!reply.WriteInt32(ret)) {
356         HILOG_ERROR("Parameter Rename fail to WriteInt32 ret");
357         return E_IPCS;
358     }
359 
360     std::string insideOutputUri = newFileUri.ToString();
361     if (!reply.WriteString(insideOutputUri)) {
362         HILOG_ERROR("Parameter Rename fail to WriteParcelable newFileUri");
363         return E_IPCS;
364     }
365 
366     return ERR_OK;
367 }
368 
CmdListFile(MessageParcel & data,MessageParcel & reply)369 ErrCode FileAccessExtStub::CmdListFile(MessageParcel &data, MessageParcel &reply)
370 {
371     UserAccessTracer trace;
372     trace.Start("CmdListFile");
373     std::shared_ptr<FileInfo> fileInfo(data.ReadParcelable<FileInfo>());
374     if (fileInfo == nullptr) {
375         HILOG_ERROR("Parameter ListFile fail to ReadParcelable fileInfo");
376         return E_IPCS;
377     }
378 
379     int64_t offset = 0;
380     if (!data.ReadInt64(offset)) {
381         HILOG_ERROR("parameter ListFile offset is invalid");
382         return E_IPCS;
383     }
384 
385     int64_t maxCount = 0;
386     if (!data.ReadInt64(maxCount)) {
387         HILOG_ERROR("parameter ListFile maxCount is invalid");
388         return E_IPCS;
389     }
390 
391     std::shared_ptr<FileFilter> filter(data.ReadParcelable<FileFilter>());
392     if (filter == nullptr) {
393         HILOG_ERROR("parameter ListFile FileFilter is invalid");
394         return E_IPCS;
395     }
396 
397     std::vector<FileInfo> fileInfoVec;
398     int ret = ListFile(*fileInfo, offset, maxCount, *filter, fileInfoVec);
399     if (!reply.WriteInt32(ret)) {
400         HILOG_ERROR("Parameter ListFile fail to WriteInt32 ret");
401         return E_IPCS;
402     }
403 
404     int64_t count {fileInfoVec.size()};
405     if (!reply.WriteInt64(count)) {
406         HILOG_ERROR("Parameter ListFile fail to WriteInt64 count");
407         return E_IPCS;
408     }
409 
410     for (const auto &fileInfo : fileInfoVec) {
411         if (!reply.WriteParcelable(&fileInfo)) {
412             HILOG_ERROR("parameter ListFile fail to WriteParcelable fileInfoVec");
413             return E_IPCS;
414         }
415     }
416 
417     return ERR_OK;
418 }
419 
CmdScanFile(MessageParcel & data,MessageParcel & reply)420 ErrCode FileAccessExtStub::CmdScanFile(MessageParcel &data, MessageParcel &reply)
421 {
422     UserAccessTracer trace;
423     trace.Start("CmdScanFile");
424     std::shared_ptr<FileInfo> fileInfo(data.ReadParcelable<FileInfo>());
425     if (fileInfo == nullptr) {
426         HILOG_ERROR("Parameter ScanFile fail to ReadParcelable fileInfo");
427         return E_IPCS;
428     }
429 
430     int64_t offset = 0;
431     if (!data.ReadInt64(offset)) {
432         HILOG_ERROR("parameter ScanFile offset is invalid");
433         return E_IPCS;
434     }
435 
436     int64_t maxCount = 0;
437     if (!data.ReadInt64(maxCount)) {
438         HILOG_ERROR("parameter ScanFile maxCount is invalid");
439         return E_IPCS;
440     }
441 
442     std::shared_ptr<FileFilter> filter(data.ReadParcelable<FileFilter>());
443     if (filter == nullptr) {
444         HILOG_ERROR("parameter ScanFile FileFilter is invalid");
445         return E_IPCS;
446     }
447 
448     std::vector<FileInfo> fileInfoVec;
449     int ret = ScanFile(*fileInfo, offset, maxCount, *filter, fileInfoVec);
450     if (!reply.WriteInt32(ret)) {
451         HILOG_ERROR("Parameter ScanFile fail to WriteInt32 ret");
452         return E_IPCS;
453     }
454 
455     int64_t count {fileInfoVec.size()};
456     if (!reply.WriteInt64(count)) {
457         HILOG_ERROR("Parameter ScanFile fail to WriteInt64 count");
458         return E_IPCS;
459     }
460 
461     for (const auto &fileInfo : fileInfoVec) {
462         if (!reply.WriteParcelable(&fileInfo)) {
463             HILOG_ERROR("parameter ScanFile fail to WriteParcelable fileInfoVec");
464             return E_IPCS;
465         }
466     }
467 
468     return ERR_OK;
469 }
470 
CmdGetRoots(MessageParcel & data,MessageParcel & reply)471 ErrCode FileAccessExtStub::CmdGetRoots(MessageParcel &data, MessageParcel &reply)
472 {
473     UserAccessTracer trace;
474     trace.Start("CmdGetRoots");
475 
476     std::vector<RootInfo> rootInfoVec;
477     int ret = GetRoots(rootInfoVec);
478     if (!reply.WriteInt32(ret)) {
479         HILOG_ERROR("Parameter GetRoots fail to WriteInt32 ret");
480         return E_IPCS;
481     }
482 
483     int64_t count {rootInfoVec.size()};
484     if (!reply.WriteInt64(count)) {
485         HILOG_ERROR("Parameter GetRoots fail to WriteInt64 count");
486         return E_IPCS;
487     }
488 
489     for (const auto &rootInfo : rootInfoVec) {
490         if (!reply.WriteParcelable(&rootInfo)) {
491             HILOG_ERROR("parameter ListFile fail to WriteParcelable rootInfo");
492             return E_IPCS;
493         }
494     }
495 
496     return ERR_OK;
497 }
498 
CmdQuery(MessageParcel & data,MessageParcel & reply)499 ErrCode FileAccessExtStub::CmdQuery(MessageParcel &data, MessageParcel &reply)
500 {
501     UserAccessTracer trace;
502     trace.Start("CmdQuery");
503     std::string uri;
504     if (!data.ReadString(uri)) {
505         HILOG_ERROR("Parameter Query fail to ReadParcelable uri");
506         return E_IPCS;
507     }
508 
509     int64_t count = 0;
510     if (!data.ReadInt64(count)) {
511         HILOG_ERROR("Query operation failed to Read count");
512         return E_IPCS;
513     }
514     if (count > FILE_RESULT_TYPE.size()) {
515         HILOG_ERROR(" The number of query operations exceeds %{public}zu ", FILE_RESULT_TYPE.size());
516         return EINVAL;
517     }
518     std::vector<std::string> columns;
519     for (int64_t i = 0; i < count; i++) {
520         columns.push_back(data.ReadString());
521     }
522     std::vector<std::string> results;
523     Uri sourceUri(uri);
524     int ret = Query(sourceUri, columns, results);
525     if (!reply.WriteInt32(ret)) {
526         HILOG_ERROR("Parameter Query fail to WriteInt32 ret");
527         return E_IPCS;
528     }
529 
530     int64_t resCount {results.size()};
531     if (!reply.WriteInt64(resCount)) {
532         HILOG_ERROR("Parameter Query fail to WriteInt64 count");
533         return E_IPCS;
534     }
535 
536     for (const auto &result : results) {
537         if (!reply.WriteString(result)) {
538             HILOG_ERROR("parameter Query fail to WriteParcelable column");
539             return E_IPCS;
540         }
541     }
542 
543     return ERR_OK;
544 }
545 
CmdGetThumbnail(MessageParcel & data,MessageParcel & reply)546 ErrCode FileAccessExtStub::CmdGetThumbnail(MessageParcel &data, MessageParcel &reply)
547 {
548     UserAccessTracer trace;
549     trace.Start("CmdGetThumbnail");
550     std::string uri;
551     if (!data.ReadString(uri)) {
552         HILOG_ERROR("Parameter GetThumbnail fail to ReadParcelable uri");
553         return E_IPCS;
554     }
555 
556     std::shared_ptr<ThumbnailSize> thumbnailSize(data.ReadParcelable<ThumbnailSize>());
557     if (thumbnailSize == nullptr) {
558         HILOG_ERROR("Parameter GetThumbnail fail to ReadParcelable thumbnailSize");
559         return E_URIS;
560     }
561 
562     std::shared_ptr<PixelMap> pixelMap;
563     Uri sourceUri(uri);
564     int ret = GetThumbnail(sourceUri, *thumbnailSize, pixelMap);
565     if (!reply.WriteInt32(ret)) {
566         HILOG_ERROR("Parameter CmdGetThumbnail fail to WriteInt32 ret");
567         return E_IPCS;
568     }
569 
570     if (!reply.WriteParcelable(pixelMap.get())) {
571         HILOG_ERROR("Parameter CmdGetThumbnail fail to WriteParcelable pixelMap");
572         return E_IPCS;
573     }
574 
575     return ERR_OK;
576 }
577 
CmdGetFileInfoFromUri(MessageParcel & data,MessageParcel & reply)578 ErrCode FileAccessExtStub::CmdGetFileInfoFromUri(MessageParcel &data, MessageParcel &reply)
579 {
580     UserAccessTracer trace;
581     trace.Start("CmdGetFileInfoFromUri");
582     std::string uri;
583     if (!data.ReadString(uri)) {
584         HILOG_ERROR("Parameter GetFileInfoFromUri fail to ReadParcelable uri");
585         return E_IPCS;
586     }
587 
588     FileInfo fileInfoTemp;
589     Uri sourceUri(uri);
590     int ret = GetFileInfoFromUri(sourceUri, fileInfoTemp);
591     if (!reply.WriteInt32(ret)) {
592         HILOG_ERROR("Parameter GetFileInfoFromUri fail to WriteInt32 ret");
593         return E_IPCS;
594     }
595 
596     if (!reply.WriteParcelable(&fileInfoTemp)) {
597         HILOG_ERROR("Parameter GetFileInfoFromUri fail to WriteParcelable fileInfoTemp");
598         return E_IPCS;
599     }
600 
601     return ERR_OK;
602 }
603 
CmdGetFileInfoFromRelativePath(MessageParcel & data,MessageParcel & reply)604 ErrCode FileAccessExtStub::CmdGetFileInfoFromRelativePath(MessageParcel &data, MessageParcel &reply)
605 {
606     UserAccessTracer trace;
607     trace.Start("CmdGetFileInfoFromRelativePath");
608     std::string relativePath(data.ReadString());
609 
610     FileInfo fileInfoTemp;
611     int ret = GetFileInfoFromRelativePath(relativePath, fileInfoTemp);
612     if (!reply.WriteInt32(ret)) {
613         HILOG_ERROR("Parameter CmdGetFileInfoFromRelativePath fail to WriteInt32 ret");
614         return E_IPCS;
615     }
616 
617     if (!reply.WriteParcelable(&fileInfoTemp)) {
618         HILOG_ERROR("Parameter CmdGetFileInfoFromRelativePath fail to WriteParcelable fileInfoTemp");
619         return E_IPCS;
620     }
621 
622     return ERR_OK;
623 }
624 
CmdAccess(MessageParcel & data,MessageParcel & reply)625 ErrCode FileAccessExtStub::CmdAccess(MessageParcel &data, MessageParcel &reply)
626 {
627     UserAccessTracer trace;
628     trace.Start("CmdAccess");
629     std::string uri;
630     if (!data.ReadString(uri)) {
631         HILOG_ERROR("Parameter Query fail to ReadParcelable uri");
632         return E_IPCS;
633     }
634 
635     bool isExist = false;
636     Uri sourceUri(uri);
637     int ret = Access(sourceUri, isExist);
638     if (!reply.WriteInt32(ret)) {
639         HILOG_ERROR("Parameter Access fail to WriteInt32 ret");
640         return E_IPCS;
641     }
642 
643     if (!reply.WriteBool(isExist)) {
644         HILOG_ERROR("Parameter Access fail to WriteBool isExist");
645         return E_IPCS;
646     }
647 
648     return ERR_OK;
649 }
650 
CmdStartWatcher(MessageParcel & data,MessageParcel & reply)651 ErrCode FileAccessExtStub::CmdStartWatcher(MessageParcel &data, MessageParcel &reply)
652 {
653     UserAccessTracer trace;
654     trace.Start("CmdStartWatcher");
655     std::string uriString;
656     if (!data.ReadString(uriString)) {
657         HILOG_ERROR("Parameter StartWatcher fail to ReadParcelable uri");
658         return E_IPCS;
659     }
660 
661     if (uriString.empty()) {
662         HILOG_ERROR("Parameter StartWatcher insideInputUri is empty");
663         return EINVAL;
664     }
665 
666     Uri uri(uriString);
667     int ret = StartWatcher(uri);
668     if (!reply.WriteInt32(ret)) {
669         HILOG_ERROR("Parameter StartWatcher fail to WriteInt32 ret");
670         return E_IPCS;
671     }
672 
673     return ERR_OK;
674 }
675 
CmdStopWatcher(MessageParcel & data,MessageParcel & reply)676 ErrCode FileAccessExtStub::CmdStopWatcher(MessageParcel &data, MessageParcel &reply)
677 {
678     UserAccessTracer trace;
679     trace.Start("CmdStopWatcher");
680     std::string uriString;
681     if (!data.ReadString(uriString)) {
682         HILOG_ERROR("Parameter StopWatcher fail to ReadParcelable uri");
683         return E_IPCS;
684     }
685 
686     if (uriString.empty()) {
687         HILOG_ERROR("Parameter StopWatcher insideInputUri is empty");
688         return EINVAL;
689     }
690 
691     bool isUnregisterAll = false;
692     if (!data.ReadBool(isUnregisterAll)) {
693         HILOG_ERROR("Parameter Copy fail to ReadBool isUnregisterAll");
694         return E_IPCS;
695     }
696 
697     Uri uri(uriString);
698     int ret = StopWatcher(uri, isUnregisterAll);
699     if (!reply.WriteInt32(ret)) {
700         HILOG_ERROR("Parameter StopWatcher fail to WriteInt32 ret");
701         return E_IPCS;
702     }
703 
704     return ERR_OK;
705 }
CheckCallingPermission(const std::string & permission)706 bool FileAccessExtStub::CheckCallingPermission(const std::string &permission)
707 {
708     UserAccessTracer trace;
709     trace.Start("CheckCallingPermission");
710     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
711     int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permission);
712     if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
713         HILOG_ERROR("FileAccessExtStub::CheckCallingPermission have no fileAccess permission");
714         return false;
715     }
716 
717     return true;
718 }
719 } // namespace FileAccessFwk
720 } // namespace OHOS
721