1 /*
2  * Copyright (c) 2024 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 #ifndef API_CORE_IO_IFILE_MANAGER_H
17 #define API_CORE_IO_IFILE_MANAGER_H
18 
19 #include <cstdint>
20 
21 #include <base/containers/refcnt_ptr.h>
22 #include <base/containers/string_view.h>
23 #include <base/namespace.h>
24 #include <base/util/uid.h>
25 #include <core/io/intf_directory.h>
26 #include <core/io/intf_file.h>
27 #include <core/io/intf_file_system.h>
28 #include <core/namespace.h>
29 #include <core/plugin/intf_interface.h>
30 
CORE_BEGIN_NAMESPACE()31 CORE_BEGIN_NAMESPACE()
32 
33 /** @ingroup group_io_ifilemanager */
34 /**
35  * File Manager.
36  * Provides basic file I/O across different file systems and protocols.
37  */
38 class IFileManager : public IInterface {
39 public:
40     static constexpr auto UID = BASE_NS::Uid { "5507e02b-b900-44e4-a969-0eda2d0918ac" };
41 
42     using Ptr = BASE_NS::refcnt_ptr<IFileManager>;
43 
44     /** Get uri information.
45      *  @param uri Absolute uri, such as '%file://logs/log.txt'
46      *  @return IDirectory::Entry defining entry type , size and timestamp.
47      */
48     virtual IDirectory::Entry GetEntry(BASE_NS::string_view uri) = 0;
49 
50     /** Opens file in given uri.
51      *  @param uri The complete file uri, such as '%file://images/texture.png'
52      *  @return Read-only file that was opened, empty ptr if file cannot be opened.
53      */
54     virtual IFile::Ptr OpenFile(BASE_NS::string_view uri) = 0;
55 
56     /** Creates a new file to given uri.
57      *  @param uri The complete file uri, such as '%file://logs/log.txt'
58      *  @return File that was created, empty ptr if file cannot be created.
59      */
60     virtual IFile::Ptr CreateFile(BASE_NS::string_view uri) = 0;
61 
62     /** Delete file.
63      *  @param uri The complete file uri, such as '%file://logs/log.txt'
64      *  @return True if deletion is successful, otherwise false.
65      */
66     virtual bool DeleteFile(BASE_NS::string_view uri) = 0;
67 
68     /** Opens directory in given uri.
69      *  @param uri The complete directory uri, such as '%file://images/'
70      *  @return Directory that was opened, empty ptr if directory cannot be opened.
71      */
72     virtual IDirectory::Ptr OpenDirectory(BASE_NS::string_view uri) = 0;
73 
74     /** Creates a new directory to given uri.
75      *  @param uri The complete directory uri, such as '%file://logs/'
76      *  @return Directory that was created, empty ptr if directory cannot be created.
77      */
78     virtual IDirectory::Ptr CreateDirectory(BASE_NS::string_view uri) = 0;
79 
80     /** Delete directory.
81      *  @param uri The complete directory uri, such as '%file://logs/'
82      *  @return True if deletion is successful, otherwise false.
83      */
84     virtual bool DeleteDirectory(BASE_NS::string_view uri) = 0;
85 
86     /** Rename file or directory
87      *  @param fromUri The complete uri for file/directory to rename, such as '%file://logs/old.txt'
88      *  @param toUri The complete file uri for destination name, such as '%file://logs/new.txt'
89      *  @return True if rename is successful, otherwise false.
90      */
91     virtual bool Rename(BASE_NS::string_view fromUri, BASE_NS::string_view toUri) = 0;
92 
93     /** Register a new custom filesystem. Filesystem data can be accessed using the functions in this file manager using
94      *  an uri with the registered protocol string.
95      *  @param protocol protocol string that is used to point to the filesystem.
96      *  @param filesystem file system object that implements the filesystem.
97      */
98     virtual void RegisterFilesystem(BASE_NS::string_view protocol, IFilesystem::Ptr filesystem) = 0;
99 
100     /** Unregister a filesystem.
101      *  @param protocol protocol string that is used to point to the filesystem.
102      */
103     virtual void UnregisterFilesystem(BASE_NS::string_view protocol) = 0;
104 
105     /** Register an uri that will be mapped to the "assets:" protocol. Each mapped uri will be searched for assets in
106      *  the order they are added.
107      *  @param uri Path to assets.
108      */
109     virtual void RegisterAssetPath(BASE_NS::string_view uri) = 0;
110 
111     /** Unregister an asset path.
112      *  @param uri Path to assets.
113      */
114     virtual void UnregisterAssetPath(BASE_NS::string_view uri) = 0;
115 
116     /** Register uri with given protocol for assets usage
117      *  @param protocol Protocol to be registered
118      *  @param uri Uri where protocol points
119      *  @param prepend If true, adds path to begin of search list
120      */
121     virtual bool RegisterPath(BASE_NS::string_view protocol, BASE_NS::string_view uri, bool prepend) = 0;
122 
123     /**
124      * Unregister uri from given protocol
125      * @param protocol A protocol where to remove search path
126      * @param uri Uri which is removed from paths
127      */
128     virtual void UnregisterPath(BASE_NS::string_view protocol, BASE_NS::string_view uri) = 0;
129 
130     virtual IFilesystem::Ptr CreateROFilesystem(const void* const data, uint64_t size) = 0;
131 
132 protected:
133     IFileManager() = default;
134     virtual ~IFileManager() = default;
135 };
136 
GetName(const IFileManager *)137 inline constexpr BASE_NS::string_view GetName(const IFileManager*)
138 {
139     return "IFileManager";
140 }
141 CORE_END_NAMESPACE()
142 
143 #endif // API_CORE_IO_IFILE_MANAGER_H
144