File: FileSystem.h

package info (click to toggle)
jazz2-native 3.5.0-3
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (375 lines) | stat: -rw-r--r-- 15,753 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#pragma once

#include "Stream.h"
#include "FileAccess.h"
#include "../Containers/String.h"

#include <memory>
#include <optional>

namespace Death { namespace IO {
//###==##====#=====--==~--~=~- --- -- -  -  -   -

	/**
		@brief File system related methods
	*/
	class FileSystem
	{
	public:
		/** @{ @name Constants */

		// Windows 10 supports long paths everywhere and Unix systems usually also support at least 2048 characters
		/** @brief Maximum path length supported */
		static constexpr std::size_t MaxPathLength = 2048;

#if defined(DEATH_TARGET_WINDOWS)
		/** @brief Native path separator */
		static constexpr char PathSeparator[] = "\\";
#else
		/** @brief Native path separator */
		static constexpr char PathSeparator[] = "/";
#endif

		/** @} */

		/** @brief Available permissions to check or set, supports a bitwise combination of its member values */
		enum class Permission
		{
			None = 0,			/**< None */

			Read = 0x01,		/**< Read */
			Write = 0x02,		/**< Write */
			Execute = 0x04		/**< Execute */
		};

		DEATH_PRIVATE_ENUM_FLAGS(Permission);

		/** @brief Options that modify behavior of @ref Directory, supports a bitwise combination of its member values */
		enum class EnumerationOptions
		{
			/** @brief Default behavior */
			None = 0,

			/** @brief Skip regular files */
			SkipFiles = 0x01,
			/** @brief Skip directories */
			SkipDirectories = 0x02,
			/** @brief Skip everything that is not a file or directory */
			SkipSpecial = 0x04
		};

		DEATH_PRIVATE_ENUM_FLAGS(EnumerationOptions);

		/** @brief Handles directory traversal, should be used as iterator */
		class Directory
		{
		public:
#ifndef DOXYGEN_GENERATING_OUTPUT
			class Proxy;

			// Iterator defines
			using iterator_category = std::input_iterator_tag;
			using difference_type = std::ptrdiff_t;
			//using reference = const Containers::StringView&;
			using value_type = Containers::StringView;
#endif

			Directory() noexcept;
			Directory(Containers::StringView path, EnumerationOptions options = EnumerationOptions::None);
			~Directory();

			Directory(const Directory& other);
			Directory& operator=(const Directory& other);
			Directory(Directory&& other) noexcept;
			Directory& operator=(Directory&& other) noexcept;

			Containers::StringView operator*() const & noexcept;
			Directory& operator++();
			Proxy operator++(int);

			bool operator==(const Directory& other) const;
			bool operator!=(const Directory& other) const;

			Directory begin() noexcept {
				return *this;
			}

			Directory end() noexcept {
				return Directory();
			}

		private:
			class Impl;
			std::shared_ptr<Impl> _impl;
		};

		FileSystem() = delete;
		~FileSystem() = delete;

#if defined(DEATH_TARGET_WINDOWS) || defined(DEATH_TARGET_SWITCH)
		/**
		 * @brief Returns path with correct case on case-sensitive platforms (or `{}` if path not found)
		 * 
		 * Windows® is already case-insensitive by default, so no validation is performed.
		 */
		DEATH_ALWAYS_INLINE static Containers::StringView FindPathCaseInsensitive(Containers::StringView path) {
			return path;
		}

		/** @overload */
		DEATH_ALWAYS_INLINE static Containers::String FindPathCaseInsensitive(Containers::String&& path) {
			return path;
		}
#else
		/**
		 * @brief Returns path with correct case on case-sensitive platforms (or `{}` if path not found)
		 */
		static Containers::String FindPathCaseInsensitive(Containers::StringView path);

		/** @overload */
		DEATH_ALWAYS_INLINE static Containers::String FindPathCaseInsensitive(Containers::String&& path) {
			return FindPathCaseInsensitive(Containers::StringView{path});
		}
#endif

