sys: Refactor of filesystem functions. Fixed crashes where fs errors weren't caught correctly

Addresses the crash mentioned in #462
This commit is contained in:
WerWolv
2022-03-04 11:36:37 +01:00
parent 7866e3fc2a
commit 2739320f10
60 changed files with 442 additions and 398 deletions

View File

@@ -254,29 +254,29 @@ namespace hex::plugin::builtin {
ContentRegistry::PatternLanguage::Namespace nsStdFile = { "builtin", "std", "file" };
{
static u32 fileCounter = 0;
static std::map<u32, File> openFiles;
static std::map<u32, fs::File> openFiles;
/* open(path, mode) */
ContentRegistry::PatternLanguage::addDangerousFunction(nsStdFile, "open", 2, [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
const auto path = Token::literalToString(params[0], false);
const auto modeEnum = Token::literalToUnsigned(params[1]);
File::Mode mode;
fs::File::Mode mode;
switch (modeEnum) {
case 1:
mode = File::Mode::Read;
mode = fs::File::Mode::Read;
break;
case 2:
mode = File::Mode::Write;
mode = fs::File::Mode::Write;
break;
case 3:
mode = File::Mode::Create;
mode = fs::File::Mode::Create;
break;
default:
LogConsole::abortEvaluation("invalid file open mode");
}
auto file = File(path, mode);
fs::File file(path, mode);
if (!file.isValid())
LogConsole::abortEvaluation(hex::format("failed to open file {}", path));

View File

@@ -60,7 +60,7 @@ namespace hex::plugin::builtin::prv {
}
void DiskProvider::setPath(const fs::path &path) {
void DiskProvider::setPath(const std::fs::path &path) {
this->m_path = path;
}

View File

@@ -1,6 +1,5 @@
#include "content/providers/file_provider.hpp"
#include <ctime>
#include <cstring>
#include <hex/api/localization.hpp>
@@ -83,8 +82,8 @@ namespace hex::plugin::builtin::prv {
this->applyPatches();
}
void FileProvider::saveAs(const fs::path &path) {
File file(path, File::Mode::Create);
void FileProvider::saveAs(const std::fs::path &path) {
fs::File file(path, fs::File::Mode::Create);
if (file.isValid()) {
auto provider = ImHexApi::Provider::get();
@@ -106,7 +105,7 @@ namespace hex::plugin::builtin::prv {
this->close();
{
auto file = File(this->m_path, File::Mode::Write);
fs::File file(this->m_path, fs::File::Mode::Write);
file.setSize(newSize);
this->m_fileSize = file.getSize();
@@ -141,7 +140,7 @@ namespace hex::plugin::builtin::prv {
}
std::string FileProvider::getName() const {
return fs::path(this->m_path).filename().string();
return std::fs::path(this->m_path).filename().string();
}
std::vector<std::pair<std::string, std::string>> FileProvider::getDataInformation() const {
@@ -159,7 +158,7 @@ namespace hex::plugin::builtin::prv {
return result;
}
void FileProvider::setPath(const fs::path &path) {
void FileProvider::setPath(const std::fs::path &path) {
this->m_path = path;
}

View File

@@ -217,7 +217,7 @@ namespace hex::plugin::builtin::prv {
this->applyPatches();
}
void GDBProvider::saveAs(const fs::path &path) {
void GDBProvider::saveAs(const std::fs::path &path) {
}
size_t GDBProvider::getActualSize() const {

View File

@@ -238,10 +238,10 @@ namespace hex::plugin::builtin {
ImGui::SameLine();
if (ImGui::IconButton(ICON_VS_FOLDER_OPENED, ImGui::GetStyleColorVec4(ImGuiCol_Text))) {
return hex::openFileBrowser("hex.builtin.setting.font.font_path", DialogMode::Open, {
{"TTF Font", "ttf"}
return fs::openFileBrowser("hex.builtin.setting.font.font_path", fs::DialogMode::Open, {
{"TTF Font", "ttf"}
},
[&](const fs::path &path) {
[&](const std::fs::path &path) {
fontPath = path.string();
setting = fontPath;
});
@@ -294,7 +294,7 @@ namespace hex::plugin::builtin {
ImGui::BeginGroup();
if (ImGui::IconButton(ICON_VS_NEW_FOLDER, ImGui::GetCustomColorVec4(ImGuiCustomCol_DescButton), ImVec2(30, 30))) {
hex::openFileBrowser("Select include folder", hex::DialogMode::Folder, {}, [&](fs::path path) {
fs::openFileBrowser("Select include folder", fs::DialogMode::Folder, {}, [&](std::fs::path path) {
auto pathStr = path.string();
if (std::find(folders.begin(), folders.end(), pathStr) == folders.end()) {

View File

@@ -6,7 +6,7 @@
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/literals.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/api/localization.hpp>
#include <hex/ui/view.hpp>
@@ -571,7 +571,7 @@ namespace hex::plugin::builtin {
static hex::Net net;
static std::future<Response<std::string>> uploadProcess;
static fs::path currFile;
static std::fs::path currFile;
static std::vector<UploadedFile> links;
bool uploading = uploadProcess.valid() && uploadProcess.wait_for(0s) != std::future_status::ready;
@@ -579,7 +579,7 @@ namespace hex::plugin::builtin {
ImGui::Header("hex.builtin.tools.file_uploader.control"_lang, true);
if (!uploading) {
if (ImGui::Button("hex.builtin.tools.file_uploader.upload"_lang)) {
hex::openFileBrowser("hex.builtin.tools.file_uploader.done"_lang, DialogMode::Open, {}, [&](auto path) {
fs::openFileBrowser("hex.builtin.tools.file_uploader.done"_lang, fs::DialogMode::Open, {}, [&](auto path) {
uploadProcess = net.uploadFile("https://api.anonfiles.com/upload", path);
currFile = path;
});
@@ -744,7 +744,7 @@ namespace hex::plugin::builtin {
ImGui::InputText("##path", selectedFile);
ImGui::SameLine();
if (ImGui::Button("...")) {
hex::openFileBrowser("hex.builtin.tools.file_tools.shredder.picker"_lang, DialogMode::Open, {}, [](const auto &path) {
fs::openFileBrowser("hex.builtin.tools.file_tools.shredder.picker"_lang, fs::DialogMode::Open, {}, [](const auto &path) {
selectedFile = path.string();
});
}
@@ -768,7 +768,7 @@ namespace hex::plugin::builtin {
shredding = false;
selectedFile.clear();
};
File file(selectedFile, File::Mode::Write);
fs::File file(selectedFile, fs::File::Mode::Write);
if (!file.isValid()) {
View::showErrorPopup("hex.builtin.tools.file_tools.shredder.error.open"_lang);
@@ -886,7 +886,7 @@ namespace hex::plugin::builtin {
ImGui::InputText("##path", selectedFile);
ImGui::SameLine();
if (ImGui::Button("...##input")) {
hex::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.input"_lang, DialogMode::Open, {}, [](const auto &path) {
fs::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.input"_lang, fs::DialogMode::Open, {}, [](const auto &path) {
selectedFile = path.string();
});
}
@@ -896,7 +896,7 @@ namespace hex::plugin::builtin {
ImGui::InputText("##base_path", baseOutputPath);
ImGui::SameLine();
if (ImGui::Button("...##output")) {
hex::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.output"_lang, DialogMode::Save, {}, [](const auto &path) {
fs::openFileBrowser("hex.builtin.tools.file_tools.splitter.picker.output"_lang, fs::DialogMode::Save, {}, [](const auto &path) {
baseOutputPath = path.string();
});
}
@@ -934,7 +934,7 @@ namespace hex::plugin::builtin {
selectedFile.clear();
baseOutputPath.clear();
};
File file(selectedFile, File::Mode::Read);
fs::File file(selectedFile, fs::File::Mode::Read);
if (!file.isValid()) {
View::showErrorPopup("hex.builtin.tools.file_tools.splitter.error.open"_lang);
@@ -951,7 +951,7 @@ namespace hex::plugin::builtin {
for (u64 offset = 0; offset < file.getSize(); offset += splitSize) {
task.update(offset);
File partFile(baseOutputPath + hex::format(".{:05}", index), File::Mode::Create);
fs::File partFile(baseOutputPath + hex::format(".{:05}", index), fs::File::Mode::Create);
if (!partFile.isValid()) {
View::showErrorPopup(hex::format("hex.builtin.tools.file_tools.splitter.error.create"_lang, index));
@@ -991,7 +991,7 @@ namespace hex::plugin::builtin {
i32 index = 0;
for (auto &file : files) {
if (ImGui::Selectable(fs::path(file).filename().string().c_str(), index == selectedIndex))
if (ImGui::Selectable(std::fs::path(file).filename().string().c_str(), index == selectedIndex))
selectedIndex = index;
index++;
}
@@ -1025,7 +1025,7 @@ namespace hex::plugin::builtin {
ImGui::BeginDisabled(combining);
{
if (ImGui::Button("hex.builtin.tools.file_tools.combiner.add"_lang)) {
hex::openFileBrowser("hex.builtin.tools.file_tools.combiner.add.picker"_lang, DialogMode::Open, {}, [](const auto &path) {
fs::openFileBrowser("hex.builtin.tools.file_tools.combiner.add.picker"_lang, fs::DialogMode::Open, {}, [](const auto &path) {
files.push_back(path.string());
});
}
@@ -1049,7 +1049,7 @@ namespace hex::plugin::builtin {
ImGui::InputText("##output_path", outputPath);
ImGui::SameLine();
if (ImGui::Button("...")) {
hex::openFileBrowser("hex.builtin.tools.file_tools.combiner.output.picker"_lang, DialogMode::Save, {}, [](const auto &path) {
fs::openFileBrowser("hex.builtin.tools.file_tools.combiner.output.picker"_lang, fs::DialogMode::Save, {}, [](const auto &path) {
outputPath = path.string();
});
}
@@ -1069,7 +1069,7 @@ namespace hex::plugin::builtin {
std::thread([] {
ON_SCOPE_EXIT { combining = false; };
File output(outputPath, File::Mode::Create);
fs::File output(outputPath, fs::File::Mode::Create);
if (!output.isValid()) {
View::showErrorPopup("hex.builtin.tools.file_tools.combiner.error.open_output"_lang);
@@ -1083,9 +1083,9 @@ namespace hex::plugin::builtin {
task.update(fileIndex);
fileIndex++;
File input(file, File::Mode::Read);
fs::File input(file, fs::File::Mode::Read);
if (!input.isValid()) {
View::showErrorPopup(hex::format("hex.builtin.tools.file_tools.combiner.open_input"_lang, fs::path(file).filename().string()));
View::showErrorPopup(hex::format("hex.builtin.tools.file_tools.combiner.open_input"_lang, std::fs::path(file).filename().string()));
return;
}

View File

@@ -104,7 +104,7 @@ namespace hex::plugin::builtin {
// Save file as
ImGui::Disabled([&provider] {
if (ImGui::ToolBarButton(ICON_VS_SAVE_AS, ImGui::GetCustomColorVec4(ImGuiCustomCol_ToolbarBlue)))
hex::openFileBrowser("hex.builtin.view.hex_editor.save_as"_lang, DialogMode::Save, {}, [&provider](auto path) {
fs::openFileBrowser("hex.builtin.view.hex_editor.save_as"_lang, fs::DialogMode::Save, {}, [&provider](auto path) {
provider->saveAs(path);
});
},

View File

@@ -1,6 +1,6 @@
#include "content/views/view_constants.hpp"
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/utils.hpp>
@@ -24,10 +24,11 @@ namespace hex::plugin::builtin {
this->m_constants.clear();
this->m_filterIndices.clear();
for (const auto &path : hex::getPath(ImHexPath::Constants)) {
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Constants)) {
if (!fs::exists(path)) continue;
for (auto &file : fs::directory_iterator(path)) {
std::error_code error;
for (auto &file : std::fs::directory_iterator(path, error)) {
if (!file.is_regular_file()) continue;
try {

View File

@@ -41,7 +41,7 @@ namespace hex::plugin::builtin {
}
});
EventManager::subscribe<EventFileLoaded>(this, [this](const fs::path &path) {
EventManager::subscribe<EventFileLoaded>(this, [this](const std::fs::path &path) {
for (auto &node : this->m_nodes) {
node->setCurrentOverlay(nullptr);
}
@@ -54,22 +54,22 @@ namespace hex::plugin::builtin {
ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 3000, [&, this] {
if (ImGui::MenuItem("hex.builtin.view.data_processor.menu.file.load_processor"_lang)) {
hex::openFileBrowser("hex.builtin.view.data_processor.menu.file.load_processor"_lang, DialogMode::Open, {
{"hex.builtin.view.data_processor.name"_lang, "hexnode"}
fs::openFileBrowser("hex.builtin.view.data_processor.menu.file.load_processor"_lang, fs::DialogMode::Open, {
{"hex.builtin.view.data_processor.name"_lang, "hexnode"}
},
[this](const fs::path &path) {
File file(path, File::Mode::Read);
[this](const std::fs::path &path) {
fs::File file(path, fs::File::Mode::Read);
if (file.isValid())
this->loadNodes(file.readString());
});
}
if (ImGui::MenuItem("hex.builtin.view.data_processor.menu.file.save_processor"_lang, nullptr, false, !this->m_nodes.empty())) {
hex::openFileBrowser("hex.builtin.view.data_processor.menu.file.save_processor"_lang, DialogMode::Save, {
{"hex.builtin.view.data_processor.name"_lang, "hexnode"}
fs::openFileBrowser("hex.builtin.view.data_processor.menu.file.save_processor"_lang, fs::DialogMode::Save, {
{"hex.builtin.view.data_processor.name"_lang, "hexnode"}
},
[this](const fs::path &path) {
File file(path, File::Mode::Create);
[this](const std::fs::path &path) {
fs::File file(path, fs::File::Mode::Create);
if (file.isValid())
file.write(this->saveNodes());
});
@@ -77,7 +77,7 @@ namespace hex::plugin::builtin {
});
ContentRegistry::FileHandler::add({ ".hexnode" }, [this](const auto &path) {
File file(path, File::Mode::Read);
fs::File file(path, fs::File::Mode::Read);
if (!file.isValid()) return false;
this->loadNodes(file.readString());

View File

@@ -4,7 +4,7 @@
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/logger.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/utils.hpp>
#include <romfs/romfs.hpp>
@@ -119,15 +119,15 @@ namespace hex::plugin::builtin {
ImGui::TableSetupColumn("Type");
ImGui::TableSetupColumn("Paths");
constexpr std::array<std::pair<const char *, ImHexPath>, 8> PathTypes = {
{{ "Resources", ImHexPath::Resources },
{ "Config", ImHexPath::Config },
{ "Magic", ImHexPath::Magic },
{ "Patterns", ImHexPath::Patterns },
{ "Patterns Includes", ImHexPath::PatternsInclude },
{ "Plugins", ImHexPath::Plugins },
{ "Python Scripts", ImHexPath::Python },
{ "Yara Patterns", ImHexPath::Yara }}
constexpr std::array<std::pair<const char *, fs::ImHexPath>, 8> PathTypes = {
{{ "Resources", fs::ImHexPath::Resources },
{ "Config", fs::ImHexPath::Config },
{ "Magic", fs::ImHexPath::Magic },
{ "Patterns", fs::ImHexPath::Patterns },
{ "Patterns Includes", fs::ImHexPath::PatternsInclude },
{ "Plugins", fs::ImHexPath::Plugins },
{ "Python Scripts", fs::ImHexPath::Python },
{ "Yara Patterns", fs::ImHexPath::Yara }}
};
ImGui::TableHeadersRow();
@@ -137,7 +137,7 @@ namespace hex::plugin::builtin {
ImGui::TextUnformatted(name);
ImGui::TableNextColumn();
for (auto &path : hex::getPath(type))
for (auto &path : fs::getDefaultPaths(type))
ImGui::TextUnformatted(path.string().c_str());
}

View File

@@ -6,7 +6,7 @@
#include <hex/providers/provider.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/patches.hpp>
#include <hex/helpers/project_file_handler.hpp>
#include <hex/helpers/loader_script_handler.hpp>
@@ -267,7 +267,7 @@ namespace hex::plugin::builtin {
}
static void saveAs() {
hex::openFileBrowser("hex.builtin.view.hex_editor.save_as"_lang, DialogMode::Save, {}, [](const auto &path) {
fs::openFileBrowser("hex.builtin.view.hex_editor.save_as"_lang, fs::DialogMode::Save, {}, [](const auto &path) {
ImHexApi::Provider::get()->saveAs(path);
});
}
@@ -297,8 +297,8 @@ namespace hex::plugin::builtin {
ImGui::InputText("##nolabel", this->m_loaderScriptScriptPath.data(), this->m_loaderScriptScriptPath.length(), ImGuiInputTextFlags_ReadOnly);
ImGui::SameLine();
if (ImGui::Button("hex.builtin.view.hex_editor.script.script"_lang)) {
hex::openFileBrowser("hex.builtin.view.hex_editor.script.script.title"_lang, DialogMode::Open, {
{"Python Script", "py"}
fs::openFileBrowser("hex.builtin.view.hex_editor.script.script.title"_lang, fs::DialogMode::Open, {
{"Python Script", "py"}
},
[this](const auto &path) {
this->m_loaderScriptScriptPath = path.string();
@@ -307,7 +307,7 @@ namespace hex::plugin::builtin {
ImGui::InputText("##nolabel", this->m_loaderScriptFilePath.data(), this->m_loaderScriptFilePath.length(), ImGuiInputTextFlags_ReadOnly);
ImGui::SameLine();
if (ImGui::Button("hex.builtin.view.hex_editor.script.file"_lang)) {
hex::openFileBrowser("hex.builtin.view.hex_editor.script.file.title"_lang, DialogMode::Open, {}, [this](const auto &path) {
fs::openFileBrowser("hex.builtin.view.hex_editor.script.file.title"_lang, fs::DialogMode::Open, {}, [this](const auto &path) {
this->m_loaderScriptFilePath = path.string();
});
}
@@ -379,7 +379,7 @@ namespace hex::plugin::builtin {
}
}
void ViewHexEditor::openFile(const fs::path &path) {
void ViewHexEditor::openFile(const std::fs::path &path) {
hex::prv::Provider *provider = nullptr;
EventManager::post<RequestCreateProvider>("hex.builtin.provider.file", &provider);
@@ -863,10 +863,10 @@ namespace hex::plugin::builtin {
}
});
EventManager::subscribe<RequestOpenWindow>(this, [this](std::string name) {
EventManager::subscribe<RequestOpenWindow>(this, [this](const std::string &name) {
if (name == "Create File") {
hex::openFileBrowser("hex.builtin.view.hex_editor.create_file"_lang, DialogMode::Save, {}, [this](const auto &path) {
File file(path, File::Mode::Create);
fs::openFileBrowser("hex.builtin.view.hex_editor.create_file"_lang, fs::DialogMode::Save, {}, [this](const auto &path) {
fs::File file(path, fs::File::Mode::Create);
if (!file.isValid()) {
View::showErrorPopup("hex.builtin.view.hex_editor.error.create"_lang);
@@ -879,13 +879,13 @@ namespace hex::plugin::builtin {
this->getWindowOpenState() = true;
});
} else if (name == "Open File") {
hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [this](const auto &path) {
fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [this](const auto &path) {
EventManager::post<RequestOpenFile>(path);
this->getWindowOpenState() = true;
});
} else if (name == "Open Project") {
hex::openFileBrowser("hex.builtin.view.hex_editor.open_project"_lang, DialogMode::Open, {
{"Project File", "hexproj"}
fs::openFileBrowser("hex.builtin.view.hex_editor.open_project"_lang, fs::DialogMode::Open, {
{"Project File", "hexproj"}
},
[this](const auto &path) {
ProjectFile::load(path);
@@ -998,7 +998,7 @@ namespace hex::plugin::builtin {
});
ShortcutManager::addShortcut(this, CTRL + Keys::O, [] {
hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [](const auto &path) {
fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [](const auto &path) {
EventManager::post<RequestOpenFile>(path);
});
});
@@ -1057,8 +1057,8 @@ namespace hex::plugin::builtin {
bool providerValid = ImHexApi::Provider::isValid();
if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.open_project"_lang, "")) {
hex::openFileBrowser("hex.builtin.view.hex_editor.menu.file.open_project"_lang, DialogMode::Open, {
{"Project File", "hexproj"}
fs::openFileBrowser("hex.builtin.view.hex_editor.menu.file.open_project"_lang, fs::DialogMode::Open, {
{"Project File", "hexproj"}
},
[](const auto &path) {
ProjectFile::load(path);
@@ -1067,10 +1067,10 @@ namespace hex::plugin::builtin {
if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.save_project"_lang, "", false, providerValid && provider->isWritable())) {
if (ProjectFile::getProjectFilePath() == "") {
hex::openFileBrowser("hex.builtin.view.hex_editor.save_project"_lang, DialogMode::Save, {
{"Project File", "hexproj"}
fs::openFileBrowser("hex.builtin.view.hex_editor.save_project"_lang, fs::DialogMode::Save, {
{"Project File", "hexproj"}
},
[](fs::path path) {
[](std::fs::path path) {
if (path.extension() != ".hexproj") {
path.replace_extension(".hexproj");
}
@@ -1082,9 +1082,10 @@ namespace hex::plugin::builtin {
}
if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.load_encoding_file"_lang)) {
std::vector<fs::path> paths;
for (const auto &path : hex::getPath(ImHexPath::Encodings)) {
for (const auto &entry : fs::recursive_directory_iterator(path)) {
std::vector<std::fs::path> paths;
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Encodings)) {
std::error_code error;
for (const auto &entry : std::fs::recursive_directory_iterator(path, error)) {
if (!entry.is_regular_file()) continue;
paths.push_back(entry);
@@ -1110,8 +1111,8 @@ namespace hex::plugin::builtin {
if (ImGui::BeginMenu("hex.builtin.view.hex_editor.menu.file.import"_lang)) {
if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.import.base64"_lang)) {
hex::openFileBrowser("hex.builtin.view.hex_editor.menu.file.import.base64"_lang, DialogMode::Open, {}, [this](const auto &path) {
File file(path, File::Mode::Read);
fs::openFileBrowser("hex.builtin.view.hex_editor.menu.file.import.base64"_lang, fs::DialogMode::Open, {}, [this](const auto &path) {
fs::File file(path, fs::File::Mode::Read);
if (!file.isValid()) {
View::showErrorPopup("hex.builtin.view.hex_editor.error.open"_lang);
return;
@@ -1135,12 +1136,12 @@ namespace hex::plugin::builtin {
if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.import.ips"_lang, nullptr, false, !this->m_processingImportExport)) {
hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [this](const auto &path) {
fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [this](const auto &path) {
this->m_processingImportExport = true;
std::thread([this, path] {
auto task = ImHexApi::Tasks::createTask("hex.builtin.view.hex_editor.processing", 0);
auto patchData = File(path, File::Mode::Read).readBytes();
auto patchData = fs::File(path, fs::File::Mode::Read).readBytes();
auto patch = hex::loadIPSPatch(patchData);
task.setMaxValue(patch.size());
@@ -1163,12 +1164,12 @@ namespace hex::plugin::builtin {
}
if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.import.ips32"_lang, nullptr, false, !this->m_processingImportExport)) {
hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [this](const auto &path) {
fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [this](const auto &path) {
this->m_processingImportExport = true;
std::thread([this, path] {
auto task = ImHexApi::Tasks::createTask("hex.builtin.view.hex_editor.processing", 0);
auto patchData = File(path, File::Mode::Read).readBytes();
auto patchData = fs::File(path, fs::File::Mode::Read).readBytes();
auto patch = hex::loadIPS32Patch(patchData);
task.setMaxValue(patch.size());
@@ -1218,8 +1219,8 @@ namespace hex::plugin::builtin {
this->m_processingImportExport = false;
ImHexApi::Tasks::doLater([this] {
hex::openFileBrowser("hex.builtin.view.hex_editor.menu.file.export.title"_lang, DialogMode::Save, {}, [this](const auto &path) {
auto file = File(path, File::Mode::Create);
fs::openFileBrowser("hex.builtin.view.hex_editor.menu.file.export.title"_lang, fs::DialogMode::Save, {}, [this](const auto &path) {
auto file = fs::File(path, fs::File::Mode::Create);
if (!file.isValid()) {
View::showErrorPopup("hex.builtin.view.hex_editor.error.create"_lang);
return;
@@ -1247,8 +1248,8 @@ namespace hex::plugin::builtin {
this->m_processingImportExport = false;
ImHexApi::Tasks::doLater([this] {
hex::openFileBrowser("hex.builtin.view.hex_editor.menu.file.export.title"_lang, DialogMode::Save, {}, [this](const auto &path) {
auto file = File(path, File::Mode::Create);
fs::openFileBrowser("hex.builtin.view.hex_editor.menu.file.export.title"_lang, fs::DialogMode::Save, {}, [this](const auto &path) {
auto file = fs::File(path, fs::File::Mode::Create);
if (!file.isValid()) {
View::showErrorPopup("hex.builtin.view.hex_editor.error.create"_lang);
return;

View File

@@ -3,7 +3,7 @@
#include <hex/api/content_registry.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/fmt.hpp>
#include <hex/helpers/literals.hpp>
@@ -46,9 +46,8 @@ namespace hex::plugin::builtin {
});
ContentRegistry::FileHandler::add({ ".mgc" }, [](const auto &path) {
for (const auto &destPath : hex::getPath(ImHexPath::Magic)) {
std::error_code error;
if (fs::copy_file(path, destPath / path.filename(), fs::copy_options::overwrite_existing, error)) {
for (const auto &destPath : fs::getDefaultPaths(fs::ImHexPath::Magic)) {
if (fs::copyFile(path, destPath / path.filename(), std::fs::copy_options::overwrite_existing)) {
View::showMessagePopup("hex.builtin.view.information.magic_db_added"_lang);
return true;
}

View File

@@ -8,7 +8,7 @@
#include <hex/pattern_language/ast/ast_node_type_decl.hpp>
#include <hex/pattern_language/ast/ast_node_builtin_type.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/project_file_handler.hpp>
@@ -100,7 +100,7 @@ namespace hex::plugin::builtin {
this->m_textEditor.InsertText(code);
});
EventManager::subscribe<EventFileLoaded>(this, [this](const fs::path &path) {
EventManager::subscribe<EventFileLoaded>(this, [this](const std::fs::path &path) {
if (!ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.auto_load_patterns", 1))
return;
@@ -124,13 +124,13 @@ namespace hex::plugin::builtin {
this->m_possiblePatternFiles.clear();
std::error_code errorCode;
for (const auto &dir : hex::getPath(ImHexPath::Patterns)) {
for (auto &entry : fs::directory_iterator(dir, errorCode)) {
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Patterns)) {
for (auto &entry : std::fs::directory_iterator(dir, errorCode)) {
foundCorrectType = false;
if (!entry.is_regular_file())
continue;
File file(entry.path().string(), File::Mode::Read);
fs::File file(entry.path().string(), fs::File::Mode::Read);
if (!file.isValid())
continue;
@@ -181,8 +181,8 @@ namespace hex::plugin::builtin {
});
}
ContentRegistry::FileHandler::add({ ".hexpat", ".pat" }, [](const fs::path &path) -> bool {
File file(path.string(), File::Mode::Read);
ContentRegistry::FileHandler::add({ ".hexpat", ".pat" }, [](const std::fs::path &path) -> bool {
fs::File file(path.string(), fs::File::Mode::Read);
if (file.isValid()) {
EventManager::post<RequestSetPatternLanguageCode>(file.readString());
@@ -195,12 +195,13 @@ namespace hex::plugin::builtin {
ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 2000, [&, this] {
if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.file.load_pattern"_lang)) {
std::vector<fs::path> paths;
std::vector<std::fs::path> paths;
for (const auto &imhexPath : hex::getPath(ImHexPath::Patterns)) {
for (const auto &imhexPath : fs::getDefaultPaths(fs::ImHexPath::Patterns)) {
if (!fs::exists(imhexPath)) continue;
for (auto &entry : fs::recursive_directory_iterator(imhexPath)) {
std::error_code error;
for (auto &entry : std::fs::recursive_directory_iterator(imhexPath, error)) {
if (entry.is_regular_file() && entry.path().extension() == ".hexpat") {
paths.push_back(entry.path());
}
@@ -210,17 +211,17 @@ namespace hex::plugin::builtin {
View::showFileChooserPopup(paths, {
{"Pattern File", "hexpat"}
},
[this](const fs::path &path) {
[this](const std::fs::path &path) {
this->loadPatternFile(path);
});
}
if (ImGui::MenuItem("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang)) {
hex::openFileBrowser("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang, DialogMode::Save, {
{"Pattern", "hexpat"}
fs::openFileBrowser("hex.builtin.view.pattern_editor.menu.file.save_pattern"_lang, fs::DialogMode::Save, {
{"Pattern", "hexpat"}
},
[this](const auto &path) {
File file(path, File::Mode::Create);
fs::File file(path, fs::File::Mode::Create);
file.write(this->m_textEditor.GetText());
});
@@ -549,7 +550,7 @@ namespace hex::plugin::builtin {
entries.resize(this->m_possiblePatternFiles.size());
for (u32 i = 0; i < entries.size(); i++) {
entries[i] = fs::path(this->m_possiblePatternFiles[i]).filename().string();
entries[i] = std::fs::path(this->m_possiblePatternFiles[i]).filename().string();
}
if (ImGui::BeginListBox("##patterns_accept", ImVec2(-FLT_MIN, 0))) {
@@ -580,8 +581,8 @@ namespace hex::plugin::builtin {
}
void ViewPatternEditor::loadPatternFile(const fs::path &path) {
File file(path, File::Mode::Read);
void ViewPatternEditor::loadPatternFile(const std::fs::path &path) {
fs::File file(path, fs::File::Mode::Read);
if (file.isValid()) {
auto code = file.readString();

View File

@@ -11,7 +11,7 @@
#include <hex/helpers/logger.hpp>
#include <hex/helpers/magic.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <fstream>
#include <filesystem>
@@ -44,7 +44,7 @@ namespace hex::plugin::builtin {
this->refresh();
}
auto drawTab = [this](auto title, ImHexPath pathType, auto &content, const std::function<void(const StoreEntry &)> &downloadDoneCallback) {
auto drawTab = [this](auto title, fs::ImHexPath pathType, auto &content, const std::function<void(const StoreEntry &)> &downloadDoneCallback) {
if (ImGui::BeginTabItem(title)) {
if (ImGui::BeginTable("##pattern_language", 3, ImGuiTableFlags_ScrollY | ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_RowBg)) {
ImGui::TableSetupScrollFreeze(0, 1);
@@ -84,14 +84,14 @@ namespace hex::plugin::builtin {
mtar_header_t header;
auto extractBasePath = this->m_downloadPath.parent_path() / this->m_downloadPath.stem();
while (mtar_read_header(&ctx, &header) != MTAR_ENULLRECORD) {
auto filePath = extractBasePath / fs::path(header.name);
auto filePath = extractBasePath / std::fs::path(header.name);
if (filePath.filename() == "@PaxHeader") {
mtar_next(&ctx);
continue;
}
fs::create_directories(filePath.parent_path());
File outputFile(filePath.string(), File::Mode::Create);
fs::createDirectories(filePath.parent_path());
fs::File outputFile(filePath.string(), fs::File::Mode::Create);
std::vector<u8> buffer(0x10000);
for (u64 offset = 0; offset < header.size; offset += buffer.size()) {
@@ -142,12 +142,12 @@ namespace hex::plugin::builtin {
};
if (ImGui::BeginTabBar("storeTabs")) {
drawTab("hex.builtin.view.store.tab.patterns"_lang, ImHexPath::Patterns, this->m_patterns, [](auto) {});
drawTab("hex.builtin.view.store.tab.libraries"_lang, ImHexPath::PatternsInclude, this->m_includes, [](auto) {});
drawTab("hex.builtin.view.store.tab.magics"_lang, ImHexPath::Magic, this->m_magics, [](auto) { magic::compile(); });
drawTab("hex.builtin.view.store.tab.constants"_lang, ImHexPath::Constants, this->m_constants, [](auto) {});
drawTab("hex.builtin.view.store.tab.encodings"_lang, ImHexPath::Encodings, this->m_encodings, [](auto) {});
drawTab("hex.builtin.view.store.tab.yara"_lang, ImHexPath::Yara, this->m_yara, [](auto) {});
drawTab("hex.builtin.view.store.tab.patterns"_lang, fs::ImHexPath::Patterns, this->m_patterns, [](auto) {});
drawTab("hex.builtin.view.store.tab.libraries"_lang, fs::ImHexPath::PatternsInclude, this->m_includes, [](auto) {});
drawTab("hex.builtin.view.store.tab.magics"_lang, fs::ImHexPath::Magic, this->m_magics, [](auto) { magic::compile(); });
drawTab("hex.builtin.view.store.tab.constants"_lang, fs::ImHexPath::Constants, this->m_constants, [](auto) {});
drawTab("hex.builtin.view.store.tab.encodings"_lang, fs::ImHexPath::Encodings, this->m_encodings, [](auto) {});
drawTab("hex.builtin.view.store.tab.yara"_lang, fs::ImHexPath::Yara, this->m_yara, [](auto) {});
ImGui::EndTabBar();
}
@@ -169,7 +169,7 @@ namespace hex::plugin::builtin {
if (response.code == 200) {
auto json = nlohmann::json::parse(response.body);
auto parseStoreEntries = [](auto storeJson, const std::string &name, ImHexPath pathType, std::vector<StoreEntry> &results) {
auto parseStoreEntries = [](auto storeJson, const std::string &name, fs::ImHexPath pathType, std::vector<StoreEntry> &results) {
// Check if the response handles the type of files
if (storeJson.contains(name)) {
@@ -182,15 +182,15 @@ namespace hex::plugin::builtin {
StoreEntry storeEntry = { entry["name"], entry["desc"], entry["file"], entry["url"], entry["hash"], entry["folder"], false, false, false };
// Check if file is installed already or has an update available
for (const auto &folder : hex::getPath(pathType)) {
for (const auto &folder : fs::getDefaultPaths(pathType)) {
auto path = folder / fs::path(storeEntry.fileName);
auto path = folder / std::fs::path(storeEntry.fileName);
if (fs::exists(path) && hex::isPathWritable(folder)) {
if (fs::exists(path) && fs::isPathWritable(folder)) {
storeEntry.installed = true;
std::ifstream file(path, std::ios::in | std::ios::binary);
std::vector<u8> data(fs::file_size(path), 0x00);
std::vector<u8> data(fs::getFileSize(path), 0x00);
file.read(reinterpret_cast<char *>(data.data()), data.size());
auto fileHash = crypt::sha256(data);
@@ -207,12 +207,12 @@ namespace hex::plugin::builtin {
}
};
parseStoreEntries(json, "patterns", ImHexPath::Patterns, this->m_patterns);
parseStoreEntries(json, "includes", ImHexPath::PatternsInclude, this->m_includes);
parseStoreEntries(json, "magic", ImHexPath::Magic, this->m_magics);
parseStoreEntries(json, "constants", ImHexPath::Constants, this->m_constants);
parseStoreEntries(json, "yara", ImHexPath::Yara, this->m_yara);
parseStoreEntries(json, "encodings", ImHexPath::Encodings, this->m_encodings);
parseStoreEntries(json, "patterns", fs::ImHexPath::Patterns, this->m_patterns);
parseStoreEntries(json, "includes", fs::ImHexPath::PatternsInclude, this->m_includes);
parseStoreEntries(json, "magic", fs::ImHexPath::Magic, this->m_magics);
parseStoreEntries(json, "constants", fs::ImHexPath::Constants, this->m_constants);
parseStoreEntries(json, "yara", fs::ImHexPath::Yara, this->m_yara);
parseStoreEntries(json, "encodings", fs::ImHexPath::Encodings, this->m_encodings);
}
this->m_apiRequest = {};
}
@@ -234,13 +234,13 @@ namespace hex::plugin::builtin {
}
}
bool ViewStore::download(ImHexPath pathType, const std::string &fileName, const std::string &url, bool update) {
bool ViewStore::download(fs::ImHexPath pathType, const std::string &fileName, const std::string &url, bool update) {
bool downloading = false;
for (const auto &path : hex::getPath(pathType)) {
if (!hex::isPathWritable(path))
for (const auto &path : fs::getDefaultPaths(pathType)) {
if (!fs::isPathWritable(path))
continue;
auto fullPath = path / fs::path(fileName);
auto fullPath = path / std::fs::path(fileName);
if (!update || fs::exists(fullPath)) {
downloading = true;
@@ -258,13 +258,11 @@ namespace hex::plugin::builtin {
return true;
}
bool ViewStore::remove(ImHexPath pathType, const std::string &fileName) {
bool ViewStore::remove(fs::ImHexPath pathType, const std::string &fileName) {
bool removed = false;
for (const auto &path : hex::getPath(pathType)) {
std::error_code error;
bool removedFile = fs::remove(path / fs::path(fileName), error);
bool removedFolder = fs::remove(path / fs::path(fileName).stem(), error);
for (const auto &path : fs::getDefaultPaths(pathType)) {
bool removedFile = fs::remove(path / std::fs::path(fileName));
bool removedFolder = fs::remove(path / std::fs::path(fileName).stem());
removed = removed || removedFile || removedFolder;
}

View File

@@ -5,14 +5,14 @@
#include <hex/providers/provider.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/file.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
#include <yara.h>
#include <filesystem>
#include <thread>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
namespace hex::plugin::builtin {
@@ -22,9 +22,8 @@ namespace hex::plugin::builtin {
this->reloadRules();
ContentRegistry::FileHandler::add({ ".yar" }, [](const auto &path) {
for (const auto &destPath : hex::getPath(ImHexPath::Yara)) {
std::error_code error;
if (fs::copy_file(path, destPath / path.filename(), fs::copy_options::overwrite_existing, error)) {
for (const auto &destPath : fs::getDefaultPaths(fs::ImHexPath::Yara)) {
if (fs::copyFile(path, destPath / path.filename(), std::fs::copy_options::overwrite_existing)) {
View::showMessagePopup("hex.builtin.view.yara.rule_added"_lang);
return true;
}
@@ -134,13 +133,14 @@ namespace hex::plugin::builtin {
void ViewYara::reloadRules() {
this->m_rules.clear();
for (const auto path : hex::getPath(ImHexPath::Yara)) {
for (const auto path : fs::getDefaultPaths(fs::ImHexPath::Yara)) {
if (!fs::exists(path))
continue;
for (const auto &entry : fs::recursive_directory_iterator(path)) {
std::error_code error;
for (const auto &entry : std::fs::recursive_directory_iterator(path, error)) {
if (entry.is_regular_file() && entry.path().extension() == ".yar") {
this->m_rules.push_back({ fs::relative(entry.path(), fs::path(path)).string(), entry.path().string() });
this->m_rules.push_back({ std::fs::relative(entry.path(), std::fs::path(path)).string(), entry.path().string() });
}
}
}
@@ -172,7 +172,7 @@ namespace hex::plugin::builtin {
[](const char *includeName, const char *callingRuleFileName, const char *callingRuleNamespace, void *userData) -> const char * {
auto currFilePath = static_cast<const char *>(userData);
File file((fs::path(currFilePath).parent_path() / includeName).string(), File::Mode::Read);
fs::File file((std::fs::path(currFilePath).parent_path() / includeName).string(), fs::File::Mode::Read);
if (!file.isValid())
return nullptr;
@@ -189,7 +189,7 @@ namespace hex::plugin::builtin {
this->m_rules[this->m_selectedRule].second.data());
File file(this->m_rules[this->m_selectedRule].second, File::Mode::Read);
fs::File file(this->m_rules[this->m_selectedRule].second, fs::File::Mode::Read);
if (!file.isValid()) return;
if (yr_compiler_add_file(compiler, file.getHandle(), nullptr, nullptr) != 0) {

View File

@@ -4,7 +4,7 @@
#include <hex/api/localization.hpp>
#include <hex/api/plugin_manager.hpp>
#include <hex/ui/view.hpp>
#include <hex/helpers/paths.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
@@ -28,9 +28,9 @@ namespace hex::plugin::builtin {
static bool s_layoutConfigured = false;
static ImGui::Texture s_bannerTexture;
static std::string s_bannerTextureName;
static std::list<fs::path> s_recentFilePaths;
static std::list<std::fs::path> s_recentFilePaths;
static fs::path s_safetyBackupPath;
static std::fs::path s_safetyBackupPath;
static std::string s_tipOfTheDay;
@@ -153,7 +153,7 @@ namespace hex::plugin::builtin {
ImGui::SetCursorPosY(ImGui::GetCursorPosY() + 5_scaled);
{
for (auto &path : s_recentFilePaths) {
if (ImGui::BulletHyperlink(fs::path(path).filename().string().c_str())) {
if (ImGui::BulletHyperlink(std::fs::path(path).filename().string().c_str())) {
EventManager::post<RequestOpenFile>(path);
break;
}
@@ -384,7 +384,7 @@ namespace hex::plugin::builtin {
s_recentFilePaths.push_front(path);
{
std::list<fs::path> uniques;
std::list<std::fs::path> uniques;
for (auto &file : s_recentFilePaths) {
bool exists = false;
@@ -420,16 +420,16 @@ namespace hex::plugin::builtin {
ContentRegistry::Interface::addMenuItem("hex.builtin.menu.file", 1050, [&] {
if (ImGui::MenuItem("hex.builtin.view.hex_editor.menu.file.open_file"_lang, "CTRL + O")) {
hex::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, DialogMode::Open, {}, [](const auto &path) {
fs::openFileBrowser("hex.builtin.view.hex_editor.open_file"_lang, fs::DialogMode::Open, {}, [](const auto &path) {
EventManager::post<RequestOpenFile>(path);
});
}
if (ImGui::BeginMenu("hex.builtin.view.hex_editor.menu.file.open_recent"_lang, !s_recentFilePaths.empty())) {
// Copy to avoid chaning list while iteration
std::list<fs::path> recentFilePaths = s_recentFilePaths;
// Copy to avoid changing list while iteration
std::list<std::fs::path> recentFilePaths = s_recentFilePaths;
for (auto &path : recentFilePaths) {
auto filename = fs::path(path).filename().string();
auto filename = std::fs::path(path).filename().string();
if (ImGui::MenuItem(filename.c_str())) {
EventManager::post<RequestOpenFile>(path);
}
@@ -461,8 +461,8 @@ namespace hex::plugin::builtin {
constexpr auto CrashBackupFileName = "crash_backup.hexproj";
for (const auto &path : hex::getPath(ImHexPath::Config)) {
if (auto filePath = fs::path(path) / CrashBackupFileName; fs::exists(filePath)) {
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) {
if (auto filePath = std::fs::path(path) / CrashBackupFileName; fs::exists(filePath)) {
s_safetyBackupPath = filePath;
ImHexApi::Tasks::doLater([] { ImGui::OpenPopup("hex.builtin.welcome.safety_backup.title"_lang); });
}