Libmagicxx v10.0.3
A modern C++23 wrapper for libmagic — the library that powers the Unix file command.
Loading...
Searching...
No Matches
magic.hpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: Copyright (c) 2022-2026 Oğuz Toraman <oguz.toraman@tutanota.com> */
2/* SPDX-License-Identifier: LGPL-3.0-only */
3
70
71#ifndef MAGIC_HPP
72#define MAGIC_HPP
73
74#include <bitset>
75#include <expected>
76#include <map>
77#include <memory>
78#include <optional>
79#include <string_view>
80#include <vector>
81
82#include "magic_exception.hpp"
83#include "progress_tracker.hpp"
84#include "utility.hpp"
85
101namespace Recognition {
216class Magic {
217public:
223 using FlagsMaskT = std::bitset<30uz>;
224
230 using FileTypeT = std::string;
231
237 using ErrorMessageT = std::string;
238
244 using ExpectedFileTypeT = std::expected<FileTypeT, ErrorMessageT>;
245
251 using FileTypeMapT = std::map<std::filesystem::path, FileTypeT>;
252
258 using FileTypeEntryT = FileTypeMapT::value_type;
259
265 using ExpectedFileTypeMapT = std::map<
266 std::filesystem::path,
268 >;
269
275 using ExpectedFileTypeEntryT = ExpectedFileTypeMapT::value_type;
276
285
314 enum Flags : unsigned long long {
315 /* clang-format off */
316 None = 0ULL,
317 Debug = 1ULL << 0,
318 Symlink = 1ULL << 1,
319 Compress = 1ULL << 2,
320 Devices = 1ULL << 3,
321 MimeType = 1ULL << 4,
322 ContinueSearch = 1ULL << 5,
323 CheckDatabase = 1ULL << 6,
324 PreserveAtime = 1ULL << 7,
325 Raw = 1ULL << 8,
326 Error = 1ULL << 9,
327 MimeEncoding = 1ULL << 10,
328 Mime = 1ULL << 11,
329 Apple = 1ULL << 12,
330 Extension = 1ULL << 13,
331 CompressTransp = 1ULL << 14,
332 NoCompressFork = 1ULL << 15,
333 Nodesc = 1ULL << 16,
334 NoCheckCompress = 1ULL << 17,
335 NoCheckTar = 1ULL << 18,
336 NoCheckSoft = 1ULL << 19,
337 NoCheckApptype = 1ULL << 20,
338 NoCheckElf = 1ULL << 21,
339 NoCheckText = 1ULL << 22,
340 NoCheckCdf = 1ULL << 23,
341 NoCheckCsv = 1ULL << 24,
342 NoCheckTokens = 1ULL << 25,
343 NoCheckEncoding = 1ULL << 26,
344 NoCheckJson = 1ULL << 27,
345 NoCheckSimh = 1ULL << 28,
346 NoCheckBuiltin = 1ULL << 29
347 /* clang-format on */
348 };
349
381 enum class Parameters : std::size_t {
382 /* clang-format off */
383 IndirMax = 0uz,
384 NameMax = 1uz,
388 RegexMax = 5uz,
389 BytesMax = 6uz,
393 /* clang-format on */
394 };
395
401 using FlagsContainerT = std::vector<Flags>;
402
408 using ParameterValueMapT = std::map<Parameters, std::size_t>;
409
415 using ParameterValueT = ParameterValueMapT::value_type;
416
431 static std::string_view DEFAULT_DATABASE_FILE;
432
442
462 Magic() noexcept;
463
497 explicit Magic(
498 FlagsMaskT flags_mask,
499 const std::filesystem::path& database_file = DEFAULT_DATABASE_FILE
500 );
501
525 Magic(
526 FlagsMaskT flags_mask,
527 const std::nothrow_t& tag,
528 const std::filesystem::path& database_file = DEFAULT_DATABASE_FILE
529 ) noexcept;
530
557 explicit Magic(
558 const FlagsContainerT& flags_container,
559 const std::filesystem::path& database_file = DEFAULT_DATABASE_FILE
560 );
561
578 Magic(
579 const FlagsContainerT& flags_container,
580 const std::nothrow_t& tag,
581 const std::filesystem::path& database_file = DEFAULT_DATABASE_FILE
582 ) noexcept;
583
585
595
613 Magic(Magic&& other) noexcept;
614
621 Magic(const Magic&) = delete;
622
635 Magic& operator=(Magic&& other) noexcept;
636
643 Magic& operator=(const Magic&) = delete;
644
653
655
665
688 [[nodiscard]] operator bool() const noexcept;
689
712 [[nodiscard]] static bool Check(
713 const std::filesystem::path& database_file = DEFAULT_DATABASE_FILE
714 ) noexcept;
715
717
727
750 void Close() noexcept;
751
775 [[nodiscard]] static bool Compile(
776 const std::filesystem::path& database_file = DEFAULT_DATABASE_FILE
777 ) noexcept;
778
780
790
810 [[nodiscard]] FlagsContainerT GetFlags() const;
811
821 [[nodiscard]] std::optional<FlagsContainerT> GetFlags(
822 const std::nothrow_t& tag
823 ) const noexcept;
824
826
836
858 [[nodiscard]] std::size_t GetParameter(Parameters parameter) const;
859
870 [[nodiscard]] std::optional<std::size_t> GetParameter(
871 Parameters parameter,
872 const std::nothrow_t& tag
873 ) const noexcept;
874
896 [[nodiscard]] ParameterValueMapT GetParameters() const;
897
907 [[nodiscard]] std::optional<ParameterValueMapT> GetParameters(
908 const std::nothrow_t& tag
909 ) const noexcept;
910
912
922
938 [[nodiscard]] static std::string GetVersion() noexcept;
939
941
951
978 [[nodiscard]] FileTypeT IdentifyFile(
979 const std::filesystem::path& path
980 ) const;
981
1003 [[nodiscard]] ExpectedFileTypeT IdentifyFile(
1004 const std::filesystem::path& path,
1005 const std::nothrow_t& tag
1006 ) const noexcept;
1007
1038 const std::filesystem::path& directory,
1039 std::filesystem::directory_options option = std::filesystem::
1040 directory_options::follow_directory_symlink
1041 ) const
1042 {
1043 return IdentifyDirectoryImpl(directory, option);
1044 }
1045
1083 const std::filesystem::path& directory,
1084 ProgressTrackerT progress_tracker,
1085 std::filesystem::directory_options option = std::filesystem::
1086 directory_options::follow_directory_symlink
1087 ) const
1088 {
1089 return IdentifyDirectoryImpl(directory, option, progress_tracker);
1090 }
1091
1106 const std::filesystem::path& directory,
1107 const std::nothrow_t& tag,
1108 std::filesystem::directory_options option = std::filesystem::
1109 directory_options::follow_directory_symlink
1110 ) const noexcept
1111 {
1112 return IdentifyDirectoryImpl(directory, tag, option);
1113 }
1114
1128 const std::filesystem::path& directory,
1129 const std::nothrow_t& tag,
1130 ProgressTrackerT progress_tracker,
1131 std::filesystem::directory_options option = std::filesystem::
1132 directory_options::follow_directory_symlink
1133 ) const noexcept
1134 {
1135 return IdentifyDirectoryImpl(directory, tag, option, progress_tracker);
1136 }
1137
1168 const Utility::FileContainer auto& files
1169 ) const
1170 {
1171 return IdentifyContainerImpl(
1172 {std::ranges::begin(files), std::ranges::end(files)}
1173 );
1174 }
1175
1190 const Utility::FileContainer auto& files,
1191 ProgressTrackerT progress_tracker
1192 ) const
1193 {
1194 return IdentifyContainerImpl(
1195 {std::ranges::begin(files), std::ranges::end(files)},
1196 progress_tracker
1197 );
1198 }
1199
1211 const Utility::FileContainer auto& files,
1212 const std::nothrow_t& tag
1213 ) const noexcept
1214 {
1215 return IdentifyContainerImpl(
1216 {std::ranges::begin(files), std::ranges::end(files)},
1217 tag
1218 );
1219 }
1220
1233 const Utility::FileContainer auto& files,
1234 const std::nothrow_t& tag,
1235 ProgressTrackerT progress_tracker
1236 ) const noexcept
1237 {
1238 return IdentifyContainerImpl(
1239 {std::ranges::begin(files), std::ranges::end(files)},
1240 tag,
1241 progress_tracker
1242 );
1243 }
1244
1246
1256
1277 [[nodiscard]] bool IsDatabaseLoaded() const noexcept;
1278
1302 [[nodiscard]] bool IsOpen() const noexcept;
1303
1334 [[nodiscard]] bool IsValid() const noexcept;
1335
1337
1347
1378 void LoadDatabaseFile(
1379 const std::filesystem::path& database_file = DEFAULT_DATABASE_FILE
1380 );
1381
1394 [[nodiscard]] bool LoadDatabaseFile(
1395 const std::nothrow_t& tag,
1396 const std::filesystem::path& database_file = DEFAULT_DATABASE_FILE
1397 ) noexcept;
1398
1400
1410
1442 void Open(FlagsMaskT flags_mask);
1443
1454 [[nodiscard]] bool Open(
1455 FlagsMaskT flags_mask,
1456 const std::nothrow_t& tag
1457 ) noexcept;
1458
1468 void Open(const FlagsContainerT& flags_container);
1469
1480 [[nodiscard]] bool Open(
1481 const FlagsContainerT& flags_container,
1482 const std::nothrow_t& tag
1483 ) noexcept;
1484
1486
1496
1515 void SetFlags(FlagsMaskT flags_mask);
1516
1527 [[nodiscard]] bool SetFlags(
1528 FlagsMaskT flags_mask,
1529 const std::nothrow_t& tag
1530 ) noexcept;
1531
1542 void SetFlags(const FlagsContainerT& flags_container);
1543
1554 [[nodiscard]] bool SetFlags(
1555 const FlagsContainerT& flags_container,
1556 const std::nothrow_t& tag
1557 ) noexcept;
1558
1560
1570
1590 void SetParameter(Parameters parameter, std::size_t value);
1591
1603 [[nodiscard]] bool SetParameter(
1604 Parameters parameter,
1605 std::size_t value,
1606 const std::nothrow_t& tag
1607 ) noexcept;
1608
1629 void SetParameters(const ParameterValueMapT& parameters);
1630
1641 [[nodiscard]] bool SetParameters(
1642 const ParameterValueMapT& parameters,
1643 const std::nothrow_t& tag
1644 ) noexcept;
1645
1647
1648private:
1650 class MagicPrivate;
1651
1653 std::unique_ptr<MagicPrivate> m_impl;
1654
1656 using DefaultFileContainerT = std::vector<std::filesystem::path>;
1657
1659 [[nodiscard]] FileTypeMapT IdentifyDirectoryImpl(
1660 const std::filesystem::path& directory,
1661 std::filesystem::directory_options option,
1662 ProgressTrackerT progress_tracker = Utility::MakeSharedProgressTracker()
1663 ) const;
1664
1667 const std::filesystem::path& directory,
1668 const std::nothrow_t& tag,
1669 std::filesystem::directory_options option,
1670 ProgressTrackerT progress_tracker = Utility::MakeSharedProgressTracker()
1671 ) const noexcept;
1672
1674 [[nodiscard]] FileTypeMapT IdentifyContainerImpl(
1675 const DefaultFileContainerT& files,
1676 ProgressTrackerT progress_tracker = Utility::MakeSharedProgressTracker()
1677 ) const;
1678
1681 const DefaultFileContainerT& files,
1682 const std::nothrow_t& tag,
1683 ProgressTrackerT progress_tracker = Utility::MakeSharedProgressTracker()
1684 ) const noexcept;
1685
1687 friend std::string ToString(Flags);
1688
1690 friend std::string ToString(Parameters);
1691};
1692
1703
1725[[nodiscard]] std::string ToString(
1726 const Magic::FileTypeEntryT& file_type_entry,
1727 const std::string& type_separator = " -> "
1728);
1729
1754[[nodiscard]] std::string ToString(
1755 const Magic::FileTypeMapT& file_type_map,
1756 const std::string& type_separator = " -> ",
1757 const std::string& file_separator = "\n"
1758);
1759
1779[[nodiscard]] std::string ToString(
1780 const Magic::ExpectedFileTypeT& expected_file_type
1781);
1782
1798[[nodiscard]] std::string ToString(
1799 const Magic::ExpectedFileTypeEntryT& expected_file_type_entry,
1800 const std::string& type_separator = " -> "
1801);
1802
1819[[nodiscard]] std::string ToString(
1820 const Magic::ExpectedFileTypeMapT& expected_file_type_map,
1821 const std::string& type_separator = " -> ",
1822 const std::string& file_separator = "\n"
1823);
1824
1844[[nodiscard]] std::string ToString(Magic::Flags flag);
1845
1867[[nodiscard]] std::string ToString(
1868 const Magic::FlagsContainerT& flags,
1869 const std::string& separator = ", "
1870);
1871
1891[[nodiscard]] std::string ToString(Magic::Parameters parameter);
1892
1914[[nodiscard]] std::string ToString(
1915 const Magic::ParameterValueT& parameter_value,
1916 const std::string& value_separator = ": "
1917);
1918
1942[[nodiscard]] std::string ToString(
1943 const Magic::ParameterValueMapT& parameters,
1944 const std::string& value_separator = ": ",
1945 const std::string& parameter_separator = ", "
1946);
1947
1949} /* namespace Recognition */
1950
1951#endif /* MAGIC_HPP */
PIMPL implementation class for Magic.
Definition magic.cpp:222
static std::string_view DEFAULT_DATABASE_FILE
Path to the default magic database file.
Definition magic.hpp:431
std::vector< Flags > FlagsContainerT
Container type holding a collection of Magic::Flags.
Definition magic.hpp:401
ExpectedFileTypeMapT::value_type ExpectedFileTypeEntryT
Key-value pair representing a single file and its expected file type result.
Definition magic.hpp:275
std::string ErrorMessageT
String type representing an error message from file identification.
Definition magic.hpp:237
FileTypeMapT::value_type FileTypeEntryT
Key-value pair representing a single file and its detected type.
Definition magic.hpp:258
std::expected< FileTypeT, ErrorMessageT > ExpectedFileTypeT
Result type for file identification, containing either a file type or an error message.
Definition magic.hpp:244
FileTypeMapT IdentifyDirectoryImpl(const std::filesystem::path &directory, std::filesystem::directory_options option, ProgressTrackerT progress_tracker=Utility::MakeSharedProgressTracker()) const
Implementation for directory identification (throwing version).
Definition magic.cpp:2550
std::map< std::filesystem::path, FileTypeT > FileTypeMapT
Map from file paths to their detected types.
Definition magic.hpp:251
Utility::SharedProgressTrackerT ProgressTrackerT
Alias for a shared pointer to a progress tracker used for monitoring file identification progress.
Definition magic.hpp:284
std::unique_ptr< MagicPrivate > m_impl
Pointer to the implementation (Pimpl idiom).
Definition magic.hpp:1653
std::map< std::filesystem::path, ExpectedFileTypeT > ExpectedFileTypeMapT
Map from file paths to expected file type results (success or error).
Definition magic.hpp:265
friend std::string ToString(Flags)
Friend declaration for ToString(Flags) free function.
Definition magic.cpp:2355
std::map< Parameters, std::size_t > ParameterValueMapT
Map from Magic::Parameters to their corresponding values.
Definition magic.hpp:408
std::bitset< 30uz > FlagsMaskT
Bitmask type representing a set of Magic::Flags used to configure the Magic instance.
Definition magic.hpp:223
std::vector< std::filesystem::path > DefaultFileContainerT
Default container type for file paths used in implementation.
Definition magic.hpp:1656
ParameterValueMapT::value_type ParameterValueT
Key-value pair representing a single parameter and its value.
Definition magic.hpp:415
std::string FileTypeT
String type representing the detected type of a file.
Definition magic.hpp:230
FileTypeMapT IdentifyContainerImpl(const DefaultFileContainerT &files, ProgressTrackerT progress_tracker=Utility::MakeSharedProgressTracker()) const
Implementation for container identification (throwing version).
Definition magic.cpp:2592
Concept for containers that can hold filesystem paths.
Definition utility.hpp:250
Magic() noexcept
Default constructor. Creates an unopened Magic instance.
Definition magic.cpp:2421
void LoadDatabaseFile(const std::filesystem::path &database_file=DEFAULT_DATABASE_FILE)
Load a magic database file.
Definition magic.cpp:2638
FileTypeT IdentifyFile(const std::filesystem::path &path) const
Identify the type of a single file.
Definition magic.cpp:2530
ExpectedFileTypeMapT IdentifyFiles(const std::filesystem::path &directory, const std::nothrow_t &tag, std::filesystem::directory_options option=std::filesystem::directory_options::follow_directory_symlink) const noexcept
Identify all files in a directory (noexcept version).
Definition magic.hpp:1105
FileTypeMapT IdentifyFiles(const std::filesystem::path &directory, ProgressTrackerT progress_tracker, std::filesystem::directory_options option=std::filesystem::directory_options::follow_directory_symlink) const
Identify all files in a directory with progress tracking.
Definition magic.hpp:1082
ExpectedFileTypeMapT IdentifyFiles(const Utility::FileContainer auto &files, const std::nothrow_t &tag, ProgressTrackerT progress_tracker) const noexcept
Identify multiple files with progress tracking (noexcept version).
Definition magic.hpp:1232
FileTypeMapT IdentifyFiles(const Utility::FileContainer auto &files) const
Identify multiple files from a container.
Definition magic.hpp:1167
ExpectedFileTypeMapT IdentifyFiles(const std::filesystem::path &directory, const std::nothrow_t &tag, ProgressTrackerT progress_tracker, std::filesystem::directory_options option=std::filesystem::directory_options::follow_directory_symlink) const noexcept
Identify all files in a directory with progress tracking (noexcept version).
Definition magic.hpp:1127
FileTypeMapT IdentifyFiles(const std::filesystem::path &directory, std::filesystem::directory_options option=std::filesystem::directory_options::follow_directory_symlink) const
Identify all files in a directory.
Definition magic.hpp:1037
ExpectedFileTypeMapT IdentifyFiles(const Utility::FileContainer auto &files, const std::nothrow_t &tag) const noexcept
Identify multiple files from a container (noexcept version).
Definition magic.hpp:1210
FileTypeMapT IdentifyFiles(const Utility::FileContainer auto &files, ProgressTrackerT progress_tracker) const
Identify multiple files with progress tracking.
Definition magic.hpp:1189
FlagsContainerT GetFlags() const
Get the current flags.
Definition magic.cpp:2488
void SetFlags(FlagsMaskT flags_mask)
Set new flags for the Magic instance.
Definition magic.cpp:2674
void Close() noexcept
Close the Magic instance.
Definition magic.cpp:2476
static bool Compile(const std::filesystem::path &database_file=DEFAULT_DATABASE_FILE) noexcept
Compile a magic database file.
Definition magic.cpp:2481
Flags
Flags for configuring Magic behavior.
Definition magic.hpp:314
Parameters
Parameters for tuning Magic behavior limits.
Definition magic.hpp:381
@ Mime
Definition magic.hpp:328
@ NoCheckCompress
Definition magic.hpp:334
@ NoCheckCdf
Definition magic.hpp:340
@ Devices
Definition magic.hpp:320
@ NoCheckSimh
Definition magic.hpp:345
@ NoCheckJson
Definition magic.hpp:344
@ Apple
Definition magic.hpp:329
@ NoCheckTar
Definition magic.hpp:335
@ Extension
Definition magic.hpp:330
@ NoCheckEncoding
Definition magic.hpp:343
@ Compress
Definition magic.hpp:319
@ NoCheckApptype
Definition magic.hpp:337
@ NoCheckTokens
Definition magic.hpp:342
@ NoCheckElf
Definition magic.hpp:338
@ Raw
Definition magic.hpp:325
@ CheckDatabase
Definition magic.hpp:323
@ ContinueSearch
Definition magic.hpp:322
@ MimeEncoding
Definition magic.hpp:327
@ Error
Definition magic.hpp:326
@ NoCheckCsv
Definition magic.hpp:341
@ NoCheckText
Definition magic.hpp:339
@ Nodesc
Definition magic.hpp:333
@ CompressTransp
Definition magic.hpp:331
@ Symlink
Definition magic.hpp:318
@ Debug
Definition magic.hpp:317
@ None
Definition magic.hpp:316
@ PreserveAtime
Definition magic.hpp:324
@ NoCompressFork
Definition magic.hpp:332
@ MimeType
Definition magic.hpp:321
@ NoCheckBuiltin
Definition magic.hpp:346
@ NoCheckSoft
Definition magic.hpp:336
@ IndirMax
Definition magic.hpp:383
@ ElfShnumMax
Definition magic.hpp:386
@ EncodingMax
Definition magic.hpp:390
@ ElfShsizeMax
Definition magic.hpp:391
@ NameMax
Definition magic.hpp:384
@ MagWarnMax
Definition magic.hpp:392
@ ElfPhnumMax
Definition magic.hpp:385
@ RegexMax
Definition magic.hpp:388
@ BytesMax
Definition magic.hpp:389
@ ElfNotesMax
Definition magic.hpp:387
std::shared_ptr< ProgressTracker > SharedProgressTrackerT
Shared pointer type for ProgressTracker.
Definition progress_tracker.hpp:405
void Open(FlagsMaskT flags_mask)
Open Magic with specified flags.
Definition magic.cpp:2651
ParameterValueMapT GetParameters() const
Get all parameter values.
Definition magic.cpp:2513
std::size_t GetParameter(Parameters parameter) const
Get the value of a specific parameter.
Definition magic.cpp:2500
void SetParameter(Parameters parameter, std::size_t value)
Set a single parameter value.
Definition magic.cpp:2697
void SetParameters(const ParameterValueMapT &parameters)
Set multiple parameter values.
Definition magic.cpp:2711
bool IsValid() const noexcept
Check if the Magic instance is valid for file identification.
Definition magic.cpp:2633
bool IsOpen() const noexcept
Check if the Magic instance is open.
Definition magic.cpp:2628
bool IsDatabaseLoaded() const noexcept
Check if a magic database is loaded.
Definition magic.cpp:2623
static std::string GetVersion() noexcept
Get the libmagic library version.
Definition magic.cpp:2525
static bool Check(const std::filesystem::path &database_file=DEFAULT_DATABASE_FILE) noexcept
Check magic database file for validity.
Definition magic.cpp:2469
Exception hierarchy for the Magic file identification library.
Utility components for the libmagicxx library.
Definition percentage.hpp:39
Root namespace for the libmagicxx library.
Thread-safe progress tracking for batch file operations.
Utility concepts, classes, and functions for the Magic library.