		/** @brief Combines together specified path components */
		static Containers::String CombinePath(Containers::StringView first, Containers::StringView second);
		/** @overload */
		static Containers::String CombinePath(Containers::ArrayView<const Containers::StringView> paths);
		/** @overload */
		static Containers::String CombinePath(std::initializer_list<Containers::StringView> paths);

		/** @brief Returns the path up to, but not including, the final separator */
		static Containers::StringView GetDirectoryName(Containers::StringView path);
		/** @brief Returns the path component after the final separator */
		static Containers::StringView GetFileName(Containers::StringView path);
		/** @brief Returns the path component after the final separator without extension */
		static Containers::StringView GetFileNameWithoutExtension(Containers::StringView path);
		/** @brief Returns the extension as lower-case string without dot or empty string if it is not found */
		static Containers::String GetExtension(Containers::StringView path);
		/** @brief Converts path using native separators to forward slashes */
#if defined(DEATH_TARGET_WINDOWS)
		static Containers::String FromNativeSeparators(Containers::String path);
#else
		DEATH_ALWAYS_INLINE static Containers::StringView FromNativeSeparators(Containers::StringView path) {
			return path;
		}
#endif
		/** @brief Converts path using forward slashes to native separators */
#if defined(DEATH_TARGET_WINDOWS)
		static Containers::String ToNativeSeparators(Containers::String path);
#else
		DEATH_ALWAYS_INLINE static Containers::StringView ToNativeSeparators(Containers::StringView path) {
			return path;
		}
#endif

		/** @brief Returns an absolute path from a relative one */
		static Containers::String GetAbsolutePath(Containers::StringView path);
		/** @brief Returns `true` if the specified path is not empty and is absolute */
		static bool IsAbsolutePath(Containers::StringView path);

		/** @brief Returns the path to the executable file for the running application */
		static Containers::String GetExecutablePath();
		/**
		 * @brief Returns the path to the application-specific writable directory for saving game state
		 * 
		 * On macOS, the directory is usually equivalent to @cpp "~/Library/Application Support/<name>/" @ce. On Android,
		 * it's the internal data directory of the application. On other Unix systems, it usually points to
		 * @cb{.sh} "${XDG_CONFIG_HOME}/<name>/ @ce or @cpp "~/.config/<name>/" @ce. On Windows, it's usually
		 * @cpp "C:\\Users\\<user>\\Saved Games\\<name>\\" @ce. If the parent directory doesn't exist, @cb{.bat} %APPDATA% @ce
		 * will be used instead. On Windows RT, the local data folder of the package is returned, because
		 * the application doesn't have access to the user directories.
		 */
		static Containers::String GetSavePath(Containers::StringView applicationName);
		/** @brief Returns the path of the current working directory */
		static Containers::String GetWorkingDirectory();
		/** @brief Sets the current working directory, the starting point for interpreting relative paths */
		static bool SetWorkingDirectory(Containers::StringView path);
		/**
		 * @brief Returns the path of the user home directory
		 * 
		 * On Unix and macOS, the directory is equivalent to @cb{.sh} ${HOME} @ce environment variable. On Windows,
		 * the directory is equivalent to @cb{.bat} %USERPROFILE% @ce, which usually points to @cpp "C:\\Users\\<user>\\" @ce.
		 */
		static Containers::String GetHomeDirectory();
		/**
		 * @brief Returns the path of the directory for temporary files
		 * 
		 * On Unix and macOS, the directory is usually equivalent to @cpp "/tmp/" @ce. On Windows, the directory is
		 * equivalent to @cb{.bat} %TEMP% @ce. On Android, the directory is usually equivalent to the cache
		 * directory of the package (for example @cpp "/data/user/0/<package>/cache/" @ce).
		 */
		static Containers::String GetTempDirectory();

#if defined(DEATH_TARGET_ANDROID) || defined(DOXYGEN_GENERATING_OUTPUT)
		/**
		 * @brief Returns the path of the Android external storage directory
		 *
		 * @partialsupport Available only on @ref DEATH_TARGET_ANDROID "Android" platform.
		 */
		static Containers::String GetExternalStorage();
#endif
#if defined(DEATH_TARGET_UNIX) || defined(DOXYGEN_GENERATING_OUTPUT)
		/**
		 * @brief Returns the path pointing to `${XDG_DATA_HOME}` environment variable
		 * 
		 * If @cb{.sh} ${XDG_DATA_HOME} @ce environment variable is not set, @cpp "~/.local/share/" @ce
		 * will be used instead.
		 *
		 * @partialsupport Available only on @ref DEATH_TARGET_UNIX "Unix" platform.
		 */
		static Containers::String GetLocalStorage();
#endif
#if defined(DEATH_TARGET_WINDOWS) || defined(DOXYGEN_GENERATING_OUTPUT)
		/**
		 * @brief Returns the path of Windows® directory
		 *
		 * @partialsupport Available only on @ref DEATH_TARGET_WINDOWS "Windows" platform.
		 */
		static Containers::String GetWindowsDirectory();
#endif

