From 2b52bf4539059106e094832391caf73adc4372fb Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 22 May 2022 13:39:52 +1000 Subject: [PATCH] DEV9: Replace ghc::filesystem with our wrappers --- pcsx2-qt/Settings/DEV9SettingsWidget.cpp | 39 +++++++++----------- pcsx2/DEV9/ATA/ATA.h | 8 ++-- pcsx2/DEV9/ATA/ATA_State.cpp | 32 ++++++++++++---- pcsx2/DEV9/ATA/ATA_Transfer.cpp | 13 +++---- pcsx2/DEV9/ATA/HddCreate.cpp | 47 +++++++++++------------- pcsx2/DEV9/ATA/HddCreate.h | 7 ++-- pcsx2/DEV9/ConfigUI.cpp | 14 +++---- pcsx2/DEV9/DEV9.cpp | 21 ++++------- pcsx2/DEV9/DEV9Config.cpp | 1 - pcsx2/DEV9/pcap_io.cpp | 2 + pcsx2/DEV9/sockets.cpp | 1 + pcsx2/gui/PathUtils.cpp | 9 ----- pcsx2/gui/wxDirName.h | 5 --- 13 files changed, 91 insertions(+), 108 deletions(-) diff --git a/pcsx2-qt/Settings/DEV9SettingsWidget.cpp b/pcsx2-qt/Settings/DEV9SettingsWidget.cpp index 1cec7a6ee4..731019b8f2 100644 --- a/pcsx2-qt/Settings/DEV9SettingsWidget.cpp +++ b/pcsx2-qt/Settings/DEV9SettingsWidget.cpp @@ -19,6 +19,8 @@ #include #include +#include "common/FileSystem.h" +#include "common/Path.h" #include "common/StringUtil.h" #include "pcsx2/HostSettings.h" @@ -662,24 +664,21 @@ void DEV9SettingsWidget::onHddBrowseFileClicked() void DEV9SettingsWidget::onHddFileEdit() { //Check if file exists, if so set HddSize to correct value - //GHC uses UTF8 on all platforms - ghc::filesystem::path hddPath(m_ui.hddFile->text().toUtf8().constData()); - + std::string hddPath(m_ui.hddFile->text().toStdString()); if (hddPath.empty()) return; - if (hddPath.is_relative()) - { - ghc::filesystem::path path(EmuFolders::Settings); - hddPath = path / hddPath; - } + if (!Path::IsAbsolute(hddPath)) + hddPath = Path::Combine(EmuFolders::Settings, hddPath); - if (!ghc::filesystem::exists(hddPath)) + if (!FileSystem::FileExists(hddPath.c_str())) return; - const uintmax_t size = ghc::filesystem::file_size(hddPath); + const s64 size = FileSystem::GetPathFileSize(hddPath.c_str()); + if (size < 0) + return; - const u32 sizeSectors = (size / 512); + const u32 sizeSectors = static_cast(size / 512); const int sizeGB = size / 1024 / 1024 / 1024; QSignalBlocker sb1(m_ui.hddSizeSpinBox); @@ -713,7 +712,7 @@ void DEV9SettingsWidget::onHddSizeSpin(int i) void DEV9SettingsWidget::onHddCreateClicked() { //Do the thing - ghc::filesystem::path hddPath(m_ui.hddFile->text().toUtf8().constData()); + std::string hddPath(m_ui.hddFile->text().toStdString()); u64 sizeBytes = (u64)m_dialog->getEffectiveIntValue("DEV9/Hdd", "HddSizeSectors", 0) * 512; if (sizeBytes == 0 || hddPath.empty()) @@ -724,30 +723,26 @@ void DEV9SettingsWidget::onHddCreateClicked() return; } - if (hddPath.is_relative()) - { - //Note, EmuFolders is still wx strings - ghc::filesystem::path path(EmuFolders::Settings); - hddPath = path / hddPath; - } + if (!Path::IsAbsolute(hddPath)) + hddPath = Path::Combine(EmuFolders::Settings, hddPath); - if (ghc::filesystem::exists(hddPath)) + if (!FileSystem::FileExists(hddPath.c_str())) { //GHC uses UTF8 on all platforms QMessageBox::StandardButton selection = QMessageBox::question(this, tr("Overwrite File?"), tr("HDD image \"%1\" already exists?\n\n" "Do you want to overwrite?") - .arg(QString::fromUtf8(hddPath.u8string().c_str())), + .arg(QString::fromStdString(hddPath)), QMessageBox::Yes | QMessageBox::No); if (selection == QMessageBox::No) return; else - ghc::filesystem::remove(hddPath); + FileSystem::DeleteFilePath(hddPath.c_str()); } HddCreateQt hddCreator(this); - hddCreator.filePath = hddPath; + hddCreator.filePath = std::move(hddPath); hddCreator.neededSize = sizeBytes; hddCreator.Start(); diff --git a/pcsx2/DEV9/ATA/ATA.h b/pcsx2/DEV9/ATA/ATA.h index 08c6ea50f8..a16403ecbe 100644 --- a/pcsx2/DEV9/ATA/ATA.h +++ b/pcsx2/DEV9/ATA/ATA.h @@ -20,9 +20,6 @@ #include #include #include -#include - -#include "ghc/filesystem.h" #include "common/Path.h" #include "DEV9/SimpleQueue.h" @@ -37,7 +34,7 @@ public: private: const bool lba48Supported = false; - std::fstream hddImage; + std::FILE* hddImage = nullptr; u64 hddImageSize; int pioMode; @@ -155,8 +152,9 @@ private: public: ATA(); + ~ATA(); - int Open(ghc::filesystem::path hddPath); + int Open(const std::string& hddPath); void Close(); void ATA_HardReset(); diff --git a/pcsx2/DEV9/ATA/ATA_State.cpp b/pcsx2/DEV9/ATA/ATA_State.cpp index bea1f28d09..555d984266 100644 --- a/pcsx2/DEV9/ATA/ATA_State.cpp +++ b/pcsx2/DEV9/ATA/ATA_State.cpp @@ -16,6 +16,7 @@ #include "PrecompiledHeader.h" #include "common/Assertions.h" +#include "common/FileSystem.h" #include "ATA.h" #include "DEV9/DEV9.h" @@ -30,7 +31,13 @@ ATA::ATA() ResetEnd(true); } -int ATA::Open(ghc::filesystem::path hddPath) +ATA::~ATA() +{ + if (hddImage) + std::fclose(hddImage); +} + +int ATA::Open(const std::string& hddPath) { readBufferLen = 256 * 512; readBuffer = new u8[readBufferLen]; @@ -38,7 +45,7 @@ int ATA::Open(ghc::filesystem::path hddPath) CreateHDDinfo(EmuConfig.DEV9.HddSizeSectors); //Open File - if (!ghc::filesystem::exists(hddPath)) + if (!FileSystem::FileExists(hddPath.c_str())) { #ifndef PCSX2_CORE HddCreateWx hddCreator; @@ -52,11 +59,17 @@ int ATA::Open(ghc::filesystem::path hddPath) return -1; #endif } - hddImage = ghc::filesystem::fstream(hddPath, std::ios::in | std::ios::out | std::ios::binary); + + hddImage = FileSystem::OpenCFile(hddPath.c_str(), "r+b"); + const s64 size = hddImage ? FileSystem::FSize64(hddImage) : -1; + if (!hddImage || size < 0) + { + Console.Error("Failed to open HDD image '%s'", hddPath.c_str()); + return -1; + } //Store HddImage size for later check - hddImage.seekg(0, std::ios::end); - hddImageSize = hddImage.tellg(); + hddImageSize = static_cast(size); { std::lock_guard ioSignallock(ioMutex); @@ -95,8 +108,11 @@ void ATA::Close() } //Close File Handle - if (hddImage.is_open()) - hddImage.close(); + if (hddImage) + { + std::fclose(hddImage); + hddImage = nullptr; + } delete[] readBuffer; readBuffer = nullptr; @@ -289,7 +305,7 @@ void ATA::Write16(u32 addr, u16 value) void ATA::Async(uint cycles) { - if (!hddImage.is_open()) + if (!hddImage) return; if ((regStatus & (ATA_STAT_BUSY | ATA_STAT_DRQ)) == 0 || diff --git a/pcsx2/DEV9/ATA/ATA_Transfer.cpp b/pcsx2/DEV9/ATA/ATA_Transfer.cpp index 07e18f98a4..77d0b52504 100644 --- a/pcsx2/DEV9/ATA/ATA_Transfer.cpp +++ b/pcsx2/DEV9/ATA/ATA_Transfer.cpp @@ -16,6 +16,7 @@ #include "PrecompiledHeader.h" #include "common/Assertions.h" +#include "common/FileSystem.h" #include "ATA.h" #include "DEV9/DEV9.h" @@ -75,14 +76,13 @@ void ATA::IO_Read() } const u64 pos = lba * 512; - hddImage.seekg(pos, std::ios::beg); - if (hddImage.fail()) + if (FileSystem::FSeek64(hddImage, pos, SEEK_SET) != 0 || + std::fread(readBuffer, 512, nsector, hddImage) != static_cast(nsector)) { Console.Error("DEV9: ATA: File read error"); pxAssert(false); abort(); } - hddImage.read((char*)readBuffer, (u64)nsector * 512); { std::lock_guard ioSignallock(ioMutex); ioRead = false; @@ -99,15 +99,14 @@ bool ATA::IO_Write() return false; } - hddImage.seekp(entry.sector * 512, std::ios::beg); - hddImage.write((char*)entry.data, entry.length); - if (hddImage.fail()) + if (FileSystem::FSeek64(hddImage, entry.sector * 512, SEEK_SET) != 0 || + std::fwrite(entry.data, entry.length, 1, hddImage) != 1 || + std::fflush(hddImage) != 0) { Console.Error("DEV9: ATA: File write error"); pxAssert(false); abort(); } - hddImage.flush(); delete[] entry.data; return true; } diff --git a/pcsx2/DEV9/ATA/HddCreate.cpp b/pcsx2/DEV9/ATA/HddCreate.cpp index 61f3388da5..62eaea78b7 100644 --- a/pcsx2/DEV9/ATA/HddCreate.cpp +++ b/pcsx2/DEV9/ATA/HddCreate.cpp @@ -15,7 +15,8 @@ #include "PrecompiledHeader.h" -#include +#include "common/FileSystem.h" + #include #include "HddCreate.h" @@ -26,21 +27,20 @@ void HddCreate::Start() Cleanup(); } -void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes) +void HddCreate::WriteImage(std::string hddPath, u64 reqSizeBytes) { constexpr int buffsize = 4 * 1024; u8 buff[buffsize] = {0}; //4kb - if (ghc::filesystem::exists(hddPath)) + if (FileSystem::FileExists(hddPath.c_str())) { errored.store(true); SetError(); return; } - std::fstream newImage = ghc::filesystem::fstream(hddPath, std::ios::out | std::ios::binary); - - if (newImage.fail()) + auto newImage = FileSystem::OpenManagedCFile(hddPath.c_str(), "wb"); + if (!newImage) { errored.store(true); SetError(); @@ -48,14 +48,15 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes) } //Size file - newImage.seekp(reqSizeBytes - 1, std::ios::beg); const char zero = 0; - newImage.write(&zero, 1); + bool success = FileSystem::FSeek64(newImage.get(), reqSizeBytes - 1, SEEK_SET) == 0; + success = success && std::fwrite(&zero, 1, 1, newImage.get()) == 1; + success = success && FileSystem::FSeek64(newImage.get(), 0, SEEK_SET) == 0; - if (newImage.fail()) + if (!success) { - newImage.close(); - ghc::filesystem::remove(filePath); + newImage.reset(); + FileSystem::DeleteFilePath(filePath.c_str()); errored.store(true); SetError(); return; @@ -63,8 +64,6 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes) lastUpdate = std::chrono::steady_clock::now(); - newImage.seekp(0, std::ios::beg); - //Round up const s32 reqMiB = (reqSizeBytes + ((1024 * 1024) - 1)) / (1024 * 1024); for (s32 iMiB = 0; iMiB < reqMiB; iMiB++) @@ -73,11 +72,10 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes) const s32 req4Kib = std::min(1024, (reqSizeBytes / 1024) - (u64)iMiB * 1024) / 4; for (s32 i4kb = 0; i4kb < req4Kib; i4kb++) { - newImage.write((char*)buff, buffsize); - if (newImage.fail()) + if (std::fwrite(buff, buffsize, 1, newImage.get()) != 1) { - newImage.close(); - ghc::filesystem::remove(filePath); + newImage.reset(); + FileSystem::DeleteFilePath(filePath.c_str()); errored.store(true); SetError(); return; @@ -87,11 +85,10 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes) if (req4Kib != 256) { const s32 remainingBytes = reqSizeBytes - (((u64)iMiB) * (1024 * 1024) + req4Kib * 4096); - newImage.write((char*)buff, remainingBytes); - if (newImage.fail()) + if (std::fwrite(buff, remainingBytes, 1, newImage.get()) != 1) { - newImage.close(); - ghc::filesystem::remove(filePath); + newImage.reset(); + FileSystem::DeleteFilePath(filePath.c_str()); errored.store(true); SetError(); return; @@ -102,19 +99,17 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes) if (std::chrono::duration_cast(now - lastUpdate).count() >= 100 || (iMiB + 1) == reqMiB) { lastUpdate = now; - SetFileProgress(newImage.tellp()); + SetFileProgress(static_cast(FileSystem::FTell64(newImage.get()))); } if (canceled.load()) { - newImage.close(); - ghc::filesystem::remove(filePath); + newImage.reset(); + FileSystem::DeleteFilePath(filePath.c_str()); errored.store(true); SetError(); return; } } - newImage.flush(); - newImage.close(); } void HddCreate::SetFileProgress(u64 currentSize) diff --git a/pcsx2/DEV9/ATA/HddCreate.h b/pcsx2/DEV9/ATA/HddCreate.h index d3b5b868af..0bd21fcbae 100644 --- a/pcsx2/DEV9/ATA/HddCreate.h +++ b/pcsx2/DEV9/ATA/HddCreate.h @@ -17,15 +17,14 @@ #include #include +#include #include "common/Path.h" -#include "ghc/filesystem.h" - class HddCreate { public: - ghc::filesystem::path filePath; + std::string filePath; u64 neededSize; std::atomic_bool errored{false}; @@ -50,5 +49,5 @@ protected: void SetCanceled(); private: - void WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes); + void WriteImage(std::string hddPath, u64 reqSizeBytes); }; diff --git a/pcsx2/DEV9/ConfigUI.cpp b/pcsx2/DEV9/ConfigUI.cpp index 6718261266..2df7d91496 100644 --- a/pcsx2/DEV9/ConfigUI.cpp +++ b/pcsx2/DEV9/ConfigUI.cpp @@ -26,6 +26,8 @@ #include #include +#include "common/FileSystem.h" +#include "common/Path.h" #include "common/StringUtil.h" #include "Config.h" @@ -417,16 +419,12 @@ void DEV9configure() { dialog.Save(g_Conf->EmuOptions.DEV9); - fs::path hddPath(g_Conf->EmuOptions.DEV9.HddFile); + std::string hddPath(g_Conf->EmuOptions.DEV9.HddFile); - if (hddPath.is_relative()) - { - //GHC uses UTF8 on all platforms - ghc::filesystem::path path(EmuFolders::Settings); - hddPath = path / hddPath; - } + if (!Path::IsAbsolute(hddPath)) + hddPath = Path::Combine(EmuFolders::Settings, hddPath); - if (g_Conf->EmuOptions.DEV9.HddEnable && !fs::exists(hddPath)) + if (g_Conf->EmuOptions.DEV9.HddEnable && !FileSystem::FileExists(hddPath.c_str())) { HddCreateWx hddCreator; hddCreator.filePath = hddPath; diff --git a/pcsx2/DEV9/DEV9.cpp b/pcsx2/DEV9/DEV9.cpp index 7018a3b58b..2113c83461 100644 --- a/pcsx2/DEV9/DEV9.cpp +++ b/pcsx2/DEV9/DEV9.cpp @@ -16,6 +16,7 @@ #include "PrecompiledHeader.h" #include "common/Assertions.h" +#include "common/Path.h" #include "common/StringUtil.h" #ifdef _WIN32 @@ -26,6 +27,7 @@ #include #include #include +#include #endif #include @@ -95,23 +97,16 @@ int mapping; bool isRunning = false; -ghc::filesystem::path GetHDDPath() +std::string GetHDDPath() { //GHC uses UTF8 on all platforms - ghc::filesystem::path hddPath(EmuConfig.DEV9.HddFile); + std::string hddPath(EmuConfig.DEV9.HddFile); if (hddPath.empty()) EmuConfig.DEV9.HddEnable = false; - if (hddPath.is_relative()) - { -#ifdef _WIN32 - ghc::filesystem::path path(StringUtil::UTF8StringToWideString(EmuFolders::Settings)); -#else - ghc::filesystem::path path(EmuFolders::Settings); -#endif - hddPath = path / hddPath; - } + if (!Path::IsAbsolute(hddPath)) + hddPath = Path::Combine(EmuFolders::Settings, hddPath); return hddPath; } @@ -211,7 +206,7 @@ s32 DEV9open() #endif DevCon.WriteLn("DEV9: open r+: %s", EmuConfig.DEV9.HddFile.c_str()); - ghc::filesystem::path hddPath = GetHDDPath(); + std::string hddPath(GetHDDPath()); if (EmuConfig.DEV9.HddEnable) { @@ -1075,7 +1070,7 @@ void DEV9CheckChanges(const Pcsx2Config& old_config) //Hdd //Hdd Validate Path - ghc::filesystem::path hddPath = GetHDDPath(); + std::string hddPath(GetHDDPath()); //Hdd Compare with old config if (EmuConfig.DEV9.HddEnable) diff --git a/pcsx2/DEV9/DEV9Config.cpp b/pcsx2/DEV9/DEV9Config.cpp index 76ceeb4822..5cf511177c 100644 --- a/pcsx2/DEV9/DEV9Config.cpp +++ b/pcsx2/DEV9/DEV9Config.cpp @@ -16,7 +16,6 @@ #include "PrecompiledHeader.h" #include "common/StringUtil.h" -#include "ghc/filesystem.h" #include #include "DEV9.h" diff --git a/pcsx2/DEV9/pcap_io.cpp b/pcsx2/DEV9/pcap_io.cpp index 2b2fd80a60..be9cbd2adf 100644 --- a/pcsx2/DEV9/pcap_io.cpp +++ b/pcsx2/DEV9/pcap_io.cpp @@ -28,9 +28,11 @@ #elif defined(__linux__) #include #include +#include #elif defined(__POSIX__) #include #include +#include #endif #include diff --git a/pcsx2/DEV9/sockets.cpp b/pcsx2/DEV9/sockets.cpp index 5f5a48b0c0..4faa316e7d 100644 --- a/pcsx2/DEV9/sockets.cpp +++ b/pcsx2/DEV9/sockets.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef __linux__ #include #endif diff --git a/pcsx2/gui/PathUtils.cpp b/pcsx2/gui/PathUtils.cpp index e5f13ef535..e5b18c1ce9 100644 --- a/pcsx2/gui/PathUtils.cpp +++ b/pcsx2/gui/PathUtils.cpp @@ -184,12 +184,3 @@ wxString Path::GetRootDirectory(const wxString& src) else return wxString(src.begin(), src.begin() + pos); } - -fs::path Path::FromWxString(const wxString& path) -{ -#ifdef _WIN32 - return fs::path(path.ToStdWstring()); -#else - return fs::path(path.ToStdString()); -#endif -} diff --git a/pcsx2/gui/wxDirName.h b/pcsx2/gui/wxDirName.h index 6888e2ae3c..3083ae8f00 100644 --- a/pcsx2/gui/wxDirName.h +++ b/pcsx2/gui/wxDirName.h @@ -19,10 +19,6 @@ #include -#include "ghc/filesystem.h" - -namespace fs = ghc::filesystem; - #define g_MaxPath 255 // 255 is safer with antiquated Win32 ASCII APIs. // -------------------------------------------------------------------------------------- @@ -235,5 +231,4 @@ namespace Path extern wxString GetDirectory(const wxString& src); extern wxString GetFilenameWithoutExt(const wxString& src); extern wxString GetRootDirectory(const wxString& src); - extern fs::path FromWxString(const wxString& path); } // namespace Path