Skip to content

Commit f295f1e

Browse files
committed
draft
1 parent 0784db6 commit f295f1e

11 files changed

Lines changed: 170 additions & 66 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ option(ODR_CLI "enable command line interface" ON)
1616
option(ODR_CLANG_TIDY "Run clang-tidy static analysis" OFF)
1717
option(WITH_PDF2HTMLEX "Build with pdf2htmlEX" ON)
1818
option(WITH_WVWARE "Build with wvWare" ON)
19-
option(WITH_CUSTOM_TMPFILE "Build with custom temporary file implementation" OFF)
2019

2120
# TODO defining global compiler flags seems to be bad practice with conan
2221
# TODO consider using conan profiles
@@ -245,15 +244,19 @@ if (WITH_WVWARE)
245244
ODR_WITH_WVWARE
246245
)
247246
endif ()
248-
if (WITH_CUSTOM_TMPFILE)
249-
find_package(tmpfile REQUIRED CONFIG)
247+
if (WITH_LIBMAGIC)
248+
find_package(libmagic REQUIRED)
249+
target_sources(odr
250+
PRIVATE
251+
"src/odr/internal/libmagic/libmagic.cpp"
252+
)
250253
target_link_libraries(odr
251254
PRIVATE
252-
tmpfile::tmpfile
255+
libmagic::libmagic
253256
)
254257
target_compile_definitions(odr
255258
PRIVATE
256-
ODR_WITH_CUSTOM_TMPFILE
259+
ODR_WITH_LIBMAGIC
257260
)
258261
endif ()
259262