		/** @brief Returns `true` if the specified path is a directory */
		static bool DirectoryExists(Containers::StringView path);
		/** @brief Returns `true` if the specified path is a file */
		static bool FileExists(Containers::StringView path);

		/** @brief Returns `true` if the file or directory exists */
		static bool Exists(Containers::StringView path);
		/** @brief Returns `true` if the file or directory is readable */
		static bool IsReadable(Containers::StringView path);
		/** @brief Returns `true` if the file or directory is writeable */
		static bool IsWritable(Containers::StringView path);
		/** @brief Returns `true` if the file or directory is executable */
		static bool IsExecutable(Containers::StringView path);

		/** @brief Returns `true` if the path is a file and is readable */
		static bool IsReadableFile(Containers::StringView path);
		/** @brief Returns `true` if the path is a file and is writeable */
		static bool IsWritableFile(Containers::StringView path);

		/** @brief Returns `true` if the path is a symbolic link */
		static bool IsSymbolicLink(Containers::StringView path);

		/** @brief Returns `true` if the file or directory is hidden */
		static bool IsHidden(Containers::StringView path);
		/** @brief Makes a file or directory hidden or not */
		static bool SetHidden(Containers::StringView path, bool hidden);
		/** @brief Returns `true` if the file or directory is read-only */
		static bool IsReadOnly(Containers::StringView path);
		/** @brief Makes a file or directory read-only or not */
		static bool SetReadOnly(Containers::StringView path, bool readonly);

		/** @brief Creates a new directory */
		static bool CreateDirectories(Containers::StringView path);
		/** @brief Deletes an directory and all its content */
		static bool RemoveDirectoryRecursive(Containers::StringView path);
		/** @brief Deletes a file */
		static bool RemoveFile(Containers::StringView path);
		/** @brief Renames or moves a file or a directory */
		static bool Move(Containers::StringView oldPath, Containers::StringView newPath);
		/** @brief Moves a file or a directory to trash */
		static bool MoveToTrash(Containers::StringView path);
		/** @brief Copies a file */
		static bool Copy(Containers::StringView oldPath, Containers::StringView newPath, bool overwrite = true);

		/** @brief Returns the file size in bytes */
		static std::int64_t GetFileSize(Containers::StringView path);
		/** @brief Returns the creation time of the file or directory (if available) */
		static Containers::DateTime GetCreationTime(Containers::StringView path);
		/** @brief Returns the last time the file or directory was modified */
		static Containers::DateTime GetLastModificationTime(Containers::StringView path);
		/** @brief Returns the last time the file or directory was accessed */
		static Containers::DateTime GetLastAccessTime(Containers::StringView path);

		/** @brief Returns permissions of a given file or directory */
		static Permission GetPermissions(Containers::StringView path);
		/** @brief Sets the file or directory permissions to those of the mask */
		static bool ChangePermissions(Containers::StringView path, Permission mode);
		/** @brief Adds permissions in the mask to a file or a directory */
		static bool AddPermissions(Containers::StringView path, Permission mode);
		/** @brief Removes permissions in the mask from a file or a directory */
		static bool RemovePermissions(Containers::StringView path, Permission mode);

