#1481·magika

[magika-lib] Support no_dereference option in Session::identify_file and FeaturesOrRuled::extract_file

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

Context

While integrating magika-lib (rust/lib) with the Python pyo3 bindings (rust/pyo3) and running the Python test suite (test_magika_module_with_symlink and test_broken_symlink), we noticed that rust/lib hardcodes no_dereference = true (std::fs::symlink_metadata).

Current State in rust/lib

In rust/lib/src/session.rs:41 (Session::identify_file) and rust/lib/src/future.rs:59 (FeaturesOrRuled::extract_file):

rust
let metadata = std::fs::symlink_metadata(&file)?;
if metadata.is_dir() {
    return Ok(FileType::Directory);
}
if metadata.is_symlink() {
    return Ok(FileType::Symlink);
}
  • std::fs::symlink_metadata never follows symlinks (lstat), so Session::identify_file always behaves as no_dereference = true and returns FileType::Symlink for every symlink (even broken symlinks).
  • However, both Python Magika(no_dereference=False) and magika-cli default to no_dereference = false (following symlinks via std::fs::metadata / stat, identifying the target file, or returning ErrorKind::NotFound if the symlink target does not exist).
  • Because rust/lib hardcodes symlink_metadata, callers (rust/cli in rust/cli/src/main.rs:344 and rust/pyo3) cannot use Session::identify_file when no_dereference = false and have to reimplement path metadata inspection themselves.

Proposed Rust API Changes

Support no_dereference: bool (defaulting to false) in magika-lib (e.g., on Session / Builder configuration, or as a parameter to Session::identify_file and FeaturesOrRuled::extract_file):

  • When no_dereference == true: call std::fs::symlink_metadata(&file).
  • When no_dereference == false: call std::fs::metadata(&file) so symlinks are followed and broken symlinks return magika::Error::IOError (ErrorKind::NotFound).