summaryrefslogtreecommitdiff
path: root/src/common/file_util.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/common/file_util.cpp61
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!
77namespace Common::FS { 78namespace Common::FS {
79namespace fs = std::filesystem;
78 80
79// Remove any ending forward slashes from directory paths 81bool Exists(const fs::path& path) {
80// Modifies argument. 82 std::error_code ec;
81static 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
93bool Exists(const std::string& filename) { 86bool 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
112bool 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
136bool Delete(const std::string& filename) { 91bool Delete(const std::string& filename) {