dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Magic::Context Class Reference

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).
Contextoperator= (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.

Detailed Description

RAII wrapper around a libmagic "magic_set" context.

This class manages a libmagic context used to detect file types / MIME types from:

  • an open file descriptor,
  • a filesystem path,
  • an in-memory buffer.

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 ctx; // uses Context::defaultFlags by default
if (!ctx.isReady())
throw std::runtime_error(ctx.lastError());
std::string t1 = ctx.get("/bin/ls");
std::string t2 = ctx.get(fd);
std::string t3 = ctx.get(std::span<uint8_t const>(data, size)); // if you have const bytes, copy to uint8_t or adjust overloads
RAII wrapper around a libmagic "magic_set" context.
Definition Context.hpp:49
std::string get(int fd) const
Identify content from an open file descriptor.
bool isReady() const noexcept
Whether the context is initialized and ready to be used.
Definition Context.hpp:80
std::string lastError() const
Retrieve the last error message from libmagic.
Note
"Magic" refers to the Linux libmagic library (the one behind the file command).

Constructor & Destructor Documentation

◆ Context() [1/2]

dfx::Magic::Context::Context ( int flags = -1,
fs::path const & magicDb = {} )

Construct a libmagic context and load the magic database.

Parameters
flagslibmagic flags passed to magic_open(). If flags is -1 (default), the value of defaultFlags is used.
magicDbOptional 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.

◆ Context() [2/2]

dfx::Magic::Context::Context ( Context && other)
noexcept

Move-construct a context.

◆ ~Context()

dfx::Magic::Context::~Context ( )

Destroy the context and release the underlying libmagic handle.

Member Function Documentation

◆ DISABLE_COPY()

dfx::Magic::Context::DISABLE_COPY ( Context )

Copying is disabled (single owner of the libmagic handle).

◆ get() [1/3]

std::string dfx::Magic::Context::get ( fs::path const & path) const

Identify content from a filesystem path.

Parameters
pathPath to an existing file.
Returns
A description string as produced by libmagic, or an empty string on failure

◆ get() [2/3]

std::string dfx::Magic::Context::get ( int fd) const

Identify content from an open file descriptor.

Parameters
fdAn open file descriptor. The descriptor must remain valid for the duration of the call.
Returns
A description string as produced by libmagic (e.g. "ELF 64-bit LSB executable ...") or an empty string on failure

◆ get() [3/3]

std::string dfx::Magic::Context::get ( std::span< uint8_t > const & data) const

Identify content from an in-memory buffer.

Parameters
dataByte span containing the data to analyze.
Returns
A description string as produced by libmagic, or an empty string on failure

◆ isReady()

bool dfx::Magic::Context::isReady ( ) const
inlinenoexcept

Whether the context is initialized and ready to be used.

A context is considered ready when:

  • the underlying libmagic handle is non-null, and
  • the magic database was successfully loaded
Returns
true if ready; false otherwise.

◆ lastError()

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.

Returns
Human-readable error string describing the last libmagic failure.
Note
If the context is not ready, this is the preferred way to diagnose why.

◆ operator=()

Context & dfx::Magic::Context::operator= ( Context && other)
noexcept

Move-assign a context.

Member Data Documentation

◆ defaultFlags

int dfx::Magic::Context::defaultFlags
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


The documentation for this class was generated from the following file: