| Crates.io | magic-db |
| lib.rs | magic-db |
| version | 0.4.1 |
| created_at | 2025-11-14 15:37:40.676206+00 |
| updated_at | 2026-01-19 16:56:56.892094+00 |
| description | This crate provides a precompiled magic database for file type identification. It allows any program to perform magic file detection without the burden of compiling rules from source. |
| homepage | |
| repository | https://github.com/qjerome/magic-rs/ |
| max_upload_size | |
| id | 1933067 |
| size | 1,890,230 |
magic-db: Precompiled Magic Rules DatabaseA precompiled database of file type detection rules based on the original libmagic project,
optimized and adapted for use with pure-magic.
This crate provides ready-to-use file type detection capabilities without requiring external rule files.
libmagic rulesmagic_db::global(), a lazily-initialized, process-wide MagicDb.
This provides a convenient singleton but is optional. If you need explicit lifetime
control or multiple independent instances, use magic_db::load() instead.Add magic-db to your Cargo.toml:
[dependencies]
magic-db = "0.1" # Replace with the latest version
pure-magic = "0.1" # Required peer dependency
use std::fs::File;
use std::env::current_exe;
fn main() -> Result<(), pure_magic::Error> {
// Open the precompiled database
let db = magic_db::load()?;
// Use it to detect file types
let mut file = File::open(current_exe()?)?;
let magic = db.first_magic(&mut file, None)?;
assert!(!magic.is_default());
println!("File type: {}", magic.message());
println!("MIME type: {}", magic.mime_type());
Ok(())
}
The crate provides a convenience global database via the
global feature. This is process-wide, lazily initialized, and
kept alive until program termination.
Enable it in Cargo.toml:
magic_db = { version = "0.1", features = ["global"] }
Then use it like this:
use magic_db::global;
let db = global().unwrap();
Note: Use the global feature only if you want a single, shared
database. For multiple independent instances or explicit lifetime
management, use magic_db::load().
This database contains slightly modified versions of the original libmagic rules that are available
in the src/magdir directory of this repository.
Some of the rules have been:
pure-magic parserThe database intentionally excludes the der rules (ASN.1/DER encoding rules) because:
pure-magic parser doesn't support (yet) the specific DER test types
implemented in the original libmagicThe source magic rules are available in the repository at:
src/magdir
You can:
This project is licensed under the GPL-3.0 License.
pure-magic: The core file type detection librarymagic-embed: The macro used to create this databasemagic: Expected magic rule format