1 // Copyright 2008 Dolphin Emulator Project
2 // Licensed under GPLv2+
3 // Refer to the license.txt file included.
4 
5 #pragma once
6 
7 #include <cstddef>
8 #include <fstream>
9 #include <string>
10 #include <string_view>
11 #include <vector>
12 
13 #include <sys/stat.h>
14 
15 #include "Common/CommonTypes.h"
16 
17 #ifdef _WIN32
18 #include "Common/StringUtil.h"
19 #endif
20 
21 // User directory indices for GetUserPath
22 enum
23 {
24   D_USER_IDX,
25   D_GCUSER_IDX,
26   D_WIIROOT_IDX,          // always points to User/Wii or global user-configured directory
27   D_SESSION_WIIROOT_IDX,  // may point to minimal temporary directory for determinism
28   D_CONFIG_IDX,           // global settings
29   D_GAMESETTINGS_IDX,     // user-specified settings which override both the global and the default
30                           // settings (per game)
31   D_MAPS_IDX,
32   D_CACHE_IDX,
33   D_COVERCACHE_IDX,
34   D_REDUMPCACHE_IDX,
35   D_SHADERCACHE_IDX,
36   D_SHADERS_IDX,
37   D_STATESAVES_IDX,
38   D_SCREENSHOTS_IDX,
39   D_HIRESTEXTURES_IDX,
40   D_DUMP_IDX,
41   D_DUMPFRAMES_IDX,
42   D_DUMPOBJECTS_IDX,
43   D_DUMPAUDIO_IDX,
44   D_DUMPTEXTURES_IDX,
45   D_DUMPDSP_IDX,
46   D_DUMPSSL_IDX,
47   D_LOAD_IDX,
48   D_LOGS_IDX,
49   D_MAILLOGS_IDX,
50   D_THEMES_IDX,
51   D_STYLES_IDX,
52   D_PIPES_IDX,
53   D_MEMORYWATCHER_IDX,
54   D_WFSROOT_IDX,
55   D_BACKUP_IDX,
56   D_RESOURCEPACK_IDX,
57   F_DOLPHINCONFIG_IDX,
58   F_GCPADCONFIG_IDX,
59   F_WIIPADCONFIG_IDX,
60   F_GCKEYBOARDCONFIG_IDX,
61   F_GFXCONFIG_IDX,
62   F_DEBUGGERCONFIG_IDX,
63   F_LOGGERCONFIG_IDX,
64   F_MAINLOG_IDX,
65   F_MEM1DUMP_IDX,
66   F_MEM2DUMP_IDX,
67   F_ARAMDUMP_IDX,
68   F_FAKEVMEMDUMP_IDX,
69   F_GCSRAM_IDX,
70   F_MEMORYWATCHERLOCATIONS_IDX,
71   F_MEMORYWATCHERSOCKET_IDX,
72   F_WIISDCARD_IDX,
73   F_DUALSHOCKUDPCLIENTCONFIG_IDX,
74   NUM_PATH_INDICES
75 };
76 
77 namespace File
78 {
79 // FileSystem tree node/
80 struct FSTEntry
81 {
82   bool isDirectory;
83   u64 size;                  // File length, or for directories, recursive count of children
84   std::string physicalName;  // Name on disk
85   std::string virtualName;   // Name in FST names table
86   std::vector<FSTEntry> children;
87 };
88 
89 // The functions in this class are functionally identical to the standalone functions
90 // below, but if you are going to be calling more than one of the functions using the
91 // same path, creating a single FileInfo object and calling its functions multiple
92 // times is faster than calling standalone functions multiple times.
93 class FileInfo final
94 {
95 public:
96   explicit FileInfo(const std::string& path);
97   explicit FileInfo(const char* path);
98   explicit FileInfo(int fd);
99 
100   // Returns true if the path exists
101   bool Exists() const;
102   // Returns true if the path exists and is a directory
103   bool IsDirectory() const;
104   // Returns true if the path exists and is a file
105   bool IsFile() const;
106   // Returns the size of a file (or returns 0 if the path doesn't refer to a file)
107   u64 GetSize() const;
108 
109 private:
110   struct stat m_stat;
111   bool m_exists;
112 };
113 
114 // Returns true if the path exists
115 bool Exists(const std::string& path);
116 
117 // Returns true if the path exists and is a directory
118 bool IsDirectory(const std::string& path);
119 
120 // Returns true if the path exists and is a file
121 bool IsFile(const std::string& path);
122 
123 // Returns the size of a file (or returns 0 if the path isn't a file that exists)
124 u64 GetSize(const std::string& path);
125 
126 // Overloaded GetSize, accepts file descriptor
127 u64 GetSize(const int fd);
128 
129 // Overloaded GetSize, accepts FILE*
130 u64 GetSize(FILE* f);
131 
132 // Returns true if successful, or path already exists.
133 bool CreateDir(const std::string& filename);
134 
135 // Creates the full path of fullPath returns true on success
136 bool CreateFullPath(const std::string& fullPath);
137 
138 // Deletes a given filename, return true on success
139 // Doesn't supports deleting a directory
140 bool Delete(const std::string& filename);
141 
142 // Deletes a directory filename, returns true on success
143 bool DeleteDir(const std::string& filename);
144 
145 // renames file srcFilename to destFilename, returns true on success
146 bool Rename(const std::string& srcFilename, const std::string& destFilename);
147 
148 // ditto, but syncs the source file and, on Unix, syncs the directories after rename
149 bool RenameSync(const std::string& srcFilename, const std::string& destFilename);
150 
151 // copies file srcFilename to destFilename, returns true on success
152 bool Copy(const std::string& srcFilename, const std::string& destFilename);
153 
154 // creates an empty file filename, returns true on success
155 bool CreateEmptyFile(const std::string& filename);
156 
157 // Recursive or non-recursive list of files and directories under directory.
158 FSTEntry ScanDirectoryTree(const std::string& directory, bool recursive);
159 
160 // deletes the given directory and anything under it. Returns true on success.
161 bool DeleteDirRecursively(const std::string& directory);
162 
163 // Returns the current directory
164 std::string GetCurrentDir();
165 
166 // Create directory and copy contents (optionally overwrites existing files)
167 void CopyDir(const std::string& source_path, const std::string& dest_path,
168              bool destructive = false);
169 
170 // Set the current directory to given directory
171 bool SetCurrentDir(const std::string& directory);
172 
173 // Creates and returns the path to a new temporary directory.
174 std::string CreateTempDir();
175 
176 // Get a filename that can hopefully be atomically renamed to the given path.
177 std::string GetTempFilenameForAtomicWrite(std::string path);
178 
179 // Gets a set user directory path
180 // Don't call prior to setting the base user directory
181 const std::string& GetUserPath(unsigned int dir_index);
182 
183 // Sets a user directory path
184 // Rebuilds internal directory structure to compensate for the new directory
185 void SetUserPath(unsigned int dir_index, const std::string& path);
186 
187 // probably doesn't belong here
188 std::string GetThemeDir(const std::string& theme_name);
189 
190 // Returns the path to where the sys file are
191 std::string GetSysDirectory();
192 
193 #ifdef ANDROID
194 void SetSysDirectory(const std::string& path);
195 #endif
196 
197 #ifdef __APPLE__
198 std::string GetBundleDirectory();
199 #endif
200 
201 std::string GetExePath();
202 std::string GetExeDirectory();
203 
204 bool WriteStringToFile(const std::string& filename, std::string_view str);
205 bool ReadFileToString(const std::string& filename, std::string& str);
206 
207 // To deal with Windows being dumb at unicode:
208 template <typename T>
OpenFStream(T & fstream,const std::string & filename,std::ios_base::openmode openmode)209 void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode)
210 {
211 #ifdef _WIN32
212   fstream.open(UTF8ToTStr(filename).c_str(), openmode);
213 #else
214   fstream.open(filename.c_str(), openmode);
215 #endif
216 }
217 
218 }  // namespace File
219