conan.lock

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,79 @@
11
{
22
"version": "0.5",
33
"requires": [
4-
"zlib/1.3.1#b8bc2603263cf7eccbd6e17e66b0ed76%1754336059.551",
5-
"wvware/1.2.9-odr#feeef48fa83e803330bbd3c706718618%1754339134.808372",
6-
"vincentlaucsb-csv-parser/2.3.0#ac67e368e82c9e3da4a663c35e3a1b2f%1754336215.262",
7-
"utfcpp/4.0.4#cd6efc5d62de4e9ee0b5f3abd0df37a9%1754336214.459",
8-
"uchardet/0.0.8#a9026ce72d955751db2dbda62c83c430%1754336213.652",
9-
"pugixml/1.14#c6afdcf73d71858303d8260b0d76ff91%1754336212.85",
10-
"poppler-data/0.4.12-odr#06cdb12e4cab52261a5eb6c7d7dad273%1754339136.6022458",
11-
"poppler/24.08.0-odr#2929132c6c3c67155c6c8f050c923916%1754339139.168313",
4+
"zstd/1.5.7#fde461c0d847a22f16d3066774f61b11%1744114235.235",
5+
"zlib/1.3.1#b8bc2603263cf7eccbd6e17e66b0ed76%1733936244.862",
6+
"xz_utils/5.4.5#b885d1d79c9d30cff3803f7f551dbe66%1724318972.064",
7+
"wvware/1.2.9-odr#feeef48fa83e803330bbd3c706718618%1754432357.893",
8+
"vincentlaucsb-csv-parser/2.3.0#ac67e368e82c9e3da4a663c35e3a1b2f%1718528275.177",
9+
"util-linux-libuuid/2.39.2#637bd312b6310c18190469fae4e1d480%1748076007.711",
10+
"utfcpp/4.0.4#cd6efc5d62de4e9ee0b5f3abd0df37a9%1722968964.685",
11+
"uchardet/0.0.8#a9026ce72d955751db2dbda62c83c430%1703972855.616",
12+
"pugixml/1.14#c6afdcf73d71858303d8260b0d76ff91%1696206310.014",
13+
"poppler-data/0.4.12-odr#06cdb12e4cab52261a5eb6c7d7dad273%1754427610.1551769",
14+
"poppler/24.08.0-odr#2929132c6c3c67155c6c8f050c923916%1754427612.566178",
1215
"pixman/0.43.4#0dcdf859941e32fcc7bfb73ea1946a7f%1718828937.421",
13-
"pdf2htmlex/0.18.8.rc1-odr-git-eb5d291#01227c46d8bced149c1e2dd031406e57%1754339135.855592",
14-
"pcre2/10.42#9a35f5089feb875ec61a38eca364ce77%1754336050.663",
16+
"pdf2htmlex/0.18.8.rc1-odr-git-eb5d291#01227c46d8bced149c1e2dd031406e57%1754427609.135946",
17+
"pcre2/10.42#9a35f5089feb875ec61a38eca364ce77%1743524593.693",
1518
"openlibm/0.8.3#61d8454cf655e95e0d3d3359bde58ac7%1754339136.456942",
1619
"openjpeg/2.5.2#6f7b733e151d1bbf5ed05cbabb846828%1709653017.024",
17-
"nlohmann_json/3.11.3#45828be26eb619a2e04ca517bb7b828d%1754336206.708",
18-
"miniz/3.0.2#bfbce07c6654293cce27ee24129d2df7%1754336205.867",
20+
"nlohmann_json/3.11.3#45828be26eb619a2e04ca517bb7b828d%1701220705.259",
21+
"miniz/3.0.2#bfbce07c6654293cce27ee24129d2df7%1743673472.805",
1922
"lzo/2.10#5725914235423c771cb1c6b607109b45%1685099992.798",
20-
"libxml2/2.12.7#1c4d20b7ab8b618ce699733723ba4df6%1754336034.974",
21-
"libpng/1.6.48#dd0fc04a42b9a23bce065545a81d4847%1754336033.871",
22-
"libjpeg/9f#8edfe2699565c80c825d0256002504ff%1754336032.766",
23-
"libiconv/1.17#1e65319e945f2d31941a9d28cc13c058%1754336113.172",
24-
"libgsf/1.14.52#9b22c41267004c80ba5cde5d01e23a24%1754339136.153343",
25-
"libgettext/0.22#35d2811b2dd27a98f69e4daa86ca2000%1754336030.102",
26-
"libffi/3.4.8#06926dca35bcf8e321fcc24def952cde%1754336028.993",
27-
"libelf/0.8.13#ba59bbc89757ed62cfd7690a73bf81be%1754336027.877",
23+
"libxml2/2.12.7#1c4d20b7ab8b618ce699733723ba4df6%1721306327.767",
24+
"libselinux/3.6#5a78ff6ae5034eeaac8da723361a8ce4%1748075177.52",
25+
"libpng/1.6.48#dd0fc04a42b9a23bce065545a81d4847%1746141949.748",
26+
"libmagic/5.45#791d5bad38d33272bb120994a198b1ac%1727273086.09",
27+
"libjpeg/9f#8edfe2699565c80c825d0256002504ff%1723665907.087",
28+
"libiconv/1.17#1e65319e945f2d31941a9d28cc13c058%1751451666.321",
29+
"libgsf/1.14.52#9b22c41267004c80ba5cde5d01e23a24%1754427609.682325",
30+
"libgettext/0.22#35d2811b2dd27a98f69e4daa86ca2000%1714393058.647",
31+
"libffi/3.4.8#06926dca35bcf8e321fcc24def952cde%1748531860.405",
32+
"libelf/0.8.13#ba59bbc89757ed62cfd7690a73bf81be%1741781951.327",
2833
"lcms/2.16#fb083506ff40fd950c9e5c39df8bed54%1703969656.459",
29-
"gtest/1.14.0#f8f0757a574a8dd747d16af62d6eb1b7%1754336203.113",
30-
"glib/2.81.0-odr#ddf445d5af468f972978af93c44d26e1%1754339135.261312",
31-
"giflib/5.2.2#3923fc0f7ffec2f0bdbdee9b548f9248%1754336019.711",
32-
"freetype/2.13.2#5d2563803c8558d4ef47271a82c73d20%1754336015.019",
33-
"fontforge/20240423-git#525c82a5e57385c14b647b966e86ea58%1754339139.32232",
34-
"fontconfig/2.15.0-odr#2febbef44ca469204c8ff38cfd21db59%1754339136.30191",
34+
"gtest/1.14.0#f8f0757a574a8dd747d16af62d6eb1b7%1743410807.169",
35+
"glib/2.81.0-odr#ddf445d5af468f972978af93c44d26e1%1754427608.367101",
36+
"giflib/5.2.2#3923fc0f7ffec2f0bdbdee9b548f9248%1731663927.701",
37+
"freetype/2.13.2#5d2563803c8558d4ef47271a82c73d20%1728736671.752",
38+
"fontforge/20240423-git#525c82a5e57385c14b647b966e86ea58%1754432335.33",
39+
"fontconfig/2.15.0-odr#2febbef44ca469204c8ff38cfd21db59%1754427609.845061",
3540
"expat/2.6.3#39b80d3109fbe578fddfe4951f0b1d57%1725469045.298",
36-
"cryptopp/8.9.0#fe3de584c28c0ecc938a1671e3f1bd72%1754336202.171",
37-
"cpp-httplib/0.16.3#7aa89fbb81ffd19539a49fc132502966%1754336201.354",
38-
"cairo/1.18.0-odr#c1d0ad14a91ad6d161e756d54277ac13%1754339136.007648",
39-
"bzip2/1.0.8#00b4a4658791c1f06914e087f0e792f5%1754336009.237",
40-
"brotli/1.1.0#406ce8f1c997f4ef7852fa01ff85ef9f%1754336007.831",
41+
"cryptopp/8.9.0#fe3de584c28c0ecc938a1671e3f1bd72%1731421245.374",
42+
"cpp-httplib/0.16.3#7aa89fbb81ffd19539a49fc132502966%1748426320.106",
43+
"cairo/1.18.0-odr#c1d0ad14a91ad6d161e756d54277ac13%1754427609.450017",
44+
"bzip2/1.0.8#00b4a4658791c1f06914e087f0e792f5%1744702067.178",
45+
"brotli/1.1.0#406ce8f1c997f4ef7852fa01ff85ef9f%1743158659.041",
4146
"boost/1.86.0#cd839a2082585255010f9e82eea94c7f%1728027203.247",
42-
"argon2/20190702-odr#965901884bc82ec8a7c0a1305d42c127%1754339135.113191",
43-
"libselinux/3.6#5a78ff6ae5034eeaac8da723361a8ce4%1748075177.52",
44-
"util-linux-libuuid/2.39.2#637bd312b6310c18190469fae4e1d480%1748076007.711"
47+
"argon2/20190702-odr#965901884bc82ec8a7c0a1305d42c127%1754427608.210625"
4548
],
4649
"build_requires": [
47-
"zlib/1.3.1#b8bc2603263cf7eccbd6e17e66b0ed76%1754336059.551",
48-
"pkgconf/2.2.0#6462942a22803086372db44689ba825f%1754336122.883",
49-
"pkgconf/2.1.0#27f44583701117b571307cf5b5fe5605%1754336055.341",
50-
"pkgconf/2.0.3#f996677e96e61e6552d85e83756c328b%1754336054.459",
51-
"pcre2/10.42#9a35f5089feb875ec61a38eca364ce77%1754336050.663",
52-
"ninja/1.13.0#53ff096207a5599ced46a633271b3cef%1754336120.489",
53-
"meson/1.4.0#2262941cc8fbb0099dd0c196ca2a6c01%1754336038.389",
54-
"meson/1.3.2#26ce8a76a36cc275cdfee1d757bc6561%1754336037.623",
55-
"meson/1.2.2#21b73818ba96d9eea465b310b5bbc993%1754336037.251",
50+
"zlib/1.3.1#b8bc2603263cf7eccbd6e17e66b0ed76%1733936244.862",
51+
"pkgconf/2.2.0#6462942a22803086372db44689ba825f%1713364853.749",
52+
"pkgconf/2.1.0#27f44583701117b571307cf5b5fe5605%1701537936.436",
53+
"pkgconf/2.0.3#f996677e96e61e6552d85e83756c328b%1696606182.229",
54+
"pcre2/10.42#9a35f5089feb875ec61a38eca364ce77%1743524593.693",
55+
"ninja/1.13.0#53ff096207a5599ced46a633271b3cef%1751046277.036",
56+
"meson/1.4.0#2262941cc8fbb0099dd0c196ca2a6c01%1726730116.631",
57+
"meson/1.3.2#26ce8a76a36cc275cdfee1d757bc6561%1726730118.251",
58+
"meson/1.2.2#21b73818ba96d9eea465b310b5bbc993%1726730120.212",
5659
"meson/1.2.1#f2b0c7763308df8e33172744dace8845%1726730117.905",
57-
"m4/1.4.19#b38ced39a01e31fef5435bc634461fd2%1754336036.417",
58-
"libtool/2.4.7#a182d7ce8d4c346a19dbd4a5d532ef68%1754336116.018",
59-
"libiconv/1.17#1e65319e945f2d31941a9d28cc13c058%1754336113.172",
60-
"libgettext/0.22#35d2811b2dd27a98f69e4daa86ca2000%1754336030.102",
61-
"libffi/3.4.8#06926dca35bcf8e321fcc24def952cde%1754336028.993",
62-
"libelf/0.8.13#ba59bbc89757ed62cfd7690a73bf81be%1754336027.877",
63-
"gtk-doc-stub/cci.20181216#09072d684ce1458596b44a30a747494c%1754336106.527",
64-
"gperf/3.1#1d622ad9717e9348ed3685c9994ad0b9%1709324989.76",
65-
"gnu-config/cci.20210814#dc430d754f465e8c74463019672fb97b%1754336027.04",
66-
"glib/2.81.0-odr#ddf445d5af468f972978af93c44d26e1%1754339135.261312",
67-
"gettext/0.22.5#a1f31cc77dee0345699745ef39686dd0%1754336091.687",
68-
"cmake/3.31.8#dd6e07c418afc4b30cb1c21584dccc49%1754336200.956",
69-
"bzip2/1.0.8#00b4a4658791c1f06914e087f0e792f5%1754336009.237",
70-
"automake/1.16.5#058bda3e21c36c9aa8425daf3c1faf50%1754336006.921",
71-
"autoconf/2.71#f9307992909d7fb3df459340f1932809%1754336006.057",
60+
"m4/1.4.19#b38ced39a01e31fef5435bc634461fd2%1700758725.451",
61+
"libtool/2.4.7#a182d7ce8d4c346a19dbd4a5d532ef68%1742900203.747",
7262
"libselinux/3.6#5a78ff6ae5034eeaac8da723361a8ce4%1748075177.52",
73-
"flex/2.6.4#e35bc44b3fcbcd661e0af0dc5b5b1ad4%1748075168.656"
63+
"libiconv/1.17#1e65319e945f2d31941a9d28cc13c058%1751451666.321",
64+
"libgettext/0.22#35d2811b2dd27a98f69e4daa86ca2000%1714393058.647",
65+
"libffi/3.4.8#06926dca35bcf8e321fcc24def952cde%1748531860.405",
66+
"libelf/0.8.13#ba59bbc89757ed62cfd7690a73bf81be%1741781951.327",
67+
"gtk-doc-stub/cci.20181216#09072d684ce1458596b44a30a747494c%1687277608.37",
68+
"gperf/3.1#1d622ad9717e9348ed3685c9994ad0b9%1709324989.76",
69+
"gnu-config/cci.20210814#dc430d754f465e8c74463019672fb97b%1701248168.479",
70+
"glib/2.81.0-odr#ddf445d5af468f972978af93c44d26e1%1754427608.367101",
71+
"gettext/0.22.5#a1f31cc77dee0345699745ef39686dd0%1750252839.982",
72+
"flex/2.6.4#e35bc44b3fcbcd661e0af0dc5b5b1ad4%1748075168.656",
73+
"cmake/3.31.8#dd6e07c418afc4b30cb1c21584dccc49%1750223587.75",
74+
"bzip2/1.0.8#00b4a4658791c1f06914e087f0e792f5%1744702067.178",
75+
"automake/1.16.5#058bda3e21c36c9aa8425daf3c1faf50%1688481772.751",
76+
"autoconf/2.71#f9307992909d7fb3df459340f1932809%1711983104.648"
7477
],
7578
"python_requires": [],
7679
"config_requires": []

conanfile.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ class OpenDocumentCoreConan(ConanFile):
1919
"fPIC": [True, False],
2020
"with_pdf2htmlEX": [True, False],
2121
"with_wvWare": [True, False],
22+
"with_libmagic": [True, False],
2223
}
2324
default_options = {
2425
"shared": False,
2526
"fPIC": True,
2627
"with_pdf2htmlEX": True,
2728
"with_wvWare": True,
29+
"with_libmagic": True,
2830
}
2931

