diff --git a/CMakeLists.txt b/CMakeLists.txt index 7094a10..2887071 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/cmake/Modules/FindLibMagic.cmake b/cmake/Modules/FindLibMagic.cmake new file mode 100644 index 0000000..84fbe69 --- /dev/null +++ b/cmake/Modules/FindLibMagic.cmake @@ -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}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fb58c3e..0d88b21 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,5 @@ # globally include own includes include_directories(${PROJECT_SOURCE_DIR}/include) +add_subdirectory(util) add_subdirectory(core) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 7923183..762b40d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -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) diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt new file mode 100644 index 0000000..a49ec18 --- /dev/null +++ b/src/util/CMakeLists.txt @@ -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}) diff --git a/src/util/magicwrapper.cpp b/src/util/magicwrapper.cpp new file mode 100644 index 0000000..6a0c2db --- /dev/null +++ b/src/util/magicwrapper.cpp @@ -0,0 +1,49 @@ +// system includes +#include +#include + +// 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; + } + } + } +} diff --git a/src/util/magicwrapper.h b/src/util/magicwrapper.h new file mode 100644 index 0000000..6404b0a --- /dev/null +++ b/src/util/magicwrapper.h @@ -0,0 +1,37 @@ +// system includes +#include +#include +#include + +#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(); + }; + } + } +}