mirror of https://github.com/WerWolv/ImHex.git
store: Fixed more download issues when some folders don't have write perms
This commit is contained in:
parent
2847098020
commit
5a02c38fcd
|
@ -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; }
|
||||
|
|
|
@ -44,6 +44,7 @@ namespace hex {
|
|||
std::string encodeByteString(const std::vector<u8> &bytes);
|
||||
std::vector<u8> 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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -23,6 +23,7 @@
|
|||
#endif
|
||||
|
||||
#include <hex/helpers/logger.hpp>
|
||||
#include <hex/helpers/file.hpp>
|
||||
|
||||
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<nfdfilteritem_t> &validExtensions, const std::function<void(fs::path)> &callback, const std::string &defaultPath) {
|
||||
NFD::Init();
|
||||
|
|
|
@ -8,8 +8,6 @@
|
|||
#include "init/splash_window.hpp"
|
||||
#include "init/tasks.hpp"
|
||||
|
||||
#include <hex/helpers/file.hpp>
|
||||
|
||||
int main(int argc, char **argv, char **envp) {
|
||||
using namespace hex;
|
||||
ImHexApi::System::impl::setProgramArguments(argc, argv, envp);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue