3 unstable releases

0.13.1 Oct 9, 2024
0.12.4 Oct 9, 2024
0.12.3 Dec 27, 2020

#390 in Database interfaces

Download history 43/week @ 2024-08-19 77/week @ 2024-08-26 121/week @ 2024-09-02 89/week @ 2024-09-09 50/week @ 2024-09-16 73/week @ 2024-09-23 57/week @ 2024-09-30 399/week @ 2024-10-07 151/week @ 2024-10-14 85/week @ 2024-10-21 80/week @ 2024-10-28 44/week @ 2024-11-04 15/week @ 2024-11-11 28/week @ 2024-11-18 51/week @ 2024-11-25 70/week @ 2024-12-02

166 downloads per month
Used in 3 crates (2 directly)

MIT license

1MB
18K SLoC

C 16K SLoC // 0.1% comments Automake 643 SLoC // 0.0% comments Rust 528 SLoC // 0.0% comments Python 358 SLoC // 0.2% comments M4 92 SLoC // 0.3% comments Shell 14 SLoC // 0.6% comments

Contains (Zip file, 15KB) file/tests/HWP2016.hwpx.zip.testfile, (obscure autoconf code, 9KB) file/configure.ac, (Cab file, 10KB) file/tests/HWP2016.hwp.testfile, (Zip file, 9KB) file/tests/escapevel.testfile, (Zip file, 4KB) file/tests/issue311docx.testfile, (Zip file, 5KB) file/tests/issue359xlsx.testfile and 1 more.

filemagic-rs

Crates.io Documentation Build Status

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 = "0.13.1"

vendored

The vendored feature uses the cc crate to compile and static link a vendored version of libmagic, currently based on 5.45.

Adding dependency to your Cargo.toml file

filemagic = { version = "0.13.1", features = ["vendored"] }

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