1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | |
16 | * \author Hechong.xyf |
17 | * \date Apr 2018 |
18 | * \brief Interface of AiLego Utility File |
19 | */ |
20 | |
21 | #ifndef __AILEGO_IO_FILE_H__ |
22 | #define __AILEGO_IO_FILE_H__ |
23 | |
24 | #include <ailego/utility/file_helper.h> |
25 | |
26 | namespace ailego { |
27 | |
28 | /*! File Utility |
29 | */ |
30 | class File { |
31 | public: |
32 | //! Native Handle in OS |
33 | typedef FileHelper::NativeHandle NativeHandle; |
34 | |
35 | //! Invalid Handle |
36 | static constexpr NativeHandle InvalidHandle = (NativeHandle)(-1); |
37 | |
38 | //! Specifies the position in a file to use for seeking. |
39 | enum struct Origin { Begin = 0, Current = 1, End = 2 }; |
40 | |
41 | //! Options of memory mapping |
42 | enum { |
43 | MMAP_READONLY = 1, |
44 | MMAP_SHARED = 2, |
45 | MMAP_LOCKED = 4, |
46 | MMAP_WARMUP = 8, |
47 | MMAP_POPULATE = 16 |
48 | }; |
49 | |
50 | //! Constructor |
51 | File(void) : native_handle_(File::InvalidHandle), read_only_(false) {} |
52 | |
53 | //! Constructor |
54 | File(File &&rhs) { |
55 | read_only_ = rhs.read_only_; |
56 | native_handle_ = rhs.native_handle_; |
57 | rhs.read_only_ = false; |
58 | rhs.native_handle_ = File::InvalidHandle; |
59 | } |
60 | |
61 | //! Destructor |
62 | ~File(void) { |
63 | this->close(); |
64 | } |
65 | |
66 | //! Assignment |
67 | File &operator=(File &&rhs) { |
68 | read_only_ = rhs.read_only_; |
69 | native_handle_ = rhs.native_handle_; |
70 | rhs.read_only_ = false; |
71 | rhs.native_handle_ = File::InvalidHandle; |
72 | return *this; |
73 | } |
74 | |
75 | //! Test if the file is valid |
76 | bool is_valid(void) const { |
77 | return (native_handle_ != File::InvalidHandle); |
78 | } |
79 | |
80 | //! Retrieve non-zero if memory region is read only |
81 | bool read_only(void) const { |
82 | return read_only_; |
83 | } |
84 | |
85 | //! Retrieve native handle |
86 | NativeHandle native_handle(void) const { |
87 | return native_handle_; |
88 | } |
89 | |
90 | //! Create a local file |
91 | bool create(const char *path, size_t size, bool direct); |
92 | |
93 | //! Open a local file |
94 | bool open(const char *path, bool rdonly, bool direct); |
95 | |
96 | //! Close the local file |
97 | void close(void); |
98 | |
99 | //! Reset the file |
100 | void reset(void); |
101 | |
102 | //! Write data into the file |
103 | size_t write(const void *data, size_t len); |
104 | |
105 | //! Write data into the file |
106 | size_t write(ssize_t off, const void *data, size_t len); |
107 | |
108 | //! Read data from the file |
109 | size_t read(void *buf, size_t len); |
110 | |
111 | //! Read data from the file |
112 | size_t read(ssize_t off, void *buf, size_t len); |
113 | |
114 | //! Synchronize memory with physical storage |
115 | bool flush(void); |
116 | |
117 | //! Sets the current position of the file to the given value |
118 | bool seek(ssize_t off, Origin origin); |
119 | |
120 | //! Truncate the file to a specified length |
121 | bool truncate(size_t len); |
122 | |
123 | //! Retrieve size of file |
124 | size_t size(void) const; |
125 | |
126 | //! Retrieve offset of file |
127 | ssize_t offset(void) const; |
128 | |
129 | //! Create a local file |
130 | bool create(const char *path, size_t len) { |
131 | return this->create(path, len, false); |
132 | } |
133 | |
134 | //! Create a local file |
135 | bool create(const std::string &path, size_t len, bool direct) { |
136 | return this->create(path.c_str(), len, direct); |
137 | } |
138 | |
139 | //! Create a local file |
140 | bool create(const std::string &path, size_t len) { |
141 | return this->create(path.c_str(), len); |
142 | } |
143 | |
144 | //! Open a local file |
145 | bool open(const char *path, bool rdonly) { |
146 | return this->open(path, rdonly, false); |
147 | } |
148 | |
149 | //! Open a local file |
150 | bool open(const std::string &path, bool rdonly, bool direct) { |
151 | return this->open(path.c_str(), rdonly, direct); |
152 | } |
153 | |
154 | //! Open a local file |
155 | bool open(const std::string &path, bool rdonly) { |
156 | return this->open(path.c_str(), rdonly); |
157 | } |
158 | |
159 | //! Map a region of file into memory |
160 | void *map(ssize_t off, size_t len, int opts) { |
161 | if (read_only_) { |
162 | opts |= File::MMAP_READONLY; |
163 | } |
164 | return File::MemoryMap(native_handle_, off, len, opts); |
165 | } |
166 | |
167 | //! Map a region of file into memory |
168 | static void *MemoryMap(NativeHandle handle, ssize_t off, size_t len, |
169 | int opts); |
170 | |
171 | //! Map an anonymous region into memory |
172 | static void *MemoryMap(size_t len, int opts); |
173 | |
174 | //! Remap the region into memory |
175 | static void *MemoryRemap(void *oldptr, size_t oldsize, void *newptr, |
176 | size_t newsize); |
177 | |
178 | //! Unmap a mapping region |
179 | static void MemoryUnmap(void *addr, size_t len); |
180 | |
181 | //! Synchronize a memory map |
182 | static bool MemoryFlush(void *addr, size_t len); |
183 | |
184 | //! Lock the memory region into RAM |
185 | static bool MemoryLock(void *addr, size_t len); |
186 | |
187 | //! Unlock the memory region in RAM |
188 | static bool MemoryUnlock(void *addr, size_t len); |
189 | |
190 | //! Warm up a memory region |
191 | static void MemoryWarmup(void *addr, size_t len); |
192 | |
193 | //! Delete a name and possibly the file it refers to |
194 | static bool Delete(const char *path) { |
195 | return FileHelper::DeleteFile(path); |
196 | } |
197 | |
198 | //! Delete a name and possibly the file it refers to |
199 | static bool Delete(const std::string &path) { |
200 | return FileHelper::DeleteFile(path.c_str()); |
201 | } |
202 | |
203 | //! Change the name or location of a file |
204 | static bool Rename(const char *oldpath, const char *newpath) { |
205 | return FileHelper::RenameFile(oldpath, newpath); |
206 | } |
207 | |
208 | //! Change the name or location of a file |
209 | static bool Rename(const std::string &oldpath, const std::string &newpath) { |
210 | return FileHelper::RenameFile(oldpath.c_str(), newpath.c_str()); |
211 | } |
212 | |
213 | //! Retrieve the base name from a path |
214 | static const char *BaseName(const char *path) { |
215 | return FileHelper::BaseName(path); |
216 | } |
217 | |
218 | //! Retrieve the base name from a path |
219 | static const char *BaseName(const std::string &path) { |
220 | return BaseName(path.c_str()); |
221 | } |
222 | |
223 | //! Make directories' path |
224 | static bool MakePath(const char *path) { |
225 | return FileHelper::MakePath(path); |
226 | } |
227 | |
228 | //! Make directories' path |
229 | static bool MakePath(const std::string &path) { |
230 | return FileHelper::MakePath(path.c_str()); |
231 | } |
232 | |
233 | //! Remove a file or a directory (includes files & subdirectories) |
234 | static bool RemovePath(const char *path) { |
235 | return FileHelper::RemovePath(path); |
236 | } |
237 | |
238 | //! Remove a file or a directory (includes files & subdirectories) |
239 | static bool RemovePath(const std::string &path) { |
240 | return FileHelper::RemovePath(path.c_str()); |
241 | } |
242 | |
243 | //! Remove a directory (includes files & subdirectories) |
244 | static bool RemoveDirectory(const char *path) { |
245 | return FileHelper::RemoveDirectory(path); |
246 | } |
247 | |
248 | //! Remove a directory (includes files & subdirectories) |
249 | static bool RemoveDirectory(const std::string &path) { |
250 | return FileHelper::RemoveDirectory(path.c_str()); |
251 | } |
252 | |
253 | //! Retrieve non-zero if the path exists |
254 | static bool IsExist(const char *path) { |
255 | return FileHelper::IsExist(path); |
256 | } |
257 | |
258 | //! Retrieve non-zero if the path exists |
259 | static bool IsExist(const std::string &path) { |
260 | return FileHelper::IsExist(path.c_str()); |
261 | } |
262 | |
263 | //! Retrieve non-zero if the path is a regular file |
264 | static bool IsRegular(const char *path) { |
265 | return FileHelper::IsRegular(path); |
266 | } |
267 | |
268 | //! Retrieve non-zero if the path is a regular file |
269 | static bool IsRegular(const std::string &path) { |
270 | return FileHelper::IsRegular(path.c_str()); |
271 | } |
272 | |
273 | //! Retrieve non-zero if the path is a directory |
274 | static bool IsDirectory(const char *path) { |
275 | return FileHelper::IsDirectory(path); |
276 | } |
277 | |
278 | //! Retrieve non-zero if the path is a directory |
279 | static bool IsDirectory(const std::string &path) { |
280 | return FileHelper::IsDirectory(path.c_str()); |
281 | } |
282 | |
283 | //! Retrieve non-zero if the path is a symbolic link |
284 | static bool IsSymbolicLink(const char *path) { |
285 | return FileHelper::IsSymbolicLink(path); |
286 | } |
287 | |
288 | //! Retrieve non-zero if the path is a symbolic link |
289 | static bool IsSymbolicLink(const std::string &path) { |
290 | return FileHelper::IsSymbolicLink(path.c_str()); |
291 | } |
292 | |
293 | //! Retrieve non-zero if two paths are pointing to the same file |
294 | static bool IsSame(const char *path1, const char *path2) { |
295 | return FileHelper::IsSame(path1, path2); |
296 | } |
297 | |
298 | //! Retrieve non-zero if two paths are pointing to the same file |
299 | static bool IsSame(const std::string &path1, const std::string &path2) { |
300 | return FileHelper::IsSame(path1.c_str(), path2.c_str()); |
301 | } |
302 | |
303 | private: |
304 | //! Disable them |
305 | File(const File &) = delete; |
306 | File &operator=(const File &) = delete; |
307 | |
308 | //! Members |
309 | NativeHandle native_handle_; |
310 | bool read_only_; |
311 | }; |
312 | |
313 | } // namespace ailego |
314 | |
315 | #endif // __AILEGO_IO_FILE_H__ |
316 | |