diff options
| author | 2020-12-08 18:36:49 -0500 | |
|---|---|---|
| committer | 2020-12-08 18:36:53 -0500 | |
| commit | 0e54aa17e64a5c5b62d60a70414742be29eccff9 (patch) | |
| tree | 13c55de6081891fa19afe4526fa83328520c9026 /src/common/file_util.cpp | |
| parent | Merge pull request #5171 from lat9nq/ci-unicorn-cleanup (diff) | |
| download | yuzu-0e54aa17e64a5c5b62d60a70414742be29eccff9.tar.gz yuzu-0e54aa17e64a5c5b62d60a70414742be29eccff9.tar.xz yuzu-0e54aa17e64a5c5b62d60a70414742be29eccff9.zip | |
file_util: Migrate Exists() and IsDirectory() over to std::filesystem
Greatly simplifies our file-handling code for these functions.
Diffstat (limited to '')
| -rw-r--r-- | src/common/file_util.cpp | 61 |
1 files changed, 8 insertions, 53 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 18fbfa25b..67fcef3e4 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <array> | 5 | #include <array> |
| 6 | #include <filesystem> | ||
| 6 | #include <limits> | 7 | #include <limits> |
| 7 | #include <memory> | 8 | #include <memory> |
| 8 | #include <sstream> | 9 | #include <sstream> |
| @@ -75,62 +76,16 @@ | |||
| 75 | // The code still needs a ton of cleanup. | 76 | // The code still needs a ton of cleanup. |
| 76 | // REMEMBER: strdup considered harmful! | 77 | // REMEMBER: strdup considered harmful! |
| 77 | namespace Common::FS { | 78 | namespace Common::FS { |
| 79 | namespace fs = std::filesystem; | ||
| 78 | 80 | ||
| 79 | // Remove any ending forward slashes from directory paths | 81 | bool Exists(const fs::path& path) { |
| 80 | // Modifies argument. | 82 | std::error_code ec; |
| 81 | static void StripTailDirSlashes(std::string& fname) { | 83 | return fs::exists(path, ec); |
| 82 | if (fname.length() <= 1) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | |||
| 86 | std::size_t i = fname.length(); | ||
| 87 | while (i > 0 && fname[i - 1] == DIR_SEP_CHR) { | ||
| 88 | --i; | ||
| 89 | } | ||
| 90 | fname.resize(i); | ||
| 91 | } | 84 | } |
| 92 | 85 | ||
| 93 | bool Exists(const std::string& filename) { | 86 | bool IsDirectory(const fs::path& path) { |
| 94 | struct stat file_info; | 87 | std::error_code ec; |
| 95 | 88 | return fs::is_directory(path, ec); | |
| 96 | std::string copy(filename); | ||
| 97 | StripTailDirSlashes(copy); | ||
| 98 | |||
| 99 | #ifdef _WIN32 | ||
| 100 | // Windows needs a slash to identify a driver root | ||
| 101 | if (copy.size() != 0 && copy.back() == ':') | ||
| 102 | copy += DIR_SEP_CHR; | ||
| 103 | |||
| 104 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); | ||
| 105 | #else | ||
| 106 | int result = stat(copy.c_str(), &file_info); | ||
| 107 | #endif | ||
| 108 | |||
| 109 | return (result == 0); | ||
| 110 | } | ||
| 111 | |||
| 112 | bool IsDirectory(const std::string& filename) { | ||
| 113 | struct stat file_info; | ||
| 114 | |||
| 115 | std::string copy(filename); | ||
| 116 | StripTailDirSlashes(copy); | ||
| 117 | |||
| 118 | #ifdef _WIN32 | ||
| 119 | // Windows needs a slash to identify a driver root | ||
| 120 | if (copy.size() != 0 && copy.back() == ':') | ||
| 121 | copy += DIR_SEP_CHR; | ||
| 122 | |||
| 123 | int result = _wstat64(Common::UTF8ToUTF16W(copy).c_str(), &file_info); | ||
| 124 | #else | ||
| 125 | int result = stat(copy.c_str(), &file_info); | ||
| 126 | #endif | ||
| 127 | |||
| 128 | if (result < 0) { | ||
| 129 | LOG_DEBUG(Common_Filesystem, "stat failed on {}: {}", filename, GetLastErrorMsg()); | ||
| 130 | return false; | ||
| 131 | } | ||
| 132 | |||
| 133 | return S_ISDIR(file_info.st_mode); | ||
| 134 | } | 89 | } |
| 135 | 90 | ||
| 136 | bool Delete(const std::string& filename) { | 91 | bool Delete(const std::string& filename) { |