DEV9: Replace ghc::filesystem with our wrappers

This commit is contained in:
Connor McLaughlin 2022-05-22 13:39:52 +10:00 committed by refractionpcsx2
parent 40f315a259
commit 2b52bf4539
13 changed files with 91 additions and 108 deletions

View File

@ -19,6 +19,8 @@
#include <QtWidgets/QFileDialog> #include <QtWidgets/QFileDialog>
#include <algorithm> #include <algorithm>
#include "common/FileSystem.h"
#include "common/Path.h"
#include "common/StringUtil.h" #include "common/StringUtil.h"
#include "pcsx2/HostSettings.h" #include "pcsx2/HostSettings.h"
@ -662,24 +664,21 @@ void DEV9SettingsWidget::onHddBrowseFileClicked()
void DEV9SettingsWidget::onHddFileEdit() void DEV9SettingsWidget::onHddFileEdit()
{ {
//Check if file exists, if so set HddSize to correct value //Check if file exists, if so set HddSize to correct value
//GHC uses UTF8 on all platforms std::string hddPath(m_ui.hddFile->text().toStdString());
ghc::filesystem::path hddPath(m_ui.hddFile->text().toUtf8().constData());
if (hddPath.empty()) if (hddPath.empty())
return; return;
if (hddPath.is_relative()) if (!Path::IsAbsolute(hddPath))
{ hddPath = Path::Combine(EmuFolders::Settings, hddPath);
ghc::filesystem::path path(EmuFolders::Settings);
hddPath = path / hddPath;
}
if (!ghc::filesystem::exists(hddPath)) if (!FileSystem::FileExists(hddPath.c_str()))
return; 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<u32>(size / 512);
const int sizeGB = size / 1024 / 1024 / 1024; const int sizeGB = size / 1024 / 1024 / 1024;
QSignalBlocker sb1(m_ui.hddSizeSpinBox); QSignalBlocker sb1(m_ui.hddSizeSpinBox);
@ -713,7 +712,7 @@ void DEV9SettingsWidget::onHddSizeSpin(int i)
void DEV9SettingsWidget::onHddCreateClicked() void DEV9SettingsWidget::onHddCreateClicked()
{ {
//Do the thing //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; u64 sizeBytes = (u64)m_dialog->getEffectiveIntValue("DEV9/Hdd", "HddSizeSectors", 0) * 512;
if (sizeBytes == 0 || hddPath.empty()) if (sizeBytes == 0 || hddPath.empty())
@ -724,30 +723,26 @@ void DEV9SettingsWidget::onHddCreateClicked()
return; return;
} }
if (hddPath.is_relative()) if (!Path::IsAbsolute(hddPath))
{ hddPath = Path::Combine(EmuFolders::Settings, hddPath);
//Note, EmuFolders is still wx strings
ghc::filesystem::path path(EmuFolders::Settings);
hddPath = path / hddPath;
}
if (ghc::filesystem::exists(hddPath)) if (!FileSystem::FileExists(hddPath.c_str()))
{ {
//GHC uses UTF8 on all platforms //GHC uses UTF8 on all platforms
QMessageBox::StandardButton selection = QMessageBox::StandardButton selection =
QMessageBox::question(this, tr("Overwrite File?"), QMessageBox::question(this, tr("Overwrite File?"),
tr("HDD image \"%1\" already exists?\n\n" tr("HDD image \"%1\" already exists?\n\n"
"Do you want to overwrite?") "Do you want to overwrite?")
.arg(QString::fromUtf8(hddPath.u8string().c_str())), .arg(QString::fromStdString(hddPath)),
QMessageBox::Yes | QMessageBox::No); QMessageBox::Yes | QMessageBox::No);
if (selection == QMessageBox::No) if (selection == QMessageBox::No)
return; return;
else else
ghc::filesystem::remove(hddPath); FileSystem::DeleteFilePath(hddPath.c_str());
} }
HddCreateQt hddCreator(this); HddCreateQt hddCreator(this);
hddCreator.filePath = hddPath; hddCreator.filePath = std::move(hddPath);
hddCreator.neededSize = sizeBytes; hddCreator.neededSize = sizeBytes;
hddCreator.Start(); hddCreator.Start();

View File

@ -20,9 +20,6 @@
#include <atomic> #include <atomic>
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
#include <fstream>
#include "ghc/filesystem.h"
#include "common/Path.h" #include "common/Path.h"
#include "DEV9/SimpleQueue.h" #include "DEV9/SimpleQueue.h"
@ -37,7 +34,7 @@ public:
private: private:
const bool lba48Supported = false; const bool lba48Supported = false;
std::fstream hddImage; std::FILE* hddImage = nullptr;
u64 hddImageSize; u64 hddImageSize;
int pioMode; int pioMode;
@ -155,8 +152,9 @@ private:
public: public:
ATA(); ATA();
~ATA();
int Open(ghc::filesystem::path hddPath); int Open(const std::string& hddPath);
void Close(); void Close();
void ATA_HardReset(); void ATA_HardReset();

View File

@ -16,6 +16,7 @@
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include "common/Assertions.h" #include "common/Assertions.h"
#include "common/FileSystem.h"
#include "ATA.h" #include "ATA.h"
#include "DEV9/DEV9.h" #include "DEV9/DEV9.h"
@ -30,7 +31,13 @@ ATA::ATA()
ResetEnd(true); 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; readBufferLen = 256 * 512;
readBuffer = new u8[readBufferLen]; readBuffer = new u8[readBufferLen];
@ -38,7 +45,7 @@ int ATA::Open(ghc::filesystem::path hddPath)
CreateHDDinfo(EmuConfig.DEV9.HddSizeSectors); CreateHDDinfo(EmuConfig.DEV9.HddSizeSectors);
//Open File //Open File
if (!ghc::filesystem::exists(hddPath)) if (!FileSystem::FileExists(hddPath.c_str()))
{ {
#ifndef PCSX2_CORE #ifndef PCSX2_CORE
HddCreateWx hddCreator; HddCreateWx hddCreator;
@ -52,11 +59,17 @@ int ATA::Open(ghc::filesystem::path hddPath)
return -1; return -1;
#endif #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 //Store HddImage size for later check
hddImage.seekg(0, std::ios::end); hddImageSize = static_cast<u64>(size);
hddImageSize = hddImage.tellg();
{ {
std::lock_guard ioSignallock(ioMutex); std::lock_guard ioSignallock(ioMutex);
@ -95,8 +108,11 @@ void ATA::Close()
} }
//Close File Handle //Close File Handle
if (hddImage.is_open()) if (hddImage)
hddImage.close(); {
std::fclose(hddImage);
hddImage = nullptr;
}
delete[] readBuffer; delete[] readBuffer;
readBuffer = nullptr; readBuffer = nullptr;
@ -289,7 +305,7 @@ void ATA::Write16(u32 addr, u16 value)
void ATA::Async(uint cycles) void ATA::Async(uint cycles)
{ {
if (!hddImage.is_open()) if (!hddImage)
return; return;
if ((regStatus & (ATA_STAT_BUSY | ATA_STAT_DRQ)) == 0 || if ((regStatus & (ATA_STAT_BUSY | ATA_STAT_DRQ)) == 0 ||

View File

@ -16,6 +16,7 @@
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include "common/Assertions.h" #include "common/Assertions.h"
#include "common/FileSystem.h"
#include "ATA.h" #include "ATA.h"
#include "DEV9/DEV9.h" #include "DEV9/DEV9.h"
@ -75,14 +76,13 @@ void ATA::IO_Read()
} }
const u64 pos = lba * 512; const u64 pos = lba * 512;
hddImage.seekg(pos, std::ios::beg); if (FileSystem::FSeek64(hddImage, pos, SEEK_SET) != 0 ||
if (hddImage.fail()) std::fread(readBuffer, 512, nsector, hddImage) != static_cast<size_t>(nsector))
{ {
Console.Error("DEV9: ATA: File read error"); Console.Error("DEV9: ATA: File read error");
pxAssert(false); pxAssert(false);
abort(); abort();
} }
hddImage.read((char*)readBuffer, (u64)nsector * 512);
{ {
std::lock_guard ioSignallock(ioMutex); std::lock_guard ioSignallock(ioMutex);
ioRead = false; ioRead = false;
@ -99,15 +99,14 @@ bool ATA::IO_Write()
return false; return false;
} }
hddImage.seekp(entry.sector * 512, std::ios::beg); if (FileSystem::FSeek64(hddImage, entry.sector * 512, SEEK_SET) != 0 ||
hddImage.write((char*)entry.data, entry.length); std::fwrite(entry.data, entry.length, 1, hddImage) != 1 ||
if (hddImage.fail()) std::fflush(hddImage) != 0)
{ {
Console.Error("DEV9: ATA: File write error"); Console.Error("DEV9: ATA: File write error");
pxAssert(false); pxAssert(false);
abort(); abort();
} }
hddImage.flush();
delete[] entry.data; delete[] entry.data;
return true; return true;
} }

View File

@ -15,7 +15,8 @@
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include <fstream> #include "common/FileSystem.h"
#include <fmt/format.h> #include <fmt/format.h>
#include "HddCreate.h" #include "HddCreate.h"
@ -26,21 +27,20 @@ void HddCreate::Start()
Cleanup(); Cleanup();
} }
void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes) void HddCreate::WriteImage(std::string hddPath, u64 reqSizeBytes)
{ {
constexpr int buffsize = 4 * 1024; constexpr int buffsize = 4 * 1024;
u8 buff[buffsize] = {0}; //4kb u8 buff[buffsize] = {0}; //4kb
if (ghc::filesystem::exists(hddPath)) if (FileSystem::FileExists(hddPath.c_str()))
{ {
errored.store(true); errored.store(true);
SetError(); SetError();
return; return;
} }
std::fstream newImage = ghc::filesystem::fstream(hddPath, std::ios::out | std::ios::binary); auto newImage = FileSystem::OpenManagedCFile(hddPath.c_str(), "wb");
if (!newImage)
if (newImage.fail())
{ {
errored.store(true); errored.store(true);
SetError(); SetError();
@ -48,14 +48,15 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes)
} }
//Size file //Size file
newImage.seekp(reqSizeBytes - 1, std::ios::beg);
const char zero = 0; 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(); newImage.reset();
ghc::filesystem::remove(filePath); FileSystem::DeleteFilePath(filePath.c_str());
errored.store(true); errored.store(true);
SetError(); SetError();
return; return;
@ -63,8 +64,6 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes)
lastUpdate = std::chrono::steady_clock::now(); lastUpdate = std::chrono::steady_clock::now();
newImage.seekp(0, std::ios::beg);
//Round up //Round up
const s32 reqMiB = (reqSizeBytes + ((1024 * 1024) - 1)) / (1024 * 1024); const s32 reqMiB = (reqSizeBytes + ((1024 * 1024) - 1)) / (1024 * 1024);
for (s32 iMiB = 0; iMiB < reqMiB; iMiB++) 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<s32>(1024, (reqSizeBytes / 1024) - (u64)iMiB * 1024) / 4; const s32 req4Kib = std::min<s32>(1024, (reqSizeBytes / 1024) - (u64)iMiB * 1024) / 4;
for (s32 i4kb = 0; i4kb < req4Kib; i4kb++) for (s32 i4kb = 0; i4kb < req4Kib; i4kb++)
{ {
newImage.write((char*)buff, buffsize); if (std::fwrite(buff, buffsize, 1, newImage.get()) != 1)
if (newImage.fail())
{ {
newImage.close(); newImage.reset();
ghc::filesystem::remove(filePath); FileSystem::DeleteFilePath(filePath.c_str());
errored.store(true); errored.store(true);
SetError(); SetError();
return; return;
@ -87,11 +85,10 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes)
if (req4Kib != 256) if (req4Kib != 256)
{ {
const s32 remainingBytes = reqSizeBytes - (((u64)iMiB) * (1024 * 1024) + req4Kib * 4096); const s32 remainingBytes = reqSizeBytes - (((u64)iMiB) * (1024 * 1024) + req4Kib * 4096);
newImage.write((char*)buff, remainingBytes); if (std::fwrite(buff, remainingBytes, 1, newImage.get()) != 1)
if (newImage.fail())
{ {
newImage.close(); newImage.reset();
ghc::filesystem::remove(filePath); FileSystem::DeleteFilePath(filePath.c_str());
errored.store(true); errored.store(true);
SetError(); SetError();
return; return;
@ -102,19 +99,17 @@ void HddCreate::WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes)
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastUpdate).count() >= 100 || (iMiB + 1) == reqMiB) if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastUpdate).count() >= 100 || (iMiB + 1) == reqMiB)
{ {
lastUpdate = now; lastUpdate = now;
SetFileProgress(newImage.tellp()); SetFileProgress(static_cast<u64>(FileSystem::FTell64(newImage.get())));
} }
if (canceled.load()) if (canceled.load())
{ {
newImage.close(); newImage.reset();
ghc::filesystem::remove(filePath); FileSystem::DeleteFilePath(filePath.c_str());
errored.store(true); errored.store(true);
SetError(); SetError();
return; return;
} }
} }
newImage.flush();
newImage.close();
} }
void HddCreate::SetFileProgress(u64 currentSize) void HddCreate::SetFileProgress(u64 currentSize)

