![]() |
dfx 0.1.0
Linux-based dynamic dataflow executor
|
RAII wrapper around a libmagic "magic_set" context. More...
#include <dfx-magic/Context.hpp>
Public Member Functions | |
| Context (int flags=-1, fs::path const &magicDb={}) | |
| Construct a libmagic context and load the magic database. | |
| Context (Context &&other) noexcept | |
| Move-construct a context. | |
| ~Context () | |
| Destroy the context and release the underlying libmagic handle. | |
| DISABLE_COPY (Context) | |
| Copying is disabled (single owner of the libmagic handle). | |
| Context & | operator= (Context &&other) noexcept |
| Move-assign a context. | |
| bool | isReady () const noexcept |
| Whether the context is initialized and ready to be used. | |
| std::string | lastError () const |
| Retrieve the last error message from libmagic. | |
| std::string | get (int fd) const |
| Identify content from an open file descriptor. | |
| std::string | get (fs::path const &path) const |
| Identify content from a filesystem path. | |
| std::string | get (std::span< uint8_t > const &data) const |
| Identify content from an in-memory buffer. | |
Static Public Attributes | |
| static int | defaultFlags |
Default libmagic flags used when the constructor argument flags is -1. | |
RAII wrapper around a libmagic "magic_set" context.
This class manages a libmagic context used to detect file types / MIME types from:
It owns the underlying libmagic handle and releases it in the destructor. Copying is disabled (libmagic contexts are not trivially copyable), but moving is supported.
Typical usage:
| dfx::Magic::Context::Context | ( | int | flags = -1, |
| fs::path const & | magicDb = {} ) |
Construct a libmagic context and load the magic database.
| flags | libmagic flags passed to magic_open(). If flags is -1 (default), the value of defaultFlags is used. |
| magicDb | Optional path to a specific magic database file (or directory, depending on how your implementation loads it). If empty, the system default database resolution is used (typically magic_load(ctx, nullptr)). |
After construction, check isReady() to know whether initialization and database loading succeeded.
|
noexcept |
Move-construct a context.
| dfx::Magic::Context::~Context | ( | ) |
Destroy the context and release the underlying libmagic handle.
| dfx::Magic::Context::DISABLE_COPY | ( | Context | ) |
Copying is disabled (single owner of the libmagic handle).
| std::string dfx::Magic::Context::get | ( | fs::path const & | path | ) | const |
Identify content from a filesystem path.
| path | Path to an existing file. |
| std::string dfx::Magic::Context::get | ( | int | fd | ) | const |
Identify content from an open file descriptor.
| fd | An open file descriptor. The descriptor must remain valid for the duration of the call. |
| std::string dfx::Magic::Context::get | ( | std::span< uint8_t > const & | data | ) | const |
Identify content from an in-memory buffer.
| data | Byte span containing the data to analyze. |
|
inlinenoexcept |
Whether the context is initialized and ready to be used.
A context is considered ready when:
| std::string dfx::Magic::Context::lastError | ( | ) | const |
Retrieve the last error message from libmagic.
Typically wraps magic_error(_ctx) when available, or returns an empty string if no detailed error is available.
|
static |
Default libmagic flags used when the constructor argument flags is -1.
This value is intended to be configured by the application (e.g. enabling MIME output, following symlinks, preserving access time, etc.). By default it is set to MAGIC_MIME_TYPE | MAGIC_NO_CHECK_COMPRESS | MAGIC_NO_CHECK_ELF | MAGIC_NO_CHECK_ENCODING