From c8e64da4eb331c5ea623f4973aae4e389cdcb0a6 Mon Sep 17 00:00:00 2001 From: Sandy Carter Date: Thu, 9 Apr 2020 10:09:18 -0400 Subject: [PATCH] filesystem: use std for PathExists Remove custom platform implementation of `PathExists` and replace uses with `std::filesystem::exists`. --- src/xenia/app/emulator_window.cc | 2 +- src/xenia/app/xenia_main.cc | 2 +- src/xenia/base/filesystem.cc | 2 +- src/xenia/base/filesystem.h | 3 --- src/xenia/base/filesystem_posix.cc | 5 ----- src/xenia/base/filesystem_win.cc | 7 +------ src/xenia/config.cc | 8 ++++---- src/xenia/hid/sdl/sdl_input_driver.cc | 2 +- src/xenia/kernel/xam/content_manager.cc | 14 +++++++------- src/xenia/ui/imgui_drawer.cc | 3 +-- src/xenia/vfs/devices/host_path_device.cc | 2 +- src/xenia/vfs/devices/stfs_container_device.cc | 9 +++++---- 12 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/xenia/app/emulator_window.cc b/src/xenia/app/emulator_window.cc index fe7c38a92..8ff73e83f 100644 --- a/src/xenia/app/emulator_window.cc +++ b/src/xenia/app/emulator_window.cc @@ -351,7 +351,7 @@ void EmulatorWindow::ShowContentDirectory() { target_path = package_root; } - if (!xe::filesystem::PathExists(target_path)) { + if (!std::filesystem::exists(target_path)) { xe::filesystem::CreateFolder(target_path); } diff --git a/src/xenia/app/xenia_main.cc b/src/xenia/app/xenia_main.cc index a341bc427..2efe2b358 100644 --- a/src/xenia/app/xenia_main.cc +++ b/src/xenia/app/xenia_main.cc @@ -215,7 +215,7 @@ int xenia_main(const std::vector& args) { std::filesystem::path storage_root = cvars::storage_root; if (storage_root.empty()) { storage_root = xe::filesystem::GetExecutableFolder(); - if (!xe::filesystem::PathExists(storage_root / "portable.txt")) { + if (!std::filesystem::exists(storage_root / "portable.txt")) { storage_root = xe::filesystem::GetUserFolder(); #if defined(XE_PLATFORM_WIN32) || defined(XE_PLATFORM_LINUX) storage_root = storage_root / "Xenia"; diff --git a/src/xenia/base/filesystem.cc b/src/xenia/base/filesystem.cc index c141720fa..861b81951 100644 --- a/src/xenia/base/filesystem.cc +++ b/src/xenia/base/filesystem.cc @@ -17,7 +17,7 @@ namespace filesystem { bool CreateParentFolder(const std::filesystem::path& path) { if (path.has_parent_path()) { auto parent_path = path.parent_path(); - if (!PathExists(parent_path)) { + if (!std::filesystem::exists(parent_path)) { return CreateFolder(parent_path); } } diff --git a/src/xenia/base/filesystem.h b/src/xenia/base/filesystem.h index 90a314b45..5da5ad941 100644 --- a/src/xenia/base/filesystem.h +++ b/src/xenia/base/filesystem.h @@ -36,9 +36,6 @@ std::filesystem::path GetExecutableFolder(); // Get user folder. std::filesystem::path GetUserFolder(); -// Returns true of the specified path exists as either a directory or file. -bool PathExists(const std::filesystem::path& path); - // Creates the parent folder of the specified path if needed. // This can be used to ensure the destination path for a new file exists before // attempting to create it. diff --git a/src/xenia/base/filesystem_posix.cc b/src/xenia/base/filesystem_posix.cc index 7f19332a7..44cdeff13 100644 --- a/src/xenia/base/filesystem_posix.cc +++ b/src/xenia/base/filesystem_posix.cc @@ -77,11 +77,6 @@ std::filesystem::path GetUserFolder() { return std::filesystem::path(home) / ".local" / "share"; } -bool PathExists(const std::filesystem::path& path) { - struct stat st; - return stat(path.c_str(), &st) == 0; -} - FILE* OpenFile(const std::filesystem::path& path, const std::string_view mode) { return fopen(path.c_str(), std::string(mode).c_str()); } diff --git a/src/xenia/base/filesystem_win.cc b/src/xenia/base/filesystem_win.cc index 3242583fb..7910d6ff4 100644 --- a/src/xenia/base/filesystem_win.cc +++ b/src/xenia/base/filesystem_win.cc @@ -61,18 +61,13 @@ std::filesystem::path GetUserFolder() { return result; } -bool PathExists(const std::filesystem::path& path) { - DWORD attrib = GetFileAttributes(path.c_str()); - return attrib != INVALID_FILE_ATTRIBUTES; -} - bool CreateFolder(const std::filesystem::path& path) { std::filesystem::path create_path; for (auto it = path.begin(); it != path.end(); ++it) { create_path /= *it; CreateDirectoryW(create_path.c_str(), nullptr); } - return PathExists(path); + return std::filesystem::exists(path); } bool DeleteFolder(const std::filesystem::path& path) { diff --git a/src/xenia/config.cc b/src/xenia/config.cc index 99cd1be7f..542344e8c 100644 --- a/src/xenia/config.cc +++ b/src/xenia/config.cc @@ -114,7 +114,7 @@ void SaveConfig() { output << fmt::format("\t# {}\n", config_var->description()); } - if (xe::filesystem::PathExists(config_path)) { + if (std::filesystem::exists(config_path)) { std::ifstream existingConfigStream(config_path); const std::string existingConfig( (std::istreambuf_iterator(existingConfigStream)), @@ -136,7 +136,7 @@ void SetupConfig(const std::filesystem::path& config_folder) { // check if the user specified a specific config to load if (!cvars::config.empty()) { config_path = xe::to_path(cvars::config); - if (xe::filesystem::PathExists(config_path)) { + if (std::filesystem::exists(config_path)) { ReadConfig(config_path); return; } @@ -145,7 +145,7 @@ void SetupConfig(const std::filesystem::path& config_folder) { // let's also load the default config if (!config_folder.empty()) { config_path = config_folder / config_name; - if (xe::filesystem::PathExists(config_path)) { + if (std::filesystem::exists(config_path)) { ReadConfig(config_path); } // we only want to save the config if the user is using the default @@ -158,7 +158,7 @@ void LoadGameConfig(const std::string_view title_id) { const auto game_config_folder = config_folder / "config"; const auto game_config_path = game_config_folder / (std::string(title_id) + game_config_suffix); - if (xe::filesystem::PathExists(game_config_path)) { + if (std::filesystem::exists(game_config_path)) { ReadGameConfig(game_config_path); } } diff --git a/src/xenia/hid/sdl/sdl_input_driver.cc b/src/xenia/hid/sdl/sdl_input_driver.cc index e2e1bca5a..00d69d8a4 100644 --- a/src/xenia/hid/sdl/sdl_input_driver.cc +++ b/src/xenia/hid/sdl/sdl_input_driver.cc @@ -121,7 +121,7 @@ X_STATUS SDLInputDriver::Setup() { sdl_gamecontroller_initialized_ = true; if (!cvars::mappings_file.empty()) { - if (!filesystem::PathExists(cvars::mappings_file)) { + if (!std::filesystem::exists(cvars::mappings_file)) { XELOGW("SDL GameControllerDB: file '{}' does not exist.", xe::path_to_utf8(cvars::mappings_file)); } else { diff --git a/src/xenia/kernel/xam/content_manager.cc b/src/xenia/kernel/xam/content_manager.cc index a62fd31e1..7a6357a38 100644 --- a/src/xenia/kernel/xam/content_manager.cc +++ b/src/xenia/kernel/xam/content_manager.cc @@ -122,7 +122,7 @@ std::vector ContentManager::ListContent(uint32_t device_id, std::unique_ptr ContentManager::ResolvePackage( const std::string_view root_name, const XCONTENT_DATA& data) { auto package_path = ResolvePackagePath(data); - if (!xe::filesystem::PathExists(package_path)) { + if (!std::filesystem::exists(package_path)) { return nullptr; } @@ -135,7 +135,7 @@ std::unique_ptr ContentManager::ResolvePackage( bool ContentManager::ContentExists(const XCONTENT_DATA& data) { auto path = ResolvePackagePath(data); - return xe::filesystem::PathExists(path); + return std::filesystem::exists(path); } X_RESULT ContentManager::CreateContent(const std::string_view root_name, @@ -148,7 +148,7 @@ X_RESULT ContentManager::CreateContent(const std::string_view root_name, } auto package_path = ResolvePackagePath(data); - if (xe::filesystem::PathExists(package_path)) { + if (std::filesystem::exists(package_path)) { // Exists, must not! return X_ERROR_ALREADY_EXISTS; } @@ -175,7 +175,7 @@ X_RESULT ContentManager::OpenContent(const std::string_view root_name, } auto package_path = ResolvePackagePath(data); - if (!xe::filesystem::PathExists(package_path)) { + if (!std::filesystem::exists(package_path)) { // Does not exist, must be created. return X_ERROR_FILE_NOT_FOUND; } @@ -209,7 +209,7 @@ X_RESULT ContentManager::GetContentThumbnail(const XCONTENT_DATA& data, auto global_lock = global_critical_region_.Acquire(); auto package_path = ResolvePackagePath(data); auto thumb_path = package_path / kThumbnailFileName; - if (xe::filesystem::PathExists(thumb_path)) { + if (std::filesystem::exists(thumb_path)) { auto file = xe::filesystem::OpenFile(thumb_path, "rb"); fseek(file, 0, SEEK_END); size_t file_len = ftell(file); @@ -228,7 +228,7 @@ X_RESULT ContentManager::SetContentThumbnail(const XCONTENT_DATA& data, auto global_lock = global_critical_region_.Acquire(); auto package_path = ResolvePackagePath(data); xe::filesystem::CreateFolder(package_path); - if (xe::filesystem::PathExists(package_path)) { + if (std::filesystem::exists(package_path)) { auto thumb_path = package_path / kThumbnailFileName; auto file = xe::filesystem::OpenFile(thumb_path, "wb"); fwrite(buffer.data(), 1, buffer.size(), file); @@ -243,7 +243,7 @@ X_RESULT ContentManager::DeleteContent(const XCONTENT_DATA& data) { auto global_lock = global_critical_region_.Acquire(); auto package_path = ResolvePackagePath(data); - if (xe::filesystem::PathExists(package_path)) { + if (std::filesystem::exists(package_path)) { xe::filesystem::DeleteFolder(package_path); return X_ERROR_SUCCESS; } else { diff --git a/src/xenia/ui/imgui_drawer.cc b/src/xenia/ui/imgui_drawer.cc index 2027512e3..6749fb627 100644 --- a/src/xenia/ui/imgui_drawer.cc +++ b/src/xenia/ui/imgui_drawer.cc @@ -11,7 +11,6 @@ #include "third_party/imgui/imgui.h" #include "xenia/base/assert.h" -#include "xenia/base/filesystem.h" #include "xenia/base/logging.h" #include "xenia/ui/window.h" @@ -136,7 +135,7 @@ void ImGuiDrawer::SetupFont() { // TODO(benvanik): jp font on other platforms? // https://github.com/Koruri/kibitaki looks really good, but is 1.5MiB. const char* jp_font_path = "C:\\Windows\\Fonts\\msgothic.ttc"; - if (xe::filesystem::PathExists(jp_font_path)) { + if (std::filesystem::exists(jp_font_path)) { ImFontConfig jp_font_config; jp_font_config.MergeMode = true; jp_font_config.OversampleH = jp_font_config.OversampleV = 1; diff --git a/src/xenia/vfs/devices/host_path_device.cc b/src/xenia/vfs/devices/host_path_device.cc index 04b43bd66..ad77614c3 100644 --- a/src/xenia/vfs/devices/host_path_device.cc +++ b/src/xenia/vfs/devices/host_path_device.cc @@ -26,7 +26,7 @@ HostPathDevice::HostPathDevice(const std::string_view mount_path, HostPathDevice::~HostPathDevice() = default; bool HostPathDevice::Initialize() { - if (!xe::filesystem::PathExists(host_path_)) { + if (!std::filesystem::exists(host_path_)) { if (!read_only_) { // Create the path. xe::filesystem::CreateFolder(host_path_); diff --git a/src/xenia/vfs/devices/stfs_container_device.cc b/src/xenia/vfs/devices/stfs_container_device.cc index f3aea474d..967c616fa 100644 --- a/src/xenia/vfs/devices/stfs_container_device.cc +++ b/src/xenia/vfs/devices/stfs_container_device.cc @@ -61,13 +61,14 @@ StfsContainerDevice::~StfsContainerDevice() = default; bool StfsContainerDevice::Initialize() { // Resolve a valid STFS file if a directory is given. - if (std::filesystem::is_directory(host_path_) && !ResolveFromFolder(host_path_)) { + if (std::filesystem::is_directory(host_path_) && + !ResolveFromFolder(host_path_)) { XELOGE("Could not resolve an STFS container given path {}", xe::path_to_utf8(host_path_)); return false; } - if (!filesystem::PathExists(host_path_)) { + if (!std::filesystem::exists(host_path_)) { XELOGE("Path to STFS container does not exist: {}", xe::path_to_utf8(host_path_)); return false; @@ -120,7 +121,7 @@ StfsContainerDevice::Error StfsContainerDevice::MapFiles() { // If the STFS package is multi-file, it is an SVOD system. We need to map // the files in the .data folder and can discard the header. auto data_fragment_path = host_path_ / ".data"; - if (!filesystem::PathExists(data_fragment_path)) { + if (!std::filesystem::exists(data_fragment_path)) { XELOGE("STFS container is multi-file, but path {} does not exist.", xe::path_to_utf8(data_fragment_path)); return Error::kErrorFileMismatch; @@ -778,4 +779,4 @@ bool StfsContainerDevice::ResolveFromFolder(const std::filesystem::path& path) { } } // namespace vfs -} // namespace xe \ No newline at end of file +} // namespace xe