Debian Codegraph @

Source/Core/Common/FileUtil.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <filesystem>
7 #include <string>
8 #include <string_view>
9 #include <vector>
10
11 #include <sys/stat.h>
12
13 #include "Common/CommonTypes.h"
14
15 #ifdef _WIN32
16 #include "Common/StringUtil.h"
17 #endif
18
19 #ifdef ANDROID
20 #include "Common/StringUtil.h"
21 #include "jni/AndroidCommon/AndroidCommon.h"
22 #endif
23
24 // User directory indices for GetUserPath
25 enum
26 {
27 D_USER_IDX,
28 D_GCUSER_IDX,
29 D_WIIROOT_IDX, // always points to User/Wii or global user-configured directory
30 D_TRIUSER_IDX,
31 D_SESSION_WIIROOT_IDX, // may point to minimal temporary directory for determinism
32 D_CONFIG_IDX, // global settings
33 D_GAMESETTINGS_IDX, // user-specified settings which override both the global and the default
34 // settings (per game)
35 D_SKYLANDERS_IDX,
36
37 D_MAPS_IDX,
38 D_CACHE_IDX,
39 D_COVERCACHE_IDX,
40 D_REDUMPCACHE_IDX,
41 D_SHADERCACHE_IDX,
42 D_RETROACHIEVEMENTSCACHE_IDX,
43 D_SHADERS_IDX,
44 D_STATESAVES_IDX,
45 D_SCREENSHOTS_IDX,
46 D_HIRESTEXTURES_IDX,
47 D_RIIVOLUTION_IDX,
48 D_DUMP_IDX,
49 D_DUMPFRAMES_IDX,
50 D_DUMPOBJECTS_IDX,
51 D_DUMPAUDIO_IDX,
52 D_DUMPTEXTURES_IDX,
53 D_DUMPDSP_IDX,
54 D_DUMPSSL_IDX,
55 D_DUMPDEBUG_IDX,
56 D_DUMPDEBUG_BRANCHWATCH_IDX,
57 D_DUMPDEBUG_JITBLOCKS_IDX,
58 D_LOAD_IDX,
59 D_LOGS_IDX,
60 D_THEMES_IDX,
61 D_STYLES_IDX,
62 D_PIPES_IDX,
63 D_MEMORYWATCHER_IDX,
64 D_WFSROOT_IDX,
65 D_BACKUP_IDX,
66 D_RESOURCEPACK_IDX,
67 D_DYNAMICINPUT_IDX,
68 D_GRAPHICSMOD_IDX,
69 D_FIRMWARE_IDX,
70 D_GBAUSER_IDX,
71 D_GBASAVES_IDX,
72 D_WIISDCARDSYNCFOLDER_IDX,
73 D_WIISYSCONF_IDX,
74 D_GPU_DRIVERS_EXTRACTED,
75 D_GPU_DRIVERS_TMP,
76 D_GPU_DRIVERS_HOOKS,
77 D_GPU_DRIVERS_FILE_REDIRECT,
78 D_ASM_ROOT_IDX,
79 D_BANNERS_WIIROOT_IDX,
80 FIRST_FILE_USER_PATH_IDX,
81 F_DOLPHINCONFIG_IDX = FIRST_FILE_USER_PATH_IDX,
82 F_GCPADCONFIG_IDX,
83 F_WIIPADCONFIG_IDX,
84 F_GCKEYBOARDCONFIG_IDX,
85 F_GFXCONFIG_IDX,
86 F_LOGGERCONFIG_IDX,
87 F_MAINLOG_IDX,
88 F_MEM1DUMP_IDX,
89 F_MEM2DUMP_IDX,
90 F_ARAMDUMP_IDX,
91 F_FAKEVMEMDUMP_IDX,
92 F_GCSRAM_IDX,
93 F_MEMORYWATCHERLOCATIONS_IDX,
94 F_MEMORYWATCHERSOCKET_IDX,
95 F_WIISDCARDIMAGE_IDX,
96 F_WIISYSCONF_IDX,
97 F_DUALSHOCKUDPCLIENTCONFIG_IDX,
98 F_FREELOOKCONFIG_IDX,
99 F_GBABIOS_IDX,
100 F_RETROACHIEVEMENTSCONFIG_IDX,
101 NUM_PATH_INDICES
102 };
103
104 namespace File
105 {
106 // FileSystem tree node
107 struct FSTEntry
108 {
109 bool isDirectory = false;
110 u64 size = 0; // File length, or for directories, recursive count of children
111 std::string physicalName; // Name on disk
112 std::string virtualName; // Name in FST names table
113 std::vector<FSTEntry> children;
114 };
115
116 // The functions in this class are functionally identical to the standalone functions
117 // below, but if you are going to be calling more than one of the functions using the
118 // same path, creating a single FileInfo object and calling its functions multiple
119 // times is faster than calling standalone functions multiple times.
120 class FileInfo final
121 {
122 public:
123 explicit FileInfo(const std::string& path);
124 explicit FileInfo(const char* path);
125
126 // Returns true if the path exists
127 bool Exists() const;
128 // Returns true if the path exists and is a directory
129 bool IsDirectory() const;
130 // Returns true if the path exists and is a file
131 bool IsFile() const;
132 // Returns the size of a file (or returns 0 if the path doesn't refer to a file)
133 u64 GetSize() const;
134
135 private:
136 std::filesystem::file_status m_status;
137 std::uintmax_t m_size;
138 bool m_exists;
139 };
140
141 // Returns true if the path exists
142 bool Exists(const std::string& path);
143
144 // Returns true if the path exists and is a directory
145 bool IsDirectory(const std::string& path);
146
147 // Returns true if the path exists and is a file
148 bool IsFile(const std::string& path);
149
150 // Returns the size of a file (or returns 0 if the path isn't a file that exists)
151 u64 GetSize(const std::string& path);
152
153 // Overloaded GetSize, accepts FILE*
154 u64 GetSize(FILE* f);
155
156 // Creates a single directory. Returns true if successful or if the path already exists.
157 bool CreateDir(const std::string& filename);
158
159 // Creates directories recursively. Returns true if successful or if the path already exists.
160 bool CreateDirs(std::string_view filename);
161
162 // Creates the full path to the file given in fullPath.
163 // That is, for path '/a/b/c.bin', creates folders '/a' and '/a/b'.
164 // Returns true if creation is successful or if the path already exists.
165 bool CreateFullPath(std::string_view fullPath);
166
167 enum class IfAbsentBehavior
168 {
169 ConsoleWarning,
170 NoConsoleWarning
171 };
172
173 // Deletes a given filename, return true on success
174 // Doesn't supports deleting a directory
175 bool Delete(const std::string& filename,
176 IfAbsentBehavior behavior = IfAbsentBehavior::ConsoleWarning);
177
178 // Deletes a directory filename, returns true on success
179 bool DeleteDir(const std::string& filename,
180 IfAbsentBehavior behavior = IfAbsentBehavior::ConsoleWarning);
181
182 // renames file srcFilename to destFilename, returns true on success
183 bool Rename(const std::string& srcFilename, const std::string& destFilename);
184
185 // ditto, but syncs the source file and, on Unix, syncs the directories after rename
186 bool RenameSync(const std::string& srcFilename, const std::string& destFilename);
187
188 // Copies a file at source_path to destination_path, as if by std::filesystem::copy_file().
189 // If a file already exists at destination_path it is overwritten. Returns true on success.
190 bool CopyRegularFile(std::string_view source_path, std::string_view destination_path);
191
192 // creates an empty file filename, returns true on success
193 bool CreateEmptyFile(const std::string& filename);
194
195 // Recursive or non-recursive list of files and directories under directory.
196 FSTEntry ScanDirectoryTree(const std::string& directory, bool recursive);
197
198 // deletes the given directory and anything under it. Returns true on success.
199 bool DeleteDirRecursively(const std::string& directory);
200
201 // Returns the current directory
202 std::string GetCurrentDir();
203
204 // Copies source_path to dest_path, as if by std::filesystem::copy(). Returns true on success or if
205 // the source and destination are already the same (as determined by std::filesystem::equivalent()).
206 bool Copy(std::string_view source_path, std::string_view dest_path,
207 bool overwrite_existing = false);
208
209 // Moves source_path to dest_path. On success, the source_path will no longer exist, and the
210 // dest_path will contain the data previously in source_path. Files in dest_path will be overwritten
211 // if they match files in source_path, but files that only exist in dest_path will be kept. No
212 // guarantee on the state is given on failure; the move may have completely failed or partially
213 // completed.
214 bool MoveWithOverwrite(std::string_view source_path, std::string_view dest_path);
215
216 // Set the current directory to given directory
217 bool SetCurrentDir(const std::string& directory);
218
219 // Creates and returns the path to a new temporary directory.
220 std::string CreateTempDir();
221
222 // Get a filename that can hopefully be atomically renamed to the given path.
223 std::string GetTempFilenameForAtomicWrite(std::string path);
224
225 // Gets a set user directory path
226 // Don't call prior to setting the base user directory
227 const std::string& GetUserPath(unsigned int dir_index);
228
229 // Sets a user directory path
230 // Rebuilds internal directory structure to compensate for the new directory
231 void SetUserPath(unsigned int dir_index, std::string path);
232
233 // probably doesn't belong here
234 std::string GetThemeDir(const std::string& theme_name);
235
236 // Returns the path to where the sys file are
237 const std::string& GetSysDirectory();
238
239 #ifdef ANDROID
240 void SetSysDirectory(const std::string& path);
241 void SetGpuDriverDirectories(const std::string& path, const std::string& lib_path);
242 const std::string GetGpuDriverDirectory(unsigned int dir_index);
243 #endif
244
245 #ifdef __APPLE__
246 std::string GetBundleDirectory();
247 #endif
248
249 std::string GetExePath();
250 std::string GetExeDirectory();
251
252 bool WriteStringToFile(const std::string& filename, std::string_view str);
253 bool ReadFileToString(const std::string& filename, std::string& str);
254
255 // To deal with Windows not fully supporting UTF-8 and Android not fully supporting paths.
256 template <typename T>
257 void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode)
258 {
259 #ifdef _WIN32
260 fstream.open(UTF8ToTStr(filename).c_str(), openmode);
261 #else
262 #ifdef ANDROID
263 // Unfortunately it seems like the non-standard __open is the only way to use a file descriptor
264 if (IsPathAndroidContent(filename))
265 fstream.__open(OpenAndroidContent(filename, OpenModeToAndroid(openmode)), openmode);
266 else
267 #endif
268 fstream.open(filename.c_str(), openmode);
269 #endif
270 }
271
272 } // namespace File