Skip to main content

detect_format

Function detect_format 

Source
pub fn detect_format(path: &Path) -> Result<MagicFileFormat, ParseError>
Expand description

Detect the format of a magic file or directory

This function examines the filesystem metadata and file contents to determine whether the path points to a text magic file, a directory, or a binary .mgc file.

§Detection Logic

  1. Check if path is a directory -> MagicFileFormat::Directory
  2. Read first 4 bytes and check for binary magic number 0xF11E041C -> MagicFileFormat::Binary
  3. Otherwise -> MagicFileFormat::Text

§Arguments

  • path - Path to the magic file or directory to detect

§Errors

Returns ParseError::IoError if the path doesn’t exist or cannot be read.

§Notes

This function only detects the format and returns it. It does not validate whether the format is supported by the parser. Higher-level code should check the returned format and decide how to handle unsupported formats (e.g., binary .mgc files).

§Examples

use libmagic_rs::parser::detect_format;
use std::path::Path;

let format = detect_format(Path::new("/usr/share/file/magic"))?;