1
0
mirror of synced 2025-01-18 09:04:52 +01:00

fix: Magic compile dumping files into cwd (#1212)

This PR fix libmagic dumping files in the imhex cwd when compiling them

This code was actually written by you (notice the source branch), this
PR is just a reminder that the fix works and you can merge it ^^

---------

Co-authored-by: WerWolv <werwolv98@gmail.com>
This commit is contained in:
iTrooz 2023-07-30 21:36:48 +02:00 committed by GitHub
parent 26e7e12f09
commit 4d6e6cf75a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@
#include <hex/helpers/utils.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
#include <wolv/utils/guards.hpp>
#include <wolv/utils/string.hpp>
@ -13,6 +14,7 @@
#include <string>
#include <magic.h>
#include <unistd.h>
#if defined(OS_WINDOWS)
#define MAGIC_PATH_SEPARATOR ";"
@ -52,7 +54,32 @@ namespace hex::magic {
if (!magicFiles.has_value())
return false;
return magic_compile(ctx, magicFiles->c_str()) == 0;
std::array<char, 1024> cwd = { 0x00 };
if (getcwd(cwd.data(), cwd.size()) == nullptr)
return false;
std::optional<std::fs::path> magicFolder;
for (const auto &dir : fs::getDefaultPaths(fs::ImHexPath::Magic)) {
if (std::fs::exists(dir) && fs::isPathWritable(dir)) {
magicFolder = dir;
break;
}
}
if (!magicFolder.has_value()) {
log::error("Could not find a writable magic folder");
return false;
}
if (chdir(wolv::util::toUTF8String(*magicFolder).c_str()) != 0)
return false;
auto result = magic_compile(ctx, magicFiles->c_str()) == 0;
if (chdir(cwd.data()) != 0)
return false;
return result;
}
std::string getDescription(const std::vector<u8> &data) {