From 202a02af103a3aa0593009793dda86ba3ff8be96 Mon Sep 17 00:00:00 2001 From: Lukas Cone Date: Sun, 17 Apr 2022 23:07:14 +0200 Subject: [PATCH] fix: Crash on linux when opened file gets modified (#487) --- .../content/providers/file_provider.hpp | 1 + .../content/providers/file_provider.cpp | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/plugins/builtin/include/content/providers/file_provider.hpp b/plugins/builtin/include/content/providers/file_provider.hpp index c2e337b95..177c29612 100644 --- a/plugins/builtin/include/content/providers/file_provider.hpp +++ b/plugins/builtin/include/content/providers/file_provider.hpp @@ -36,6 +36,7 @@ namespace hex::plugin::builtin::prv { void readRaw(u64 offset, void *buffer, size_t size) override; void writeRaw(u64 offset, const void *buffer, size_t size) override; [[nodiscard]] size_t getActualSize() const override; + [[nodiscard]] size_t getRealTimeSize(); void save() override; void saveAs(const std::fs::path &path) override; diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index e3bded88a..2ec7b125f 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -65,7 +65,7 @@ namespace hex::plugin::builtin::prv { } void FileProvider::readRaw(u64 offset, void *buffer, size_t size) { - if ((offset + size) > this->getActualSize() || buffer == nullptr || size == 0) + if ((offset + size) > this->getRealTimeSize() || buffer == nullptr || size == 0) return; std::memcpy(buffer, reinterpret_cast(this->m_mappedFile) + offset, size); @@ -135,6 +135,20 @@ namespace hex::plugin::builtin::prv { Provider::insert(offset, size); } + size_t FileProvider::getRealTimeSize() { +#if defined(OS_LINUX) + if (struct stat newStats; (this->m_fileStatsValid = fstat(this->m_file, &newStats) == 0)) { + if (static_cast(this->m_fileSize) != newStats.st_size || + std::memcmp(&newStats.st_mtim, &this->m_fileStats.st_mtim, sizeof(newStats.st_mtim))) { + this->m_fileStats = newStats; + this->m_fileSize = this->m_fileStats.st_size; + msync(this->m_mappedFile, this->m_fileStats.st_size, MS_INVALIDATE); + } + } +#endif + return getActualSize(); + } + size_t FileProvider::getActualSize() const { return this->m_fileSize; } @@ -171,7 +185,7 @@ namespace hex::plugin::builtin::prv { this->m_fileStatsValid = wstat(path.c_str(), &this->m_fileStats) == 0; - LARGE_INTEGER fileSize = { }; + LARGE_INTEGER fileSize = {}; this->m_file = reinterpret_cast(CreateFileW(path.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr)); GetFileSizeEx(this->m_file, &fileSize); @@ -236,10 +250,10 @@ namespace hex::plugin::builtin::prv { fileCleanup.release(); #else - const auto &path = this->m_path.native(); + const auto &path = this->m_path.native(); this->m_fileStatsValid = stat(path.c_str(), &this->m_fileStats) == 0; - int mmapprot = PROT_READ | PROT_WRITE; + int mmapprot = PROT_READ | PROT_WRITE; this->m_file = ::open(path.c_str(), O_RDWR); if (this->m_file == -1) {