impr: Don't memory map files, never keep a write handle open for long

Closes #592
This commit is contained in:
WerWolv
2023-02-17 10:26:09 +01:00
parent e48761b5c0
commit bf8089dc7e
5 changed files with 72 additions and 119 deletions

View File

@@ -8,12 +8,20 @@
#include <hex/helpers/fs.hpp>
#include <sys/stat.h>
#if defined(OS_MACOS)
#include <unistd.h>
#include <sys/fcntl.h>
#define off64_t off_t
#define fopen64 fopen
#define fseeko64 fseek
#define ftello64 ftell
#define ftruncate64 ftruncate
#elif defined(OS_LINUX)
#include <unistd.h>
#include <fcntl.h>
#endif
namespace hex::fs {
@@ -65,6 +73,8 @@ namespace hex::fs {
void disableBuffering();
std::optional<struct stat> getFileInfo();
private:
FILE *m_file;
std::fs::path m_path;

View File

@@ -168,4 +168,18 @@ namespace hex::fs {
std::setvbuf(this->m_file, nullptr, _IONBF, 0);
}
std::optional<struct stat> File::getFileInfo() {
struct stat fileInfo = { };
#if defined(OS_WINDOWS)
if (wstat(this->m_path.c_str(), &fileInfo) != 0)
return std::nullopt;
#else
if (stat(hex::toUTF8String(this->m_path).c_str(), &fileInfo) != 0)
return std::nullopt;
#endif
return fileInfo;
}
}

View File

@@ -114,6 +114,10 @@ namespace hex::prv {
for (auto &[patchAddress, patch] : getPatches()) {
this->writeRaw(patchAddress - this->getBaseAddress(), &patch, 1);
}
if (!this->isWritable())
return;
this->markDirty();
this->m_patches.emplace_back();