3032
exports_sources = ["cli/*", "cmake/*", "resources/dist/*", "src/*", "CMakeLists.txt"]
@@ -49,6 +51,8 @@ def requirements(self):
4951
self.requires("wvware/1.2.9-odr")
5052
self.requires("cpp-httplib/0.16.3")
5153
self.requires("argon2/20190702-odr")
54+
if self.options.get_safe("with_libmagic", False):
55+
self.requires("libmagic/5.45")
5256

5357
def build_requirements(self):
5458
self.test_requires("gtest/1.14.0")
@@ -67,6 +71,7 @@ def generate(self):
6771
tc.variables["ODR_TEST"] = False
6872
tc.variables["WITH_PDF2HTMLEX"] = self.options.get_safe("with_pdf2htmlEX", False)
6973
tc.variables["WITH_WVWARE"] = self.options.get_safe("with_wvWare", False)
74+
tc.variables["WITH_LIBMAGIC"] = self.options.get_safe("with_libmagic", False)
7075

7176
# Get runenv info, exported by package_info() of dependencies
7277
# We need to obtain PDF2HTMLEX_DATA_DIR, POPPLER_DATA_DIR, FONTCONFIG_PATH and WVDATADIR

src/odr/global_params.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const std::string &GlobalParams::pdf2htmlex_data_path() {
4141
return instance().m_pdf2htmlex_data_path;
4242
}
4343

