From 74d59705adaabb1c2a0afc372f250708cd2cba58 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 19 Feb 2024 22:22:59 +0100 Subject: [PATCH] fix: Issues where files are only being partially overwritten instead of truncated --- lib/libimhex/source/api/achievement_manager.cpp | 4 +--- lib/libimhex/source/api/content_registry.cpp | 3 +-- lib/libimhex/source/api/workspace_manager.cpp | 2 ++ .../builtin/source/content/providers/file_provider.cpp | 10 +--------- 4 files changed, 5 insertions(+), 14 deletions(-) diff --git a/lib/libimhex/source/api/achievement_manager.cpp b/lib/libimhex/source/api/achievement_manager.cpp index 431a7338b..09fe516df 100644 --- a/lib/libimhex/source/api/achievement_manager.cpp +++ b/lib/libimhex/source/api/achievement_manager.cpp @@ -237,8 +237,7 @@ namespace hex { for (const auto &directory : fs::getDefaultPaths(fs::ImHexPath::Config)) { auto path = directory / AchievementsFile; - wolv::io::File file(path, wolv::io::File::Mode::Write); - + wolv::io::File file(path, wolv::io::File::Mode::Create); if (!file.isValid()) { continue; } @@ -254,7 +253,6 @@ namespace hex { } const auto result = json.dump(4); - file.setSize(0); file.writeString(result); break; } diff --git a/lib/libimhex/source/api/content_registry.cpp b/lib/libimhex/source/api/content_registry.cpp index 5620179a6..f15285395 100644 --- a/lib/libimhex/source/api/content_registry.cpp +++ b/lib/libimhex/source/api/content_registry.cpp @@ -130,10 +130,9 @@ namespace hex { return; } for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Config)) { - wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Write); + wolv::io::File file(dir / SettingsFile, wolv::io::File::Mode::Create); if (file.isValid()) { - file.setSize(0); file.writeString(result); break; } diff --git a/lib/libimhex/source/api/workspace_manager.cpp b/lib/libimhex/source/api/workspace_manager.cpp index 617e5cfbf..645977d47 100644 --- a/lib/libimhex/source/api/workspace_manager.cpp +++ b/lib/libimhex/source/api/workspace_manager.cpp @@ -40,6 +40,7 @@ namespace hex { wolv::io::File file(path, wolv::io::File::Mode::Read); if (!file.isValid()) { log::error("Failed to load workspace from file '{}'", path.string()); + file.remove(); return; } @@ -56,6 +57,7 @@ namespace hex { }; } catch (nlohmann::json::exception &e) { log::error("Failed to load workspace from file '{}': {}", path.string(), e.what()); + file.remove(); } } diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index 4d764ab70..383ed962c 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -83,15 +83,7 @@ namespace hex::plugin::builtin { } void FileProvider::resizeRaw(u64 newSize) { - this->close(); - - { - wolv::io::File file(m_path, wolv::io::File::Mode::Write); - - file.setSize(newSize); - } - - (void)this->open(); + m_file.setSize(newSize); } void FileProvider::insertRaw(u64 offset, u64 size) {