1
0
mirror of synced 2025-01-31 12:03:46 +01:00

fix: Occasional crash when multiple threads are reading data from a file provider

This commit is contained in:
WerWolv 2023-03-17 11:43:50 +01:00
parent 880568cc60
commit 1f8645fd43
4 changed files with 13 additions and 8 deletions

View File

@ -385,7 +385,7 @@ namespace hex {
namespace impl { namespace impl {
using Callback = std::function<bool(std::filesystem::path)>; using Callback = std::function<bool(std::fs::path)>;
struct Entry { struct Entry {
std::vector<std::string> extensions; std::vector<std::string> extensions;
Callback callback; Callback callback;

View File

@ -175,7 +175,7 @@ namespace hex {
void setBorderlessWindowMode(bool enabled); void setBorderlessWindowMode(bool enabled);
void setCustomFontPath(const std::filesystem::path &path); void setCustomFontPath(const std::fs::path &path);
void setFontSize(float size); void setFontSize(float size);
void setGPUVendor(const std::string &vendor); void setGPUVendor(const std::string &vendor);

View File

@ -4,9 +4,9 @@
#include <wolv/io/file.hpp> #include <wolv/io/file.hpp>
#include <mutex>
#include <string_view> #include <string_view>
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
class FileProvider : public hex::prv::Provider { class FileProvider : public hex::prv::Provider {
@ -59,13 +59,15 @@ namespace hex::plugin::builtin {
protected: protected:
std::fs::path m_path; std::fs::path m_path;
wolv::io::File m_sizeFile;
std::map<std::thread::id, wolv::io::File> m_files; std::map<std::thread::id, wolv::io::File> m_files;
size_t m_fileSize = 0;
std::optional<struct stat> m_fileStats; std::optional<struct stat> m_fileStats;
bool m_readable = false, m_writable = false; bool m_readable = false, m_writable = false;
bool m_openReadOnly = true;
std::mutex m_fileAccessMutex, m_writeMutex;
}; };
} }

View File

@ -71,6 +71,7 @@ namespace hex::plugin::builtin {
if ((offset + size) > this->getActualSize() || buffer == nullptr || size == 0) if ((offset + size) > this->getActualSize() || buffer == nullptr || size == 0)
return; return;
std::scoped_lock lock(this->m_writeMutex);
wolv::io::File writeFile(this->m_path, wolv::io::File::Mode::Write); wolv::io::File writeFile(this->m_path, wolv::io::File::Mode::Write);
if (!writeFile.isValid()) if (!writeFile.isValid())
return; return;
@ -140,7 +141,7 @@ namespace hex::plugin::builtin {
} }
size_t FileProvider::getActualSize() const { size_t FileProvider::getActualSize() const {
return this->m_fileSize; return this->m_sizeFile.getSize();
} }
std::string FileProvider::getName() const { std::string FileProvider::getName() const {
@ -203,7 +204,7 @@ namespace hex::plugin::builtin {
} }
this->m_fileStats = file.getFileInfo(); this->m_fileStats = file.getFileInfo();
this->m_fileSize = file.getSize(); this->m_sizeFile = wolv::io::File(this->m_path, wolv::io::File::Mode::Read);
return true; return true;
} }
@ -213,6 +214,8 @@ namespace hex::plugin::builtin {
} }
wolv::io::File& FileProvider::getFile() { wolv::io::File& FileProvider::getFile() {
std::scoped_lock lock(this->m_fileAccessMutex);
if (this->m_files.size() > 5) if (this->m_files.size() > 5)
this->m_files.clear(); this->m_files.clear();
@ -229,7 +232,7 @@ namespace hex::plugin::builtin {
std::fs::path path = std::u8string(pathString.begin(), pathString.end()); std::fs::path path = std::u8string(pathString.begin(), pathString.end());
if (auto projectPath = ProjectFile::getPath(); !projectPath.empty()) if (auto projectPath = ProjectFile::getPath(); !projectPath.empty())
this->setPath(std::filesystem::weakly_canonical(projectPath.parent_path() / path)); this->setPath(std::fs::weakly_canonical(projectPath.parent_path() / path));
else else
this->setPath(path); this->setPath(path);
} }