diff --git a/lib/libimhex/include/hex/helpers/file.hpp b/lib/libimhex/include/hex/helpers/file.hpp index c43c14eb1..fe4bcc47d 100644 --- a/lib/libimhex/include/hex/helpers/file.hpp +++ b/lib/libimhex/include/hex/helpers/file.hpp @@ -56,7 +56,7 @@ namespace hex { void setSize(u64 size); void flush(); - void remove(); + bool remove(); auto getHandle() { return this->m_file; } const fs::path &getPath() { return this->m_path; } diff --git a/lib/libimhex/include/hex/helpers/utils.hpp b/lib/libimhex/include/hex/helpers/utils.hpp index d697fd0a7..bd042cf79 100644 --- a/lib/libimhex/include/hex/helpers/utils.hpp +++ b/lib/libimhex/include/hex/helpers/utils.hpp @@ -44,6 +44,7 @@ namespace hex { std::string encodeByteString(const std::vector &bytes); std::vector decodeByteString(const std::string &string); + bool isPathWritable(fs::path path); [[nodiscard]] constexpr inline u64 extract(u8 from, u8 to, const hex::unsigned_integral auto &value) { if (from < to) std::swap(from, to); diff --git a/lib/libimhex/source/helpers/file.cpp b/lib/libimhex/source/helpers/file.cpp index 84939b015..7fcf530dd 100644 --- a/lib/libimhex/source/helpers/file.cpp +++ b/lib/libimhex/source/helpers/file.cpp @@ -122,9 +122,9 @@ namespace hex { fflush(this->m_file); } - void File::remove() { + bool File::remove() { this->close(); - std::remove(this->m_path.string().c_str()); + return std::remove(this->m_path.string().c_str()) == 0; } } \ No newline at end of file diff --git a/lib/libimhex/source/helpers/utils.cpp b/lib/libimhex/source/helpers/utils.cpp index fbf2b1435..3cecf2405 100644 --- a/lib/libimhex/source/helpers/utils.cpp +++ b/lib/libimhex/source/helpers/utils.cpp @@ -23,6 +23,7 @@ #endif #include +#include namespace hex { @@ -393,6 +394,23 @@ namespace hex { return result; } + bool isPathWritable(fs::path path) { + constexpr static auto TestFileName = "__imhex__tmp__"; + { + File file(path / TestFileName, File::Mode::Read); + if (file.isValid()) { + if (!file.remove()) + return false; + } + } + + File file(path / TestFileName, File::Mode::Create); + bool result = file.isValid(); + if (!file.remove()) + return false; + + return result; + } bool openFileBrowser(const std::string &title, DialogMode mode, const std::vector &validExtensions, const std::function &callback, const std::string &defaultPath) { NFD::Init(); diff --git a/main/source/main.cpp b/main/source/main.cpp index 870620bb0..7e6dce816 100644 --- a/main/source/main.cpp +++ b/main/source/main.cpp @@ -8,8 +8,6 @@ #include "init/splash_window.hpp" #include "init/tasks.hpp" -#include - int main(int argc, char **argv, char **envp) { using namespace hex; ImHexApi::System::impl::setProgramArguments(argc, argv, envp); diff --git a/plugins/builtin/source/content/views/view_store.cpp b/plugins/builtin/source/content/views/view_store.cpp index b0b51ce0c..2030bfc2b 100644 --- a/plugins/builtin/source/content/views/view_store.cpp +++ b/plugins/builtin/source/content/views/view_store.cpp @@ -110,7 +110,7 @@ namespace hex::plugin::builtin { downloadDoneCallback(entry); } else - log::error("Download failed!"); + log::error("Download failed! HTTP Code {}", response.code); this->m_download = {}; @@ -186,7 +186,7 @@ namespace hex::plugin::builtin { auto path = folder / fs::path(storeEntry.fileName); - if (fs::exists(path)) { + if (fs::exists(path) && hex::isPathWritable(folder)) { storeEntry.installed = true; std::ifstream file(path, std::ios::in | std::ios::binary); @@ -237,7 +237,11 @@ namespace hex::plugin::builtin { bool ViewStore::download(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)) + continue; + auto fullPath = path / fs::path(fileName); + if (!update || fs::exists(fullPath)) { downloading = true; this->m_downloadPath = fullPath; @@ -257,8 +261,10 @@ namespace hex::plugin::builtin { bool ViewStore::remove(ImHexPath pathType, const std::string &fileName) { bool removed = false; for (const auto &path : hex::getPath(pathType)) { - bool removedFile = fs::remove(path / fs::path(fileName)); - bool removedFolder = fs::remove(path / fs::path(fileName).stem()); + std::error_code error; + + bool removedFile = fs::remove(path / fs::path(fileName), error); + bool removedFolder = fs::remove(path / fs::path(fileName).stem(), error); removed = removed || removedFile || removedFolder; }