44+
const std::string &GlobalParams::libmagic_data_path() {
45+
return instance().m_libmagic_data_path;
46+
}
47+
4448
void GlobalParams::set_odr_core_data_path(const std::string &path) {
4549
instance().m_odr_core_data_path = path;
4650
}
@@ -62,10 +66,15 @@ void GlobalParams::set_pdf2htmlex_data_path(const std::string &path) {
6266
instance().m_pdf2htmlex_data_path = path;
6367
}
6468

69+
void GlobalParams::set_libmagic_data_path(const std::string &path) {
70+
instance().m_libmagic_data_path = path;
71+
}
72+
6573
GlobalParams::GlobalParams()
6674
: m_odr_core_data_path{internal::project_info::odr_data_path()},
6775
m_fontconfig_data_path{internal::project_info::fontconfig_data_path()},
6876
m_poppler_data_path{internal::project_info::poppler_data_path()},
69-
m_pdf2htmlex_data_path{internal::project_info::pdf2htmlex_data_path()} {}
77+
m_pdf2htmlex_data_path{internal::project_info::pdf2htmlex_data_path()},
78+
m_libmagic_data_path{internal::project_info::libmagic_data_path()} {}
7079

7180
} // namespace odr

src/odr/global_params.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ class GlobalParams {
1010
static const std::string &fontconfig_data_path();
1111
static const std::string &poppler_data_path();
1212
static const std::string &pdf2htmlex_data_path();
13+
static const std::string &libmagic_data_path();
1314

1415
static void set_odr_core_data_path(const std::string &path);
1516
static void set_fontconfig_data_path(const std::string &path);
1617
static void set_poppler_data_path(const std::string &path);
1718
static void set_pdf2htmlex_data_path(const std::string &path);
19+
static void set_libmagic_data_path(const std::string &path);
1820

1921
private:
2022
static GlobalParams &instance();
@@ -25,6 +27,7 @@ class GlobalParams {
2527
std::string m_fontconfig_data_path;
2628
std::string m_poppler_data_path;
2729
std::string m_pdf2htmlex_data_path;
30+
std::string m_libmagic_data_path;
2831
};
2932

3033
} // namespace odr
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <odr/internal/libmagic/libmagic.hpp>
2+
3+
#include <odr/global_params.hpp>
4+
5+
#include <magic.h>
6+
7+
namespace odr::internal {
8+
9+
namespace {
10+
11+
void magic_deleter(const magic_t magic_cookie) {
12+
if (magic_cookie != nullptr) {
13+
magic_close(magic_cookie);
14+
}
15+
}
16+
17+
magic_t get_magic_cookie() {
18+
using Holder =
19+
std::unique_ptr<std::remove_pointer_t<magic_t>, decltype(&magic_deleter)>;
20+
static Holder magic_cookie(nullptr, &magic_deleter);
21+
22+
if (magic_cookie) {
23+
return magic_cookie.get();
24+
}
25+
26+
magic_cookie = Holder(magic_open(MAGIC_MIME_TYPE), &magic_deleter);
27+
if (!magic_cookie) {
28+
throw std::runtime_error("magic_open failed");
29+
}
30+
if (magic_load(magic_cookie.get(),
31+
GlobalParams::libmagic_data_path().c_str()) == 0) {
32+
return magic_cookie.get();
33+
}
34+
if (magic_load(magic_cookie.get(), nullptr) == 0) {
35+
return magic_cookie.get();
36+
}
37+
magic_cookie.reset();
38+
throw std::runtime_error("magic_load failed");
39+
}
40+
41+
} // namespace
42+
43+
const char *libmagic::mime_type(const std::string &path) {
44+
const magic_t magic_cookie = get_magic_cookie();
45+
return magic_file(magic_cookie, path.c_str());
46+
}
47+
48+
} // namespace odr::internal
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
namespace odr::internal::libmagic {
6+
const char *mime_type(const std::string &path);
7+
}

