summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Zach Hilman2018-08-06 23:21:37 -0400
committerGravatar Zach Hilman2018-08-08 21:18:45 -0400
commit2b6128fe0b8788318a4bbe1fc55ea14aed2981e4 (patch)
tree39a8c25dbea98e2f8b5761f408d62cf1669f89bc
parentloader: Remove unused IdentifyFile overload (diff)
downloadyuzu-2b6128fe0b8788318a4bbe1fc55ea14aed2981e4.tar.gz
yuzu-2b6128fe0b8788318a4bbe1fc55ea14aed2981e4.tar.xz
yuzu-2b6128fe0b8788318a4bbe1fc55ea14aed2981e4.zip
file_util: Use enum instead of bool for specifing path behavior
Diffstat (limited to '')
-rw-r--r--src/common/file_util.cpp8
-rw-r--r--src/common/file_util.h7
-rw-r--r--src/core/file_sys/vfs_real.cpp44
-rw-r--r--src/yuzu/game_list_p.h2
4 files changed, 37 insertions, 24 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 190cac6d9..3ce590062 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -884,12 +884,12 @@ std::string_view RemoveTrailingSlash(std::string_view path) {
884 return path; 884 return path;
885} 885}
886 886
887std::string SanitizePath(std::string_view path_, bool with_platform_slashes) { 887std::string SanitizePath(std::string_view path_, DirectorySeparator directory_separator) {
888 std::string path(path_); 888 std::string path(path_);
889 char type1 = '\\'; 889 char type1 = directory_separator == DirectorySeparator::BackwardSlash ? '/' : '\\';
890 char type2 = '/'; 890 char type2 = directory_separator == DirectorySeparator::BackwardSlash ? '\\' : '/';
891 891
892 if (with_platform_slashes) { 892 if (directory_separator == DirectorySeparator::PlatformDefault) {
893#ifdef _WIN32 893#ifdef _WIN32
894 type1 = '/'; 894 type1 = '/';
895 type2 = '\\'; 895 type2 = '\\';
diff --git a/src/common/file_util.h b/src/common/file_util.h
index ca63d7466..2711872ae 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -182,9 +182,12 @@ std::vector<T> SliceVector(const std::vector<T>& vector, size_t first, size_t la
182 return std::vector<T>(vector.begin() + first, vector.begin() + first + last); 182 return std::vector<T>(vector.begin() + first, vector.begin() + first + last);
183} 183}
184 184
185enum class DirectorySeparator { ForwardSlash, BackwardSlash, PlatformDefault };
186
185// Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\' 187// Removes trailing slash, makes all '\\' into '/', and removes duplicate '/'. Makes '/' into '\\'
186// if windows and with_platform_slashes is true. 188// depending if directory_separator is BackwardSlash or PlatformDefault and running on windows
187std::string SanitizePath(std::string_view path, bool with_platform_slashes = false); 189std::string SanitizePath(std::string_view path,
190 DirectorySeparator directory_separator = DirectorySeparator::ForwardSlash);
188 191
189// simple wrapper for cstdlib file functions to 192// simple wrapper for cstdlib file functions to
190// hopefully will make error checking easier 193// hopefully will make error checking easier
diff --git a/src/core/file_sys/vfs_real.cpp b/src/core/file_sys/vfs_real.cpp
index 21ea35aaf..1b5919737 100644
--- a/src/core/file_sys/vfs_real.cpp
+++ b/src/core/file_sys/vfs_real.cpp
@@ -53,7 +53,7 @@ bool RealVfsFilesystem::IsWritable() const {
53} 53}
54 54
55VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const { 55VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const {
56 const auto path = FileUtil::SanitizePath(path_, true); 56 const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
57 if (!FileUtil::Exists(path)) 57 if (!FileUtil::Exists(path))
58 return VfsEntryType::None; 58 return VfsEntryType::None;
59 if (FileUtil::IsDirectory(path)) 59 if (FileUtil::IsDirectory(path))
@@ -63,7 +63,7 @@ VfsEntryType RealVfsFilesystem::GetEntryType(std::string_view path_) const {
63} 63}
64 64
65VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) { 65VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
66 const auto path = FileUtil::SanitizePath(path_, true); 66 const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
67 if (cache.find(path) != cache.end()) { 67 if (cache.find(path) != cache.end()) {
68 auto weak = cache[path]; 68 auto weak = cache[path];
69 if (!weak.expired()) { 69 if (!weak.expired()) {
@@ -82,15 +82,17 @@ VirtualFile RealVfsFilesystem::OpenFile(std::string_view path_, Mode perms) {
82} 82}
83 83
84VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) { 84VirtualFile RealVfsFilesystem::CreateFile(std::string_view path_, Mode perms) {
85 const auto path = FileUtil::SanitizePath(path_, true); 85 const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
86 if (!FileUtil::Exists(path) && !FileUtil::CreateEmptyFile(path)) 86 if (!FileUtil::Exists(path) && !FileUtil::CreateEmptyFile(path))
87 return nullptr; 87 return nullptr;
88 return OpenFile(path, perms); 88 return OpenFile(path, perms);
89} 89}
90 90
91VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) { 91VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_view new_path_) {
92 const auto old_path = FileUtil::SanitizePath(old_path_, true); 92 const auto old_path =
93 const auto new_path = FileUtil::SanitizePath(new_path_, true); 93 FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
94 const auto new_path =
95 FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
94 96
95 if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || 97 if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
96 FileUtil::IsDirectory(old_path) || !FileUtil::Copy(old_path, new_path)) 98 FileUtil::IsDirectory(old_path) || !FileUtil::Copy(old_path, new_path))
@@ -99,8 +101,10 @@ VirtualFile RealVfsFilesystem::CopyFile(std::string_view old_path_, std::string_
99} 101}
100 102
101VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) { 103VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_view new_path_) {
102 const auto old_path = FileUtil::SanitizePath(old_path_, true); 104 const auto old_path =
103 const auto new_path = FileUtil::SanitizePath(new_path_, true); 105 FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
106 const auto new_path =
107 FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
104 108
105 if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || 109 if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
106 FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path)) 110 FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path))
@@ -119,7 +123,7 @@ VirtualFile RealVfsFilesystem::MoveFile(std::string_view old_path_, std::string_
119} 123}
120 124
121bool RealVfsFilesystem::DeleteFile(std::string_view path_) { 125bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
122 const auto path = FileUtil::SanitizePath(path_, true); 126 const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
123 if (cache.find(path) != cache.end()) { 127 if (cache.find(path) != cache.end()) {
124 if (!cache[path].expired()) 128 if (!cache[path].expired())
125 cache[path].lock()->Close(); 129 cache[path].lock()->Close();
@@ -129,13 +133,13 @@ bool RealVfsFilesystem::DeleteFile(std::string_view path_) {
129} 133}
130 134
131VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) { 135VirtualDir RealVfsFilesystem::OpenDirectory(std::string_view path_, Mode perms) {
132 const auto path = FileUtil::SanitizePath(path_, true); 136 const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
133 // Cannot use make_shared as RealVfsDirectory constructor is private 137 // Cannot use make_shared as RealVfsDirectory constructor is private
134 return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms)); 138 return std::shared_ptr<RealVfsDirectory>(new RealVfsDirectory(*this, path, perms));
135} 139}
136 140
137VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) { 141VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms) {
138 const auto path = FileUtil::SanitizePath(path_, true); 142 const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
139 if (!FileUtil::Exists(path) && !FileUtil::CreateDir(path)) 143 if (!FileUtil::Exists(path) && !FileUtil::CreateDir(path))
140 return nullptr; 144 return nullptr;
141 // Cannot use make_shared as RealVfsDirectory constructor is private 145 // Cannot use make_shared as RealVfsDirectory constructor is private
@@ -144,8 +148,10 @@ VirtualDir RealVfsFilesystem::CreateDirectory(std::string_view path_, Mode perms
144 148
145VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_, 149VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_,
146 std::string_view new_path_) { 150 std::string_view new_path_) {
147 const auto old_path = FileUtil::SanitizePath(old_path_, true); 151 const auto old_path =
148 const auto new_path = FileUtil::SanitizePath(new_path_, true); 152 FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
153 const auto new_path =
154 FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
149 if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || 155 if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
150 !FileUtil::IsDirectory(old_path)) 156 !FileUtil::IsDirectory(old_path))
151 return nullptr; 157 return nullptr;
@@ -155,8 +161,10 @@ VirtualDir RealVfsFilesystem::CopyDirectory(std::string_view old_path_,
155 161
156VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_, 162VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
157 std::string_view new_path_) { 163 std::string_view new_path_) {
158 const auto old_path = FileUtil::SanitizePath(old_path_, true); 164 const auto old_path =
159 const auto new_path = FileUtil::SanitizePath(new_path_, true); 165 FileUtil::SanitizePath(old_path_, FileUtil::DirectorySeparator::PlatformDefault);
166 const auto new_path =
167 FileUtil::SanitizePath(new_path_, FileUtil::DirectorySeparator::PlatformDefault);
160 if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) || 168 if (!FileUtil::Exists(old_path) || FileUtil::Exists(new_path) ||
161 FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path)) 169 FileUtil::IsDirectory(old_path) || !FileUtil::Rename(old_path, new_path))
162 return nullptr; 170 return nullptr;
@@ -164,9 +172,11 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
164 for (auto& kv : cache) { 172 for (auto& kv : cache) {
165 // Path in cache starts with old_path 173 // Path in cache starts with old_path
166 if (kv.first.rfind(old_path, 0) == 0) { 174 if (kv.first.rfind(old_path, 0) == 0) {
167 const auto file_old_path = FileUtil::SanitizePath(kv.first, true); 175 const auto file_old_path =
176 FileUtil::SanitizePath(kv.first, FileUtil::DirectorySeparator::PlatformDefault);
168 const auto file_new_path = 177 const auto file_new_path =
169 FileUtil::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()), true); 178 FileUtil::SanitizePath(new_path + DIR_SEP + kv.first.substr(old_path.size()),
179 FileUtil::DirectorySeparator::PlatformDefault);
170 auto cached = cache[file_old_path]; 180 auto cached = cache[file_old_path];
171 if (!cached.expired()) { 181 if (!cached.expired()) {
172 auto file = cached.lock(); 182 auto file = cached.lock();
@@ -181,7 +191,7 @@ VirtualDir RealVfsFilesystem::MoveDirectory(std::string_view old_path_,
181} 191}
182 192
183bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) { 193bool RealVfsFilesystem::DeleteDirectory(std::string_view path_) {
184 const auto path = FileUtil::SanitizePath(path_, true); 194 const auto path = FileUtil::SanitizePath(path_, FileUtil::DirectorySeparator::PlatformDefault);
185 for (auto& kv : cache) { 195 for (auto& kv : cache) {
186 // Path in cache starts with old_path 196 // Path in cache starts with old_path
187 if (kv.first.rfind(path, 0) == 0) { 197 if (kv.first.rfind(path, 0) == 0) {
diff --git a/src/yuzu/game_list_p.h b/src/yuzu/game_list_p.h
index 49a3f6181..114a0fc7f 100644
--- a/src/yuzu/game_list_p.h
+++ b/src/yuzu/game_list_p.h
@@ -140,7 +140,7 @@ class GameListWorker : public QObject, public QRunnable {
140 140
141public: 141public:
142 GameListWorker(FileSys::VirtualFilesystem vfs, QString dir_path, bool deep_scan) 142 GameListWorker(FileSys::VirtualFilesystem vfs, QString dir_path, bool deep_scan)
143 : dir_path(std::move(dir_path)), deep_scan(deep_scan) {} 143 : vfs(std::move(vfs)), dir_path(std::move(dir_path)), deep_scan(deep_scan) {}
144 144
145public slots: 145public slots:
146 /// Starts the processing of directory tree information. 146 /// Starts the processing of directory tree information.