Skip to main content

MagicDatabase

Struct MagicDatabase 

Source
pub struct MagicDatabase { /* private fields */ }
Expand description

Main interface for magic rule database

Implementations§

Source§

impl MagicDatabase

Source

pub fn with_builtin_rules() -> Result<Self>

Create a database using built-in magic rules.

Loads magic rules that are compiled into the library binary at build time from src/builtin_rules.magic. These rules provide high-confidence detection for common file types including executables (ELF, PE/DOS), archives (ZIP, TAR, GZIP), images (JPEG, PNG, GIF, BMP), and documents (PDF).

§Errors

Currently always returns Ok. In future implementations, this may return an error if the built-in rules fail to load or validate.

§Examples
use libmagic_rs::MagicDatabase;

let db = MagicDatabase::with_builtin_rules()?;
let result = db.evaluate_buffer(b"\x7fELF")?;
// Returns actual file type detection (e.g., "ELF")
Source

pub fn with_builtin_rules_and_config(config: EvaluationConfig) -> Result<Self>

Create database with built-in rules and custom configuration.

Loads built-in magic rules compiled at build time and applies the specified evaluation configuration (e.g., custom timeout settings).

§Arguments
  • config - Custom evaluation configuration to use with the built-in rules
§Errors

Returns LibmagicError if the configuration is invalid (e.g., timeout is zero).

§Examples
use libmagic_rs::{MagicDatabase, EvaluationConfig};

let config = EvaluationConfig {
    timeout_ms: Some(5000), // 5 second timeout
    ..EvaluationConfig::default()
};
let db = MagicDatabase::with_builtin_rules_and_config(config)?;
Source

pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self>

Load magic rules from a file

§Arguments
  • path - Path to the magic file to load
§Errors

Returns LibmagicError::IoError if the file cannot be read. Returns LibmagicError::ParseError if the magic file format is invalid.

§Examples
use libmagic_rs::MagicDatabase;

let db = MagicDatabase::load_from_file("magic.db")?;
Source

pub fn load_from_file_with_config<P: AsRef<Path>>( path: P, config: EvaluationConfig, ) -> Result<Self>

Load from file with custom config (e.g., timeout)

§Errors

Returns error if file cannot be read, parsed, or config is invalid

Source

pub fn evaluate_file<P: AsRef<Path>>(&self, path: P) -> Result<EvaluationResult>

Evaluate magic rules against a file

§Arguments
  • path - Path to the file to evaluate
§Errors

Returns LibmagicError::IoError if the file cannot be accessed. Returns LibmagicError::EvaluationError if rule evaluation fails.

§Examples
use libmagic_rs::MagicDatabase;

let db = MagicDatabase::load_from_file("magic.db")?;
let result = db.evaluate_file("sample.bin")?;
println!("File type: {}", result.description);
Source

pub fn evaluate_buffer(&self, buffer: &[u8]) -> Result<EvaluationResult>

Evaluate magic rules against an in-memory buffer

This method evaluates a byte buffer directly without reading from disk, which is useful for stdin input or pre-loaded data.

§Arguments
  • buffer - Byte buffer to evaluate
§Errors

Returns LibmagicError::EvaluationError if rule evaluation fails.

§Examples
use libmagic_rs::MagicDatabase;

let db = MagicDatabase::load_from_file("/usr/share/misc/magic")?;
let buffer = b"test data";
let result = db.evaluate_buffer(buffer)?;
println!("Buffer type: {}", result.description);
Source

pub fn config(&self) -> &EvaluationConfig

Returns the evaluation configuration used by this database.

This provides read-only access to the evaluation configuration for callers that need to inspect resource limits or evaluation options.

Source

pub fn source_path(&self) -> Option<&Path>

Returns the path from which magic rules were loaded.

This method returns the source path that was used to load the magic rules into this database. It is useful for debugging, logging, and tracking the origin of magic rules.

§Returns
  • Some(&Path) - If the database was loaded from a file or directory using load_from_file()
  • None - If the database was constructed programmatically or the source path was not recorded
§Examples
use libmagic_rs::MagicDatabase;

let db = MagicDatabase::load_from_file("/usr/share/misc/magic")?;
if let Some(path) = db.source_path() {
    println!("Rules loaded from: {}", path.display());
}

Trait Implementations§

Source§

impl Debug for MagicDatabase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.