		/** @brief Tries to open specified directory in operating system's file manager */
		static bool LaunchDirectoryAsync(Containers::StringView path);

#if defined(DEATH_TARGET_EMSCRIPTEN) || defined(DOXYGEN_GENERATING_OUTPUT)
		/**
		 * @brief Mounts specified path to persistent file system
		 *
		 * @partialsupport Available only on @ref DEATH_TARGET_EMSCRIPTEN "Emscripten" platform.
		 */
		static bool MountAsPersistent(Containers::StringView path);

		/**
		 * @brief Saves all changes to all persistent file systems
		 *
		 * @partialsupport Available only on @ref DEATH_TARGET_EMSCRIPTEN "Emscripten" platform.
		 */
		static void SyncToPersistent();
#endif

		/** @brief Opens a file stream with specified access mode */
		static std::unique_ptr<Stream> Open(Containers::StringView path, FileAccess mode, std::int32_t bufferSize = 8192);

#	if defined(DEATH_TARGET_ANDROID) || defined(DEATH_TARGET_APPLE) || defined(DEATH_TARGET_UNIX) || (defined(DEATH_TARGET_WINDOWS) && !defined(DEATH_TARGET_WINDOWS_RT)) || defined(DOXYGEN_GENERATING_OUTPUT)
		/**
			@brief Memory-mapped file deleter
		
			@partialsupport Available on all platforms except @ref DEATH_TARGET_EMSCRIPTEN "Emscripten",
				@ref DEATH_TARGET_SWITCH "Nintendo Switch" and @ref DEATH_TARGET_WINDOWS_RT "Windows RT".
		*/
		class MapDeleter
		{
#	if defined(DEATH_TARGET_ANDROID) || defined(DEATH_TARGET_APPLE) || defined(DEATH_TARGET_UNIX)
		public:
			constexpr explicit MapDeleter() : _fd{} {}
			constexpr explicit MapDeleter(int fd) noexcept : _fd{fd} {}
			void operator()(const char* data, std::size_t size);
		private:
			int _fd;
#	elif defined(DEATH_TARGET_WINDOWS) && !defined(DEATH_TARGET_WINDOWS_RT)
		public:
			constexpr explicit MapDeleter() : _hFile{}, _hMap{} {}
			constexpr explicit MapDeleter(void* hFile, void* hMap) noexcept : _hFile{hFile}, _hMap{hMap} {}
			void operator()(const char* data, std::size_t size);
		private:
			void* _hFile;
			void* _hMap;
#	endif
		};

		/**
		 * @brief Maps a file for reading and/or writing
		 *
		 * Maps the file as a read-write memory. The array deleter takes care of unmapping. If the file doesn't exist
		 * or an error occurs while mapping, returns @ref std::nullopt_t. If the file is empty it's only opened
		 * but not mapped and a zero-sized @cpp nullptr @ce array is returned, with the deleter containing the
		 * open file handle.
		 * 
		 * @m_class{m-block m-warning}
		 * 
		 * @par Reading and writing a file while it's mapped
		 *		On @ref DEATH_TARGET_UNIX "Unix"-like systems, it's possible to write to a file that's currently
		 *		mapped with this function and the changes will be reflected to the mapping. The mapped size
		 *		however cannot change --- if the file gets longer, the additional data will not be present
		 *		in the mapping, if it gets shorter, the suffix gets filled with @cpp '\0' @ce bytes.
		 * @par
		 *		On @ref DEATH_TARGET_WINDOWS "Windows", it's not possible to open a file for writing while it's
		 *		mapped with this function. Doing so will result in an *Invalid Argument* error.
		 * @par
		 *		Opening a file for reading while it's mapped with this function works on all platforms and gives
		 *		back the same contents as the (potentially updated) mapped memory.
		 * 
		 * @partialsupport Available on all platforms except @ref DEATH_TARGET_EMSCRIPTEN "Emscripten",
		 *		@ref DEATH_TARGET_SWITCH "Nintendo Switch" and @ref DEATH_TARGET_WINDOWS_RT "Windows RT".
		 */
		static std::optional<Containers::Array<char, MapDeleter>> OpenAsMemoryMapped(Containers::StringView path, FileAccess mode);
#endif
	};

	/** @brief Convenient shortcut to @ref FileSystem */
	using fs = FileSystem;
}}