View File

@ -17,15 +17,14 @@
#include <atomic> #include <atomic>
#include <chrono> #include <chrono>
#include <string>
#include "common/Path.h" #include "common/Path.h"
#include "ghc/filesystem.h"
class HddCreate class HddCreate
{ {
public: public:
ghc::filesystem::path filePath; std::string filePath;
u64 neededSize; u64 neededSize;
std::atomic_bool errored{false}; std::atomic_bool errored{false};
@ -50,5 +49,5 @@ protected:
void SetCanceled(); void SetCanceled();
private: private:
void WriteImage(ghc::filesystem::path hddPath, u64 reqSizeBytes); void WriteImage(std::string hddPath, u64 reqSizeBytes);
}; };

View File

@ -26,6 +26,8 @@
#include <wx/spinctrl.h> #include <wx/spinctrl.h>
#include <wx/gbsizer.h> #include <wx/gbsizer.h>
#include "common/FileSystem.h"
#include "common/Path.h"
#include "common/StringUtil.h" #include "common/StringUtil.h"
#include "Config.h" #include "Config.h"
@ -417,16 +419,12 @@ void DEV9configure()
{ {
dialog.Save(g_Conf->EmuOptions.DEV9); 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()) if (!Path::IsAbsolute(hddPath))
{ hddPath = Path::Combine(EmuFolders::Settings, hddPath);
//GHC uses UTF8 on all platforms
ghc::filesystem::path path(EmuFolders::Settings);
hddPath = path / hddPath;
}
if (g_Conf->EmuOptions.DEV9.HddEnable && !fs::exists(hddPath)) if (g_Conf->EmuOptions.DEV9.HddEnable && !FileSystem::FileExists(hddPath.c_str()))
{ {
HddCreateWx hddCreator; HddCreateWx hddCreator;
hddCreator.filePath = hddPath; hddCreator.filePath = hddPath;

View File

@ -16,6 +16,7 @@
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include "common/Assertions.h" #include "common/Assertions.h"
#include "common/Path.h"
#include "common/StringUtil.h" #include "common/StringUtil.h"
#ifdef _WIN32 #ifdef _WIN32
@ -26,6 +27,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <err.h> #include <err.h>
#include <unistd.h>
#endif #endif
#include <fcntl.h> #include <fcntl.h>
@ -95,23 +97,16 @@ int mapping;
bool isRunning = false; bool isRunning = false;
ghc::filesystem::path GetHDDPath() std::string GetHDDPath()
{ {
//GHC uses UTF8 on all platforms //GHC uses UTF8 on all platforms
ghc::filesystem::path hddPath(EmuConfig.DEV9.HddFile); std::string hddPath(EmuConfig.DEV9.HddFile);
if (hddPath.empty()) if (hddPath.empty())
EmuConfig.DEV9.HddEnable = false; EmuConfig.DEV9.HddEnable = false;
if (hddPath.is_relative()) if (!Path::IsAbsolute(hddPath))
{ hddPath = Path::Combine(EmuFolders::Settings, hddPath);
#ifdef _WIN32
ghc::filesystem::path path(StringUtil::UTF8StringToWideString(EmuFolders::Settings));
#else
ghc::filesystem::path path(EmuFolders::Settings);
#endif
hddPath = path / hddPath;
}
return hddPath; return hddPath;
} }
@ -211,7 +206,7 @@ s32 DEV9open()
#endif #endif
DevCon.WriteLn("DEV9: open r+: %s", EmuConfig.DEV9.HddFile.c_str()); DevCon.WriteLn("DEV9: open r+: %s", EmuConfig.DEV9.HddFile.c_str());
ghc::filesystem::path hddPath = GetHDDPath(); std::string hddPath(GetHDDPath());
if (EmuConfig.DEV9.HddEnable) if (EmuConfig.DEV9.HddEnable)
{ {
@ -1075,7 +1070,7 @@ void DEV9CheckChanges(const Pcsx2Config& old_config)
//Hdd //Hdd
//Hdd Validate Path //Hdd Validate Path
ghc::filesystem::path hddPath = GetHDDPath(); std::string hddPath(GetHDDPath());
//Hdd Compare with old config //Hdd Compare with old config
if (EmuConfig.DEV9.HddEnable) if (EmuConfig.DEV9.HddEnable)

View File

@ -16,7 +16,6 @@
#include "PrecompiledHeader.h" #include "PrecompiledHeader.h"
#include "common/StringUtil.h" #include "common/StringUtil.h"
#include "ghc/filesystem.h"
#include <wx/fileconf.h> #include <wx/fileconf.h>
#include "DEV9.h" #include "DEV9.h"

View File

@ -28,9 +28,11 @@
#elif defined(__linux__) #elif defined(__linux__)
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <net/if.h> #include <net/if.h>
#include <unistd.h>
#elif defined(__POSIX__) #elif defined(__POSIX__)
#include <sys/types.h> #include <sys/types.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <unistd.h>
#endif #endif
#include <stdio.h> #include <stdio.h>

View File

@ -27,6 +27,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <net/if.h> #include <net/if.h>
#include <unistd.h>
#ifdef __linux__ #ifdef __linux__
#include <sys/ioctl.h> #include <sys/ioctl.h>
#endif #endif

View File

@ -184,12 +184,3 @@ wxString Path::GetRootDirectory(const wxString& src)
else else
return wxString(src.begin(), src.begin() + pos); 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
}

View File

@ -19,10 +19,6 @@
#include <wx/filename.h> #include <wx/filename.h>
#include "ghc/filesystem.h"
namespace fs = ghc::filesystem;
#define g_MaxPath 255 // 255 is safer with antiquated Win32 ASCII APIs. #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 GetDirectory(const wxString& src);
extern wxString GetFilenameWithoutExt(const wxString& src); extern wxString GetFilenameWithoutExt(const wxString& src);
extern wxString GetRootDirectory(const wxString& src); extern wxString GetRootDirectory(const wxString& src);
extern fs::path FromWxString(const wxString& path);
} // namespace Path } // namespace Path