Sitelet https://github.com/linuxdeploy/linuxdeploy/commit/22df8758492fc3fbfcca0f11dac1407e7b63a1aa
Skip to content

Commit 22df875

Browse files
committed
Add libmagic wrapper
Also builds separate static library linuxdeploy_util providing old util header and new magic wrapper.
1 parent b64270c commit 22df875

7 files changed

Lines changed: 116 additions & 2 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
88
set(LINUXDEPLOY_VERSION 0.1-alpha-1)
99
add_definitions(-DLINUXDEPLOY_VERSION="${LINUXDEPLOY_VERSION}")
1010

11+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
12+
1113
add_subdirectory(lib)
1214

1315
add_subdirectory(src)

‎cmake/Modules/FindLibMagic.cmake‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
message(STATUS "Searching for libmagic")
2+
3+
find_library(LIBMAGIC_A libmagic.a)
4+
5+
if(NOT LIBMAGIC_A)
6+
message(FATAL_ERROR "libmagic.a not found")
7+
endif()
8+
9+
find_path(LIBMAGIC_MAGIC_H
10+
NAMES magic.h
11+
HINTS ${CMAKE_INSTALL_PREFIX}
12+
PATH_SUFFIXES include include/linux
13+
)
14+
15+
if(NOT LIBMAGIC_MAGIC_H)
16+
message(FATAL_ERROR "magic.h not found")
17+
endif()
18+
19+
add_library(libmagic_static INTERFACE IMPORTED)
20+
set_property(TARGET libmagic_static PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${LIBMAGIC_MAGIC_H})
21+
set_property(TARGET libmagic_static PROPERTY INTERFACE_LINK_LIBRARIES ${LIBMAGIC_A})

‎src/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# globally include own includes
22
include_directories(${PROJECT_SOURCE_DIR}/include)
33

4+
add_subdirectory(util)
45
add_subdirectory(core)

‎src/core/CMakeLists.txt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ file(GLOB HEADERS ${PROJECT_SOURCE_DIR}/include/linuxdeploy/core/*.h)
77

88
set(Boost_USE_STATIC_LIBS ON)
99
find_package(Boost REQUIRED COMPONENTS filesystem regex)
10+
1011
find_package(Threads)
1112

13+
find_package(LibMagic)
14+
1215
find_package(PkgConfig)
1316
pkg_check_modules(magick++ REQUIRED IMPORTED_TARGET Magick++)
1417

@@ -19,9 +22,8 @@ execute_process(
1922
)
2023

2124
add_library(linuxdeploy_core STATIC elf.cpp log.cpp appdir.cpp desktopfile.cpp ${HEADERS})
22-
target_link_libraries(linuxdeploy_core PUBLIC Boost::filesystem Boost::regex subprocess cpp-feather-ini-parser PkgConfig::magick++ ${CMAKE_THREAD_LIBS_INIT})
25+
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})
2326
target_include_directories(linuxdeploy_core PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
24-
target_include_directories(linuxdeploy_core PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../util)
2527
target_include_directories(linuxdeploy_core PUBLIC ${PROJECT_SOURCE_DIR}/include)
2628
target_compile_definitions(linuxdeploy_core PUBLIC -DBOOST_NO_CXX11_SCOPED_ENUMS)
2729

‎src/util/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_library(linuxdeploy_util STATIC magicwrapper.cpp magicwrapper.h util.h)
2+
target_include_directories(linuxdeploy_util PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

‎src/util/magicwrapper.cpp‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// system includes
2+
#include <magic.h>
3+
#include <string>
4+
5+
// local includes
6+
#include "magicwrapper.h"
7+
8+
namespace linuxdeploy {
9+
namespace util {
10+
namespace magic {
11+
class MagicWrapper::PrivateData {
12+
private:
13+
magic_t cookie;
14+
15+
public:
16+
PrivateData() noexcept(false) {
17+
cookie = magic_open(MAGIC_DEBUG | MAGIC_SYMLINK | MAGIC_MIME_TYPE);
18+
19+
if (cookie == nullptr)
20+
throw MagicError("Failed to open magic database");
21+
}
22+
23+
~PrivateData() {
24+
if (cookie != nullptr) {
25+
magic_close(cookie);
26+
cookie = nullptr;
27+
}
28+
};
29+
30+
std::string fileType(const std::string& path) {
31+
const auto* buf = magic_file(cookie, path.c_str());
32+
33+
if (buf == nullptr)
34+
return "";
35+
36+
return buf;
37+
}
38+
};
39+
40+
MagicWrapper::MagicWrapper() {
41+
d = new PrivateData();
42+
}
43+
44+
MagicWrapper::~MagicWrapper() {
45+
delete d;
46+
}
47+
}
48+
}
49+
}

‎src/util/magicwrapper.h‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// system includes
2+
#include <exception>
3+
#include <string>
4+
#include <utility>
5+
6+
#pragma once
7+
8+
namespace linuxdeploy {
9+
namespace util {
10+
namespace magic {
11+
// thrown by constructors if opening the magic database fails
12+
class MagicError : std::exception {
13+
private:
14+
const std::string message;
15+
16+
public:
17+
explicit MagicError() : message() {}
18+
19+
explicit MagicError(std::string message) : message(std::move(message)) {}
20+
21+
const char* what() {
22+
return message.c_str();
23+
}
24+
};
25+
26+
class MagicWrapper {
27+
private:
28+
class PrivateData;
29+
PrivateData *d;
30+
31+
public:
32+
MagicWrapper();
33+
~MagicWrapper();
34+
};
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)