#1482·magika

[magika-lib] Handle non-regular / special files (/dev/null, FIFOs, devices, sockets) as ContentType::Unknown

Author: reyammerCreated Sep 18, 2026Updated Sep 18, 2026

Context

While running the expanded Python public API test suite (PR #1458, specifically test_special_device_file), we found that magika-lib (rust/lib) does not check metadata.is_file() before opening a path in Session::identify_file (rust/lib/src/session.rs:41-50) and FeaturesOrRuled::extract_file (rust/lib/src/future.rs:59-67).

Current State in rust/lib

rust
let metadata = std::fs::symlink_metadata(&file)?;
if metadata.is_dir() {
    return Ok(FileType::Directory);
}
if metadata.is_symlink() {
    return Ok(FileType::Symlink);
}
let file = std::fs::File::open(file)?;

rust/lib only checks metadata.is_dir() and metadata.is_symlink(), and assumes any other path is a regular file (metadata.is_file()). This causes two issues on Unix special files:

  1. Character/block devices like /dev/null:
    • std::fs::File::open("/dev/null") succeeds and metadata.len() is 0, so rust/lib classifies /dev/null as ContentType::Empty (empty).
    • In Python Magika (test_special_device_file), non-regular files (!stat.S_ISREG(st_mode)) are never opened; they immediately return status = Status.OK, output.label = ContentTypeLabel.UNKNOWN, dl.label = ContentTypeLabel.UNDEFINED, score = 1.0 (i.e. FileType::Ruled(ContentType::Unknown)).
  2. Named pipes (FIFOs) and sockets:
    • Calling std::fs::File::open on a FIFO with no writer blocks indefinitely, hanging the process.
  3. Divergence between rust/cli, rust/lib, and python:
    • In PR #1464 (9096fd3), rust/cli added ensure!(metadata.is_file(), "Not a regular file") in rust/cli/src/main.rs:355, which treats /dev/null and other special files as an error rather than ContentType::Unknown.
    • Meanwhile, Python Magika treats non-regular files as a valid FileType::Ruled(ContentType::Unknown) result (status = Status.OK, output = UNKNOWN, dl = UNDEFINED).

Proposed Rust API Changes

In rust/lib (Session::identify_file and FeaturesOrRuled::extract_file), after checking metadata.is_dir() and metadata.is_symlink(), check:

rust
if !metadata.is_file() {
    return Ok(FileType::Ruled(ContentType::Unknown)); // or FeaturesOrRuled::Ruled(ContentType::Unknown)
}

without calling std::fs::File::open. This prevents magika-lib from misclassifying /dev/null as empty or hanging on FIFOs, and allows rust/pyo3 (and rust/cli, if desired) to rely directly on magika-lib without custom special-file checks in the bindings layer.