1 unstable release

Uses old Rust 2015

0.12.3 Dec 27, 2020

#967 in Database interfaces

Download history 103/week @ 2023-10-19 80/week @ 2023-10-26 62/week @ 2023-11-02 65/week @ 2023-11-09 66/week @ 2023-11-16 38/week @ 2023-11-23 25/week @ 2023-11-30 28/week @ 2023-12-07 48/week @ 2023-12-14 36/week @ 2023-12-21 18/week @ 2023-12-28 49/week @ 2024-01-04 42/week @ 2024-01-11 30/week @ 2024-01-18 61/week @ 2024-01-25 38/week @ 2024-02-01

173 downloads per month
Used in dionysos

MIT license

22KB
382 lines

filemagic-rs

Build Status Documentation

filemagic is a Rust wrapper for libmagic, the library that supports the file command on most Unix systems. The package provides a simple Rust API for identifying files using the extensive database of magic strings that ships with libmagic. It can also load a custom database of magic strings.

Requirements

  • Rust 1.40.0 or above stable version
  • libmagic
    • macOS: brew install libmagic
    • Linux: apt install libmagic1 libmagic-dev

Usage

Adding dependency to your Cargo.toml file

filemagic = { git = "https://github.com/marirs/filemagic-rs", branch = "master" }

Using Macros

  • Using default libmagic database:
use filemagic::magic;

fn main() {
  let test_file_path = "/path/to/file";
  let magic = magic!().expect("error");
  
  println!("{}", magic.file(&test_file_path).expect("error"));
}
  • Using custom Magic database:
use filemagic::magic;

fn main() {
  let test_file_path = "/path/to/file";
  let databases = vec!["data/db-images-png"];
  
  let magic = magic!(,&databases).expect("error");
  
  println!("{}", magic.file(&test_file_path).expect("error"));
}

Using the function

  • Using the default libmagic database:
use filemagic::Magic;

fn main() {
    let test_file_path = "/path/to/file";
    // Create a new default configuration
    let cookie = Magic::open(Default::default()).expect("error");
    cookie.load::<String>(&[]).expect("error");
    let magic = cookie.file(&test_file_path).expect("error in magic");
    println!("magic= {}", magic);
}
  • Using custom Magic database:
use filemagic::Magic;

fn main() {
    // Create a new default configuration
    let cookie = Magic::open(Default::default()).expect("error");
    // Load one specific magic database
    let databases = vec!["data/db-images-png"];
    assert!(cookie.load(&databases).is_ok());

    // Recognize the magic of a test file
    let test_file_path = "data/rust-logo-128x128-blk.png";
    let expected_magic = "PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced";
    assert_eq!(cookie.file(&test_file_path).unwrap(), expected_magic);
}

To generate the docs

cargo doc --release

References:

Dependencies

~125KB