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 <algorithm>
#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<u32>(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();

View File

@ -20,9 +20,6 @@
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <fstream>
#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();

View File

@ -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<u64>(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 ||

View File

@ -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<size_t>(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;
}

View File

@ -15,7 +15,8 @@
#include "PrecompiledHeader.h"
#include <fstream>
#include "common/FileSystem.h"
#include <fmt/format.h>
#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<s32>(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<std::chrono::milliseconds>(now - lastUpdate).count() >= 100 || (iMiB + 1) == reqMiB)
{
lastUpdate = now;
SetFileProgress(newImage.tellp());
SetFileProgress(static_cast<u64>(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)

View File

@ -17,15 +17,14 @@
#include <atomic>
#include <chrono>
#include <string>
#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);
};

View File

@ -26,6 +26,8 @@
#include <wx/spinctrl.h>
#include <wx/gbsizer.h>
#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;

View File

@ -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 <sys/types.h>
#include <sys/mman.h>
#include <err.h>
#include <unistd.h>
#endif
#include <fcntl.h>
@ -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)

View File

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

View File

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

View File

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

View File

@ -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
}

View File

@ -19,10 +19,6 @@
#include <wx/filename.h>
#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