src/odr/internal/magic.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <odr/file.hpp>
44

55
#include <odr/internal/abstract/file.hpp>
6+
#include <odr/internal/libmagic/libmagic.hpp>
67
#include <odr/internal/util/string_util.hpp>
78

89
#include <iostream>
@@ -86,4 +87,12 @@ FileType magic::file_type(const File &file) {
8687
return file_type(*file.stream());
8788
}
8889

90+
const char *magic::mime_type(const std::string &path) {
91+
#ifdef ODR_USE_LIBMAGIC
92+
return libmagic::mime_type(path);
93+
#else
94+
return nullptr;
95+
#endif
96+
}
97+
8998
} // namespace odr::internal

src/odr/internal/magic.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class File;
1515
namespace odr::internal::magic {
1616
FileType file_type(const std::string &magic);
1717
FileType file_type(std::istream &in);
18-
FileType file_type(const internal::abstract::File &file);
18+
FileType file_type(const abstract::File &file);
1919
FileType file_type(const File &file);
20+
21+
const char *mime_type(const std::string &path);
2022
} // namespace odr::internal::magic

src/odr/internal/project_info.cpp.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ bool project_info::has_pdf2htmlex() noexcept{
2828
#endif
2929
}
3030

31+
bool project_info::has_libmagic() noexcept{
32+
#ifdef ODR_WITH_LIBMAGIC
33+
return true;
34+
#else
35+
return false;
36+
#endif
37+
}
38+
3139
const char *project_info::odr_data_path() noexcept {
3240
return "${ODR_DATA_PATH}";
3341
}
@@ -44,4 +52,8 @@ const char *project_info::pdf2htmlex_data_path() noexcept{
4452
return "${PDF2HTMLEX_DATA_DIR}";
4553
}
4654

55+
const char *project_info::libmagic_data_path() noexcept{
56+
return "${LIBMAGIC_DATA_DIR}";
57+
}
58+
4759
} // namespace odr::internal

0 commit comments

Comments
 (0)