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

View File

@@ -8,6 +8,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(LINUXDEPLOY_VERSION 0.1-alpha-1)
add_definitions(-DLINUXDEPLOY_VERSION="${LINUXDEPLOY_VERSION}")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
add_subdirectory(lib)
add_subdirectory(src)

View File

@@ -0,0 +1,21 @@
message(STATUS "Searching for libmagic")
find_library(LIBMAGIC_A libmagic.a)
if(NOT LIBMAGIC_A)
message(FATAL_ERROR "libmagic.a not found")
endif()
find_path(LIBMAGIC_MAGIC_H
NAMES magic.h
HINTS ${CMAKE_INSTALL_PREFIX}
PATH_SUFFIXES include include/linux
)
if(NOT LIBMAGIC_MAGIC_H)
message(FATAL_ERROR "magic.h not found")
endif()
add_library(libmagic_static INTERFACE IMPORTED)
set_property(TARGET libmagic_static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${LIBMAGIC_MAGIC_H})
set_property(TARGET libmagic_static PROPERTY INTERFACE_LINK_LIBRARIES ${LIBMAGIC_A})

View File

@@ -1,4 +1,5 @@
# globally include own includes
include_directories(${PROJECT_SOURCE_DIR}/include)
add_subdirectory(util)
add_subdirectory(core)

View File

@@ -7,8 +7,11 @@ file(GLOB HEADERS ${PROJECT_SOURCE_DIR}/include/linuxdeploy/core/*.h)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost REQUIRED COMPONENTS filesystem regex)
find_package(Threads)
find_package(LibMagic)
find_package(PkgConfig)
pkg_check_modules(magick++ REQUIRED IMPORTED_TARGET Magick++)
@@ -19,9 +22,8 @@ execute_process(
)
add_library(linuxdeploy_core STATIC elf.cpp log.cpp appdir.cpp desktopfile.cpp ${HEADERS})
target_link_libraries(linuxdeploy_core PUBLIC Boost::filesystem Boost::regex subprocess cpp-feather-ini-parser PkgConfig::magick++ ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries(linuxdeploy_core PUBLIC linuxdeploy_util Boost::filesystem Boost::regex subprocess cpp-feather-ini-parser PkgConfig::magick++ libmagic_static ${CMAKE_THREAD_LIBS_INIT})
target_include_directories(linuxdeploy_core PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(linuxdeploy_core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../util)
target_include_directories(linuxdeploy_core PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_compile_definitions(linuxdeploy_core PUBLIC -DBOOST_NO_CXX11_SCOPED_ENUMS)

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();
};
}
}
}