path_util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <filesystem>
  6. #include <vector>
  7. #include "common/fs/fs_util.h"
  8. namespace Common::FS {
  9. enum class YuzuPath {
  10. YuzuDir, // Where yuzu stores its data.
  11. CacheDir, // Where cached filesystem data is stored.
  12. ConfigDir, // Where config files are stored.
  13. DumpDir, // Where dumped data is stored.
  14. KeysDir, // Where key files are stored.
  15. LoadDir, // Where cheat/mod files are stored.
  16. LogDir, // Where log files are stored.
  17. NANDDir, // Where the emulated NAND is stored.
  18. ScreenshotsDir, // Where yuzu screenshots are stored.
  19. SDMCDir, // Where the emulated SDMC is stored.
  20. ShaderDir, // Where shaders are stored.
  21. };
  22. /**
  23. * Converts a filesystem path to a UTF-8 encoded std::string.
  24. *
  25. * @param path Filesystem path
  26. *
  27. * @returns UTF-8 encoded std::string.
  28. */
  29. [[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path);
  30. /**
  31. * Validates a given path.
  32. *
  33. * A given path is valid if it meets these conditions:
  34. * - The path is not empty
  35. * - The path is not too long
  36. *
  37. * @param path Filesystem path
  38. *
  39. * @returns True if the path is valid, false otherwise.
  40. */
  41. [[nodiscard]] bool ValidatePath(const std::filesystem::path& path);
  42. #ifdef _WIN32
  43. template <typename Path>
  44. [[nodiscard]] bool ValidatePath(const Path& path) {
  45. if constexpr (IsChar<typename Path::value_type>) {
  46. return ValidatePath(ToU8String(path));
  47. } else {
  48. return ValidatePath(std::filesystem::path{path});
  49. }
  50. }
  51. #endif
  52. /**
  53. * Concatenates two filesystem paths together.
  54. *
  55. * This is needed since the following occurs when using std::filesystem::path's operator/:
  56. * first: "/first/path"
  57. * second: "/second/path" (Note that the second path has a directory separator in the front)
  58. * first / second yields "/second/path" when the desired result is first/path/second/path
  59. *
  60. * @param first First filesystem path
  61. * @param second Second filesystem path
  62. *
  63. * @returns A concatenated filesystem path.
  64. */
  65. [[nodiscard]] std::filesystem::path ConcatPath(const std::filesystem::path& first,
  66. const std::filesystem::path& second);
  67. #ifdef _WIN32
  68. template <typename Path1, typename Path2>
  69. [[nodiscard]] std::filesystem::path ConcatPath(const Path1& first, const Path2& second) {
  70. using ValueType1 = typename Path1::value_type;
  71. using ValueType2 = typename Path2::value_type;
  72. if constexpr (IsChar<ValueType1> && IsChar<ValueType2>) {
  73. return ConcatPath(ToU8String(first), ToU8String(second));
  74. } else if constexpr (IsChar<ValueType1> && !IsChar<ValueType2>) {
  75. return ConcatPath(ToU8String(first), second);
  76. } else if constexpr (!IsChar<ValueType1> && IsChar<ValueType2>) {
  77. return ConcatPath(first, ToU8String(second));
  78. } else {
  79. return ConcatPath(std::filesystem::path{first}, std::filesystem::path{second});
  80. }
  81. }
  82. #endif
  83. /**
  84. * Safe variant of ConcatPath that takes in a base path and an offset path from the given base path.
  85. *
  86. * If ConcatPath(base, offset) resolves to a path that is sandboxed within the base path,
  87. * this will return the concatenated path. Otherwise this will return the base path.
  88. *
  89. * @param base Base filesystem path
  90. * @param offset Offset filesystem path
  91. *
  92. * @returns A concatenated filesystem path if it is within the base path,
  93. * returns the base path otherwise.
  94. */
  95. [[nodiscard]] std::filesystem::path ConcatPathSafe(const std::filesystem::path& base,
  96. const std::filesystem::path& offset);
  97. #ifdef _WIN32
  98. template <typename Path1, typename Path2>
  99. [[nodiscard]] std::filesystem::path ConcatPathSafe(const Path1& base, const Path2& offset) {
  100. using ValueType1 = typename Path1::value_type;
  101. using ValueType2 = typename Path2::value_type;
  102. if constexpr (IsChar<ValueType1> && IsChar<ValueType2>) {
  103. return ConcatPathSafe(ToU8String(base), ToU8String(offset));
  104. } else if constexpr (IsChar<ValueType1> && !IsChar<ValueType2>) {
  105. return ConcatPathSafe(ToU8String(base), offset);
  106. } else if constexpr (!IsChar<ValueType1> && IsChar<ValueType2>) {
  107. return ConcatPathSafe(base, ToU8String(offset));
  108. } else {
  109. return ConcatPathSafe(std::filesystem::path{base}, std::filesystem::path{offset});
  110. }
  111. }
  112. #endif
  113. /**
  114. * Checks whether a given path is sandboxed within a given base path.
  115. *
  116. * @param base Base filesystem path
  117. * @param path Filesystem path
  118. *
  119. * @returns True if the given path is sandboxed within the given base path, false otherwise.
  120. */
  121. [[nodiscard]] bool IsPathSandboxed(const std::filesystem::path& base,
  122. const std::filesystem::path& path);
  123. #ifdef _WIN32
  124. template <typename Path1, typename Path2>
  125. [[nodiscard]] bool IsPathSandboxed(const Path1& base, const Path2& path) {
  126. using ValueType1 = typename Path1::value_type;
  127. using ValueType2 = typename Path2::value_type;
  128. if constexpr (IsChar<ValueType1> && IsChar<ValueType2>) {
  129. return IsPathSandboxed(ToU8String(base), ToU8String(path));
  130. } else if constexpr (IsChar<ValueType1> && !IsChar<ValueType2>) {
  131. return IsPathSandboxed(ToU8String(base), path);
  132. } else if constexpr (!IsChar<ValueType1> && IsChar<ValueType2>) {
  133. return IsPathSandboxed(base, ToU8String(path));
  134. } else {
  135. return IsPathSandboxed(std::filesystem::path{base}, std::filesystem::path{path});
  136. }
  137. }
  138. #endif
  139. /**
  140. * Checks if a character is a directory separator (either a forward slash or backslash).
  141. *
  142. * @param character Character
  143. *
  144. * @returns True if the character is a directory separator, false otherwise.
  145. */
  146. [[nodiscard]] bool IsDirSeparator(char character);
  147. /**
  148. * Checks if a character is a directory separator (either a forward slash or backslash).
  149. *
  150. * @param character Character
  151. *
  152. * @returns True if the character is a directory separator, false otherwise.
  153. */
  154. [[nodiscard]] bool IsDirSeparator(char8_t character);
  155. /**
  156. * Removes any trailing directory separators if they exist in the given path.
  157. *
  158. * @param path Filesystem path
  159. *
  160. * @returns The filesystem path without any trailing directory separators.
  161. */
  162. [[nodiscard]] std::filesystem::path RemoveTrailingSeparators(const std::filesystem::path& path);
  163. #ifdef _WIN32
  164. template <typename Path>
  165. [[nodiscard]] std::filesystem::path RemoveTrailingSeparators(const Path& path) {
  166. if constexpr (IsChar<typename Path::value_type>) {
  167. return RemoveTrailingSeparators(ToU8String(path));
  168. } else {
  169. return RemoveTrailingSeparators(std::filesystem::path{path});
  170. }
  171. }
  172. #endif
  173. /**
  174. * Gets the filesystem path associated with the YuzuPath enum.
  175. *
  176. * @param yuzu_path YuzuPath enum
  177. *
  178. * @returns The filesystem path associated with the YuzuPath enum.
  179. */
  180. [[nodiscard]] const std::filesystem::path& GetYuzuPath(YuzuPath yuzu_path);
  181. /**
  182. * Gets the filesystem path associated with the YuzuPath enum as a UTF-8 encoded std::string.
  183. *
  184. * @param yuzu_path YuzuPath enum
  185. *
  186. * @returns The filesystem path associated with the YuzuPath enum as a UTF-8 encoded std::string.
  187. */
  188. [[nodiscard]] std::string GetYuzuPathString(YuzuPath yuzu_path);
  189. /**
  190. * Sets a new filesystem path associated with the YuzuPath enum.
  191. * If the filesystem object at new_path is not a directory, this function will not do anything.
  192. *
  193. * @param yuzu_path YuzuPath enum
  194. * @param new_path New filesystem path
  195. */
  196. void SetYuzuPath(YuzuPath yuzu_path, const std::filesystem::path& new_path);
  197. #ifdef _WIN32
  198. template <typename Path>
  199. [[nodiscard]] void SetYuzuPath(YuzuPath yuzu_path, const Path& new_path) {
  200. if constexpr (IsChar<typename Path::value_type>) {
  201. SetYuzuPath(yuzu_path, ToU8String(new_path));
  202. } else {
  203. SetYuzuPath(yuzu_path, std::filesystem::path{new_path});
  204. }
  205. }
  206. #endif
  207. #ifdef _WIN32
  208. /**
  209. * Gets the path of the directory containing the executable of the current process.
  210. *
  211. * @returns The path of the directory containing the executable of the current process.
  212. */
  213. [[nodiscard]] std::filesystem::path GetExeDirectory();
  214. /**
  215. * Gets the path of the current user's %APPDATA% directory (%USERPROFILE%/AppData/Roaming).
  216. *
  217. * @returns The path of the current user's %APPDATA% directory.
  218. */
  219. [[nodiscard]] std::filesystem::path GetAppDataRoamingDirectory();
  220. #else
  221. /**
  222. * Gets the path of the directory specified by the #HOME environment variable.
  223. * If $HOME is not defined, it will attempt to query the user database in passwd instead.
  224. *
  225. * @returns The path of the current user's home directory.
  226. */
  227. [[nodiscard]] std::filesystem::path GetHomeDirectory();
  228. /**
  229. * Gets the relevant paths for yuzu to store its data based on the given XDG environment variable.
  230. * See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
  231. * Defaults to $HOME/.local/share for main application data,
  232. * $HOME/.cache for cached data, and $HOME/.config for configuration files.
  233. *
  234. * @param env_name XDG environment variable name
  235. *
  236. * @returns The path where yuzu should store its data.
  237. */
  238. [[nodiscard]] std::filesystem::path GetDataDirectory(const std::string& env_name);
  239. #endif
  240. #ifdef __APPLE__
  241. [[nodiscard]] std::filesystem::path GetBundleDirectory();
  242. #endif
  243. // vvvvvvvvvv Deprecated vvvvvvvvvv //
  244. // Removes the final '/' or '\' if one exists
  245. [[nodiscard]] std::string_view RemoveTrailingSlash(std::string_view path);
  246. enum class DirectorySeparator {
  247. ForwardSlash,
  248. BackwardSlash,
  249. PlatformDefault,
  250. };
  251. // Splits the path on '/' or '\' and put the components into a vector
  252. // i.e. "C:\Users\Yuzu\Documents\save.bin" becomes {"C:", "Users", "Yuzu", "Documents", "save.bin" }
  253. [[nodiscard]] std::vector<std::string> SplitPathComponents(std::string_view filename);
  254. // Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\'
  255. // depending if directory_separator is BackwardSlash or PlatformDefault and running on windows
  256. [[nodiscard]] std::string SanitizePath(
  257. std::string_view path,
  258. DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash);
  259. // Gets all of the text up to the last '/' or '\' in the path.
  260. [[nodiscard]] std::string_view GetParentPath(std::string_view path);
  261. // Gets all of the text after the first '/' or '\' in the path.
  262. [[nodiscard]] std::string_view GetPathWithoutTop(std::string_view path);
  263. // Gets the filename of the path
  264. [[nodiscard]] std::string_view GetFilename(std::string_view path);
  265. // Gets the extension of the filename
  266. [[nodiscard]] std::string_view GetExtensionFromFilename(std::string_view name);
  267. } // namespace Common::FS