Add libmagic wrapper

Also builds separate static library linuxdeploy_util providing old util
header and new magic wrapper.
This commit is contained in:
TheAssassin
2018-06-03 02:09:24 +02:00
parent b64270c37b
commit 22df875849
7 changed files with 116 additions and 2 deletions

2
src/util/CMakeLists.txt Normal file
View File

@@ -0,0 +1,2 @@
add_library(linuxdeploy_util STATIC magicwrapper.cpp magicwrapper.h util.h)
target_include_directories(linuxdeploy_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

49
src/util/magicwrapper.cpp Normal file
View File

@@ -0,0 +1,49 @@
// system includes
#include <magic.h>
#include <string>
// local includes
#include "magicwrapper.h"
namespace linuxdeploy {
namespace util {
namespace magic {
class MagicWrapper::PrivateData {
private:
magic_t cookie;
public:
PrivateData() noexcept(false) {
cookie = magic_open(MAGIC_DEBUG | MAGIC_SYMLINK | MAGIC_MIME_TYPE);
if (cookie == nullptr)
throw MagicError("Failed to open magic database");
}
~PrivateData() {
if (cookie != nullptr) {
magic_close(cookie);
cookie = nullptr;
}
};
std::string fileType(const std::string& path) {
const auto* buf = magic_file(cookie, path.c_str());
if (buf == nullptr)
return "";
return buf;
}
};
MagicWrapper::MagicWrapper() {
d = new PrivateData();
}
MagicWrapper::~MagicWrapper() {
delete d;
}
}
}
}

37
src/util/magicwrapper.h Normal file
View File

@@ -0,0 +1,37 @@
// system includes
#include <exception>
#include <string>
#include <utility>
#pragma once
namespace linuxdeploy {
namespace util {
namespace magic {
// thrown by constructors if opening the magic database fails
class MagicError : std::exception {
private:
const std::string message;
public:
explicit MagicError() : message() {}
explicit MagicError(std::string message) : message(std::move(message)) {}
const char* what() {
return message.c_str();
}
};
class MagicWrapper {
private:
class PrivateData;
PrivateData *d;
public:
MagicWrapper();
~MagicWrapper();
};
}
}
}