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:
parent
26e7e12f09
commit
4d6e6cf75a
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user